diff options
Diffstat (limited to 'demo/wat2wasm/examples.js')
-rw-r--r-- | demo/wat2wasm/examples.js | 112 |
1 files changed, 100 insertions, 12 deletions
diff --git a/demo/wat2wasm/examples.js b/demo/wat2wasm/examples.js index 901b5da8..504d83d2 100644 --- a/demo/wat2wasm/examples.js +++ b/demo/wat2wasm/examples.js @@ -25,11 +25,10 @@ var examples = [ name: 'simple', contents: `(module - (func $addTwo (param i32 i32) (result i32) - get_local 0 - get_local 1 - i32.add) - (export "addTwo" (func $addTwo))) + (func (export "addTwo") (param i32 i32) (result i32) + local.get 0 + local.get 1 + i32.add)) `, js: `const wasmInstance = @@ -45,21 +44,20 @@ for (let i = 0; i < 10; i++) { name: 'factorial', contents: `(module - (func $fac (param f64) (result f64) - get_local 0 + (func (export "fac") (param f64) (result f64) + local.get 0 f64.const 1 f64.lt if (result f64) f64.const 1 else - get_local 0 - get_local 0 + local.get 0 + local.get 0 f64.const 1 f64.sub call $fac f64.mul - end) - (export "fac" (func $fac))) + end)) `, js: `const wasmInstance = new WebAssembly.Instance(wasmModule, {}); @@ -85,11 +83,101 @@ for (let i = 1; i <= 15; i++) { drop) (export "e" (func 1))) `, - js: `var wasmInstance = new WebAssembly.Instance(wasmModule, { + js: `const wasmInstance = new WebAssembly.Instance(wasmModule, { foo: { bar() {} }, }); `, + }, + + { + name: 'mutable globals', + contents: +`(module + (import "env" "g" (global (mut i32))) + (func (export "f") + i32.const 100 + global.set 0)) +`, + js: +` + const g = new WebAssembly.Global({value: 'i32', mutable: true}); + const wasmInstance = new WebAssembly.Instance(wasmModule, {env: {g}}); + console.log('before: ' + g.value); + wasmInstance.exports.f(); + console.log('after: ' + g.value); +` + }, + + { + name: "saturating float-to-int", + contents: +`(module + (func (export "f") (param f32) (result i32) + local.get 0 + i32.trunc_sat_f32_s))`, + js: +`const wasmInstance = new WebAssembly.Instance(wasmModule); +const {f} = wasmInstance.exports; +console.log(f(Infinity));` + }, + + { + name: "sign extension", + contents: +`(module + (func (export "f") (param i32) (result i32) + local.get 0 + i32.extend8_s)) +`, + js: +`const wasmInstance = new WebAssembly.Instance(wasmModule); +const {f} = wasmInstance.exports; +console.log(f(0)); +console.log(f(127)); +console.log(f(128)); +console.log(f(255));` + }, + + { + name: "multi value", + contents: +`(module + (func $swap (param i32 i32) (result i32 i32) + local.get 1 + local.get 0) + + (func (export "reverseSub") (param i32 i32) (result i32) + local.get 0 + local.get 1 + call $swap + i32.sub)) +`, + js: +`const wasmInstance = new WebAssembly.Instance(wasmModule); +const {reverseSub} = wasmInstance.exports; +console.log(reverseSub(10, 3));` + }, + + { + name: "bulk memory", + contents: +`(module + (memory (export "mem") 1) + (func (export "fill") (param i32 i32 i32) + local.get 0 + local.get 1 + local.get 2 + memory.fill)) +`, + js: +`const wasmInstance = new WebAssembly.Instance(wasmModule); +const {fill, mem} = wasmInstance.exports; +fill(0, 13, 5); +fill(10, 77, 7); +fill(20, 255, 1000); +console.log(new Uint8Array(mem.buffer, 0, 50)); +` } ]; |