Inline C++ functions in Javascript

Inline C++ functions in Javascript

Author: Jos de Jong, 2011
License: Apache License, Version 2.0

Introduction

cpp2js is a tool which allows you to embed native C++ methods inside javascript. The tool is able to compile and execute embedded C++ code on the server side.

Defining and calling C++ methods can be done both via synchronous and asynchronous calls to the server.

Why?

There are various reasons why inline c++ code can be useful.

Risks

Yes, this is very risky: allowing to compile and run any kind of code on the server via a client side call... (Well, there is some safety: the limitations imposed by CGI)

The tool cpp2js is just a concept and is not safe to use on the real world wide web. Use this on your own risk.

Requirements

Download

Click the following link to download a zipped file containing sourcecode, executable, examples, and documentation.

cpp2js.tar.gz

Usage

To make it easy to use inline C++ code in your javascript, a method cpp2js(code {, callback}) is provided in the file cpp2js.js. Include this file in your webpage:

<script type='text/javascript' src="cpp2js.js"></script>
To create an inline c++ function, the function need to be defined. This can be done in two ways: synchroneously or asynchronously (preferred).

Synchronous

Creating a function in a synchronous way. This is not the preferred usage, as the browser will be blocked while a call to the server is being made, i.e. during compilation of the code and during evaluation of a function.

var code = "double sum(double a, double b) {return a+b;}";
var sum = cpp2js(code);    // compile the code on the server side
var ans = sum(2.3, 6.2);   // evaluate on the server side, returns 8.5
alert(ans);
See cpp2js_example_synchroneous.html for a fully working example.

Asynchronous

The best approach is to compile and evaluate inline c++ functions in an asynchronous way. The browser will stay responsive while compiling and evaluating code on the server side.

var code = "double sum(double a, double b) {return a+b;}";

function evaluateCallback(ans) {
  alert(ans);
}

function compileCallback(factorial, error) {
  if (factorial != undefined) {
    factorial(2.3, 6.2, evaluateCallback); // evaluate on the server side
  }
  else 
    alert(error);
}
cpp2js(code, compileCallback); // compile the code on the server side
See cpp2js_example_asynchroneous.html for a fully working example.

How does it work?

The GCI program cpp2js can be called with the code of a c++ function as argument in the url, for example:

/cgi-bin/cpp2js?double sum(double a, double b) {return a+b;}
cpp2js parses the function and creates a compilable c++ program from it. This code is compiled and stored as a standalone executable in the same folder as the program cpp2js itself. The compiled code gets a name cpp2js_xxx.cgi, where xxx is the MD5 hash of the code. This name is thus uniquely determined from the provided code. Before compiling code, cpp2js checks if this code is already compiled before, and if so, does not compile again but directly returns the result. So, only the first time a request with some specific code is made, the processing actually takes some time.

In the case of the above example, cpp2js returns the following filename for the compiled code:

cpp2js_e6c9eb22819190965f64f46ffd2490b6
From now on, this name can be used as url to execute the compiled function. The function parameters are passed in the url. For example:
/cgi-bin/cpp2js_e6c9eb22819190965f64f46ffd2490b6?a=2.1&b=4.5
Which returns:
6.600000
Note that all special characters in parameter values should be encoded (space, &, etc.).