Skip to content

Commit 762a5fe

Browse files
committed
- Expand from 172 to 531 opcodes (SIMD, atomics, bulk memory, reference types, tail calls, relaxed SIMD, exception handling, memory64, extended-const)
- Add feature/target system (mvp, 2.0, 3.0, latest) with verification - Add WAT parser, exception handling, extended constant expressions - Add interactive playground with 65 examples, search/filter, split-view editor - Add 25 new test suites (1732 total tests) - Add CI workflows and docs
1 parent e455cd7 commit 762a5fe

66 files changed

Lines changed: 35240 additions & 5059 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ TypeScript library for programmatically generating WebAssembly modules. Build WA
1010
## Features
1111

1212
- Fluent builder pattern for constructing WASM modules
13-
- Full instruction set — arithmetic, control flow, memory, tables, globals
14-
- i32, i64, f32, f64 value types with BigInt support for i64
13+
- **531 instructions** — arithmetic, control flow, memory, tables, globals, SIMD, atomics, exception handling
14+
- **Target system**`mvp`, `2.0`, `3.0`, `latest` with automatic feature gating
15+
- i32, i64, f32, f64, v128 value types with BigInt support for i64
16+
- **128-bit SIMD** — 236 vector instructions + 20 relaxed SIMD
17+
- **Threads & atomics** — shared memory, 67 atomic operations
18+
- **Exception handling** — tags, throw, try/catch/rethrow
19+
- **Memory64** — 64-bit addressed memory
20+
- Tail calls, multi-value returns, bulk memory, reference types
1521
- Function imports/exports with host interop
1622
- Memory and table management with import/export
17-
- WAT text format output via `TextModuleWriter`
18-
- WAT text format parsing via `parseWat()`
23+
- WAT text format output and parsing
1924
- Binary reader for inspecting compiled modules
2025
- Compile-time verification (control flow + operand stack)
21-
- Debug name section generation
26+
- Data-driven — opcodes generated from `generator/opcodes.json`
2227
- Zero production dependencies
2328

2429
## Install
@@ -53,6 +58,8 @@ The entry point for building a module. Create an instance, define functions/memo
5358

5459
```typescript
5560
const mod = new ModuleBuilder('myModule', {
61+
target: 'latest', // 'mvp' | '2.0' | '3.0' | 'latest'
62+
features: [], // additional features beyond target
5663
generateNameSection: true,
5764
disableVerification: false,
5865
});
@@ -120,10 +127,16 @@ const instance = await mod.instantiate({
120127
### Memory
121128

122129
```typescript
123-
const mem = mod.defineMemory(1, 4); // 1 page initial, 4 max (64KB per page)
130+
const mem = mod.defineMemory(1, 4); // 1 page initial, 4 max (64KB per page)
124131
mod.exportMemory(mem, 'memory');
125132

126-
// Or import memory
133+
// Shared memory (for threads/atomics — requires maximum size)
134+
const shared = mod.defineMemory(1, 10, true); // shared = true
135+
136+
// 64-bit addressed memory
137+
const mem64 = mod.defineMemory(1, 100, false, true); // memory64 = true
138+
139+
// Import memory
127140
mod.importMemory('env', 'mem', 1, 4);
128141
```
129142

@@ -152,7 +165,7 @@ See the [API Reference](docs/api.md) for complete documentation.
152165

153166
## Playground
154167

155-
Try webasmjs in the browser with the [interactive playground](https://devnamedzed.github.io/webasmjs/). It includes examples for arithmetic, control flow, memory, tables, imports, floating point, i64/BigInt, algorithms, and WAT parsing.
168+
Try webasmjs in the browser with the [interactive playground](https://devnamedzed.github.io/webasmjs/). It includes 40+ examples covering arithmetic, control flow, memory, tables, imports, floating point, i64/BigInt, SIMD, algorithms, WAT parsing, and post-MVP features.
156169

157170
To run the playground locally:
158171

docs/api.md

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ new ModuleBuilder(name: string, options?: ModuleBuilderOptions)
1616
|--------|------|---------|-------------|
1717
| `generateNameSection` | `boolean` | `true` | Include debug name section in output |
1818
| `disableVerification` | `boolean` | `false` | Skip control flow and operand stack verification |
19+
| `target` | `WasmTarget` | `'latest'` | WebAssembly target: `'mvp'`, `'2.0'`, `'3.0'`, or `'latest'` |
20+
| `features` | `WasmFeature[]` | `[]` | Additional feature flags beyond those included by the target |
1921

2022
### Methods
2123

@@ -49,7 +51,7 @@ importFunction(
4951
): ImportBuilder
5052

5153
// Import memory
52-
importMemory(module: string, name: string, initial: number, maximum?: number): void
54+
importMemory(module: string, name: string, initial: number, maximum?: number, shared?: boolean): ImportBuilder
5355

5456
// Import a table
5557
importTable(
@@ -69,6 +71,13 @@ importGlobal(
6971
): ImportBuilder
7072
```
7173

