Skip to content

Commit 6e20c3c

Browse files
committed
Merge pull request #14681 from pandamicro/v3.10
Fix ComponentJS proxy management issue in JSB
2 parents 280a8e9 + 64276bd commit 6e20c3c

File tree

5 files changed

+68
-20
lines changed

5 files changed

+68
-20
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ before_install:
3737
# whitelist
3838
branches:
3939
only:
40-
- v3
40+
- v3.10

cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5770,6 +5770,41 @@ void js_register_cocos2dx_AutoPolygon(JSContext *cx, JS::HandleObject global) {
57705770
jsb_register_class<cocos2d::AutoPolygon>(cx, jsb_cocos2d_AutoPolygon_class, proto, JS::NullPtr());
57715771
}
57725772

5773+
// ComponentJS controls the native js proxy itself, must be bound manually
5774+
bool js_cocos2dx_ComponentJS_create(JSContext *cx, uint32_t argc, jsval *vp)
5775+
{
5776+
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
5777+
bool ok = true;
5778+
if (argc == 1) {
5779+
std::string arg0;
5780+
ok &= jsval_to_std_string(cx, args.get(0), &arg0);
5781+
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_ComponentJS_create : Error processing arguments");
5782+
5783+
auto ret = cocos2d::ComponentJS::create(arg0);
5784+
JS::RootedObject jsret(cx, static_cast<JSObject*>(ret->getScriptObject()));
5785+
args.rval().set(OBJECT_TO_JSVAL(jsret));
5786+
return true;
5787+
}
5788+
JS_ReportError(cx, "js_cocos2dx_ComponentJS_create : wrong number of arguments");
5789+
return false;
5790+
}
5791+
static bool js_cocos2dx_ComponentJS_ctor(JSContext *cx, uint32_t argc, jsval *vp)
5792+
{
5793+
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
5794+
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
5795+
bool ok = true;
5796+
std::string arg0;
5797+
ok &= jsval_to_std_string(cx, args.get(0), &arg0);
5798+
JSB_PRECONDITION2(ok, cx, false, "js_cocos2d_ComponentJS_ctor : Error processing arguments");
5799+
cocos2d::ComponentJS *nobj = new (std::nothrow) cocos2d::ComponentJS(arg0);
5800+
// autorelease it
5801+
nobj->autorelease();
5802+
bool isFound = false;
5803+
if (JS_HasProperty(cx, obj, "_ctor", &isFound) && isFound)
5804+
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), "_ctor", args);
5805+
args.rval().setUndefined();
5806+
return true;
5807+
}
57735808
bool js_cocos2dx_ComponentJS_getScriptObject(JSContext *cx, uint32_t argc, jsval *vp)
57745809
{
57755810
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
@@ -6034,8 +6069,12 @@ void register_cocos2dx_js_core(JSContext* cx, JS::HandleObject global)
60346069
tmpObj.set(jsb_cocos2d_ClippingNode_prototype);
60356070
JS_DefineFunction(cx, tmpObj, "init", js_cocos2dx_ClippingNode_init, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT);
60366071

6072+
JS_GetProperty(cx, ccObj, "ComponentJS", &tmpVal);
6073+
tmpObj = tmpVal.toObjectOrNull();
6074+
JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_ComponentJS_create, 1, JSPROP_READONLY | JSPROP_PERMANENT);
60376075
tmpObj.set(jsb_cocos2d_ComponentJS_prototype);
60386076
JS_DefineFunction(cx, tmpObj, "getScriptObject", js_cocos2dx_ComponentJS_getScriptObject, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT);
6077+
JS_DefineFunction(cx, tmpObj, "ctor", js_cocos2dx_ComponentJS_ctor, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
60396078

60406079
JS_DefineFunction(cx, ccObj, "glEnableVertexAttribs", js_cocos2dx_ccGLEnableVertexAttribs, 1, JSPROP_READONLY | JSPROP_PERMANENT);
60416080
JS_DefineFunction(cx, ccObj, "pAdd", js_cocos2dx_ccpAdd, 1, JSPROP_READONLY | JSPROP_PERMANENT);

cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,15 @@ ComponentJS::ComponentJS(const std::string& scriptFileName)
6666
JS::RootedValue protoValue(cx);
6767
JS_GetProperty(cx, classObj, "prototype", &protoValue);
6868

69-
TypeTest<ComponentJS> t;
70-
js_type_class_t *typeClass = nullptr;
71-
std::string typeName = t.s_name();
72-
auto typeMapIter = _js_global_type_map.find(typeName);
73-
CCASSERT(typeMapIter != _js_global_type_map.end(), "Can't find the class type!");
74-
typeClass = typeMapIter->second;
75-
7669
mozilla::Maybe<JS::PersistentRootedObject> *jsObj = new mozilla::Maybe<JS::PersistentRootedObject>();
7770

71+
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::ComponentJS>(this);
7872
JS::RootedObject proto(cx, protoValue.toObjectOrNull());
7973
JS::RootedObject parent(cx, typeClass->proto.ref());
8074
jsObj->construct(cx);
8175
JS::RootedObject obj(cx, JS_NewObject(cx, theClass, proto, parent));
8276
jsObj->ref() = obj;
8377

84-
// Unbind current proxy binding
85-
js_proxy_t* jsproxy = js_get_or_create_proxy<ComponentJS>(cx, this);
86-
JS::RemoveObjectRoot(cx, &jsproxy->obj);
87-
jsb_remove_proxy(jsb_get_native_proxy(this), jsproxy);
8878
// link the native object with the javascript object
8979
jsb_new_proxy(this, jsObj->ref());
9080

@@ -95,6 +85,17 @@ ComponentJS::ComponentJS(const std::string& scriptFileName)
9585
ComponentJS::~ComponentJS()
9686
{
9787
mozilla::Maybe<JS::PersistentRootedObject>* jsObj = static_cast<mozilla::Maybe<JS::PersistentRootedObject>*>(_jsObj);
88+
if (jsObj && !jsObj->empty())
89+
{
90+
// Remove proxy
91+
js_proxy_t* jsproxy = jsb_get_js_proxy(jsObj->ref());
92+
if (jsproxy)
93+
{
94+
js_proxy_t* nproxy = jsb_get_native_proxy(jsproxy->ptr);
95+
jsb_remove_proxy(nproxy, jsproxy);
96+
}
97+
}
98+
// Delete rooted object
9899
if (jsObj != nullptr)
99100
{
100101
delete jsObj;
@@ -104,7 +105,14 @@ ComponentJS::~ComponentJS()
104105
void* ComponentJS::getScriptObject() const
105106
{
106107
mozilla::Maybe<JS::PersistentRootedObject>* jsObj = static_cast<mozilla::Maybe<JS::PersistentRootedObject>*>(_jsObj);
107-
return jsObj->ref().get();
108+
if (jsObj && !jsObj->empty())
109+
{
110+
return jsObj->ref().get();
111+
}
112+
else
113+
{
114+
return nullptr;
115+
}
108116
}
109117

110118
void ComponentJS::update(float delta)

cocos/scripting/js-bindings/script/jsb_cocos2d.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,29 +2771,30 @@ cc.GLProgram.prototype.setUniformLocationWithMatrix2fv = function(){
27712771
var tempArray = Array.prototype.slice.call(arguments);
27722772
tempArray = Array.prototype.concat.call(tempArray, 2);
27732773
this.setUniformLocationWithMatrixfvUnion.apply(this, tempArray);
2774-
}
2774+
};
27752775

27762776
cc.GLProgram.prototype.setUniformLocationWithMatrix3fv = function(){
27772777
var tempArray = Array.prototype.slice.call(arguments);
27782778
tempArray = Array.prototype.concat.call(tempArray, 3);
27792779
this.setUniformLocationWithMatrixfvUnion.apply(this, tempArray);
2780-
}
2780+
};
27812781
cc.GLProgram.prototype.setUniformLocationWithMatrix4fv = function(){
27822782
var tempArray = Array.prototype.slice.call(arguments);
27832783
tempArray = Array.prototype.concat.call(tempArray, 4);
27842784
this.setUniformLocationWithMatrixfvUnion.apply(this, tempArray);
2785-
}
2785+
};
27862786

