Dergwasm is a WASM interpreter that runs as a mod in Resonite.
Several languages have support for compiling into WASM. Just to name a few:
- C/C++
- C#
- Ruby
- Rust
- Go
- Java
- and more
Some of these languages don't actually compile into WASM, but rather provide a bytecode interpreter written in WASM. For example, Python.
WARNING: Dergwasm is currently in alpha. This means that hardly any Resonite API methods have been implemented.
NOTE: Dergwasm does not have access to your computer's filesystem, except for loading the WASM file you want to run.
Dergwasm includes a WASM binary, firmware.wasm
, which implements a MicroPython interpreter. This means you can execute a subset of Python code in Resonite! MicroPython only supports syntax up to Python 3.4.
NOTE: The MicroPython implementation does not have access to your computer's filesystem. Instead, it uses a slot as a virtual filesystem.
WARNING: Installation at this point isn't turnkey. You must be familiar with the Dev Tool, the inspector, and the ProtoFlux tool.
-
If you haven't already done so, install Resonite Mod Loader.
-
From the latest release, under Assets, download
Dergwasm.dll
. -
Copy
Dergwasm.dll
into yourrml_mods
directory. See the Resonite Mod Loader instructions for details about that. -
Start Resonite.
-
To verify that the mod loaded, you can look in your Resonite logs for something like this line:
6:38:54 PM.614 ( -1 FPS) [INFO] [ResoniteModLoader/Dergwasm] Dergwasm patches applied
-
Copy this URL, and paste it into your world in Resonite:
resrec:///U-Xekri/R-dccc5d61-435f-4bc2-ac88-b8de3ccfd678
. This is a Dergwasm slot hierarchy, already set up with a WASM file and slot-based filesystem implementing MicroPython. -
Re-initialize Dergwasm by using the ProtofluxTool to create a Dynamic Impulse Trigger node. Set its
Tag
to_dergwasm_init
and itsTargetHierarchy
to the top-level Dergwasm slot. Now trigger the node. There will be a slight hitch as the WASM file is read in and parsed.
-
The
firwmware.wasm
slot has a tag_dergwasm_wasm_file
. It's just an imported WASM file. The name of the slot doesn't matter, since Dergwasm searches for it by tag. You can actually set this for any WASM file you have, but there must be only one under the Dergwasm hierarchy. -
There is a
Text
object (ByteDisplay
) with tag_dergwasm_byte_display
. Currently this object is only intended to display your computer's file path where it loadsfirmware.wasm
from. -
The
Console
slot is a text display adapted slightly from the standard text display that Resonite spawns when you import a text file. This will display debug messages from Dergwasm, as well as any printed output from WASM. Buried within this hiearchy is a Content slot with tag_dergwasm_console_content
, which is how Dergwasm finds the text for the console. -
There's a slot under the
Dergwasm
slot calledArgs
, with tag_dergwasm_args
. It has aValueField<string>
component. This field contains the WASM function name you want to call.If you're just interested in running MicroPython, the value in the field should be
mp_js_do_str
. -
Under the
Args
slot, you can add as many slots as you want, one for each argument to the WASM function. Make sure theOrderOffset
fields are set to order the slots according to the argument order. Each slot must have oneValueField
component.If you're just interested in running MicroPython, you only want one argument with a
ValueField<string>
component, containing your Python code. Here I used aText
object, and used aValueCopy
component to copy the text into theValueField<string>
component. -
Create a
Dynamic Impulse Trigger
ProtoFlux node. Set its tag input to_dergwasm
and its hierarchy input to yourArgs
slot. Call it when you want to execute a WASM function.
WASM code is normally assumed to be running in a browser, but in general, it relies on a "host environment". Thus, compiled WASM code normally also comes with a JavaScript file which is the host environment. However, Dergwasm implements a host environment in C#.
Any "external" functions required by the WASM code are implemented in the host environment, whether that be JavaScript, or, as in Dergwasm, C#. This includes a Resonite API.
A WASM file requires all of its external functions to be present. If an external function is not present, Dergwasm will output a message to the Resonite log and to dergwasm_console_content
, and you will not be able to do anything else until Dergwasm provides an implementation of that external function.
If you want to compile the mod, I'm not sure I captured all the requirements. At a minimum:
-
Use Visual Studio 2022 to load and build the Dergwasm project that is in
dergwasm_mod/Dergwasm.sln
. This should give youDergwasm.dll
. -
Use a Linux system, install Emscripten. I suggest using the
emsdk
tool. -
On the Linux system, clone the MicroPython repo.
-
Make a directory
user_modules/resonite
in the repo root. -
Copy everything from
usercmodule/resonite
into that directory. -
Copy everything from
c
into that directory. -
cd ports/webassembly
-
make clean
-
make V=1 USER_C_MODULES=../../user_modules
-
This should give you
build/firmware.wasm
.