@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/README.md +128 -0
- package/build/bindings/index.d.ts +9 -0
- package/build/bindings/index.d.ts.map +1 -0
- package/build/bindings/index.js +8 -0
- package/build/bindings/index.js.map +1 -0
- package/build/bindings/wasm-loader.d.ts +104 -0
- package/build/bindings/wasm-loader.d.ts.map +1 -0
- package/build/bindings/wasm-loader.js +187 -0
- package/build/bindings/wasm-loader.js.map +1 -0
- package/build/mathts-debug.d.ts +1371 -0
- package/build/mathts-debug.js +1484 -0
- package/build/mathts-debug.wasm +0 -0
- package/build/mathts-debug.wasm.map +1 -0
- package/build/mathts-debug.wat +13058 -0
- package/build/mathts.d.ts +1371 -0
- package/build/mathts.js +1484 -0
- package/build/mathts.wasm +0 -0
- package/build/mathts.wat +9643 -0
- package/package.json +61 -0
- package/src/bindings/index.ts +9 -0
- package/src/bindings/wasm-loader.ts +261 -0
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# @danielsimonjr/mathts-wasm
|
|
2
|
+
|
|
3
|
+
High-performance WebAssembly module for MathTS, built with AssemblyScript.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **SIMD Acceleration**: Utilizes WebAssembly SIMD for vectorized operations
|
|
8
|
+
- **Zero-Copy Operations**: Works directly on typed arrays for minimal overhead
|
|
9
|
+
- **Comprehensive Math**: Scalar, array, matrix, and complex number operations
|
|
10
|
+
- **Cross-Platform**: Works in browsers, Node.js, Deno, and edge runtimes
|
|
11
|
+
|
|
12
|
+
## Building
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# Install dependencies
|
|
16
|
+
npm install
|
|
17
|
+
|
|
18
|
+
# Build debug WASM (with debugging info)
|
|
19
|
+
npm run asbuild:debug
|
|
20
|
+
|
|
21
|
+
# Build release WASM (optimized)
|
|
22
|
+
npm run asbuild:release
|
|
23
|
+
|
|
24
|
+
# Build both
|
|
25
|
+
npm run asbuild
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Browser
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { MathTSWasm } from '@danielsimonjr/mathts-wasm';
|
|
34
|
+
|
|
35
|
+
const wasm = await MathTSWasm.create('/path/to/mathts.wasm');
|
|
36
|
+
|
|
37
|
+
// Scalar operations
|
|
38
|
+
const sum = wasm.add(2, 3); // 5
|
|
39
|
+
const sqrt = wasm.sqrt(16); // 4
|
|
40
|
+
const sin = wasm.sin(Math.PI/2); // 1
|
|
41
|
+
|
|
42
|
+
// For array/matrix operations, use the raw exports
|
|
43
|
+
const exports = wasm.raw;
|
|
44
|
+
// ... work with typed arrays and pointers
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Node.js
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { loadWasmSync } from '@danielsimonjr/mathts-wasm';
|
|
51
|
+
import fs from 'fs';
|
|
52
|
+
|
|
53
|
+
const buffer = fs.readFileSync('./build/mathts.wasm');
|
|
54
|
+
const { exports } = loadWasmSync(buffer);
|
|
55
|
+
|
|
56
|
+
console.log(exports.add_f64(2, 3)); // 5
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Module Structure
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
assembly/
|
|
63
|
+
├── src/
|
|
64
|
+
│ ├── index.ts # Main entry point
|
|
65
|
+
│ ├── types/
|
|
66
|
+
│ │ └── complex.ts # Complex number type
|
|
67
|
+
│ ├── ops/
|
|
68
|
+
│ │ ├── scalar.ts # Scalar operations
|
|
69
|
+
│ │ ├── array.ts # Array operations
|
|
70
|
+
│ │ ├── matrix.ts # Matrix operations
|
|
71
|
+
│ │ ├── complex-ops.ts # Complex operations
|
|
72
|
+
│ │ └── complex-array.ts # Complex array operations
|
|
73
|
+
│ ├── env/
|
|
74
|
+
│ │ └── abort.ts # Error handling
|
|
75
|
+
│ └── bindings/
|
|
76
|
+
│ └── wasm-loader.ts # JS/TS bindings
|
|
77
|
+
├── build/ # WASM output
|
|
78
|
+
├── tests/
|
|
79
|
+
│ └── run.js # Test runner
|
|
80
|
+
├── asconfig.json # AssemblyScript config
|
|
81
|
+
└── package.json
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Available Operations
|
|
85
|
+
|
|
86
|
+
### Scalar Operations
|
|
87
|
+
- Basic: `add_f64`, `sub_f64`, `mul_f64`, `div_f64`, `mod_f64`, `neg_f64`
|
|
88
|
+
- Power: `sqrt_f64`, `pow_f64`, `square_f64`, `cube_f64`, `cbrt_f64`
|
|
89
|
+
- Exp/Log: `exp_f64`, `log_f64`, `log10_f64`, `log2_f64`
|
|
90
|
+
- Trig: `sin_f64`, `cos_f64`, `tan_f64`, `asin_f64`, `acos_f64`, `atan_f64`
|
|
91
|
+
- Hyperbolic: `sinh_f64`, `cosh_f64`, `tanh_f64`, `asinh_f64`, `acosh_f64`, `atanh_f64`
|
|
92
|
+
- Rounding: `abs_f64`, `floor_f64`, `ceil_f64`, `round_f64`, `trunc_f64`
|
|
93
|
+
|
|
94
|
+
### Array Operations
|
|
95
|
+
- Reductions: `array_sum`, `array_product`, `array_mean`, `array_variance`, `array_min`, `array_max`
|
|
96
|
+
- Norms: `array_norm`, `array_norm_l1`, `array_norm_linf`
|
|
97
|
+
- Element-wise: `array_add`, `array_sub`, `array_mul`, `array_div`, `array_scale`
|
|
98
|
+
- BLAS-like: `array_dot`, `array_axpby`, `array_distance`
|
|
99
|
+
|
|
100
|
+
### Matrix Operations
|
|
101
|
+
- Creation: `matrix_zeros`, `matrix_ones`, `matrix_identity`, `matrix_diag`
|
|
102
|
+
- Arithmetic: `matrix_add`, `matrix_sub`, `matrix_scale`, `matrix_multiply`
|
|
103
|
+
- Properties: `matrix_trace`, `matrix_norm_frobenius`, `matrix_transpose`
|
|
104
|
+
- BLAS: `matrix_gemm`, `matrix_gemv`, `matrix_axpy`
|
|
105
|
+
|
|
106
|
+
### Complex Operations
|
|
107
|
+
- Arithmetic: `complex_add`, `complex_sub`, `complex_mul`, `complex_div`
|
|
108
|
+
- Functions: `complex_exp`, `complex_log`, `complex_sqrt`, `complex_pow`
|
|
109
|
+
- Trig: `complex_sin`, `complex_cos`, `complex_tan`, `complex_asin`, `complex_acos`
|
|
110
|
+
- Properties: `complex_abs`, `complex_arg`, `complex_conj`
|
|
111
|
+
|
|
112
|
+
## Performance Notes
|
|
113
|
+
|
|
114
|
+
1. **SIMD**: Enabled by default for vector operations
|
|
115
|
+
2. **Memory Layout**: Matrices use row-major order
|
|
116
|
+
3. **Complex Arrays**: Use interleaved storage `[re0, im0, re1, im1, ...]`
|
|
117
|
+
|
|
118
|
+
## Testing
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
# Run tests (requires build first)
|
|
122
|
+
npm run asbuild:debug
|
|
123
|
+
npm test
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT
|
|
@@ -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
|
+
export { MathTSWasm, loadWasm, loadWasmSync } from './wasm-loader.js';
|
|
8
|
+
export type { MathTSWasmExports, MathTSWasmInstance } from './wasm-loader.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bindings/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACtE,YAAY,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
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
|
+
export { MathTSWasm, loadWasm, loadWasmSync } from './wasm-loader.js';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bindings/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
export interface MathTSWasmExports {
|
|
8
|
+
memory: WebAssembly.Memory;
|
|
9
|
+
add_f64(a: number, b: number): number;
|
|
10
|
+
sub_f64(a: number, b: number): number;
|
|
11
|
+
mul_f64(a: number, b: number): number;
|
|
12
|
+
div_f64(a: number, b: number): number;
|
|
13
|
+
sqrt_f64(a: number): number;
|
|
14
|
+
pow_f64(a: number, b: number): number;
|
|
15
|
+
exp_f64(a: number): number;
|
|
16
|
+
log_f64(a: number): number;
|
|
17
|
+
sin_f64(a: number): number;
|
|
18
|
+
cos_f64(a: number): number;
|
|
19
|
+
tan_f64(a: number): number;
|
|
20
|
+
abs_f64(a: number): number;
|
|
21
|
+
array_sum(dataPtr: number): number;
|
|
22
|
+
array_dot(aPtr: number, bPtr: number): number;
|
|
23
|
+
array_norm(dataPtr: number): number;
|
|
24
|
+
matrix_multiply(aPtr: number, aRows: number, aCols: number, bPtr: number, bCols: number, resultPtr: number): void;
|
|
25
|
+
matrix_transpose(aPtr: number, rows: number, cols: number, resultPtr: number): void;
|
|
26
|
+
matrix_trace(dataPtr: number, rows: number, cols: number): number;
|
|
27
|
+
matrix_norm_frobenius(dataPtr: number): number;
|
|
28
|
+
__new(size: number, id: number): number;
|
|
29
|
+
__pin(ptr: number): number;
|
|
30
|
+
__unpin(ptr: number): void;
|
|
31
|
+
__collect(): void;
|
|
32
|
+
}
|
|
33
|
+
export interface MathTSWasmInstance {
|
|
34
|
+
exports: MathTSWasmExports;
|
|
35
|
+
module: WebAssembly.Module;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Load the WASM module from a URL or buffer
|
|
39
|
+
*/
|
|
40
|
+
export declare function loadWasm(source: string | BufferSource): Promise<MathTSWasmInstance>;
|
|
41
|
+
/**
|
|
42
|
+
* Synchronously load the WASM module (Node.js only)
|
|
43
|
+
*/
|
|
44
|
+
export declare function loadWasmSync(source: BufferSource): MathTSWasmInstance;
|
|
45
|
+
/**
|
|
46
|
+
* High-level WASM wrapper with convenient array operations
|
|
47
|
+
*/
|
|
48
|
+
export declare class MathTSWasm {
|
|
49
|
+
private instance;
|
|
50
|
+
private memory;
|
|
51
|
+
private exports;
|
|
52
|
+
private constructor();
|
|
53
|
+
/**
|
|
54
|
+
* Create a new MathTSWasm instance
|
|
55
|
+
*/
|
|
56
|
+
static create(source: string | BufferSource): Promise<MathTSWasm>;
|
|
57
|
+
/**
|
|
58
|
+
* Get raw exports for advanced usage
|
|
59
|
+
*/
|
|
60
|
+
get raw(): MathTSWasmExports;
|
|
61
|
+
add(a: number, b: number): number;
|
|
62
|
+
subtract(a: number, b: number): number;
|
|
63
|
+
multiply(a: number, b: number): number;
|
|
64
|
+
divide(a: number, b: number): number;
|
|
65
|
+
sqrt(a: number): number;
|
|
66
|
+
pow(a: number, b: number): number;
|
|
67
|
+
exp(a: number): number;
|
|
68
|
+
log(a: number): number;
|
|
69
|
+
sin(a: number): number;
|
|
70
|
+
cos(a: number): number;
|
|
71
|
+
tan(a: number): number;
|
|
72
|
+
abs(a: number): number;
|
|
73
|
+
/**
|
|
74
|
+
* Allocate a Float64Array in WASM memory
|
|
75
|
+
*/
|
|
76
|
+
allocFloat64Array(length: number): {
|
|
77
|
+
ptr: number;
|
|
78
|
+
array: Float64Array;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Free a previously allocated array
|
|
82
|
+
*/
|
|
83
|
+
freeArray(ptr: number): void;
|
|
84
|
+
/**
|
|
85
|
+
* Copy a JavaScript array to WASM memory
|
|
86
|
+
*/
|
|
87
|
+
copyToWasm(data: ArrayLike<number>): {
|
|
88
|
+
ptr: number;
|
|
89
|
+
array: Float64Array;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Copy from WASM memory to a new JavaScript array
|
|
93
|
+
*/
|
|
94
|
+
copyFromWasm(ptr: number, length: number): Float64Array;
|
|
95
|
+
/**
|
|
96
|
+
* Run garbage collection
|
|
97
|
+
*/
|
|
98
|
+
gc(): void;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Default export for convenience
|
|
102
|
+
*/
|
|
103
|
+
export default MathTSWasm;
|
|
104
|
+
//# sourceMappingURL=wasm-loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm-loader.d.ts","sourceRoot":"","sources":["../../src/bindings/wasm-loader.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,MAAM,WAAW,iBAAiB;IAEhC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC;IAG3B,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,OAAO,CA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AC,CAAC,EAAE,MAAM,GAAG,MAAM;IAItB,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAItB,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAItB,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAItB,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAQtB;;OAEG;IACH,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,YAAY,CAAA;KAAE;IAcvE;;OAEG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAI5B;;OAEG;IACH,UAAU,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,YAAY,CAAA;KAAE;IAQzE;;OAEG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY;IAKvD;;OAEG;IACH,EAAE,IAAI,IAAI;CAGX;AAED;;GAEG;AACH,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,187 @@
|
|
|
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
|
+
* Load the WASM module from a URL or buffer
|
|
9
|
+
*/
|
|
10
|
+
export async function loadWasm(source) {
|
|
11
|
+
let wasmModule;
|
|
12
|
+
if (typeof source === 'string') {
|
|
13
|
+
// Load from URL
|
|
14
|
+
if (typeof fetch !== 'undefined') {
|
|
15
|
+
const response = await fetch(source);
|
|
16
|
+
const buffer = await response.arrayBuffer();
|
|
17
|
+
wasmModule = await WebAssembly.compile(buffer);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
// Node.js environment
|
|
21
|
+
const fs = await import('fs');
|
|
22
|
+
const path = await import('path');
|
|
23
|
+
const buffer = fs.readFileSync(path.resolve(source));
|
|
24
|
+
wasmModule = await WebAssembly.compile(buffer);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// Load from buffer
|
|
29
|
+
wasmModule = await WebAssembly.compile(source);
|
|
30
|
+
}
|
|
31
|
+
const imports = createImports();
|
|
32
|
+
const instance = await WebAssembly.instantiate(wasmModule, imports);
|
|
33
|
+
return {
|
|
34
|
+
exports: instance.exports,
|
|
35
|
+
module: wasmModule,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Synchronously load the WASM module (Node.js only)
|
|
40
|
+
*/
|
|
41
|
+
export function loadWasmSync(source) {
|
|
42
|
+
const wasmModule = new WebAssembly.Module(source);
|
|
43
|
+
const imports = createImports();
|
|
44
|
+
const instance = new WebAssembly.Instance(wasmModule, imports);
|
|
45
|
+
return {
|
|
46
|
+
exports: instance.exports,
|
|
47
|
+
module: wasmModule,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create the import object for WASM instantiation
|
|
52
|
+
*/
|
|
53
|
+
function createImports() {
|
|
54
|
+
return {
|
|
55
|
+
env: {
|
|
56
|
+
// Abort handler
|
|
57
|
+
abort(messagePtr, fileNamePtr, line, column) {
|
|
58
|
+
console.error(`WASM abort at ${line}:${column}`);
|
|
59
|
+
throw new Error('WASM execution aborted');
|
|
60
|
+
},
|
|
61
|
+
// Trace for debugging (optional)
|
|
62
|
+
trace(messagePtr, numArgs) {
|
|
63
|
+
// Debug tracing - can be implemented if needed
|
|
64
|
+
},
|
|
65
|
+
// Seed for random numbers
|
|
66
|
+
seed() {
|
|
67
|
+
return Date.now() * Math.random();
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* High-level WASM wrapper with convenient array operations
|
|
74
|
+
*/
|
|
75
|
+
export class MathTSWasm {
|
|
76
|
+
instance;
|
|
77
|
+
memory;
|
|
78
|
+
exports;
|
|
79
|
+
constructor(instance) {
|
|
80
|
+
this.instance = instance;
|
|
81
|
+
this.exports = instance.exports;
|
|
82
|
+
this.memory = instance.exports.memory;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create a new MathTSWasm instance
|
|
86
|
+
*/
|
|
87
|
+
static async create(source) {
|
|
88
|
+
const instance = await loadWasm(source);
|
|
89
|
+
return new MathTSWasm(instance);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get raw exports for advanced usage
|
|
93
|
+
*/
|
|
94
|
+
get raw() {
|
|
95
|
+
return this.exports;
|
|
96
|
+
}
|
|
97
|
+
// =========================================================================
|
|
98
|
+
// Scalar Operations
|
|
99
|
+
// =========================================================================
|
|
100
|
+
add(a, b) {
|
|
101
|
+
return this.exports.add_f64(a, b);
|
|
102
|
+
}
|
|
103
|
+
subtract(a, b) {
|
|
104
|
+
return this.exports.sub_f64(a, b);
|
|
105
|
+
}
|
|
106
|
+
multiply(a, b) {
|
|
107
|
+
return this.exports.mul_f64(a, b);
|
|
108
|
+
}
|
|
109
|
+
divide(a, b) {
|
|
110
|
+
return this.exports.div_f64(a, b);
|
|
111
|
+
}
|
|
112
|
+
sqrt(a) {
|
|
113
|
+
return this.exports.sqrt_f64(a);
|
|
114
|
+
}
|
|
115
|
+
pow(a, b) {
|
|
116
|
+
return this.exports.pow_f64(a, b);
|
|
117
|
+
}
|
|
118
|
+
exp(a) {
|
|
119
|
+
return this.exports.exp_f64(a);
|
|
120
|
+
}
|
|
121
|
+
log(a) {
|
|
122
|
+
return this.exports.log_f64(a);
|
|
123
|
+
}
|
|
124
|
+
sin(a) {
|
|
125
|
+
return this.exports.sin_f64(a);
|
|
126
|
+
}
|
|
127
|
+
cos(a) {
|
|
128
|
+
return this.exports.cos_f64(a);
|
|
129
|
+
}
|
|
130
|
+
tan(a) {
|
|
131
|
+
return this.exports.tan_f64(a);
|
|
132
|
+
}
|
|
133
|
+
abs(a) {
|
|
134
|
+
return this.exports.abs_f64(a);
|
|
135
|
+
}
|
|
136
|
+
// =========================================================================
|
|
137
|
+
// Memory Helpers
|
|
138
|
+
// =========================================================================
|
|
139
|
+
/**
|
|
140
|
+
* Allocate a Float64Array in WASM memory
|
|
141
|
+
*/
|
|
142
|
+
allocFloat64Array(length) {
|
|
143
|
+
const FLOAT64_ARRAY_ID = 3; // AssemblyScript array type ID
|
|
144
|
+
const byteLength = length * 8;
|
|
145
|
+
const ptr = this.exports.__new(byteLength + 16, FLOAT64_ARRAY_ID);
|
|
146
|
+
this.exports.__pin(ptr);
|
|
147
|
+
const view = new DataView(this.memory.buffer);
|
|
148
|
+
view.setInt32(ptr, ptr + 16, true); // data pointer
|
|
149
|
+
view.setInt32(ptr + 4, length, true); // length
|
|
150
|
+
const array = new Float64Array(this.memory.buffer, ptr + 16, length);
|
|
151
|
+
return { ptr, array };
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Free a previously allocated array
|
|
155
|
+
*/
|
|
156
|
+
freeArray(ptr) {
|
|
157
|
+
this.exports.__unpin(ptr);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Copy a JavaScript array to WASM memory
|
|
161
|
+
*/
|
|
162
|
+
copyToWasm(data) {
|
|
163
|
+
const { ptr, array } = this.allocFloat64Array(data.length);
|
|
164
|
+
for (let i = 0; i < data.length; i++) {
|
|
165
|
+
array[i] = data[i];
|
|
166
|
+
}
|
|
167
|
+
return { ptr, array };
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Copy from WASM memory to a new JavaScript array
|
|
171
|
+
*/
|
|
172
|
+
copyFromWasm(ptr, length) {
|
|
173
|
+
const offset = ptr + 16; // Skip header
|
|
174
|
+
return new Float64Array(this.memory.buffer.slice(offset, offset + length * 8));
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Run garbage collection
|
|
178
|
+
*/
|
|
179
|
+
gc() {
|
|
180
|
+
this.exports.__collect();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Default export for convenience
|
|
185
|
+
*/
|
|
186
|
+
export default MathTSWasm;
|
|
187
|
+
//# sourceMappingURL=wasm-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm-loader.js","sourceRoot":"","sources":["../../src/bindings/wasm-loader.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA4CH;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,MAA6B;IAC1D,IAAI,UAA8B,CAAC;IAEnC,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,gBAAgB;QAChB,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE,CAAC;YACjC,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,MAAM,CAAC,CAAC;YACrC,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC;YAC5C,UAAU,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACjD,CAAC;aAAM,CAAC;YACN,sBAAsB;YACtB,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC;YAC9B,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,CAAC;YAClC,MAAM,MAAM,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;YACrD,UAAU,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;SAAM,CAAC;QACN,mBAAmB;QACnB,UAAU,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACjD,CAAC;IAED,MAAM,OAAO,GAAG,aAAa,EAAE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