Skip to content

Gojo is a JavaScript runtime powered by Go using the goja JavaScript engine. It provides a Node.js-like environment for executing JavaScript files with support for event loops, timers, and common APIs.

License

Notifications You must be signed in to change notification settings

nomad-pixel/gojo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gojo

Gojo Demo

A JavaScript runtime built with Go, powered by goja.

Overview

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.

Features

  • 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, and clearInterval
  • Console API: Built-in console.log for output
  • Process API: Access to command-line arguments via process.argv and process.exit()

Installation

go get github.com/nomad-pixel/gojo

Usage

Running a JavaScript file

go run cmd/gojo/main.go <path-to-js-file>

Example

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.js

API Reference

Global Objects

console

  • console.log(...args) - Print values to stdout

process

  • process.argv - Array of command-line arguments
  • process.exit(code) - Exit the process with the specified code

Timer Functions

  • setTimeout(callback, delay) - Execute callback after delay in milliseconds
  • setInterval(callback, delay) - Execute callback repeatedly with delay between executions
  • clearTimeout(id) - Cancel a timeout
  • clearInterval(id) - Cancel an interval

Architecture

Gojo is built with the following components:

Requirements

  • Go 1.22.6 or higher

License

MIT

About

Gojo is a JavaScript runtime powered by Go using the goja JavaScript engine. It provides a Node.js-like environment for executing JavaScript files with support for event loops, timers, and common APIs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages