Code Monkey home page Code Monkey logo

Comments (6)

pepyakin avatar pepyakin commented on May 22, 2024

That is very good idea actually!

Why?
Because result of invoking of a function might yield only two outcomes:

  • return a result
  • trap

However, the docs at the invoke_export actually give a glimpse of a problem:

Returns `Err` if:

- there are no export with a given name or this export is not a function,
- given arguments doesn't match to function signature,
- trap occured at the execution time,

So in other words, today, we have invoke_index that returns an wasmi::Error which includes various variants that just can't happen in result of invoking a function.

from wasmi.

pepyakin avatar pepyakin commented on May 22, 2024

First step to implement this is in #29

from wasmi.

bddap avatar bddap commented on May 22, 2024

How does this look for a start?

instance
    .export_by_name(func_name)?
    .as_func()?
    .with_externals(&mut NopExternals)
    .with_args(&args)?
    .invoke()
    .ok()?;

export_by_name() -> Option<ExternVal> and as_func() -> Option<&FuncRef> already exist.

The rest could be implemented by adding the following to func.rs:

impl FuncRef {
    pub fn with_externals<'a, E: Externals>(
        &'a self,
        externals: &'a mut E,
    ) -> BoundExternalsFunc<'a, E> {
        BoundExternalsFunc {
            func_instance: self,
            externals,
        }
    }
}

/// First step in chained function calling pattern.                                                                                  
pub struct BoundExternalsFunc<'a, E: Externals + 'a> {
    func_instance: &'a FuncRef,
    externals: &'a mut E,
}

impl<'a, E: Externals> BoundExternalsFunc<'a, E> {
    pub fn with_args(self, args: &'a [RuntimeValue]) -> Option<BoundFunc<'a, E>> {
        BoundFunc::new(self.func_instance, self.externals, args)
    }
}

/// FuncRef with verified arguments.                                                                                                 
pub struct BoundFunc<'a, E: Externals + 'a> {
    func_instance: &'a FuncRef,
    externals: &'a mut E,
    args: &'a [RuntimeValue],
}

impl<'a, E: Externals> BoundFunc<'a, E> {
    fn new(
        func_instance: &'a FuncRef,
        externals: &'a mut E,
        args: &'a [RuntimeValue],
    ) -> Option<BoundFunc<'a, E>> {
        check_function_args(func_instance.signature(), &args).ok()?;
        Some(BoundFunc {
            func_instance,
            externals,
            args,
        })
    }

    pub fn invoke(&mut self) -> Result<Option<RuntimeValue>, Trap> {
        FuncInstance::invoke(&self.func_instance, self.args, self.externals)
    }
}

from wasmi.

bddap avatar bddap commented on May 22, 2024

I can submit a pr, just checking first to see if this is approach it useful.

from wasmi.

pepyakin avatar pepyakin commented on May 22, 2024

Hello, @bddap ! I'm very sorry for the delay and for that I missed your comment.

Yeah, it is a good start. But we can go beyond that. For example, there is a related problem with limits (#51). And we might want to add other configuration options.

Can you see an approach that can incorporate such changes?

from wasmi.

Robbepop avatar Robbepop commented on May 22, 2024

This issue is no longer relevant due to the fact that the current wasmi version is using Wasmtime based API which mostly is pretty well designed already.

from wasmi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.