ashtonmeuser / godot-wasm

Interact with WebAssembly modules from Godot
https://github.com/ashtonmeuser/godot-wasm/wiki
MIT License
193 stars 11 forks source link

Minimal WASI support #38

Closed ashtonmeuser closed 1 year ago

ashtonmeuser commented 1 year ago

Support for a small subset of WASI wasi_snapshot_preview1. Includes the following WASI functionality.

  1. If not provided, a default import function will be provided for wasi_snapshot_preview1.fd_write. This allows simple logging from a Wasm module. This is supported in AssemblyScript (via WASI shim) using console.log(), console.error(), etc. and Grain using print(). Should help immensely with debugging.
  2. If not provided, a default import function will be provided for wasi_snapshot_preview1.proc_exit. This allows Wasm modules to terminate with an error code. Supported in AssemblyScript (via WASI shim) using process.exit() and Grain using Process.exit().
  3. If a Wasm module exports a _start function, it will be called immediately on instantiation.
ashtonmeuser commented 1 year ago

@fire This is a simple yet hugely impactful change that may be of interest to you. Should really expand Godot Wasm's compatibility with languages targeting Wasm. Logging support is also super helpful for debugging. There is plenty more WASI functionality that could be added, but this is a good start.

Edit: The following is only partially relevant after making Godot 4 the target of the main branch to address #30. The Godot 3 branch will now be the "floating" branch that is occasionally rebased.

Once this is solid, I'll port it to Godot 4.x.

Speaking of Godot 4, I'll be rebasing the Godot 4 branch onto master to better reflect the changes that were done specifically for Godot 4 support (2 commits) and remove the commits cherry picked from master (21 commits). Linear history makes these changes more obvious and will catch any drift/differences incurred by cherry picking for a long stretch. It'll end up looking like this (which is identical to v0.1.9-godot-4) and history will be preserved by the v0.1.9-godot-4 tag. I'll likely do this every release or few releases. When the Godot 4 branch becomes main (#30), I'll do the same with the Godot 3 branch.

ashtonmeuser commented 1 year ago

Some of the obvious next targets for WASI wasi_snapshot_preview1 support would be:

Some of these would simply be stubbed out because args are irrelevant and environment should probably not be exposed to modules. However, at least providing these imports will allow loading modules that expect WASI bindings.

Edit: I would be very hesitant to implement any of the WASI filesystem functions. However, between this and environment variables, perhaps an optional permission model that defaults to safe is required. For example, you could set permissions with something like the following.

wasm.permissions({
  "env": true,
  "fs_read": true,
  "fs_write": false,
})
ashtonmeuser commented 1 year ago

Decided to address #30 and swap default branch to target Godot 4.x first. This'll be rebased and tested with Godot 4 before being backported to 3.