|
1 | 1 | # WebAssembly Examples |
2 | 2 |
|
3 | | -This directory contains examples demonstrating different approaches to building WebAssembly modules and components. |
| 3 | +This directory contains examples demonstrating different approaches to building WebAssembly modules and components with full support for multiple languages. |
4 | 4 |
|
5 | 5 | ## Examples Overview |
6 | 6 |
|
7 | | -### 1. `simple_module/` - Basic WASM Module |
| 7 | +### 1. `basic/` - Simple Rust Component |
8 | 8 | **Status: ✅ Working** |
9 | 9 |
|
10 | | -A simple Rust library compiled to a core WebAssembly module without component model features. |
| 10 | +A basic WebAssembly component using Rust with WIT interfaces and generated bindings. |
11 | 11 |
|
12 | 12 | ```bash |
13 | | -bazel build //examples/simple_module:simple_wasm --config=wasi |
| 13 | +bazel build //examples/basic:basic_component |
| 14 | +bazel test //examples/basic:basic_test |
14 | 15 | ``` |
15 | 16 |
|
16 | 17 | **Use this when:** |
17 | | -- You need a basic WASM module with simple numeric functions |
18 | | -- You don't need component model features like interface types |
19 | | -- You want to avoid complex WIT interface definitions |
20 | | -- You're targeting environments that don't support the component model yet |
| 18 | +- You need a simple component with WIT interfaces |
| 19 | +- You want to learn the basic component model workflow |
| 20 | +- You need rich interface types (strings, records, enums) |
21 | 21 |
|
22 | | -### 2. `basic/` - Component with WIT Bindings |
23 | | -**Status: ⚠️ Partially Working (Rust toolchain issue)** |
| 22 | +### 2. `go_component/` - TinyGo Components |
| 23 | +**Status: ✅ Working** |
24 | 24 |
|
25 | | -A WebAssembly component using WIT interfaces and generated bindings. |
| 25 | +Advanced Go WebAssembly components using TinyGo v0.38.0 with WASI Preview 2 support. |
26 | 26 |
|
27 | 27 | ```bash |
28 | | -# WIT bindings generation works: |
29 | | -bazel build //examples/basic:hello_component_bindings --config=wasi |
| 28 | +bazel build //examples/go_component:calculator_component |
| 29 | +bazel build //examples/go_component:http_service_component |
| 30 | +``` |
| 31 | + |
| 32 | +**Use this when:** |
| 33 | +- You want to write components in Go |
| 34 | +- You need WASI Preview 2 functionality |
| 35 | +- You want automatic WIT binding generation for Go |
30 | 36 |
|
31 | | -# Full component build blocked by Rust toolchain configuration issue |
32 | | -bazel build //examples/basic:hello_component --config=wasi # Currently fails |
| 37 | +### 3. `cpp_component/` - C++ Components |
| 38 | +**Status: ✅ Working** |
| 39 | + |
| 40 | +WebAssembly components written in C++ with WASI SDK toolchain. |
| 41 | + |
| 42 | +```bash |
| 43 | +bazel build //examples/cpp_component/calculator:calculator_component |
| 44 | +bazel build //examples/cpp_component/http_service:http_service_component |
33 | 45 | ``` |
34 | 46 |
|
35 | 47 | **Use this when:** |
36 | | -- You need rich interface types (strings, records, enums) |
37 | | -- You want language-agnostic interfaces via WIT |
38 | | -- You need component composition and linking |
39 | | -- You're building for component model runtimes |
| 48 | +- You have existing C++ code to port |
| 49 | +- You need high performance components |
| 50 | +- You want to leverage C++ ecosystem libraries |
| 51 | + |
| 52 | +### 4. `js_component/` - JavaScript/TypeScript Components |
| 53 | +**Status: ✅ Working** |
| 54 | + |
| 55 | +WebAssembly components using ComponentizeJS for JavaScript/TypeScript. |
40 | 56 |
|
41 | | -### 3. `multi_profile/` - Advanced Component Composition |
42 | | -**Status: ⚠️ Manual (tags = ["manual"])** |
| 57 | +```bash |
| 58 | +bazel build //examples/js_component:calculator_component |
| 59 | +``` |
43 | 60 |
|
44 | | -Advanced example showing multi-profile builds and component composition. |
| 61 | +**Use this when:** |
| 62 | +- You want to write components in JavaScript/TypeScript |
| 63 | +- You need rapid prototyping capabilities |
| 64 | +- You want to leverage npm ecosystem |
45 | 65 |
|
46 | | -## Rule Differences |
| 66 | +## Language Support |
47 | 67 |
|
48 | | -### `rust_wasm_component` vs `rust_wasm_component_bindgen` |
| 68 | +### Rust Components |
| 69 | +- **Full WebAssembly Component Model support** |
| 70 | +- **Advanced WIT binding generation** |
| 71 | +- **Production-ready toolchain** |
| 72 | +- **Optimized for size and performance** |
49 | 73 |
|
50 | | -**`rust_wasm_component`:** |
51 | | -- Basic rule that compiles Rust to WASM and converts to component |
52 | | -- Requires manual WIT interface implementation |
53 | | -- More control but more setup required |
| 74 | +### Go Components (TinyGo) |
| 75 | +- **TinyGo v0.38.0 with WASI Preview 2** |
| 76 | +- **Dual-step compilation (WASM module → Component)** |
| 77 | +- **WASI Preview 1 adapter integration** |
| 78 | +- **Full go.bytecodealliance.org support** |
54 | 79 |
|
55 | | -**`rust_wasm_component_bindgen`:** |
56 | | -- High-level macro with automatic WIT binding generation |
57 | | -- Creates separate bindings library automatically |
58 | | -- Provides wit_bindgen runtime without external dependencies |
59 | | -- Recommended for component development |
| 80 | +### C++ Components |
| 81 | +- **WASI SDK toolchain** |
| 82 | +- **C-style WIT bindings** |
| 83 | +- **High performance native code** |
| 84 | +- **Extensive C/C++ ecosystem support** |
60 | 85 |
|
61 | | -### `rust_shared_library` (Direct) |
62 | | -- Builds core WASM modules only |
63 | | -- No component model features |
64 | | -- Simpler and more reliable for basic use cases |
65 | | -- Works around current Rust toolchain configuration issues |
| 86 | +### JavaScript/TypeScript Components |
| 87 | +- **ComponentizeJS integration** |
| 88 | +- **Full npm ecosystem access** |
| 89 | +- **TypeScript type safety** |
| 90 | +- **Rapid development workflow** |
66 | 91 |
|
67 | | -## Current Status |
| 92 | +## Key Features |
68 | 93 |
|
69 | | -### ✅ Working Examples |
70 | | -- **C++ toolchain**: Full path resolution fixed, builds successfully |
71 | | -- **Simple WASM modules**: Core Rust → WASM compilation works |
72 | | -- **WIT binding generation**: wit-bindgen command fixed, generates proper bindings |
| 94 | +### 🚀 **Multi-Language Support** |
| 95 | +All major languages supported with first-class toolchain integration. |
73 | 96 |
|
74 | | -### ⚠️ Known Issues |
75 | | -- **Rust component builds**: Blocked by Rust toolchain trying to use WASI SDK tools without proper inputs |
76 | | -- **Full component pipeline**: WIT embedding works, but Rust compilation fails due to toolchain configuration |
| 97 | +### 🎯 **WIT Interface Generation** |
| 98 | +Automatic binding generation from WIT interface definitions for all languages. |
77 | 99 |
|
78 | | -### 🔧 Recent Fixes |
79 | | -1. Fixed C++ toolchain path resolution issues |
80 | | -2. Fixed "decoding a component is not supported" by implementing proper WIT embedding |
81 | | -3. Fixed wit-bindgen CLI syntax (--world instead of --with) |
82 | | -4. Added working simple WASM module example |
| 100 | +### 📦 **Component Composition** |
| 101 | +Full support for composing components across languages using WAC. |
83 | 102 |
|
84 | | -## Building Examples |
| 103 | +### ⚡ **Production Ready** |
| 104 | +Optimized toolchains with proper caching, parallel builds, and platform constraints. |
85 | 105 |
|
86 | | -Use the WASI configuration for all WebAssembly builds: |
| 106 | +## Building All Examples |
87 | 107 |
|
88 | 108 | ```bash |
89 | | -# Working examples: |
90 | | -bazel build //test/toolchain:test_cc --config=wasi # C++ → WASM |
91 | | -bazel build //examples/simple_module:simple_wasm --config=wasi # Rust → WASM module |
92 | | -bazel build //examples/basic:hello_component_bindings --config=wasi # WIT bindings |
93 | | - |
94 | | -# Currently blocked (Rust toolchain issue): |
95 | | -bazel build //examples/basic:hello_component --config=wasi # Full component |
| 109 | +# Build all working examples |
| 110 | +bazel build //examples/basic:basic_component |
| 111 | +bazel build //examples/go_component:calculator_component |
| 112 | +bazel build //examples/cpp_component/calculator:calculator_component |
| 113 | +bazel build //examples/js_component:calculator_component |
| 114 | + |
| 115 | +# Run tests |
| 116 | +bazel test //examples/basic:basic_test |
| 117 | +bazel test //examples/go_component:... |
96 | 118 | ``` |
97 | 119 |
|
98 | | -The toolchain infrastructure is now solid - the remaining work is resolving the Rust toolchain configuration to properly include WASI SDK tools in Rust build actions. |
| 120 | +All examples are **production ready** and demonstrate best practices for WebAssembly Component Model development! 🎉 |
0 commit comments