wazero icon indicating copy to clipboard operation
wazero copied to clipboard

Compile functions at runtime

Open ktye opened this issue 2 years ago • 5 comments

Is it (or will it) be possible to extend a running wasm instance with a new function?

E.g. compile a function from wasm binary representation and add it to the function table. The new function should be able to call all other internal functions. It could be restricted to function signatures that are already present in the module.

waforth is doing something similar, but compiles a complete module each time, instead of extending the running one (which is not possible in the js-interface).

ktye avatar Jun 18 '22 07:06 ktye

Is the reason you want to change a module at runtime because you are accumulating state (as opposed to reloading when there is a new function)?

codefromthecrypt avatar Jun 19 '22 23:06 codefromthecrypt

ps. the current answer is no, but we didn't consider special casing. Bear in mind that compiling wasm in ModuleBuilder will likely occur at some point. Also, a host module can do anything including reload its own functions.

What's interesting here is it seems you want to call functions that aren't exported. To call non-exported functions from a different module starts sounding like a security issue unless you are trying to re-compile an updated module incrementally?

Maybe you can discuss the entire use case you are looking for as there seem to be multiple things connected here.

codefromthecrypt avatar Jun 19 '22 23:06 codefromthecrypt

The use case is an interpreter that compiles new functions defined in the repl. Each compiled function should be added to the module, as if it was a native function in the first place. This is very similar to the forth interpreter mentioned above, but in my case it's for the k language. It needs to preserve all previous state. As a workaround, I guess i could also just store memory in the embedder, compile everything again and restore.

The interpreter is a small 60k wasm binary, written in low level go and transpiled on the ast level using wg

The interpreter with the wazero embedder is here: wa0. It passes all tests in interpreted mode, not yet for the jit compiler (i assume simd is not completed, otherwise i can share bug reports).

ktye avatar Jun 20 '22 06:06 ktye

one idea until I look closer.. what if the memory was imported? in this case the state is preserved. ex define memory in one module and any other state. then for the repl function recompile each time it changes?

(just to give a thought until I have time to dig deeper)

codefromthecrypt avatar Jun 20 '22 08:06 codefromthecrypt

There is a golang compiler that compiles to WASM. The golang compiler itself compiles to WASM.

mots called hackpad. It’s currently working but a little slow.

https://github.com/hack-pad/hackpad

the code lives on a file system inside the browser. See: https://github.com/hack-pad/hackpadfs

There is even an ide and terminal

gedw99 avatar Jul 27 '22 05:07 gedw99

I believe this is out of our scope, and you can do anything with the host functions

mathetake avatar Feb 10 '23 01:02 mathetake