@danielsimonjr/mathts-wasm 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -40
- package/build/mathts-debug.wasm +0 -0
- package/build/mathts-debug.wasm.map +1 -1
- package/build/mathts-debug.wat +823 -2061
- package/build/mathts.wasm +0 -0
- package/build/mathts.wat +757 -1633
- package/build/wasm-manifest.json +2 -2
- package/package.json +4 -6
- package/build/bindings/index.d.ts +0 -9
- package/build/bindings/index.d.ts.map +0 -1
- package/build/bindings/index.js +0 -8
- package/build/bindings/index.js.map +0 -1
- package/build/bindings/wasm-loader.d.ts +0 -121
- package/build/bindings/wasm-loader.d.ts.map +0 -1
- package/build/bindings/wasm-loader.js +0 -259
- package/build/bindings/wasm-loader.js.map +0 -1
- package/src/bindings/index.ts +0 -9
- package/src/bindings/wasm-loader.ts +0 -382
|
@@ -1,382 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WASM Module Loader
|
|
3
|
-
*
|
|
4
|
-
* TypeScript bindings for loading and using the MathTS WASM module.
|
|
5
|
-
* This provides a clean interface for JavaScript/TypeScript consumers.
|
|
6
|
-
*
|
|
7
|
-
* Security: optional SHA-384 integrity check via a sibling
|
|
8
|
-
* `wasm-manifest.json`. See verifyWasm() / loadManifest() below — kept
|
|
9
|
-
* inline rather than imported because the assembly package ships
|
|
10
|
-
* standalone (no functions/ dependency).
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
interface WasmManifest {
|
|
14
|
-
[fileName: string]: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
async function sha384Base64(buffer: ArrayBuffer | Uint8Array): Promise<string> {
|
|
18
|
-
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
19
|
-
const isNode = typeof process !== 'undefined' && (process as { versions?: { node?: string } }).versions?.node !== undefined;
|
|
20
|
-
let digest: Uint8Array;
|
|
21
|
-
if (isNode) {
|
|
22
|
-
const { createHash } = await import('crypto');
|
|
23
|
-
const h = createHash('sha384');
|
|
24
|
-
h.update(bytes);
|
|
25
|
-
digest = new Uint8Array(h.digest());
|
|
26
|
-
} else {
|
|
27
|
-
// Copy into a fresh ArrayBuffer so SubtleCrypto.digest accepts the
|
|
28
|
-
// bytes regardless of the source backing (ArrayBuffer or SharedArrayBuffer).
|
|
29
|
-
const copy = new ArrayBuffer(bytes.byteLength);
|
|
30
|
-
new Uint8Array(copy).set(bytes);
|
|
31
|
-
const out = await crypto.subtle.digest('SHA-384', copy);
|
|
32
|
-
digest = new Uint8Array(out);
|
|
33
|
-
}
|
|
34
|
-
let bin = '';
|
|
35
|
-
for (let i = 0; i < digest.length; i++) bin += String.fromCharCode(digest[i]);
|
|
36
|
-
const b64 =
|
|
37
|
-
typeof btoa === 'function'
|
|
38
|
-
? btoa(bin)
|
|
39
|
-
: (globalThis as { Buffer: { from(data: Uint8Array): { toString(encoding: string): string } } }).Buffer.from(digest).toString('base64');
|
|
40
|
-
return `sha384-${b64}`;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async function loadManifest(wasmPath: string): Promise<WasmManifest | null> {
|
|
44
|
-
const manifestPath = wasmPath.replace(/[^/\\]+$/, 'wasm-manifest.json');
|
|
45
|
-
const isNode = typeof process !== 'undefined' && (process as { versions?: { node?: string } }).versions?.node !== undefined;
|
|
46
|
-
try {
|
|
47
|
-
if (isNode) {
|
|
48
|
-
const fs = await import('fs');
|
|
49
|
-
const text = await fs.promises.readFile(manifestPath, 'utf8');
|
|
50
|
-
return JSON.parse(text) as WasmManifest;
|
|
51
|
-
} else {
|
|
52
|
-
const res = await fetch(manifestPath);
|
|
53
|
-
if (!res.ok) return null;
|
|
54
|
-
return (await res.json()) as WasmManifest;
|
|
55
|
-
}
|
|
56
|
-
} catch {
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async function verifyWasm(buffer: ArrayBuffer | Uint8Array, wasmPath: string): Promise<void> {
|
|
62
|
-
const manifest = await loadManifest(wasmPath);
|
|
63
|
-
if (!manifest) {
|
|
64
|
-
if (typeof console !== 'undefined') {
|
|
65
|
-
console.warn(
|
|
66
|
-
`[wasm-integrity] no manifest found beside "${wasmPath}"; skipping SHA-384 verification`
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
const fileName = wasmPath.split(/[/\\]/).pop() || wasmPath;
|
|
72
|
-
const expected = manifest[fileName];
|
|
73
|
-
if (!expected) return;
|
|
74
|
-
const actual = await sha384Base64(buffer);
|
|
75
|
-
if (actual !== expected) {
|
|
76
|
-
throw new Error(
|
|
77
|
-
`WASM integrity check failed for "${fileName}": expected ${expected}, got ${actual}`
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Type definitions for the WASM module exports
|
|
83
|
-
export interface MathTSWasmExports {
|
|
84
|
-
// Memory
|
|
85
|
-
memory: WebAssembly.Memory;
|
|
86
|
-
|
|
87
|
-
// Scalar operations
|
|
88
|
-
add_f64(a: number, b: number): number;
|
|
89
|
-
sub_f64(a: number, b: number): number;
|
|
90
|
-
mul_f64(a: number, b: number): number;
|
|
91
|
-
div_f64(a: number, b: number): number;
|
|
92
|
-
sqrt_f64(a: number): number;
|
|
93
|
-
pow_f64(a: number, b: number): number;
|
|
94
|
-
exp_f64(a: number): number;
|
|
95
|
-
log_f64(a: number): number;
|
|
96
|
-
sin_f64(a: number): number;
|
|
97
|
-
cos_f64(a: number): number;
|
|
98
|
-
tan_f64(a: number): number;
|
|
99
|
-
abs_f64(a: number): number;
|
|
100
|
-
|
|
101
|
-
// Array operations (pointers)
|
|
102
|
-
array_sum(dataPtr: number): number;
|
|
103
|
-
array_dot(aPtr: number, bPtr: number): number;
|
|
104
|
-
array_norm(dataPtr: number): number;
|
|
105
|
-
|
|
106
|
-
// Matrix operations (pointers)
|
|
107
|
-
matrix_multiply(
|
|
108
|
-
aPtr: number,
|
|
109
|
-
aRows: number,
|
|
110
|
-
aCols: number,
|
|
111
|
-
bPtr: number,
|
|
112
|
-
bCols: number,
|
|
113
|
-
resultPtr: number
|
|
114
|
-
): void;
|
|
115
|
-
matrix_transpose(aPtr: number, rows: number, cols: number, resultPtr: number): void;
|
|
116
|
-
matrix_trace(dataPtr: number, rows: number, cols: number): number;
|
|
117
|
-
matrix_norm_frobenius(dataPtr: number): number;
|
|
118
|
-
|
|
119
|
-
// Dense matrix decompositions — AS exports so the AS backend can stop
|
|
120
|
-
// falling back to JS for these ops. AS calling convention: pass
|
|
121
|
-
// typed-array *header* references (the AS runtime carries length, no
|
|
122
|
-
// ptr+len args). See `assembly/src/algebra/decomposition.ts` for the kernels.
|
|
123
|
-
matrix_lu_decompose?(
|
|
124
|
-
a: Float64Array,
|
|
125
|
-
n: number,
|
|
126
|
-
l_out: Float64Array,
|
|
127
|
-
u_out: Float64Array,
|
|
128
|
-
perm_out: Int32Array
|
|
129
|
-
): number;
|
|
130
|
-
matrix_qr_decompose?(
|
|
131
|
-
a: Float64Array,
|
|
132
|
-
m: number,
|
|
133
|
-
n: number,
|
|
134
|
-
q_out: Float64Array,
|
|
135
|
-
r_out: Float64Array,
|
|
136
|
-
v_work?: Float64Array | null
|
|
137
|
-
): number;
|
|
138
|
-
matrix_cholesky?(a: Float64Array, n: number, l_out: Float64Array): number;
|
|
139
|
-
matrix_inverse?(a: Float64Array, n: number, result: Float64Array, work: Float64Array): number;
|
|
140
|
-
matrix_determinant?(a: Float64Array, n: number, work: Float64Array): number;
|
|
141
|
-
|
|
142
|
-
// Bitwise operations (Int32Array, elementwise) — AS-backend kernels.
|
|
143
|
-
// A legacy backend shipped an equivalent set under the `*Array` /
|
|
144
|
-
// `*ArrayPerElement` naming (still declared, now vestigial, in
|
|
145
|
-
// functions/src/wasm/WasmLoader.ts's interface); the AS kernels keep the
|
|
146
|
-
// `_i32_array` suffix to match AS export-name discoverability.
|
|
147
|
-
bitAnd_i32_array?(a: Int32Array, b: Int32Array, result: Int32Array): void;
|
|
148
|
-
bitOr_i32_array?(a: Int32Array, b: Int32Array, result: Int32Array): void;
|
|
149
|
-
bitXor_i32_array?(a: Int32Array, b: Int32Array, result: Int32Array): void;
|
|
150
|
-
bitNot_i32_array?(a: Int32Array, result: Int32Array): void;
|
|
151
|
-
leftShift_i32_array?(a: Int32Array, b: Int32Array, result: Int32Array): void;
|
|
152
|
-
rightArithShift_i32_array?(a: Int32Array, b: Int32Array, result: Int32Array): void;
|
|
153
|
-
rightLogShift_i32_array?(a: Int32Array, b: Int32Array, result: Int32Array): void;
|
|
154
|
-
|
|
155
|
-
// Memory management
|
|
156
|
-
__new(size: number, id: number): number;
|
|
157
|
-
__pin(ptr: number): number;
|
|
158
|
-
__unpin(ptr: number): void;
|
|
159
|
-
__collect(): void;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export interface MathTSWasmInstance {
|
|
163
|
-
exports: MathTSWasmExports;
|
|
164
|
-
module: WebAssembly.Module;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Load the WASM module from a URL or buffer
|
|
169
|
-
*/
|
|
170
|
-
export async function loadWasm(source: string | BufferSource): Promise<MathTSWasmInstance> {
|
|
171
|
-
let wasmModule: WebAssembly.Module;
|
|
172
|
-
|
|
173
|
-
if (typeof source === 'string') {
|
|
174
|
-
// Load from URL
|
|
175
|
-
if (typeof fetch !== 'undefined') {
|
|
176
|
-
const response = await fetch(source);
|
|
177
|
-
const buffer = await response.arrayBuffer();
|
|
178
|
-
await verifyWasm(buffer, source);
|
|
179
|
-
wasmModule = await WebAssembly.compile(buffer);
|
|
180
|
-
} else {
|
|
181
|
-
// Node.js environment
|
|
182
|
-
const fs = await import('fs');
|
|
183
|
-
const path = await import('path');
|
|
184
|
-
const resolved = path.resolve(source);
|
|
185
|
-
const buffer = await fs.promises.readFile(resolved);
|
|
186
|
-
await verifyWasm(buffer, resolved);
|
|
187
|
-
wasmModule = await WebAssembly.compile(buffer);
|
|
188
|
-
}
|
|
189
|
-
} else {
|
|
190
|
-
// Load from buffer — caller is responsible for verifying integrity
|
|
191
|
-
// since we have no path to locate a sibling manifest.
|
|
192
|
-
wasmModule = await WebAssembly.compile(source);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const imports = createImports();
|
|
196
|
-
const instance = await WebAssembly.instantiate(wasmModule, imports);
|
|
197
|
-
|
|
198
|
-
return {
|
|
199
|
-
exports: instance.exports as unknown as MathTSWasmExports,
|
|
200
|
-
module: wasmModule,
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Synchronously load the WASM module (Node.js only)
|
|
206
|
-
*/
|
|
207
|
-
export function loadWasmSync(source: BufferSource): MathTSWasmInstance {
|
|
208
|
-
const wasmModule = new WebAssembly.Module(source);
|
|
209
|
-
const imports = createImports();
|
|
210
|
-
const instance = new WebAssembly.Instance(wasmModule, imports);
|
|
211
|
-
|
|
212
|
-
return {
|
|
213
|
-
exports: instance.exports as unknown as MathTSWasmExports,
|
|
214
|
-
module: wasmModule,
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Create the import object for WASM instantiation
|
|
220
|
-
*/
|
|
221
|
-
function createImports(): WebAssembly.Imports {
|
|
222
|
-
return {
|
|
223
|
-
env: {
|
|
224
|
-
// Abort handler
|
|
225
|
-
abort(messagePtr: number, fileNamePtr: number, line: number, column: number): void {
|
|
226
|
-
console.error(`WASM abort at ${line}:${column}`);
|
|
227
|
-
throw new Error('WASM execution aborted');
|
|
228
|
-
},
|
|
229
|
-
|
|
230
|
-
// Trace for debugging (optional)
|
|
231
|
-
trace(_messagePtr: number, _numArgs: number): void {
|
|
232
|
-
// Debug tracing - can be implemented if needed
|
|
233
|
-
},
|
|
234
|
-
|
|
235
|
-
// Seed for random numbers
|
|
236
|
-
seed(): number {
|
|
237
|
-
return Date.now() * Math.random();
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* High-level WASM wrapper with convenient array operations
|
|
245
|
-
*/
|
|
246
|
-
export class MathTSWasm {
|
|
247
|
-
private instance: MathTSWasmInstance;
|
|
248
|
-
private memory: WebAssembly.Memory;
|
|
249
|
-
private exports: MathTSWasmExports;
|
|
250
|
-
|
|
251
|
-
private constructor(instance: MathTSWasmInstance) {
|
|
252
|
-
this.instance = instance;
|
|
253
|
-
this.exports = instance.exports;
|
|
254
|
-
this.memory = instance.exports.memory;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Create a new MathTSWasm instance
|
|
259
|
-
*/
|
|
260
|
-
static async create(source: string | BufferSource): Promise<MathTSWasm> {
|
|
261
|
-
const instance = await loadWasm(source);
|
|
262
|
-
return new MathTSWasm(instance);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Get raw exports for advanced usage
|
|
267
|
-
*/
|
|
268
|
-
get raw(): MathTSWasmExports {
|
|
269
|
-
return this.exports;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// =========================================================================
|
|
273
|
-
// Scalar Operations
|
|
274
|
-
// =========================================================================
|
|
275
|
-
|
|
276
|
-
add(a: number, b: number): number {
|
|
277
|
-
return this.exports.add_f64(a, b);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
subtract(a: number, b: number): number {
|
|
281
|
-
return this.exports.sub_f64(a, b);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
multiply(a: number, b: number): number {
|
|
285
|
-
return this.exports.mul_f64(a, b);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
divide(a: number, b: number): number {
|
|
289
|
-
return this.exports.div_f64(a, b);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
sqrt(a: number): number {
|
|
293
|
-
return this.exports.sqrt_f64(a);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
pow(a: number, b: number): number {
|
|
297
|
-
return this.exports.pow_f64(a, b);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
exp(a: number): number {
|
|
301
|
-
return this.exports.exp_f64(a);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
log(a: number): number {
|
|
305
|
-
return this.exports.log_f64(a);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
sin(a: number): number {
|
|
309
|
-
return this.exports.sin_f64(a);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
cos(a: number): number {
|
|
313
|
-
return this.exports.cos_f64(a);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
tan(a: number): number {
|
|
317
|
-
return this.exports.tan_f64(a);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
abs(a: number): number {
|
|
321
|
-
return this.exports.abs_f64(a);
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// =========================================================================
|
|
325
|
-
// Memory Helpers
|
|
326
|
-
// =========================================================================
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Allocate a Float64Array in WASM memory
|
|
330
|
-
*/
|
|
331
|
-
allocFloat64Array(length: number): { ptr: number; array: Float64Array } {
|
|
332
|
-
const FLOAT64_ARRAY_ID = 3; // AssemblyScript array type ID
|
|
333
|
-
const byteLength = length * 8;
|
|
334
|
-
const ptr = this.exports.__new(byteLength + 16, FLOAT64_ARRAY_ID);
|
|
335
|
-
this.exports.__pin(ptr);
|
|
336
|
-
|
|
337
|
-
const view = new DataView(this.memory.buffer);
|
|
338
|
-
view.setInt32(ptr, ptr + 16, true); // data pointer
|
|
339
|
-
view.setInt32(ptr + 4, length, true); // length
|
|
340
|
-
|
|
341
|
-
const array = new Float64Array(this.memory.buffer, ptr + 16, length);
|
|
342
|
-
return { ptr, array };
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Free a previously allocated array
|
|
347
|
-
*/
|
|
348
|
-
freeArray(ptr: number): void {
|
|
349
|
-
this.exports.__unpin(ptr);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Copy a JavaScript array to WASM memory
|
|
354
|
-
*/
|
|
355
|
-
copyToWasm(data: ArrayLike<number>): { ptr: number; array: Float64Array } {
|
|
356
|
-
const { ptr, array } = this.allocFloat64Array(data.length);
|
|
357
|
-
for (let i = 0; i < data.length; i++) {
|
|
358
|
-
array[i] = data[i];
|
|
359
|
-
}
|
|
360
|
-
return { ptr, array };
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Copy from WASM memory to a new JavaScript array
|
|
365
|
-
*/
|
|
366
|
-
copyFromWasm(ptr: number, length: number): Float64Array {
|
|
367
|
-
const offset = ptr + 16; // Skip header
|
|
368
|
-
return new Float64Array(this.memory.buffer.slice(offset, offset + length * 8));
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Run garbage collection
|
|
373
|
-
*/
|
|
374
|
-
gc(): void {
|
|
375
|
-
this.exports.__collect();
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* Default export for convenience
|
|
381
|
-
*/
|
|
382
|
-
export default MathTSWasm;
|