scirs2-wasm 0.2.0

WebAssembly (WASM) bindings for SciRS2 - JavaScript/TypeScript interop for scientific computing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# SciRS2-WASM Development Guide

Complete guide for building, testing, and deploying SciRS2 WebAssembly bindings.

## Table of Contents

- [Prerequisites]#prerequisites
- [Building]#building
- [Testing]#testing
- [Optimization]#optimization
- [Deployment]#deployment
- [Browser Compatibility]#browser-compatibility
- [Performance Tips]#performance-tips
- [Troubleshooting]#troubleshooting

## Prerequisites

### Required Tools

1. **Rust** (1.70+)
   ```bash
   rustup install stable
   rustup target add wasm32-unknown-unknown
   ```

2. **wasm-pack**
   ```bash
   curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
   ```

3. **Node.js** (18+)
   ```bash
   # Using nvm
   nvm install 18
   nvm use 18
   ```

4. **wasm-opt** (optional, for optimization)
   ```bash
   # macOS
   brew install binaryen

   # Ubuntu/Debian
   sudo apt-get install binaryen

   # From source
   git clone https://github.com/WebAssembly/binaryen
   cd binaryen && cmake . && make
   ```

## Building

### Standard Build

```bash
# Build for bundlers (webpack, rollup, parcel)
wasm-pack build --target bundler

# Build for web (ES modules)
wasm-pack build --target web

# Build for Node.js
wasm-pack build --target nodejs

# Build all targets
npm run build:all
```

### Release Build (Optimized)

```bash
# Release build with size optimization
wasm-pack build --release --target bundler

# Or use npm script
npm run build:release
```

### SIMD Build (Advanced)

For browsers with WASM SIMD support:

```bash
# Build with SIMD instructions
RUSTFLAGS='-C target-feature=+simd128' \
  wasm-pack build --target bundler --release -- --features simd

# Or use npm script
npm run build:simd
```

### Custom Features

```bash
# Build with specific features
wasm-pack build --release -- \
  --features "linalg,stats,fft,signal"

# Build with all modules
wasm-pack build --release -- --features all-modules
```

## Testing

### Browser Tests

```bash
# Firefox (headless)
wasm-pack test --headless --firefox

# Chrome (headless)
wasm-pack test --headless --chrome

# Safari (requires macOS)
wasm-pack test --headless --safari

# All browsers
npm test
```

### Node.js Tests

```bash
# Run tests in Node.js
wasm-pack test --node

# Or use npm script
npm run test:node
```

### Interactive Testing

```bash
# Serve the example web app
cd www
npm install
npm start

# Open browser to http://localhost:8080
```

### Manual Testing

```bash
# Build and run Node.js example
wasm-pack build --target nodejs
node examples/node_example.js
```

## Optimization

### Size Optimization

1. **Build with size optimization**
   ```bash
   wasm-pack build --release --target bundler
   ```

2. **Run wasm-opt**
   ```bash
   cd pkg
   wasm-opt -Oz -o scirs2_wasm_bg_opt.wasm scirs2_wasm_bg.wasm

   # Or use npm script
   npm run optimize
   ```

3. **Compare sizes**
   ```bash
   ls -lh pkg/*.wasm
   ```

### Profile Configuration

Edit `Cargo.toml` for different optimization profiles:

```toml
[profile.release]
opt-level = "z"     # "z" for size, "3" for speed
lto = true          # Link-time optimization
codegen-units = 1   # Better optimization
panic = "abort"     # Smaller binary
strip = true        # Remove debug symbols
```

### Feature Gating

Build only what you need:

```bash
# Minimal build (array operations only)
wasm-pack build --release --no-default-features

# With specific features
wasm-pack build --release --features "linalg,stats"
```

## Deployment

### NPM Package

1. **Build for NPM**
   ```bash
   wasm-pack build --release --target bundler
   ```

2. **Publish** (requires npm account)
   ```bash
   cd pkg
   npm publish
   ```

3. **Install in projects**
   ```bash
   npm install scirs2-wasm
   ```

### CDN Deployment

#### Using unpkg

```html
<script type="module">
  import init, * as scirs2 from 'https://unpkg.com/scirs2-wasm/scirs2_wasm.js';

  async function main() {
    await init();
    // Use scirs2...
  }

  main();
</script>
```

#### Using jsDelivr

```html
<script type="module">
  import init, * as scirs2 from 'https://cdn.jsdelivr.net/npm/scirs2-wasm/scirs2_wasm.js';
  // ...
</script>
```

### Self-Hosting

1. **Copy WASM files to your server**
   ```bash
   cp pkg/*.wasm public/
   cp pkg/*.js public/
   cp pkg/*.d.ts public/
   ```

2. **Configure MIME types**
   ```nginx
   # nginx
   location ~ \.wasm$ {
       types { application/wasm wasm; }
   }
   ```

3. **Enable compression**
   ```nginx
   # nginx
   gzip on;
   gzip_types application/wasm;
   ```

## Browser Compatibility

### Standard WASM

- Chrome 57+
- Firefox 52+
- Safari 11+
- Edge 16+
- Node.js 8+

