- return " __attribute__((visibility(\"default\"))) TS_" + fn_name + "("
-
- def wasm_import_header(self, target):
- res = """
-const imports: any = {};
-imports.env = {};
-
-imports.env.tableBase = 0;
-imports.env.table = new WebAssembly.Table({initial: 4, element: 'anyfunc'});
-
-imports.env["abort"] = function () {
- console.error("ABORT");
-};
-imports.env["js_invoke_function"] = function(fn: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number, arg7: number, arg8: number, arg9: number, arg10: number) {
- console.log('function called from wasm:', fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
-};
-imports.env["js_free_function_ptr"] = function(fn: number) {
- console.log("function ptr free'd from wasm:", fn);
-};
-
-imports.wasi_snapshot_preview1 = {
- "fd_write" : () => {
- console.log("ABORT");
- },
- "random_get" : () => {
- console.log("RAND GET");
- },
- "environ_sizes_get" : () => {
- console.log("wasi_snapshot_preview1:environ_sizes_get");
- },
- "proc_exit" : () => {
- console.log("wasi_snapshot_preview1:proc_exit");
- },
- "environ_get" : () => {
- console.log("wasi_snapshot_preview1:environ_get");
- },
-};
-
-var wasm = null;
-let isWasmInitialized: boolean = false;
-"""
-
- if target == Target.NODEJS:
- res += """import * as fs from 'fs';
-export async function initializeWasm(path) {
- const source = fs.readFileSync(path);
- const { instance: wasmInstance } = await WebAssembly.instantiate(source, imports);
- wasm = wasmInstance.exports;
- isWasmInitialized = true;
-};
-"""
- else:
- res += """
-export async function initializeWasm(uri) {
- const stream = fetch(uri);
- const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
- wasm = wasmInstance.exports;
- isWasmInitialized = true;
-};
-
-"""
-
- return res + """
-
-
-// WASM CODEC
-
-const nextMultipleOfFour = (value: number) => {
- return Math.ceil(value / 4) * 4;
-}
-
-const encodeUint8Array = (inputArray) => {
- const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
- const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1);
- arrayLengthView[0] = inputArray.length;
- const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 4, inputArray.length);
- arrayMemoryView.set(inputArray);
- return cArrayPointer;
-}
-
-const encodeUint32Array = (inputArray) => {
- const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4);
- const arrayMemoryView = new Uint32Array(wasm.memory.buffer, cArrayPointer, inputArray.length);
- arrayMemoryView.set(inputArray, 1);
- arrayMemoryView[0] = inputArray.length;
- return cArrayPointer;
-}
-
-const getArrayLength = (arrayPointer) => {
- const arraySizeViewer = new Uint32Array(
- wasm.memory.buffer, // value
- arrayPointer, // offset
- 1 // one int
- );
- return arraySizeViewer[0];
-}
-const decodeUint8Array = (arrayPointer, free = true) => {
- const arraySize = getArrayLength(arrayPointer);
- const actualArrayViewer = new Uint8Array(
- wasm.memory.buffer, // value
- arrayPointer + 4, // offset (ignoring length bytes)
- arraySize // uint8 count
- );
- // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
- // will free the underlying memory when it becomes unreachable instead of copying here.
- const actualArray = actualArrayViewer.slice(0, arraySize);
- if (free) {
- wasm.TS_free(arrayPointer);
- }
- return actualArray;
-}
-const decodeUint32Array = (arrayPointer, free = true) => {
- const arraySize = getArrayLength(arrayPointer);
- const actualArrayViewer = new Uint32Array(
- wasm.memory.buffer, // value
- arrayPointer + 4, // offset (ignoring length bytes)
- arraySize // uint32 count
- );
- // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
- // will free the underlying memory when it becomes unreachable instead of copying here.
- const actualArray = actualArrayViewer.slice(0, arraySize);
- if (free) {
- wasm.TS_free(arrayPointer);
- }
- return actualArray;
-}
-
-const encodeString = (string) => {
- // make malloc count divisible by 4
- const memoryNeed = nextMultipleOfFour(string.length + 1);
- const stringPointer = wasm.TS_malloc(memoryNeed);
- const stringMemoryView = new Uint8Array(
- wasm.memory.buffer, // value
- stringPointer, // offset
- string.length + 1 // length
- );
- for (let i = 0; i < string.length; i++) {
- stringMemoryView[i] = string.charCodeAt(i);
- }
- stringMemoryView[string.length] = 0;
- return stringPointer;
-}
-
-const decodeString = (stringPointer, free = true) => {
- const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer);
- let cursor = 0;
- let result = '';
-
- while (memoryView[cursor] !== 0) {
- result += String.fromCharCode(memoryView[cursor]);
- cursor++;
- }
-
- if (free) {
- wasm.wasm_free(stringPointer);
- }
-
- return result;
-};
-"""