@danielsimonjr/mathts-wasm 0.1.1

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/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@danielsimonjr/mathts-wasm",
3
+ "version": "0.1.1",
4
+ "description": "MathTS WebAssembly module - high-performance numerical computing",
5
+ "type": "module",
6
+ "main": "build/release.js",
7
+ "types": "build/release.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./build/release.js",
11
+ "types": "./build/release.d.ts"
12
+ },
13
+ "./wasm": {
14
+ "import": "./build/mathts.wasm"
15
+ },
16
+ "./debug": {
17
+ "import": "./build/mathts-debug.wasm"
18
+ }
19
+ },
20
+ "files": [
21
+ "build/",
22
+ "src/bindings/"
23
+ ],
24
+ "scripts": {
25
+ "asbuild:debug": "asc src/index.ts --target debug",
26
+ "asbuild:release": "asc src/index.ts --target release",
27
+ "asbuild": "npm run asbuild:debug && npm run asbuild:release",
28
+ "build:bindings": "tsc -p tsconfig.bindings.json",
29
+ "build": "npm run asbuild && npm run build:bindings",
30
+ "clean": "rm -rf build/",
31
+ "test": "node --experimental-wasm-simd tests/run.js",
32
+ "benchmark": "node --experimental-wasm-simd benchmarks/run.js"
33
+ },
34
+ "devDependencies": {
35
+ "assemblyscript": "^0.27.27",
36
+ "typescript": "^5.3.0"
37
+ },
38
+ "keywords": [
39
+ "math",
40
+ "wasm",
41
+ "webassembly",
42
+ "assemblyscript",
43
+ "numerical",
44
+ "matrix",
45
+ "linear-algebra",
46
+ "simd"
47
+ ],
48
+ "author": "MathTS Contributors",
49
+ "license": "MIT",
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "https://github.com/danielsimonjr/MathTS.git",
53
+ "directory": "assembly"
54
+ },
55
+ "bugs": {
56
+ "url": "https://github.com/danielsimonjr/MathTS/issues"
57
+ },
58
+ "publishConfig": {
59
+ "access": "public"
60
+ }
61
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * MathTS WASM Bindings
3
+ *
4
+ * High-level TypeScript interface for the MathTS WASM module.
5
+ * Handles memory management and provides a clean API.
6
+ */
7
+
8
+ export { MathTSWasm, loadWasm, loadWasmSync } from './wasm-loader.js';
9
+ export type { MathTSWasmExports, MathTSWasmInstance } from './wasm-loader.js';
@@ -0,0 +1,261 @@
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
+
8
+ // Type definitions for the WASM module exports
9
+ export interface MathTSWasmExports {
10
+ // Memory
11
+ memory: WebAssembly.Memory;
12
+
13
+ // Scalar operations
14
+ add_f64(a: number, b: number): number;
15
+ sub_f64(a: number, b: number): number;
16
+ mul_f64(a: number, b: number): number;
17
+ div_f64(a: number, b: number): number;
18
+ sqrt_f64(a: number): number;
19
+ pow_f64(a: number, b: number): number;
20
+ exp_f64(a: number): number;
21
+ log_f64(a: number): number;
22
+ sin_f64(a: number): number;
23
+ cos_f64(a: number): number;
24
+ tan_f64(a: number): number;
25
+ abs_f64(a: number): number;
26
+
27
+ // Array operations (pointers)
28
+ array_sum(dataPtr: number): number;
29
+ array_dot(aPtr: number, bPtr: number): number;
30
+ array_norm(dataPtr: number): number;
31
+
32
+ // Matrix operations (pointers)
33
+ matrix_multiply(aPtr: number, aRows: number, aCols: number, bPtr: number, bCols: number, resultPtr: number): void;
34
+ matrix_transpose(aPtr: number, rows: number, cols: number, resultPtr: number): void;
35
+ matrix_trace(dataPtr: number, rows: number, cols: number): number;
36
+ matrix_norm_frobenius(dataPtr: number): number;
37
+
38
+ // Memory management
39
+ __new(size: number, id: number): number;
40
+ __pin(ptr: number): number;
41
+ __unpin(ptr: number): void;
42
+ __collect(): void;
43
+ }
44
+
45
+ export interface MathTSWasmInstance {
46
+ exports: MathTSWasmExports;
47
+ module: WebAssembly.Module;
48
+ }
49
+
50
+ /**
51
+ * Load the WASM module from a URL or buffer
52
+ */
53
+ export async function loadWasm(source: string | BufferSource): Promise<MathTSWasmInstance> {
54
+ let wasmModule: WebAssembly.Module;
55
+
56
+ if (typeof source === 'string') {
57
+ // Load from URL
58
+ if (typeof fetch !== 'undefined') {
59
+ const response = await fetch(source);
60
+ const buffer = await response.arrayBuffer();
61
+ wasmModule = await WebAssembly.compile(buffer);
62
+ } else {
63
+ // Node.js environment
64
+ const fs = await import('fs');
65
+ const path = await import('path');
66
+ const buffer = fs.readFileSync(path.resolve(source));
67
+ wasmModule = await WebAssembly.compile(buffer);
68
+ }
69
+ } else {
70
+ // Load from buffer
71
+ wasmModule = await WebAssembly.compile(source);
72
+ }
73
+
74
+ const imports = createImports();
75
+ const instance = await WebAssembly.instantiate(wasmModule, imports);
76
+
77
+ return {
78
+ exports: instance.exports as unknown as MathTSWasmExports,
79
+ module: wasmModule,
80
+ };
81
+ }
82
+
83
+ /**
84
+ * Synchronously load the WASM module (Node.js only)
85
+ */
86
+ export function loadWasmSync(source: BufferSource): MathTSWasmInstance {
87
+ const wasmModule = new WebAssembly.Module(source);
88
+ const imports = createImports();
89
+ const instance = new WebAssembly.Instance(wasmModule, imports);
90
+
91
+ return {
92
+ exports: instance.exports as unknown as MathTSWasmExports,
93
+ module: wasmModule,
94
+ };
95
+ }
96
+
97
+ /**
98
+ * Create the import object for WASM instantiation
99
+ */
100
+ function createImports(): WebAssembly.Imports {
101
+ return {
102
+ env: {
103
+ // Abort handler
104
+ abort(messagePtr: number, fileNamePtr: number, line: number, column: number): void {
105
+ console.error(`WASM abort at ${line}:${column}`);
106
+ throw new Error('WASM execution aborted');
107
+ },
108
+
109
+ // Trace for debugging (optional)
110
+ trace(messagePtr: number, numArgs: number): void {
111
+ // Debug tracing - can be implemented if needed
112
+ },
113
+
114
+ // Seed for random numbers
115
+ seed(): number {
116
+ return Date.now() * Math.random();
117
+ },
118
+ },
119
+ };
120
+ }
121
+
122
+ /**
123
+ * High-level WASM wrapper with convenient array operations
124
+ */
125
+ export class MathTSWasm {
126
+ private instance: MathTSWasmInstance;
127
+ private memory: WebAssembly.Memory;
128
+ private exports: MathTSWasmExports;
129
+
130
+ private constructor(instance: MathTSWasmInstance) {
131
+ this.instance = instance;
132
+ this.exports = instance.exports;
133
+ this.memory = instance.exports.memory;
134
+ }
135
+
136
+ /**
137
+ * Create a new MathTSWasm instance
138
+ */
139
+ static async create(source: string | BufferSource): Promise<MathTSWasm> {
140
+ const instance = await loadWasm(source);
141
+ return new MathTSWasm(instance);
142
+ }
143
+
144
+ /**
145
+ * Get raw exports for advanced usage
146
+ */
147
+ get raw(): MathTSWasmExports {
148
+ return this.exports;
149
+ }
150
+
151
+ // =========================================================================
152
+ // Scalar Operations
153
+ // =========================================================================
154
+
155
+ add(a: number, b: number): number {
156
+ return this.exports.add_f64(a, b);
157
+ }
158
+
159
+ subtract(a: number, b: number): number {
160
+ return this.exports.sub_f64(a, b);
161
+ }
162
+
163
+ multiply(a: number, b: number): number {
164
+ return this.exports.mul_f64(a, b);
165
+ }
166
+
167
+ divide(a: number, b: number): number {
168
+ return this.exports.div_f64(a, b);
169
+ }
170
+
171
+ sqrt(a: number): number {
172
+ return this.exports.sqrt_f64(a);
173
+ }
174
+
175
+ pow(a: number, b: number): number {
176
+ return this.exports.pow_f64(a, b);
177
+ }
178
+
179
+ exp(a: number): number {
180
+ return this.exports.exp_f64(a);
181
+ }
182
+
183
+ log(a: number): number {
184
+ return this.exports.log_f64(a);
185
+ }
186
+
187
+ sin(a: number): number {
188
+ return this.exports.sin_f64(a);
189
+ }
190
+
191
+ cos(a: number): number {
192
+ return this.exports.cos_f64(a);
193
+ }
194
+
195
+ tan(a: number): number {
196
+ return this.exports.tan_f64(a);
197
+ }
198
+
199
+ abs(a: number): number {
200
+ return this.exports.abs_f64(a);
201
+ }
202
+
203
+ // =========================================================================
204
+ // Memory Helpers
205
+ // =========================================================================
206
+
207
+ /**
208
+ * Allocate a Float64Array in WASM memory
209
+ */
210
+ allocFloat64Array(length: number): { ptr: number; array: Float64Array } {
211
+ const FLOAT64_ARRAY_ID = 3; // AssemblyScript array type ID
212
+ const byteLength = length * 8;
213
+ const ptr = this.exports.__new(byteLength + 16, FLOAT64_ARRAY_ID);
214
+ this.exports.__pin(ptr);
215
+
216
+ const view = new DataView(this.memory.buffer);
217
+ view.setInt32(ptr, ptr + 16, true); // data pointer
218
+ view.setInt32(ptr + 4, length, true); // length
219
+
220
+ const array = new Float64Array(this.memory.buffer, ptr + 16, length);
221
+ return { ptr, array };
222
+ }
223
+
224
+ /**
225
+ * Free a previously allocated array
226
+ */
227
+ freeArray(ptr: number): void {
228
+ this.exports.__unpin(ptr);
229
+ }
230
+
231
+ /**
232
+ * Copy a JavaScript array to WASM memory
233
+ */
234
+ copyToWasm(data: ArrayLike<number>): { ptr: number; array: Float64Array } {
235
+ const { ptr, array } = this.allocFloat64Array(data.length);
236
+ for (let i = 0; i < data.length; i++) {
237
+ array[i] = data[i];
238
+ }
239
+ return { ptr, array };
240
+ }
241
+
242
+ /**
243
+ * Copy from WASM memory to a new JavaScript array
244
+ */
245
+ copyFromWasm(ptr: number, length: number): Float64Array {
246
+ const offset = ptr + 16; // Skip header
247
+ return new Float64Array(this.memory.buffer.slice(offset, offset + length * 8));
248
+ }
249
+
250
+ /**
251
+ * Run garbage collection
252
+ */
253
+ gc(): void {
254
+ this.exports.__collect();
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Default export for convenience
260
+ */
261
+ export default MathTSWasm;