hybridgroup / mechanoid

Mechanoid is a framework for WebAssembly applications on embedded systems and IoT devices.
https://mechanoid.io
Apache License 2.0
174 stars 8 forks source link

Meet host-defined functions registry #6

Closed orsinium closed 7 months ago

orsinium commented 7 months ago

Both wasman and wazero allow to define functions that accept a slice of raw ValueType as input and return a slice of raw ValueType as output. In both cases, the raw representation is the same and uses uint64 as the underlying type. Also, wazero requires all functions in a module to be defined in one go.

To solve code duplication issues and avoid using reflection in wazero, this PR introduces a Modules registry:

modules := funcs.Modules{
    "env": {
        "hello": funcs.F00(func() {
            println("I'm alive!")
        }),
    },
}

The registry is not used in any way in this PR yet but it will be used something like this (which will replace the current DefineFunc):

interpreter.Modules(modules)
deadprogram commented 7 months ago

I'd really like to preserve the ability to infer the call function to use based on the signature, e.g.

modules := funcs.Modules{
    "env": {
        "hello": helloFunc,
                "add": addFunc,
    },
}

func helloFunc() {
        println("I'm alive!")
}

func addFunc(x, y int32) int32 {
        return x+y
}
orsinium commented 7 months ago

I'd really like to preserve the ability to infer the call function to use based on the signature, e.g.

Can it be done without reflect?

What I do is not much different, you just need to wrap it which isn't too verbose:

modules := funcs.Modules{
    "env": {
        "hello": funcs.F00(helloFunc),
                "add": funcs.F21(addFunc),
    },
}
deadprogram commented 7 months ago

Would be good to look at https://github.com/stealthrocket/wazergo for inspiration here?

deadprogram commented 7 months ago

I think this is a great step in the direction we need, which is being able to be called from guest, and not needing reflection. Now merging, thanks @orsinium