JavaScript Bindings

Hi guys,

sorry for announcing this so late, but here it goes:

My name is Rolando and I’ve been working on the cocos2d-x-javascript bindings for a while. We (Zynga) released as open source the branch old-js-bindings, that was using JavaScriptCore last week and right now we’re openly working on a new, better, improved, faster, javascript bindings using SpiderMonkey. You can check the progress in my personal fork (I’ll be merging code to the cocos2d-x project as it gets stable):

Why we moved from JavaScriptCore to SpiderMonkey?

Apparently everyone is using JavaScriptCore, but although that seems to be true, they are all infringing the LGPL license (actually Apple is infringing the License), so we searched for other JavaScript VMs and decided that SpiderMonkey was a good candidate. We got it working on iOS and Android fairly easy:

And we’re using that on the new bindings.

How do the new bindings work?

We’re going to build them in a two step process: we want to create a simple script that will parse the cocos2d.h header file and automagically create the bindings for cocos2d-x (or any other C/C++ project for that matter). We’re half-way through with this script and you can check the progress in my fork. The script is using clang to produce an xml for the AST, so that makes it very powerful. This script will only get us that far: producing a 1-1 mapping of the API. Although that’s a good thing, is not what we’re aiming, we want to have a very javascript-friendly and awesome API, leaving room for a future where you could code your game in javascript and run it natively (using cocos2d-x) on your device, or using another port of cocos2d on the browser.

The second part will be written in javascript, we’re going to take the 1-1 API conversion and we’re going to wrap it to make it more appealing for javascript developers. We’re going to start on that as soon as we finish the automatic bindings.

As you can see, we decided to move with this in the open source model, so you can join us in the fun and contribute (either just testing, bug reporting, or with code) to this project to make it even better.

What is working today?

We have a working SpiderMonkey for iOS and Android and basic autobindings for properties (fundamental, pointer and reference types), classes and a few other things. We’re adding functions/constructors support as I write this, so that should be there pretty soon. You can check the Readme in the js directory in the js-bindings branch to test it.

Anyway… that’s it, I hope you’re all as excited as we are regarding this project and hopefully you’ll be coding your games in javascript pretty soon

Rolando

Oh, also… since we’re using clang to parse the headers, we’ll try to do the same for cocos2d-iphone.

Enjoy!

Great job! gles20 branch is also improving very fast. We can probably merge these 2 branches in April, if each of them are mature, then launch a super release. :slight_smile:

very cool! i’m trying it out.

a more functional version is on my fork:

currently working:

CCPoint CCSize CCRect CCDirector CCNode CCSprite
CCScene CCSpriteFrameCache CCSpriteFrame CCAction
CCAnimate CCAnimation CCRepeatForever CCLayer
CCTouch CCSet

check the JS-test for a sample javascript demo.

Dang, sounds extremely exciting.

How long do you think it would take to get to a functional state where we can use Javascript to develop for iOS, Android, and HTML5?

for iOS and Android that should be soon (couple of weeks?). For the HTML5 side… I would say more, but maybe the cocos2d-x team has more insight in that.

For now, here’s a sneak peak (iOS only, Android following soon

Enjoy)

Would be possible to add non-cocos2d-x clases to the build?

Like Ad-related librarys (admob/tapjoy/etc…)

I’m very interesting in the JS binding, but I need to know If I could extend the funcionality (easily).

I apologiced for my english.

Probably you need to write js wrappers for cpp, and cpp wrappers for objective c/ java

It is possible to use extend the functionally easily. The current bindings are being generated automatically using the cocos2d.h header file, and our purpose is to do the same with other libraries.
Currently you need clang-2.8 and it only works for C++ (so far, we’re trying to extend it for obj-c as well), you can then give a look at the generate_cocos2d_xml.sh script, which basically runs clang and outputs an XML version of the AST:

https://github.com/funkaster/cocos2d-x/blob/js-bindings/js/generate_cocos2d_xml.sh

that xml file is then feeded to the generate_bindings.rb script that will create the proper .hpp and .cpp creating javascript classes, methods, etc. There are some conventions that I’ll describe in a document soon, but it works for most of the cases. It will not produce a “pretty” API, but is a great first step. You can then write a simple JS wrapper if you want to make it more js-friendly. That’s the way we’re approaching to release a fully functional cocos2d-x API.

Now, if you don’t want to generate the bindings automatically, you can always use the SpiderMonkey API (which is very nice!) to create your custom bindings:

hth,
Rolando

Thank’s Rolando, I will begin using the JS binding on my next project. Any date for the Android version?

Later this week we’ll have Android support, I’ll update this thread as soon as we have that ready.

Rolando, gles20 branch of cocos2d-x is much improved recently. FishingJoy2 is using this branch, so I’m sure that it’s mature enough. Could you bind js onto gles20, we can abandon the js-binding feature on 1.x. I saw that CocosBuilder v1.1 and higher only supports cocos2d 2.0

Moving to the gles20 branch should be straight forward: I assume very little external API has changed, and I didn’t modify any part of cocos2d-x internals, so migrating to that should be easy. I’m focusing right now on having a working javascript bindings and then I’ll move the code to the new branch.

Will the JavaScript bindings work with the Windows 8 Metro cocos2d branch? Thanks!

@Paul, We have no plans to run jsbinding in win8 metro currently. But it can work on win8 desktop, using compatible mode the same to most windows programs.

Thanks for the reply. I guess Metro apps can be authored using HTML5 cocos2d.

This JS binding looks amazing! I’m really eager to start learning it. But… Is it possible to submit a JS-binded game to the App Store? Indeed, I’d like to be sure about that before writing a very time consuming application…

As far as I known, the JS-Binding is compiled to native code using a script.

The JS-Bindig is for multiple deployment. (with the same source-code)

`martin it is possible to submit a game to the AppStore. We’re internally developing the bindings at Zynga and we did some research before investing time. This is the same as with the lua bindings, and even better than other approaches doing the same using JavaScriptCore: due to licenses problems, you should not be submitting a game using JavaScriptCore, but we’re using SpiderMonkey, which has a more permissive license.

`Jose: it does not compile to native. Ideally, however you want to have something like google’s closure compiler to run through your code before shipping it.