Skip to content

brunobasto/native.js

Repository files navigation

native.js

CI License: ISC

Compile a focused subset of JavaScript and TypeScript into readable C for embedded targets.

native.js is built for microcontrollers that need predictable native code, low overhead, and output you can actually inspect. There is no JS runtime on-device: the compiler turns supported TS/JS into C, then target presets adapt that C to a platform such as Arduino AVR or RP2040.

Why native.js

  • Write firmware in TypeScript syntax instead of hand-authoring every detail in C
  • Keep generated output readable enough to debug, audit, and integrate into existing toolchains
  • Reuse a compiler core across multiple hardware targets through presets
  • Add hardware features incrementally instead of shipping a full interpreter or VM

Targets

  • nativejs-preset-standard - base C runtime helpers and standard plugins
  • nativejs-preset-arduino - Arduino-oriented headers, plugins, and examples
  • nativejs-preset-rp2040 - RP2040 / Pico SDK support with GPIO, timing, PWM, ADC, I2C, SPI, and build helpers

Quick Start

Install dependencies:

yarn install

Build the compiler and presets:

yarn prepare

Run lint and tests:

yarn lint
yarn test

Example

TypeScript input:

declare var GPIO: any;
declare var sleep_ms: any;

const LED_PIN = 25;
GPIO.init(LED_PIN, "output");

while (true) {
  GPIO.write(LED_PIN, true);
  sleep_ms(500);
  GPIO.write(LED_PIN, false);
  sleep_ms(500);
}

This can be compiled with the RP2040 preset into C that plugs into the Pico SDK build flow. See packages/nativejs-preset-rp2040/examples/src/blink.ts.

Repository Layout

  • packages/nativejs-compiler - AST traversal, type inference, template-based C generation, CLI
  • packages/nativejs-preset-standard - base headers, helpers, and standard library-style plugins
  • packages/nativejs-preset-arduino - Arduino target support and examples
  • packages/nativejs-preset-rp2040 - RP2040 target support, Pico SDK integration, and examples

RP2040 Support

The new RP2040 preset includes:

  • console.log via Pico stdio
  • GPIO.init, GPIO.write, GPIO.read
  • sleep_ms, millis, micros, setInterval
  • ADC.read
  • PWM.init, PWM.write
  • I2C byte, buffer, and register helpers
  • SPI byte, buffer, and register helpers

Useful entry points:

  • packages/nativejs-preset-rp2040/README.md
  • packages/nativejs-preset-rp2040/examples/src
  • packages/nativejs-preset-rp2040/scripts/pico-sdk.js

Architecture

The project is structured as:

  1. TypeScript/JavaScript source
  2. TypeScript compiler API traversal and type analysis
  3. Native type mapping and template-driven C emission
  4. Target preset injection for headers, plugins, setup code, and platform helpers
  5. Platform-native build system such as AVR tooling or the Pico SDK

Status

native.js is still experimental, but the repository now has:

  • modernized tooling and CI
  • a working compiler test suite
  • multiple target presets
  • compile-time validation for RP2040 examples

The language surface is intentionally a focused subset rather than full JavaScript compatibility.

Contributing

Good next contributions include:

  • new target presets
  • broader language feature coverage
  • more peripheral support for RP2040 and Arduino
  • compiler output and type-system fixes

If you are evaluating the repo, start with packages/nativejs-compiler/README.md and packages/nativejs-preset-rp2040/README.md.

About

Write JavaScript software for your hardware.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors