Skip to content

Commit dc07392

Browse files
authored
Update Binaryen for 2020 (#1034)
1 parent 58011e9 commit dc07392

15 files changed

+407
-107
lines changed

lib/sdk/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# ![AS](https://avatars1.githubusercontent.com/u/28916798?s=48) SDK
2+
3+
An overly simple SDK to use the AssemblyScript compiler on the web. This is built
4+
to distribution files using the exact versions of the compiler and its
5+
dependencies.
6+
7+
Expects [require.js](https://requirejs.org) (or compatible) on the web,
8+
primarily targeting [WebAssembly Studio](https://webassembly.studio). Note that
9+
consuming the source file in this directory directly does not solve any
10+
versioning issues - use `dist/sdk.js` instead. Do not try to bundle this.
11+
12+
Exports
13+
-------
14+
15+
* **binaryen**<br />
16+
The version of binaryen required by the compiler.
17+
18+
* **assemblyscript**<br />
19+
The AssemblyScript compiler as a library.
20+
21+
* **asc**<br />
22+
AssemblyScript compiler frontend that one will interact with
23+
([see](https://github.com/AssemblyScript/assemblyscript/tree/master/cli)).
24+
25+
Example usage
26+
-------------
27+
28+
```js
29+
require(
30+
["https://cdn.jsdelivr.net/npm/assemblyscript@nightly/dist/sdk"],
31+
function(sdk) {
32+
const { binaryen, assemblyscript, asc } = sdk;
33+
asc.ready.then(() => {
34+
asc.main(...);
35+
});
36+
}
37+
);
38+
```

lib/sdk/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const BINARYEN_VERSION = "nightly";
2+
const ASSEMBLYSCRIPT_VERSION = "nightly";
3+
4+
// AMD/require.js (browser)
5+
if (typeof define === "function" && define.amd) {
6+
const paths = {
7+
"binaryen": "https://cdn.jsdelivr.net/npm/binaryen@" + BINARYEN_VERSION + "/index",
8+
"assemblyscript": "https://cdn.jsdelivr.net/npm/assemblyscript@" + ASSEMBLYSCRIPT_VERSION + "/dist/assemblyscript",
9+
"assemblyscript/cli/asc": "https://cdn.jsdelivr.net/npm/assemblyscript@" + ASSEMBLYSCRIPT_VERSION + "/dist/asc",
10+
};
11+
require.config({ paths });
12+
define(Object.keys(paths), (binaryen, assemblyscript, asc) => ({
13+
BINARYEN_VERSION,
14+
ASSEMBLYSCRIPT_VERSION,
15+
binaryen,
16+
assemblyscript,
17+
asc
18+
}));
19+
20+
// CommonJS fallback (node)
21+
} else if (typeof module === "object" && module.exports) {
22+
module.exports = {
23+
BINARYEN_VERSION,
24+
ASSEMBLYSCRIPT_VERSION,
25+
binaryen: require("binaryen"),
26+
assemblyscript: require("assemblyscript"),
27+
asc: require("assemblyscript/cli/asc")
28+
};
29+
}

lib/sdk/tests/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
2+
<script>
3+
function assert(x) {
4+
if (!x) throw Error("assertion failed");
5+
}
6+
require(["../index"], function(sdk) {
7+
const { BINARYEN_VERSION, ASSEMBLYSCRIPT_VERSION, binaryen, assemblyscript, asc } = sdk;
8+
assert(typeof BINARYEN_VERSION === "string");
9+
assert(typeof ASSEMBLYSCRIPT_VERSION === "string");
10+
console.log("Binaryen@" + BINARYEN_VERSION);
11+
console.log("AssemblyScript@" + ASSEMBLYSCRIPT_VERSION);
12+
assert(typeof binaryen === "object" && binaryen && typeof binaryen._BinaryenTypeNone === "function");
13+
assert(typeof assemblyscript === "object" && assemblyscript && typeof assemblyscript.parse === "function");
14+
assert(typeof asc === "object" && asc && typeof asc.main === "function");
15+
asc.ready.then(() => console.log("ready", sdk));
16+
});
17+
</script>

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
2222
},
2323
"dependencies": {
24-
"binaryen": "90.0.0-nightly.20191231",
24+
"binaryen": "90.0.0-nightly.20200101",
2525
"long": "^4.0.0",
2626
"source-map-support": "^0.5.16",
2727
"ts-node": "^6.2.0"
@@ -48,9 +48,10 @@
4848
"asinit": "bin/asinit"
4949
},
5050
"scripts": {
51-
"build": "npm run build:bundle && npm run build:dts",
51+
"build": "npm run build:bundle && npm run build:dts && npm run build:sdk",
5252
"build:bundle": "webpack --mode production --display-modules",
5353
"build:dts": "node scripts/build-dts && tsc --noEmit --target ESNEXT --module commonjs --experimentalDecorators tests/require/index-release",
54+
"build:sdk": "node scripts/build-sdk",
5455
"clean": "node scripts/clean",
5556
"check": "npm run check:config && npm run check:compiler && tsc --noEmit --target ESNEXT --module commonjs --experimentalDecorators tests/require/index",
5657
"check:config": "tsc --noEmit -p src --diagnostics --listFiles",

scripts/build-sdk.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const path = require("path");
2+
const fs = require("fs");
3+
const pkg = require("../package.json");
4+
5+
fs.readFile(path.join(__dirname, "..", "lib", "sdk", "index.js"), "utf8", function(err, data) {
6+
if (err) throw err;
7+
data = data
8+
.replace(/BINARYEN_VERSION = "nightly"/, "BINARYEN_VERSION = " + JSON.stringify(pkg.dependencies.binaryen))
9+
.replace(/ASSEMBLYSCRIPT_VERSION = "nightly"/, "ASSEMBLYSCRIPT_VERSION = " + JSON.stringify(pkg.version));
10+
fs.writeFile(path.join(__dirname, "..", "dist", "sdk.js"), data, function(err) {
11+
if (err) throw err;
12+
});
13+
});

scripts/update-constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ binaryen.ready.then(() => {
1313
var match = val.match(/\b(_Binaryen\w+)\b/);
1414
if (match) {
1515
let fn = match[1];
16+
if (typeof binaryen[fn] !== "function") throw Error("API mismatch: Is Binaryen up to date?");
1617
let id = binaryen[fn]();
1718
console.log(fn + " = " + id);
1819
return key + " = " + id + " /* " + fn + " */";

src/builtins.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,6 @@ export function compileCall(
907907
case TypeKind.USIZE: { value = "usize"; break; }
908908
case TypeKind.V128: { value = "v128"; break; }
909909
case TypeKind.ANYREF: { value = "anyref"; break; }
910-
case TypeKind.EXNREF: { value = "exnref"; break; }
911910
default: assert(false);
912911
case TypeKind.VOID: { value = "void"; break; }
913912
}

src/compiler.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7144,7 +7144,7 @@ export class Compiler extends DiagnosticEmitter {
71447144
this.currentType = signatureReference.type.asNullable();
71457145
return module.i32(0);
71467146
}
7147-
// TODO: anyref context yields <usize>0
7147+
return module.ref_null();
71487148
}
71497149
this.currentType = options.usizeType;
71507150
return options.isWasm64
@@ -7282,15 +7282,15 @@ export class Compiler extends DiagnosticEmitter {
72827282
}
72837283
case ElementKind.GLOBAL: {
72847284
if (!this.compileGlobal(<Global>target)) { // reports; not yet compiled if a static field
7285-
return this.module.unreachable();
7285+
return module.unreachable();
72867286
}
72877287
let type = (<Global>target).type;
72887288
assert(type != Type.void);
72897289
if ((<Global>target).is(CommonFlags.INLINED)) {
72907290
return this.compileInlineConstant(<Global>target, contextualType, constraints);
72917291
}
72927292
this.currentType = type;
7293-
return this.module.global_get((<Global>target).internalName, type.toNativeType());
7293+
return module.global_get((<Global>target).internalName, type.toNativeType());
72947294
}
72957295
case ElementKind.ENUMVALUE: { // here: if referenced from within the same enum
72967296
if (!target.is(CommonFlags.COMPILED)) {
@@ -7299,14 +7299,14 @@ export class Compiler extends DiagnosticEmitter {
72997299
expression.range
73007300
);
73017301
this.currentType = Type.i32;
7302-
return this.module.unreachable();
7302+
return module.unreachable();
73037303
}
73047304
this.currentType = Type.i32;
73057305
if ((<EnumValue>target).is(CommonFlags.INLINED)) {
73067306
assert((<EnumValue>target).constantValueKind == ConstantValueKind.INTEGER);
7307-
return this.module.i32(i64_low((<EnumValue>target).constantIntegerValue));
7307+
return module.i32(i64_low((<EnumValue>target).constantIntegerValue));
73087308
}
7309-
return this.module.global_get((<EnumValue>target).internalName, NativeType.I32);
7309+
return module.global_get((<EnumValue>target).internalName, NativeType.I32);
73107310
}
73117311
case ElementKind.FUNCTION_PROTOTYPE: {
73127312
let instance = this.resolver.resolveFunction(
@@ -7315,9 +7315,13 @@ export class Compiler extends DiagnosticEmitter {
73157315
makeMap<string,Type>(flow.contextualTypeArguments)
73167316
);
73177317
if (!(instance && this.compileFunction(instance))) return module.unreachable();
7318+
if (contextualType.is(TypeFlags.HOST | TypeFlags.REFERENCE)) {
7319+
this.currentType = Type.anyref;
7320+
return module.ref_func(instance.internalName);
7321+
}
73187322
let index = this.ensureFunctionTableEntry(instance);
73197323
this.currentType = instance.signature.type;
7320-
return this.module.i32(index);
7324+
return module.i32(index);
73217325
}
73227326
}
73237327
this.error(
@@ -9001,6 +9005,7 @@ export class Compiler extends DiagnosticEmitter {
90019005
case TypeKind.F32: return module.f32(0);
90029006
case TypeKind.F64: return module.f64(0);
90039007
case TypeKind.V128: return module.v128(v128_zero);
9008+
case TypeKind.ANYREF: return module.ref_null();
90049009
}
90059010
}
90069011

@@ -9099,9 +9104,11 @@ export class Compiler extends DiagnosticEmitter {
90999104
flow.freeTempLocal(temp);
91009105
return ret;
91019106
}
9102-
// case TypeKind.ANYREF: {
9103-
// TODO: !ref.is_null
9104-
// }
9107+
case TypeKind.ANYREF: {
9108+
// TODO: non-null object might still be considered falseish
9109+
// i.e. a ref to Boolean(false), Number(0), String("") etc.
9110+
return module.unary(UnaryOp.EqzI32, module.ref_is_null(expr));
9111+
}
91059112
default: {
91069113
assert(false);
91079114
return module.i32(0);

src/glue/binaryen.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export declare function _BinaryenTypeInt64(): BinaryenType;
3232
export declare function _BinaryenTypeFloat32(): BinaryenType;
3333
export declare function _BinaryenTypeFloat64(): BinaryenType;
3434
export declare function _BinaryenTypeVec128(): BinaryenType;
35+
export declare function _BinaryenTypeFuncref(): BinaryenType;
3536
export declare function _BinaryenTypeAnyref(): BinaryenType;
37+
export declare function _BinaryenTypeNullref(): BinaryenType;
3638
export declare function _BinaryenTypeExnref(): BinaryenType;
3739
export declare function _BinaryenTypeUnreachable(): BinaryenType;
3840
export declare function _BinaryenTypeAuto(): BinaryenType;
@@ -95,6 +97,9 @@ export declare function _BinaryenMemoryInitId(): BinaryenExpressionId;
9597
export declare function _BinaryenDataDropId(): BinaryenExpressionId;
9698
export declare function _BinaryenMemoryCopyId(): BinaryenExpressionId;
9799
export declare function _BinaryenMemoryFillId(): BinaryenExpressionId;
100+
export declare function _BinaryenRefNullId(): BinaryenExpressionId;
101+
export declare function _BinaryenRefIsNullId(): BinaryenExpressionId;
102+
export declare function _BinaryenRefFuncId(): BinaryenExpressionId;
98103
export declare function _BinaryenTryId(): BinaryenExpressionId;
99104
export declare function _BinaryenThrowId(): BinaryenExpressionId;
100105
export declare function _BinaryenRethrowId(): BinaryenExpressionId;
@@ -468,7 +473,7 @@ export declare function _BinaryenStore(module: BinaryenModuleRef, bytes: u32, of
468473
export declare function _BinaryenConst(module: BinaryenModuleRef, value: usize): BinaryenExpressionRef;
469474
export declare function _BinaryenUnary(module: BinaryenModuleRef, op: BinaryenOp, value: BinaryenExpressionRef): BinaryenExpressionRef;
470475
export declare function _BinaryenBinary(module: BinaryenModuleRef, op: BinaryenOp, left: BinaryenExpressionRef, right: BinaryenExpressionRef): BinaryenExpressionRef;
471-
export declare function _BinaryenSelect(module: BinaryenModuleRef, condition: BinaryenExpressionRef, ifTrue: BinaryenExpressionRef, ifFalse: BinaryenExpressionRef): BinaryenExpressionRef;
476+
export declare function _BinaryenSelect(module: BinaryenModuleRef, condition: BinaryenExpressionRef, ifTrue: BinaryenExpressionRef, ifFalse: BinaryenExpressionRef, type: BinaryenType): BinaryenExpressionRef;
472477
export declare function _BinaryenDrop(module: BinaryenModuleRef, value: BinaryenExpressionRef): BinaryenExpressionRef;
473478
export declare function _BinaryenReturn(module: BinaryenModuleRef, value: BinaryenExpressionRef): BinaryenExpressionRef;
474479
export declare function _BinaryenHost(module: BinaryenModuleRef, op: BinaryenOp, name: usize | 0, operands: usize, numOperands: BinaryenIndex): BinaryenExpressionRef;
@@ -495,6 +500,10 @@ export declare function _BinaryenDataDrop(module: BinaryenModuleRef, segment: u3
495500
export declare function _BinaryenMemoryCopy(module: BinaryenModuleRef, dest: BinaryenExpressionRef, source: BinaryenExpressionRef, size: BinaryenExpressionRef): BinaryenExpressionRef;
496501
export declare function _BinaryenMemoryFill(module: BinaryenModuleRef, dest: BinaryenExpressionRef, value: BinaryenExpressionRef, size: BinaryenExpressionRef): BinaryenExpressionRef;
497502

503+
export declare function _BinaryenRefNull(module: BinaryenModuleRef): BinaryenExpressionRef;
504+
export declare function _BinaryenRefIsNull(module: BinaryenModuleRef, value: BinaryenExpressionRef): BinaryenExpressionRef;
505+
export declare function _BinaryenRefFunc(module: BinaryenModuleRef, funcName: usize): BinaryenExpressionRef;
506+
498507
export declare function _BinaryenTry(module: BinaryenModuleRef, body: BinaryenExpressionRef, catchBody: BinaryenExpressionRef): BinaryenExpressionRef;
499508
export declare function _BinaryenThrow(module: BinaryenModuleRef, event: usize, operands: usize, numOperands: BinaryenIndex): BinaryenExpressionRef;
500509
export declare function _BinaryenRethrow(module: BinaryenModuleRef, exnref: BinaryenExpressionRef): BinaryenExpressionRef;
@@ -651,6 +660,10 @@ export declare function _BinaryenMemoryFillGetDest(expr: BinaryenExpressionRef):
651660
export declare function _BinaryenMemoryFillGetValue(expr: BinaryenExpressionRef): BinaryenExpressionRef;
652661
export declare function _BinaryenMemoryFillGetSize(expr: BinaryenExpressionRef): BinaryenExpressionRef;
653662

663+
export declare function _BinaryenRefIsNullGetValue(expr: BinaryenExpressionRef): BinaryenExpressionRef;
664+
665+
export declare function _BinaryenRefFuncGetFunc(expr: BinaryenExpressionRef): usize;
666+
654667
export declare function _BinaryenTryGetBody(expr: BinaryenExpressionRef): BinaryenExpressionRef;
655668
export declare function _BinaryenTryGetCatchBody(expr: BinaryenExpressionRef): BinaryenExpressionRef;
656669

0 commit comments

Comments
 (0)