Skip to content

Commit

Permalink
Prepare for Binaryen compiled to Wasm (#1029)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 31, 2019
1 parent 4da0316 commit 4611609
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 492 deletions.
2 changes: 1 addition & 1 deletion bin/asc
Expand Up @@ -18,5 +18,5 @@ try { require("source-map-support").install(); } catch (e) {}

const asc = module.exports = require("../cli/asc.js");
if (/\basc$/.test(process.argv[1])) {
process.exitCode = asc.main(process.argv.slice(2));
asc.ready.then(() => process.exitCode = asc.main(process.argv.slice(2)));
}
33 changes: 19 additions & 14 deletions cli/README.md
Expand Up @@ -17,19 +17,21 @@ The API accepts the same options as the CLI but also lets you override stdout an

```js
const asc = require("assemblyscript/cli/asc");
asc.main([
"myModule.ts",
"--binaryFile", "myModule.wasm",
"--optimize",
"--sourceMap",
"--measure"
], {
stdout: process.stdout,
stderr: process.stderr
}, function(err) {
if (err)
throw err;
...
asc.ready.then(() => {
asc.main([
"myModule.ts",
"--binaryFile", "myModule.wasm",
"--optimize",
"--sourceMap",
"--measure"
], {
stdout: process.stdout,
stderr: process.stderr
}, function(err) {
if (err)
throw err;
...
});
});
```

Expand All @@ -43,6 +45,9 @@ const options = require("assemblyscript/cli/asc.json");
You can also compile a source string directly, for example in a browser environment:

```js
const { binary, text, stdout, stderr } = asc.compileString(`...`, { optimize: 2 });
const asc = require("assemblyscript/cli/asc");
asc.ready.then(() => {
const { binary, text, stdout, stderr } = asc.compileString(`...`, { optimize: 2 });
});
...
```
3 changes: 3 additions & 0 deletions cli/asc.d.ts
@@ -1,6 +1,9 @@
import { OptionDescription } from "./util/options";
export { OptionDescription };

/** Ready promise resolved once/if the compiler is ready. */
export const ready: Promise<void>;

/** Whether this is a webpack bundle or not. */
export const isBundle: boolean;

Expand Down
44 changes: 23 additions & 21 deletions cli/asc.js
Expand Up @@ -24,37 +24,39 @@ const mkdirp = require("./util/mkdirp");
const find = require("./util/find");
const EOL = process.platform === "win32" ? "\r\n" : "\n";
const SEP = process.platform === "win32" ? "\\" : "/";
const binaryen = global.Binaryen || (global.Binaryen = require("binaryen"));

// global.Binaryen = require("../lib/binaryen");
// Proxy Binaryen's ready event
Object.defineProperty(exports, "ready", {
get: function() { return binaryen.ready; }
});

// Emscripten adds an `uncaughtException` listener to Binaryen that results in an additional
// useless code fragment on top of an actual error. suppress this:
if (process.removeAllListeners) process.removeAllListeners("uncaughtException");

// Use distribution files if present, otherwise run the sources directly
var assemblyscript, isDev = false;
(() => {
try { // `asc` on the command line
assemblyscript = require("../dist/assemblyscript.js");
} catch (e) {
try { // `asc` on the command line without dist files
require("ts-node").register({
project: path.join(__dirname, "..", "src", "tsconfig.json"),
skipIgnore: true,
compilerOptions: { target: "ES2016" }
});
require("../src/glue/js");
assemblyscript = require("../src");
isDev = true;
} catch (e_ts) {
try { // `require("dist/asc.js")` in explicit browser tests
assemblyscript = eval("require('./assemblyscript')");
} catch (e) {
throw Error(e_ts.stack + "\n---\n" + e.stack);
}
try { // `asc` on the command line
assemblyscript = require("../dist/assemblyscript.js");
} catch (e) {
try { // `asc` on the command line without dist files
require("ts-node").register({
project: path.join(__dirname, "..", "src", "tsconfig.json"),
skipIgnore: true,
compilerOptions: { target: "ES2016" }
});
require("../src/glue/js");
assemblyscript = require("../src");
isDev = true;
} catch (e_ts) {
try { // `require("dist/asc.js")` in explicit browser tests
assemblyscript = eval("require('./assemblyscript')");
} catch (e) {
throw Error(e_ts.stack + "\n---\n" + e.stack);
}
}
})();
}

/** Whether this is a webpack bundle or not. */
exports.isBundle = typeof BUNDLE_VERSION === "string";
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
},
"dependencies": {
"binaryen": "89.0.0-nightly.20191215",
"binaryen": "90.0.0-nightly.20191231",
"long": "^4.0.0",
"source-map-support": "^0.5.16",
"ts-node": "^6.2.0"
Expand Down
24 changes: 24 additions & 0 deletions scripts/update-constants.js
@@ -0,0 +1,24 @@
// Updates the Binaryen constants in src/module.ts

const fs = require("fs");
const path = require("path");
const binaryen = require("binaryen");

const srcfile = path.join(__dirname, "..", "src", "module.ts");
var src = fs.readFileSync(srcfile, "utf8");

binaryen.ready.then(() => {
src = src.replace(/enum (\w+) \{([^}]*)\}/g, function($0) {
return $0.replace(/(\w+)[ ]+=[ ]+([^,;\n]+)/g, function($0, key, val) {
var match = val.match(/\b(_Binaryen\w+)\b/);
if (match) {
let fn = match[1];
let id = binaryen[fn]();
console.log(fn + " = " + id);
return key + " = " + id + " /* " + fn + " */";
}
return $0;
});
});
fs.writeFileSync(srcfile, src, "utf8");
});
14 changes: 14 additions & 0 deletions src/README.md
Expand Up @@ -2,4 +2,18 @@ Portable compiler sources that compile to both JavaScript using `tsc` and, event

Architecture
------------

![](https://assemblyscript.github.io/assemblyscript/media/architecture.svg)

Usage
-----

Note that using the compiler as a library requires awaiting Binaryen ready state, like so:

```js
const binaryen = require("binaryen");
const assemblyscript = require("assemblyscript");
binaryen.ready.then(() => {
// do something with assemblyscript
});
```
2 changes: 2 additions & 0 deletions src/glue/binaryen.d.ts
Expand Up @@ -352,6 +352,7 @@ export declare function _BinaryenMinSVecI8x16(): BinaryenOp;
export declare function _BinaryenMinUVecI8x16(): BinaryenOp;
export declare function _BinaryenMaxSVecI8x16(): BinaryenOp;
export declare function _BinaryenMaxUVecI8x16(): BinaryenOp;
export declare function _BinaryenAvgrUVecI8x16(): BinaryenOp;
export declare function _BinaryenNegVecI16x8(): BinaryenOp;
export declare function _BinaryenAnyTrueVecI16x8(): BinaryenOp;
export declare function _BinaryenAllTrueVecI16x8(): BinaryenOp;
Expand All @@ -369,6 +370,7 @@ export declare function _BinaryenMinSVecI16x8(): BinaryenOp;
export declare function _BinaryenMinUVecI16x8(): BinaryenOp;
export declare function _BinaryenMaxSVecI16x8(): BinaryenOp;
export declare function _BinaryenMaxUVecI16x8(): BinaryenOp;
export declare function _BinaryenAvgrUVecI16x8(): BinaryenOp;
export declare function _BinaryenNegVecI32x4(): BinaryenOp;
export declare function _BinaryenAnyTrueVecI32x4(): BinaryenOp;
export declare function _BinaryenAllTrueVecI32x4(): BinaryenOp;
Expand Down
2 changes: 1 addition & 1 deletion src/glue/binaryen.js
@@ -1,4 +1,4 @@
const binaryen = global.Binaryen || require("binaryen");
const binaryen = global.Binaryen || (global.Binaryen = require("binaryen"));

module.exports = binaryen;

Expand Down

0 comments on commit 4611609

Please sign in to comment.