Skip to content

Commit ed11807

Browse files
committed
Merge pull request #1560 from pandamicro/ImproveBindings
Improve bindings and upgrade to SpiderMonkey v34 for iOS
2 parents e584dbb + c4626e1 commit ed11807

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2911
-2081
lines changed

frameworks/js-bindings/bindings/manual/3d/jsb_cocos2dx_3d_manual.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,13 @@ bool js_cocos2dx_Mesh_getMeshVertexAttribute(JSContext *cx, uint32_t argc, jsval
144144

145145
void register_all_cocos2dx_3d_manual(JSContext *cx, JS::HandleObject global)
146146
{
147+
JS::RootedValue tmpVal(cx);
148+
JS::RootedObject ccObj(cx);
147149
JS::RootedObject tmpObj(cx);
148-
tmpObj = anonEvaluate(cx, global, "(function () { return cc.Sprite3D; })()").toObjectOrNull();
150+
get_or_create_js_obj(cx, global, "cc", &ccObj);
151+
152+
JS_GetProperty(cx, ccObj, "Sprite3D", &tmpVal);
153+
tmpObj = tmpVal.toObjectOrNull();
149154
JS_DefineFunction(cx, tmpObj, "createAsync", js_cocos2dx_Sprite3D_createAsync, 4, JSPROP_READONLY | JSPROP_PERMANENT);
150155

151156
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_Sprite3D_prototype), "getAABB", js_cocos2dx_Sprite3D_getAABB, 0, JSPROP_READONLY | JSPROP_PERMANENT);

