Skip to content

Update to AS 0.18 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion game-of-life/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Afterwards, run
$> npm start
```

to start a <a href="http://localhost:9080">local server</a>. Should also automatically launch a browser.
to start a local server.
Binary file modified game-of-life/build/optimized.wasm
Binary file not shown.
9 changes: 4 additions & 5 deletions game-of-life/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"license": "Apache-2.0",
"private": true,
"scripts": {
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime none --importMemory --sourceMap --debug --measure",
"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",
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime stub --importMemory --sourceMap --debug --measure",
"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",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"start": "http-server . -o -c-1"
"start": "npx serve"
},
"devDependencies": {
"assemblyscript": "latest",
"http-server": "^0.12.3"
"assemblyscript": "latest"
}
}
4 changes: 2 additions & 2 deletions i64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"types": "index.d.ts",
"scripts": {
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"asbuild:untouched": "asc assembly/i64.ts -t build/untouched.wat -b build/untouched.wasm --runtime none --sourceMap --debug --measure",
"asbuild:optimized": "asc assembly/i64.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts -O3 --runtime none --sourceMap --measure",
"asbuild:untouched": "asc assembly/i64.ts -t build/untouched.wat -b build/untouched.wasm --runtime stub --sourceMap --debug --measure",
"asbuild:optimized": "asc assembly/i64.ts -b build/optimized.wasm -t build/optimized.wat -d build/optimized.d.ts -O3 --runtime stub --sourceMap --measure",
"test": "node tests"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion interference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Afterwards, run
$> npm start
```

to start a <a href="http://127.0.0.1:8080">local server</a>. Should also automatically launch a browser.
to start a local server.
Binary file modified interference/build/optimized.wasm
Binary file not shown.
9 changes: 4 additions & 5 deletions interference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"license": "Apache-2.0",
"private": true,
"scripts": {
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --runtime none --debug",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --runtime none --optimize",
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --runtime stub --debug",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --runtime stub --optimize",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"start": "http-server . -o -c-1"
"start": "npx serve"
},
"devDependencies": {
"assemblyscript": "latest",
"http-server": "^0.12.3"
"assemblyscript": "latest"
}
}
4 changes: 2 additions & 2 deletions libm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"types": "index.d.ts",
"scripts": {
"asbuild": "npm run asbuild:libm && npm run asbuild:libmf",
"asbuild:libm": "asc assembly/libm.ts -O3 -b build/libm.wasm -t build/libm.wat --runtime none",
"asbuild:libmf": "asc assembly/libmf.ts -O3 -b build/libmf.wasm -t build/libmf.wat --runtime none",
"asbuild:libm": "asc assembly/libm.ts -O3 -b build/libm.wasm -t build/libm.wat --runtime stub",
"asbuild:libmf": "asc assembly/libmf.ts -O3 -b build/libmf.wasm -t build/libmf.wat --runtime stub",
"test": "node tests"
},
"files": [
Expand Down
Binary file modified loader/build/optimized.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"license": "Apache-2.0",
"private": true,
"scripts": {
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --debug",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --optimize",
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --exportRuntime --sourceMap --debug",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --exportRuntime --sourceMap --optimize",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"test": "node tests"
},
Expand Down
81 changes: 40 additions & 41 deletions loader/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const myModule = require("../index");
// Obtain the runtime helpers for
const {
// memory management
__allocString, __allocArray,
__newString, __newArray,
// garbage collection
__retain, __release,
__pin, __unpin,
// and interop
__getString, __getArray, __getArrayView
} = myModule;
Expand All @@ -15,40 +15,34 @@ const {
{
console.log("Example 1:");

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

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

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

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

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

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

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

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

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

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

__release(ptr); // we are done with the array
__unpin(ptr); // it is ok if the array becomes garbage collected now
}

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

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

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

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

// Allocate a new array, but this time its elements are pointers to strings.
// Note: Allocating an array of strings or other objects will automatically
// take care of retaining references to its elements, but the array itself
// must be dealt with as usual.
const inPtr = __retain(__allocArray(myModule.ArrayOfStrings_ID, [ "hello", "world" ].map(__allocString)));
const elemPtrs = [ "hello", "world" ].map(v => __pin(__newString(v)));
const inPtr = __pin(__newArray(myModule.ArrayOfStrings_ID, elemPtrs));

// The array keeps its values alive from now on
elemPtrs.forEach(__unpin);

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

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

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

// Create a new player. Note that the loader makes a nice object structure
// of our exports, here a class `Player` within the `Game` namespace. So
// let's call the `Player` constructor (this is also an allocation):
// let's call the `Player` constructor:
let player;
{
const namePtr = __retain(__allocString("Gordon Freeman"));
const namePtr = __pin(__newString("Gordon Freeman"));
player = new myModule.Game.Player(namePtr);
__release(namePtr);
__pin(player);
__unpin(namePtr);
// Pro tip: Pinning is optional in this exact case
}

// Let's see how our player looks now by calling toString
{
const strPtr = player.toString();
const strPtr = __pin(player.toString());
console.log(" Player (new): " + __getString(strPtr));
__release(strPtr);
__unpin(strPtr);
// Pro tip: Pinning is optional in this exact case
}

// Move them and log again
{
player.move(10, 20);
const strPtr = player.toString();
const strPtr = __pin(player.toString());
console.log(" Player (moved): " + __getString(strPtr));
__release(strPtr);
__unpin(strPtr);
// Pro tip: Pinning is optional in this exact case
}

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

position.x -= 100;
position.y += 200;

const strPtr = position.toString();
const strPtr = __pin(position.toString());
console.log(" Position (moved): " + __getString(strPtr));
__release(strPtr);
__unpin(strPtr);

__release(positionPtr);
__unpin(positionPtr);
}

// Finish 'em
{
player.kill();
const strPtr = player.toString();
const strPtr = __pin(player.toString());
console.log(" Player (finished): " + __getString(strPtr));
__release(strPtr); // we are done with the returned object
__unpin(strPtr);
// Pro tip: Pinning is optional in this exact case
}

__release(player); // a tidy house, a tidy mind.
__unpin(player); // a tidy house, a tidy mind.
}

