|
| 1 | +async function instantiate(module, imports = {}) { |
| 2 | + const adaptedImports = { |
| 3 | + env: Object.assign(Object.create(globalThis), imports.env || {}, { |
| 4 | + abort(message, fileName, lineNumber, columnNumber) { |
| 5 | + // ~lib/builtins/abort(~lib/string/String | null?, ~lib/string/String | null?, u32?, u32?) => void |
| 6 | + message = __liftString(message >>> 0); |
| 7 | + fileName = __liftString(fileName >>> 0); |
| 8 | + lineNumber = lineNumber >>> 0; |
| 9 | + columnNumber = columnNumber >>> 0; |
| 10 | + (() => { |
| 11 | + // @external.js |
| 12 | + throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`); |
| 13 | + })(); |
| 14 | + }, |
| 15 | + }), |
| 16 | + }; |
| 17 | + const { exports } = await WebAssembly.instantiate(module, adaptedImports); |
| 18 | + const memory = exports.memory || imports.env.memory; |
| 19 | + const adaptedExports = Object.setPrototypeOf({ |
| 20 | + sum(arr) { |
| 21 | + // compute/sum/sum(~lib/typedarray/Int32Array) => i32 |
| 22 | + arr = __lowerTypedArray(Int32Array, 4, 2, arr) || __notnull(); |
| 23 | + return exports.sum(arr); |
| 24 | + }, |
| 25 | + sumFloat(arr) { |
| 26 | + // compute/sum/sumFloat(~lib/typedarray/Float64Array) => f64 |
| 27 | + arr = __lowerTypedArray(Float64Array, 5, 3, arr) || __notnull(); |
| 28 | + return exports.sumFloat(arr); |
| 29 | + }, |
| 30 | + average(arr) { |
| 31 | + // compute/sum/average(~lib/typedarray/Int32Array) => f64 |
| 32 | + arr = __lowerTypedArray(Int32Array, 4, 2, arr) || __notnull(); |
| 33 | + return exports.average(arr); |
| 34 | + }, |
| 35 | + fibonacciSequence(n) { |
| 36 | + // compute/fibonacci/fibonacciSequence(i32) => ~lib/typedarray/Int64Array |
| 37 | + return __liftTypedArray(BigInt64Array, exports.fibonacciSequence(n) >>> 0); |
| 38 | + }, |
| 39 | + isFibonacci(num) { |
| 40 | + // compute/fibonacci/isFibonacci(i64) => bool |
| 41 | + num = num || 0n; |
| 42 | + return exports.isFibonacci(num) != 0; |
| 43 | + }, |
| 44 | + mandelbrot(width, height, zoom, panX, panY, maxIter) { |
| 45 | + // compute/mandelbrot/mandelbrot(i32, i32, f64, f64, f64, i32) => ~lib/typedarray/Uint32Array |
| 46 | + return __liftTypedArray(Uint32Array, exports.mandelbrot(width, height, zoom, panX, panY, maxIter) >>> 0); |
| 47 | + }, |
| 48 | + julia(width, height, cRe, cIm, zoom, maxIter) { |
| 49 | + // compute/mandelbrot/julia(i32, i32, f64, f64, f64, i32) => ~lib/typedarray/Uint32Array |
| 50 | + return __liftTypedArray(Uint32Array, exports.julia(width, height, cRe, cIm, zoom, maxIter) >>> 0); |
| 51 | + }, |
| 52 | + matrixMultiply(a, b, aRows, aCols, bCols) { |
| 53 | + // compute/matrix/matrixMultiply(~lib/typedarray/Float64Array, ~lib/typedarray/Float64Array, i32, i32, i32) => ~lib/typedarray/Float64Array |
| 54 | + a = __retain(__lowerTypedArray(Float64Array, 5, 3, a) || __notnull()); |
| 55 | + b = __lowerTypedArray(Float64Array, 5, 3, b) || __notnull(); |
| 56 | + try { |
| 57 | + return __liftTypedArray(Float64Array, exports.matrixMultiply(a, b, aRows, aCols, bCols) >>> 0); |
| 58 | + } finally { |
| 59 | + __release(a); |
| 60 | + } |
| 61 | + }, |
| 62 | + matrixTranspose(matrix, rows, cols) { |
| 63 | + // compute/matrix/matrixTranspose(~lib/typedarray/Float64Array, i32, i32) => ~lib/typedarray/Float64Array |
| 64 | + matrix = __lowerTypedArray(Float64Array, 5, 3, matrix) || __notnull(); |
| 65 | + return __liftTypedArray(Float64Array, exports.matrixTranspose(matrix, rows, cols) >>> 0); |
| 66 | + }, |
| 67 | + matrixAdd(a, b) { |
| 68 | + // compute/matrix/matrixAdd(~lib/typedarray/Float64Array, ~lib/typedarray/Float64Array) => ~lib/typedarray/Float64Array |
| 69 | + a = __retain(__lowerTypedArray(Float64Array, 5, 3, a) || __notnull()); |
| 70 | + b = __lowerTypedArray(Float64Array, 5, 3, b) || __notnull(); |
| 71 | + try { |
| 72 | + return __liftTypedArray(Float64Array, exports.matrixAdd(a, b) >>> 0); |
| 73 | + } finally { |
| 74 | + __release(a); |
| 75 | + } |
| 76 | + }, |
| 77 | + matrixScale(matrix, scalar) { |
| 78 | + // compute/matrix/matrixScale(~lib/typedarray/Float64Array, f64) => ~lib/typedarray/Float64Array |
| 79 | + matrix = __lowerTypedArray(Float64Array, 5, 3, matrix) || __notnull(); |
| 80 | + return __liftTypedArray(Float64Array, exports.matrixScale(matrix, scalar) >>> 0); |
| 81 | + }, |
| 82 | + dotProduct(a, b) { |
| 83 | + // compute/matrix/dotProduct(~lib/typedarray/Float64Array, ~lib/typedarray/Float64Array) => f64 |
| 84 | + a = __retain(__lowerTypedArray(Float64Array, 5, 3, a) || __notnull()); |
| 85 | + b = __lowerTypedArray(Float64Array, 5, 3, b) || __notnull(); |
| 86 | + try { |
| 87 | + return exports.dotProduct(a, b); |
| 88 | + } finally { |
| 89 | + __release(a); |
| 90 | + } |
| 91 | + }, |
| 92 | + vectorMagnitude(v) { |
| 93 | + // compute/matrix/vectorMagnitude(~lib/typedarray/Float64Array) => f64 |
| 94 | + v = __lowerTypedArray(Float64Array, 5, 3, v) || __notnull(); |
| 95 | + return exports.vectorMagnitude(v); |
| 96 | + }, |
| 97 | + vectorNormalize(v) { |
| 98 | + // compute/matrix/vectorNormalize(~lib/typedarray/Float64Array) => ~lib/typedarray/Float64Array |
| 99 | + v = __lowerTypedArray(Float64Array, 5, 3, v) || __notnull(); |
| 100 | + return __liftTypedArray(Float64Array, exports.vectorNormalize(v) >>> 0); |
| 101 | + }, |
| 102 | + getBufferPtr() { |
| 103 | + // compute/blur/getBufferPtr() => usize |
| 104 | + return exports.getBufferPtr() >>> 0; |
| 105 | + }, |
| 106 | + }, exports); |
| 107 | + function __liftString(pointer) { |
| 108 | + if (!pointer) return null; |
| 109 | + const |
| 110 | + end = pointer + new Uint32Array(memory.buffer)[pointer - 4 >>> 2] >>> 1, |
| 111 | + memoryU16 = new Uint16Array(memory.buffer); |
| 112 | + let |
| 113 | + start = pointer >>> 1, |
| 114 | + string = ""; |
| 115 | + while (end - start > 1024) string += String.fromCharCode(...memoryU16.subarray(start, start += 1024)); |
| 116 | + return string + String.fromCharCode(...memoryU16.subarray(start, end)); |
| 117 | + } |
| 118 | + function __liftTypedArray(constructor, pointer) { |
| 119 | + if (!pointer) return null; |
| 120 | + return new constructor( |
| 121 | + memory.buffer, |
| 122 | + __getU32(pointer + 4), |
| 123 | + __dataview.getUint32(pointer + 8, true) / constructor.BYTES_PER_ELEMENT |
| 124 | + ).slice(); |
| 125 | + } |
| 126 | + function __lowerTypedArray(constructor, id, align, values) { |
| 127 | + if (values == null) return 0; |
| 128 | + const |
| 129 | + length = values.length, |
| 130 | + buffer = exports.__pin(exports.__new(length << align, 1)) >>> 0, |
| 131 | + header = exports.__new(12, id) >>> 0; |
| 132 | + __setU32(header + 0, buffer); |
| 133 | + __dataview.setUint32(header + 4, buffer, true); |
| 134 | + __dataview.setUint32(header + 8, length << align, true); |
| 135 | + new constructor(memory.buffer, buffer, length).set(values); |
| 136 | + exports.__unpin(buffer); |
| 137 | + return header; |
| 138 | + } |
| 139 | + const refcounts = new Map(); |
| 140 | + function __retain(pointer) { |
| 141 | + if (pointer) { |
| 142 | + const refcount = refcounts.get(pointer); |
| 143 | + if (refcount) refcounts.set(pointer, refcount + 1); |
| 144 | + else refcounts.set(exports.__pin(pointer), 1); |
| 145 | + } |
| 146 | + return pointer; |
| 147 | + } |
| 148 | + function __release(pointer) { |
| 149 | + if (pointer) { |
| 150 | + const refcount = refcounts.get(pointer); |
| 151 | + if (refcount === 1) exports.__unpin(pointer), refcounts.delete(pointer); |
| 152 | + else if (refcount) refcounts.set(pointer, refcount - 1); |
| 153 | + else throw Error(`invalid refcount '${refcount}' for reference '${pointer}'`); |
| 154 | + } |
| 155 | + } |
| 156 | + function __notnull() { |
| 157 | + throw TypeError("value must not be null"); |
| 158 | + } |
| 159 | + let __dataview = new DataView(memory.buffer); |
| 160 | + function __setU32(pointer, value) { |
| 161 | + try { |
| 162 | + __dataview.setUint32(pointer, value, true); |
| 163 | + } catch { |
| 164 | + __dataview = new DataView(memory.buffer); |
| 165 | + __dataview.setUint32(pointer, value, true); |
| 166 | + } |
| 167 | + } |
| 168 | + function __getU32(pointer) { |
| 169 | + try { |
| 170 | + return __dataview.getUint32(pointer, true); |
| 171 | + } catch { |
| 172 | + __dataview = new DataView(memory.buffer); |
| 173 | + return __dataview.getUint32(pointer, true); |
| 174 | + } |
| 175 | + } |
| 176 | + return adaptedExports; |
| 177 | +} |
| 178 | +export const { |
| 179 | + memory, |
| 180 | + sum, |
| 181 | + sumFloat, |
| 182 | + average, |
| 183 | + fibonacci, |
| 184 | + fibonacciSequence, |
| 185 | + isFibonacci, |
| 186 | + mandelbrot, |
| 187 | + julia, |
| 188 | + matrixMultiply, |
| 189 | + matrixTranspose, |
| 190 | + matrixAdd, |
| 191 | + matrixScale, |
| 192 | + dotProduct, |
| 193 | + vectorMagnitude, |
| 194 | + vectorNormalize, |
| 195 | + getBufferPtr, |
| 196 | + blurImage, |
| 197 | +} = await (async url => instantiate( |
| 198 | + await (async () => { |
| 199 | + const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); |
| 200 | + if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } |
| 201 | + else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } |
| 202 | + })(), { |
| 203 | + } |
| 204 | +))(new URL("compute.wasm", import.meta.url)); |
0 commit comments