From 330b588215da65b04ee430528151bc212d5fc877 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 14 Dec 2015 13:13:54 +0800 Subject: [PATCH 1/2] Fix evalString doesn't return result issue --- cocos/scripting/js-bindings/manual/ScriptingCore.cpp | 10 ++-------- cocos/scripting/js-bindings/manual/ScriptingCore.h | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index ae2481d067..85fb7e0c9d 100644 --- a/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -500,16 +500,10 @@ void ScriptingCore::string_report(JS::HandleValue val) { } } -bool ScriptingCore::evalString(const char *string, jsval *outVal, const char *filename, JSContext* cx, JSObject* global) +bool ScriptingCore::evalString(const char *string, JS::MutableHandleValue outVal, JSContext* cx, JS::HandleObject global, const char *filename) { - if (cx == NULL) - cx = _cx; - if (global == NULL) - global = _global.ref().get(); - JSAutoCompartment ac(cx, global); - JS::RootedObject jsglobal(cx, global); - return JS_EvaluateScript(cx, jsglobal, string, (unsigned)strlen(string), "ScriptingCore::evalString", 1); + return JS_EvaluateScript(cx, global, string, (unsigned)strlen(string), "ScriptingCore::evalString", 1, outVal); } void ScriptingCore::start() diff --git a/cocos/scripting/js-bindings/manual/ScriptingCore.h b/cocos/scripting/js-bindings/manual/ScriptingCore.h index 336909455c..e167569cb8 100644 --- a/cocos/scripting/js-bindings/manual/ScriptingCore.h +++ b/cocos/scripting/js-bindings/manual/ScriptingCore.h @@ -234,7 +234,7 @@ public: * @param global @~english The js global object * @return @~english Return true if successfully invoked, otherwise return false. */ - bool evalString(const char *string, jsval *outVal, const char *filename = NULL, JSContext* cx = NULL, JSObject* global = NULL); + bool evalString(const char *string, JS::MutableHandleValue outVal, JSContext* cx, JS::HandleObject global, const char *filename = NULL); /** @brief @~english Get script object for the given path From 662c7fbec50e7a6802cb6febe8db5cc05a255ca2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 14 Dec 2015 13:55:17 +0800 Subject: [PATCH 2/2] Made API consistent with old version --- .../js-bindings/manual/ScriptingCore.cpp | 13 ++++++++++++- .../js-bindings/manual/ScriptingCore.h | 17 ++++++++++++++++- .../platform/android/CCJavascriptJavaBridge.cpp | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index 85fb7e0c9d..aab6456212 100644 --- a/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -500,12 +500,23 @@ void ScriptingCore::string_report(JS::HandleValue val) { } } -bool ScriptingCore::evalString(const char *string, JS::MutableHandleValue outVal, JSContext* cx, JS::HandleObject global, const char *filename) +bool ScriptingCore::evalString(const char *string, JS::MutableHandleValue outVal, const char *filename, JSContext* cx, JS::HandleObject global) { JSAutoCompartment ac(cx, global); return JS_EvaluateScript(cx, global, string, (unsigned)strlen(string), "ScriptingCore::evalString", 1, outVal); } +bool ScriptingCore::evalString(const char *string, JS::MutableHandleValue outVal) +{ + return evalString(string, outVal, nullptr, _cx, _global.ref()); +} + +bool ScriptingCore::evalString(const char *string) +{ + JS::RootedValue retVal(_cx); + return evalString(string, &retVal); +} + void ScriptingCore::start() { // for now just this diff --git a/cocos/scripting/js-bindings/manual/ScriptingCore.h b/cocos/scripting/js-bindings/manual/ScriptingCore.h index e167569cb8..748694e518 100644 --- a/cocos/scripting/js-bindings/manual/ScriptingCore.h +++ b/cocos/scripting/js-bindings/manual/ScriptingCore.h @@ -234,7 +234,22 @@ public: * @param global @~english The js global object * @return @~english Return true if successfully invoked, otherwise return false. */ - bool evalString(const char *string, JS::MutableHandleValue outVal, JSContext* cx, JS::HandleObject global, const char *filename = NULL); + bool evalString(const char *string, JS::MutableHandleValue outVal, const char *filename, JSContext* cx, JS::HandleObject global); + + /**@~english + * Evaluate the specified js code string + * @param string @~english The string with the javascript code to be evaluated + * @param outVal @~english The jsval that will hold the return value of the evaluation. + * @return @~english Return true if successfully invoked, otherwise return false. + */ + bool evalString(const char *string, JS::MutableHandleValue outVal); + + /**@~english + * Evaluate the specified js code string + * @param string @~english The string with the javascript code to be evaluated + * @return @~english Return true if successfully invoked, otherwise return false. + */ + bool evalString(const char *string); /** @brief @~english Get script object for the given path diff --git a/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp b/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp index 8b65563ebf..419dc4b603 100644 --- a/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp +++ b/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp @@ -46,7 +46,7 @@ JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxJavascriptJavaBridge_evalSt CCLOG("Cocos2dxJavascriptJavaBridge_evalString error, invalid string code"); return 0; } - ScriptingCore::getInstance()->evalString(strValue.c_str(), nullptr); + ScriptingCore::getInstance()->evalString(strValue.c_str()); return 1; }