diff --git a/index.html b/index.html new file mode 100644 index 0000000..06a376a --- /dev/null +++ b/index.html @@ -0,0 +1,20 @@ + + + + + + + diff --git a/t.js b/t.js new file mode 100644 index 0000000..a3d8ac2 --- /dev/null +++ b/t.js @@ -0,0 +1,18 @@ +const imports = { + guppy: { + print: (arg) => console.log (arg) + } +} + +// Assume add.wasm file exists that contains a single function adding 2 provided arguments +const fs = require('node:fs'); + +// Use the readFileSync function to read the contents of the "add.wasm" file +const wasmBuffer = fs.readFileSync('u.wasm'); + +// Use the WebAssembly.instantiate method to instantiate the WebAssembly module +WebAssembly.instantiate(wasmBuffer, imports).then(wasmModule => { + // Exported function lives under instance.exports object + const { main } = wasmModule.instance.exports; + main () +}); diff --git a/u.wasm b/u.wasm new file mode 100644 index 0000000..702623c Binary files /dev/null and b/u.wasm differ diff --git a/u.wat b/u.wat index f34484c..c4cd5c7 100644 --- a/u.wat +++ b/u.wat @@ -1,7 +1,78 @@ (module + (func $print (import "guppy" "print") (param i32)) + (table 2 funcref) + (elem (i32.const 0) $halt) + (type $cont (func (param i32))) - (type $cont-stack-type (array (ref null $cont))) + (type $cont-stack-type (array (mut (ref null $cont)))) (global $cont-stack (ref $cont-stack-type) (array.new_default $cont-stack-type (i32.const 128))) - (func (export "main") (result (ref eq)) - (ref.i31 (i32.const 123)))) + (global $cont-stack-top (mut i32) (i32.const 0)) + + (type $arg-array-type (array (mut (ref null eq)))) + (global $arg-array (ref $arg-array-type) + (array.new_default $arg-array-type (i32.const 32))) + + ;; (memory $memory i32 1) + ;; (global $arg-stack-base i32 (i32.const 0)) + ;; (global $arg-stack-ptr i32 (global.get $arg-stack-base)) + ;; (global $cont-stack-base i32 (i32.const 32)) + ;; (global $cont-stack-ptr i32 (global.get $cont-stack-base)) + + (func $add (param $nargs i32) + (local $x (ref eq)) + (local $y (ref eq)) + (local $return (ref $cont)) + (local.set $x (ref.as_non_null + (array.get $arg-array-type + (global.get $arg-array) + (i32.const 0)))) + (local.set $y (ref.as_non_null + (array.get $arg-array-type + (global.get $arg-array) + (i32.const 1)))) + (array.set $arg-array-type + (global.get $arg-array) + (i32.const 0) + (ref.i31 + (i32.add (i31.get_s (ref.cast (ref i31) (local.get $x))) + (i31.get_s (ref.cast (ref i31) (local.get $y)))))) + (return_call_ref + $cont + (i32.const 1) + (block (result (ref $cont)) + (ref.as_non_null + (array.get $cont-stack-type + (global.get $cont-stack) + (global.get $cont-stack-top))) + (global.set $cont-stack-top + (i32.sub (global.get $cont-stack-top) + (i32.const 1)))))) + (func $halt (param $nargs i32) + (call $print + (i31.get_s + (ref.cast + (ref i31) + (ref.as_non_null + (array.get $arg-array-type + (global.get $arg-array) + (i32.const 0))))))) + (func (export "main") + ;; push args + (array.set $arg-array-type + (global.get $arg-array) + (i32.const 0) + (ref.i31 (i32.const 4))) + (array.set $arg-array-type + (global.get $arg-array) + (i32.const 1) + (ref.i31 (i32.const 5))) + ;; push return continuation + (array.set $cont-stack-type + (global.get $cont-stack) + (i32.const 0) + (ref.func $halt)) + ;; make call }:) + (return_call $add + ;; inform $add how many arguments we called it with + (i32.const 2))))