Skip to content

Commit 911ed74

Browse files
committed
add two skills about binary and wasm operations
1 parent 566bd00 commit 911ed74

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
name: typed-arrays
3+
description: |
4+
Modern TypedArray and ArrayBuffer features including resizable buffers,
5+
transfer operations, Float16Array, and Uint8Array base64/hex encoding.
6+
compatibility: Node.js 20+ and all the modern browsers
7+
---
8+
9+
# Modern Typed Arrays
10+
11+
## ES2023: Change Array by Copy
12+
13+
Immutable operations returning new arrays:
14+
15+
```typescript
16+
const arr = new Uint8Array([3, 1, 2]);
17+
18+
arr.toReversed(); // Uint8Array [2, 1, 3]
19+
arr.toSorted((a, b) => a - b); // Uint8Array [1, 2, 3]
20+
arr.with(0, 99); // Uint8Array [99, 1, 2]
21+
```
22+
23+
## ES2023: findLast / findLastIndex
24+
25+
```typescript
26+
const arr = new Uint8Array([1, 2, 3, 2, 1]);
27+
28+
arr.findLast(x => x === 2); // 2
29+
arr.findLastIndex(x => x === 2); // 3
30+
```
31+
32+
## ES2024: Resizable ArrayBuffer
33+
34+
```typescript
35+
const buffer = new ArrayBuffer(16, { maxByteLength: 1024 });
36+
37+
buffer.resizable; // true
38+
buffer.maxByteLength; // 1024
39+
buffer.resize(64); // grow
40+
buffer.resize(8); // shrink
41+
```
42+
43+
### Growable SharedArrayBuffer
44+
45+
```typescript
46+
const shared = new SharedArrayBuffer(16, { maxByteLength: 1024 });
47+
shared.growable; // true
48+
shared.grow(64); // can only grow, not shrink
49+
```
50+
51+
### TypedArray tracks resizable buffer
52+
53+
```typescript
54+
const buffer = new ArrayBuffer(16, { maxByteLength: 64 });
55+
const view = new Uint8Array(buffer);
56+
view.length; // 16
57+
buffer.resize(32);
58+
view.length; // 32 (auto-tracks)
59+
```
60+
61+
## ES2024: ArrayBuffer Transfer
62+
63+
```typescript
64+
const buffer = new ArrayBuffer(16);
65+
const arr = new Uint8Array(buffer);
66+
arr[0] = 42;
67+
68+
const newBuffer = buffer.transfer(); // zero-copy transfer
69+
buffer.detached; // true
70+
newBuffer.byteLength; // 16
71+
72+
// Transfer with resize
73+
const grown = buffer.transfer(64);
74+
75+
// Convert resizable to fixed
76+
const fixed = resizable.transferToFixedLength();
77+
```
78+
79+
## ES2025: Float16Array
80+
81+
```typescript
82+
const f16 = new Float16Array(4);
83+
const f16arr = Float16Array.of(1.5, 2.5, 3.5);
84+
85+
Float16Array.BYTES_PER_ELEMENT; // 2
86+
// Range: ±65504 (max), ±6.1e-5 (min positive)
87+
```
88+
89+
### DataView Float16
90+
91+
```typescript
92+
const view = new DataView(buffer);
93+
view.setFloat16(0, 3.14, true); // little-endian
94+
view.getFloat16(0, true); // ≈3.140625
95+
```
96+
97+
## ES2026: Uint8Array Base64
98+
99+
Not yet in Node.js v24.
100+
101+
### Base64
102+
103+
```typescript
104+
const bytes = new Uint8Array([72, 101, 108, 108, 111]);
105+
106+
bytes.toBase64(); // "SGVsbG8="
107+
bytes.toBase64({ alphabet: "base64url" }); // URL-safe
108+
bytes.toBase64({ omitPadding: true }); // no trailing =
109+
110+
Uint8Array.fromBase64("SGVsbG8=");
111+
Uint8Array.fromBase64("SGVsbG8", { alphabet: "base64url" });
112+
113+
// Write to existing buffer
114+
const { read, written } = target.setFromBase64("SGVsbG8=");
115+
```