### WASM SIMD (simd128)

- Chrome 91+
- Firefox 89+
- Safari 16.4+ (limited support)
- Edge 91+
- Node.js 20+

### Feature Detection

```javascript
// Check WASM support
if (typeof WebAssembly === 'undefined') {
  console.error('WebAssembly is not supported');
}

// Check SIMD support
const simdSupported = scirs2.has_simd_support();
console.log('SIMD support:', simdSupported);
```

## Performance Tips

### 1. Reuse Arrays

```javascript
// ❌ Bad: Creates new arrays each iteration
for (let i = 0; i < 1000; i++) {
  const arr = new scirs2.WasmArray([1, 2, 3]);
  // process arr...
}

// ✅ Good: Reuse array
const arr = new scirs2.WasmArray([1, 2, 3]);
for (let i = 0; i < 1000; i++) {
  // process arr...
}
```

### 2. Batch Operations

```javascript
// ❌ Bad: Multiple small operations
for (let i = 0; i < data.length; i++) {
  result[i] = scirs2.compute(data[i]);
}

// ✅ Good: Single batch operation
const result = scirs2.compute_batch(data);
```

### 3. Use Typed Arrays

```javascript
// ✅ Better performance with TypedArrays
const data = new Float64Array([1, 2, 3, 4]);
const arr = new scirs2.WasmArray(data);
```

### 4. Minimize JS<->WASM Crossing

```javascript
// ❌ Bad: Frequent JS<->WASM calls
for (let i = 0; i < arr.len(); i++) {
  const val = arr.get(i);  // WASM call
  console.log(val);
}

// ✅ Good: Single bulk operation
const data = arr.to_array();  // One WASM call
for (let i = 0; i < data.length; i++) {
  console.log(data[i]);
}
```

### 5. Use Web Workers

```javascript
// worker.js
importScripts('scirs2_wasm.js');

self.onmessage = async (e) => {
  const { data } = e;
  await wasm_bindgen('scirs2_wasm_bg.wasm');

  const arr = new wasm_bindgen.WasmArray(data);
  const result = wasm_bindgen.compute(arr);

  self.postMessage(result.to_array());
};
```

### 6. Enable SIMD

```bash
# Build with SIMD
RUSTFLAGS='-C target-feature=+simd128' \
  wasm-pack build --release --features simd
```

## Troubleshooting

### Build Errors

#### "wasm32-unknown-unknown target not found"

```bash
rustup target add wasm32-unknown-unknown
```

#### "wasm-pack not found"

```bash
cargo install wasm-pack
```

#### "memory access out of bounds"

Increase stack size in `.cargo/config.toml`:

```toml
[target.wasm32-unknown-unknown]
rustflags = ["-C", "link-arg=-zstack-size=2097152"]
```

### Runtime Errors

#### "Module not found" in Node.js

```bash
# Rebuild for Node.js target
wasm-pack build --target nodejs
```

#### "MIME type error" in browser

Configure server to serve `.wasm` files with correct MIME type:

```javascript
// Express.js
app.use(express.static('public', {
  setHeaders: (res, path) => {
    if (path.endsWith('.wasm')) {
      res.set('Content-Type', 'application/wasm');
    }
  }
}));
```

#### Memory issues

```javascript
// Create arrays in smaller chunks
const CHUNK_SIZE = 10000;
for (let i = 0; i < totalSize; i += CHUNK_SIZE) {
  const chunk = data.slice(i, i + CHUNK_SIZE);
  processChunk(chunk);
}
```

### Performance Issues

1. **Enable release mode**
   ```bash
   wasm-pack build --release
   ```

2. **Run wasm-opt**
   ```bash
   wasm-opt -O3 input.wasm -o output.wasm
   ```

3. **Profile with Chrome DevTools**
   - Open DevTools → Performance
   - Record and analyze

4. **Check bundle size**
   ```bash
   ls -lh pkg/*.wasm
   ```

## Advanced Topics

### Custom Memory Management

```rust
// In Rust code
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn allocate_buffer(size: usize) -> Vec<f64> {
    vec![0.0; size]
}
```

### Streaming Compilation

```javascript
// For large WASM files
async function loadWasm() {
  const response = await fetch('scirs2_wasm_bg.wasm');
  const module = await WebAssembly.compileStreaming(response);
  // ...
}
```

### Multi-threading (Experimental)

Requires SharedArrayBuffer and Atomics:

```javascript
// Check support
const supportsThreads = typeof SharedArrayBuffer !== 'undefined';
```

## Resources

- [wasm-bindgen documentation]https://rustwasm.github.io/wasm-bindgen/
- [wasm-pack documentation]https://rustwasm.github.io/wasm-pack/
- [WebAssembly MDN]https://developer.mozilla.org/en-US/docs/WebAssembly
- [Rust WASM Book]https://rustwasm.github.io/docs/book/

## Support

For issues and questions:
- GitHub Issues: https://github.com/cool-japan/scirs/issues
- Discussions: https://github.com/cool-japan/scirs/discussions

## License

Apache-2.0