diff --git a/game-of-life/README.md b/game-of-life/README.md index 52a970d..f62d69a 100644 --- a/game-of-life/README.md +++ b/game-of-life/README.md @@ -24,4 +24,4 @@ Afterwards, run $> npm start ``` -to start a local server. Should also automatically launch a browser. +to start a local server. diff --git a/game-of-life/build/optimized.wasm b/game-of-life/build/optimized.wasm index 75db097..4fb95d0 100644 Binary files a/game-of-life/build/optimized.wasm and b/game-of-life/build/optimized.wasm differ diff --git a/game-of-life/package.json b/game-of-life/package.json index 45b024d..e567bf7 100644 --- a/game-of-life/package.json +++ b/game-of-life/package.json @@ -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" } } diff --git a/i64/package.json b/i64/package.json index dffb55b..ef38428 100644 --- a/i64/package.json +++ b/i64/package.json @@ -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": [ diff --git a/interference/README.md b/interference/README.md index 4c6c858..7f694fc 100644 --- a/interference/README.md +++ b/interference/README.md @@ -24,4 +24,4 @@ Afterwards, run $> npm start ``` -to start a local server. Should also automatically launch a browser. +to start a local server. diff --git a/interference/build/optimized.wasm b/interference/build/optimized.wasm index d6581a9..bb258c2 100644 Binary files a/interference/build/optimized.wasm and b/interference/build/optimized.wasm differ diff --git a/interference/package.json b/interference/package.json index 5ccbb27..dd7fa7b 100644 --- a/interference/package.json +++ b/interference/package.json @@ -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" } } diff --git a/libm/package.json b/libm/package.json index 900b0c0..a8fcc56 100644 --- a/libm/package.json +++ b/libm/package.json @@ -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": [ diff --git a/loader/build/optimized.wasm b/loader/build/optimized.wasm index 994c501..5903c4c 100644 Binary files a/loader/build/optimized.wasm and b/loader/build/optimized.wasm differ diff --git a/loader/package.json b/loader/package.json index 286aab2..019045a 100644 --- a/loader/package.json +++ b/loader/package.json @@ -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" }, diff --git a/loader/tests/index.js b/loader/tests/index.js index 0042010..45329c5 100644 --- a/loader/tests/index.js +++ b/loader/tests/index.js @@ -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; @@ -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); @@ -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. @@ -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`) 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); @@ -82,8 +76,7 @@ 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. @@ -91,18 +84,19 @@ const { 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. @@ -114,27 +108,31 @@ 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 @@ -142,29 +140,30 @@ const { // 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 :) diff --git a/mandelbrot/README.md b/mandelbrot/README.md index 06e392c..f8ad965 100644 --- a/mandelbrot/README.md +++ b/mandelbrot/README.md @@ -24,4 +24,4 @@ Afterwards, run $> npm start ``` -to start a local server. Should also automatically launch a browser. +to start a local server. diff --git a/mandelbrot/build/optimized.wasm b/mandelbrot/build/optimized.wasm index 076eed8..bd2f855 100644 Binary files a/mandelbrot/build/optimized.wasm and b/mandelbrot/build/optimized.wasm differ diff --git a/mandelbrot/package.json b/mandelbrot/package.json index 122d777..5b6c6b9 100644 --- a/mandelbrot/package.json +++ b/mandelbrot/package.json @@ -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" } } diff --git a/n-body/README.md b/n-body/README.md index 6d55b2d..f3d370b 100644 --- a/n-body/README.md +++ b/n-body/README.md @@ -24,7 +24,7 @@ Afterwards, run $> npm start ``` -to start a local server. Should also automatically launch a browser. +to start a local server. To run the benchmark: diff --git a/n-body/build/as_nbody.wasm b/n-body/build/as_nbody.wasm index 43a2275..ed27e75 100644 Binary files a/n-body/build/as_nbody.wasm and b/n-body/build/as_nbody.wasm differ diff --git a/n-body/package.json b/n-body/package.json index 7252eb4..1fb86a8 100644 --- a/n-body/package.json +++ b/n-body/package.json @@ -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" } } diff --git a/n-body/scripts/postprocess-js.js b/n-body/scripts/postprocess-js.js index 263251a..9801c05 100644 --- a/n-body/scripts/postprocess-js.js +++ b/n-body/scripts/postprocess-js.js @@ -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); diff --git a/sdk/index.html b/sdk/index.html index 2a3f325..6319231 100644 --- a/sdk/index.html +++ b/sdk/index.html @@ -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`); @@ -37,7 +37,7 @@ asc.main([ "module.ts", "-O3", - "--runtime", "none", + "--runtime", "stub", "--binaryFile", "module.wasm", "--textFile", "module.wat", "--sourceMap" diff --git a/sdk/package.json b/sdk/package.json index 25bdf5d..7f95c92 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -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" } } diff --git a/transform/package.json b/transform/package.json index bd3ebc4..c46dca4 100644 --- a/transform/package.json +++ b/transform/package.json @@ -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" } }