| |
| |
| |
| |
| |
|
|
| addToLibrary({ |
| |
| |
| $uleb128EncodeWithLen__internal: true, |
| $uleb128EncodeWithLen: (arr) => { |
| const n = arr.length; |
| #if ASSERTIONS |
| assert(n < 16384); |
| #endif |
| |
| |
| return [(n % 128) | 128, n >> 7, ...arr]; |
| }, |
| $wasmTypeCodes__internal: true, |
| |
| |
| |
| $wasmTypeCodes: `{ |
| 'i': 0x7f, // i32 |
| #if MEMORY64 |
| 'p': 0x7e, // i64 |
| #else |
| 'p': 0x7f, // i32 |
| #endif |
| 'j': 0x7e, // i64 |
| 'f': 0x7d, // f32 |
| 'd': 0x7c, // f64 |
| 'e': 0x6f, // externref |
| }`, |
|
|
| $generateTypePack__internal: true, |
| $generateTypePack__deps: ['$uleb128EncodeWithLen', '$wasmTypeCodes'], |
| $generateTypePack: (types) => uleb128EncodeWithLen(Array.from(types, (type) => { |
| var code = wasmTypeCodes[type]; |
| #if ASSERTIONS |
| assert(code, `invalid signature char: ${type}`); |
| #endif |
| return code; |
| })), |
|
|
| #if !WASM2JS || WASM == 2 |
| |
| $convertJsFunctionToWasm__deps: [ |
| '$uleb128EncodeWithLen', |
| '$generateTypePack' |
| ], |
| $convertJsFunctionToWasm: (func, sig) => { |
| #if ASSERTIONS && !WASM_BIGINT |
| assert(!sig.includes('j'), 'i64 not permitted in function signatures when WASM_BIGINT is disabled'); |
| #endif |
| |
| |
| var bytes = Uint8Array.of( |
| 0x00, 0x61, 0x73, 0x6d, |
| 0x01, 0x00, 0x00, 0x00, |
| 0x01, |
| |
| |
| ...uleb128EncodeWithLen([ |
| 0x01, |
| 0x60 , |
| |
| ...generateTypePack(sig.slice(1)), |
| |
| ...generateTypePack(sig[0] === 'v' ? '' : sig[0]) |
| ]), |
| |
| 0x02, 0x07, |
| |
| 0x01, 0x01, 0x65, 0x01, 0x66, 0x00, 0x00, |
| 0x07, 0x05, |
| |
| 0x01, 0x01, 0x66, 0x00, 0x00, |
| ); |
|
|
| |
| |
| var module = new WebAssembly.Module(bytes); |
| var instance = new WebAssembly.Instance(module, { 'e': { 'f': func } }); |
| var wrappedFunc = instance.exports['f']; |
| return wrappedFunc; |
| }, |
| #endif |
|
|
| $freeTableIndexes: [], |
|
|
| |
| $functionsInTableMap: undefined, |
|
|
| $getEmptyTableSlot__deps: ['$freeTableIndexes', '$wasmTable'], |
| $getEmptyTableSlot: () => { |
| |
| if (freeTableIndexes.length) { |
| return freeTableIndexes.pop(); |
| } |
| #if ASSERTIONS |
| try { |
| #endif |
| |
| return wasmTable['grow']({{{ toIndexType('1') }}}); |
| #if ASSERTIONS |
| } catch (err) { |
| if (!(err instanceof RangeError)) { |
| throw err; |
| } |
| abort('Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.'); |
| } |
| #endif |
| }, |
|
|
| $updateTableMap__deps: ['$getWasmTableEntry'], |
| $updateTableMap: (offset, count) => { |
| if (functionsInTableMap) { |
| for (var i = offset; i < offset + count; i++) { |
| var item = getWasmTableEntry(i); |
| |
| if (item) { |
| functionsInTableMap.set(item, i); |
| } |
| } |
| } |
| }, |
|
|
| $getFunctionAddress__deps: ['$updateTableMap', '$functionsInTableMap', '$wasmTable'], |
| $getFunctionAddress: (func) => { |
| |
| if (!functionsInTableMap) { |
| functionsInTableMap = new WeakMap(); |
| updateTableMap(0, {{{ from64Expr('wasmTable.length') }}}); |
| } |
| return functionsInTableMap.get(func) || 0; |
| }, |
|
|
| |
| |
| |
| |
| $addFunction__docs: '/** @param {string=} sig */', |
| $addFunction__deps: ['$getFunctionAddress', |
| '$functionsInTableMap', '$getEmptyTableSlot', |
| '$setWasmTableEntry', |
| #if !WASM2JS || WASM == 2 |
| '$convertJsFunctionToWasm', |
| #endif |
| #if ASSERTIONS >= 2 |
| '$getWasmTableEntry', '$wasmTable', |
| #endif |
| ], |
|
|
| $addFunction: (func, sig) => { |
| #if ASSERTIONS |
| assert(typeof func != 'undefined'); |
| #endif |
| |
| |
| var rtn = getFunctionAddress(func); |
| if (rtn) { |
| return rtn; |
| } |
|
|
| |
|
|
| #if ASSERTIONS >= 2 |
| |
| |
| |
| for (var i = 0; i < wasmTable.length; i++) { |
| assert(getWasmTableEntry(i) != func, 'function in Table but not functionsInTableMap'); |
| } |
| #endif |
|
|
| var ret = getEmptyTableSlot(); |
|
|
| #if WASM2JS && WASM != 2 |
| setWasmTableEntry(ret, func); |
| #else |
| |
| try { |
| |
| setWasmTableEntry(ret, func); |
| } catch (err) { |
| if (!(err instanceof TypeError)) { |
| throw err; |
| } |
| #if ASSERTIONS |
| assert(typeof sig != 'undefined', 'Missing signature argument to addFunction: ' + func); |
| #endif |
| var wrapped = convertJsFunctionToWasm(func, sig); |
| setWasmTableEntry(ret, wrapped); |
| } |
| #endif |
|
|
| functionsInTableMap.set(func, ret); |
|
|
| return ret; |
| }, |
|
|
| $removeFunction__deps: ['$functionsInTableMap', '$freeTableIndexes', |
| '$getWasmTableEntry', '$setWasmTableEntry'], |
| $removeFunction: (index) => { |
| functionsInTableMap.delete(getWasmTableEntry(index)); |
| setWasmTableEntry(index, null); |
| freeTableIndexes.push(index); |
| }, |
| }); |
|
|