Running Your First Script
Let’s start with a simple “Hello World” example:Using the REPL
Porffor includes an interactive REPL (Read-Eval-Print Loop) for experimenting with JavaScript:Ctrl+C twice or type .exit to quit the REPL.
Compilation Modes
Porffor supports multiple compilation targets. Here’s how to use each one:Compile to WebAssembly
Generate a standalone.wasm file:
Compile to C
Compile JavaScript to C code using Porffor’s 2c compiler:Compile to Native Binary
Create a standalone executable binary:Native binaries are compiled with Clang at
Ofast optimization level by default and are automatically stripped for smaller file sizes.Customizing Native Compilation
Control the compiler and optimization level:Working with TypeScript
Porffor can compile TypeScript files with type annotations:Porffor parses type annotations but does not perform type checking. The
--opt-types flag uses types as compiler hints for optimization.Real-World Examples
Here are some practical examples from Porffor’s benchmark suite:Performance Timing
loops.js
String Methods
strings.js
Compiler Options
Porffor provides several flags to customize compilation:Parser Selection
When using
--parse-types without specifying a parser, Porffor automatically defaults to @babel/parser.Value Type
Choose the internal value representation:Optimization Levels
Control compilation optimizations:Advanced Features
Profiling
Profile your JavaScript code to identify performance bottlenecks:Debugging
Debug compiled JavaScript:Tips for Writing Porffor-Compatible Code
Since Porffor is experimental and has limitations, keep these tips in mind:Avoid async/await
Promise and async/await support has known bugs. Use synchronous code when possible.
Use explicit types
When writing TypeScript, use explicit type annotations to help the compiler optimize your code.
Check examples
Look at the files in the
bench directory of the Porffor repository for working examples.Keep it simple
Porffor supports a subset of JavaScript. Start with simple programs and gradually add complexity.
Common Issues
Limited API Support
Many JavaScript APIs are not yet implemented. If you encounter errors about unsupported features:- Check if the API or language feature is listed in the current limitations
- Look for alternative approaches using supported features
- Consider filing an issue on the Porffor GitHub repository
Scope Limitations
Variables cannot be accessed between scopes (except arguments and globals):No Dynamic Code Evaluation
eval() and Function() are not supported due to AOT compilation:
Next Steps
Now that you understand the basics of using Porffor, explore more:GitHub Repository
View the source code, examples, and contribute to the project
Join Discord
Ask questions and get help from the Porffor community