// Interested in all the details? https://docs.assemblyscript.org/details :)
2 changes: 1 addition & 1 deletion mandelbrot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Afterwards, run
$> npm start
```

to start a <a href="http://127.0.0.1:8080">local server</a>. Should also automatically launch a browser.
to start a local server.
Binary file modified mandelbrot/build/optimized.wasm
Binary file not shown.
9 changes: 4 additions & 5 deletions mandelbrot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"license": "Apache-2.0",
"private": true,
"scripts": {
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime none --importMemory --sourceMap --debug --measure",
"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",
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime stub --importMemory --sourceMap --debug --measure",
"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",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"start": "http-server . -o -c-1"
"start": "npx serve"
},
"devDependencies": {
"assemblyscript": "latest",
"http-server": "^0.12.3"
"assemblyscript": "latest"
}
}
2 changes: 1 addition & 1 deletion n-body/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Afterwards, run
$> npm start
```

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

To run the benchmark:

Expand Down
Binary file modified n-body/build/as_nbody.wasm
Binary file not shown.
9 changes: 4 additions & 5 deletions n-body/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
"license": "Apache-2.0",
"private": true,
"scripts": {
"asbuild:wasm": "asc assembly/index.ts -b build/as_nbody.wasm -t build/as_nbody.wat -O3 --runtime none --noAssert --importMemory",
"asbuild:js": "asc assembly/index.ts -j build/as_nbody.js -O3 --runtime none --noAssert && node scripts/postprocess-js",
"asbuild:wasm": "asc assembly/index.ts -b build/as_nbody.wasm -t build/as_nbody.wat -O3 --runtime stub --noAssert --importMemory",
"asbuild:js": "asc assembly/index.ts -j build/as_nbody.js -O3 --runtime stub --noAssert && node scripts/postprocess-js",
"asbuild": "npm run asbuild:wasm && npm run asbuild:js",
"tsbuild": "tsc -p assembly -t ES2017 -m commonjs --outDir build",
"rsbuild": "cd rust && RUSTFLAGS='-C link-arg=-s' cargo +nightly build --release",
"build": "npm run asbuild && npm run tsbuild && npm run rsbuild",
"start": "http-server . -o -c-1",
"start": "npx serve",
"test": "node --no-wasm-bounds-checks --no-wasm-stack-checks --expose-gc tests"
},
"devDependencies": {
"assemblyscript": "latest",
"http-server": "^0.12.3",
"typescript": "^3.8.3"
"typescript": "^3.9.7"
}
}
1 change: 1 addition & 0 deletions n-body/scripts/postprocess-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ const path = require("path");
const filename = path.join(__dirname, "..", "build" , "as_nbody.js");
var source = fs.readFileSync(filename, { encoding: "utf8" });
source = source.replace(/^export var ([^ ]+) =/mg, ($0, $1) => "exports." + $1 + " = ");
source = source.replace("import { abort } from 'env';", "function abort() { throw new Error('abort'); }");
fs.writeFileSync(filename, source);
4 changes: 2 additions & 2 deletions sdk/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
function simpleExample(asc) {
const { text, binary } = asc.compileString(SOURCE_CODE, {
optimizeLevel: 3,
runtime: "none"
runtime: "stub"
});
console.log(`>>> TEXT >>>\n${text}`);
console.log(`>>> BINARY >>>\n${binary.length} bytes`);
Expand All @@ -37,7 +37,7 @@
asc.main([
"module.ts",
"-O3",
"--runtime", "none",
"--runtime", "stub",
"--binaryFile", "module.wasm",
"--textFile", "module.wat",
"--sourceMap"
Expand Down
5 changes: 1 addition & 4 deletions sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"license": "Apache-2.0",
"private": true,
"scripts": {
"start": "http-server . -o -c-1"
},
"devDependencies": {
"http-server": "^0.12.3"
"start": "npx serve"
}
}
10 changes: 5 additions & 5 deletions transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"license": "Apache-2.0",
"private": true,
"scripts": {
"test:js": "asc assembly/index.ts --runtime none --transform ./mytransform.js",
"test:ts": "asc assembly/index.ts --runtime none --transform ./mytransform.ts",
"test:multi": "asc assembly/index.ts --runtime none --transform ./mytransform.js --transform ./mytransform.ts",
"test:js": "asc assembly/index.ts --runtime stub --transform ./mytransform.js",
"test:ts": "asc assembly/index.ts --runtime stub --transform ./mytransform.ts",
"test:multi": "asc assembly/index.ts --runtime stub --transform ./mytransform.js --transform ./mytransform.ts",
"test": "npm run test:js && npm run test:ts && npm run test:multi"
},
"devDependencies": {
"assemblyscript": "latest",
"ts-node": "^8.10.1",
"typescript": "^3.8.3"
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
}
}