27872787

27882788
//
27892789
// Script Component
27902790
//
27912791
cc._ComponentJS = cc.ComponentJS;
2792+
cc._ComponentJS.extend = cc.Class.extend;
27922793
cc.ComponentJS = function (filename) {
27932794
var comp = cc._ComponentJS.create(filename);
27942795
var res = comp.getScriptObject();
27952796
return res;
2796-
}
2797+
};
27972798
cc.ComponentJS.extend = function (prop) {
27982799
return cc._ComponentJS.extend(prop);
27992800
};

tools/tojs/cocos2dx.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ replace_headers = CCComponentJS.h::component/CCComponentJS.h
3131

3232
classes = New.* Sprite SpriteBatchNode SpriteFrame SpriteFrameCache Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn ReverseTime Animate AnimationFrame Animation AnimationCache Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* TMX.* CallFunc CallFuncN RenderTexture GridAction Grid3DAction Grid3D TiledGrid3D GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Lens3D Ripple3D PageTurn3D ShakyTiles3D ShatteredTiles3D WavesTiles3D JumpTiles3D Speed ActionManager Set SimpleAudioEngine Scheduler Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram GLProgramCache Application ClippingNode MotionStreak TextFieldTTF GLViewProtocol GLView Component ComponentContainer __NodeRGBA __LayerRGBA SAXParser Event(?!.*(Physics).*).* Device Configuration ProtectedNode GLProgramState Image .*Light$ AsyncTaskPool Properties Material Technique RenderState Pass ComponentJS
3333

