Files
Marcos Paulo a31eb59678 feat(plugins): sandboxed wasmi plugin host
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-13 19:24:35 -03:00

42 lines
1.9 KiB
WebAssembly Text Format

;; A well-behaved plugin: greets on load, echoes its command arguments, reads a note and
;; says goodbye on unload. It only imports `ns` calls, as the ABI demands.
(module
(import "ns" "log" (func $log (param i32 i32 i32) (result i64)))
(import "ns" "vault_read" (func $vault_read (param i32 i32) (result i64)))
(import "ns" "notice" (func $notice (param i32 i32) (result i64)))
(memory (export "memory") 32)
(global $bump (mut i32) (i32.const 1048576))
(data (i32.const 2048) "running")
(data (i32.const 2060) "notes/a.md")
(data (i32.const 2080) "goodbye")
(func (export "ns_alloc") (param $n i32) (result i32)
(local $p i32)
(local.set $p (global.get $bump))
(global.set $bump (i32.add (local.get $p) (local.get $n)))
(local.get $p))
(func (export "ns_free") (param i32) (param i32))
(func (export "ns_abi_version") (result i32) (i32.const 1))
(func (export "ns_on_load") (result i32)
(drop (call $log (i32.const 2) (i32.const 2048) (i32.const 7)))
(i32.const 0))
(func (export "ns_on_unload") (result i32)
(drop (call $notice (i32.const 2080) (i32.const 7)))
(i32.const 0))
(func (export "ns_on_event") (param i32) (param i32) (result i32) (i32.const 0))
;; Copies the JSON arguments to 3000 and hands the host back that block, packed.
(func (export "ns_run_command") (param $cp i32) (param $cl i32) (param $ap i32) (param $al i32) (result i64)
(local $i i32)
(drop (call $log (i32.const 2) (i32.const 2048) (i32.const 7)))
(drop (call $vault_read (i32.const 2060) (i32.const 10)))
(block $done
(loop $copy
(br_if $done (i32.ge_u (local.get $i) (local.get $al)))
(i32.store8 (i32.add (i32.const 3000) (local.get $i)) (i32.load8_u (i32.add (local.get $ap) (local.get $i))))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $copy)))
(i64.or (i64.shl (i64.const 3000) (i64.const 32)) (i64.extend_i32_u (local.get $al))))
)