1. Technology

Using JavaScript in Your C++ Applications

By January 26, 2009

Follow me on:

When Google released their Chrome browser, they included a very fast implementation of JavaScript called V8, the client side scripting language included in all browsers. My first encounter with JavaScript was ten years ago back in the era of Netscape 4.1 and to be honest I hated the language then. Why? Because there were no tools for debugging, each browser had different implementations (and different versions of Netscape browsers differed as well). It wasn't pleasant writing cross-browser code and testing it on lots of different browsers.

But since then, Google Maps and Gmail came along a few years ago using the whole Ajax (Asynchronous JavaScript and XML) technologies and JavaScript has had a major comeback and there are now one or two decent tools for it. Google's V8 (written in C++) compiles and executes JavaScript source code, handles memory allocation for objects, and garbage collects objects it no longer needs. These design details explain why V8 is so much faster than the JavaScript in other browsers. In short because it compiles to native machine code not bytecode that's interpreted.

If your C++ application requires scripting, i.e. for users to be able to write code that executes at run-time then you could consider embedding V8 in your application. It's licensed under the liberal BSD license. Google have even provided an embedder's guide.

Here's a simple example that Google provides. The classic Hello World in JavaScript (in C++!)

int main(int argc, char* argv[]) {

  // Create a string holding the JavaScript source code.
  String source = String::New("'Hello' + ', World'") ;

  // Compile it.
  Script script = Script::Compile(source) ;
  
  // Run it.
  Value result = script->Run() ;

  // Convert the result to an ASCII string and display it.
  String::AsciiValue ascii(result) ;
  printf("%s\n", *ascii) ;
  return 0;
}

Comments
January 27, 2009 at 2:11 am
(1) Oliver says:

Nice article, keep up the good work!

April 4, 2010 at 10:36 am
(2) Takashi says:

Very useful. thanks for sharing this

November 25, 2010 at 10:31 am
(3) Ed says:

Hi, nice example. But, how do i call that Script::Compile?? how do i initialize it? Thanks

August 25, 2011 at 8:32 pm
(4) Gregaw says:

“-D, David, it works great, -)

November 26, 2012 at 4:12 am
(5) suman says:

hey,
can we write any kind of js source code . Ho will the http calls inside js will get execute?

TIA

March 7, 2013 at 11:29 pm
(6) Anand says:

Libjspp C++ template based wrapper for embedding and extending Javascript engine spidermonkey 1 . 8 . 5 and more

SpiderMonkey? is Mozilla Project’s Javascript/ECMAScript engine.

Libjspp allows C++ developers to embed SpiderMonkey? simply and easily into their applications. Libjspp allows to run multiple Javascript Engines within same process which suits one engine per thread para dime which is helpful in achieving true parallisim. Also Libjspp no way stops user from running multiple threads within engine.

Motivation for writing this library was there does not exist a decent library to wrap Javascript spidermonkey engine , SpiderApe?, Flusspferd are old and are not being maintained and are not up to date with new spidermonkey 1.8.5 API

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.