A JavaScript runtime built with Go, powered by goja.
Gojo is a lightweight JavaScript runtime that allows you to execute JavaScript code using Go. It implements an event loop with support for asynchronous operations like setTimeout and setInterval, making it suitable for running Node.js-style JavaScript code.
- JavaScript Execution: Run JavaScript files with ES6+ syntax support
- Event Loop: Full implementation of event loop for asynchronous operations
- Timer APIs: Support for
setTimeout,setInterval,clearTimeout, andclearInterval - Console API: Built-in
console.logfor output - Process API: Access to command-line arguments via
process.argvandprocess.exit()
go get github.com/nomad-pixel/gojogo run cmd/gojo/main.go <path-to-js-file>Create a JavaScript file examples/hello.js:
console.log("start");
setTimeout(() => {
console.log("timeout 1 (100ms)");
}, 100);
const a = () => {
console.log("timeout 2 (50ms)");
}
let count = 0;
const id = setInterval(() => {
console.log("interval tick", ++count);
if (count >= 3) {
console.log("clearInterval");
clearInterval(id);
}
}, 200);
a();
console.log("end");Run it:
go run cmd/gojo/main.go examples/hello.jsconsole.log(...args)- Print values to stdout
process.argv- Array of command-line argumentsprocess.exit(code)- Exit the process with the specified code
setTimeout(callback, delay)- Execute callback after delay in millisecondssetInterval(callback, delay)- Execute callback repeatedly with delay between executionsclearTimeout(id)- Cancel a timeoutclearInterval(id)- Cancel an interval
Gojo is built with the following components:
- Runtime (internal/runtime/runtime.go) - Main runtime that initializes the VM and global APIs
- Event Loop (internal/runtime/loop.go) - Implements the event loop and timer management
- CLI (cmd/gojo/main.go) - Command-line interface for running JavaScript files
- Go 1.22.6 or higher
MIT