COCOS JSBinding Workflow

Setup JSB Enviroment

ScriptingCore* sc = ScriptingCore::getInstance();
sc->addRegisterCallback(register_all_cocos2dx);
sc->addRegisterCallback(register_cocos2dx_js_core);
sc->addRegisterCallback(jsb_register_system);
// ......
sc->start();    
sc->runScript("jsb_boot.js");

initRegister

Add registerDefaultClasses for later Class && Functions registeration

ScriptingCore::ScriptingCore() {
    initRegister();
}
void ScriptingCore::initRegister() {
    this->addRegisterCallback(registerDefaultClasses);
}

addRegisterCallback

Add Native cocos Class to list for later Class && Functions registeration

createGlobalContext

Setup SpiderMonkey Enviroment

jsb_prepare.js

Register some Functions in javascript

Register registrationList

Register registerDefaultClasses

Register cc namespace

Register global Functions

To use in javascript

Register register_all_cocos2dx

Register js_register_cocos2dx_Node

Register Native cocos::Node Class with Functions and Properties

Then we can use cc.Node in javascript

And the Bind Native Function will be called:

js_cocos2dx_Node_constructor

js_cocos2dx_Node_addChild

Add to _js_global_type_map

Add the cocos::Node class type to _js_global_type_map for later Object creation.

Register Other Native Classes

jsb_boot.js

Prepare boot enviroment in javascript

Create Cocos Object

Call new cc.Sprite("xxx.png") from javascript

Trigger ctor in C++

Create NativeObject cocos::Sprite

Create JSObject

Bind NativeObject with JSObject

Add JSObject to GC Root

Return created Object to javascript

Call _ctor in javascript if any from C++

jsb_create_apis.js

Trigger initWithFile in C++

Try to find the NativeObject (cobj) bind with the JSObject

Call initWithFile on NativeObject with param

Remove Cocos Object

Call removeFromParentAndCleanup from javascript

Trigger removeFromParentAndCleanup in C++

Try to find the NativeObject bind to the JSObject when created

Call removeFromParent on NativeObject with param

C++ do the remove stuffs

Trigger cocos::Ref destructor

Trigger removeScriptObjectByObject in C++

Find the JSObject bind to the NativeObject

Remove the JSObject from GC Root

Remove the proxy

Event Register in Cocos

Call addCustomListener from javascript

Trigger addCustomEventListener in C++

Check Function

Create JSFunctionWrapper with this and onGameShow

Create a lambda for Callback with data

Create cocos2d::EventListenerCustom

Return cocos2d::EventListenerCustom to javascript

Event Dispatch in Cocos

Call dispatchCustomEvent from javascript

Create cc.EventCustom in javascript

Trigger EventCustom_constructor in C++

Set user data in javascript

**User data is not passed to C++, stored in javascript

Call dispatchEvent

Trigger EventDispatcher_dispatchEvent in C++

Trigger stored lamba function

JSAPI invoke called

javascript receive event with data

Get user data stored in javascript

CallFunc in Cocos

Call cc.callFunc in javascript

Trigger CallFunc_ctor in C++

Trigger _ctor in javascript

Trigger initWithFunction in C++

Create JSCallbackWrapper to keep the param

target selector data

Add a lamba function to NativeObject

Trigger lamba from C++

Trigger callback in javascript

Schedule in Cocos

Call schedule in javascript

Trigger node_schedule in C++

Get the Schedule Object bind to the NativeObject

Create a JSScheduleWrapper to keep params

Call schedual function on the NativeObject

Trigger JSScheduleWrapper::scheduleFunc

Trigger callback in javascript

Last updated

Was this helpful?