74+
#### Tags
75+
76+
```typescript
77+
// Define an exception tag
78+
defineTag(parameters: ValueType[]): TagBuilder
79+
```
80+
7281
#### Exports
7382

7483
```typescript
@@ -81,7 +90,7 @@ exportGlobal(global: GlobalBuilder, name: string): void
8190
#### Memory
8291

8392
```typescript
84-
defineMemory(initial: number, maximum?: number): MemoryBuilder
93+
defineMemory(initial: number, maximum?: number, shared?: boolean, memory64?: boolean): MemoryBuilder
8594
defineData(data: Uint8Array, offset: number): DataSegmentBuilder
8695
```
8796

@@ -96,6 +105,13 @@ defineTableSegment(
96105
): ElementSegmentBuilder
97106
```
98107

108+
#### Passive Element Segments
109+
110+
```typescript
111+
// Define a passive element segment (for use with table.init)
112+
definePassiveElementSegment(elements: FunctionBuilder[]): ElementSegmentBuilder
113+
```
114+
99115
#### Globals
100116

101117
```typescript
@@ -121,6 +137,13 @@ instantiate(imports?: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInsta
121137
toString(): string
122138
```
123139

140+
#### Feature Checking
141+
142+
```typescript
143+
// Check if a feature is enabled for this module
144+
hasFeature(feature: WasmFeature): boolean
145+
```
146+
124147
---
125148

126149
## FunctionBuilder
@@ -281,6 +304,50 @@ select(): void
281304
nop(): void
282305
```
283306
307+
### Tail Calls
308+
309+
```typescript
310+
return_call(target: FunctionBuilder | ImportBuilder): void
311+
return_call_indirect(funcType: FuncTypeBuilder): void
312+
```
313+
314+
### Atomic Operations
315+
316+
```typescript
317+
// Load/store (alignment and offset immediates)
318+
atomic_load_i32(align: number, offset: number): void
319+
atomic_load_i64(align: number, offset: number): void
320+
atomic_store_i32(align: number, offset: number): void
321+
atomic_store_i64(align: number, offset: number): void
322+
323+
// Read-modify-write
324+
atomic_rmw_add_i32(align: number, offset: number): void
325+
atomic_rmw_sub_i32(align: number, offset: number): void
326+
atomic_rmw_and_i32(align: number, offset: number): void
327+
atomic_rmw_or_i32(align: number, offset: number): void
328+
atomic_rmw_xor_i32(align: number, offset: number): void
329+
atomic_rmw_xchg_i32(align: number, offset: number): void
330+
atomic_rmw_cmpxchg_i32(align: number, offset: number): void
331+
// Same patterns for i64 variants
332+
333+
// Synchronization
334+
atomic_fence(flags: number): void
335+
atomic_notify(align: number, offset: number): void
336+
atomic_wait32(align: number, offset: number): void
337+
atomic_wait64(align: number, offset: number): void
338+
```
339+
340+
### Exception Handling
341+
342+
```typescript
343+
throw(tagIndex: number): void
344+
try(blockType: BlockType): void
345+
catch(tagIndex: number): void
346+
catch_all(): void
347+
rethrow(depth: number): void
348+
delegate(depth: number): void
349+
```
350+
284351
---
285352
286353
## GlobalBuilder
@@ -378,6 +445,7 @@ ValueType.Int32 // i32
378445
ValueType.Int64 // i64
379446
ValueType.Float32 // f32
380447
ValueType.Float64 // f64
448+
ValueType.V128 // v128 (128-bit SIMD vector)
381449
```
382450

383451
### BlockType
@@ -388,6 +456,7 @@ BlockType.Int32 // block produces an i32
388456
BlockType.Int64 // block produces an i64
389457
BlockType.Float32 // block produces an f32
390458
BlockType.Float64 // block produces an f64
459+
BlockType.V128 // block produces a v128
391460
```
392461

393462
### ElementType
@@ -404,3 +473,21 @@ ExternalKind.Table
404473
ExternalKind.Memory
405474
ExternalKind.Global
406475
```
476+
477+
### WasmTarget
478+
479+
```typescript
480+
'mvp' // WebAssembly 1.0 — no extensions
481+
'2.0' // WebAssembly 2.0 — widely deployed post-MVP features
482+
'3.0' // WebAssembly 3.0 — all standardized features
483+
'latest' // Everything in 3.0 + newly standardized extensions (default)
484+
```
485+
486+
### WasmFeature
487+
488+
```typescript
489+
'sign-extend' | 'sat-trunc' | 'bulk-memory' | 'reference-types' | 'simd'
490+
| 'multi-value' | 'mutable-globals' | 'tail-call' | 'extended-const'
491+
| 'threads' | 'exception-handling' | 'multi-memory' | 'multi-table'
492+
| 'relaxed-simd' | 'memory64' | 'gc'
493+
```

0 commit comments

Comments
 (0)