this mod add wasm support for CC: Tweaked.
this mod uses chicory runtime (v1.7.5), and includes wasi support.
wasm— global program dir, read-only,.minecraft/wasm/wasm_save— per-save program dir, read-only,.minecraft/saves/<world>/computercraft/wasm/shared— per-save writable data dir (1 GB quota),.minecraft/saves/<world>/computercraft/shared/
put .wasm files (compiled from Rust, see cc_wasm_api)
into the wasm folder (.minecraft/wasm/), then in game:
exec_wasm <name>— load wasm (interpreter) and drive its coroutine loop (tick/stopped)exec_wasm_aot <name>— load wasm with AOT + disk cache, then drive the coroutine loopwasm_compile_aot <name> [global|save|auto]— precompile a wasm to the AOT disk cache in background
wasm.load_wasm(name, [useAoT], [source], [useStdio])— load a module (no.wasmextension).useAoTdefaults totrue;sourceis"auto"(save first) /"global"/"save". withuseStdio = truethe module gets three extra methods:stdin(...)— push strings into the WASI stdin (empty stdin = EOF, never blocks)stdout()— return all output written to stdout since the last call (and clear it)stderr()— same for stderr
wasm.precompile(name, [source])— start background AOT compilation (threadccwasm-aot), writes result to the disk cache. Needed for large modules, because a single Lua call is limited by the computer thread timeout (~7.5s), while AOT compiling a big module (e.g. the python example) takes tens of seconds.wasm.precompile_done(name)— poll status:"compiling"/"done"/"failed"/nil. In Lua, poll withos.sleepto avoid the timeout.
after a module is precompiled once, wasm.load_wasm(name, true) loads it from the cache
in ~1.4s no matter how big it is.
compiled machine code is cached in .minecraft/wasm_cache/ (keyed by the wasm content sha256,
so changing the wasm invalidates the cache automatically; corrupted cache falls back to a
cache-less recompile).
use this crate.
features (opt-in, Cargo features):
coroutine—tick/stoppedexports for driving a loop withexec_wasm/exec_wasm_aoteval— call back into Lua (eval_string/eval_result)addon— local monitor / vec2d / time helpersdebug— debug output helpers
see the examples here: minesweeper, pic_display, txt_display, python, obj_demo, type_test.
-
download this file and put it in
.minecraft/wasm/ -
place a monitor on top of a computer
-
in computer craft's shell run
exec_wasm minesweeper
-
download this file and put it in
.minecraft/wasm/ -
place a monitor on top of a computer
-
in computer craft's shell run
exec_wasm pic_display [path to picture file(in cc's fs)]
python interpreter example,
download this file and put it in ./wasm/.
python.wasm is big (9.6MB): AOT compiling it takes ~11s, which exceeds the computer thread timeout for a single Lua call, so precompile it first (runs in a background thread and writes the AOT disk cache):
wasm_compile_aot python
then in computer craft lua run:
py = wasm.load_wasm("python") -- AOT, loads from disk cache in ~1.4s
py.init()
py.exec("import time as t")
print(py.eval("t.time()"))note: when using stdio (load_wasm(..., true)), python's sys.stderr.write is line-buffered
(chicory reports isatty), so call flush() explicitly if there is no newline; print flushes
on its own.
test scripts live in wasm/test/ (dev run dir), entry point test.lua (run dofile from a
computer, e.g. dofile("wasm/test/test.lua")). Results are written to shared/.