.claude/skills/wasm/SKILL.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
name: wasm
3+
description: |
4+
Modern WebAssembly (WASM) development expertise covering WASM 3.0 features
5+
and optimization techniques. Use this skill when working with WebAssembly
6+
modules, optimizing WASM performance, or integrating WASM with JavaScript/TypeScript.
7+
compatibility: WebAssembly v3.0 and later
8+
---
9+
10+
# WebAssembly Development Skill
11+
12+
## WAT Syntax
13+
14+
Use **folded (S-expression) syntax** for readability:
15+
16+
```wat
17+
;; Folded syntax (preferred)
18+
(i32.add (local.get $x) (local.get $y))
19+
20+
;; Flat syntax (avoid)
21+
local.get $x
22+
local.get $y
23+
i32.add
24+
```
25+
26+
## WebAssembly 3.0 Features
27+
28+
### Memory64 (64-bit Address Space)
29+
- Memories and tables use `i64` as address type
30+
- Expands addressable space from 4GB to 16 exabytes
31+
- Syntax: `(memory i64 1)` instead of `(memory 1)`
32+
33+
### Multiple Memories
34+
```wat
35+
(module
36+
(memory $main 1)
37+
(memory $scratch 1))
38+
```
39+
40+
### Tail Call Optimization
41+
- Efficient recursion via `return_call` and `return_call_indirect`
42+
- Prevents stack overflow for tail-recursive functions
43+
```wat
44+
(func $factorial (param $n i64) (param $acc i64) (result i64)
45+
(if (result i64) (i64.eqz (local.get $n))
46+
(then (local.get $acc))
47+
(else (return_call $factorial
48+
(i64.sub (local.get $n) (i64.const 1))
49+
(i64.mul (local.get $n) (local.get $acc))))))
50+
```
51+
52+
### Exception Handling
53+
- Native try/catch/throw semantics
54+
- Interoperates with JavaScript exceptions
55+
```wat
56+
(tag $error (param i32))
57+
(func $may_throw
58+
(throw $error (i32.const 42)))
59+
```
60+
61+
### Relaxed SIMD
62+
- Hardware-dependent SIMD optimizations beyond fixed-width 128-bit
63+
- `i8x16.relaxed_swizzle`, `f32x4.relaxed_madd`, etc.
64+
65+
### SIMD Example
66+
```wat
67+
;; Process 16 bytes at a time
68+
(v128.store (local.get $dst)
69+
(i8x16.add
70+
(v128.load (local.get $src1))
71+
(v128.load (local.get $src2))))
72+
```
73+
74+
## Toolchain (Binaryen)
75+
76+
| Task | Command |
77+
|------|---------|
78+
| Assemble WAT to WASM | `wasm-as module.wat -o module.wasm` |
79+
| Disassemble WASM to WAT | `wasm-dis module.wasm -o module.wat` |
80+
| Optimize for size | `wasm-opt -Oz in.wasm -o out.wasm` |
81+
| Optimize for speed | `wasm-opt -O3 in.wasm -o out.wasm` |
82+
83+
## JavaScript/TypeScript Integration
84+
85+
### Instantiation
86+
```typescript
87+
const module = await WebAssembly.compileStreaming(fetch('module.wasm'));
88+
const instance = await WebAssembly.instantiate(module, imports);
89+
```
90+
91+
### Memory Access
92+
```typescript
93+
const memory = new WebAssembly.Memory({ initial: 1, maximum: 100 });
94+
const buffer = new Uint8Array(instance.exports.memory.buffer);
95+
buffer.set(data, offset);
96+
```
97+
98+
## Resources
99+
100+
- [WebAssembly Specification](https://webassembly.github.io/spec/)
101+
- [Binaryen](https://github.com/WebAssembly/binaryen)

0 commit comments

Comments
 (0)