|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | + |
| 6 | +export async function loadDriverWasm(bytes) { |
| 7 | + const wasmModule = await WebAssembly.instantiate(bytes, { env: {} }); |
| 8 | + const exports = wasmModule.instance.exports; |
| 9 | + const memory = exports.memory; |
| 10 | + |
| 11 | + const textEncoder = new TextEncoder(); |
| 12 | + const textDecoder = new TextDecoder(); |
| 13 | + |
| 14 | + // -- Helpers -- |
| 15 | + function allocAndWriteString(str) { |
| 16 | + const bytes = textEncoder.encode(str); |
| 17 | + const ptr = exports.alloc(bytes.length); |
| 18 | + new Uint8Array(memory.buffer, ptr, bytes.length).set(bytes); |
| 19 | + return [ptr, bytes.length]; |
| 20 | + } |
| 21 | + |
| 22 | + function allocAndWriteBytes(bytes) { |
| 23 | + const ptr = exports.alloc(bytes.length); |
| 24 | + new Uint8Array(memory.buffer, ptr, bytes.length).set(bytes); |
| 25 | + return [ptr, bytes.length]; |
| 26 | + } |
| 27 | + |
| 28 | + function readBuffer(ptr, bufSize) { |
| 29 | + const view = new DataView(memory.buffer, ptr, 4); |
| 30 | + const actualSize = view.getUint32(0, true); |
| 31 | + |
| 32 | + if (actualSize > bufSize - 4) { |
| 33 | + exports.dealloc(ptr, bufSize); |
| 34 | + throw new Error(`Invalid output size: ${actualSize}`); |
| 35 | + } |
| 36 | + |
| 37 | + const data = new Uint8Array(memory.buffer, ptr + 4, actualSize); |
| 38 | + const result = data.slice(); // clone to detach from WASM memory |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + function runWithOutputBuffer(fn) { |
| 43 | + const outSize = 64 * 1024; // 64 KB default buffer |
| 44 | + const outPtr = exports.alloc(outSize); |
| 45 | + try { |
| 46 | + const code = fn(outPtr, outSize); |
| 47 | + if (code !== 0) { |
| 48 | + const errMsg = _internalGetLastError(); |
| 49 | + throw new Error(`FFI call failed (${code}): ${errMsg}`); |
| 50 | + } |
| 51 | + return readBuffer(outPtr, outSize); |
| 52 | + } finally { |
| 53 | + exports.dealloc(outPtr, outSize); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + function _internalGetLastError() { |
| 58 | + const outSize = 1024; |
| 59 | + const outPtr = exports.alloc(outSize); |
| 60 | + try { |
| 61 | + exports.get_last_error(outPtr, outSize); |
| 62 | + const result = readBuffer(outPtr, outSize); |
| 63 | + return textDecoder.decode(result); |
| 64 | + } finally { |
| 65 | + exports.dealloc(outPtr, outSize); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return { |
| 70 | + /** Initializes the contract driver */ |
| 71 | + init: () => exports.init(), |
| 72 | + |
| 73 | + /** Encodes JSON input into RKYV bytes */ |
| 74 | + encodeInputFn: (fnName, json) => { |
| 75 | + const [fnPtr, fnLen] = allocAndWriteString(fnName); |
| 76 | + const [jsonPtr, jsonLen] = allocAndWriteString(json); |
| 77 | + |
| 78 | + const result = runWithOutputBuffer((outPtr, outSize) => |
| 79 | + exports.encode_input_fn(fnPtr, fnLen, jsonPtr, jsonLen, outPtr, outSize) |
| 80 | + ); |
| 81 | + |
| 82 | + exports.dealloc(fnPtr, fnLen); |
| 83 | + exports.dealloc(jsonPtr, jsonLen); |
| 84 | + |
| 85 | + return result; // returns Uint8Array |
| 86 | + }, |
| 87 | + |
| 88 | + /** Decodes RKYV input bytes into JSON */ |
| 89 | + decodeInputFn: (fnName, rkyvBytes) => { |
| 90 | + const [fnPtr, fnLen] = allocAndWriteString(fnName); |
| 91 | + const [rkyvPtr, rkyvLen] = allocAndWriteBytes(rkyvBytes); |
| 92 | + |
| 93 | + const result = runWithOutputBuffer((outPtr, outSize) => |
| 94 | + exports.decode_input_fn(fnPtr, fnLen, rkyvPtr, rkyvLen, outPtr, outSize) |
| 95 | + ); |
| 96 | + |
| 97 | + exports.dealloc(fnPtr, fnLen); |
| 98 | + exports.dealloc(rkyvPtr, rkyvLen); |
| 99 | + |
| 100 | + return JSON.parse(textDecoder.decode(result)); |
| 101 | + }, |
| 102 | + |
| 103 | + /** Decodes RKYV output bytes into JSON */ |
| 104 | + decodeOutputFn: (fnName, rkyvBytes) => { |
| 105 | + const [fnPtr, fnLen] = allocAndWriteString(fnName); |
| 106 | + const [rkyvPtr, rkyvLen] = allocAndWriteBytes(rkyvBytes); |
| 107 | + |
| 108 | + const result = runWithOutputBuffer((outPtr, outSize) => |
| 109 | + exports.decode_output_fn(fnPtr, fnLen, rkyvPtr, rkyvLen, outPtr, outSize) |
| 110 | + ); |
| 111 | + |
| 112 | + exports.dealloc(fnPtr, fnLen); |
| 113 | + exports.dealloc(rkyvPtr, rkyvLen); |
| 114 | + |
| 115 | + return JSON.parse(textDecoder.decode(result)); |
| 116 | + }, |
| 117 | + |
| 118 | + /** Decodes RKYV event bytes into JSON */ |
| 119 | + decodeEvent: (eventName, rkyvBytes) => { |
| 120 | + const [eventPtr, eventLen] = allocAndWriteString(eventName); |
| 121 | + const [rkyvPtr, rkyvLen] = allocAndWriteBytes(rkyvBytes); |
| 122 | + |
| 123 | + const result = runWithOutputBuffer((outPtr, outSize) => |
| 124 | + exports.decode_event(eventPtr, eventLen, rkyvPtr, rkyvLen, outPtr, outSize) |
| 125 | + ); |
| 126 | + |
| 127 | + exports.dealloc(eventPtr, eventLen); |
| 128 | + exports.dealloc(rkyvPtr, rkyvLen); |
| 129 | + |
| 130 | + return JSON.parse(textDecoder.decode(result)); |
| 131 | + }, |
| 132 | + |
| 133 | + /** Returns the contract's JSON schema */ |
| 134 | + getSchema: () => { |
| 135 | + const result = runWithOutputBuffer((outPtr, outSize) => |
| 136 | + exports.get_schema(outPtr, outSize) |
| 137 | + ); |
| 138 | + return JSON.parse(textDecoder.decode(result)); |
| 139 | + }, |
| 140 | + |
| 141 | + /** Returns the contract's JSON schema */ |
| 142 | + getVersion: () => { |
| 143 | + const result = runWithOutputBuffer((outPtr, outSize) => |
| 144 | + exports.get_version(outPtr, outSize) |
| 145 | + ); |
| 146 | + return textDecoder.decode(result); |
| 147 | + }, |
| 148 | + }; |
| 149 | +} |
0 commit comments