Scarpet docs - Xendergo/Minecraft-wasm-runtime GitHub Wiki
Minecraft wasm runtime works nicely with scarpet, though carpet mod isn't required for the mod to run.
Module type
Minecraft wasm runtime adds a new type WasmModule which represents a webassembly module.
get_module(name: String): WasmModule
Get a module that's already loaded by name
load_module(name: String): WasmModule
Get a module that's already loaded, and if it isn't loaded, load it.
reload_module(name: String): WasmModule
Unloads a module if it's loaded, then loads it again and returns the module.
call_wasm_function(module: WasmModule, name: String, args...): [return_values]
Call a function exported by the given wasm module by name. You must provide all the arguments it requires.
Example:
// nth fibonacci number
export function fibonacci(n: i32): i32 {
let a: i32 = 1;
let b: i32 = 1;
let c: i32 = 0;
for (let i = 0; i < n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
module = load_module('myModule.release');
print(call_wasm_function(module, 'fibonacci', 3));
Chat: [5]
provide_import(module: WasmModule, name: String, callback: Function<(params...), return>)
Provide a scarpet function for the webassembly module to import
Example:
declare function tp(x: f32, y: f32, z: f32): f32;
// nth fibonacci number
export function fibonacci(n: i32): void {
let a: f32 = 1;
let b: f32 = 1;
let c: f32 = 0;
for (let i = 0; i < n; i++) {
c = add(a, b);
a = b;
b = c;
}
tp(a, b, c);
}
module = load_module('myModule.release');
provide_import(module, 'tp', _(x, y, z) -> (
modify(player(), 'pos', l(x, y, z));
));
call_wasm_function(module, 'fibonacci', 10);
This would teleport the player to (89, 144, 144)
read_string(module: WasmModule, ptr: List<?>): String
If a wasm function returns a string, you can get the string from the pointer by running this function. The type of ptr would depend on the language the wasm has set itself as, but putting the value from call_wasm_function directly into this should always work.
new_string(module: WasmModule, string: String): List<?>
Convert a string into a pointer, the pointer can be passed directly into call_wasm_function.
Example:
export function repeat(n: String, amt: i32): String {
return n.repeat(amt);
}
module = load_module('myModule.release');
print(read_string(module, call_wasm_function(module, 'repeat', new_string(module, 'yee'), 5)));
Chat: yeeyeeyeeyeeyee