compile() function is Porffor’s main compiler API that transforms JavaScript code into WebAssembly.
Function Signature
JavaScript source code to compile
Whether the source should be treated as a module. When
true, enables module features like import/export. Defaults to script mode.Function to use for printing output (used by
console.log, etc). Receives a string argument.Default: str => process.stdout.write(str)Return Value
Object containing exported functions from the compiled code. Each function returns the JavaScript representation of Porffor values.
main- The main entry point (renamed fromm)$- WebAssembly memory instance- Other named exports from the source code
Raw WebAssembly binary (Wasm bytecode)
Array of compilation timing metrics:
[0]- Compilation time in milliseconds[1]- Instantiation time in milliseconds
Number of WebAssembly memory pages allocated
Generated C code (only when using C target)
Usage
Basic Compilation
Script Mode
Custom Print Function
Accessing Compilation Metrics
Advanced Usage
Compiling from Existing AST
You can also pass a pre-compiled object instead of source code:Exception Handling
The compiled functions throw JavaScript exceptions that match the behavior of the original code:Performance Notes
- Compilation happens at runtime and may take time for large codebases
- The
timesarray provides metrics for profiling - Set
Prefs.profileCompiler = truefor detailed compilation logging - Use
Prefs.disassemble = trueto get WebAssembly disassembly output
See Also
- Module API - For module-specific compilation features
- Porffor.wasm - Low-level WebAssembly operations