cocos2d Javascript bindings

Forums Programming JavaScript Bindings cocos2d Javascript bindings

This topic contains 221 replies, has 36 voices, and was last updated by  hacioglu 1 month, 2 weeks ago.

Viewing 25 posts - 1 through 25 (of 222 total)
Author Posts
Author Posts
April 25, 2012 at 1:29 am #240881

riq
Keymaster
@admin

Hi,

So, I started playing with the JS bindings for cocos2d.

History:

1) I decided to use regular expressions to parse the cocos2d headers, then generate intermediate data, then parse that data and generate the the JS bindings/wrappers

But it was too complicated. The regular expressions were not trivial (although there is project that is currently doing that).

2) So I decided to generate the intermediate data using the Objective-C reflexion properties.

Then I thought that it should be possible to avoid the intermediate data and generate bindings in runtime.

3) Then I played a little bit with JSCocoa. I did nothing. Just include JSCocoa in cocos2d-mac, and apparently all the cocos2d classes are bound to JS.

I have an example working here:

https://github.com/ricardoquesada/cocos2d-iphone/tree/js-jscocoa

Try the JavascriptTest from the cocos2d-mac project.

Example code:

// sprite
var director = [CCDirector sharedDirector];
var mainScene = director.runningScene;
var layer = mainScene.children[0];
var sprite = CCSprite.alloc.initWithFile('grossini.png');
layer.addChild( sprite );
var s = [director winSize];
sprite.setPosition( CGPointMake( s.width/2, s.height/2 ) );
sprite.release;

// action
var rotate = [CCRotateBy actionWithDuration:2 angle:360];
var move = [CCMoveBy actionWithDuration:2 position:CGPointMake(200,0)];
var seq = [CCSequence actionsWithArray: [rotate, move] ];
sprite.runAction( seq );

So, JSCocoa, apparently is not allowed on iPhone.

But the main purpose of having JS, is to use it as a prototype language now…

You can use it with an editor (eg: CocosBuilder + JS).

In the near future I will try to have a working version for iOS too.

April 26, 2012 at 12:46 am #373670

riq
Keymaster
@admin

Merged JS test into main branch.

Added more tests cases using both Objective-J and pure Javascript.

https://github.com/cocos2d/cocos2d-iphone/blob/gles20/tests/js/test-sprite.js

May 2, 2012 at 1:14 am #373671

riq
Keymaster
@admin

I fought with a memory corruption bug during the last 5 days. Then I realized that I was releasing the JSCocoa controller while JS was being executed… ouch.

After learning that lesson, now it is possible to subclass cocos2d classed in JS, receive callbacks, call JS code from objective-c and vice-versa, use Objective-J syntax, etc…

I started to create the tests from scratch using 100% JS… I will be creating many tests during these days.

current JS tests (without using Objective-J):

https://github.com/cocos2d/cocos2d-iphone/blob/gles20/tests/js/test-actions.js

May 2, 2012 at 11:44 pm #373672

riq
Keymaster
@admin

And more tests:

https://github.com/cocos2d/cocos2d-iphone/blob/gles20/tests/js/test-action.js

https://github.com/cocos2d/cocos2d-iphone/blob/gles20/tests/js/test-sprite.js

May 6, 2012 at 9:08 am #373673

zxl777
@zxl777

good work

May 6, 2012 at 11:11 am #373674

Panajev
Participant
@panajev

Great work Riq, perhaps one of the most important future evolutions for Cocos2D I could ever imagine. I am looking forward to Cocos2D based IDE’s mixed with level editors and sprite/animation editors to allow rapid prototyping of game code (like Unity IDE).

I think it might be a good idea to provide a #pragma strict mode for the JavaScript code like you do in Unity too: maybe the full dynamic nature of JavaScript might be a tad too much overhead for the iPhone. Actually, AOT compilation of JavaScript code would be a good idea too.

May 16, 2012 at 11:44 pm #373675

riq
Keymaster
@admin

Status:

Added more tests, and merged fixes from JSCocoa.

JSCocoa:

Good and quick solution for Mac, specially for prototyping purposed. Not a viable solution for iOS.

Currently I am investigating a faster approach for iOS, probably by doing autogenerated manual bindings for iOS.

Approach 1:

gen_bridge_metadata generates almost all the needed info (classes and methods, constants, structs, functions, enums) but subclass info and protocol info is not generated. Apparently the info is there, but it is not generated.

Approach 2: play with clang’s cindex.py

May 18, 2012 at 7:28 am #373676

