graphprotocol / research

Research, proposals, papers, and specs
48 stars 9 forks source link

Investigate WASM runtime for mappings #34

Closed Zerim closed 6 years ago

Zerim commented 6 years ago

[] What's the subset of TypeScript that is supported in WASM(i)? [] How do we call WASM functions from Rust? [] How do we call Rust functions from WASM? [] How complex and dynamic can data structures be that we pass back and forth between Rust and WASM? [] Is the foreign function interface provided by wasmi synchronous or asynchronous? [] Is there a way to define globals that are available to WASM functions/modules? [] How do we provide TypeScript types for the foreign function parameters, return values etc.?

Jannis commented 6 years ago

How do we call WASM functions from Rust?

This is done by calling invoke_export on a module instance. Example.

How do we call Rust functions from WASM?

This is done through Externals and import resolvers that are passed to WASM functions as they are invoced from the Rust side. External functions can then do whatever they want but must return wasmi runtime values.

How complex and dynamic can data structures be that we pass back and forth between Rust and WASM?

Arbitrarily complex, however, since WASM only supports ints/floats, this requires mapping complex data structures (such as vectors or hash maps) to the underlying memory. AssemblyScript does this and they provide implementations for typed arrays and maps.

This means we can safely use arrays and maps in mappings. However, if we want to pass data from Rust to WASM, we'll need to put it in memory in the exact same format, so WASM can interpret it. AssemblyScript bases its Map implementation on two arrays (for keys and values); this kind of reuse could safe us some work.

Is the foreign function interface provided by wasmi synchronous or asynchronous?

Synchronous. You can call import Rust functions and call them but they have to return with a value immediately.

Is there a way to define globals that are available to WASM functions/modules?

Yes. This can be done through import resolvers (on the Rust side) and imports (on the TS/WASM side).