Overview
Porffor can compile JavaScript files directly to WebAssembly (Wasm) binaries using its ahead-of-time (AOT) compilation approach. The generated Wasm modules are optimized and self-contained, with minimal imports.Porffor-generated Wasm does not currently use an import standard like WASI, so standalone usage is limited. The modules work best when run through Porffor’s runtime or imported into compatible environments.
Basic Usage
Thewasm command compiles JavaScript to a WebAssembly binary:
Compile to WebAssembly
Use the Porffor will parse, optimize, and compile your JavaScript into a WebAssembly binary.
porf wasm command to generate a .wasm file:Understanding Wasm Output
Module Structure
Porffor generates spec-compliant WebAssembly modules with:- Zero constant runtime code: No preluded JavaScript runtime
- Minimal imports: Only I/O operations require imports
- Optimized bytecode: Includes multiple optimization passes
- Type-aware compilation: Uses internal type information for efficient code generation
Wasm Proposals Used
Porffor uses widely-supported WebAssembly proposals:- Multi-value (required): Functions can return multiple values
- Non-trapping float-to-int conversions (required): Safe numeric conversions
- Bulk memory operations (optional): Efficient memory copying
- Exception handling (optional): For error handling
- Tail calls (opt-in): Disabled by default, enable with
--tail-calls
Working with Compiled Wasm
Inspecting Generated Code
Use the-f flag to see disassembled WebAssembly for your functions:
Disassembly Mode
For detailed debugging, compile with the-d flag to include names and debug information:
- Preserves function and variable names in Wasm
- Adds source location mappings
- Enables better debugging in Wasm tools
Optimization Levels
Control compilation optimization with the-O flag:
-O0: No Optimization
-O1: Basic Optimization (Default)
- Instruction simplification
- Dead code elimination
- Wasm import tree-shaking
-O2: Advanced Optimization
- All -O1 optimizations
- Partial evaluation (Cyclone optimizer)
- Constant folding
- More aggressive inlining
Real-World Examples
Example: String Operations
strcat.js
Example: Computational Code
loops.js
--fast-length flag enables non-compliant optimizations for faster array length access.
Advanced Compilation Options
Parser Selection
Choose which JavaScript parser to use:Default is
acorn. Babel parser is required for TypeScript support.Memory Configuration
Adjust page size for memory management:Valtype Selection
Performance Considerations
What Compiles Well
Porffor excels at:- Numeric computations
- String manipulation with ASCII/Latin-1 characters (ByteString optimization)
- Array operations with known types
- Loop-heavy code
- Pure functions without dynamic behavior
What to Avoid
For optimal Wasm output:- Minimize dynamic property access on objects
- Avoid
eval()andFunction()(not supported in AOT compilation) - Reduce polymorphic code paths
- Use typed arrays when possible
- Prefer iteration over complex object traversal
Integration with Other Tools
Using with wasm-opt
Further optimize with Binaryen’swasm-opt:
Using with wasm2wat
Convert to WebAssembly text format for inspection:Troubleshooting
Compilation fails with parse errors
Compilation fails with parse errors
Check your JavaScript syntax. Porffor supports most ES6+ features but has limitations:
- No async/await in some contexts (known bugs)
- No eval() or Function() constructor
- Limited scope handling (variables between scopes except args and globals)
-d for better error messages:Generated Wasm is too large
Generated Wasm is too large
Use optimization flags:Check if you’re creating many large objects, as object-heavy code generates more Wasm.
Runtime errors in generated Wasm
Runtime errors in generated Wasm
Enable debug mode and inspect the output:This includes names and shows generated instructions for debugging.
Next Steps
Native Compilation
Compile JavaScript to native executables
Optimization Strategies
Learn how to write faster code
Debugging
Debug compiled WebAssembly code
TypeScript Support
Use TypeScript with Porffor