joekim
@joekim

@riq – I wanted to share what I did to use javascript for my cocos2d game, Fantasy Village.

http://itunes.apple.com/app/fantasy-village/id428967941?ls=1&mt=8

Initially, I used a headless UIWebView to process javascript. It had the advantage of being able to dynamically download code from the Internet, but the down side was that it made the game freeze while it was handling a request.

The problem is that the UIWebView only runs on the main thread and it was locking up UITouches and cocos2d. To compensate, I made change changes to cocos2d loop to run on a secondary thread so that the UI would still update even though javascript was running. I ended up abandoning this route though because UITouches were still too unresponsive and that only runs on the main thread.

Ultimately, I embedded a version of JavascriptCore and used it’s C++ bindings to hook into cocos2d. JavascriptCore is a part of WebKit and is present on iOS, but it’s a private API, so it can’t be used. Since JavascriptCore is open source, developers can embed a version into their app. That complies with App Store policy as long as it only executes code bundled with the app. The nitro version of JavascriptCore dynamically generates object-code, so that version cannot be used by an iOS app.

I looked at JSCocoa, but it was too much for me to deal with from a development perspective. Simply using the JavascriptCore C++ bindings was easier for me.

To make development faster, I run the javascript logic in a nodejs server that communicates to the client via serialized JSON over TCP. When I make scripting changes, the server restarts automatically and I can test the new logic in a running client without needing to recompile and deploy.

For deployment, I embed the same javascript into the client and it runs on the embedded engine.

One of the libraries I wrote to make integrating with JavaScriptCore easier handled conversions from NSDictionary,NSARRAY,etc to their javascript counterparts and vice versa.

If any of this stuff sounds useful, I could look into open sourcing it.

May 18, 2012 at 7:36 am #373677

joekim
@joekim

@Panajev – I’ve been using Unity lately, and it does seem like a good idea to use something like OpenMono to compile .net languages like jscript to script iOS apps for performance. My game Fantasy Village is unplayably slow on a iPhone 3g.

I’m also a big fan of using a visual editor to increase development speed.

May 18, 2012 at 5:03 pm #373678

riq
Keymaster
@admin

@joekim:

Thanks for the detailed info.

I am curious, how did you create the bindings for cocos2d ? Did you create an obj-c parser that generates binding code (JS <-> ObjC) ? Or did you manually generate the binding code ?

JSCocoa seems to work pretty good for cocos2d-mac. I have all the cocos2d bindings for free. But this is not a good solution for iOS… It is going to be pretty slow and I don’t even know if Apple will approve it.

So, I few days ago I started to create automatic bindings for JS <–> Objc.

Stage 1):

Generate needed data: BridgeSupport cocos2d files + custom script that generates missing info (parent class + used protocols) (95%)

Stage 2):

Use data generated by 1) to generate the bindings for SpiderMonkey. I will start with basic Nodes like CCSprite. (0%)

Stage 3)

Cover all the cocos2d classes (0%)

Stage 4):

Do performance tests, coverage tests, unit tests (0%)

Stage 5):

Repeat 2), 3) for JavascriptCore.

May 18, 2012 at 7:31 pm #373679

robertsze
Participant
@robert-szeleney

Great work, looking forward to integrating this definitely

May 19, 2012 at 12:17 am #373680

riq
Keymaster
@admin

@robertsze: Thanks.

I will develop the spidermonkey integration in this repo / branch:

https://github.com/ricardoquesada/cocos2d-iphone/tree/js-spidermonkey

The test is in cocos2d-mac project. It is called “javascriptspidermonkey”.

It does not compile now, and some files needs to be copied manually… in case anyone is interested in helping, just let me know.

May 22, 2012 at 1:47 am #373681

riq
Keymaster
@admin

Update:

I started to manually wrap CCNode.

I will use TypedArray for C structs (at least for the common ones).

Callbacks (Objective-c to JS) works Ok.

This is how the bindings are being created:

https://github.com/ricardoquesada/cocos2d-iphone/blob/js-spidermonkey/tests/SpiderMonkeyTest/cocos2d_js_bindings.mm

And script will generate those kind of bindings automatically, ideally, for any kind of Objective-C code.

May 22, 2012 at 2:26 am #373682

slembcke
Administrator
@slembcke

So one thing to watch out for when you have binding code like that as compared to using an FFI, is that for a large API it can generate megabytes of code. On the iPhone, it might make sense to do a mixed FFI/native binding approach. Bind the rarely called methods using an FFI to save on space, and then generate code for the often called methods to cut down on the binary size.

