Integrate examples into tests

This commit is contained in:
dcodeIO
2017-12-14 12:30:58 +01:00
parent c6c36613e6
commit 6bea116b3e
13 changed files with 522 additions and 314 deletions

1
examples/.gitignore vendored
View File

@ -1,2 +1,3 @@
*.wast
*.wasm
node_modules/

View File

@ -6,10 +6,10 @@ An AssemblyScript example.
Instructions
------------
To build `assembly/game-of-life.ts` to `game-of-life.wasm`, run:
To build [assembly/game-of-life.ts](./assembly/game-of-life.ts) to an untouched and an optimized `.wasm` including their respective `.wast` representations, run:
```
$> npm run build
```
Afterwards, open `game-of-life.html` in a browser (ideally one that allows `fetch`ing the `.wasm` from the local filesystem).
Afterwards, open [game-of-life.html](./game-of-life.html) in a browser (ideally one that allows `fetch`ing the `.wasm` from the local filesystem).

View File

@ -1,277 +0,0 @@
(module
(type $iiv (func (param i32 i32)))
(type $v (func))
(global $assembly/game-of-life/w (mut i32) (i32.const 0))
(global $assembly/game-of-life/h (mut i32) (i32.const 0))
(global $assembly/game-of-life/s (mut i32) (i32.const 0))
(memory $0 1)
(export "init" (func $assembly/game-of-life/init))
(export "step" (func $assembly/game-of-life/step))
(export "memory" (memory $0))
(func $assembly/game-of-life/init (; 0 ;) (type $iiv) (param $0 i32) (param $1 i32)
(set_global $assembly/game-of-life/w
(get_local $0)
)
(set_global $assembly/game-of-life/h
(get_local $1)
)
(set_global $assembly/game-of-life/s
(i32.mul
(get_global $assembly/game-of-life/w)
(get_global $assembly/game-of-life/h)
)
)
)
(func $assembly/game-of-life/step (; 1 ;) (type $v)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(set_local $6
(i32.sub
(get_global $assembly/game-of-life/h)
(i32.const 1)
)
)
(set_local $7
(i32.sub
(get_global $assembly/game-of-life/w)
(i32.const 1)
)
)
(set_local $0
(i32.const 0)
)
(loop $continue|0
(if
(i32.lt_u
(get_local $0)
(get_global $assembly/game-of-life/h)
)
(block
(set_local $4
(select
(i32.sub
(get_local $0)
(i32.const 1)
)
(get_local $6)
(get_local $0)
)
)
(set_local $5
(select
(i32.const 0)
(i32.add
(get_local $0)
(i32.const 1)
)
(i32.eq
(get_local $0)
(get_local $6)
)
)
)
(set_local $1
(i32.const 0)
)
(loop $continue|1
(if
(i32.lt_u
(get_local $1)
(get_global $assembly/game-of-life/w)
)
(block
(set_local $2
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.load8_u
(i32.add
(i32.mul
(get_local $4)
(get_global $assembly/game-of-life/w)
)
(tee_local $2
(select
(i32.sub
(get_local $1)
(i32.const 1)
)
(get_local $7)
(get_local $1)
)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $4)
(get_global $assembly/game-of-life/w)
)
(get_local $1)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $4)
(get_global $assembly/game-of-life/w)
)
(tee_local $3
(select
(i32.const 0)
(i32.add
(get_local $1)
(i32.const 1)
)
(i32.eq
(get_local $1)
(get_local $7)
)
)
)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
(get_local $2)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
(get_local $3)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $5)
(get_global $assembly/game-of-life/w)
)
(get_local $2)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $5)
(get_global $assembly/game-of-life/w)
)
(get_local $1)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $5)
(get_global $assembly/game-of-life/w)
)
(get_local $3)
)
)
)
)
(if
(i32.load8_u
(i32.add
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
(get_local $1)
)
)
(if
(if (result i32)
(tee_local $3
(i32.lt_u
(get_local $2)
(i32.const 2)
)
)
(get_local $3)
(i32.gt_u
(get_local $2)
(i32.const 3)
)
)
(i32.store8
(i32.add
(i32.add
(get_global $assembly/game-of-life/s)
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
)
(get_local $1)
)
(i32.const 0)
)
)
(if
(i32.eq
(get_local $2)
(i32.const 3)
)
(i32.store8
(i32.add
(i32.add
(get_global $assembly/game-of-life/s)
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
)
(get_local $1)
)
(i32.const 1)
)
)
)
(set_local $1
(i32.add
(get_local $1)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
(set_local $0
(i32.add
(get_local $0)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)

View File

@ -1,310 +0,0 @@
(module
(type $iiv (func (param i32 i32)))
(type $v (func))
(global $assembly/game-of-life/w (mut i32) (i32.const 0))
(global $assembly/game-of-life/h (mut i32) (i32.const 0))
(global $assembly/game-of-life/s (mut i32) (i32.const 0))
(global $HEAP_START i32 (i32.const 4))
(memory $0 1)
(export "init" (func $assembly/game-of-life/init))
(export "step" (func $assembly/game-of-life/step))
(export "memory" (memory $0))
(func $assembly/game-of-life/init (; 0 ;) (type $iiv) (param $0 i32) (param $1 i32)
(set_global $assembly/game-of-life/w
(get_local $0)
)
(set_global $assembly/game-of-life/h
(get_local $1)
)
(set_global $assembly/game-of-life/s
(i32.mul
(get_global $assembly/game-of-life/w)
(get_global $assembly/game-of-life/h)
)
)
)
(func $assembly/game-of-life/step (; 1 ;) (type $v)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(local $8 i32)
(local $9 i32)
(local $10 i32)
(local $11 i32)
(nop)
(nop)
(block
(set_local $6
(i32.sub
(get_global $assembly/game-of-life/h)
(i32.const 1)
)
)
(set_local $7
(i32.sub
(get_global $assembly/game-of-life/w)
(i32.const 1)
)
)
)
(block
(set_local $10
(i32.const 0)
)
)
(block $break|0
(set_local $0
(i32.const 0)
)
(loop $continue|0
(if
(i32.lt_u
(get_local $0)
(get_global $assembly/game-of-life/h)
)
(block
(block
(set_local $1
(select
(get_local $6)
(i32.sub
(get_local $0)
(i32.const 1)
)
(i32.eq
(get_local $0)
(i32.const 0)
)
)
)
(set_local $2
(select
(i32.const 0)
(i32.add
(get_local $0)
(i32.const 1)
)
(i32.eq
(get_local $0)
(get_local $6)
)
)
)
(block $break|1
(set_local $3
(i32.const 0)
)
(loop $continue|1
(if
(i32.lt_u
(get_local $3)
(get_global $assembly/game-of-life/w)
)
(block
(block
(set_local $4
(select
(get_local $7)
(i32.sub
(get_local $3)
(i32.const 1)
)
(i32.eq
(get_local $3)
(i32.const 0)
)
)
)
(set_local $5
(select
(i32.const 0)
(i32.add
(get_local $3)
(i32.const 1)
)
(i32.eq
(get_local $3)
(get_local $7)
)
)
)
(set_local $8
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.load8_u
(i32.add
(i32.mul
(get_local $1)
(get_global $assembly/game-of-life/w)
)
(get_local $4)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $1)
(get_global $assembly/game-of-life/w)
)
(get_local $3)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $1)
(get_global $assembly/game-of-life/w)
)
(get_local $5)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
(get_local $4)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
(get_local $5)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $2)
(get_global $assembly/game-of-life/w)
)
(get_local $4)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $2)
(get_global $assembly/game-of-life/w)
)
(get_local $3)
)
)
)
(i32.load8_u
(i32.add
(i32.mul
(get_local $2)
(get_global $assembly/game-of-life/w)
)
(get_local $5)
)
)
)
)
(set_local $9
(i32.load8_u
(i32.add
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
(get_local $3)
)
)
)
(if
(get_local $9)
(if
(if (result i32)
(tee_local $11
(i32.lt_u
(get_local $8)
(i32.const 2)
)
)
(get_local $11)
(i32.gt_u
(get_local $8)
(i32.const 3)
)
)
(i32.store8
(i32.add
(i32.add
(get_global $assembly/game-of-life/s)
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
)
(get_local $3)
)
(i32.const 0)
)
)
(if
(i32.eq
(get_local $8)
(i32.const 3)
)
(i32.store8
(i32.add
(i32.add
(get_global $assembly/game-of-life/s)
(i32.mul
(get_local $0)
(get_global $assembly/game-of-life/w)
)
)
(get_local $3)
)
(i32.const 1)
)
)
)
)
(set_local $3
(i32.add
(get_local $3)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
)
)
(set_local $0
(i32.add
(get_local $0)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)
)

View File

@ -1,7 +1,7 @@
<canvas id="canvas" width="640" height="480"></canvas><script>
// Fetch the .wasm
fetch("game-of-life.wasm").then(response => response.arrayBuffer()).then(binary => {
fetch("game-of-life.optimized.wasm").then(response => response.arrayBuffer()).then(binary => {
// Instantiate the module
var module = new WebAssembly.Module(binary);

View File

@ -3,7 +3,9 @@
"version": "1.0.0",
"private": true,
"scripts": {
"build": "asc assembly/game-of-life.ts -t assembly/game-of-life.wast -c && asc assembly/game-of-life.ts -b game-of-life.wasm -t assembly/game-of-life.optimized.wast -O -c",
"build": "npm run build:untouched && npm run build:optimized",
"build:untouched": "asc assembly/game-of-life.ts -b game-of-life.untouched.wasm -t game-of-life.untouched.wast --validate",
"build:optimized": "asc -O assembly/game-of-life.ts -b game-of-life.optimized.wasm -t game-of-life.optimized.wast --validate",
"browser": "game-of-life.html"
}
}

View File

@ -6,10 +6,10 @@ An AssemblyScript example.
Instructions
------------
To build `assembly/i64.ts` to `i64.wasm`, run:
To build [assembly/i64.ts](./assembly/i64.ts) to an untouched and an optimized `.wasm` including their respective `.wast` representations, run:
```
$> npm run build
```
Afterwards, `require` the node module as usual (entry point is `index.js`).
Afterwards, `require` the node module as usual (CommonJS entry point is [index.js](./index.js)).

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
var fs = require("fs");
// Instantiate the module
var mod = new WebAssembly.Module(fs.readFileSync(__dirname + "/i64.wasm"));
var mod = new WebAssembly.Module(fs.readFileSync(__dirname + "/i64.optimized.wasm"));
var ins = new WebAssembly.Instance(mod, { /* no imports */ });
// Export its exports

View File

@ -3,6 +3,8 @@
"version": "1.0.0",
"private": true,
"scripts": {
"build": "asc assembly/i64.ts -t assembly/i64.wast -c && asc assembly/i64.ts -b i64.wasm -t assembly/i64.optimized.wast -O -c"
"build": "npm run build:untouched && npm run build:optimized",
"build:untouched": "asc assembly/i64.ts -t i64.untouched.wast -b i64.untouched.wasm --validate",
"build:optimized": "asc -O assembly/i64.ts -b i64.optimized.wasm -t i64.optimized.wast --validdate"
}
}