Skip to content

Commit 176f720

Browse files
authored
Update to AS 0.18 (#11)
1 parent 3133266 commit 176f720

21 files changed

+75
-82
lines changed

game-of-life/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Afterwards, run
2424
$> npm start
2525
```
2626

27-
to start a <a href="http://localhost:9080">local server</a>. Should also automatically launch a browser.
27+
to start a local server.

game-of-life/build/optimized.wasm

0 Bytes
Binary file not shown.

game-of-life/package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime none --importMemory --sourceMap --debug --measure",
8-
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts --use Math=JSMath -O3 --runtime none --importMemory --sourceMap --measure",
7+
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime stub --importMemory --sourceMap --debug --measure",
8+
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts --use Math=JSMath -O3 --runtime stub --importMemory --sourceMap --measure",
99
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
10-
"start": "http-server . -o -c-1"
10+
"start": "npx serve"
1111
},
1212
"devDependencies": {
13-
"assemblyscript": "latest",
14-
"http-server": "^0.12.3"
13+
"assemblyscript": "latest"
1514
}
1615
}

i64/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"types": "index.d.ts",
77
"scripts": {
88
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
9-
"asbuild:untouched": "asc assembly/i64.ts -t build/untouched.wat -b build/untouched.wasm --runtime none --sourceMap --debug --measure",
10-
"asbuild:optimized": "asc assembly/i64.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts -O3 --runtime none --sourceMap --measure",
9+
"asbuild:untouched": "asc assembly/i64.ts -t build/untouched.wat -b build/untouched.wasm --runtime stub --sourceMap --debug --measure",
10+
"asbuild:optimized": "asc assembly/i64.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts -O3 --runtime stub --sourceMap --measure",
1111
"test": "node tests"
1212
},
1313
"files": [

interference/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Afterwards, run
2424
$> npm start
2525
```
2626

27-
to start a <a href="http://127.0.0.1:8080">local server</a>. Should also automatically launch a browser.
27+
to start a local server.

interference/build/optimized.wasm

-15 Bytes
Binary file not shown.

interference/package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --runtime none --debug",
8-
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --runtime none --optimize",
7+
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --runtime stub --debug",
8+
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --runtime stub --optimize",
99
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
10-
"start": "http-server . -o -c-1"
10+
"start": "npx serve"
1111
},
1212
"devDependencies": {
13-
"assemblyscript": "latest",
14-
"http-server": "^0.12.3"
13+
"assemblyscript": "latest"
1514
}
1615
}

libm/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"types": "index.d.ts",
88
"scripts": {
99
"asbuild": "npm run asbuild:libm && npm run asbuild:libmf",
10-
"asbuild:libm": "asc assembly/libm.ts -O3 -b build/libm.wasm -t build/libm.wat --runtime none",
11-
"asbuild:libmf": "asc assembly/libmf.ts -O3 -b build/libmf.wasm -t build/libmf.wat --runtime none",
10+
"asbuild:libm": "asc assembly/libm.ts -O3 -b build/libm.wasm -t build/libm.wat --runtime stub",
11+
"asbuild:libmf": "asc assembly/libmf.ts -O3 -b build/libmf.wasm -t build/libmf.wat --runtime stub",
1212
"test": "node tests"
1313
},
1414
"files": [

loader/build/optimized.wasm

2.36 KB
Binary file not shown.

loader/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --debug",
8-
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --optimize",
7+
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --exportRuntime --sourceMap --debug",
8+
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --exportRuntime --sourceMap --optimize",
99
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
1010
"test": "node tests"
1111
},

loader/tests/index.js

