Skip to content

Commit

Permalink
Merge pull request #61 from torch2424/as-0.18
Browse files Browse the repository at this point in the history
Upgraded as-bind to support AssemblyScript v0.18.x
  • Loading branch information
torch2424 committed Feb 9, 2021
2 parents 468356b + b7ed0ac commit 0b97be5
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 476 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -53,7 +53,13 @@ You can install as-bind in your project by running the following:

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

`asc ./node_modules/as-bind/lib/assembly/as-bind.ts your-entryfile.ts [...other cli options...]`
`asc ./node_modules/as-bind/lib/assembly/as-bind.ts your-entryfile.ts --runtime incremental --exportRuntime [...other cli options...]`

The things to notice are:

- `./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
- `--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).
- `--exportRuntime` - This allows us to use the [AssemblyScript Garbage Collection functions added in 0.18.x](https://www.assemblyscript.org/garbage-collection.html)

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

Expand Down
16 changes: 12 additions & 4 deletions lib/asbind-instance/asbind-instance.js
Expand Up @@ -112,25 +112,33 @@ export default class AsbindInstance {

enableExportFunctionTypeCaching() {
Object.keys(this.exports).forEach(exportKey => {
this.exports[exportKey].shouldCacheTypes = true;
if (this.exports[exportKey]) {
this.exports[exportKey].shouldCacheTypes = true;
}
});
}

disableExportFunctionTypeCaching() {
Object.keys(this.exports).forEach(exportKey => {
this.exports[exportKey].shouldCacheTypes = false;
if (this.exports[exportKey]) {
this.exports[exportKey].shouldCacheTypes = false;
}
});
}

enableExportFunctionUnsafeReturnValue() {
Object.keys(this.exports).forEach(exportKey => {
this.exports[exportKey].unsafeReturnValue = true;
if (this.exports[exportKey]) {
this.exports[exportKey].unsafeReturnValue = true;
}
});
}

disableExportFunctionUnsafeReturnValue() {
Object.keys(this.exports).forEach(exportKey => {
this.exports[exportKey].unsafeReturnValue = false;
if (this.exports[exportKey]) {
this.exports[exportKey].unsafeReturnValue = false;
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/asbind-instance/bind-function.js
Expand Up @@ -184,9 +184,9 @@ export function bindExportFunction(asbindInstance, exportFunctionKey) {
argumentsWithReplacedRefs
);

// Release all references
// Unpin all references
refIndexes.forEach(refIndex => {
exports.__release(argumentsWithReplacedRefs[refIndex]);
exports.__unpin(argumentsWithReplacedRefs[refIndex]);
});

// Get the response item from the returned reference
Expand Down
8 changes: 4 additions & 4 deletions lib/asbind-instance/reserved-export-keys.js
Expand Up @@ -19,8 +19,8 @@ export const RESERVED_RUNTIME_EXPORT_KEYS = [
"__getFloat64ArrayView",
"__new",
"__renew",
"__retain",
"__release",
"__pin",
"__unpin",
"__instanceof",
"__collect",
"__rtti_base",
Expand All @@ -34,8 +34,8 @@ export const REQUIRED_RUNTIME_EXPORT_KEYS = [
"__new",
"__newString",
"__newArray",
"__retain",
"__release",
"__pin",
"__unpin",
"__instanceof",
"__getString",
"__getArrayBuffer",
Expand Down
18 changes: 9 additions & 9 deletions lib/asbind-instance/supported-ref-types.js
Expand Up @@ -14,7 +14,7 @@ export const SUPPORTED_REF_TYPES = {
return wasmExports.__instanceof(ref, wasmExports.__asbind_String_ID);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(wasmExports.__newString(arg));
return wasmExports.__pin(wasmExports.__newString(arg));
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getString(responseRef);
Expand All @@ -28,7 +28,7 @@ export const SUPPORTED_REF_TYPES = {
return wasmExports.__instanceof(ref, wasmExports.__asbind_Int8Array_ID);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(
return wasmExports.__pin(
wasmExports.__newArray(wasmExports.__asbind_Int8Array_ID, arg)
);
},
Expand All @@ -50,7 +50,7 @@ export const SUPPORTED_REF_TYPES = {
return wasmExports.__instanceof(ref, wasmExports.__asbind_Uint8Array_ID);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(
return wasmExports.__pin(
wasmExports.__newArray(wasmExports.__asbind_Uint8Array_ID, arg)
);
},
Expand All @@ -72,7 +72,7 @@ export const SUPPORTED_REF_TYPES = {
return wasmExports.__instanceof(ref, wasmExports.__asbind_Int16Array_ID);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(
return wasmExports.__pin(
wasmExports.__newArray(wasmExports.__asbind_Int16Array_ID, arg)
);
},
Expand All @@ -94,7 +94,7 @@ export const SUPPORTED_REF_TYPES = {
return wasmExports.__instanceof(ref, wasmExports.__asbind_Uint16Array_ID);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(
return wasmExports.__pin(
wasmExports.__newArray(wasmExports.__asbind_Uint16Array_ID, arg)
);
},
Expand All @@ -116,7 +116,7 @@ export const SUPPORTED_REF_TYPES = {
return wasmExports.__instanceof(ref, wasmExports.__asbind_Int32Array_ID);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(
return wasmExports.__pin(
wasmExports.__newArray(wasmExports.__asbind_Int32Array_ID, arg)
);
},
Expand All @@ -138,7 +138,7 @@ export const SUPPORTED_REF_TYPES = {
return wasmExports.__instanceof(ref, wasmExports.__asbind_Uint32Array_ID);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(
return wasmExports.__pin(
wasmExports.__newArray(wasmExports.__asbind_Uint32Array_ID, arg)
);
},
Expand All @@ -163,7 +163,7 @@ export const SUPPORTED_REF_TYPES = {
);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(
return wasmExports.__pin(
wasmExports.__newArray(wasmExports.__asbind_Float32Array_ID, arg)
);
},
Expand All @@ -188,7 +188,7 @@ export const SUPPORTED_REF_TYPES = {
);
},
getRef: (wasmExports, arg) => {
return wasmExports.__retain(
return wasmExports.__pin(
wasmExports.__newArray(wasmExports.__asbind_Float64Array_ID, arg)
);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/asbind-instance/validate.js
Expand Up @@ -19,7 +19,7 @@ export function validateExportsAndFunction(exports, exportFunction) {
REQUIRED_RUNTIME_EXPORT_KEYS.forEach(key => {
if (!exports[key]) {
throw new Error(
'Required Exported AssemblyScript Runtime functions are not present. Runtime must be set to "full" or "stub"'
'Required Exported AssemblyScript Runtime functions are not present. Please compile your AssemblyScript with "--exportRuntime", and "--runtime" must be set to "incremental", "minimal", or "stub"'
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/assembly/tsconfig.json
@@ -1,4 +1,4 @@
{
"extends": "../../../.nvm/versions/node/v12.2.0/lib/node_modules/assemblyscript/std/assembly.json",
"extends": "assemblyscript/std/assembly.json",
"include": ["./**/*.ts"]
}

0 comments on commit 0b97be5

Please sign in to comment.