Generate C source code from JavaScript or TypeScript using Porffor’s 2c (Wasm → C) compiler.
2c is Porffor’s experimental WebAssembly to C compiler. The generated C code is functional but still in development.
Usage
porf c [flags] input.js [output.c]
When no output file is specified, C code is printed to stdout.
Examples
Output to file
Output to stdout
With optimization
TypeScript input
porf c script.js output.c
Arguments
JavaScript or TypeScript source file to compile
Output C source file (optional - prints to stdout if omitted)
Compilation Flags
Porffor optimization level (affects generated Wasm and C) porf c -O0 script.js output.c # No optimization
porf c -O3 script.js output.c # Maximum optimization
Include debug information
Preserves variable and function names
Adds comments and debug markers
More readable C output
porf c -d script.js output.c
Value type: i32 or f64 porf c --valtype=i32 script.js output.c
TypeScript Support
Enable TypeScript parsing
Use type annotations for optimization porf c --parse-types --opt-types script.ts output.c
Generated C Code
The output C code:
Is standard C (C99/C11 compatible)
Requires minimal dependencies (just math.h)
Can be compiled with any C compiler
Contains little boilerplate or preluded code
Example Workflow
# Generate C code
porf c script.js output.c
# Compile with any C compiler
gcc -lm -O3 output.c -o myapp
clang -lm -O3 output.c -o myapp
zig cc -lm -O3 output.c -o myapp
# Run the executable
./myapp
Output to Stdout
Useful for inspecting or piping:
View in pager
Count
Compile directly
Save and compile
Inspecting Generated Code
Readable output
Optimized output
View specific functions
# Use debug mode for readable C
porf c -d script.js output.c
Compilation Options
Once you have C code, compile it with various options:
# Basic
gcc -lm output.c -o myapp
# Optimized
gcc -lm -O3 -fomit-frame-pointer -s output.c -o myapp
# Maximum optimization
gcc -lm -Ofast -flto -march=native output.c -o myapp
# Debug
gcc -lm -O0 -g output.c -o myapp_debug
# Basic
clang -lm output.c -o myapp
# Optimized
clang -lm -O3 -fomit-frame-pointer output.c -o myapp
# Maximum optimization
clang -lm -Ofast -flto -march=native output.c -o myapp
# Debug
clang -lm -O0 -g output.c -o myapp_debug
# Basic
zig cc -lm output.c -o myapp
# Optimized
zig cc -lm -O3 output.c -o myapp
# Cross-compile
zig cc -target x86_64-windows -lm output.c -o myapp.exe
Use Cases
1. Understanding Compilation
Inspect how JavaScript is translated:
porf c -d simple.js | less
2. Custom Build Process
Integrate into existing C build systems:
# In Makefile
myapp.c: script.js
porf c $ < $@
myapp: myapp.c
gcc -lm -O3 $ < -o $@
3. Cross-Compilation
Generate C once, compile for multiple platforms:
# Generate C
porf c script.js output.c
# Compile for different platforms
gcc -lm output.c -o myapp_linux
x86_64-w64-mingw32-gcc -lm output.c -o myapp.exe
zig cc -target aarch64-macos -lm output.c -o myapp_mac
4. Manual Optimization
Edit generated C for specific optimizations:
porf c -d script.js output.c
# Edit output.c manually
gcc -lm -O3 output.c -o myapp
Code Structure
The generated C code typically includes:
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
// Memory management
static uint8_t * memory;
static size_t memory_size;
// Generated functions
void func_0 () { /* ... */ }
void func_1 () { /* ... */ }
// Main entry point
int main () {
// Initialize memory
// Call main function
return 0 ;
}
Advanced Options
Enable Cyclone optimizer (affects C output) porf c --cyclone script.js output.c
Log compilation performance porf c --profile-compiler script.js output.c
Comparing with Native Command
The porf c and porf native commands are related:
# These are equivalent:
porf native script.js myapp
# Is the same as:
porf c script.js temp.c
clang -lm -Ofast -flto temp.c -o myapp
strip myapp
Use porf native for convenience, porf c for control and inspection.
Maximum Porffor optimization
Debugging
Fast iteration
porf c -O3 --cyclone script.js output.c
gcc -lm -Ofast -flto -march=native output.c -o myapp
Limitations
Generated C is platform-independent but requires C compiler
No WASI or standard imports included
Requires math.h for mathematical operations
Not human-written quality (machine-generated)
Troubleshooting
C Compilation Errors
# Try debug mode for more readable C
porf c -d script.js output.c
# Check C compiler compatibility
gcc --version
clang --version
Missing Math Library
# Always link math library
gcc -lm output.c -o myapp
clang -lm output.c -o myapp
Large C Files
# Use optimization to reduce size
porf c -O3 script.js output.c
# Or compile with aggressive optimization
gcc -lm -O3 -ffunction-sections -fdata-sections -Wl,--gc-sections output.c -o myapp
Next Steps
Native Compilation Compile directly to executables
Wasm Output Generate WebAssembly instead
Profile First Optimize before compiling
Run Code Test before generating C