n2liquid's sandbox

CXXJS: JS-augmented C++

Posted on: May 12, 2011

C++ is a declining love of mine. The language always meant two things for me: tidiness and speediness. It was when I read about a paradigm called SCOOP (Static C++ Object-Oriented Programming; see also SCOOP2) that I noticed my programming dreamland wasn’t possible using C++ alone. C++ is not tidy enough when you become obsessed for tidiness, and that little extra cutesy you want in your code hurts speed as much as speed is hurt all the time when programming in Java, and suddenly your C++ becomes Java: not that fast and absolutely not tidy anyway.

I definitely want to write my own language some time, but for now I’ll just mix and match shit up because it’s easier.

CXXJS is something I’m thinking about doing to make advanced C++ less tedious (because advanced C++ really is a daily thing for anyone who truly does C++). In short, CXXJS is like old C macros, but context-aware (less aware than, but similar to templates) and in JavaScript.

For example:

// Math.cxxjs
Math.Sum = function (a, b) { return a + b; }

// main.cpp
#include <iostream>
#include <Math.cxxjs> // gets wiped out by JS parser

int main()
    // the following becomes std::cout << 3 << '\n';
    std::cout << CXXJS::Math.Sum(1, 2) << '\n';

Since JavaScript has a pretty C-styled syntax, the inline JS reads very naturally.

My motivation came when reading V8-Juice‘s documentation on binding near-arbitrary functions to JS objects: why do I have to write [Code A] when [Code B] would look and feel so natural? And what about [Code C]?

// [Code A]
        (convert::FunctionForwarder<1>::Invocable<unsigned int,
                unsigned int,::sleep>)->GetFunction()

// [Code B]
// Things beginning with `CXXJS::` are JavaScript function calls
// or variable accesses and things inside CXX() are converted to
// strings by the parser; this is used to keep syntax coloring
    String::New("sleep"), CXXJS::Bind
        CXX(unsigned), CXX(unsigned), CXX(::sleep)

// [Code C]
// Here a supposed V8Object function decorates jsObj's interface
    "sleep", CXXJS::Bind(CXX(unsigned (unsigned)), CXX(::sleep))

The possibilities are quite unlimited. All this requires a bit of C++ (and JS) parsing though, which is known to be a nightmare, but maybe I should give it a try some day?

1 Response to "CXXJS: JS-augmented C++"

[…] explaining how to modify it, so I’m assuming it shouldn’t be hard to make it recognize CXXJS::* expressions and pass them to an external CXXJS translator to patch the original C++ […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Cute bouncing pixels


Twitter (technical)

Error: Please make sure the Twitter account is public.

Twitter (personal)

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Get messaged when I post something new!
Just enter your e-mail and hit Follow:

Join 171 other followers

%d bloggers like this: