Add mandelbrot example; Update game-of-life example

This commit is contained in:
dcodeIO 2018-04-19 17:32:23 +02:00
parent 350befee9b
commit 485c3fd9b2
30 changed files with 2069 additions and 980 deletions

View File

@ -15,6 +15,9 @@ Examples
* **[Conway's Game of Life](./examples/game-of-life)**<br /> * **[Conway's Game of Life](./examples/game-of-life)**<br />
Continuously updates the cellular automaton and visualizes its state on a canvas. There's also [this WebAssembly Studio fiddle](https://webassembly.studio/?f=gvuw4enb3qk) of it. Continuously updates the cellular automaton and visualizes its state on a canvas. There's also [this WebAssembly Studio fiddle](https://webassembly.studio/?f=gvuw4enb3qk) of it.
* **[Mandelbrot Set](./examples/mandelbrot)**<br />
Renders the Mandelbrot set to a canvas.
* **[i64 polyfill](./examples/i64-polyfill)**<br /> * **[i64 polyfill](./examples/i64-polyfill)**<br />
Exposes WebAssembly's i64 operations to JavaScript using 32-bit integers (low and high bits). Exposes WebAssembly's i64 operations to JavaScript using 32-bit integers (low and high bits).

View File

@ -1 +0,0 @@
node_modules/

View File

@ -1,7 +1,7 @@
Conway's Game of Life Conway's Game of Life
===================== =====================
An [AssemblyScript](http://assemblyscript.org) example. Continuously updates the cellular automaton and visualizes its state on a canvas. Compiles to ~450 bytes of optimized WASM, no strings attached. An [AssemblyScript](http://assemblyscript.org) example. Continuously updates the cellular automaton and visualizes its state on a canvas. Compiles to ~450 bytes of optimized WASM.
Instructions Instructions
------------ ------------
@ -12,10 +12,10 @@ First, install the development dependencies:
$> npm install $> npm install
``` ```
Now, to build [assembly/game-of-life.ts](./assembly/game-of-life.ts) to an untouched and an optimized `.wasm` including their respective `.wat` representations, run: Now, to build [assembly/index.ts](./assembly/index.ts) to an untouched and an optimized `.wasm` including their respective `.wat` representations, run:
``` ```
$> npm run build $> npm run asbuild
``` ```
Afterwards, run `node server` to start a <a href="http://localhost:9080">local server</a>. Should also automatically launch a browser. Afterwards, run `npm run server` to start a <a href="http://localhost:9080">local server</a>. Should also automatically launch a browser.

View File

@ -0,0 +1,3 @@
*.wasm
*.wasm.map
*.asm.js

View File

@ -0,0 +1,367 @@
(module
(type $iiv (func (param i32 i32)))
(type $v (func))
(import "env" "memory" (memory $0 1))
(global $assembly/index/w (mut i32) (i32.const 0))
(global $assembly/index/h (mut i32) (i32.const 0))
(global $assembly/index/s (mut i32) (i32.const 0))
(export "init" (func $assembly/index/init))
(export "step" (func $assembly/index/step))
(export "memory" (memory $0))
(func $assembly/index/init (; 0 ;) (type $iiv) (param $0 i32) (param $1 i32)
;;@ assembly/index.ts:9:2
(set_global $assembly/index/w
;;@ assembly/index.ts:9:6
(get_local $0)
)
;;@ assembly/index.ts:10:2
(set_global $assembly/index/h
;;@ assembly/index.ts:10:6
(get_local $1)
)
;;@ assembly/index.ts:11:2
(set_global $assembly/index/s
;;@ assembly/index.ts:11:6
(i32.mul
(get_local $0)
;;@ assembly/index.ts:11:14
(get_local $1)
)
)
)
(func $assembly/index/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 $5
;;@ assembly/index.ts:16:12
(i32.sub
(get_global $assembly/index/h)
;;@ assembly/index.ts:16:16
(i32.const 1)
)
)
(set_local $6
;;@ assembly/index.ts:17:12
(i32.sub
(get_global $assembly/index/w)
;;@ assembly/index.ts:17:16
(i32.const 1)
)
)
(loop $continue|0
(if
;;@ assembly/index.ts:21:18
(i32.lt_s
(get_local $0)
;;@ assembly/index.ts:21:22
(get_global $assembly/index/h)
)
(block
(set_local $3
;;@ assembly/index.ts:23:14
(if (result i32)
(get_local $0)
;;@ assembly/index.ts:23:29
(i32.sub
(get_local $0)
;;@ assembly/index.ts:23:33
(i32.const 1)
)
;;@ assembly/index.ts:23:23
(get_local $5)
)
)
(set_local $4
;;@ assembly/index.ts:24:14
(if (result i32)
(i32.eq
(get_local $0)
;;@ assembly/index.ts:24:19
(get_local $5)
)
;;@ assembly/index.ts:24:25
(i32.const 0)
;;@ assembly/index.ts:24:29
(i32.add
(get_local $0)
;;@ assembly/index.ts:24:33
(i32.const 1)
)
)
)
;;@ assembly/index.ts:25:9
(set_local $1
;;@ assembly/index.ts:25:17
(i32.const 0)
)
(loop $continue|1
(if
;;@ assembly/index.ts:25:20
(i32.lt_s
(get_local $1)
;;@ assembly/index.ts:25:24
(get_global $assembly/index/w)
)
(block
;;@ assembly/index.ts:31:6
(set_local $2
;;@ assembly/index.ts:31:27
(i32.add
;;@ assembly/index.ts:32:8
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.load8_u
;;@ assembly/index.ts:32:17
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/index.ts:32:23
(get_global $assembly/index/w)
)
(tee_local $2
;;@ assembly/index.ts:26:16
(if (result i32)
(get_local $1)
;;@ assembly/index.ts:26:31
(i32.sub
(get_local $1)
;;@ assembly/index.ts:26:35
(i32.const 1)
)
;;@ assembly/index.ts:26:25
(get_local $6)
)
)
)
)
;;@ assembly/index.ts:32:34
(i32.load8_u
;;@ assembly/index.ts:32:43
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/index.ts:32:49
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:32:53
(get_local $1)
)
)
)
;;@ assembly/index.ts:32:58
(i32.load8_u
;;@ assembly/index.ts:32:67
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/index.ts:32:73
(get_global $assembly/index/w)
)
(tee_local $7
;;@ assembly/index.ts:27:16
(if (result i32)
(i32.eq
(get_local $1)
;;@ assembly/index.ts:27:21
(get_local $6)
)
;;@ assembly/index.ts:27:27
(i32.const 0)
;;@ assembly/index.ts:27:31
(i32.add
(get_local $1)
;;@ assembly/index.ts:27:35
(i32.const 1)
)
)
)
)
)
)
;;@ assembly/index.ts:33:8
(i32.load8_u
;;@ assembly/index.ts:33:17
(i32.add
(i32.mul
(get_local $0)
;;@ assembly/index.ts:33:23
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:33:27
(get_local $2)
)
)
)
;;@ assembly/index.ts:33:58
(i32.load8_u
;;@ assembly/index.ts:33:67
(i32.add
(i32.mul
(get_local $0)
;;@ assembly/index.ts:33:73
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:33:77
(get_local $7)
)
)
)
;;@ assembly/index.ts:34:8
(i32.load8_u
;;@ assembly/index.ts:34:17
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/index.ts:34:23
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:34:27
(get_local $2)
)
)
)
;;@ assembly/index.ts:34:34
(i32.load8_u
;;@ assembly/index.ts:34:43
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/index.ts:34:49
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:34:53
(get_local $1)
)
)
)
;;@ assembly/index.ts:34:58
(i32.load8_u
;;@ assembly/index.ts:34:67
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/index.ts:34:73
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:34:77
(get_local $7)
)
)
)
)
;;@ assembly/index.ts:38:6
(if
;;@ assembly/index.ts:37:18
(i32.load8_u
;;@ assembly/index.ts:37:27
(i32.add
(i32.mul
(get_local $0)
;;@ assembly/index.ts:37:31
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:37:35
(get_local $1)
)
)
;;@ assembly/index.ts:39:8
(block $break|2
(br_if $break|2
(i32.eq
;;@ assembly/index.ts:39:16
(get_local $2)
;;@ assembly/index.ts:44:15
(i32.const 2)
)
)
(br_if $break|2
(i32.eq
(get_local $2)
;;@ assembly/index.ts:44:23
(i32.const 3)
)
)
;;@ assembly/index.ts:42:21
(i32.store8
;;@ assembly/index.ts:42:31
(i32.add
(i32.add
(get_global $assembly/index/s)
;;@ assembly/index.ts:42:35
(i32.mul
(get_local $0)
;;@ assembly/index.ts:42:39
(get_global $assembly/index/w)
)
)
;;@ assembly/index.ts:42:43
(get_local $1)
)
;;@ assembly/index.ts:42:46
(i32.const 0)
)
)
(if
(i32.eq
;;@ assembly/index.ts:47:16
(get_local $2)
;;@ assembly/index.ts:49:15
(i32.const 3)
)
;;@ assembly/index.ts:49:20
(i32.store8
;;@ assembly/index.ts:49:30
(i32.add
(i32.add
(get_global $assembly/index/s)
;;@ assembly/index.ts:49:34
(i32.mul
(get_local $0)
;;@ assembly/index.ts:49:38
(get_global $assembly/index/w)
)
)
;;@ assembly/index.ts:49:42
(get_local $1)
)
;;@ assembly/index.ts:49:45
(i32.const 1)
)
)
)
;;@ assembly/index.ts:25:27
(set_local $1
(i32.add
;;@ assembly/index.ts:25:29
(get_local $1)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
;;@ assembly/index.ts:21:25
(set_local $0
(i32.add
;;@ assembly/index.ts:21:27
(get_local $0)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)

View File

@ -0,0 +1,441 @@
(module
(type $iiv (func (param i32 i32)))
(type $v (func))
(import "env" "memory" (memory $0 1))
(global $assembly/index/w (mut i32) (i32.const 0))
(global $assembly/index/h (mut i32) (i32.const 0))
(global $assembly/index/s (mut i32) (i32.const 0))
(global $HEAP_BASE i32 (i32.const 4))
(export "init" (func $assembly/index/init))
(export "step" (func $assembly/index/step))
(export "memory" (memory $0))
(func $assembly/index/init (; 0 ;) (type $iiv) (param $0 i32) (param $1 i32)
;;@ assembly/index.ts:9:2
(set_global $assembly/index/w
;;@ assembly/index.ts:9:6
(get_local $0)
)
;;@ assembly/index.ts:10:2
(set_global $assembly/index/h
;;@ assembly/index.ts:10:6
(get_local $1)
)
;;@ assembly/index.ts:11:2
(set_global $assembly/index/s
;;@ assembly/index.ts:11:6
(i32.mul
(get_local $0)
;;@ assembly/index.ts:11:14
(get_local $1)
)
)
)
(func $assembly/index/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)
;;@ assembly/index.ts:16:2
(block
(set_local $0
;;@ assembly/index.ts:16:12
(i32.sub
(get_global $assembly/index/h)
;;@ assembly/index.ts:16:16
(i32.const 1)
)
)
(set_local $1
;;@ assembly/index.ts:17:12
(i32.sub
(get_global $assembly/index/w)
;;@ assembly/index.ts:17:16
(i32.const 1)
)
)
)
;;@ assembly/index.ts:21:2
(block $break|0
;;@ assembly/index.ts:21:7
(set_local $2
;;@ assembly/index.ts:21:15
(i32.const 0)
)
(loop $continue|0
(if
;;@ assembly/index.ts:21:18
(i32.lt_s
(get_local $2)
;;@ assembly/index.ts:21:22
(get_global $assembly/index/h)
)
(block
(block
(block
(set_local $3
;;@ assembly/index.ts:23:14
(if (result i32)
(i32.eq
(get_local $2)
;;@ assembly/index.ts:23:19
(i32.const 0)
)
;;@ assembly/index.ts:23:23
(get_local $0)
;;@ assembly/index.ts:23:29
(i32.sub
(get_local $2)
;;@ assembly/index.ts:23:33
(i32.const 1)
)
)
)
(set_local $4
;;@ assembly/index.ts:24:14
(if (result i32)
(i32.eq
(get_local $2)
;;@ assembly/index.ts:24:19
(get_local $0)
)
;;@ assembly/index.ts:24:25
(i32.const 0)
;;@ assembly/index.ts:24:29
(i32.add
(get_local $2)
;;@ assembly/index.ts:24:33
(i32.const 1)
)
)
)
)
;;@ assembly/index.ts:25:4
(block $break|1
;;@ assembly/index.ts:25:9
(set_local $5
;;@ assembly/index.ts:25:17
(i32.const 0)
)
(loop $continue|1
(if
;;@ assembly/index.ts:25:20
(i32.lt_s
(get_local $5)
;;@ assembly/index.ts:25:24
(get_global $assembly/index/w)
)
(block
(block
(block
(set_local $6
;;@ assembly/index.ts:26:16
(if (result i32)
(i32.eq
(get_local $5)
;;@ assembly/index.ts:26:21
(i32.const 0)
)
;;@ assembly/index.ts:26:25
(get_local $1)
;;@ assembly/index.ts:26:31
(i32.sub
(get_local $5)
;;@ assembly/index.ts:26:35
(i32.const 1)
)
)
)
(set_local $7
;;@ assembly/index.ts:27:16
(if (result i32)
(i32.eq
(get_local $5)
;;@ assembly/index.ts:27:21
(get_local $1)
)
;;@ assembly/index.ts:27:27
(i32.const 0)
;;@ assembly/index.ts:27:31
(i32.add
(get_local $5)
;;@ assembly/index.ts:27:35
(i32.const 1)
)
)
)
)
;;@ assembly/index.ts:31:6
(set_local $8
;;@ assembly/index.ts:31:27
(i32.add
;;@ assembly/index.ts:32:8
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.load8_u
;;@ assembly/index.ts:32:17
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/index.ts:32:23
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:32:27
(get_local $6)
)
)
;;@ assembly/index.ts:32:34
(i32.load8_u
;;@ assembly/index.ts:32:43
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/index.ts:32:49
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:32:53
(get_local $5)
)
)
)
;;@ assembly/index.ts:32:58
(i32.load8_u
;;@ assembly/index.ts:32:67
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/index.ts:32:73
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:32:77
(get_local $7)
)
)
)
;;@ assembly/index.ts:33:8
(i32.load8_u
;;@ assembly/index.ts:33:17
(i32.add
(i32.mul
(get_local $2)
;;@ assembly/index.ts:33:23
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:33:27
(get_local $6)
)
)
)
;;@ assembly/index.ts:33:58
(i32.load8_u
;;@ assembly/index.ts:33:67
(i32.add
(i32.mul
(get_local $2)
;;@ assembly/index.ts:33:73
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:33:77
(get_local $7)
)
)
)
;;@ assembly/index.ts:34:8
(i32.load8_u
;;@ assembly/index.ts:34:17
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/index.ts:34:23
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:34:27
(get_local $6)
)
)
)
;;@ assembly/index.ts:34:34
(i32.load8_u
;;@ assembly/index.ts:34:43
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/index.ts:34:49
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:34:53
(get_local $5)
)
)
)
;;@ assembly/index.ts:34:58
(i32.load8_u
;;@ assembly/index.ts:34:67
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/index.ts:34:73
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:34:77
(get_local $7)
)
)
)
)
;;@ assembly/index.ts:37:6
(set_local $9
;;@ assembly/index.ts:37:18
(i32.load8_u
;;@ assembly/index.ts:37:27
(i32.add
(i32.mul
(get_local $2)
;;@ assembly/index.ts:37:31
(get_global $assembly/index/w)
)
;;@ assembly/index.ts:37:35
(get_local $5)
)
)
)
;;@ assembly/index.ts:38:6
(if
;;@ assembly/index.ts:38:10
(get_local $9)
;;@ assembly/index.ts:39:8
(block $break|2
(block $case2|2
(block $case1|2
(block $case0|2
(set_local $10
;;@ assembly/index.ts:39:16
(get_local $8)
)
(br_if $case1|2
(i32.eq
(get_local $10)
;;@ assembly/index.ts:44:15
(i32.const 2)
)
)
(br_if $case2|2
(i32.eq
(get_local $10)
;;@ assembly/index.ts:44:23
(i32.const 3)
)
)
(br $case0|2)
)
;;@ assembly/index.ts:42:19
(block
;;@ assembly/index.ts:42:21
(i32.store8
;;@ assembly/index.ts:42:31
(i32.add
(i32.add
(get_global $assembly/index/s)
;;@ assembly/index.ts:42:35
(i32.mul
(get_local $2)
;;@ assembly/index.ts:42:39
(get_global $assembly/index/w)
)
)
;;@ assembly/index.ts:42:43
(get_local $5)
)
;;@ assembly/index.ts:42:46
(i32.const 0)
)
;;@ assembly/index.ts:42:50
(br $break|2)
)
)
)
)
;;@ assembly/index.ts:47:8
(block $break|3
(block $case1|3
(block $case0|3
(set_local $10
;;@ assembly/index.ts:47:16
(get_local $8)
)
(br_if $case0|3
(i32.eq
(get_local $10)
;;@ assembly/index.ts:49:15
(i32.const 3)
)
)
(br $case1|3)
)
;;@ assembly/index.ts:49:18
(block
;;@ assembly/index.ts:49:20
(i32.store8
;;@ assembly/index.ts:49:30
(i32.add
(i32.add
(get_global $assembly/index/s)
;;@ assembly/index.ts:49:34
(i32.mul
(get_local $2)
;;@ assembly/index.ts:49:38
(get_global $assembly/index/w)
)
)
;;@ assembly/index.ts:49:42
(get_local $5)
)
;;@ assembly/index.ts:49:45
(i32.const 1)
)
;;@ assembly/index.ts:49:49
(br $break|3)
)
)
)
)
)
;;@ assembly/index.ts:25:27
(set_local $5
(i32.add
;;@ assembly/index.ts:25:29
(get_local $5)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
)
)
;;@ assembly/index.ts:21:25
(set_local $2
(i32.add
;;@ assembly/index.ts:21:27
(get_local $2)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)
)

View File

@ -1,76 +0,0 @@
<html>
<head>
<style>
html, body { height: 100%; margin: 0; overflow: hidden; color: #111; background: #fff; font-family: sans-serif; }
h1 { padding: 20px; font-size: 12pt; }
a { color: #111; text-decoration: none; }
a:hover { color: #0074C1; text-decoration: underline; }
canvas { position: absolute; top: 60px; left: 20px; width: calc(100% - 40px); height: calc(100% - 80px); }
</style>
</head>
<body>
<h1>
<a href="https://en.wikipedia.org/wiki/Conway's_Game_of_Life">Conway's Game Of Life</a> in
<a href="http://assemblyscript.org">AssemblyScript</a>
</h1>
<canvas id="canvas"></canvas>
<script>
// Set up the canvas with a 2D rendering context
var cnv = document.getElementById("canvas");
var ctx = cnv.getContext("2d");
var bcr = cnv.getBoundingClientRect();
cnv.width = bcr.width | 0;
cnv.height = bcr.height | 0;
// Compute the size of the universe (here: 2px per cell)
var w = bcr.width >>> 1;
var h = bcr.height >>> 1;
var s = w * h; // memory required to store either input or output
var S = s + s; // total memory required to store input and output
// Compute the size of and instantiate the module's memory
var nPages = ((S + 0xffff) & ~0xffff) >>> 16; // aligned up in 64k units
var memory = new WebAssembly.Memory({ initial: nPages });
// Fetch and instantiate the module
WebAssembly.instantiateStreaming(fetch("game-of-life.optimized.wasm"), {
env: { memory: memory }
})
.then(module => {
var exports = module.instance.exports;
// Tell the module about the universe's width and height
exports.init(w, h);
// Fill input at [0, s-1] with random live cells
var mem = new Uint8Array(memory.buffer);
for (var y = 0; y < h; ++y)
for (var x = 0; x < w; ++x)
mem[y * w + x] = Math.random() > 0.1 ? 0 : 1;
// Update about 30 times a second
(function update() {
setTimeout(update, 30);
exports.step();
mem.copyWithin(0, s, S); // copy output at [s, S] to input at [0, s]
})();
// Keep rendering the output at [s, S]
(function render() {
requestAnimationFrame(render);
ctx.fillStyle = "rgba(238,238,238,0.67)";
ctx.fillRect(0, 0, w << 1, h << 1);
ctx.fillStyle = "#333";
for (var y = 0; y < h; ++y)
for (var x = 0; x < w; ++x)
if (mem[s + y * w + x])
ctx.fillRect(x << 1, y << 1, 2, 2);
})();
}).catch(err => {
throw err;
});
</script>
</body>
</html>

View File

@ -1,367 +0,0 @@
(module
(type $iiv (func (param i32 i32)))
(type $v (func))
(import "env" "memory" (memory $0 1))
(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))
(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)
;;@ assembly/game-of-life.ts:9:2
(set_global $assembly/game-of-life/w
;;@ assembly/game-of-life.ts:9:6
(get_local $0)
)
;;@ assembly/game-of-life.ts:10:2
(set_global $assembly/game-of-life/h
;;@ assembly/game-of-life.ts:10:6
(get_local $1)
)
;;@ assembly/game-of-life.ts:11:2
(set_global $assembly/game-of-life/s
;;@ assembly/game-of-life.ts:11:6
(i32.mul
(get_local $0)
;;@ assembly/game-of-life.ts:11:14
(get_local $1)
)
)
)
(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 $5
;;@ assembly/game-of-life.ts:16:12
(i32.sub
(get_global $assembly/game-of-life/h)
;;@ assembly/game-of-life.ts:16:16
(i32.const 1)
)
)
(set_local $6
;;@ assembly/game-of-life.ts:17:12
(i32.sub
(get_global $assembly/game-of-life/w)
;;@ assembly/game-of-life.ts:17:16
(i32.const 1)
)
)
(loop $continue|0
(if
;;@ assembly/game-of-life.ts:21:18
(i32.lt_s
(get_local $0)
;;@ assembly/game-of-life.ts:21:22
(get_global $assembly/game-of-life/h)
)
(block
(set_local $3
;;@ assembly/game-of-life.ts:23:14
(if (result i32)
(get_local $0)
;;@ assembly/game-of-life.ts:23:29
(i32.sub
(get_local $0)
;;@ assembly/game-of-life.ts:23:33
(i32.const 1)
)
;;@ assembly/game-of-life.ts:23:23
(get_local $5)
)
)
(set_local $4
;;@ assembly/game-of-life.ts:24:14
(if (result i32)
(i32.eq
(get_local $0)
;;@ assembly/game-of-life.ts:24:19
(get_local $5)
)
;;@ assembly/game-of-life.ts:24:25
(i32.const 0)
;;@ assembly/game-of-life.ts:24:29
(i32.add
(get_local $0)
;;@ assembly/game-of-life.ts:24:33
(i32.const 1)
)
)
)
;;@ assembly/game-of-life.ts:25:9
(set_local $1
;;@ assembly/game-of-life.ts:25:17
(i32.const 0)
)
(loop $continue|1
(if
;;@ assembly/game-of-life.ts:25:20
(i32.lt_s
(get_local $1)
;;@ assembly/game-of-life.ts:25:24
(get_global $assembly/game-of-life/w)
)
(block
;;@ assembly/game-of-life.ts:31:6
(set_local $2
;;@ assembly/game-of-life.ts:31:27
(i32.add
;;@ assembly/game-of-life.ts:32:8
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.load8_u
;;@ assembly/game-of-life.ts:32:17
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/game-of-life.ts:32:23
(get_global $assembly/game-of-life/w)
)
(tee_local $2
;;@ assembly/game-of-life.ts:26:16
(if (result i32)
(get_local $1)
;;@ assembly/game-of-life.ts:26:31
(i32.sub
(get_local $1)
;;@ assembly/game-of-life.ts:26:35
(i32.const 1)
)
;;@ assembly/game-of-life.ts:26:25
(get_local $6)
)
)
)
)
;;@ assembly/game-of-life.ts:32:34
(i32.load8_u
;;@ assembly/game-of-life.ts:32:43
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/game-of-life.ts:32:49
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:32:53
(get_local $1)
)
)
)
;;@ assembly/game-of-life.ts:32:58
(i32.load8_u
;;@ assembly/game-of-life.ts:32:67
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/game-of-life.ts:32:73
(get_global $assembly/game-of-life/w)
)
(tee_local $7
;;@ assembly/game-of-life.ts:27:16
(if (result i32)
(i32.eq
(get_local $1)
;;@ assembly/game-of-life.ts:27:21
(get_local $6)
)
;;@ assembly/game-of-life.ts:27:27
(i32.const 0)
;;@ assembly/game-of-life.ts:27:31
(i32.add
(get_local $1)
;;@ assembly/game-of-life.ts:27:35
(i32.const 1)
)
)
)
)
)
)
;;@ assembly/game-of-life.ts:33:8
(i32.load8_u
;;@ assembly/game-of-life.ts:33:17
(i32.add
(i32.mul
(get_local $0)
;;@ assembly/game-of-life.ts:33:23
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:33:27
(get_local $2)
)
)
)
;;@ assembly/game-of-life.ts:33:58
(i32.load8_u
;;@ assembly/game-of-life.ts:33:67
(i32.add
(i32.mul
(get_local $0)
;;@ assembly/game-of-life.ts:33:73
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:33:77
(get_local $7)
)
)
)
;;@ assembly/game-of-life.ts:34:8
(i32.load8_u
;;@ assembly/game-of-life.ts:34:17
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/game-of-life.ts:34:23
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:34:27
(get_local $2)
)
)
)
;;@ assembly/game-of-life.ts:34:34
(i32.load8_u
;;@ assembly/game-of-life.ts:34:43
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/game-of-life.ts:34:49
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:34:53
(get_local $1)
)
)
)
;;@ assembly/game-of-life.ts:34:58
(i32.load8_u
;;@ assembly/game-of-life.ts:34:67
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/game-of-life.ts:34:73
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:34:77
(get_local $7)
)
)
)
)
;;@ assembly/game-of-life.ts:38:6
(if
;;@ assembly/game-of-life.ts:37:18
(i32.load8_u
;;@ assembly/game-of-life.ts:37:27
(i32.add
(i32.mul
(get_local $0)
;;@ assembly/game-of-life.ts:37:31
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:37:35
(get_local $1)
)
)
;;@ assembly/game-of-life.ts:39:8
(block $break|2
(br_if $break|2
(i32.eq
;;@ assembly/game-of-life.ts:39:16
(get_local $2)
;;@ assembly/game-of-life.ts:44:15
(i32.const 2)
)
)
(br_if $break|2
(i32.eq
(get_local $2)
;;@ assembly/game-of-life.ts:44:23
(i32.const 3)
)
)
;;@ assembly/game-of-life.ts:42:21
(i32.store8
;;@ assembly/game-of-life.ts:42:31
(i32.add
(i32.add
(get_global $assembly/game-of-life/s)
;;@ assembly/game-of-life.ts:42:35
(i32.mul
(get_local $0)
;;@ assembly/game-of-life.ts:42:39
(get_global $assembly/game-of-life/w)
)
)
;;@ assembly/game-of-life.ts:42:43
(get_local $1)
)
;;@ assembly/game-of-life.ts:42:46
(i32.const 0)
)
)
(if
(i32.eq
;;@ assembly/game-of-life.ts:47:16
(get_local $2)
;;@ assembly/game-of-life.ts:49:15
(i32.const 3)
)
;;@ assembly/game-of-life.ts:49:20
(i32.store8
;;@ assembly/game-of-life.ts:49:30
(i32.add
(i32.add
(get_global $assembly/game-of-life/s)
;;@ assembly/game-of-life.ts:49:34
(i32.mul
(get_local $0)
;;@ assembly/game-of-life.ts:49:38
(get_global $assembly/game-of-life/w)
)
)
;;@ assembly/game-of-life.ts:49:42
(get_local $1)
)
;;@ assembly/game-of-life.ts:49:45
(i32.const 1)
)
)
)
;;@ assembly/game-of-life.ts:25:27
(set_local $1
(i32.add
;;@ assembly/game-of-life.ts:25:29
(get_local $1)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
;;@ assembly/game-of-life.ts:21:25
(set_local $0
(i32.add
;;@ assembly/game-of-life.ts:21:27
(get_local $0)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)

View File

@ -1,441 +0,0 @@
(module
(type $iiv (func (param i32 i32)))
(type $v (func))
(import "env" "memory" (memory $0 1))
(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_BASE i32 (i32.const 4))
(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)
;;@ assembly/game-of-life.ts:9:2
(set_global $assembly/game-of-life/w
;;@ assembly/game-of-life.ts:9:6
(get_local $0)
)
;;@ assembly/game-of-life.ts:10:2
(set_global $assembly/game-of-life/h
;;@ assembly/game-of-life.ts:10:6
(get_local $1)
)
;;@ assembly/game-of-life.ts:11:2
(set_global $assembly/game-of-life/s
;;@ assembly/game-of-life.ts:11:6
(i32.mul
(get_local $0)
;;@ assembly/game-of-life.ts:11:14
(get_local $1)
)
)
)
(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)
;;@ assembly/game-of-life.ts:16:2
(block
(set_local $0
;;@ assembly/game-of-life.ts:16:12
(i32.sub
(get_global $assembly/game-of-life/h)
;;@ assembly/game-of-life.ts:16:16
(i32.const 1)
)
)
(set_local $1
;;@ assembly/game-of-life.ts:17:12
(i32.sub
(get_global $assembly/game-of-life/w)
;;@ assembly/game-of-life.ts:17:16
(i32.const 1)
)
)
)
;;@ assembly/game-of-life.ts:21:2
(block $break|0
;;@ assembly/game-of-life.ts:21:7
(set_local $2
;;@ assembly/game-of-life.ts:21:15
(i32.const 0)
)
(loop $continue|0
(if
;;@ assembly/game-of-life.ts:21:18
(i32.lt_s
(get_local $2)
;;@ assembly/game-of-life.ts:21:22
(get_global $assembly/game-of-life/h)
)
(block
(block
(block
(set_local $3
;;@ assembly/game-of-life.ts:23:14
(if (result i32)
(i32.eq
(get_local $2)
;;@ assembly/game-of-life.ts:23:19
(i32.const 0)
)
;;@ assembly/game-of-life.ts:23:23
(get_local $0)
;;@ assembly/game-of-life.ts:23:29
(i32.sub
(get_local $2)
;;@ assembly/game-of-life.ts:23:33
(i32.const 1)
)
)
)
(set_local $4
;;@ assembly/game-of-life.ts:24:14
(if (result i32)
(i32.eq
(get_local $2)
;;@ assembly/game-of-life.ts:24:19
(get_local $0)
)
;;@ assembly/game-of-life.ts:24:25
(i32.const 0)
;;@ assembly/game-of-life.ts:24:29
(i32.add
(get_local $2)
;;@ assembly/game-of-life.ts:24:33
(i32.const 1)
)
)
)
)
;;@ assembly/game-of-life.ts:25:4
(block $break|1
;;@ assembly/game-of-life.ts:25:9
(set_local $5
;;@ assembly/game-of-life.ts:25:17
(i32.const 0)
)
(loop $continue|1
(if
;;@ assembly/game-of-life.ts:25:20
(i32.lt_s
(get_local $5)
;;@ assembly/game-of-life.ts:25:24
(get_global $assembly/game-of-life/w)
)
(block
(block
(block
(set_local $6
;;@ assembly/game-of-life.ts:26:16
(if (result i32)
(i32.eq
(get_local $5)
;;@ assembly/game-of-life.ts:26:21
(i32.const 0)
)
;;@ assembly/game-of-life.ts:26:25
(get_local $1)
;;@ assembly/game-of-life.ts:26:31
(i32.sub
(get_local $5)
;;@ assembly/game-of-life.ts:26:35
(i32.const 1)
)
)
)
(set_local $7
;;@ assembly/game-of-life.ts:27:16
(if (result i32)
(i32.eq
(get_local $5)
;;@ assembly/game-of-life.ts:27:21
(get_local $1)
)
;;@ assembly/game-of-life.ts:27:27
(i32.const 0)
;;@ assembly/game-of-life.ts:27:31
(i32.add
(get_local $5)
;;@ assembly/game-of-life.ts:27:35
(i32.const 1)
)
)
)
)
;;@ assembly/game-of-life.ts:31:6
(set_local $8
;;@ assembly/game-of-life.ts:31:27
(i32.add
;;@ assembly/game-of-life.ts:32:8
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.add
(i32.load8_u
;;@ assembly/game-of-life.ts:32:17
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/game-of-life.ts:32:23
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:32:27
(get_local $6)
)
)
;;@ assembly/game-of-life.ts:32:34
(i32.load8_u
;;@ assembly/game-of-life.ts:32:43
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/game-of-life.ts:32:49
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:32:53
(get_local $5)
)
)
)
;;@ assembly/game-of-life.ts:32:58
(i32.load8_u
;;@ assembly/game-of-life.ts:32:67
(i32.add
(i32.mul
(get_local $3)
;;@ assembly/game-of-life.ts:32:73
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:32:77
(get_local $7)
)
)
)
;;@ assembly/game-of-life.ts:33:8
(i32.load8_u
;;@ assembly/game-of-life.ts:33:17
(i32.add
(i32.mul
(get_local $2)
;;@ assembly/game-of-life.ts:33:23
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:33:27
(get_local $6)
)
)
)
;;@ assembly/game-of-life.ts:33:58
(i32.load8_u
;;@ assembly/game-of-life.ts:33:67
(i32.add
(i32.mul
(get_local $2)
;;@ assembly/game-of-life.ts:33:73
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:33:77
(get_local $7)
)
)
)
;;@ assembly/game-of-life.ts:34:8
(i32.load8_u
;;@ assembly/game-of-life.ts:34:17
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/game-of-life.ts:34:23
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:34:27
(get_local $6)
)
)
)
;;@ assembly/game-of-life.ts:34:34
(i32.load8_u
;;@ assembly/game-of-life.ts:34:43
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/game-of-life.ts:34:49
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:34:53
(get_local $5)
)
)
)
;;@ assembly/game-of-life.ts:34:58
(i32.load8_u
;;@ assembly/game-of-life.ts:34:67
(i32.add
(i32.mul
(get_local $4)
;;@ assembly/game-of-life.ts:34:73
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:34:77
(get_local $7)
)
)
)
)
;;@ assembly/game-of-life.ts:37:6
(set_local $9
;;@ assembly/game-of-life.ts:37:18
(i32.load8_u
;;@ assembly/game-of-life.ts:37:27
(i32.add
(i32.mul
(get_local $2)
;;@ assembly/game-of-life.ts:37:31
(get_global $assembly/game-of-life/w)
)
;;@ assembly/game-of-life.ts:37:35
(get_local $5)
)
)
)
;;@ assembly/game-of-life.ts:38:6
(if
;;@ assembly/game-of-life.ts:38:10
(get_local $9)
;;@ assembly/game-of-life.ts:39:8
(block $break|2
(block $case2|2
(block $case1|2
(block $case0|2
(set_local $10
;;@ assembly/game-of-life.ts:39:16
(get_local $8)
)
(br_if $case1|2
(i32.eq
(get_local $10)
;;@ assembly/game-of-life.ts:44:15
(i32.const 2)
)
)
(br_if $case2|2
(i32.eq
(get_local $10)
;;@ assembly/game-of-life.ts:44:23
(i32.const 3)
)
)
(br $case0|2)
)
;;@ assembly/game-of-life.ts:42:19
(block
;;@ assembly/game-of-life.ts:42:21
(i32.store8
;;@ assembly/game-of-life.ts:42:31
(i32.add
(i32.add
(get_global $assembly/game-of-life/s)
;;@ assembly/game-of-life.ts:42:35
(i32.mul
(get_local $2)
;;@ assembly/game-of-life.ts:42:39
(get_global $assembly/game-of-life/w)
)
)
;;@ assembly/game-of-life.ts:42:43
(get_local $5)
)
;;@ assembly/game-of-life.ts:42:46
(i32.const 0)
)
;;@ assembly/game-of-life.ts:42:50
(br $break|2)
)
)
)
)
;;@ assembly/game-of-life.ts:47:8
(block $break|3
(block $case1|3
(block $case0|3
(set_local $10
;;@ assembly/game-of-life.ts:47:16
(get_local $8)
)
(br_if $case0|3
(i32.eq
(get_local $10)
;;@ assembly/game-of-life.ts:49:15
(i32.const 3)
)
)
(br $case1|3)
)
;;@ assembly/game-of-life.ts:49:18
(block
;;@ assembly/game-of-life.ts:49:20
(i32.store8
;;@ assembly/game-of-life.ts:49:30
(i32.add
(i32.add
(get_global $assembly/game-of-life/s)
;;@ assembly/game-of-life.ts:49:34
(i32.mul
(get_local $2)
;;@ assembly/game-of-life.ts:49:38
(get_global $assembly/game-of-life/w)
)
)
;;@ assembly/game-of-life.ts:49:42
(get_local $5)
)
;;@ assembly/game-of-life.ts:49:45
(i32.const 1)
)
;;@ assembly/game-of-life.ts:49:49
(br $break|3)
)
)
)
)
)
;;@ assembly/game-of-life.ts:25:27
(set_local $5
(i32.add
;;@ assembly/game-of-life.ts:25:29
(get_local $5)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
)
)
;;@ assembly/game-of-life.ts:21:25
(set_local $2
(i32.add
;;@ assembly/game-of-life.ts:21:27
(get_local $2)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)
)

View File

@ -0,0 +1,90 @@
<!DOCTYPE html>
<html>
<head>
<title>Conway's Game Of Life - AssemblyScript</title>
<style>
html, body { height: 100%; margin: 0; overflow: hidden; color: #111; background: #fff; font-family: sans-serif; }
h1 { padding: 20px; font-size: 12pt; }
a { color: #111; text-decoration: none; }
a:hover { color: #851ba7; text-decoration: underline; }
canvas { position: absolute; top: 60px; left: 20px; width: calc(100% - 40px); height: calc(100% - 80px); background: #eee; }
</style>
</head>
<body>
<h1>
<a href="https://en.wikipedia.org/wiki/Conway's_Game_of_Life">Conway's Game Of Life</a> in
<a href="http://assemblyscript.org">AssemblyScript</a>
</h1>
<canvas></canvas>
<script>
// Set up the canvas with a 2D rendering context
var cnv = document.getElementsByTagName("canvas")[0];
var ctx = cnv.getContext("2d");
var bcr = cnv.getBoundingClientRect();
// Compute the size of the universe (here: 2px per cell)
var width = bcr.width >>> 1;
var height = bcr.height >>> 1;
var size = width * height;
var byteSize = size + size; // input & output
cnv.width = width;
cnv.height = height;
cnv.style = style = `
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: -o-crisp-edges;
image-rendering: pixelated;
-ms-interpolation-mode: nearest-neighbor;
`;
ctx.imageSmoothingEnabled = false;
// Compute the size of and instantiate the module's memory
var memory = new WebAssembly.Memory({ initial: ((byteSize + 0xffff) & ~0xffff) >>> 16 });
// Fetch and instantiate the module
fetch("build/optimized.wasm")
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, {
env: { memory: memory }
}))
.then(module => {
var exports = module.instance.exports;
// Tell the module about the universe's width and height
exports.init(width, height);
// Fill input at [0, s-1] with random live cells
var mem = new Uint8Array(memory.buffer);
for (var y = 0; y < height; ++y)
for (var x = 0; x < width; ++x)
mem[y * width + x] = Math.random() > 0.1 ? 0 : 1;
// Update about 30 times a second
(function update() {
setTimeout(update, 1000 / 30);
exports.step();
mem.copyWithin(0, size, byteSize); // copy output to input
})();
// Keep rendering the output at [s, S]
(function render() {
requestAnimationFrame(render);
var imageData = ctx.createImageData(width, height);
var colors = new Uint32Array(imageData.data.buffer);
for (let y = 0; y < height; ++y) {
for (let x = 0; x < width; ++x) {
if (mem[size + y * width + x]) colors[y * width + x] = 0xff851ba7;
}
}
ctx.putImageData(imageData, 0, 0);
})();
}).catch(err => {
throw err;
});
</script>
</body>
</html>

View File

@ -3,11 +3,12 @@
"version": "1.0.0", "version": "1.0.0",
"private": true, "private": true,
"scripts": { "scripts": {
"build": "npm run build:untouched && npm run build:optimized", "asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --importMemory --sourceMap --validate --measure",
"build:untouched": "asc assembly/game-of-life.ts -b game-of-life.untouched.wasm -t game-of-life.untouched.wat --importMemory --validate --sourceMap --measure", "asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -O3 --importMemory --sourceMap --validate --noDebug --measure",
"build:optimized": "asc -O3 assembly/game-of-life.ts -b game-of-life.optimized.wasm -t game-of-life.optimized.wat --importMemory --validate --sourceMap --noDebug --measure" "asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"server": "http-server . -o -c-1"
}, },
"devDependencies": { "devDependencies": {
"opn": "^5.3.0" "http-server": "^0.11.1"
} }
} }

View File

@ -1,32 +0,0 @@
var http = require("http");
var fs = require("fs");
var open = require("opn");
const PORT = 9080;
http.createServer((req, res) => {
var url = req.url;
console.log(req.method + " " + url);
if (/^\/([\w\-_]+(\.\w+)+)?$/i.test(url)) {
if (url === "/") url = "/game-of-life.html";
fs.readFile(__dirname + url, function(err, data) {
if (err) {
res.writeHeader(404);
} else {
res.writeHeader(200, {
"Content-Type":
/\.wasm$/.test(url) ? "application/wasm" :
/\.(json|map)$/.test(url) ? "application/json"
: "text/html"
});
res.write(data);
}
res.end();
});
} else {
res.writeHeader(404);
res.end();
}
}).listen(PORT, () => {
open("http://localhost:9080/");
});

View File

@ -0,0 +1,21 @@
Mandelbrot Set
==============
An [AssemblyScript](http://assemblyscript.org) example. Renders the Mandelbrot set to a canvas. Compiles to ~350 bytes of optimized WASM.
Instructions
------------
First, install the development dependencies:
```
$> npm install
```
Now, to build [assembly/index.ts](./assembly/index.ts) to an untouched and an optimized `.wasm` including their respective `.wat` representations, run:
```
$> npm run asbuild
```
Afterwards, run `npm run server` to start a <a href="http://127.0.0.1:8080">local server</a>. Should also automatically launch a browser.

View File

@ -0,0 +1,19 @@
export function update(width: u32, height: u32, max: u32): void {
var translateX = width / 1.6;
var translateY = height / 2.0;
var scale = 4.0 / width;
for (let y: u32 = 0; y < height; ++y) {
let imaginary = (y - translateY) * scale;
for (let x: u32 = 0; x < width; ++x) {
let real = (x - translateX) * scale;
let iteration = <u32>0;
for (let ix = 0.0, iy = 0.0; ix * ix + iy * iy <= 4.0; ++iteration) {
let t = ix * ix - iy * iy + real;
iy = 2.0 * ix * iy + imaginary;
ix = t;
if (++iteration >= max) break;
}
store<u8>(y * width + x, iteration * 255 / max);
}
}
}

View File

@ -0,0 +1,6 @@
{
"extends": "../../../std/assembly.d.ts",
"include": [
"./**/*.ts"
]
}

3
examples/mandelbrot/build/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.wasm
*.wasm.map
*.asm.js

Binary file not shown.

View File

@ -0,0 +1,261 @@
(module
(type $iiiv (func (param i32 i32 i32)))
(memory $0 1)
(export "update" (func $assembly/index/update))
(export "memory" (memory $0))
(func $assembly/index/update (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 f64)
(local $4 f64)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(local $8 f64)
(local $9 f64)
(local $10 f64)
(local $11 f64)
(local $12 f64)
(local $13 f64)
;;@ assembly/index.ts:2:2
(set_local $9
;;@ assembly/index.ts:2:19
(f64.div
(f64.convert_u/i32
(get_local $0)
)
;;@ assembly/index.ts:2:27
(f64.const 1.6)
)
)
;;@ assembly/index.ts:3:2
(set_local $10
;;@ assembly/index.ts:3:19
(f64.div
(f64.convert_u/i32
(get_local $1)
)
;;@ assembly/index.ts:3:28
(f64.const 2)
)
)
;;@ assembly/index.ts:4:2
(set_local $8
;;@ assembly/index.ts:4:14
(f64.div
(f64.const 4)
(f64.convert_u/i32
;;@ assembly/index.ts:4:20
(get_local $0)
)
)
)
(loop $continue|0
(if
;;@ assembly/index.ts:5:23
(i32.lt_u
(get_local $5)
;;@ assembly/index.ts:5:27
(get_local $1)
)
(block
;;@ assembly/index.ts:6:4
(set_local $11
;;@ assembly/index.ts:6:20
(f64.mul
(f64.sub
(f64.convert_u/i32
;;@ assembly/index.ts:6:21
(get_local $5)
)
;;@ assembly/index.ts:6:25
(get_local $10)
)
;;@ assembly/index.ts:6:39
(get_local $8)
)
)
;;@ assembly/index.ts:7:9
(set_local $6
;;@ assembly/index.ts:7:22
(i32.const 0)
)
(loop $continue|1
(if
;;@ assembly/index.ts:7:25
(i32.lt_u
(get_local $6)
;;@ assembly/index.ts:7:29
(get_local $0)
)
(block
;;@ assembly/index.ts:8:6
(set_local $12
;;@ assembly/index.ts:8:17
(f64.mul
(f64.sub
(f64.convert_u/i32
;;@ assembly/index.ts:8:18
(get_local $6)
)
;;@ assembly/index.ts:8:22
(get_local $9)
)
;;@ assembly/index.ts:8:36
(get_local $8)
)
)
;;@ assembly/index.ts:9:6
(set_local $7
;;@ assembly/index.ts:9:22
(i32.const 0)
)
;;@ assembly/index.ts:10:6
(block $break|2
(set_local $3
;;@ assembly/index.ts:10:20
(f64.const 0)
)
(set_local $4
;;@ assembly/index.ts:10:30
(f64.const 0)
)
(loop $continue|2
(if
;;@ assembly/index.ts:10:35
(f64.le
(f64.add
(f64.mul
(get_local $3)
;;@ assembly/index.ts:10:40
(get_local $3)
)
;;@ assembly/index.ts:10:45
(f64.mul
(get_local $4)
;;@ assembly/index.ts:10:50
(get_local $4)
)
)
;;@ assembly/index.ts:10:56
(f64.const 4)
)
(block
;;@ assembly/index.ts:11:8
(set_local $13
;;@ assembly/index.ts:11:16
(f64.add
(f64.sub
(f64.mul
(get_local $3)
;;@ assembly/index.ts:11:21
(get_local $3)
)
;;@ assembly/index.ts:11:26
(f64.mul
(get_local $4)
;;@ assembly/index.ts:11:31
(get_local $4)
)
)
;;@ assembly/index.ts:11:36
(get_local $12)
)
)
;;@ assembly/index.ts:12:8
(set_local $4
;;@ assembly/index.ts:12:13
(f64.add
(f64.mul
(f64.mul
(f64.const 2)
;;@ assembly/index.ts:12:19
(get_local $3)
)
;;@ assembly/index.ts:12:24
(get_local $4)
)
;;@ assembly/index.ts:12:29
(get_local $11)
)
)
;;@ assembly/index.ts:13:8
(set_local $3
;;@ assembly/index.ts:13:13
(get_local $13)
)
;;@ assembly/index.ts:14:32
(br_if $break|2
;;@ assembly/index.ts:14:12
(i32.ge_u
(tee_local $7
(i32.add
;;@ assembly/index.ts:14:14
(get_local $7)
(i32.const 1)
)
)
;;@ assembly/index.ts:14:27
(get_local $2)
)
)
;;@ assembly/index.ts:10:61
(set_local $7
(i32.add
;;@ assembly/index.ts:10:63
(get_local $7)
(i32.const 1)
)
)
(br $continue|2)
)
)
)
)
;;@ assembly/index.ts:16:6
(i32.store8
;;@ assembly/index.ts:16:16
(i32.add
(i32.mul
(get_local $5)
;;@ assembly/index.ts:16:20
(get_local $0)
)
;;@ assembly/index.ts:16:28
(get_local $6)
)
;;@ assembly/index.ts:16:31
(i32.div_u
(i32.mul
(get_local $7)
;;@ assembly/index.ts:16:43
(i32.const 255)
)
;;@ assembly/index.ts:16:49
(get_local $2)
)
)
;;@ assembly/index.ts:7:36
(set_local $6
(i32.add
;;@ assembly/index.ts:7:38
(get_local $6)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
;;@ assembly/index.ts:5:35
(set_local $5
(i32.add
;;@ assembly/index.ts:5:37
(get_local $5)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)

View File

@ -0,0 +1,283 @@
(module
(type $iiiv (func (param i32 i32 i32)))
(global $HEAP_BASE i32 (i32.const 4))
(memory $0 1)
(export "update" (func $assembly/index/update))
(export "memory" (memory $0))
(func $assembly/index/update (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 f64)
(local $4 f64)
(local $5 f64)
(local $6 i32)
(local $7 f64)
(local $8 i32)
(local $9 f64)
(local $10 i32)
(local $11 f64)
(local $12 f64)
(local $13 f64)
;;@ assembly/index.ts:2:2
(set_local $3
;;@ assembly/index.ts:2:19
(f64.div
(f64.convert_u/i32
(get_local $0)
)
;;@ assembly/index.ts:2:27
(f64.const 1.6)
)
)
;;@ assembly/index.ts:3:2
(set_local $4
;;@ assembly/index.ts:3:19
(f64.div
(f64.convert_u/i32
(get_local $1)
)
;;@ assembly/index.ts:3:28
(f64.const 2)
)
)
;;@ assembly/index.ts:4:2
(set_local $5
;;@ assembly/index.ts:4:14
(f64.div
(f64.const 4)
(f64.convert_u/i32
;;@ assembly/index.ts:4:20
(get_local $0)
)
)
)
;;@ assembly/index.ts:5:2
(block $break|0
;;@ assembly/index.ts:5:7
(set_local $6
;;@ assembly/index.ts:5:20
(i32.const 0)
)
(loop $continue|0
(if
;;@ assembly/index.ts:5:23
(i32.lt_u
(get_local $6)
;;@ assembly/index.ts:5:27
(get_local $1)
)
(block
(block
;;@ assembly/index.ts:6:4
(set_local $7
;;@ assembly/index.ts:6:20
(f64.mul
(f64.sub
(f64.convert_u/i32
;;@ assembly/index.ts:6:21
(get_local $6)
)
;;@ assembly/index.ts:6:25
(get_local $4)
)
;;@ assembly/index.ts:6:39
(get_local $5)
)
)
;;@ assembly/index.ts:7:4
(block $break|1
;;@ assembly/index.ts:7:9
(set_local $8
;;@ assembly/index.ts:7:22
(i32.const 0)
)
(loop $continue|1
(if
;;@ assembly/index.ts:7:25
(i32.lt_u
(get_local $8)
;;@ assembly/index.ts:7:29
(get_local $0)
)
(block
(block
;;@ assembly/index.ts:8:6
(set_local $9
;;@ assembly/index.ts:8:17
(f64.mul
(f64.sub
(f64.convert_u/i32
;;@ assembly/index.ts:8:18
(get_local $8)
)
;;@ assembly/index.ts:8:22
(get_local $3)
)
;;@ assembly/index.ts:8:36
(get_local $5)
)
)
;;@ assembly/index.ts:9:6
(set_local $10
;;@ assembly/index.ts:9:22
(i32.const 0)
)
;;@ assembly/index.ts:10:6
(block $break|2
(block
(set_local $11
;;@ assembly/index.ts:10:20
(f64.const 0)
)
(set_local $12
;;@ assembly/index.ts:10:30
(f64.const 0)
)
)
(loop $continue|2
(if
;;@ assembly/index.ts:10:35
(f64.le
(f64.add
(f64.mul
(get_local $11)
;;@ assembly/index.ts:10:40
(get_local $11)
)
;;@ assembly/index.ts:10:45
(f64.mul
(get_local $12)
;;@ assembly/index.ts:10:50
(get_local $12)
)
)
;;@ assembly/index.ts:10:56
(f64.const 4)
)
(block
(block
;;@ assembly/index.ts:11:8
(set_local $13
;;@ assembly/index.ts:11:16
(f64.add
(f64.sub
(f64.mul
(get_local $11)
;;@ assembly/index.ts:11:21
(get_local $11)
)
;;@ assembly/index.ts:11:26
(f64.mul
(get_local $12)
;;@ assembly/index.ts:11:31
(get_local $12)
)
)
;;@ assembly/index.ts:11:36
(get_local $9)
)
)
;;@ assembly/index.ts:12:8
(set_local $12
;;@ assembly/index.ts:12:13
(f64.add
(f64.mul
(f64.mul
(f64.const 2)
;;@ assembly/index.ts:12:19
(get_local $11)
)
;;@ assembly/index.ts:12:24
(get_local $12)
)
;;@ assembly/index.ts:12:29
(get_local $7)
)
)
;;@ assembly/index.ts:13:8
(set_local $11
;;@ assembly/index.ts:13:13
(get_local $13)
)
;;@ assembly/index.ts:14:8
(if
;;@ assembly/index.ts:14:12
(i32.ge_u
(tee_local $10
(i32.add
;;@ assembly/index.ts:14:14
(get_local $10)
(i32.const 1)
)
)
;;@ assembly/index.ts:14:27
(get_local $2)
)
;;@ assembly/index.ts:14:32
(br $break|2)
)
)
;;@ assembly/index.ts:10:61
(set_local $10
(i32.add
;;@ assembly/index.ts:10:63
(get_local $10)
(i32.const 1)
)
)
(br $continue|2)
)
)
)
)
;;@ assembly/index.ts:16:6
(i32.store8
;;@ assembly/index.ts:16:16
(i32.add
(i32.mul
(get_local $6)
;;@ assembly/index.ts:16:20
(get_local $0)
)
;;@ assembly/index.ts:16:28
(get_local $8)
)
;;@ assembly/index.ts:16:31
(i32.div_u
(i32.mul
(get_local $10)
;;@ assembly/index.ts:16:43
(i32.const 255)
)
;;@ assembly/index.ts:16:49
(get_local $2)
)
)
)
;;@ assembly/index.ts:7:36
(set_local $8
(i32.add
;;@ assembly/index.ts:7:38
(get_local $8)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
)
)
;;@ assembly/index.ts:5:35
(set_local $6
(i32.add
;;@ assembly/index.ts:5:37
(get_local $6)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)
)

View File

@ -0,0 +1,78 @@
<!DOCTYPE html>
<html>
<head>
<title>Mandelbrot Set - AssemblyScript</title>
<style>
html, body { height: 100%; margin: 0; overflow: hidden; color: #111; background: #fff; font-family: sans-serif; }
h1 { padding: 20px; font-size: 12pt; }
a { color: #111; text-decoration: none; }
a:hover { color: #0074C1; text-decoration: underline; }
canvas { position: absolute; top: 60px; left: 20px; width: calc(100% - 40px); height: calc(100% - 80px); background: #eee; }
</style>
</head>
<body>
<h1>
<a href="https://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot Set</a> in
<a href="http://assemblyscript.org">AssemblyScript</a>
</h1>
<canvas></canvas>
<script>
// Set up a 2D rendering context
var cnv = document.getElementsByTagName("canvas")[0];
var ctx = cnv.getContext("2d");
var bcr = cnv.getBoundingClientRect();
cnv.width = bcr.width | 0;
cnv.height = bcr.height | 0;
// Fetch and instantiate the module
fetch("build/optimized.wasm")
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer))
.then(module => {
var exports = module.instance.exports;
var memory = exports.memory;
// Compute required memory size and grow if necessary
var width = cnv.width | 0;
var height = cnv.height | 0;
var byteSize = width * height;
if (memory.buffer.byteLength < byteSize) {
let pages = ((byteSize - memory.buffer.byteLength + 0xffff) & ~0xffff) >>> 16;
memory.grow(pages);
}
// Update
exports.update(width, height, 100);
// Render
var mem = new Uint8Array(memory.buffer);
var imageData = ctx.createImageData(width, height);
var argb = new Uint32Array(imageData.data.buffer);
for (let y = 0; y < height; ++y) {
let yx = y * width;
for (let x = 0; x < width; ++x) argb[yx + x] = colors[mem[yx + x]];
}
ctx.putImageData(imageData, 0, 0);
});
// Compute a nice set of colors using a gradient
var colors = (() => {
var cnv = document.createElement("canvas");
cnv.width = 256;
cnv.height = 1;
var ctx = cnv.getContext("2d");
var grd = ctx.createLinearGradient(0, 0, 256, 0);
grd.addColorStop(0, "#000764");
grd.addColorStop(0.16, "#2068CB");
grd.addColorStop(0.42, "#EDFFFF");
grd.addColorStop(0.6425, "#FFAA00");
grd.addColorStop(0.8575, "#000200");
grd.addColorStop(1.0, "#000764");
ctx.fillStyle = grd;
ctx.fillRect(0, 0, 256, 1);
return new Uint32Array(ctx.getImageData(0, 0, 256, 1).data.buffer);
})();
</script>
</body>
</html>

View File

@ -0,0 +1,6 @@
const fs = require("fs");
const compiled = new WebAssembly.Module(fs.readFileSync(__dirname + "/build/optimized.wasm"));
const imports = {};
Object.defineProperty(module, "exports", {
get: () => new WebAssembly.Instance(compiled, imports).exports
});

View File

@ -0,0 +1,14 @@
{
"name": "@assemblyscript/mandelbrot-example",
"version": "1.0.0",
"private": true,
"scripts": {
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --measure",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -O3 --sourceMap --validate --noDebug --measure",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"server": "http-server . -o -c-1"
},
"devDependencies": {
"http-server": "^0.11.1"
}
}

View File

@ -1,28 +1,28 @@
(module (module
(type $iiv (func (param i32 i32))) (type $iiv (func (param i32 i32)))
(type $v (func)) (type $v (func))
(global $../../examples/game-of-life/assembly/game-of-life/w (mut i32) (i32.const 0)) (global $../../examples/game-of-life/assembly/index/w (mut i32) (i32.const 0))
(global $../../examples/game-of-life/assembly/game-of-life/h (mut i32) (i32.const 0)) (global $../../examples/game-of-life/assembly/index/h (mut i32) (i32.const 0))
(global $../../examples/game-of-life/assembly/game-of-life/s (mut i32) (i32.const 0)) (global $../../examples/game-of-life/assembly/index/s (mut i32) (i32.const 0))
(memory $0 1) (memory $0 1)
(export "init" (func $../../examples/game-of-life/assembly/game-of-life/init)) (export "init" (func $../../examples/game-of-life/assembly/index/init))
(export "step" (func $../../examples/game-of-life/assembly/game-of-life/step)) (export "step" (func $../../examples/game-of-life/assembly/index/step))
(export "memory" (memory $0)) (export "memory" (memory $0))
(func $../../examples/game-of-life/assembly/game-of-life/init (; 0 ;) (type $iiv) (param $0 i32) (param $1 i32) (func $../../examples/game-of-life/assembly/index/init (; 0 ;) (type $iiv) (param $0 i32) (param $1 i32)
(set_global $../../examples/game-of-life/assembly/game-of-life/w (set_global $../../examples/game-of-life/assembly/index/w
(get_local $0) (get_local $0)
) )
(set_global $../../examples/game-of-life/assembly/game-of-life/h (set_global $../../examples/game-of-life/assembly/index/h
(get_local $1) (get_local $1)
) )
(set_global $../../examples/game-of-life/assembly/game-of-life/s (set_global $../../examples/game-of-life/assembly/index/s
(i32.mul (i32.mul
(get_local $0) (get_local $0)
(get_local $1) (get_local $1)
) )
) )
) )
(func $../../examples/game-of-life/assembly/game-of-life/step (; 1 ;) (type $v) (func $../../examples/game-of-life/assembly/index/step (; 1 ;) (type $v)
(local $0 i32) (local $0 i32)
(local $1 i32) (local $1 i32)
(local $2 i32) (local $2 i32)
@ -33,13 +33,13 @@
(local $7 i32) (local $7 i32)
(set_local $5 (set_local $5
(i32.sub (i32.sub
(get_global $../../examples/game-of-life/assembly/game-of-life/h) (get_global $../../examples/game-of-life/assembly/index/h)
(i32.const 1) (i32.const 1)
) )
) )
(set_local $6 (set_local $6
(i32.sub (i32.sub
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
(i32.const 1) (i32.const 1)
) )
) )
@ -47,7 +47,7 @@
(if (if
(i32.lt_s (i32.lt_s
(get_local $0) (get_local $0)
(get_global $../../examples/game-of-life/assembly/game-of-life/h) (get_global $../../examples/game-of-life/assembly/index/h)
) )
(block (block
(set_local $3 (set_local $3
@ -80,7 +80,7 @@
(if (if
(i32.lt_s (i32.lt_s
(get_local $1) (get_local $1)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(block (block
(set_local $2 (set_local $2
@ -95,7 +95,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $3) (get_local $3)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(tee_local $2 (tee_local $2
(select (select
@ -113,7 +113,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $3) (get_local $3)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $1) (get_local $1)
) )
@ -123,7 +123,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $3) (get_local $3)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(tee_local $7 (tee_local $7
(select (select
@ -145,7 +145,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $0) (get_local $0)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $2) (get_local $2)
) )
@ -155,7 +155,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $0) (get_local $0)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $7) (get_local $7)
) )
@ -165,7 +165,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $4) (get_local $4)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $2) (get_local $2)
) )
@ -175,7 +175,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $4) (get_local $4)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $1) (get_local $1)
) )
@ -185,7 +185,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $4) (get_local $4)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $7) (get_local $7)
) )
@ -197,7 +197,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $0) (get_local $0)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $1) (get_local $1)
) )
@ -218,10 +218,10 @@
(i32.store8 (i32.store8
(i32.add (i32.add
(i32.add (i32.add
(get_global $../../examples/game-of-life/assembly/game-of-life/s) (get_global $../../examples/game-of-life/assembly/index/s)
(i32.mul (i32.mul
(get_local $0) (get_local $0)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
) )
(get_local $1) (get_local $1)
@ -237,10 +237,10 @@
(i32.store8 (i32.store8
(i32.add (i32.add
(i32.add (i32.add
(get_global $../../examples/game-of-life/assembly/game-of-life/s) (get_global $../../examples/game-of-life/assembly/index/s)
(i32.mul (i32.mul
(get_local $0) (get_local $0)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
) )
(get_local $1) (get_local $1)

View File

@ -1 +1 @@
export { init, step } from "../../examples/game-of-life/assembly/game-of-life"; export { init, step } from "../../examples/game-of-life/assembly/index";

View File

@ -1,29 +1,29 @@
(module (module
(type $iiv (func (param i32 i32))) (type $iiv (func (param i32 i32)))
(type $v (func)) (type $v (func))
(global $../../examples/game-of-life/assembly/game-of-life/w (mut i32) (i32.const 0)) (global $../../examples/game-of-life/assembly/index/w (mut i32) (i32.const 0))
(global $../../examples/game-of-life/assembly/game-of-life/h (mut i32) (i32.const 0)) (global $../../examples/game-of-life/assembly/index/h (mut i32) (i32.const 0))
(global $../../examples/game-of-life/assembly/game-of-life/s (mut i32) (i32.const 0)) (global $../../examples/game-of-life/assembly/index/s (mut i32) (i32.const 0))
(global $HEAP_BASE i32 (i32.const 4)) (global $HEAP_BASE i32 (i32.const 4))
(memory $0 1) (memory $0 1)
(export "init" (func $../../examples/game-of-life/assembly/game-of-life/init)) (export "init" (func $../../examples/game-of-life/assembly/index/init))
(export "step" (func $../../examples/game-of-life/assembly/game-of-life/step)) (export "step" (func $../../examples/game-of-life/assembly/index/step))
(export "memory" (memory $0)) (export "memory" (memory $0))
(func $../../examples/game-of-life/assembly/game-of-life/init (; 0 ;) (type $iiv) (param $0 i32) (param $1 i32) (func $../../examples/game-of-life/assembly/index/init (; 0 ;) (type $iiv) (param $0 i32) (param $1 i32)
(set_global $../../examples/game-of-life/assembly/game-of-life/w (set_global $../../examples/game-of-life/assembly/index/w
(get_local $0) (get_local $0)
) )
(set_global $../../examples/game-of-life/assembly/game-of-life/h (set_global $../../examples/game-of-life/assembly/index/h
(get_local $1) (get_local $1)
) )
(set_global $../../examples/game-of-life/assembly/game-of-life/s (set_global $../../examples/game-of-life/assembly/index/s
(i32.mul (i32.mul
(get_local $0) (get_local $0)
(get_local $1) (get_local $1)
) )
) )
) )
(func $../../examples/game-of-life/assembly/game-of-life/step (; 1 ;) (type $v) (func $../../examples/game-of-life/assembly/index/step (; 1 ;) (type $v)
(local $0 i32) (local $0 i32)
(local $1 i32) (local $1 i32)
(local $2 i32) (local $2 i32)
@ -38,13 +38,13 @@
(block (block
(set_local $0 (set_local $0
(i32.sub (i32.sub
(get_global $../../examples/game-of-life/assembly/game-of-life/h) (get_global $../../examples/game-of-life/assembly/index/h)
(i32.const 1) (i32.const 1)
) )
) )
(set_local $1 (set_local $1
(i32.sub (i32.sub
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
(i32.const 1) (i32.const 1)
) )
) )
@ -57,7 +57,7 @@
(if (if
(i32.lt_s (i32.lt_s
(get_local $2) (get_local $2)
(get_global $../../examples/game-of-life/assembly/game-of-life/h) (get_global $../../examples/game-of-life/assembly/index/h)
) )
(block (block
(block (block
@ -97,7 +97,7 @@
(if (if
(i32.lt_s (i32.lt_s
(get_local $5) (get_local $5)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(block (block
(block (block
@ -141,7 +141,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $3) (get_local $3)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $6) (get_local $6)
) )
@ -150,7 +150,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $3) (get_local $3)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $5) (get_local $5)
) )
@ -160,7 +160,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $3) (get_local $3)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $7) (get_local $7)
) )
@ -170,7 +170,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $2) (get_local $2)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $6) (get_local $6)
) )
@ -180,7 +180,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $2) (get_local $2)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $7) (get_local $7)
) )
@ -190,7 +190,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $4) (get_local $4)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $6) (get_local $6)
) )
@ -200,7 +200,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $4) (get_local $4)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $5) (get_local $5)
) )
@ -210,7 +210,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $4) (get_local $4)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $7) (get_local $7)
) )
@ -222,7 +222,7 @@
(i32.add (i32.add
(i32.mul (i32.mul
(get_local $2) (get_local $2)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
(get_local $5) (get_local $5)
) )
@ -255,10 +255,10 @@
(i32.store8 (i32.store8
(i32.add (i32.add
(i32.add (i32.add
(get_global $../../examples/game-of-life/assembly/game-of-life/s) (get_global $../../examples/game-of-life/assembly/index/s)
(i32.mul (i32.mul
(get_local $2) (get_local $2)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
) )
(get_local $5) (get_local $5)
@ -288,10 +288,10 @@
(i32.store8 (i32.store8
(i32.add (i32.add
(i32.add (i32.add
(get_global $../../examples/game-of-life/assembly/game-of-life/s) (get_global $../../examples/game-of-life/assembly/index/s)
(i32.mul (i32.mul
(get_local $2) (get_local $2)
(get_global $../../examples/game-of-life/assembly/game-of-life/w) (get_global $../../examples/game-of-life/assembly/index/w)
) )
) )
(get_local $5) (get_local $5)

View File

@ -0,0 +1,196 @@
(module
(type $iiiv (func (param i32 i32 i32)))
(memory $0 1)
(export "update" (func $../../examples/mandelbrot/assembly/index/update))
(export "memory" (memory $0))
(func $../../examples/mandelbrot/assembly/index/update (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 f64)
(local $4 f64)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(local $8 f64)
(local $9 f64)
(local $10 f64)
(local $11 f64)
(local $12 f64)
(local $13 f64)
(set_local $9
(f64.div
(f64.convert_u/i32
(get_local $0)
)
(f64.const 1.6)
)
)
(set_local $10
(f64.div
(f64.convert_u/i32
(get_local $1)
)
(f64.const 2)
)
)
(set_local $8
(f64.div
(f64.const 4)
(f64.convert_u/i32
(get_local $0)
)
)
)
(loop $continue|0
(if
(i32.lt_u
(get_local $5)
(get_local $1)
)
(block
(set_local $11
(f64.mul
(f64.sub
(f64.convert_u/i32
(get_local $5)
)
(get_local $10)
)
(get_local $8)
)
)
(set_local $6
(i32.const 0)
)
(loop $continue|1
(if
(i32.lt_u
(get_local $6)
(get_local $0)
)
(block
(set_local $12
(f64.mul
(f64.sub
(f64.convert_u/i32
(get_local $6)
)
(get_local $9)
)
(get_local $8)
)
)
(set_local $7
(i32.const 0)
)
(block $break|2
(set_local $3
(f64.const 0)
)
(set_local $4
(f64.const 0)
)
(loop $continue|2
(if
(f64.le
(f64.add
(f64.mul
(get_local $3)
(get_local $3)
)
(f64.mul
(get_local $4)
(get_local $4)
)
)
(f64.const 4)
)
(block
(set_local $13
(f64.add
(f64.sub
(f64.mul
(get_local $3)
(get_local $3)
)
(f64.mul
(get_local $4)
(get_local $4)
)
)
(get_local $12)
)
)
(set_local $4
(f64.add
(f64.mul
(f64.mul
(f64.const 2)
(get_local $3)
)
(get_local $4)
)
(get_local $11)
)
)
(set_local $3
(get_local $13)
)
(br_if $break|2
(i32.ge_u
(tee_local $7
(i32.add
(get_local $7)
(i32.const 1)
)
)
(get_local $2)
)
)
(set_local $7
(i32.add
(get_local $7)
(i32.const 1)
)
)
(br $continue|2)
)
)
)
)
(i32.store8
(i32.add
(i32.mul
(get_local $5)
(get_local $0)
)
(get_local $6)
)
(i32.div_u
(i32.mul
(get_local $7)
(i32.const 255)
)
(get_local $2)
)
)
(set_local $6
(i32.add
(get_local $6)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
(set_local $5
(i32.add
(get_local $5)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)

View File

@ -0,0 +1 @@
export { update } from "../../examples/mandelbrot/assembly/index";

View File

@ -0,0 +1,213 @@
(module
(type $iiiv (func (param i32 i32 i32)))
(global $HEAP_BASE i32 (i32.const 4))
(memory $0 1)
(export "update" (func $../../examples/mandelbrot/assembly/index/update))
(export "memory" (memory $0))
(func $../../examples/mandelbrot/assembly/index/update (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 f64)
(local $4 f64)
(local $5 f64)
(local $6 i32)
(local $7 f64)
(local $8 i32)
(local $9 f64)
(local $10 i32)
(local $11 f64)
(local $12 f64)
(local $13 f64)
(set_local $3
(f64.div
(f64.convert_u/i32
(get_local $0)
)
(f64.const 1.6)
)
)
(set_local $4
(f64.div
(f64.convert_u/i32
(get_local $1)
)
(f64.const 2)
)
)
(set_local $5
(f64.div
(f64.const 4)
(f64.convert_u/i32
(get_local $0)
)
)
)
(block $break|0
(set_local $6
(i32.const 0)
)
(loop $continue|0
(if
(i32.lt_u
(get_local $6)
(get_local $1)
)
(block
(block
(set_local $7
(f64.mul
(f64.sub
(f64.convert_u/i32
(get_local $6)
)
(get_local $4)
)
(get_local $5)
)
)
(block $break|1
(set_local $8
(i32.const 0)
)
(loop $continue|1
(if
(i32.lt_u
(get_local $8)
(get_local $0)
)
(block
(block
(set_local $9
(f64.mul
(f64.sub
(f64.convert_u/i32
(get_local $8)
)
(get_local $3)
)
(get_local $5)
)
)
(set_local $10
(i32.const 0)
)
(block $break|2
(block
(set_local $11
(f64.const 0)
)
(set_local $12
(f64.const 0)
)
)
(loop $continue|2
(if
(f64.le
(f64.add
(f64.mul
(get_local $11)
(get_local $11)
)
(f64.mul
(get_local $12)
(get_local $12)
)
)
(f64.const 4)
)
(block
(block
(set_local $13
(f64.add
(f64.sub
(f64.mul
(get_local $11)
(get_local $11)
)
(f64.mul
(get_local $12)
(get_local $12)
)
)
(get_local $9)
)
)
(set_local $12
(f64.add
(f64.mul
(f64.mul
(f64.const 2)
(get_local $11)
)
(get_local $12)
)
(get_local $7)
)
)
(set_local $11
(get_local $13)
)
(if
(i32.ge_u
(tee_local $10
(i32.add
(get_local $10)
(i32.const 1)
)
)
(get_local $2)
)
(br $break|2)
)
)
(set_local $10
(i32.add
(get_local $10)
(i32.const 1)
)
)
(br $continue|2)
)
)
)
)
(i32.store8
(i32.add
(i32.mul
(get_local $6)
(get_local $0)
)
(get_local $8)
)
(i32.div_u
(i32.mul
(get_local $10)
(i32.const 255)
)
(get_local $2)
)
)
)
(set_local $8
(i32.add
(get_local $8)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
)
)
(set_local $6
(i32.add
(get_local $6)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)
)