This article is the official HTML5 training course for Brother Lian it education organization, mainly introduces: JavaScript intensive tutorial--cocos2d-js automatic jsb binding rule modification
What is JSB
JSB is the abbreviation for JavaScript binding. The JavaScript engine used in Cocos2d-js is Mozilla's SpiderMonkey, and JSB bindings are designed to allow JavaScript scripts to invoke C + + code, which can be called into JavaScript scripts.
How to generate JSB bindings
1. Automatically generate scripts
In the Tools/tojs directory under the COCOSD-JS engine directory, there is an official script to generate an automatic version. We can start from here to learn.
(1) Setting up the environment
Need to install the following three software
Python 2.7.x (): The Bindings-generator in the engine currently supports only the python2.7.x version
Py-ymal (): This is a python third-party package, the download page has different system installation method, you can refer to generator.py will call to this package.
Cheetah (): This is also a Python third-party package, a template, generator.py will be called to this package, installed in the same manner as py-ymal.
Download the good package after unpacking. Execute under the software catalog
1
Python setup.py Install
In the case of a Mac, at the front of the command, sudo.
For more information, you can view the website's Tutorials (address)
(2) Running genbindings.py
After setting up the environment, open the command line and enter the directory COCOS2D-JS/TOOLS/TOJS. Input command
1
Python genbindings.py
Automatic binding can be seen as shown after success
20150521210554473.jpg
The generated code after the binding is in the directory
Cocos2d-js/frameworks/js-bindings/bindings/auto
As for why this code is generated, you can look at the code in genbindings.py. This is not a detailed explanation.
2. Generate scripts manually (this article does not explain)
How to make an auto-generated script binding rule modification
The constraints of automatic binding rules are mainly tools/bindings-generator/targets/spidermonkey in this directory.
Let's take a look at Conversions.yaml, which is a configuration file about binding code.
1. Type conversion
20150521211842536.jpg
As you can see. In automatic binding, the float type of C + + is converted to a double type because the float type has no way to generate the binding. Short type converted to int32_t
2. Namespace Conversion
20150521212053405.jpg
The C + + code with the left namespace is defined in JS as the right-hand namespace.
3. Convert JS-passed script variable to C + + variable
20150521212332451.jpg
For example, the passed JS variable needs to be converted to an int type, and the right code is used to convert it.
4. Convert the Script object from JS to C + + object
20150521212601552.jpg
The type of object that JS passes over needs to be mapped with C + + objects. If you want to increase the number of objects to be empty worth processing the decision, you can add here.
5. Convert C + + variables to JS variables
20150521212946602.jpg
For example, an int object needs to be converted to a JS variable so it is int32_to_jsval.
6. Convert C + + objects to JS objects
20150521213139219.jpg
Look at the comparison code can be seen. The functions of C + + converted to JS objects go through these steps
Js_get_or_create_proxy
Object_to_jsval
The above is all the conversion basis rules, and then the production of automatic binding code is completed according to some templates.
The templates directory is a pair of generated code templates
20150521211533483.jpg
There are templates for constructors, templates for overloaded functions, and so on, so you can look at the code notation inside.
JavaScript Hardening Tutorial--cocos2d-js automatic jsb binding rule modification