From 65eb5fa4bab68c8b502cbc277bfdb4fb108466a4 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 9 Dec 2015 22:40:57 +0800 Subject: [PATCH] Bug fix for shouldStartLoading method, should return value from JS, also fix that webview callback is already in Cocos Thread, don't need to post to cocos thread for running again. --- ...b_cocos2dx_experimental_webView_manual.cpp | 118 ++++++++---------- 1 file changed, 53 insertions(+), 65 deletions(-) diff --git a/cocos/scripting/js-bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp b/cocos/scripting/js-bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp index 08b0e37cac..27badfad61 100644 --- a/cocos/scripting/js-bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp +++ b/cocos/scripting/js-bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp @@ -20,24 +20,21 @@ static bool jsb_cocos2dx_experimental_webView_setOnShouldStartLoading(JSContext if(argc == 1){ std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); cobj->setOnShouldStartLoading([=](experimental::ui::WebView *sender, const std::string &url)->bool{ - Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] - { - JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET - jsval arg[2]; - js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); - if(proxy) - arg[0] = OBJECT_TO_JSVAL(proxy->obj); - else - arg[0] = JSVAL_NULL; - arg[1] = std_string_to_jsval(cx, url); - JS::RootedValue rval(cx); + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); - bool ok = func->invoke(2, arg, &rval); - if (!ok && JS_IsExceptionPending(cx)) { - JS_ReportPendingException(cx); - } - }); - return true; + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + return rval.toBoolean(); }); return true; } @@ -54,23 +51,20 @@ static bool jsb_cocos2dx_experimental_webView_setOnDidFinishLoading(JSContext *c if(argc == 1){ std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); cobj->setOnDidFinishLoading([=](experimental::ui::WebView *sender, const std::string &url)->void{ - Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] - { - JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET - jsval arg[2]; - js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); - if(proxy) - arg[0] = OBJECT_TO_JSVAL(proxy->obj); - else - arg[0] = JSVAL_NULL; - arg[1] = std_string_to_jsval(cx, url); - JS::RootedValue rval(cx); + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); - bool ok = func->invoke(2, arg, &rval); - if (!ok && JS_IsExceptionPending(cx)) { - JS_ReportPendingException(cx); - } - }); + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } }); return true; } @@ -87,23 +81,20 @@ static bool jsb_cocos2dx_experimental_webView_setOnDidFailLoading(JSContext *cx, if(argc == 1){ std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); cobj->setOnDidFailLoading([=](experimental::ui::WebView *sender, const std::string &url)->void{ - Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] - { - JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET - jsval arg[2]; - js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); - if(proxy) - arg[0] = OBJECT_TO_JSVAL(proxy->obj); - else - arg[0] = JSVAL_NULL; - arg[1] = std_string_to_jsval(cx, url); - JS::RootedValue rval(cx); + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); - bool ok = func->invoke(2, arg, &rval); - if (!ok && JS_IsExceptionPending(cx)) { - JS_ReportPendingException(cx); - } - }); + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } }); return true; } @@ -120,23 +111,20 @@ static bool jsb_cocos2dx_experimental_webView_setOnJSCallback(JSContext *cx, uin if(argc == 1){ std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); cobj->setOnJSCallback([=](experimental::ui::WebView *sender, const std::string &url)->void{ - Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] - { - JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET - jsval arg[2]; - js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); - if(proxy) - arg[0] = OBJECT_TO_JSVAL(proxy->obj); - else - arg[0] = JSVAL_NULL; - arg[1] = std_string_to_jsval(cx, url); - JS::RootedValue rval(cx); + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); - bool ok = func->invoke(2, arg, &rval); - if (!ok && JS_IsExceptionPending(cx)) { - JS_ReportPendingException(cx); - } - }); + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } }); return true; }