npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nativejs-compiler

v2.0.0

Published

Write JavaScript software for your hardware.

Downloads

151

Readme

JavaScript/TypeScript to C transpiler

Produces readable C89 code from JS/TS code.

For example, this JavaScript:

console.log("Hello world!");

transpiles to the following C code:

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    return 0;
}

No excessive code that is not actually needed is ever generated.

The output is as readable as possible and mostly maps well to the original code.

Another example:

var obj = { key: "hello" };
obj["newKey"] = "test";
console.log(obj);

transpiles to the following C code:

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

struct obj_t {
    const char * key;
    const char * newKey;
};

static struct obj_t * obj;
int main(void) {

    obj = malloc(sizeof(*obj));
    assert(obj != NULL);
    obj->key = "hello";
    obj->newKey = "test";

    printf("{ ");
    printf("key: \"%s\"", obj->key);
    printf(", ");
    printf("newKey: \"%s\"", obj->newKey);
    printf(" }\n");

    free(obj);

    return 0;
}

Project status

Work in progress: it works, but only about 50% of ES3 syntax is currently supported.

Overview of currently supported language features (compared to ES3 Standard):

  • statements [76%]: var, if-else, do-while, while, for, for-of, for-in, continue, break, return, function, block, empty statement, expression statement
  • expressions [63%]:
    • primary expressions [80%]: variables; number, string, regex and boolean literals; array and object initializers; grouping operator
    • left-hand-side expressions [60%]: property accessors, function calls
    • postfix expressions [100%]: ++, --
    • unary operators [44%]: ++, --, +, !
    • multiplicative operators [50%]: *, /, %
    • additive operators [50%]: +, -
    • bitwise shift operators [33%]: <<, >>
    • relational operators [33%]: <, >, <=, >=
    • equality operators [50%]: ==, !=, ===, !==
    • binary bitwise operators [33%]: &, |
    • binary logical operators [100%]: &&, ||
    • conditional operator [100%]: ?-:
    • assignment operators [52%]: =, +=
    • comma operator [100%]
  • built-in objects [14%]:
    • Global [0%]
    • Object [0%]
    • Function [0%]
    • Array [78%]: push(), pop(), shift(), unshift(), splice(), slice(), concat(), join(), toString(), sort(), reverse(), indexOf(), lastIndexOf(), length
    • String [61%]: indexOf(), lastIndexOf(), search(), match(), charAt(), charCodeAt(), concat(), substring(), slice(), toString(), valueOf(), length
    • Boolean [0%]
    • Number [0%]
    • Math [0%]
    • Date [0%]
    • RegExp [0%]

Note: some of these features supported only partially. Detailed information about supported and planned features can be found in COVERAGE.md.

Notable NOT supported features include, for example: float numbers, this, new, function inside expression, typeof, etc.

Memory management is done via escape analysis.

Live demo

You can try it out yourself online:

  • https://andrei-markeev.github.io/ts2c/

Rationale

The main motivation behind this project was to solve problem that IoT and wearables cannot be currently efficiently programmed with JavaScript.

The thing is, for sustainable IoT devices that can work for a long time on single battery, things like Raspberry Pi won't do. You'll have to use low-power microcontrollers, which usually have very little memory available.

RAM ranges literally from 512 bytes to 120KB, and ROM/Flash from 1KB to 4MB. In such conditions, even optimized JS interpreters like JerryScript, Espruino or V7 are sometimes too much of an overhead and usually lead to the increased battery drain and/or don't leave a lot of system resources to your program.

Of course, transpiler cannot map 100% of the JavaScript language and some things are have to be left out, for example eval and try-catch. Still, current conclusion is, that it is possible to transpile most of the language.

These are some examples of planned target platforms for using with TS2C:

Usage

Syntax:

ts2c <files to transpile>

In browser (also see index.html file):

<script src="https://unpkg.com/typescript"></script>
<script src="ts2c.bundle.js"></script>
<script>
    var cCode = ts2c.transpile("console.log('Hello world!')");
    alert(cCode);
</script>

In Node.js:

const ts2c = require("ts2c");
const cCode = ts2c.transpile("console.log('Hello world!')");
console.log(cCode);