JavaScript Bindings

I am just curious if you could give an estimate of how much work hours are required to implement Javascript bindings? The reason I ask is because perhaps a kickstarter project could be started to get bindings for all of the other platforms as well (XNA and Blackberry). I think that there really is something here with Cocos2d-x. Free, open-source, native support for multiple platforms, lua and javascript scripting, decent tools (cocosbuilder, tiled map editor), most importantly single codebase.

Hi

Is this complete for Android?
I want to write cocos2d-x by JavaScript. :slight_smile:

Hi everybody,

I am working on a ios project, and I want to call an Obj-C class from Javascript with Cocos2D-X JSB.
So I created a .mm class wrapper, I can call a static function of my Obj-C class with C**.
Now I need to bind JS functions to that C** class.
I try to make my own binding with help of this : https://github.com/funkaster/cocos2d-x/tree/js-bindings/js/test_bindings
After making my binding files, what is the exact procedure, because I think these only files don’t make any bindings.
Is there some code to write ?
Which files must we include to register JSB functions ?

Thanks

Hi,
You need to include the spidermonkey lib in scripting/javascript/spidermonkey-ios as well as the files in scripting/javascript/bindings and scripting/javascript/bindings/generated
To generate your own js bindings from the native class use cxx-generator from tools/cxx-generator. As an example, check out tools/tojs
As a JSB example, you can check out the MoonWarriors or TestJavascript projects in the samples directory.
Best of luck

Hi,

Thanks for your quick reply.
I tried to make my bindings with your help, but I can’t make it working…

Here is my unsuccessful story about trying to generate JS file from C*+ files wrapping Obj-C files:
# I made Obj-C files and made a wrapper in C*+, until here no prob.
# Then I went to the tools/cxxgenerator folder to create our corresponding JS bindings.
# From there, I created our own ini file but I first wanted to make the test sample work with the simple class file.
# With test.ini I launched test.sh which launches generator.py internally
# Here becomes the nightmare. I needed to painfully install the following:
* YAML with all the dependencies
* Pygment.lexer
* Cheetah, Cheetah.Templates and from there I had a badValueSubstitution error where the only solution was to modify the dynamic paths with hardcoded paths
* Python, and python sources in the system headers
* Need to compile _namemapper.c

I ended with the following message:

You don't have the C version of NameMapper installed! I'm disabling Cheetah's useStackFrames option as it is painfully slow with the Python version of NameMapper. You should get a copy of Cheetah with the compiled C version of NameMapper.
  "\nYou don't have the C version of NameMapper installed! "
Exception ctypes.ArgumentError: "argument 1: : 'TranslationUnit' object has no attribute '_as_parameter_'" in > ignored

Did you encounter any similar issues ? Do you have some clues to fix my problem ? Do you know if other simpler tools to generate the bindings would be available in the near future ?

Thanks very much

Yes it’s indeed a daunting task to do all it’s necessary.

Ok. The first “You don’t have C version…” you can ignore that - it’s basically saying you don’t have _namemapper.pyd compiled from c/c++ sources. It is working fine with the python one too, so you can ignore it.
The Exception, i had a similar one, basically you probably set a path or a headers filename wrong, and python can’t find it. Check again your config files and make sure everything is allright, as well as check the userconfig.ini auto generated file to make sure it’s being generated correctly.

Other than these, i’m getting a bunch of errors about some problems in android ndk sources, but i just ignore those. If you don’t see any errors about your own source files, you should be good to go. Granted that you’ll be able to solve the Exception above.

Rémy T. wrote:

# From there, I created our own ini file but I first wanted to make the test sample work with the simple class file.
# With test.ini I launched test.sh which launches generator.py internally
# Here becomes the nightmare. I needed to painfully install the following:
* YAML with all the dependencies
* Pygment.lexer
* Cheetah, Cheetah.Templates and from there I had a badValueSubstitution error where the only solution was to modify the dynamic paths with hardcoded paths
* Python, and python sources in the system headers
* Need to compile _namemapper.c
>

Which platform are you running this on?

On MacOSX you should be able to install all required dependencies easily using MacPorts
sudo port install python27 py27-yaml py27-cheetah clang-3.1 llvm-3.1

On Debian based Linux you should be able to similarly install using apt… or on RedHat using yum.

I updated the documentation on how to run the test on OSX

I’ll add instructions for Linux too.

Thanks for all your replies, I’ll take all these advices, but I think I know where my issue was.
I’m trying your procedure from GitHub, and let you know about how it terminates.
Thanks again.

Edit:
I have not succeed to make bindings with your test. but after removing every uses and references to std::string, I made it
Did you know why it does not work with string ?
Now I can continue to work =). Thanks.

std::string headers should be provided by Android NDK’s gnu libstdc++

Please provide more information :

