• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    CoffeeScript
  • Created over 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A toy js -> c++ compiler written in coffeescript. Uses escodegen to write c++ and tern to figure out types.

Build Status

What's this?

It's the product of the crazy idea of using the very excellent tern.js project to figure out the types of things and use them!

js2cpp doesn't have a garbage collector yet. But it is usable and can run some JavaScript code as is!

How can I use it?

Yeah, it actually does some things. This is how js2cpp handles strings:

$ echo '"lol" + "lel"' | bin/js2cpp
#include "js2c.h"
#include <string>

String("lol") + String("lel");

And here's console.log!

$ echo 'console.log(1, 2, "3")' | bin/js2cpp
#include "js2c.h"
#include <string>

console.log(1, 2, String("3"));

It works because there's a "console" instance of a "Console" class with a variadic "log" function.

Here is tern.js figuring out a couple of types.

$ echo 'var aNumber = 1; var aString = "1";' | bin/js2cpp
#include "js2c.h"
#include <string>

double aNumber = 1;
String aString = String("1");

How do I run this? (Usage)

 ~ β™₯  bin/js2cpp -h
Usage: js2cpp --run (run javascript from stdin)
Usage: js2cpp --run filename.js
Usage: js2cpp (compile from stdin)
Usage: js2cpp filename.js (compile)

environment variables:
 ~ $ RUN_VALGRIND=1 js2cpp --run ...    - Run the compiled program with `valgrind`
 ~ $ GPP_BINARY=/path/to/g++ js2cpp ... - Select what g++ binary to use (defaults to `g++`)
  • First, you need to update your compiler to a version that supports C++14. Your system probably already has this. If not, install linuxbrew or homebrew and just run brew install gcc
  • Then, clone this repo and run npm install
  • Optionally npm test just to make sure it works on your machine ;) If it doesn't please make an issue about it and I'll try to look into it. It tries to use the g++ binary from your PATH. If you want to use another binary specify it with the GPP_BINARY environment variable, like so: GPP_BINARY=g++-5 npm test.
  • To compile some javascript, run ./bin/js2cpp < your-javascript.js > your-cee-plus-plus.cpp.
  • To run it, run ./bin/js2cpp --run your-javascript.js, or ./bin/js2cpp --run, type in javascript, then press ^D when you're done. The GPP_BINARY variable also applies here.

Dumbjs

js2cpp is kind of a frontend to fabiosantoscode/dumbjs, a javascript simplifier. It turns javascript that looks like this:

function foo(x) {
    x++
    return function () { return x }
}
console.log(foo(0)())

Into something like this:

var _flatten_0 = function (_closure) {
    return _closure.x
}
var foo = function (x) {
    var _closure_0 = {}
    _closure_0.x = x
    _closure_0.x++
    return BIND(_flatten_0, _closure_0)
}
var main = function () {
    console.log(foo(0)())
}

(the BIND function must be defined elsewhere, in this case in js2cpp)

Its features are closure simulation, main-ification (put stuff in a main() function), function de-nesting, and more will come.

Since all of it is a much simpler form of javascript, it takes a huge weight off the shoulders of js2cpp and makes sure its code remains (somewhat) understandable by not mixing up concerns.

This also means that you can use dumbjs to transpile javascript to other languages. Most of its features are switchable, because not every transpilation target language will need you to do things like wrapping things in a main() function or simulating closures.

Roadmap

(unordered)

The main objective of this project is to implement at least 80% of the parts of javascript you use every day.

This means that, like a lot of npm modules which accidentally work in the browser when browserified, a lot of npm modules should accidentally work natively when js2cpp-ified.

All of the following features may be implemented in dumbjs, in this project, or in both at the same time, depending on whether they apply to each project.

  • Plug in the Boehm-Demers-Weiser conservative garbage collector (which works in C and C++, it seems!)
  • Rewrite this and dumbjs in pure javascript
  • Implement arguments and this
  • Import the most common modules from node core (mostly depends on the above), rewrite parts of them in C++ if necessary.
  • Implement promises
  • Implement boxed types, for those variables which can have 2 or more types
  • Implement JSON.parse() and JSON.stringify() (depends on the above)
  • Implement libuv bindings and shim, fake and steal node's IO APIs
  • Implement ES5-style classes (possibly by turning them into ES6-style classes first)
  • Implement or fake commonJS (require(), module.exports) modules.
  • Implement several javascript APIs such as Date, Symbol, and Array (which currently is simply a C++ std::vector).

Hey this is AMAZING I WANNA FORK IT SELL IT OR WHATEVER WHAT IS TEH LICENSSSS

Just have fun with it! WTFPL

More Repositories

1

require-emscripten

Require emscripten'd files in node and in the browser
CoffeeScript
38
star
2

tjsx

Like mikeal's bel but for React. Tired of compiling JSX in development? Use this!
JavaScript
16
star
3

magicProxy

A proxy for front-end web development. It changes responses from remote websites
JavaScript
12
star
4

cycles-xml-exporter

Blender to standalone Cycles exporter.
Python
11
star
5

dumbjs

A first pass for js2cpp. Uses browserify's dependencies to flatten the dependency tree into a single file, then makes the file not use closures at all by implementing closures in pure javascript.
JavaScript
7
star
6

inotifydo

Wait for files to change, execute shell command, repeat
Shell
4
star
7

powproxy

Rust
4
star
8

mp

Simple multiplayer deathmatch game written in javascripts
JavaScript
4
star
9

infralang

JavaScript
2
star
10

ecmacomp

JavaScript
2
star
11

django-queryset-serialization

Because pickling Django querysets is more trouble than it's worth.
Python
2
star
12

blender-cloudrender

JavaScript
2
star
13

preloadFn

Allows to create a function that preloads some data but only applies it when .apply is called
TypeScript
2
star
14

sjedon

A JavaScript virtual machine written in JavaScript.
JavaScript
1
star
15

persistent-lang

A language whose state can be persisted to disk, through the use of a fully-JSON state object
JavaScript
1
star
16

veerc

It's a secret!
JavaScript
1
star
17

horsemouth

the ES spec impl straight from the horse's mouth (?)
HTML
1
star
18

js-cripple

A little library which messes with your object prototypes so as to remove features you might be relying on, but aren't available in most browsers.
JavaScript
1
star
19

precommitify

JavaScript
1
star
20

crpy

CrowdProcess Python API Client
Python
1
star
21

code-editor-elm

Elm
1
star
22

joomla-auto-menus

Automatic menus for Joomla! CMS 2.5
PHP
1
star
23

fruity-background-generator

Generates data:url in-CSS PNG tileable graphics of stripes.
JavaScript
1
star
24

expense-o-tron

JavaScript
1
star
25

lnwtf

For those who can never remember the argument order of ln!
JavaScript
1
star
26

python-stack-traces

Better stack traces for python
Python
1
star
27

worklet

Simpler API for the Worklet W3C API
JavaScript
1
star
28

wentto

Some kind of goto using a sequence of functions. Can't break out of nested loops but you can use a labelled break for that.
JavaScript
1
star
29

dbus-verbose-format-parser

Parse the data format that comes out of dbus-monitor and dbus-send. Unmaintained, barely tested, surely incomplete.
JavaScript
1
star
30

mochallel

Mocha tests running in parallel
JavaScript
1
star