34-
classes_need_extend = Node __NodeRGBA Layer.* Sprite SpriteBatchNode SpriteFrame Menu MenuItem.* Scene DrawNode Component .*Action.* GridBase Grid3D TiledGrid3D MotionStreak ParticleBatchNode ParticleSystem TextFieldTTF RenderTexture TileMapAtlas TMXLayer TMXTiledMap TMXMapInfo TransitionScene ProgressTimer ParallaxNode Label.* GLProgram Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn ReverseTime Animate AnimationFrame Animation AnimationCache Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* Orbit.* Follow.* Bezier.* Hide CallFunc CallFuncN ComponentJS
34+
classes_need_extend = Node __NodeRGBA Layer.* Sprite SpriteBatchNode SpriteFrame Menu MenuItem.* Scene DrawNode Component .*Action.* GridBase Grid3D TiledGrid3D MotionStreak ParticleBatchNode ParticleSystem TextFieldTTF RenderTexture TileMapAtlas TMXLayer TMXTiledMap TMXMapInfo TransitionScene ProgressTimer ParallaxNode Label.* GLProgram Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn ReverseTime Animate AnimationFrame Animation AnimationCache Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* Orbit.* Follow.* Bezier.* Hide CallFunc CallFuncN
3535

3636
# what should we skip? in the format ClassName::[function function]
3737
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
@@ -140,7 +140,7 @@ skip = Node::[^setPosition$ setGLServerState description getUserObject .*UserDat
140140
Camera::[unproject isVisibleInFrustum],
141141
ClippingNode::[init],
142142
RenderState::[setStateBlock],
143-
ComponentJS::[getScriptObject update]
143+
ComponentJS::[create getScriptObject update]
144144

145145
rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame],
146146
MenuItemFont::[setFontNameObj=setFontName setFontSizeObj=setFontSize getFontSizeObj=getFontSize getFontNameObj=getFontName],
@@ -186,7 +186,7 @@ base_classes_to_skip = Ref Clonable
186186
# classes that create no constructor
187187
# Set is special and we will use a hand-written constructor
188188

189-
abstract_classes = Action FiniteTimeAction ActionInterval ActionEase EaseRateAction EaseElastic EaseBounce ActionInstant GridAction Grid3DAction TiledGrid3DAction Director SpriteFrameCache TransitionEaseScene Set SimpleAudioEngine FileUtils Application GLViewProtocol GLView ComponentContainer SAXParser Configuration EventListener BaseLight AsyncTaskPool
189+
abstract_classes = Action FiniteTimeAction ActionInterval ActionEase EaseRateAction EaseElastic EaseBounce ActionInstant GridAction Grid3DAction TiledGrid3DAction Director SpriteFrameCache TransitionEaseScene Set SimpleAudioEngine FileUtils Application GLViewProtocol GLView ComponentContainer SAXParser Configuration EventListener BaseLight AsyncTaskPool ComponentJS
190190

191191
# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
192192
script_control_cpp = no

0 commit comments

Comments
 (0)