Skip to content

Commit 0b97be5

Browse files
authored
Merge pull request #61 from torch2424/as-0.18
Upgraded as-bind to support AssemblyScript v0.18.x
2 parents 468356b + b7ed0ac commit 0b97be5

File tree

12 files changed

+449
-476
lines changed

12 files changed

+449
-476
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ You can install as-bind in your project by running the following:
5353

5454
To enable as-bind for your AssemblyScript Wasm modules, add the as-bind entrypoint when compiling your module:
5555

56-
`asc ./node_modules/as-bind/lib/assembly/as-bind.ts your-entryfile.ts [...other cli options...]`
56+
`asc ./node_modules/as-bind/lib/assembly/as-bind.ts your-entryfile.ts --runtime incremental --exportRuntime [...other cli options...]`
57+
58+
The things to notice are:
59+
60+
- `./node_modules/as-bind/lib/assembly/as-bind.ts` - This is the as-bind entryfile, used for exporting IDs of AssemblyScript classes so we can use them for instantiating new classes
61+
- `--runtime incremental` - This specifies that we are using the incremental runtime / garbage collection option (The AssemblyScript default). However, [all the runtime options](https://www.assemblyscript.org/garbage-collection.html) are supported (incremental, minimal, and stub).
62+
- `--exportRuntime` - This allows us to use the [AssemblyScript Garbage Collection functions added in 0.18.x](https://www.assemblyscript.org/garbage-collection.html)
5763

5864
For **optional testing purposes** , let's export an example function we can try in `your-entryfile.ts`:
5965

lib/asbind-instance/asbind-instance.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,33 @@ export default class AsbindInstance {
112112

113113
enableExportFunctionTypeCaching() {
114114
Object.keys(this.exports).forEach(exportKey => {
115-
this.exports[exportKey].shouldCacheTypes = true;
115+
if (this.exports[exportKey]) {
116+
this.exports[exportKey].shouldCacheTypes = true;
117+
}
116118
});
117119
}
118120

119121
disableExportFunctionTypeCaching() {
120122
Object.keys(this.exports).forEach(exportKey => {
121-
this.exports[exportKey].shouldCacheTypes = false;
123+
if (this.exports[exportKey]) {
124+
this.exports[exportKey].shouldCacheTypes = false;
125+
}
122126
});
123127
}
124128

125129
enableExportFunctionUnsafeReturnValue() {
126130
Object.keys(this.exports).forEach(exportKey => {
127-
this.exports[exportKey].unsafeReturnValue = true;
131+
if (this.exports[exportKey]) {
132+
this.exports[exportKey].unsafeReturnValue = true;
133+
}
128134
});
129135
}
130136

131137
disableExportFunctionUnsafeReturnValue() {
132138
Object.keys(this.exports).forEach(exportKey => {
133-
this.exports[exportKey].unsafeReturnValue = false;
139+
if (this.exports[exportKey]) {
140+
this.exports[exportKey].unsafeReturnValue = false;
141+
}
134142
});
135143
}
136144

lib/asbind-instance/bind-function.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ export function bindExportFunction(asbindInstance, exportFunctionKey) {
184184
argumentsWithReplacedRefs
185185
);
186186

187-
// Release all references
187+
// Unpin all references
188188
refIndexes.forEach(refIndex => {
189-
exports.__release(argumentsWithReplacedRefs[refIndex]);
189+
exports.__unpin(argumentsWithReplacedRefs[refIndex]);
190190
});
191191

192192
// Get the response item from the returned reference

lib/asbind-instance/reserved-export-keys.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const RESERVED_RUNTIME_EXPORT_KEYS = [
1919
"__getFloat64ArrayView",
2020
"__new",
2121
"__renew",
22-
"__retain",
23-
"__release",
22+
"__pin",
23+
"__unpin",
2424
"__instanceof",
2525
"__collect",
2626
"__rtti_base",
@@ -34,8 +34,8 @@ export const REQUIRED_RUNTIME_EXPORT_KEYS = [
3434
"__new",
3535
"__newString",
3636
"__newArray",
37-
"__retain",
38-
"__release",
37+
"__pin",
38+
"__unpin",
3939
"__instanceof",
4040
"__getString",
4141
"__getArrayBuffer",

lib/asbind-instance/supported-ref-types.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const SUPPORTED_REF_TYPES = {
1414
return wasmExports.__instanceof(ref, wasmExports.__asbind_String_ID);
1515
},
1616
getRef: (wasmExports, arg) => {
17-
return wasmExports.__retain(wasmExports.__newString(arg));
17+
return wasmExports.__pin(wasmExports.__newString(arg));
1818
},
1919
getValueFromRef: (wasmExports, responseRef) => {
2020
return wasmExports.__getString(responseRef);
@@ -28,7 +28,7 @@ export const SUPPORTED_REF_TYPES = {
2828
return wasmExports.__instanceof(ref, wasmExports.__asbind_Int8Array_ID);
2929
},
3030
getRef: (wasmExports, arg) => {
31-
return wasmExports.__retain(
31+
return wasmExports.__pin(
3232
wasmExports.__newArray(wasmExports.__asbind_Int8Array_ID, arg)
3333
);
3434
},
@@ -50,7 +50,7 @@ export const SUPPORTED_REF_TYPES = {
5050
return wasmExports.__instanceof(ref, wasmExports.__asbind_Uint8Array_ID);
5151
},
5252
getRef: (wasmExports, arg) => {
53-
return wasmExports.__retain(
53+
return wasmExports.__pin(
5454
wasmExports.__newArray(wasmExports.__asbind_Uint8Array_ID, arg)
5555
);
5656
},
@@ -72,7 +72,7 @@ export const SUPPORTED_REF_TYPES = {
7272
return wasmExports.__instanceof(ref, wasmExports.__asbind_Int16Array_ID);
7373
},
7474
getRef: (wasmExports, arg) => {
75-
return wasmExports.__retain(
75+
return wasmExports.__pin(
7676
wasmExports.__newArray(wasmExports.__asbind_Int16Array_ID, arg)
7777
);
7878
},
@@ -94,7 +94,7 @@ export const SUPPORTED_REF_TYPES = {
9494
return wasmExports.__instanceof(ref, wasmExports.__asbind_Uint16Array_ID);
9595
},
9696
getRef: (wasmExports, arg) => {
97-
return wasmExports.__retain(
97+
return wasmExports.__pin(
9898
wasmExports.__newArray(wasmExports.__asbind_Uint16Array_ID, arg)
9999
);
100100
},
@@ -116,7 +116,7 @@ export const SUPPORTED_REF_TYPES = {
116116
return wasmExports.__instanceof(ref, wasmExports.__asbind_Int32Array_ID);
117117
},
118118
getRef: (wasmExports, arg) => {
119-
return wasmExports.__retain(
119+
return wasmExports.__pin(
120120
wasmExports.__newArray(wasmExports.__asbind_Int32Array_ID, arg)
121121
);
122122
},
@@ -138,7 +138,7 @@ export const SUPPORTED_REF_TYPES = {
138138
return wasmExports.__instanceof(ref, wasmExports.__asbind_Uint32Array_ID);
139139
},
140140
getRef: (wasmExports, arg) => {
141-
return wasmExports.__retain(
141+
return wasmExports.__pin(
142142
wasmExports.__newArray(wasmExports.__asbind_Uint32Array_ID, arg)
143143
);
144144
},
@@ -163,7 +163,7 @@ export const SUPPORTED_REF_TYPES = {
163163
);
164164
},
165165
getRef: (wasmExports, arg) => {
166-
return wasmExports.__retain(
166+
return wasmExports.__pin(
167167
wasmExports.__newArray(wasmExports.__asbind_Float32Array_ID, arg)
168168
);
169169
},
@@ -188,7 +188,7 @@ export const SUPPORTED_REF_TYPES = {
188188
);
189189
},
190190
getRef: (wasmExports, arg) => {
191-
return wasmExports.__retain(
191+
return wasmExports.__pin(
192192
wasmExports.__newArray(wasmExports.__asbind_Float64Array_ID, arg)
193193
);
194194
},

lib/asbind-instance/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function validateExportsAndFunction(exports, exportFunction) {
1919
REQUIRED_RUNTIME_EXPORT_KEYS.forEach(key => {
2020
if (!exports[key]) {
2121
throw new Error(
22-
'Required Exported AssemblyScript Runtime functions are not present. Runtime must be set to "full" or "stub"'
22+
'Required Exported AssemblyScript Runtime functions are not present. Please compile your AssemblyScript with "--exportRuntime", and "--runtime" must be set to "incremental", "minimal", or "stub"'
2323
);
2424
}
2525
});

lib/assembly/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"extends": "../../../.nvm/versions/node/v12.2.0/lib/node_modules/assemblyscript/std/assembly.json",
2+
"extends": "assemblyscript/std/assembly.json",
33
"include": ["./**/*.ts"]
44
}

0 commit comments

Comments
 (0)