Skip to content

Commit 4150f07

Browse files
authored
Update examples for 0.10.0 (#4)
1 parent 21e8fac commit 4150f07

File tree

10 files changed

+47
-31
lines changed

10 files changed

+47
-31
lines changed

.github/workflows/test.yml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ jobs:
1515
- uses: dcodeIO/setup-node-nvm@master
1616
with:
1717
node-version: current
18-
- run: |
18+
- name: Test
19+
run: |
1920
cd game-of-life
2021
npm install
2122
npm run asbuild
@@ -26,7 +27,8 @@ jobs:
2627
- uses: dcodeIO/setup-node-nvm@master
2728
with:
2829
node-version: current
29-
- run: |
30+
- name: Test
31+
run: |
3032
cd i64
3133
npm install
3234
npm run asbuild
@@ -38,7 +40,8 @@ jobs:
3840
- uses: dcodeIO/setup-node-nvm@master
3941
with:
4042
node-version: current
41-
- run: |
43+
- name: Test
44+
run: |
4245
cd interference
4346
npm install
4447
npm run asbuild
@@ -49,7 +52,8 @@ jobs:
4952
- uses: dcodeIO/setup-node-nvm@master
5053
with:
5154
node-version: current
52-
- run: |
55+
- name: Test
56+
run: |
5357
cd libm
5458
npm install
5559
npm run asbuild
@@ -61,7 +65,8 @@ jobs:
6165
- uses: dcodeIO/setup-node-nvm@master
6266
with:
6367
node-version: current
64-
- run: |
68+
- name: Test
69+
run: |
6570
cd loader
6671
npm install
6772
npm run asbuild
@@ -73,7 +78,8 @@ jobs:
7378
- uses: dcodeIO/setup-node-nvm@master
7479
with:
7580
node-version: current
76-
- run: |
81+
- name: Test
82+
run: |
7783
cd mandelbrot
7884
npm install
7985
npm run asbuild
@@ -84,7 +90,8 @@ jobs:
8490
- uses: dcodeIO/setup-node-nvm@master
8591
with:
8692
node-version: current
87-
- run: |
93+
- name: Test
94+
run: |
8895
cd n-body
8996
npm install
9097
npm run asbuild
@@ -96,7 +103,8 @@ jobs:
96103
- uses: dcodeIO/setup-node-nvm@master
97104
with:
98105
node-version: current
99-
- run: |
106+
- name: Test
107+
run: |
100108
cd transform
101109
npm install
102110
npm test

interference/build/optimized.wasm

-29 Bytes
Binary file not shown.

loader/build/optimized.wasm

-186 Bytes
Binary file not shown.

loader/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require("fs");
22
const loader = require("@assemblyscript/loader");
3-
const myModule = module.exports = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"),
3+
const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"),
44

55
// These are the JavaScript imports to our WebAssembly module, translating
66
// from WebAssembly strings, received as a pointer into the module's memory,
@@ -9,14 +9,15 @@ const myModule = module.exports = loader.instantiateSync(fs.readFileSync(__dirna
99
// Example 3: Calling JavaScript imports with WebAssembly strings.
1010
myConsole: {
1111
log(messagePtr) { // Called as `console.log` in assembly/index.ts
12-
console.log(myModule.__getString(messagePtr));
12+
console.log(wasmModule.exports.__getString(messagePtr));
1313
},
1414
time(labelPtr) { // Called as `console.time` in assembly/index.ts
15-
console.time(myModule.__getString(labelPtr));
15+
console.time(wasmModule.exports.__getString(labelPtr));
1616
},
1717
timeEnd(labelPtr) { // Called as `console.timeEnd` in assembly/index.ts
18-
console.timeEnd(myModule.__getString(labelPtr));
18+
console.timeEnd(wasmModule.exports.__getString(labelPtr));
1919
}
2020
}
2121
}
2222
);
23+
module.exports = wasmModule.exports;

n-body/build/as_nbody.wasm

0 Bytes
Binary file not shown.

n-body/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"asbuild:optimized": "asc assembly/index.ts -b build/as_nbody.wasm -t build/as_nbody.wat -O3 --runtime none --noAssert --importMemory",
8-
"asbuild:asmjs": "asc assembly/index.ts -a build/as_nbody.asm.js -O3 --runtime none --noAssert && node scripts/postprocess-asmjs",
9-
"asbuild": "npm run asbuild:optimized && npm run asbuild:asmjs",
7+
"asbuild:wasm": "asc assembly/index.ts -b build/as_nbody.wasm -t build/as_nbody.wat -O3 --runtime none --noAssert --importMemory",
8+
"asbuild:js": "asc assembly/index.ts -j build/as_nbody.js -O3 --runtime none --noAssert && node scripts/postprocess-js",
9+
"asbuild": "npm run asbuild:wasm && npm run asbuild:js",
1010
"tsbuild": "tsc -p assembly -t ES2017 -m commonjs --outDir build",
1111
"rsbuild": "cd rust && RUSTFLAGS='-C link-arg=-s' cargo +nightly build --release",
1212
"build": "npm run asbuild && npm run tsbuild && npm run rsbuild",
1313
"start": "http-server . -o -c-1",
14-
"test": "node --wasm-no-bounds-checks --wasm-no-stack-checks --expose-gc tests"
14+
"test": "node --no-wasm-bounds-checks --no-wasm-stack-checks --expose-gc tests"
1515
},
1616
"devDependencies": {
1717
"assemblyscript": "latest",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require("fs");
22
const path = require("path");
33

4-
const filename = path.join(__dirname, "..", "build" , "as_nbody.asm.js");
4+
const filename = path.join(__dirname, "..", "build" , "as_nbody.js");
55
var source = fs.readFileSync(filename, { encoding: "utf8" });
66
source = source.replace(/^export var ([^ ]+) =/mg, ($0, $1) => "exports." + $1 + " = ");
77
fs.writeFileSync(filename, source);

n-body/tests/index.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ const fs = require("fs");
22

33
// Load WASM version
44
const nbodyAS = require("../assembly/index.js");
5-
const nbodyRS = require("../rust/index.js");
5+
var nbodyRS;
6+
try {
7+
nbodyRS = require("../rust/index.js");
8+
} catch (e) {}
69

7-
// Load ASMJS version
8-
var src = fs.readFileSync(__dirname + "/../build/as_nbody.asm.js", "utf8")
10+
// Load JS version
11+
var src = fs.readFileSync(__dirname + "/../build/as_nbody.js", "utf8")
912
.replace(/const retasmFunc[^$]*$/g, "");
1013

11-
const nbodyAsmJS = eval(src + ";asmFunc")({
14+
const nbodyAS_JS = eval(src + ";asmFunc")({
1215
Int8Array,
1316
Int16Array,
1417
Int32Array,
@@ -70,26 +73,30 @@ console.log("\nCOLD SERIES:\n");
7073
prologue("AssemblyScript WASM", steps);
7174
epilogue(test(nbodyAS, steps));
7275

73-
prologue("AssemblyScript ASMJS", steps);
74-
epilogue(test(nbodyAsmJS, steps));
76+
prologue("AssemblyScript JS", steps);
77+
epilogue(test(nbodyAS_JS, steps));
7578

7679
prologue("JS", steps);
7780
epilogue(test(nbodyJS, steps));
7881

79-
prologue("Rust WASM", steps);
80-
epilogue(test(nbodyRS, steps));
82+
if (nbodyRS) {
83+
prologue("Rust WASM", steps);
84+
epilogue(test(nbodyRS, steps));
85+
}
8186

8287
console.log("\nWARMED UP SERIES:\n");
8388
sleep(1000);
8489

8590
prologue("AssemblyScript WASM", steps);
8691
epilogue(test(nbodyAS, steps));
8792

88-
prologue("AssemblyScript ASMJS", steps);
89-
epilogue(test(nbodyAsmJS, steps));
93+
prologue("AssemblyScript JS", steps);
94+
epilogue(test(nbodyAS_JS, steps));
9095

9196
prologue("JS", steps);
9297
epilogue(test(nbodyJS, steps));
9398

94-
prologue("Rust WASM", steps);
95-
epilogue(test(nbodyRS, steps));
99+
if (nbodyRS) {
100+
prologue("Rust WASM", steps);
101+
epilogue(test(nbodyRS, steps));
102+
}

transform/mytransform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const binaryen = require("binaryen");
55
class MyTransform extends Transform {
66
afterParse(parser) {
77
this.log("[mytransform.js] afterParse called, baseDir = " + this.baseDir);
8-
var sources = parser.program.sources;
8+
var sources = this.program.sources;
99
sources.forEach(source => this.log(" " + source.internalPath + " [" + SourceKind[source.sourceKind] + "]"));
1010
}
1111
afterInitialize(program) {

transform/mytransform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as binaryen from "binaryen";
55
class MyTransform extends Transform {
66
afterParse(parser: Parser): void {
77
this.log("[mytransform.ts] afterParse called, baseDir = " + this.baseDir);
8-
var sources = parser.program.sources;
8+
var sources = this.program.sources;
99
sources.forEach(source => this.log(" " + source.internalPath + " [" + SourceKind[source.sourceKind] + "]"));
1010
}
1111
afterInitialize(program: Program) {

0 commit comments

Comments
 (0)