Update binaryen; Replace uses of Math.* with portable built-ins

This commit is contained in:
dcodeIO 2017-12-09 01:35:18 +01:00
parent bbc71ebe81
commit 6d8de50565
7 changed files with 21 additions and 24 deletions

View File

@ -80,3 +80,5 @@ Options:
--noTreeShaking Disables tree-shaking.
--noDebug Disables assertions.
```
Unless a bundle has been built to `dist/`, `asc` runs the TypeScript sources directly via [ts-node](https://www.npmjs.com/package/ts-node). Useful for development.

6
package-lock.json generated
View File

@ -216,9 +216,9 @@
"dev": true
},
"binaryen": {
"version": "39.0.0-nightly.20171205",
"resolved": "https://registry.npmjs.org/binaryen/-/binaryen-39.0.0-nightly.20171205.tgz",
"integrity": "sha512-OYyWKbtGCSjfX1mTmwMfTANC+trKmEIOOfAMbqjJzU+qmEFCt942kIn+6+3sNeuehLLuPeXrEOq1JxKnKVdizg=="
"version": "40.0.0-nightly.20171209",
"resolved": "https://registry.npmjs.org/binaryen/-/binaryen-40.0.0-nightly.20171209.tgz",
"integrity": "sha512-ELe79ZuH6dPLo+j3tbTvGl9BagzUTtIuYlY3LWz1kj5JzgpqeZmUweay1vK6MZo7J/1IA7Sgda4J7vxUhNu03Q=="
},
"bn.js": {
"version": "4.11.8",

View File

@ -12,7 +12,7 @@
},
"dependencies": {
"@types/node": "^8.0.54",
"binaryen": "39.0.0-nightly.20171205",
"binaryen": "40.0.0-nightly.20171209",
"minimist": "^1.2.0",
"source-map-support": "^0.5.0"
},

View File

@ -23,7 +23,7 @@ declare type bool = boolean;
// Portable built-ins
/** Performs the sign-agnostic count leading zero bits operation on a 32-bit or 64-bit integer. All zero bits are considered leading if the value is zero. */
/** Performs the sign-agnostic count leading zero bits operation on a 32-bit integer. All zero bits are considered leading if the value is zero. */
declare function clz<T = i32>(value: T): T;
/** Computes the absolute value of an integer or float. */
declare function abs<T = i32 | f32 | f64>(value: T): T;

View File

@ -1707,7 +1707,7 @@ export class Compiler extends DiagnosticEmitter {
case LiteralKind.FLOAT: {
const floatValue: f64 = (<FloatLiteralExpression>expression).value;
if (contextualType == Type.f32)
return this.module.createF32(Math.fround(floatValue));
return this.module.createF32(<f32>floatValue);
this.currentType = Type.f64;
return this.module.createF64(floatValue);
}

View File

@ -37,7 +37,7 @@ export class Type {
constructor(kind: TypeKind, size: i32) {
this.kind = kind;
this.size = size;
this.byteSize = Math.ceil(<f64>size / 8);
this.byteSize = ceil<f64>(<f64>size / 8);
this.classType = null;
}

View File

@ -5,13 +5,11 @@
fetch("game-of-life.optimized.wast").then(response => response.text()).then(text => {
// Convert it to binary format
var buffer = Binaryen.parseText(text).emitBinary();
var binary = Binaryen.parseText(text).emitBinary();
// Instantiate the module
return WebAssembly.instantiate(buffer, {});
}).then(result => {
var module = result.instance;
var module = new WebAssembly.Module(binary);
var instance = new WebAssembly.Instance(module, { /* no imports */ });
// Set up the canvas with a 2D rendering context
var cnv = document.getElementById("canvas");
@ -22,28 +20,28 @@ fetch("game-of-life.optimized.wast").then(response => response.text()).then(text
S = s + s; // total memory required to store input and output
// Grow the (exported) memory if it's size isn't sufficient
var memory = module.exports.memory;
var memory = instance.exports.memory;
if (memory.buffer.byteLength < S)
memory.grow(Math.ceil((S - memory.buffer.byteLength) / 65536));
// Initialize with width and height
instance.exports.init(w, h);
// Fill input at [0, s-1] with random 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;
// Initialize with width and height
module.exports.init(w, h);
// Update about 30 times a second
function update() {
(function update() {
setTimeout(update, 33);
module.exports.step();
instance.exports.step();
mem.set(mem.subarray(s, S), 0); // copy output -> input
}
})();
// Keep rendering the output at [s, 2*s-1]
function render() {
(function render() {
requestAnimationFrame(render);
ctx.clearRect(0, 0, w, h);
ctx.fillStyle = "#333";
@ -51,10 +49,7 @@ fetch("game-of-life.optimized.wast").then(response => response.text()).then(text
for (var x = 0; x < w; ++x)
if (mem[s + y * w + x])
ctx.fillRect(x, y, 1, 1);
}
update();
render();
})();
}).catch(err => {
throw err;