Not saying that you should worry about it now, but that was my experience with the Ruby stuff I did a few years back.

May 22, 2012 at 5:16 pm #373683

riq
Keymaster
@admin

@slembcke:

Thanks, good advise.

My first approach was to use JSCocoa, which is a nice approach for Mac. You have everything bound for “free”. It uses Objective-C reflexive properties, and for C free functions and structures it uses FFI (It uses BridgeSupport to know arguments of the functions and fields of the structures).

JSCocoa + cocos2d is working good on Mac, but I don’t like to for iOS.

So, now I am trying the opposite approach. I am binding everything with wrappers. I evaluated using JS_ctypes ( https://wiki.mozilla.org/JSctypes ), but in the end I decided to go the wrappers way. BTW, I think that JS_ctypes could be a good solution for binding chipmunk with JS.

I don’t know how easy/difficult would be to use both FFI and wrappers at the same time. I will investigate it. Thanks.

May 22, 2012 at 5:31 pm #373684

slembcke
Administrator
@slembcke

Wait… Is BridgeSupport available on iOS? I never actually bothered to look considering Apple’s stance against using dynamic languages on iOS.

May 22, 2012 at 6:16 pm #373685

riq
Keymaster
@admin

@slembcke: I don’t think that the iOS Frameworks have the bridgesupport files, but you can generate them manually and use them on iOS.

For example, JSCocoa runs on iOS… but I don’t know if Apple will approve an application using JSCocoa. Not because of BridgeSupport, but for JavascriptCore… perhaps if the applications ships its own version of JavascriptCore it might be approved.

May 22, 2012 at 7:43 pm #373686

slembcke
Administrator
@slembcke

I had no idea that there was a command to generate those bridge support files. I guess it makes sense though. Good to know!

May 22, 2012 at 9:39 pm #373687

riq
Keymaster
@admin

Yes, gen_bridge_metadata is pretty useful.

This is the command line that I am using to generate bridgesupport files for cocos2d-mac:

gen_bridge_metadata -F complete --64-bit -c '-D__CC_PLATFORM_MAC -ISupport -IPlatforms -IPlatforms/Mac -I.' *.h Support/*.h Platforms/*.h Platforms/Mac/*.h -o ../Resources-Mac/cocos2d-mac.bridgesupport

May 23, 2012 at 2:24 am #373688

riq
Keymaster
@admin

Bindings update:

Manual bindings:

TypedArray are returned for custom structs (objc -> JS)

NSObject bindings are generated manually, in its own file.

Automatic bindings:

Started script. Parses bridgesupport files and hierarchy+protocols file.

class: 5%. Generates constructor and destructors. Missing methods and properties as well as protocol.

enums: 0%

functions: 0%

macros: 0%

structures: 0%

constants: 0%

In case you want to collaborate with this effort, I am committing the code here: https://github.com/ricardoquesada/cocos2d-iphone/tree/js-spidermonkey

May 24, 2012 at 2:27 am #373689

riq
Keymaster
@admin

Update:

Automatic bindings:

class: 8%. Constructors and Destructors Ok. Started to generated methods with supported ret values and arguments.

enums: 0%

functions: 0%

macros: 0%

structures: 0%

constants: 0%

May 25, 2012 at 2:32 am #373690

riq
Keymaster
@admin

Update:

Automatic bindings:

class: 18%. methods with “basic types” both in args and ret_val are generated correctly. Added some tests cases.

enums: 0%

functions: 0%

macros: 0%

structures: 0%

constants: 0%

May 26, 2012 at 12:18 am #373691

Jason A
@jason-a

This looks great! To get the spidermonkey test working I added a copy files build phase for libmozjs.dylib into the executables directory. Thanks for your work on this!

May 26, 2012 at 2:07 am #373692

riq
Keymaster
@admin

Update:

class: 30%. NSString + Objects are supported. eg: sprite.initWithFile('grossini'); parent.addChild(sprite);

enums: 0%

functions: 0%

macros: 0%

structures: 0%

constants: 0%

May 26, 2012 at 8:30 am #373693

jptsetung
Participant
@jptsetung

I’m not sure to understand what is happening here. The goal is the code your game in JS with Cocos2D-like methods? Where is exectued the JS code? In a webview? So once you’ve coded a prototype with JS, you’ve to start from cratch in real objective C for iOS?

Viewing 25 posts - 1 through 25 (of 222 total)

You must be logged in to reply to this topic.