Skip to content

Commit 6d8de50

Browse files
committed
Update binaryen; Replace uses of Math.* with portable built-ins
1 parent bbc71eb commit 6d8de50

File tree

7 files changed

+21
-24
lines changed

7 files changed

+21
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ Options:
8080
--noTreeShaking Disables tree-shaking.
8181
--noDebug Disables assertions.
8282
```
83+
84+
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.

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"dependencies": {
1414
"@types/node": "^8.0.54",
15-
"binaryen": "39.0.0-nightly.20171205",
15+
"binaryen": "40.0.0-nightly.20171209",
1616
"minimist": "^1.2.0",
1717
"source-map-support": "^0.5.0"
1818
},

portable-assembly.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ declare type bool = boolean;
2323

2424
// Portable built-ins
2525

26-
/** 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. */
26+
/** 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. */
2727
declare function clz<T = i32>(value: T): T;
2828
/** Computes the absolute value of an integer or float. */
2929
declare function abs<T = i32 | f32 | f64>(value: T): T;

src/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ export class Compiler extends DiagnosticEmitter {
17071707
case LiteralKind.FLOAT: {
17081708
const floatValue: f64 = (<FloatLiteralExpression>expression).value;
17091709
if (contextualType == Type.f32)
1710-
return this.module.createF32(Math.fround(floatValue));
1710+
return this.module.createF32(<f32>floatValue);
17111711
this.currentType = Type.f64;
17121712
return this.module.createF64(floatValue);
17131713
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Type {
3737
constructor(kind: TypeKind, size: i32) {
3838
this.kind = kind;
3939
this.size = size;
40-
this.byteSize = Math.ceil(<f64>size / 8);
40+
this.byteSize = ceil<f64>(<f64>size / 8);
4141
this.classType = null;
4242
}
4343

tests/compiler/game-of-life.html

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
fetch("game-of-life.optimized.wast").then(response => response.text()).then(text => {
66

77
// Convert it to binary format
8-
var buffer = Binaryen.parseText(text).emitBinary();
8+
var binary = Binaryen.parseText(text).emitBinary();
99

1010
// Instantiate the module
11-
return WebAssembly.instantiate(buffer, {});
12-
13-
}).then(result => {
14-
var module = result.instance;
11+
var module = new WebAssembly.Module(binary);
12+
var instance = new WebAssembly.Instance(module, { /* no imports */ });
1513

1614
// Set up the canvas with a 2D rendering context
1715
var cnv = document.getElementById("canvas");
@@ -22,39 +20,36 @@
2220
S = s + s; // total memory required to store input and output
2321

2422
// Grow the (exported) memory if it's size isn't sufficient
25-
var memory = module.exports.memory;
23+
var memory = instance.exports.memory;
2624
if (memory.buffer.byteLength < S)
2725
memory.grow(Math.ceil((S - memory.buffer.byteLength) / 65536));
2826

27+
// Initialize with width and height
28+
instance.exports.init(w, h);
29+
2930
// Fill input at [0, s-1] with random cells
3031
var mem = new Uint8Array(memory.buffer);
3132
for (var y = 0; y < h; ++y)
3233
for (var x = 0; x < w; ++x)
3334
mem[y * w + x] = Math.random() > 0.1 ? 0 : 1;
3435

35-
// Initialize with width and height
36-
module.exports.init(w, h);
37-
3836
// Update about 30 times a second
39-
function update() {
37+
(function update() {
4038
setTimeout(update, 33);
41-
module.exports.step();
39+
instance.exports.step();
4240
mem.set(mem.subarray(s, S), 0); // copy output -> input
43-
}
41+
})();
4442

4543
// Keep rendering the output at [s, 2*s-1]
46-
function render() {
44+
(function render() {
4745
requestAnimationFrame(render);
4846
ctx.clearRect(0, 0, w, h);
4947
ctx.fillStyle = "#333";
5048
for (var y = 0; y < h; ++y)
5149
for (var x = 0; x < w; ++x)
5250
if (mem[s + y * w + x])
5351
ctx.fillRect(x, y, 1, 1);
54-
}
55-
56-
update();
57-
render();
52+
})();
5853

5954
}).catch(err => {
6055
throw err;

0 commit comments

Comments
 (0)