#1 By: riq, April 25th, 2012 01:29
So, I started playing with the JS bindings for cocos2d.
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:
var director = [CCDirector sharedDirector];
var mainScene = director.runningScene;
var layer = mainScene.children;
var sprite = CCSprite.alloc.initWithFile('grossini.png');
layer.addChild( sprite );
var s = [director winSize];
sprite.setPosition( CGPointMake( s.width/2, s.height/2 ) );
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.
#2 By: riq, April 26th, 2012 00:46
Merged JS test into main branch.
#3 By: riq, May 2nd, 2012 01:14
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):
#4 By: riq, May 2nd, 2012 23:44
And more tests:
#5 By: zxl777, May 6th, 2012 09:08
#6 By: Panajev, May 6th, 2012 11:11
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).
#7 By: riq, May 16th, 2012 23:44
Added more tests, and merged fixes from 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.
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
#8 By: joekim, May 18th, 2012 07:28
If any of this stuff sounds useful, I could look into open sourcing it.
#9 By: joekim, May 18th, 2012 07:36
@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.
#10 By: riq, May 18th, 2012 17:03
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.
Generate needed data: BridgeSupport cocos2d files + custom script that generates missing info (parent class + used protocols) (95%)
Use data generated by 1) to generate the bindings for SpiderMonkey. I will start with basic Nodes like CCSprite. (0%)
Cover all the cocos2d classes (0%)
Do performance tests, coverage tests, unit tests (0%)
#11 By: robertsze, May 18th, 2012 19:31
Great work, looking forward to integrating this definitely
#12 By: riq, May 19th, 2012 00:17
I will develop the spidermonkey integration in this repo / branch:
It does not compile now, and some files needs to be copied manually... in case anyone is interested in helping, just let me know.
#13 By: riq, May 22nd, 2012 01:47
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:
And script will generate those kind of bindings automatically, ideally, for any kind of Objective-C code.
#14 By: slembcke, May 22nd, 2012 02:26
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.
#15 By: riq, May 22nd, 2012 17:16
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.
#16 By: slembcke, May 22nd, 2012 17:31
Wait... Is BridgeSupport available on iOS? I never actually bothered to look considering Apple's stance against using dynamic languages on iOS.
#17 By: riq, May 22nd, 2012 18:16
@slembcke: I don't think that the iOS Frameworks have the bridgesupport files, but you can generate them manually and use them on iOS.
#18 By: slembcke, May 22nd, 2012 19:43
I had no idea that there was a command to generate those bridge support files. I guess it makes sense though. Good to know!
#19 By: riq, May 22nd, 2012 21:39
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
#20 By: riq, May 23rd, 2012 02:24
TypedArray are returned for custom structs (objc -> JS)
NSObject bindings are generated manually, in its own file.
Started script. Parses bridgesupport files and hierarchy+protocols file.
class: 5%. Generates constructor and destructors. Missing methods and properties as well as protocol.
In case you want to collaborate with this effort, I am committing the code here: https://github.com/ricardoquesada/cocos2d-iphone/tree/js-spidermonkey
next page →