RyanLamansky / dotnet-webassembly

Create, read, modify, write and execute WebAssembly (WASM) files from .NET-based applications.
Apache License 2.0
792 stars 74 forks source link

Question: Like a script host #46

Open Astn opened 3 years ago

Astn commented 3 years ago

I've done a little script hosting before for things such as games, and rules engines, and I really like the idea of being able to do the same thing with webassembly, because the user could pick from various languages that compile to web-assembly.

I'm hoping this would be an incredible performance improvement over script hosting.

I feel like I would need to define some interfaces, and hook functions and then somehow let those be defined in web-assembly so they could be used by script clients to compile against.

I see that interface types is still a proposal and might solve this issue.

Do you have any advice on how this could be done right now? I'd love an example if you have a little time on a setup where we are able to pass an object, or struct to a standard named handler on the webAssembly side, and then deal with the result on the host side.

The specific use case I'm thinking of at the moment would be for supporting user defined functions similar to SQL server stored procedures, or UDFs inside a database engine.

Thoughts?

furesoft commented 3 years ago

I am currently writing a wrapper to allow this. But you could do it by your own. You need an interface between some types of .net and webassembly.

when you add a normal .net method eg. WriteLine(String) the wrapper adds a for me called a "grain". It adds a ptr and length parameter for the string to the webassembly function. after the call the result do the same but reverse.

RyanLamansky commented 3 years ago

JIT-based WebAssembly implementations (including this project and the ones built into browsers) are very fast and would outperform most traditional script engines.

This project currently only covers WebAssembly 1.0, which only has 4 types: 32-bit and 64-bit flavors of integer and floating point. Fortunately, most data structures can be broken down into these (a byte is 1/4th of an int, for example, and boolean is 0 vs. non-zero int). Memory is essentially a long byte array and you can reference things within using their integer address.

It would be inconvenient to break down complex .NET types every time, so wrappers (as suggested by @furesoft) and other helpers would be required. The WASM spec allows for importing and exporting memory, functions, and "global" variables, which are all the building blocks you need to do complicated things, as we've seen by all the things already being done with WASM.

furesoft commented 3 years ago

@Astn I have implemented something to import static functions easily. I could give you the source, if you want.

I also want to implement to be able to import struct values

Astn commented 3 years ago

@furesoft That would be great, thanks!

furesoft commented 3 years ago

@Astn https://github.com/furesoft/Slithin/tree/develop/Slithin.ModuleSystem

You can integrate it in your library