+40-41
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const myModule = require("../index");
44
// Obtain the runtime helpers for
55
const {
66
// memory management
7-
__allocString, __allocArray,
7+
__newString, __newArray,
88
// garbage collection
9-
__retain, __release,
9+
__pin, __unpin,
1010
// and interop
1111
__getString, __getArray, __getArrayView
1212
} = myModule;
@@ -15,40 +15,34 @@ const {
1515
{
1616
console.log("Example 1:");
1717

18-
// Obtain a pointer to our string in the module's memory. Note that `return`ing
19-
// a string, or any other object, from WebAssembly to JavaScript automatically
20-
// retains a reference for us, the caller, to release when we are done with it.
21-
const ptr = myModule.getHello();
18+
// Obtain a pointer to our string in the module's memory.
19+
const ptr = __pin(myModule.getHello());
2220

2321
// Print its contents
2422
console.log(" " + __getString(ptr));
2523

26-
__release(ptr); // we are done with the returned string but
27-
// it might still be alive in WebAssembly
24+
__unpin(ptr); // it is ok if the string becomes garbage collected now
2825
}
2926

3027
// Test for Example 2: Passing a string from JavaScript to WebAssembly.
3128
{
3229
console.log("Example 2:");
3330

34-
// Allocate a string in the module's memory and retain a reference to our allocation
35-
const ptr = __retain(__allocString("Hello world (I am a JavaScript string)"));
31+
// Allocate a string in the module's memory and pin it externally
32+
const ptr = __pin(__newString("Hello world (I am a JavaScript string)"));
3633

3734
// Pass it to our WebAssembly export, which is going to print it using our custom console
3835
myModule.sayHello(ptr);
3936

40-
__release(ptr); // we are done with the allocated string but
41-
// it might still be alive in WebAssembly
37+
__unpin(ptr); // it is ok if the string becomes garbage collected now
4238
}
4339

4440
// Test for Example 4: Passing an array from WebAssembly to JavaScript.
4541
{
4642
console.log("Example 4:");
4743

48-
// Obtain a pointer to our array in the module's memory. Note that `return`ing
49-
// an object from WebAssembly to JavaScript automatically retains a reference
50-
// for us, the caller, to release when we are done with it.
51-
const ptr = myModule.getMyArray(10);
44+
// Obtain a pointer to our array in the module's memory.
45+
const ptr = __pin(myModule.getMyArray(10));
5246

5347
// Obtain a live view on it
5448
const view = __getArrayView(ptr);
@@ -58,7 +52,7 @@ const {
5852
const copy = __getArray(ptr);
5953
console.log(" " + copy + " (copy)");
6054

61-
__release(ptr); // we are done with the array
55+
__unpin(ptr); // it is ok if the array becomes garbage collected now
6256
}
6357

6458
// Test for Example 5: Passing an array from JavaScript to WebAssembly.
@@ -68,7 +62,7 @@ const {
6862
// Allocate a new array in WebAssembly memory and get a view on it. Note that
6963
// we have to specify the runtime id of the array type we want to allocate, so
7064
// we export its id (`idof<Int32Array>`) from the module to do so.
71-
const ptr = __retain(__allocArray(myModule.Int32Array_ID, [ 1, 2, 3 ]));
65+
const ptr = __pin(__newArray(myModule.Int32Array_ID, [ 1, 2, 3 ]));
7266
const view = __getArrayView(ptr);
7367
const copy = __getArray(ptr);
7468

@@ -82,27 +76,27 @@ const {
8276
// The initial copy remains unchanged and is not linked to `ptr`
8377
console.log(" Unmodified copy: " + copy);
8478

85-
__release(ptr); // we are done with our allocated array but
86-
// it might still be alive in WebAssembly
79+
__unpin(ptr); // it is ok if the array becomes garbage collected now
8780
}
8881

8982
// Test for Example 6: WebAssembly arrays of WebAssembly strings.
9083
{
9184
console.log("Example 6:");
9285

9386
// Allocate a new array, but this time its elements are pointers to strings.
94-
// Note: Allocating an array of strings or other objects will automatically
95-
// take care of retaining references to its elements, but the array itself
96-
// must be dealt with as usual.
97-
const inPtr = __retain(__allocArray(myModule.ArrayOfStrings_ID, [ "hello", "world" ].map(__allocString)));
87+
const elemPtrs = [ "hello", "world" ].map(v => __pin(__newString(v)));
88+
const inPtr = __pin(__newArray(myModule.ArrayOfStrings_ID, elemPtrs));
89+
90+
// The array keeps its values alive from now on
91+
elemPtrs.forEach(__unpin);
9892

9993
// Provide our array of lowercase strings to WebAssembly, and obtain the new
10094
// array of uppercase strings before printing it.
101-
const outPtr = myModule.capitalize(inPtr);
95+
const outPtr = __pin(myModule.capitalize(inPtr));
10296
console.log(" Uppercased: " + __getArray(outPtr).map(__getString));
10397

104-
__release(inPtr); // release our allocation and release
105-
__release(outPtr); // the return value. you know the drill!
98+
__unpin(inPtr); // it is ok if the arrays becomes garbage collected now
99+
__unpin(outPtr);
106100

107101
// Note that Example 6 is not an especially efficient use case and one would
108102
// typically rather avoid the overhead and do this in JavaScript directly.
@@ -114,57 +108,62 @@ const {
114108

115109
// Create a new player. Note that the loader makes a nice object structure
116110
// of our exports, here a class `Player` within the `Game` namespace. So
117-
// let's call the `Player` constructor (this is also an allocation):
111+
// let's call the `Player` constructor:
118112
let player;
119113
{
120-
const namePtr = __retain(__allocString("Gordon Freeman"));
114+
const namePtr = __pin(__newString("Gordon Freeman"));
121115
player = new myModule.Game.Player(namePtr);
122-
__release(namePtr);
116+
__pin(player);
117+
__unpin(namePtr);
118+
// Pro tip: Pinning is optional in this exact case
123119
}
124120

125121
// Let's see how our player looks now by calling toString
126122
{
127-
const strPtr = player.toString();
123+
const strPtr = __pin(player.toString());
128124
console.log(" Player (new): " + __getString(strPtr));
129-
__release(strPtr);
125+
__unpin(strPtr);
126+
// Pro tip: Pinning is optional in this exact case
130127
}
131128

132129
// Move them and log again
133130
{
134131
player.move(10, 20);
135-
const strPtr = player.toString();
132+
const strPtr = __pin(player.toString());
136133
console.log(" Player (moved): " + __getString(strPtr));
137-
__release(strPtr);
134+
__unpin(strPtr);
135+
// Pro tip: Pinning is optional in this exact case
138136
}
139137

140138
// Obtaining just the position. Note that we can `wrap` any pointer with
141139
// the matching class within the object structure made by the loader, and
142140
// that a position's x and y properties are just basic values, not objects,
143141
// so tracking references does not apply to them.
144142
{
145-
const positionPtr = player.position; // implicit getter, retained for us
143+
const positionPtr = __pin(player.position);
146144
const position = myModule.Game.Position.wrap(positionPtr);
147145
console.log(" Position (wrapped): " + position.x + "/" + position.y);
148146

149147
position.x -= 100;
150148
position.y += 200;
151149

152-
const strPtr = position.toString();
150+
const strPtr = __pin(position.toString());
153151
console.log(" Position (moved): " + __getString(strPtr));
154-
__release(strPtr);
152+
__unpin(strPtr);
155153

156-
__release(positionPtr);
154+
__unpin(positionPtr);
157155
}
158156

159157
// Finish 'em
160158
{
161159
player.kill();
162-
const strPtr = player.toString();
160+
const strPtr = __pin(player.toString());
163161
console.log(" Player (finished): " + __getString(strPtr));
164-
__release(strPtr); // we are done with the returned object
162+
__unpin(strPtr);
163+
// Pro tip: Pinning is optional in this exact case
165164
}
166165

167-
__release(player); // a tidy house, a tidy mind.
166+
__unpin(player); // a tidy house, a tidy mind.
168167
}
169168

170169
// Interested in all the details? https://docs.assemblyscript.org/details :)

mandelbrot/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Afterwards, run
2424
$> npm start
2525
```
2626

27-
to start a <a href="http://127.0.0.1:8080">local server</a>. Should also automatically launch a browser.
27+
to start a local server.

mandelbrot/build/optimized.wasm

-1 Bytes
Binary file not shown.

mandelbrot/package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime none --importMemory --sourceMap --debug --measure",
8-
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts --use Math=JSMath --runtime none -O3 --importMemory --sourceMap --measure",
7+
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime stub --importMemory --sourceMap --debug --measure",
8+
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts --use Math=JSMath --runtime stub -O3 --importMemory --sourceMap --measure",
99
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
10-
"start": "http-server . -o -c-1"
10+
"start": "npx serve"
1111
},
1212
"devDependencies": {
13-
"assemblyscript": "latest",
14-
"http-server": "^0.12.3"
13+
"assemblyscript": "latest"
1514
}
1615
}

n-body/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Afterwards, run
2424
$> npm start
2525
```
2626

27-
to start a <a href="http://localhost:9080">local server</a>. Should also automatically launch a browser.
27+
to start a local server.
2828

2929
To run the benchmark:
3030

n-body/build/as_nbody.wasm

190 Bytes
Binary file not shown.

n-body/package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
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",
7+
"asbuild:wasm": "asc assembly/index.ts -b build/as_nbody.wasm -t build/as_nbody.wat -O3 --runtime stub --noAssert --importMemory",
8+
"asbuild:js": "asc assembly/index.ts -j build/as_nbody.js -O3 --runtime stub --noAssert && node scripts/postprocess-js",
99
"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",
13-
"start": "http-server . -o -c-1",
13+
"start": "npx serve",
1414
"test": "node --no-wasm-bounds-checks --no-wasm-stack-checks --expose-gc tests"
1515
},
1616
"devDependencies": {
1717
"assemblyscript": "latest",
18-
"http-server": "^0.12.3",
19-
"typescript": "^3.8.3"
18+
"typescript": "^3.9.7"
2019
}
2120
}

n-body/scripts/postprocess-js.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ const path = require("path");
44
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 + " = ");
7+
source = source.replace("import { abort } from 'env';", "function abort() { throw new Error('abort'); }");
78
fs.writeFileSync(filename, source);

sdk/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
function simpleExample(asc) {
2424
const { text, binary } = asc.compileString(SOURCE_CODE, {
2525
optimizeLevel: 3,
26-
runtime: "none"
26+
runtime: "stub"
2727
});
2828
console.log(`>>> TEXT >>>\n${text}`);
2929
console.log(`>>> BINARY >>>\n${binary.length} bytes`);
@@ -37,7 +37,7 @@
3737
asc.main([
3838
"module.ts",
3939
"-O3",
40-
"--runtime", "none",
40+
"--runtime", "stub",
4141
"--binaryFile", "module.wasm",
4242
"--textFile", "module.wat",
4343
"--sourceMap"

sdk/package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"start": "http-server . -o -c-1"
8-
},
9-
"devDependencies": {
10-
"http-server": "^0.12.3"
7+
"start": "npx serve"
118
}
129
}

transform/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"test:js": "asc assembly/index.ts --runtime none --transform ./mytransform.js",
8-
"test:ts": "asc assembly/index.ts --runtime none --transform ./mytransform.ts",
9-
"test:multi": "asc assembly/index.ts --runtime none --transform ./mytransform.js --transform ./mytransform.ts",
7+
"test:js": "asc assembly/index.ts --runtime stub --transform ./mytransform.js",
8+
"test:ts": "asc assembly/index.ts --runtime stub --transform ./mytransform.ts",
9+
"test:multi": "asc assembly/index.ts --runtime stub --transform ./mytransform.js --transform ./mytransform.ts",
1010
"test": "npm run test:js && npm run test:ts && npm run test:multi"
1111
},
1212
"devDependencies": {
1313
"assemblyscript": "latest",
14-
"ts-node": "^8.10.1",
15-
"typescript": "^3.8.3"
14+
"ts-node": "^8.10.2",
15+
"typescript": "^3.9.7"
1616
}
1717
}

0 commit comments

Comments
 (0)