* Which platform are you running on? OSX? Linux?
* Which version of the NDK are you using? (perhaps there is a bug with specific versions of the NDK.)

Sorry, I’m using OSX mountain lion
I am using <string> from system (perhaps not updated), and I use your NDK provided in a previous post.

Hi,

I’ve managed to get cxx-generator generate bindings for my own C*+ code. I ran into a problem with a struct that I use in my C*+ code for passing values between certain classes. How do I get the cxx-generator to generate bindings for this struct so that I can use it to pass values?

How would one go about to get a struct in the line of this into the js-world?

struct mystruct
{
  static mystruct Integer(int val);
  Type type;
  int integer;
  double real;
  ...
};

Thanks!

BTW. I’m really impressed with the whole JS-binding effort being done here, can’t wait to see what people can do with this.

How would one go about to get a struct in the line of this into the js-world?

There are examples of this in the cocos2d-x bindings. For example CCPoint :


https://github.com/cocos2d/cocos2d-x/blob/gles20/scripting/javascript/bindings/ScriptingCore.cpp#L878
https://github.com/cocos2d/cocos2d-x/blob/gles20/scripting/javascript/bindings/ScriptingCore.cpp#L1170

BTW. I’m really impressed with the whole JS-binding effort being done here, can’t wait to see what people can do with this.

Credit to @funkaster who wrote the original code. And the clang/llvm project.

_ H3nk3_ wrote:

Hi,
>
I’ve managed to get cxx-generator generate bindings for my own C*+ code. I ran into a problem with a struct that I use in my C*+ code for passing values between certain classes. How do I get the cxx-generator to generate bindings for this struct so that I can use it to pass values?
>
How would one go about to get a struct in the line of this into the js-world?
>
[…]
>
Thanks!
>
BTW. I’m really impressed with the whole JS-binding effort being done here, can’t wait to see what people can do with this.

Hi,

I have my binding files, (cpp, hpp and js), everything seems OK, so how the binding between JS and C*+ are made ?
I include those three files in my xCode project like cocos2dx is.
JS functions looks to be recognized @), but the originals C*+ functions are not called. Did I miss something ?

I generated bindings from the test, replacing the simpleNativeClass by my own class, and class and headers extra_parameters, but I’m not sure I’ve replaced everything I need.

In c++ you need to register your new native functions by using addRegisterCallback of ScriptingCore (check TestJavascript AppDelegate.cpp on how to do this) and pass the newly generated registercallback function.

Yay, Thank you IT WORKS !
I have to make some games with the game center, now I can do it from JS
Thanks again

Hi again,

Thanks! Managed to get it working properly now. Struggled with my namespaces making it so that the script did not find my native conversion functions that I added to conversion.yaml. When I removed them it started using them in the generated code.

Could you give any pointers how to make complex callback functions work with javascript? Say I want to use the struct below as a parameter in a callback. It could be defined like this in C**:
<pre>
typedef void ;
</pre>
Thanks!
folec r wrote:

> How would one go about to get a struct in the line of this into the js-world?
>
There are examples of this in the cocos2d-x bindings. For example CCPoint :
>
https://github.com/funkaster/cxx-generator/blob/master/targets/spidermonkey/conversions.yaml#L31
https://github.com/cocos2d/cocos2d-x/blob/gles20/scripting/javascript/bindings/ScriptingCore.cpp#L878
https://github.com/cocos2d/cocos2d-x/blob/gles20/scripting/javascript/bindings/ScriptingCore.cpp#L1170
>
> BTW. I’m really impressed with the whole JS-binding effort being done here, can’t wait to see what people can do with this.
>
Credit to @funkaster who wrote the original code. And the clang/llvm project.
>
_ H3nk3_ wrote:
> Hi,
>
> I’ve managed to get cxx-generator generate bindings for my own C** code. I ran into a problem with a struct that I use in my C++ code for passing values between certain classes. How do I get the cxx-generator to generate bindings for this struct so that I can use it to pass values?
>
> How would one go about to get a struct in the line of this into the js-world?
>
> […]
>
> Thanks!
>
> BTW. I’m really impressed with the whole JS-binding effort being done here, can’t wait to see what people can do with this.

That’s great!
I’m glad i could help.

Good luck on your games!

Rémy T. wrote:

Yay, Thank you IT WORKS !
I have to make some games with the game center, now I can do it from JS
Thanks again

Bindings for marmalade possible ?

Ovidiu Diac wrote:

In c++ you need to register your new native functions by using addRegisterCallback of ScriptingCore (check TestJavascript AppDelegate.cpp on how to do this) and pass the newly generated registercallback function.

I have my binding files, (cpp, hpp and js) generated by https://github.com/funkaster/cxx-generator. I follow your intructions, but no success. Can you help me ? Where I put the files ? Can you get me a step by step ? thanks in advance!