-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished support for import object wrapping
- Loading branch information
Showing
9 changed files
with
409 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import SUPPORTED_TYPES from "./supported-types"; | ||
|
||
// Use instance of to scan the importObject for Asbinded Import functions, | ||
// That are then replaced with the actual function that wraps around the import and does the appropriate stuff | ||
export class AsbindImportFunction { | ||
constructor(importFunction) { | ||
this.importFunction = importFunction; | ||
} | ||
|
||
applyImportFunction(wasmExports, functionArgumentRefs) { | ||
if (!wasmExports) { | ||
throw new Error("Could not access the instantiated Wasm Export"); | ||
} | ||
|
||
// Get the types from the passed references in AS | ||
const functionArguments = []; | ||
functionArgumentRefs.forEach(ref => { | ||
// Find our supported type | ||
let supportedType = undefined; | ||
Object.keys(SUPPORTED_TYPES).some(key => { | ||
if (SUPPORTED_TYPES[key].isTypeFromReference(wasmExports, ref)) { | ||
supportedType = SUPPORTED_TYPES[key]; | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
if (!supportedType) { | ||
throw new Error( | ||
`The reference, ${ref}, passed to the import object function is not a supported type by asbind` | ||
); | ||
} | ||
functionArguments.push(supportedType.getValueFromRef(wasmExports, ref)); | ||
}); | ||
|
||
// Call the import function | ||
this.importFunction.apply(null, functionArguments); | ||
|
||
// TODO: Returning from Import functions is not supported by asbind :( | ||
} | ||
} | ||
|
||
// Fuction that takes in an importFunction to be wrapped, | ||
// And the type parameters, and return parameter of the function | ||
export function asbindWrapImportObjectFunction(importFunction) { | ||
// Return our AsbindImportFunction | ||
return new AsbindImportFunction(importFunction); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Wrapper around the loader instantiate | ||
import * as loader from "assemblyscript/lib/loader"; | ||
import { AsbindImportFunction } from "./import-object"; | ||
|
||
export async function asbindInstantiate(source, importObject) { | ||
let wasmInstanceExports; | ||
|
||
// Need to traverse the importObject and replace all wrapped asbind import functions | ||
const traverseObjectAndWrapAsbindImports = function(baseObject) { | ||
if (!baseObject) { | ||
return; | ||
} | ||
|
||
Object.keys(baseObject).forEach(baseObjectKey => { | ||
if (baseObject[baseObjectKey] instanceof AsbindImportFunction) { | ||
let asbindImportFunction = baseObject[baseObjectKey]; | ||
// Wrap the asbindImportFunction as a function that passes in the argument references, | ||
// As well as the instantiated exports of the wasm module | ||
baseObject[baseObjectKey] = function() { | ||
let functionArgumentRefs = []; | ||
for (let i = 0; i < arguments.length; i++) { | ||
functionArgumentRefs.push(arguments[i]); | ||
} | ||
asbindImportFunction.applyImportFunction( | ||
wasmInstanceExports, | ||
functionArgumentRefs | ||
); | ||
}; | ||
} else if (typeof baseObject[baseObjectKey] === "object") { | ||
traverseObjectAndWrapAsbindImports(baseObject[baseObjectKey]); | ||
} | ||
}); | ||
}; | ||
traverseObjectAndWrapAsbindImports(importObject); | ||
|
||
// Use the correct loader instantiation | ||
// https://github.com/AssemblyScript/assemblyscript/tree/master/lib/loader#api | ||
if (source instanceof WebAssembly.Module) { | ||
wasmInstanceExports = loader.instantiate(source, importObject); | ||
} else if (source instanceof Uint8Array) { | ||
wasmInstanceExports = loader.instantiateBuffer(source, importObject); | ||
} else if (source instanceof Response) { | ||
wasmInstanceExports = await loader.instantiateStreaming( | ||
source, | ||
importObject | ||
); | ||
} else { | ||
throw new Error( | ||
"The type of value passed as the Wasm source is not supported" | ||
); | ||
} | ||
|
||
return wasmInstanceExports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.
Just use
return wasmExports.__getInt8Array(responseRef)
,wasmExports.__getUint8Array
,wasmExports.__getFloat32Array
and etc... They are blazing fast