frameworks/js-bindings/bindings/manual/ScriptingCore.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,16 @@ void registerDefaultClasses(JSContext* cx, JS::HandleObject global) {
393393
JS::RootedValue nsval(cx);
394394
JS::RootedObject ns(cx);
395395
JS_GetProperty(cx, global, "cc", &nsval);
396-
if (nsval == JSVAL_VOID) {
397-
JS::RootedObject proto(cx);
398-
JS::RootedObject parent(cx);
399-
ns = JS_NewObject(cx, NULL, proto, parent);
396+
// Not exist, create it
397+
if (nsval == JSVAL_VOID)
398+
{
399+
ns.set(JS_NewObject(cx, NULL, JS::NullPtr(), JS::NullPtr()));
400400
nsval = OBJECT_TO_JSVAL(ns);
401401
JS_SetProperty(cx, global, "cc", nsval);
402-
} else {
403-
JS_ValueToObject(cx, nsval, &ns);
402+
}
403+
else
404+
{
405+
ns.set(nsval.toObjectOrNull());
404406
}
405407

406408
//
@@ -554,12 +556,12 @@ static JSSecurityCallbacks securityCallbacks = {
554556
};
555557

556558
void ScriptingCore::createGlobalContext() {
557-
if (this->_cx && this->_rt) {
558-
ScriptingCore::removeAllRoots(this->_cx);
559-
JS_DestroyContext(this->_cx);
560-
JS_DestroyRuntime(this->_rt);
561-
this->_cx = NULL;
562-
this->_rt = NULL;
559+
if (_cx && _rt) {
560+
ScriptingCore::removeAllRoots(_cx);
561+
JS_DestroyContext(_cx);
562+
JS_DestroyRuntime(_rt);
563+
_cx = NULL;
564+
_rt = NULL;
563565
}
564566

565567
// Start the engine. Added in SpiderMonkey v25
@@ -568,40 +570,46 @@ void ScriptingCore::createGlobalContext() {
568570

569571
// Removed from Spidermonkey 19.
570572
//JS_SetCStringsAreUTF8();
571-
this->_rt = JS_NewRuntime(8L * 1024L * 1024L);
573+
_rt = JS_NewRuntime(8L * 1024L * 1024L);
572574
JS_SetGCParameter(_rt, JSGC_MAX_BYTES, 0xffffffff);
573575

574576
JS_SetTrustedPrincipals(_rt, &shellTrustedPrincipals);
575577
JS_SetSecurityCallbacks(_rt, &securityCallbacks);
576578
JS_SetNativeStackQuota(_rt, JSB_MAX_STACK_QUOTA);
577579

578-
this->_cx = JS_NewContext(_rt, 8192);
580+
_cx = JS_NewContext(_rt, 8192);
579581

580582
// Removed in Firefox v27
581583
// JS_SetOptions(this->_cx, JSOPTION_TYPE_INFERENCE);
582584
// Removed in Firefox v33
583585
// JS::ContextOptionsRef(_cx).setTypeInference(true);
584-
// JS::ContextOptionsRef(_cx).setIon(true);
585-
// JS::ContextOptionsRef(_cx).setBaseline(true);
586586

587587
JS::RuntimeOptionsRef(_rt).setIon(true);
588588
JS::RuntimeOptionsRef(_rt).setBaseline(true);
589589

590590
// JS_SetVersion(this->_cx, JSVERSION_LATEST);
591591

592-
JS_SetErrorReporter(this->_cx, ScriptingCore::reportError);
592+
JS_SetErrorReporter(_cx, ScriptingCore::reportError);
593593
#if defined(JS_GC_ZEAL) && defined(DEBUG)
594594
//JS_SetGCZeal(this->_cx, 2, JS_DEFAULT_ZEAL_FREQ);
595595
#endif
596-
this->_global.construct(_cx);
597-
this->_global.ref() = NewGlobalObject(_cx);
598-
599-
JSAutoCompartment ac(_cx, _global.ref().get());
600-
js::SetDefaultObjectForContext(_cx, _global.ref().get());
596+
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
597+
_global.emplace(_cx);
598+
#else
599+
_global.construct(_cx);
600+
#endif
601+
_global.ref() = NewGlobalObject(_cx);
602+
603+
JSAutoCompartment ac(_cx, _global.ref());
604+
605+
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
606+
// Removed in Firefox v34
607+
js::SetDefaultObjectForContext(_cx, _global.ref());
608+
#endif
601609

602610
for (std::vector<sc_register_sth>::iterator it = registrationList.begin(); it != registrationList.end(); it++) {
603611
sc_register_sth callback = *it;
604-
callback(this->_cx, JS::RootedObject(this->_cx, this->_global.ref().get()));
612+
callback(_cx, _global.ref());
605613
}
606614
}
607615

@@ -727,8 +735,7 @@ void ScriptingCore::cleanAllScript()
727735

728736
bool ScriptingCore::runScript(const char *path)
729737
{
730-
JS::RootedObject global(_cx, _global.ref().get());
731-
return runScript(path, global, _cx);
738+
return runScript(path, _global.ref(), _cx);
732739
}
733740

734741
bool ScriptingCore::runScript(const char *path, JS::HandleObject global, JSContext* cx)
@@ -1747,13 +1754,20 @@ bool JSBDebug_BufferWrite(JSContext* cx, unsigned argc, jsval* vp)
17471754

17481755
void ScriptingCore::enableDebugger(unsigned int port)
17491756
{
1757+
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
1758+
if (_debugGlobal.isNothing())
1759+
#else
17501760
if (_debugGlobal.empty())
1761+
#endif
17511762
{
17521763
JSAutoCompartment ac0(_cx, _global.ref().get());
17531764

17541765
JS_SetDebugMode(_cx, true);
1755-
1766+
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
1767+
_debugGlobal.emplace(_cx);
1768+
#else
17561769
_debugGlobal.construct(_cx);
1770+
#endif
17571771
_debugGlobal.ref() = NewGlobalObject(_cx, true);
17581772
// Adds the debugger object to root, otherwise it may be collected by GC.
17591773
//AddObjectRoot(_cx, &_debugGlobal); no need, it's persistent rooted now
@@ -1781,7 +1795,7 @@ void ScriptingCore::enableDebugger(unsigned int port)
17811795
// start bg thread
17821796
auto t = std::thread(&serverEntryPoint,port);
17831797
t.detach();
1784-
1798+
17851799
Scheduler* scheduler = Director::getInstance()->getScheduler();
17861800
scheduler->scheduleUpdate(this->_runLoop, 0, false);
17871801
}

frameworks/js-bindings/bindings/manual/chipmunk/js_bindings_chipmunk_manual.cpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "jsfriendapi.h"
2626
#include "extensions/cocos-ext.h"
2727
#include "js_bindings_config.h"
28+
#include "cocos2d_specifics.hpp"
2829
#ifdef JSB_INCLUDE_CHIPMUNK
2930

3031
#include "js_bindings_chipmunk_manual.h"
@@ -581,33 +582,15 @@ void JSPROXY_CCPhysicsSprite_createClass(JSContext *cx, JS::HandleObject globalO
581582

582583

583584
void register_CCPhysicsSprite(JSContext *cx, JS::HandleObject obj) {
584-
JS::RootedValue nsval(cx);
585-
JS::RootedObject ns(cx);
586-
JS_GetProperty(cx, obj, "cc", &nsval);
587-
if (nsval == JSVAL_VOID) {
588-
ns = JS_NewObject(cx, NULL, JS::NullPtr(), JS::NullPtr());
589-
nsval = OBJECT_TO_JSVAL(ns);
590-
JS_SetProperty(cx, obj, "cc", nsval);
591-
} else {
592-
JS_ValueToObject(cx, nsval, &ns);
593-
}
594-
//obj = ns;
595-
JSPROXY_CCPhysicsSprite_createClass(cx, ns);
585+
JS::RootedObject ccObj(cx);
586+
get_or_create_js_obj(cx, obj, "cc", &ccObj);
587+
JSPROXY_CCPhysicsSprite_createClass(cx, ccObj);
596588
}
597589

598590
void register_CCPhysicsDebugNode(JSContext *cx, JS::HandleObject obj) {
599-
JS::RootedValue nsval(cx);
600-
JS::RootedObject ns(cx);
601-
JS_GetProperty(cx, obj, "cc", &nsval);
602-
if (nsval == JSVAL_VOID) {
603-
ns = JS_NewObject(cx, NULL, JS::NullPtr(), JS::NullPtr());
604-
nsval = OBJECT_TO_JSVAL(ns);
605-
JS_SetProperty(cx, obj, "cc", nsval);
606-
} else {
607-
JS_ValueToObject(cx, nsval, &ns);
608-
}
609-
//obj = ns;
610-
JSB_CCPhysicsDebugNode_createClass(cx, ns, "PhysicsDebugNode");
591+
JS::RootedObject ccObj(cx);
592+
get_or_create_js_obj(cx, obj, "cc", &ccObj);
593+
JSB_CCPhysicsDebugNode_createClass(cx, ccObj, "PhysicsDebugNode");
611594
}
612595

613596
bool jsval_to_cpBB( JSContext *cx, jsval vp, cpBB *ret )

0 commit comments

Comments
 (0)