javascript and OF

this was wrapping an OF class in javascript from a while back:

I was in touch a while back with v3ga, gonna pick that up again. I’m super interested in javascript wrappers - having some higher level integration ability would be make things alot quicker. you could write the analysis code (ie, the stuff that has to be fast) in c++ and then the behavior / graphics / responsive code in a higher level language that you could reinterpret on the fly.


oF works excellently with Google’s JavaScript engine, V8. I really like V8 because it’s easy to wire up to C++ code, has excellent support for multiple processes as well as multiple threads, and is very, very quick, since it compiles down to bytecode on the fly.

I simply compiled it using the instructions at their Download and Build page (, then included the source files and the generated libV8.a file in my XCode project.

I will try and make a sample project and post it to my GitHub.

hey rtaljun, i’d surely love to see an example of that.
whatever brings some inmediacy to coding in c++ is welcome :slight_smile:…-ble-for-a-

V8 is pre-compiled for OS X. There are links to V8 documentation in the blog post. I intend on writing a couple more blog posts in a series.

i think, for beginners javascript is not a good learning language. its far to dynamic and meta-ish to write efficient code without know any knowledge. :mrgreen:

this is really interesting – thanks for posting this.

on osx, for me it’s not linking properly – maybe I need some other libraries?

Undefined symbols:
“_backtrace”, referenced from:
v8::internal::OS::StackWalk(v8::internal::OS::StackFrame*, int)in libv8.a(platform-macos.o)
“_backtrace_symbols”, referenced from:
v8::internal::OS::StackWalk(v8::internal::OS::StackFrame*, int)in libv8.a(platform-macos.o)
“_usleep$UNIX2003”, referenced from:
v8::internal::OS::Sleep(int)in libv8.a(platform-macos.o)
ld: symbol(s) not found
collect2: ld returned 1 exit status

take care ~

Are you running on PPC or x86? For my example, I compiled it for x86, not Universal Binary, IIRC. Sorry about that :¬(

If you go into the v8 directory and follow the instructions on the v8 installation page to compile it, that should be enough to update the project. The Xcode project references the header files and library files it needs, within that directory. In my personal experience found that v8 was even easier for me to compile than Apple’s own JS engine, JavaScriptCore!

I do apologize that this doesn’t work for you, I will try and see if I can play with compiler flags or maybe even load the v8 project itself into Xcode.

Also, as a couple people have mentioned, this project is empty, so it shows a grey screen if it launches correctly. I intend on making a follow-up article showing how to define your own JS functions (probably map them to OpenGL or oF’s drawing libraries).

I wanted to quickly share my opinions on why JS makes a great game scripting language, and why I think C++ with JS is more reasonable for intermediate programmers than pure C++:

  • V8 is very efficient. Each eval takes place in its own thread and each runtime exists in its own process and the JS code is compiled “Just-in-time” down to machine code. V8 is quick enough to eval JS within a game loop.

  • V8 garbage collects its variables, as well as C++ variables binding to JS variables. When communicating between C++ and JS, this allows you to code without thinking about memory management as much.

  • Functional programming constructs allow you to write much simpler code. For example,

glRotatef(PI / 2, 1, 0, 0);  
ofRect(0, 0, ofGetWidth(), ofGetHeight());  

can be rewritten:

matrix(function() {  
  glRotatef(PI / 2, 1, 0, 0);  
  ofRect(0, 0, ofGetWidth(), ofGetHeight());  

(although you would have to define matrix() somewhere):

function matrix(yield) {  

Note that any variables within that anonymous function are form a closure and variable defined within the anonymous function fall within its scope. This can help keep code more self-contained.

  • JavaScript allows users to define their own code! Did you ever want to write a game where users can script their own enemies, and weapons?

  • JSON, a data interchange format used for similar use cases as XML, is valid JS code and can be parsed on the fly. I actually use JS interpreters in my projects to parse JSON and hand the data back to C++. This allows me to define data structures in a compact, human-readable format, that is easily parsed by my application.

  • JavaScript code is relatively easy to port to and from ActionScript (both JS and AS are dialects of a language called ECMAScript). If you decide to port your game to Flash, or if you’re trying to port a game from Flash, JavaScript can shorten the translation process.

  • Please, please, please note that JS as a scripting language is barely related to browser JS. In no way do I suggest people code games like they would for a browser, using HTML, JS, CSS, and a web service. That is pure, pure insanity. At least until Google’s O3D becomes more popular :¬P

hey rtaljun, this was not my point

i come with a 5 years javascript background on day-jobs. all directions js is taking with v8 and the latest vm-states definitely let me think more of the serverside js and the language itself and not a browser-buggy ECMAScript hack…

however my point was, its not userfriendly to beginners. js is very abstract when you just begin to learn simple funtions, vars. js brings *, scopes/blocks, callbacks, events and misses others. this is not bad - as ‘higher-level’ the language is more useful pattern ship with it. so my rant is: one can ony write decent javascript when its not his first language :smiley:

please dont get me wrong, v8 and openframeworks sound cool!! still, javascript is ugly and not enjoyable.


Apologies Lian, for misunderstanding where you are coming from. I am enthusiastic at the possibilities of scripting languages and JS seems to be a pragmatic choice at this time, though I do agree that its object system is “insecure” and a bit complicated! :¬)

Zach—I forgot to mention that V8 has an .xcodeproj file associated with it. Look in the v8/tools directory. You can just double-click on that, then select “Build” from the Build menu.

I am trying to make an addon at this moment, though I might hit some snags making it cross-platform. I guess I can research the Lua addon code.

so I tried too and got the same errors as zach on a macintel 10.5.6. I even installed scons and built the v8 engine too, but to no avail.

is there anything I have to do with the project so it’s aware of the newly compiled v8?

all in all, very exciting to see a proof of concept.


did you look at dim3? i played with it some years ago… it applies JS to 3d games like you suggested, this might help.

dim3, also known as Dimension 3, is a freeware open-source 3D game engine for Mac OS X, created by Brian Barnes. It has been chosen as a staff pick for OS X development software by Apple and featured as one of their "hot game building tools."dim3 has an entry in DevMaster’s 3D engines database.

dim3 uses OpenGL for rendering, OpenAL for audio, JavaScript for scripting (using Mozilla’s SpiderMonkey implementation), XML for data and Simple DirectMedia Layer for resolution switching and input.

Changing the source code is not required; all facets of the engine can be altered through scripting. Advanced developers are welcome to alter the code if they see the need.

dim3 was cool, and included some clever usage of js. its just kinda limited… like these flash multitouch apps, sounds cool and work fairly well - only to a certain point.


Lian—thanks for the cool link!

I will post a different sample project and better instructions this evening. I was getting linker errors and then I moved some stuff around in my Xcode projects’ groupings, then it worked. So I will start from scratch and hopefully avoid some of these linker issues.

Sorry about that, you guys!

Quick update—I think I have a working Xcode project for OpenFrameworks, embedding JavaScriptCore!

JavaScriptCore is Apple’s embedded JavaScript engine. It is perfectly reasonable for most projects. It is used in Safari for OS X and for the iPhone.

Can someone try building this and seeing if it works? If it runs correctly, a grey screen will appear, and the following will be output to the console:

Let's test some simple arithmetic from JS!  
What's 1 + 2?  
JS> 3  

Hopefully this works better than my previous V8 example. I’m pretty sure I tracked down the linker problem—I think some paths were hard-coded into the project. I will try and publish a fixed version soon. Obviously I want to eventually make addons out of one of these libraries… :¬)

yep. that one compiles out of the box… I’m still not very sure what I can/want to do with this, but it sets the ground for some experimentation. so thanks a lot.


If I can get something which compiles reliably and will work on more than one platform, I will make JS bindings for oF.

At some point I will also post some sample code for the oF+JS sidescroller I’m working on :¬) I’m not quite confident enough in it yet to share it with others :¬/

this is all extremely interesting.

some more potential avenues:

HI guys! Im little confuse where ask this but at least there is some javascript involved, so… How I could lunch safari with size and location, I’m using this address on the text navigator and it works:
–> javascript:self.moveTo(0,0);self.resizeTo(1200,1100);self.location=""

so I’ve tried with this hacked ofLaunchBrowser for mac:

	string urlOF = "[";](";)  
	string javascriptcommand = "javascript:self.moveTo(0,0);self.resizeTo(1200,1100);self.location="+urlOF;  
	string commandStr = "open "+javascriptcommand;  

but as I suspected doesn’t run