2012-08-28 10:22:36 +08:00
|
|
|
//
|
|
|
|
// ScriptingCore.cpp
|
|
|
|
// testmonkey
|
|
|
|
//
|
|
|
|
// Created by Rolando Abarca on 3/14/12.
|
|
|
|
// Copyright (c) 2012 Zynga Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <vector>
|
2012-10-19 08:44:41 +08:00
|
|
|
#include <map>
|
2012-08-28 10:22:36 +08:00
|
|
|
#include "ScriptingCore.h"
|
2012-10-19 08:44:41 +08:00
|
|
|
#include "jsdbgapi.h"
|
2012-08-28 10:22:36 +08:00
|
|
|
#include "cocos2d.h"
|
2013-01-07 18:06:53 +08:00
|
|
|
#include "LocalStorage.h"
|
2012-09-19 09:44:21 +08:00
|
|
|
#include "cocos2d_specifics.hpp"
|
2013-01-11 18:57:33 +08:00
|
|
|
#include "js_bindings_config.h"
|
2012-10-19 08:44:41 +08:00
|
|
|
// for debug socket
|
2012-10-19 11:15:23 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
#include <io.h>
|
2013-01-17 17:07:18 +08:00
|
|
|
#include <WS2tcpip.h>
|
2012-10-19 11:15:23 +08:00
|
|
|
#else
|
2012-10-19 08:44:41 +08:00
|
|
|
#include <sys/socket.h>
|
2013-02-18 15:12:39 +08:00
|
|
|
#include <unistd.h>
|
2012-10-19 08:44:41 +08:00
|
|
|
#include <netdb.h>
|
2012-10-19 11:15:23 +08:00
|
|
|
#endif
|
2013-06-26 14:41:19 +08:00
|
|
|
|
|
|
|
#include <thread>
|
2012-08-28 10:22:36 +08:00
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <jni/JniHelper.h>
|
2012-10-19 13:41:03 +08:00
|
|
|
#include <netinet/in.h>
|
2012-08-28 10:22:36 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
#define LOG_TAG "ScriptingCore.cpp"
|
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define LOGD(...) js_log(__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
#include "js_bindings_config.h"
|
|
|
|
|
2013-05-14 11:48:49 +08:00
|
|
|
#if DEBUG
|
|
|
|
#define TRACE_DEBUGGER_SERVER(...) CCLOG(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define TRACE_DEBUGGER_SERVER(...)
|
|
|
|
#endif // #if DEBUG
|
|
|
|
|
2013-05-06 11:32:23 +08:00
|
|
|
#define BYTE_CODE_FILE_EXT ".jsc"
|
2013-01-17 17:07:18 +08:00
|
|
|
|
2013-06-21 22:29:48 +08:00
|
|
|
static string inData;
|
|
|
|
static string outData;
|
|
|
|
static vector<string> g_queue;
|
2013-06-26 14:41:19 +08:00
|
|
|
static std::mutex g_qMutex;
|
|
|
|
static std::mutex g_rwMutex;
|
2013-06-21 22:29:48 +08:00
|
|
|
static bool vmLock = false;
|
|
|
|
static jsval frame = JSVAL_NULL, script = JSVAL_NULL;
|
|
|
|
static int clientSocket = -1;
|
2013-01-12 06:01:36 +08:00
|
|
|
|
|
|
|
// server entry point for the bg thread
|
2013-06-26 14:41:19 +08:00
|
|
|
static void serverEntryPoint(void);
|
2013-01-12 06:01:36 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
js_proxy_t *_native_js_global_ht = NULL;
|
|
|
|
js_proxy_t *_js_native_global_ht = NULL;
|
|
|
|
js_type_class_t *_js_global_type_ht = NULL;
|
2013-06-21 22:29:48 +08:00
|
|
|
static char *_js_log_buf = NULL;
|
2012-08-28 10:22:36 +08:00
|
|
|
|
2013-06-21 22:29:48 +08:00
|
|
|
static std::vector<sc_register_sth> registrationList;
|
2012-08-28 10:22:36 +08:00
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
// name ~> JSScript map
|
2013-06-21 22:29:48 +08:00
|
|
|
static std::map<std::string, JSScript*> filename_script;
|
2012-10-19 08:44:41 +08:00
|
|
|
// port ~> socket map
|
2013-06-21 22:29:48 +08:00
|
|
|
static std::map<int,int> ports_sockets;
|
2012-10-19 08:44:41 +08:00
|
|
|
// name ~> globals
|
2013-06-21 22:29:48 +08:00
|
|
|
static std::map<std::string, js::RootedObject*> globals;
|
2012-10-19 08:44:41 +08:00
|
|
|
|
2012-10-20 00:54:21 +08:00
|
|
|
static void executeJSFunctionFromReservedSpot(JSContext *cx, JSObject *obj,
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval &dataVal, jsval &retval) {
|
|
|
|
|
|
|
|
jsval func = JS_GetReservedSlot(obj, 0);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
if(func == JSVAL_VOID) { return; }
|
|
|
|
jsval thisObj = JS_GetReservedSlot(obj, 1);
|
2013-02-22 00:34:51 +08:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
2012-08-28 10:22:36 +08:00
|
|
|
if(thisObj == JSVAL_VOID) {
|
|
|
|
JS_CallFunctionValue(cx, obj, func, 1, &dataVal, &retval);
|
|
|
|
} else {
|
|
|
|
assert(!JSVAL_IS_PRIMITIVE(thisObj));
|
|
|
|
JS_CallFunctionValue(cx, JSVAL_TO_OBJECT(thisObj), func, 1, &dataVal, &retval);
|
2012-10-20 00:54:21 +08:00
|
|
|
}
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
static void getTouchesFuncName(int eventType, std::string &funcName) {
|
|
|
|
switch(eventType) {
|
|
|
|
case CCTOUCHBEGAN:
|
|
|
|
funcName = "onTouchesBegan";
|
|
|
|
break;
|
|
|
|
case CCTOUCHENDED:
|
|
|
|
funcName = "onTouchesEnded";
|
|
|
|
break;
|
|
|
|
case CCTOUCHMOVED:
|
|
|
|
funcName = "onTouchesMoved";
|
|
|
|
break;
|
|
|
|
case CCTOUCHCANCELLED:
|
|
|
|
funcName = "onTouchesCancelled";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void getTouchFuncName(int eventType, std::string &funcName) {
|
|
|
|
switch(eventType) {
|
|
|
|
case CCTOUCHBEGAN:
|
|
|
|
funcName = "onTouchBegan";
|
|
|
|
break;
|
|
|
|
case CCTOUCHENDED:
|
|
|
|
funcName = "onTouchEnded";
|
|
|
|
break;
|
|
|
|
case CCTOUCHMOVED:
|
|
|
|
funcName = "onTouchMoved";
|
|
|
|
break;
|
|
|
|
case CCTOUCHCANCELLED:
|
|
|
|
funcName = "onTouchCancelled";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rootObject(JSContext *cx, JSObject *obj) {
|
|
|
|
JS_AddNamedObjectRoot(cx, &obj, "unnamed");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void unRootObject(JSContext *cx, JSObject *obj) {
|
|
|
|
JS_RemoveObjectRoot(cx, &obj);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
static void getJSTouchObject(JSContext *cx, Touch *x, jsval &jsret) {
|
|
|
|
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::Touch>(cx, x);
|
fixed #1617: Some improvements for JS Bindings.
1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff
[Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android.
Then cast it to unsigned int, the value will be 0. The schedule will not be able to work.
I don't know why this occurs only on android.
[Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++.
2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject.
3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks.
4) Improvments for JSTouchDelegate. Added four functions as follows:
// Set the touch delegate to map by using the key (pJSObj).
static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate);
// Get the touch delegate by the key (pJSObj).
static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj);
// Remove the delegate by the key (pJSObj).
static void removeDelegateForJSObject(JSObject* pJSObj);
void unregisterTouchDelegate();
And exported cc.unregisterTouchDelegate(); to js.
Fix a memory leak for JSTouchDelegate by making it as an autorelease object.
5) Don't add js callback function to the reserved slot of object.
[Reason]: The target object may execute more than one schedule.
Therefore, previous js callback function will be replaced
by the current one. For example:
this.scheduleOnce(function() { temporary function 1 }, 0.5);
this.scheduleOnce(function() { temporary function 2 }, 0.5);
In this case, the temporary function 1 will be removed from reserved slot 0.
And temporary function 2 will be set to reserved slot 0 of this object.
If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked,
crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce.
[Solution] Because one schedule corresponds to one JSScheduleWrapper, we root
the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it
at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
|
|
|
jsret = OBJECT_TO_JSVAL(proxy->obj);
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
static void removeJSTouchObject(JSContext *cx, Touch *x, jsval &jsret) {
|
2012-09-11 14:02:33 +08:00
|
|
|
js_proxy_t* nproxy;
|
|
|
|
js_proxy_t* jsproxy;
|
fixed #1617: Some improvements for JS Bindings.
1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff
[Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android.
Then cast it to unsigned int, the value will be 0. The schedule will not be able to work.
I don't know why this occurs only on android.
[Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++.
2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject.
3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks.
4) Improvments for JSTouchDelegate. Added four functions as follows:
// Set the touch delegate to map by using the key (pJSObj).
static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate);
// Get the touch delegate by the key (pJSObj).
static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj);
// Remove the delegate by the key (pJSObj).
static void removeDelegateForJSObject(JSObject* pJSObj);
void unregisterTouchDelegate();
And exported cc.unregisterTouchDelegate(); to js.
Fix a memory leak for JSTouchDelegate by making it as an autorelease object.
5) Don't add js callback function to the reserved slot of object.
[Reason]: The target object may execute more than one schedule.
Therefore, previous js callback function will be replaced
by the current one. For example:
this.scheduleOnce(function() { temporary function 1 }, 0.5);
this.scheduleOnce(function() { temporary function 2 }, 0.5);
In this case, the temporary function 1 will be removed from reserved slot 0.
And temporary function 2 will be set to reserved slot 0 of this object.
If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked,
crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce.
[Solution] Because one schedule corresponds to one JSScheduleWrapper, we root
the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it
at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
|
|
|
void *ptr = (void*)x;
|
2013-06-04 12:53:42 +08:00
|
|
|
nproxy = jsb_get_native_proxy(ptr);
|
2012-09-11 14:02:33 +08:00
|
|
|
if (nproxy) {
|
2013-06-04 12:53:42 +08:00
|
|
|
jsproxy = jsb_get_js_proxy(nproxy->obj);
|
fixed #1617: Some improvements for JS Bindings.
1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff
[Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android.
Then cast it to unsigned int, the value will be 0. The schedule will not be able to work.
I don't know why this occurs only on android.
[Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++.
2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject.
3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks.
4) Improvments for JSTouchDelegate. Added four functions as follows:
// Set the touch delegate to map by using the key (pJSObj).
static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate);
// Get the touch delegate by the key (pJSObj).
static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj);
// Remove the delegate by the key (pJSObj).
static void removeDelegateForJSObject(JSObject* pJSObj);
void unregisterTouchDelegate();
And exported cc.unregisterTouchDelegate(); to js.
Fix a memory leak for JSTouchDelegate by making it as an autorelease object.
5) Don't add js callback function to the reserved slot of object.
[Reason]: The target object may execute more than one schedule.
Therefore, previous js callback function will be replaced
by the current one. For example:
this.scheduleOnce(function() { temporary function 1 }, 0.5);
this.scheduleOnce(function() { temporary function 2 }, 0.5);
In this case, the temporary function 1 will be removed from reserved slot 0.
And temporary function 2 will be set to reserved slot 0 of this object.
If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked,
crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce.
[Solution] Because one schedule corresponds to one JSScheduleWrapper, we root
the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it
at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
|
|
|
JS_RemoveObjectRoot(cx, &jsproxy->obj);
|
2013-06-04 12:53:42 +08:00
|
|
|
jsb_remove_proxy(nproxy, jsproxy);
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
void ScriptingCore::executeJSFunctionWithThisObj(jsval thisObj, jsval callback,
|
2012-10-12 08:57:52 +08:00
|
|
|
jsval *data) {
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval retval;
|
|
|
|
if(callback != JSVAL_VOID || thisObj != JSVAL_VOID) {
|
2012-10-19 08:44:41 +08:00
|
|
|
JS_CallFunctionValue(cx_, JSVAL_TO_OBJECT(thisObj), callback, 1, data, &retval);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void js_log(const char *format, ...) {
|
|
|
|
if (_js_log_buf == NULL) {
|
2013-04-17 16:08:48 +08:00
|
|
|
_js_log_buf = (char *)calloc(sizeof(char), kMaxLogLen+1);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
va_list vl;
|
|
|
|
va_start(vl, format);
|
2013-04-17 16:08:48 +08:00
|
|
|
int len = vsnprintf(_js_log_buf, kMaxLogLen, format, vl);
|
2012-08-28 10:22:36 +08:00
|
|
|
va_end(vl);
|
|
|
|
if (len) {
|
2012-08-28 05:47:21 +08:00
|
|
|
CCLOG("JS: %s\n", _js_log_buf);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
#define JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES 1
|
|
|
|
|
2013-01-05 15:09:19 +08:00
|
|
|
JSBool JSBCore_platform(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
if (argc!=0)
|
2013-01-05 15:09:19 +08:00
|
|
|
{
|
|
|
|
JS_ReportError(cx, "Invalid number of arguments in __getPlatform");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
JSString * platform;
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-05 15:09:19 +08:00
|
|
|
// config.deviceType: Device Type
|
|
|
|
// 'mobile' for any kind of mobile devices, 'desktop' for PCs, 'browser' for Web Browsers
|
|
|
|
// #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
// platform = JS_InternString(_cx, "desktop");
|
|
|
|
// #else
|
|
|
|
platform = JS_InternString(cx, "mobile");
|
|
|
|
// #endif
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
jsval ret = STRING_TO_JSVAL(platform);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
JS_SET_RVAL(cx, vp, ret);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
return JS_TRUE;
|
2013-01-05 15:09:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JSBool JSBCore_version(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
2013-01-05 17:41:19 +08:00
|
|
|
if (argc!=0)
|
2013-01-05 15:09:19 +08:00
|
|
|
{
|
|
|
|
JS_ReportError(cx, "Invalid number of arguments in __getVersion");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
char version[256];
|
|
|
|
snprintf(version, sizeof(version)-1, "%s - %s", cocos2dVersion(), JSB_version);
|
|
|
|
JSString * js_version = JS_InternString(cx, version);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
jsval ret = STRING_TO_JSVAL(js_version);
|
|
|
|
JS_SET_RVAL(cx, vp, ret);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
return JS_TRUE;
|
2013-01-05 15:09:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JSBool JSBCore_os(JSContext *cx, uint32_t argc, jsval *vp)
|
2012-10-19 08:44:41 +08:00
|
|
|
{
|
2013-01-05 17:41:19 +08:00
|
|
|
if (argc!=0)
|
2013-01-05 15:09:19 +08:00
|
|
|
{
|
|
|
|
JS_ReportError(cx, "Invalid number of arguments in __getOS");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
JSString * os;
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
// osx, ios, android, windows, linux, etc..
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "ios");
|
2012-10-19 08:44:41 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "android");
|
2012-10-19 08:44:41 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "windows");
|
2012-10-19 08:44:41 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "marmalade");
|
2012-10-19 08:44:41 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "linux");
|
2012-10-19 08:44:41 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_BADA)
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "bada");
|
2012-10-19 08:44:41 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "blackberry");
|
2012-10-19 08:44:41 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "osx");
|
2012-10-19 08:44:41 +08:00
|
|
|
#else
|
2013-01-05 15:09:19 +08:00
|
|
|
os = JS_InternString(cx, "unknown");
|
2012-10-19 08:44:41 +08:00
|
|
|
#endif
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
jsval ret = STRING_TO_JSVAL(os);
|
|
|
|
JS_SET_RVAL(cx, vp, ret);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
return JS_TRUE;
|
2013-01-05 15:09:19 +08:00
|
|
|
};
|
2012-10-19 08:44:41 +08:00
|
|
|
|
2013-03-18 18:24:37 +08:00
|
|
|
JSBool JSB_core_restartVM(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
JSB_PRECONDITION2(argc==0, cx, JS_FALSE, "Invalid number of arguments in executeScript");
|
|
|
|
ScriptingCore::getInstance()->reset();
|
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
return JS_TRUE;
|
|
|
|
};
|
2012-10-10 15:24:20 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
void registerDefaultClasses(JSContext* cx, JSObject* global) {
|
2012-10-10 15:24:20 +08:00
|
|
|
// first, try to get the ns
|
|
|
|
jsval nsval;
|
|
|
|
JSObject *ns;
|
|
|
|
JS_GetProperty(cx, global, "cc", &nsval);
|
|
|
|
if (nsval == JSVAL_VOID) {
|
|
|
|
ns = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
nsval = OBJECT_TO_JSVAL(ns);
|
|
|
|
JS_SetProperty(cx, global, "cc", &nsval);
|
|
|
|
} else {
|
|
|
|
JS_ValueToObject(cx, nsval, &ns);
|
|
|
|
}
|
|
|
|
|
2012-10-20 00:54:21 +08:00
|
|
|
//
|
2012-08-28 10:22:36 +08:00
|
|
|
// Javascript controller (__jsc__)
|
|
|
|
//
|
|
|
|
JSObject *jsc = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
jsval jscVal = OBJECT_TO_JSVAL(jsc);
|
|
|
|
JS_SetProperty(cx, global, "__jsc__", &jscVal);
|
|
|
|
|
|
|
|
JS_DefineFunction(cx, jsc, "garbageCollect", ScriptingCore::forceGC, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
|
|
|
|
JS_DefineFunction(cx, jsc, "dumpRoot", ScriptingCore::dumpRoot, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
|
|
|
|
JS_DefineFunction(cx, jsc, "addGCRootObject", ScriptingCore::addRootJS, 1, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
|
|
|
|
JS_DefineFunction(cx, jsc, "removeGCRootObject", ScriptingCore::removeRootJS, 1, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
|
|
|
|
JS_DefineFunction(cx, jsc, "executeScript", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
|
|
|
|
|
|
|
|
// register some global functions
|
|
|
|
JS_DefineFunction(cx, global, "require", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, global, "log", ScriptingCore::log, 0, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, global, "executeScript", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, global, "forceGC", ScriptingCore::forceGC, 0, JSPROP_READONLY | JSPROP_PERMANENT);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-05 15:09:19 +08:00
|
|
|
JS_DefineFunction(cx, global, "__getPlatform", JSBCore_platform, 0, JSPROP_READONLY | JSPROP_PERMANENT);
|
2013-02-22 00:34:51 +08:00
|
|
|
JS_DefineFunction(cx, global, "__getOS", JSBCore_os, 0, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, global, "__getVersion", JSBCore_version, 0, JSPROP_READONLY | JSPROP_PERMANENT);
|
2013-03-18 18:24:37 +08:00
|
|
|
JS_DefineFunction(cx, global, "__restartVM", JSB_core_restartVM, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-04-25 20:56:27 +08:00
|
|
|
static void sc_finalize(JSFreeOp *freeOp, JSObject *obj) {
|
|
|
|
CCLOGINFO("jsbindings: finalizing JS object %p (global class)", obj);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSClass global_class = {
|
|
|
|
"global", JSCLASS_GLOBAL_FLAGS,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, sc_finalize,
|
|
|
|
JSCLASS_NO_OPTIONAL_MEMBERS
|
|
|
|
};
|
|
|
|
|
|
|
|
ScriptingCore::ScriptingCore()
|
2012-10-19 08:44:41 +08:00
|
|
|
: rt_(NULL)
|
|
|
|
, cx_(NULL)
|
|
|
|
, global_(NULL)
|
2012-11-16 03:14:57 +08:00
|
|
|
, debugGlobal_(NULL)
|
2012-08-28 10:22:36 +08:00
|
|
|
{
|
|
|
|
// set utf8 strings internally (we don't need utf16)
|
2013-02-27 15:16:49 +08:00
|
|
|
// XXX: Removed in SpiderMonkey 19.0
|
|
|
|
//JS_SetCStringsAreUTF8();
|
2012-08-28 10:22:36 +08:00
|
|
|
this->addRegisterCallback(registerDefaultClasses);
|
2013-02-22 00:34:51 +08:00
|
|
|
this->runLoop = new SimpleRunLoop();
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptingCore::string_report(jsval val) {
|
|
|
|
if (JSVAL_IS_NULL(val)) {
|
|
|
|
LOGD("val : (JSVAL_IS_NULL(val)");
|
|
|
|
// return 1;
|
|
|
|
} else if ((JSVAL_IS_BOOLEAN(val)) &&
|
|
|
|
(JS_FALSE == (JSVAL_TO_BOOLEAN(val)))) {
|
|
|
|
LOGD("val : (return value is JS_FALSE");
|
|
|
|
// return 1;
|
|
|
|
} else if (JSVAL_IS_STRING(val)) {
|
|
|
|
JSString *str = JS_ValueToString(this->getGlobalContext(), val);
|
|
|
|
if (NULL == str) {
|
|
|
|
LOGD("val : return string is NULL");
|
|
|
|
} else {
|
2013-02-22 00:34:51 +08:00
|
|
|
JSStringWrapper wrapper(str);
|
2012-11-16 03:14:37 +08:00
|
|
|
LOGD("val : return string =\n%s\n", (char *)wrapper);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
} else if (JSVAL_IS_NUMBER(val)) {
|
|
|
|
double number;
|
|
|
|
if (JS_FALSE ==
|
|
|
|
JS_ValueToNumber(this->getGlobalContext(), val, &number)) {
|
|
|
|
LOGD("val : return number could not be converted");
|
|
|
|
} else {
|
|
|
|
LOGD("val : return number =\n%f", number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
JSBool ScriptingCore::evalString(const char *string, jsval *outVal, const char *filename, JSContext* cx, JSObject* global)
|
2012-08-28 10:22:36 +08:00
|
|
|
{
|
2012-10-19 11:15:23 +08:00
|
|
|
if (cx == NULL)
|
|
|
|
cx = cx_;
|
|
|
|
if (global == NULL)
|
|
|
|
global = global_;
|
|
|
|
JSScript* script = JS_CompileScript(cx, global, string, strlen(string), filename, 1);
|
|
|
|
if (script) {
|
|
|
|
// JSAutoCompartment ac(cx, global);
|
2013-02-22 00:34:51 +08:00
|
|
|
JSAutoCompartment ac(cx, global);
|
2013-05-02 09:03:24 +08:00
|
|
|
JSBool evaluatedOK = JS_ExecuteScript(cx, global, script, outVal);
|
2012-10-19 11:15:23 +08:00
|
|
|
if (JS_FALSE == evaluatedOK) {
|
|
|
|
fprintf(stderr, "(evaluatedOK == JS_FALSE)\n");
|
|
|
|
}
|
|
|
|
return evaluatedOK;
|
|
|
|
}
|
2013-04-25 20:56:27 +08:00
|
|
|
return JS_FALSE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptingCore::start() {
|
|
|
|
// for now just this
|
|
|
|
this->createGlobalContext();
|
2013-06-01 09:01:05 +08:00
|
|
|
#if JSB_ENABLE_DEBUGGER
|
|
|
|
this->enableDebugger();
|
|
|
|
#endif //JSB_ENABLE_DEBUGGER
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptingCore::addRegisterCallback(sc_register_sth callback) {
|
|
|
|
registrationList.push_back(callback);
|
|
|
|
}
|
|
|
|
|
2012-09-19 09:44:21 +08:00
|
|
|
void ScriptingCore::removeAllRoots(JSContext *cx) {
|
|
|
|
js_proxy_t *current, *tmp;
|
|
|
|
HASH_ITER(hh, _js_native_global_ht, current, tmp) {
|
|
|
|
JS_RemoveObjectRoot(cx, ¤t->obj);
|
2012-10-12 09:05:25 +08:00
|
|
|
HASH_DEL(_js_native_global_ht, current);
|
|
|
|
free(current);
|
|
|
|
}
|
|
|
|
HASH_ITER(hh, _native_js_global_ht, current, tmp) {
|
|
|
|
HASH_DEL(_native_js_global_ht, current);
|
|
|
|
free(current);
|
2012-09-19 09:44:21 +08:00
|
|
|
}
|
|
|
|
HASH_CLEAR(hh, _js_native_global_ht);
|
|
|
|
HASH_CLEAR(hh, _native_js_global_ht);
|
|
|
|
}
|
|
|
|
|
2013-04-25 20:56:27 +08:00
|
|
|
static JSPrincipals shellTrustedPrincipals = { 1 };
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
CheckObjectAccess(JSContext *cx, js::HandleObject obj, js::HandleId id, JSAccessMode mode,
|
|
|
|
js::MutableHandleValue vp)
|
|
|
|
{
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSSecurityCallbacks securityCallbacks = {
|
|
|
|
CheckObjectAccess,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
void ScriptingCore::createGlobalContext() {
|
2012-10-19 08:44:41 +08:00
|
|
|
if (this->cx_ && this->rt_) {
|
|
|
|
ScriptingCore::removeAllRoots(this->cx_);
|
|
|
|
JS_DestroyContext(this->cx_);
|
|
|
|
JS_DestroyRuntime(this->rt_);
|
|
|
|
this->cx_ = NULL;
|
|
|
|
this->rt_ = NULL;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
2013-04-25 20:56:27 +08:00
|
|
|
// Removed from Spidermonkey 19.
|
2013-01-25 09:09:59 +08:00
|
|
|
//JS_SetCStringsAreUTF8();
|
2013-04-25 20:56:27 +08:00
|
|
|
this->rt_ = JS_NewRuntime(8L * 1024L * 1024L, JS_USE_HELPER_THREADS);
|
|
|
|
JS_SetGCParameter(rt_, JSGC_MAX_BYTES, 0xffffffff);
|
|
|
|
|
|
|
|
JS_SetTrustedPrincipals(rt_, &shellTrustedPrincipals);
|
|
|
|
JS_SetSecurityCallbacks(rt_, &securityCallbacks);
|
|
|
|
JS_SetNativeStackQuota(rt_, JSB_MAX_STACK_QUOTA);
|
|
|
|
|
|
|
|
this->cx_ = JS_NewContext(rt_, 8192);
|
2012-10-19 08:44:41 +08:00
|
|
|
JS_SetOptions(this->cx_, JSOPTION_TYPE_INFERENCE);
|
|
|
|
JS_SetVersion(this->cx_, JSVERSION_LATEST);
|
2013-05-22 17:31:07 +08:00
|
|
|
|
|
|
|
// Only disable METHODJIT on iOS.
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2012-10-19 08:44:41 +08:00
|
|
|
JS_SetOptions(this->cx_, JS_GetOptions(this->cx_) & ~JSOPTION_METHODJIT);
|
|
|
|
JS_SetOptions(this->cx_, JS_GetOptions(this->cx_) & ~JSOPTION_METHODJIT_ALWAYS);
|
2013-05-22 17:31:07 +08:00
|
|
|
#endif
|
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
JS_SetErrorReporter(this->cx_, ScriptingCore::reportError);
|
2012-12-01 10:17:20 +08:00
|
|
|
#if defined(JS_GC_ZEAL) && defined(DEBUG)
|
2012-12-04 17:23:08 +08:00
|
|
|
//JS_SetGCZeal(this->cx_, 2, JS_DEFAULT_ZEAL_FREQ);
|
issue #1581: JSBinding bug fixes.
Some fixes of JSBinding codes:
[1] Check whether the proxy was already added in JS_NEW_PROXY
[2] In struct schedFunc_proxy_t, JSScheduleWrapper* --> CCArray*
Reason: One js function may correspond to many targets. To debug this, you could refer to JSScheduleWrapper::dump function. It will prove that i'm right. :)
[3] In ScriptingCore::cleanupSchedulesAndActions function, we must invoke unschedule for all targets and remove the proxy both in _schedFunc_target_ht and _schedTarget_native_ht, otherwise the hash tables will grow bigger and bigger, so I added a new function JSScheduleWrapper::removeAllTargetsForNatiaveNode to make this things easier.
[4] To easily find out the bugs of binding codes, I add JS_SetGCZeal in ScriptingCore::createGlobalContext, it only works in DEBUG mode.
[5] In js_cocos2dx_CCNode_getChildren, we should add the generated array to root to avoid gc happen when invoking JS_SetElement.
[6] The JSCallFuncWrapper isn't needed since an action will be run by a cc.Node and it will be released at the CCNode::cleanup.
[7] Some improvements of JSScheduleWrapper class.
[8] Added a new function JSScheduleWrapper::setTarget, it's for js_CCNode_unschedule to find out which target need to be unscheduled.
[9] Commented JS_SetReservedSlot in js_CCNode_scheduleOnce and js_CCNode_schedule.
Reason:
For js_CCNode_scheduleOnce: Don't add the callback function to the reserved slot of this js object.Since the class of js object may be inherited from cocos class(e.g. cc.Sprite). The subclass will not contain reserved slots. It will crash if invoking this.
For js_CCNode_schedule: Don't add js callback function to the reserved slot of scheduler js object. Since the scheduler is an object always rooted.
So the callback function might not be released when gc comes.I looked inside the implementation of cc.Node.schedule, and it doesn't use JS_SetReservedSlot there.
2012-11-28 22:04:55 +08:00
|
|
|
#endif
|
2012-10-19 08:44:41 +08:00
|
|
|
this->global_ = NewGlobalObject(cx_);
|
2013-01-12 06:29:40 +08:00
|
|
|
#if JSB_ENABLE_DEBUGGER
|
2013-02-22 00:34:51 +08:00
|
|
|
JS_SetDebugMode(cx_, JS_TRUE);
|
2013-01-12 06:29:40 +08:00
|
|
|
#endif
|
2012-08-28 10:22:36 +08:00
|
|
|
for (std::vector<sc_register_sth>::iterator it = registrationList.begin(); it != registrationList.end(); it++) {
|
|
|
|
sc_register_sth callback = *it;
|
2012-10-19 08:44:41 +08:00
|
|
|
callback(this->cx_, this->global_);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 19:06:16 +08:00
|
|
|
static std::string RemoveFileExt(const std::string& filePath) {
|
2013-05-06 11:32:23 +08:00
|
|
|
size_t pos = filePath.rfind('.');
|
|
|
|
if (0 < pos) {
|
|
|
|
return filePath.substr(0, pos);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return filePath;
|
2013-04-30 03:29:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* cx)
|
2012-08-28 10:22:36 +08:00
|
|
|
{
|
2012-10-19 11:15:23 +08:00
|
|
|
if (!path) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-12 06:24:23 +08:00
|
|
|
cocos2d::FileUtils *futil = cocos2d::FileUtils::getInstance();
|
2013-05-06 19:06:16 +08:00
|
|
|
std::string fullPath = futil->fullPathForFilename(path);
|
2012-10-19 11:15:23 +08:00
|
|
|
if (global == NULL) {
|
|
|
|
global = global_;
|
|
|
|
}
|
|
|
|
if (cx == NULL) {
|
|
|
|
cx = cx_;
|
|
|
|
}
|
2013-04-30 03:29:56 +08:00
|
|
|
JSScript *script = NULL;
|
2013-02-27 16:57:36 +08:00
|
|
|
js::RootedObject obj(cx, global);
|
|
|
|
JS::CompileOptions options(cx);
|
2013-05-06 19:06:16 +08:00
|
|
|
options.setUTF8(true).setFileAndLine(fullPath.c_str(), 1);
|
2013-02-27 16:57:36 +08:00
|
|
|
|
2013-05-06 11:32:23 +08:00
|
|
|
// a) check js file first
|
2013-06-27 17:20:02 +08:00
|
|
|
std::string byteCodePath = RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT;
|
|
|
|
unsigned long length = 0;
|
|
|
|
void *data = futil->getFileData(byteCodePath.c_str(),
|
|
|
|
"rb",
|
|
|
|
&length);
|
|
|
|
if (data) {
|
|
|
|
script = JS_DecodeScript(cx, data, length, NULL, NULL);
|
2013-05-06 11:32:23 +08:00
|
|
|
}
|
2013-06-27 17:20:02 +08:00
|
|
|
|
|
|
|
// b) no jsc file, check js file
|
2013-05-06 11:32:23 +08:00
|
|
|
if (!script) {
|
2013-06-27 17:20:02 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
String* content = String::createWithContentsOfFile(path);
|
|
|
|
if (content) {
|
|
|
|
// Not supported in SpiderMonkey 19.0
|
|
|
|
//JSScript* script = JS_CompileScript(cx, global, (char*)content, contentSize, path, 1);
|
|
|
|
const char* contentCStr = content->getCString();
|
|
|
|
script = JS::Compile(cx, obj, options, contentCStr, strlen(contentCStr));
|
2013-05-06 11:32:23 +08:00
|
|
|
}
|
2013-06-27 17:20:02 +08:00
|
|
|
#else
|
|
|
|
script = JS::Compile(cx, obj, options, fullPath.c_str());
|
|
|
|
#endif
|
2013-04-30 03:29:56 +08:00
|
|
|
}
|
2013-06-27 17:20:02 +08:00
|
|
|
|
2012-10-19 11:15:23 +08:00
|
|
|
JSBool evaluatedOK = false;
|
|
|
|
if (script) {
|
|
|
|
jsval rval;
|
|
|
|
filename_script[path] = script;
|
2013-02-22 00:34:51 +08:00
|
|
|
JSAutoCompartment ac(cx, global);
|
2012-10-19 11:15:23 +08:00
|
|
|
evaluatedOK = JS_ExecuteScript(cx, global, script, &rval);
|
|
|
|
if (JS_FALSE == evaluatedOK) {
|
2013-07-24 06:20:22 +08:00
|
|
|
cocos2d::log("(evaluatedOK == JS_FALSE)");
|
2013-02-22 00:34:51 +08:00
|
|
|
JS_ReportPendingException(cx);
|
2012-10-19 11:15:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return evaluatedOK;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 00:33:37 +08:00
|
|
|
void ScriptingCore::reset()
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
ScriptingCore::~ScriptingCore()
|
2013-02-22 00:33:37 +08:00
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptingCore::cleanup()
|
2012-08-28 10:22:36 +08:00
|
|
|
{
|
2013-01-07 17:34:22 +08:00
|
|
|
localStorageFree();
|
2012-12-01 11:39:53 +08:00
|
|
|
removeAllRoots(cx_);
|
2013-02-22 00:33:37 +08:00
|
|
|
if (cx_)
|
|
|
|
{
|
|
|
|
JS_DestroyContext(cx_);
|
|
|
|
cx_ = NULL;
|
|
|
|
}
|
|
|
|
if (rt_)
|
|
|
|
{
|
|
|
|
JS_DestroyRuntime(rt_);
|
|
|
|
rt_ = NULL;
|
|
|
|
}
|
2012-08-28 10:22:36 +08:00
|
|
|
JS_ShutDown();
|
|
|
|
if (_js_log_buf) {
|
|
|
|
free(_js_log_buf);
|
|
|
|
_js_log_buf = NULL;
|
|
|
|
}
|
2012-12-03 22:17:53 +08:00
|
|
|
|
|
|
|
js_type_class_t* current, *tmp;
|
|
|
|
HASH_ITER(hh, _js_global_type_ht, current, tmp)
|
|
|
|
{
|
|
|
|
HASH_DEL(_js_global_type_ht, current);
|
|
|
|
free(current->jsclass);
|
|
|
|
free(current);
|
|
|
|
}
|
|
|
|
HASH_CLEAR(hh, _js_global_type_ht);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptingCore::reportError(JSContext *cx, const char *message, JSErrorReport *report)
|
|
|
|
{
|
2012-10-19 11:15:23 +08:00
|
|
|
js_log("%s:%u:%s\n",
|
|
|
|
report->filename ? report->filename : "<no filename=\"filename\">",
|
|
|
|
(unsigned int) report->lineno,
|
|
|
|
message);
|
2012-08-28 10:22:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
JSBool ScriptingCore::log(JSContext* cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
2012-10-19 11:15:23 +08:00
|
|
|
if (argc > 0) {
|
|
|
|
JSString *string = NULL;
|
|
|
|
JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &string);
|
|
|
|
if (string) {
|
2013-02-22 00:34:51 +08:00
|
|
|
JSStringWrapper wrapper(string);
|
2013-07-16 02:46:50 +08:00
|
|
|
js_log("%s", (char *)wrapper);
|
2012-10-19 11:15:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2012-08-28 12:04:51 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void ScriptingCore::removeScriptObjectByObject(Object* pObj)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-08-28 12:04:51 +08:00
|
|
|
js_proxy_t* nproxy;
|
|
|
|
js_proxy_t* jsproxy;
|
2012-09-11 14:02:33 +08:00
|
|
|
void *ptr = (void*)pObj;
|
2013-06-04 12:53:42 +08:00
|
|
|
nproxy = jsb_get_native_proxy(ptr);
|
2012-08-28 12:04:51 +08:00
|
|
|
if (nproxy) {
|
|
|
|
JSContext *cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-04 12:53:42 +08:00
|
|
|
jsproxy = jsb_get_js_proxy(nproxy->obj);
|
2012-09-19 09:44:21 +08:00
|
|
|
JS_RemoveObjectRoot(cx, &jsproxy->obj);
|
2013-06-04 12:53:42 +08:00
|
|
|
jsb_remove_proxy(nproxy, jsproxy);
|
2012-08-28 12:04:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
JSBool ScriptingCore::setReservedSpot(uint32_t i, JSObject *obj, jsval value) {
|
2012-10-19 11:15:23 +08:00
|
|
|
JS_SetReservedSlot(obj, i, value);
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool ScriptingCore::executeScript(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
2012-10-19 11:15:23 +08:00
|
|
|
if (argc >= 1) {
|
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
|
|
|
JSString* str = JS_ValueToString(cx, argv[0]);
|
2013-02-22 00:34:51 +08:00
|
|
|
JSStringWrapper path(str);
|
2012-10-19 11:15:23 +08:00
|
|
|
JSBool res = false;
|
|
|
|
if (argc == 2 && argv[1].isString()) {
|
|
|
|
JSString* globalName = JSVAL_TO_STRING(argv[1]);
|
2013-02-22 00:34:51 +08:00
|
|
|
JSStringWrapper name(globalName);
|
2012-10-19 11:15:23 +08:00
|
|
|
js::RootedObject* rootedGlobal = globals[name];
|
|
|
|
if (rootedGlobal) {
|
|
|
|
res = ScriptingCore::getInstance()->runScript(path, rootedGlobal->get());
|
|
|
|
} else {
|
2012-11-16 03:14:37 +08:00
|
|
|
JS_ReportError(cx, "Invalid global object: %s", (char*)name);
|
2012-10-19 11:15:23 +08:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JSObject* glob = JS_GetGlobalForScopeChain(cx);
|
|
|
|
res = ScriptingCore::getInstance()->runScript(path, glob);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool ScriptingCore::forceGC(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
2012-10-19 11:15:23 +08:00
|
|
|
JSRuntime *rt = JS_GetRuntime(cx);
|
|
|
|
JS_GC(rt);
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-05-22 17:31:07 +08:00
|
|
|
//static void dumpNamedRoot(const char *name, void *addr, JSGCRootType type, void *data)
|
|
|
|
//{
|
|
|
|
// CCLOG("Root: '%s' at %p", name, addr);
|
|
|
|
//}
|
2012-08-28 10:22:36 +08:00
|
|
|
|
|
|
|
JSBool ScriptingCore::dumpRoot(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
// JS_DumpNamedRoots is only available on DEBUG versions of SpiderMonkey.
|
|
|
|
// Mac and Simulator versions were compiled with DEBUG.
|
|
|
|
#if DEBUG
|
2012-10-19 08:44:41 +08:00
|
|
|
// JSContext *_cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
// JSRuntime *rt = JS_GetRuntime(_cx);
|
|
|
|
// JS_DumpNamedRoots(rt, dumpNamedRoot, NULL);
|
2013-04-25 20:56:27 +08:00
|
|
|
// JS_DumpHeap(rt, stdout, NULL, JSTRACE_OBJECT, NULL, 2, NULL);
|
2012-08-28 10:22:36 +08:00
|
|
|
#endif
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool ScriptingCore::addRootJS(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
if (argc == 1) {
|
|
|
|
JSObject *o = NULL;
|
|
|
|
if (JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "o", &o) == JS_TRUE) {
|
|
|
|
if (JS_AddNamedObjectRoot(cx, &o, "from-js") == JS_FALSE) {
|
|
|
|
LOGD("something went wrong when setting an object to the root");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool ScriptingCore::removeRootJS(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
if (argc == 1) {
|
|
|
|
JSObject *o = NULL;
|
|
|
|
if (JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "o", &o) == JS_TRUE) {
|
|
|
|
JS_RemoveObjectRoot(cx, &o);
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:14:39 +08:00
|
|
|
void ScriptingCore::pauseSchedulesAndActions(js_proxy_t* p)
|
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
Array * arr = JSScheduleWrapper::getTargetForJSObject(p->obj);
|
2012-10-10 05:59:16 +08:00
|
|
|
if(! arr) return;
|
2013-03-16 21:14:39 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
Node* node = (Node*)p->ptr;
|
2012-10-10 05:59:16 +08:00
|
|
|
for(unsigned int i = 0; i < arr->count(); ++i) {
|
|
|
|
if(arr->objectAtIndex(i)) {
|
|
|
|
node->getScheduler()->pauseTarget(arr->objectAtIndex(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-16 21:14:39 +08:00
|
|
|
void ScriptingCore::resumeSchedulesAndActions(js_proxy_t* p)
|
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
Array * arr = JSScheduleWrapper::getTargetForJSObject(p->obj);
|
2012-10-10 05:59:16 +08:00
|
|
|
if(!arr) return;
|
2013-03-16 21:14:39 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
Node* node = (Node*)p->ptr;
|
2012-10-10 05:59:16 +08:00
|
|
|
for(unsigned int i = 0; i < arr->count(); ++i) {
|
|
|
|
if(!arr->objectAtIndex(i)) continue;
|
|
|
|
node->getScheduler()->resumeTarget(arr->objectAtIndex(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:14:39 +08:00
|
|
|
void ScriptingCore::cleanupSchedulesAndActions(js_proxy_t* p)
|
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
Array * arr = JSCallFuncWrapper::getTargetForNativeNode((Node*)p->ptr);
|
2012-10-29 15:10:13 +08:00
|
|
|
if(arr) {
|
|
|
|
arr->removeAllObjects();
|
|
|
|
}
|
2013-03-16 21:14:39 +08:00
|
|
|
|
|
|
|
arr = JSScheduleWrapper::getTargetForJSObject(p->obj);
|
2012-10-29 15:10:13 +08:00
|
|
|
if(arr) {
|
2013-07-12 06:24:23 +08:00
|
|
|
Scheduler* pScheduler = Director::getInstance()->getScheduler();
|
2013-06-20 14:33:59 +08:00
|
|
|
Object* pObj = NULL;
|
issue #1581: JSBinding bug fixes.
Some fixes of JSBinding codes:
[1] Check whether the proxy was already added in JS_NEW_PROXY
[2] In struct schedFunc_proxy_t, JSScheduleWrapper* --> CCArray*
Reason: One js function may correspond to many targets. To debug this, you could refer to JSScheduleWrapper::dump function. It will prove that i'm right. :)
[3] In ScriptingCore::cleanupSchedulesAndActions function, we must invoke unschedule for all targets and remove the proxy both in _schedFunc_target_ht and _schedTarget_native_ht, otherwise the hash tables will grow bigger and bigger, so I added a new function JSScheduleWrapper::removeAllTargetsForNatiaveNode to make this things easier.
[4] To easily find out the bugs of binding codes, I add JS_SetGCZeal in ScriptingCore::createGlobalContext, it only works in DEBUG mode.
[5] In js_cocos2dx_CCNode_getChildren, we should add the generated array to root to avoid gc happen when invoking JS_SetElement.
[6] The JSCallFuncWrapper isn't needed since an action will be run by a cc.Node and it will be released at the CCNode::cleanup.
[7] Some improvements of JSScheduleWrapper class.
[8] Added a new function JSScheduleWrapper::setTarget, it's for js_CCNode_unschedule to find out which target need to be unscheduled.
[9] Commented JS_SetReservedSlot in js_CCNode_scheduleOnce and js_CCNode_schedule.
Reason:
For js_CCNode_scheduleOnce: Don't add the callback function to the reserved slot of this js object.Since the class of js object may be inherited from cocos class(e.g. cc.Sprite). The subclass will not contain reserved slots. It will crash if invoking this.
For js_CCNode_schedule: Don't add js callback function to the reserved slot of scheduler js object. Since the scheduler is an object always rooted.
So the callback function might not be released when gc comes.I looked inside the implementation of cc.Node.schedule, and it doesn't use JS_SetReservedSlot there.
2012-11-28 22:04:55 +08:00
|
|
|
CCARRAY_FOREACH(arr, pObj)
|
|
|
|
{
|
|
|
|
pScheduler->unscheduleAllForTarget(pObj);
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:14:39 +08:00
|
|
|
JSScheduleWrapper::removeAllTargetsForJSObject(p->obj);
|
2012-10-23 05:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-10 05:59:16 +08:00
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int ScriptingCore::handleNodeEvent(void* data)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-22 17:12:53 +08:00
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Node* node = static_cast<Node*>(basicScriptData->nativeObject);
|
|
|
|
int action = *((int*)(basicScriptData->value));
|
|
|
|
|
|
|
|
js_proxy_t * p = jsb_get_native_proxy(node);
|
2012-08-28 10:22:36 +08:00
|
|
|
if (!p) return 0;
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval retval;
|
|
|
|
jsval dataVal = INT_TO_JSVAL(1);
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
if(action == kNodeOnEnter)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-03-14 18:14:16 +08:00
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onEnter", 1, &dataVal, &retval);
|
2013-03-16 21:14:39 +08:00
|
|
|
resumeSchedulesAndActions(p);
|
2012-10-20 00:54:21 +08:00
|
|
|
}
|
2013-07-22 17:12:53 +08:00
|
|
|
else if(action == kNodeOnExit)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-03-14 18:14:16 +08:00
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onExit", 1, &dataVal, &retval);
|
2013-03-16 21:14:39 +08:00
|
|
|
pauseSchedulesAndActions(p);
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
2013-07-22 17:12:53 +08:00
|
|
|
else if(action == kNodeOnEnterTransitionDidFinish)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-03-14 18:14:16 +08:00
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onEnterTransitionDidFinish", 1, &dataVal, &retval);
|
2012-08-29 02:25:48 +08:00
|
|
|
}
|
2013-07-22 17:12:53 +08:00
|
|
|
else if(action == kNodeOnExitTransitionDidStart)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-03-14 18:14:16 +08:00
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onExitTransitionDidStart", 1, &dataVal, &retval);
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
2013-07-22 17:12:53 +08:00
|
|
|
else if(action == kNodeOnCleanup) {
|
2013-03-16 21:14:39 +08:00
|
|
|
cleanupSchedulesAndActions(p);
|
2012-10-23 05:51:35 +08:00
|
|
|
}
|
2012-08-28 10:22:36 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int ScriptingCore::handleMenuClickedEvent(void* data)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-22 17:12:53 +08:00
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (NULL == basicScriptData->nativeObject)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
MenuItem* menuItem = static_cast<MenuItem*>(basicScriptData->nativeObject);
|
|
|
|
|
|
|
|
js_proxy_t * p = jsb_get_native_proxy(menuItem);
|
2012-08-28 10:22:36 +08:00
|
|
|
if (!p) return 0;
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval retval;
|
2012-09-11 14:02:33 +08:00
|
|
|
jsval dataVal;
|
2013-07-22 17:12:53 +08:00
|
|
|
js_proxy_t *proxy = jsb_get_native_proxy(menuItem);
|
2012-09-11 14:02:33 +08:00
|
|
|
dataVal = (proxy ? OBJECT_TO_JSVAL(proxy->obj) : JSVAL_NULL);
|
2012-08-28 10:22:36 +08:00
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
executeJSFunctionFromReservedSpot(this->cx_, p->obj, dataVal, retval);
|
2012-08-28 10:22:36 +08:00
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
return 1;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int ScriptingCore::handleTouchesEvent(void* data)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-22 17:12:53 +08:00
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
TouchesScriptData* touchesScriptData = static_cast<TouchesScriptData*>(data);
|
|
|
|
if (NULL == touchesScriptData->nativeObject || NULL == touchesScriptData->touches)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Layer* pLayer = static_cast<Layer*>(touchesScriptData->nativeObject);
|
|
|
|
int eventType = touchesScriptData->actionType;
|
|
|
|
Set *pTouches = touchesScriptData->touches;
|
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
std::string funcName = "";
|
|
|
|
getTouchesFuncName(eventType, funcName);
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
JSObject *jsretArr = JS_NewArrayObject(this->cx_, 0, NULL);
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
JS_AddNamedObjectRoot(this->cx_, &jsretArr, "touchArray");
|
2012-08-28 10:22:36 +08:00
|
|
|
int count = 0;
|
2013-06-20 14:33:59 +08:00
|
|
|
for(SetIterator it = pTouches->begin(); it != pTouches->end(); ++it, ++count) {
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval jsret;
|
2013-06-20 14:33:59 +08:00
|
|
|
getJSTouchObject(this->cx_, (Touch *) *it, jsret);
|
2012-10-19 08:44:41 +08:00
|
|
|
if(!JS_SetElement(this->cx_, jsretArr, count, &jsret)) {
|
2012-08-28 10:22:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 14:02:33 +08:00
|
|
|
|
|
|
|
executeFunctionWithObjectData(pLayer, funcName.c_str(), jsretArr);
|
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
JS_RemoveObjectRoot(this->cx_, &jsretArr);
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
for(SetIterator it = pTouches->begin(); it != pTouches->end(); ++it, ++count) {
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval jsret;
|
2013-06-20 14:33:59 +08:00
|
|
|
removeJSTouchObject(this->cx_, (Touch *) *it, jsret);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
2012-09-11 14:02:33 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int ScriptingCore::handleTouchEvent(void* data)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-22 17:12:53 +08:00
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
TouchScriptData* touchScriptData = static_cast<TouchScriptData*>(data);
|
|
|
|
if (NULL == touchScriptData->nativeObject || NULL == touchScriptData->touch)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Layer* pLayer = static_cast<Layer*>(touchScriptData->nativeObject);
|
|
|
|
int eventType = touchScriptData->actionType;
|
|
|
|
Touch *pTouch = touchScriptData->touch;
|
|
|
|
|
|
|
|
|
2012-10-23 02:02:54 +08:00
|
|
|
std::string funcName = "";
|
|
|
|
getTouchFuncName(eventType, funcName);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2012-10-23 02:02:54 +08:00
|
|
|
jsval jsret;
|
2012-10-29 15:10:13 +08:00
|
|
|
getJSTouchObject(this->getGlobalContext(), pTouch, jsret);
|
2012-10-23 02:02:54 +08:00
|
|
|
JSObject *jsObj = JSVAL_TO_OBJECT(jsret);
|
2012-11-07 03:01:46 +08:00
|
|
|
bool retval = executeFunctionWithObjectData(pLayer, funcName.c_str(), jsObj);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2012-10-29 15:10:13 +08:00
|
|
|
removeJSTouchObject(this->getGlobalContext(), pTouch, jsret);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2012-11-07 03:01:46 +08:00
|
|
|
return retval;
|
2012-11-06 14:18:13 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
bool ScriptingCore::executeFunctionWithObjectData(Node *self, const char *name, JSObject *obj) {
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(self);
|
2012-11-22 10:24:26 +08:00
|
|
|
if (!p) return false;
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
jsval retval;
|
|
|
|
jsval dataVal = OBJECT_TO_JSVAL(obj);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), name, 1, &dataVal, &retval);
|
2012-11-22 10:24:26 +08:00
|
|
|
if(JSVAL_IS_NULL(retval)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(JSVAL_IS_BOOLEAN(retval)) {
|
|
|
|
return JSVAL_TO_BOOLEAN(retval);
|
|
|
|
}
|
|
|
|
return false;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
JSBool ScriptingCore::executeFunctionWithOwner(jsval owner, const char *name, uint32_t argc /* = 0 */, jsval *vp /* = NULL */, jsval* retVal /* = NULL */)
|
|
|
|
{
|
|
|
|
JSBool bRet = JS_FALSE;
|
|
|
|
JSBool hasAction;
|
|
|
|
jsval temp_retval;
|
|
|
|
JSContext* cx = this->cx_;
|
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(owner);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (JS_HasProperty(cx, obj, name, &hasAction) && hasAction) {
|
|
|
|
if(!JS_GetProperty(cx, obj, name, &temp_retval)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(temp_retval == JSVAL_VOID) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
if (retVal) {
|
|
|
|
bRet = JS_CallFunctionName(cx, obj, name, argc, vp, retVal);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
jsval jsret;
|
|
|
|
bRet = JS_CallFunctionName(cx, obj, name, argc, vp, &jsret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}while(0);
|
|
|
|
return bRet;
|
2012-09-19 09:21:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int ScriptingCore::handleAccelerometerEvent(void* data)
|
|
|
|
{
|
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Acceleration* accelerationValue = static_cast<Acceleration*>(basicScriptData->value);
|
|
|
|
Layer* layer = static_cast<Layer*>(basicScriptData->nativeObject);
|
|
|
|
|
|
|
|
jsval value = ccacceleration_to_jsval(this->getGlobalContext(), *accelerationValue);
|
2012-11-14 06:58:11 +08:00
|
|
|
JS_AddValueRoot(this->getGlobalContext(), &value);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
executeFunctionWithObjectData(layer, "onAccelerometer", JSVAL_TO_OBJECT(value));
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2012-11-14 06:58:11 +08:00
|
|
|
JS_RemoveValueRoot(this->getGlobalContext(), &value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int ScriptingCore::handleKeypadEvent(void* data)
|
2012-12-04 10:52:49 +08:00
|
|
|
{
|
2013-07-22 17:12:53 +08:00
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
KeypadScriptData* keypadScriptData = static_cast<KeypadScriptData*>(data);
|
|
|
|
if (NULL == keypadScriptData->nativeObject)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int action = keypadScriptData->actionType;
|
|
|
|
|
|
|
|
js_proxy_t * p = jsb_get_native_proxy(keypadScriptData->nativeObject);
|
2013-04-17 10:40:46 +08:00
|
|
|
|
|
|
|
if(p){
|
2013-07-22 17:12:53 +08:00
|
|
|
switch(action){
|
2013-04-17 10:40:46 +08:00
|
|
|
case kTypeBackClicked:
|
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "backClicked");
|
|
|
|
break;
|
|
|
|
case kTypeMenuClicked:
|
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "menuClicked");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-12-04 10:52:49 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 06:58:11 +08:00
|
|
|
|
2012-10-20 00:54:21 +08:00
|
|
|
int ScriptingCore::executeCustomTouchesEvent(int eventType,
|
2013-06-20 14:33:59 +08:00
|
|
|
Set *pTouches, JSObject *obj)
|
2012-08-28 10:22:36 +08:00
|
|
|
{
|
|
|
|
jsval retval;
|
|
|
|
std::string funcName;
|
|
|
|
getTouchesFuncName(eventType, funcName);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
JSObject *jsretArr = JS_NewArrayObject(this->cx_, 0, NULL);
|
|
|
|
JS_AddNamedObjectRoot(this->cx_, &jsretArr, "touchArray");
|
2012-08-28 10:22:36 +08:00
|
|
|
int count = 0;
|
2013-06-20 14:33:59 +08:00
|
|
|
for(SetIterator it = pTouches->begin(); it != pTouches->end(); ++it, ++count) {
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval jsret;
|
2013-06-20 14:33:59 +08:00
|
|
|
getJSTouchObject(this->cx_, (Touch *) *it, jsret);
|
2012-10-19 08:44:41 +08:00
|
|
|
if(!JS_SetElement(this->cx_, jsretArr, count, &jsret)) {
|
2012-08-28 10:22:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval jsretArrVal = OBJECT_TO_JSVAL(jsretArr);
|
2013-03-14 18:14:16 +08:00
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), funcName.c_str(), 1, &jsretArrVal, &retval);
|
2012-10-19 08:44:41 +08:00
|
|
|
JS_RemoveObjectRoot(this->cx_, &jsretArr);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
for(SetIterator it = pTouches->begin(); it != pTouches->end(); ++it, ++count) {
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval jsret;
|
2013-06-20 14:33:59 +08:00
|
|
|
removeJSTouchObject(this->cx_, (Touch *) *it, jsret);
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-20 00:54:21 +08:00
|
|
|
int ScriptingCore::executeCustomTouchEvent(int eventType,
|
2013-06-20 14:33:59 +08:00
|
|
|
Touch *pTouch, JSObject *obj) {
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval retval;
|
|
|
|
std::string funcName;
|
|
|
|
getTouchFuncName(eventType, funcName);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval jsTouch;
|
2012-10-19 08:44:41 +08:00
|
|
|
getJSTouchObject(this->cx_, pTouch, jsTouch);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), funcName.c_str(), 1, &jsTouch, &retval);
|
fixed #1617: Some improvements for JS Bindings.
1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff
[Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android.
Then cast it to unsigned int, the value will be 0. The schedule will not be able to work.
I don't know why this occurs only on android.
[Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++.
2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject.
3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks.
4) Improvments for JSTouchDelegate. Added four functions as follows:
// Set the touch delegate to map by using the key (pJSObj).
static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate);
// Get the touch delegate by the key (pJSObj).
static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj);
// Remove the delegate by the key (pJSObj).
static void removeDelegateForJSObject(JSObject* pJSObj);
void unregisterTouchDelegate();
And exported cc.unregisterTouchDelegate(); to js.
Fix a memory leak for JSTouchDelegate by making it as an autorelease object.
5) Don't add js callback function to the reserved slot of object.
[Reason]: The target object may execute more than one schedule.
Therefore, previous js callback function will be replaced
by the current one. For example:
this.scheduleOnce(function() { temporary function 1 }, 0.5);
this.scheduleOnce(function() { temporary function 2 }, 0.5);
In this case, the temporary function 1 will be removed from reserved slot 0.
And temporary function 2 will be set to reserved slot 0 of this object.
If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked,
crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce.
[Solution] Because one schedule corresponds to one JSScheduleWrapper, we root
the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it
at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
// Remove touch object from global hash table and unroot it.
|
fixed #1617: Some improvements for JS Bindings.
1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff
[Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android.
Then cast it to unsigned int, the value will be 0. The schedule will not be able to work.
I don't know why this occurs only on android.
[Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++.
2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject.
3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks.
4) Improvments for JSTouchDelegate. Added four functions as follows:
// Set the touch delegate to map by using the key (pJSObj).
static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate);
// Get the touch delegate by the key (pJSObj).
static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj);
// Remove the delegate by the key (pJSObj).
static void removeDelegateForJSObject(JSObject* pJSObj);
void unregisterTouchDelegate();
And exported cc.unregisterTouchDelegate(); to js.
Fix a memory leak for JSTouchDelegate by making it as an autorelease object.
5) Don't add js callback function to the reserved slot of object.
[Reason]: The target object may execute more than one schedule.
Therefore, previous js callback function will be replaced
by the current one. For example:
this.scheduleOnce(function() { temporary function 1 }, 0.5);
this.scheduleOnce(function() { temporary function 2 }, 0.5);
In this case, the temporary function 1 will be removed from reserved slot 0.
And temporary function 2 will be set to reserved slot 0 of this object.
If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked,
crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce.
[Solution] Because one schedule corresponds to one JSScheduleWrapper, we root
the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it
at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
|
|
|
removeJSTouchObject(this->cx_, pTouch, jsTouch);
|
2012-08-28 10:22:36 +08:00
|
|
|
return 1;
|
2012-10-20 00:54:21 +08:00
|
|
|
|
|
|
|
}
|
2012-08-28 10:22:36 +08:00
|
|
|
|
|
|
|
|
2012-10-20 00:54:21 +08:00
|
|
|
int ScriptingCore::executeCustomTouchEvent(int eventType,
|
2013-06-20 14:33:59 +08:00
|
|
|
Touch *pTouch, JSObject *obj,
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval &retval) {
|
|
|
|
|
|
|
|
std::string funcName;
|
|
|
|
getTouchFuncName(eventType, funcName);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval jsTouch;
|
2012-10-19 08:44:41 +08:00
|
|
|
getJSTouchObject(this->cx_, pTouch, jsTouch);
|
2012-08-28 10:22:36 +08:00
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), funcName.c_str(), 1, &jsTouch, &retval);
|
fixed #1617: Some improvements for JS Bindings.
1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff
[Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android.
Then cast it to unsigned int, the value will be 0. The schedule will not be able to work.
I don't know why this occurs only on android.
[Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++.
2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject.
3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks.
4) Improvments for JSTouchDelegate. Added four functions as follows:
// Set the touch delegate to map by using the key (pJSObj).
static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate);
// Get the touch delegate by the key (pJSObj).
static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj);
// Remove the delegate by the key (pJSObj).
static void removeDelegateForJSObject(JSObject* pJSObj);
void unregisterTouchDelegate();
And exported cc.unregisterTouchDelegate(); to js.
Fix a memory leak for JSTouchDelegate by making it as an autorelease object.
5) Don't add js callback function to the reserved slot of object.
[Reason]: The target object may execute more than one schedule.
Therefore, previous js callback function will be replaced
by the current one. For example:
this.scheduleOnce(function() { temporary function 1 }, 0.5);
this.scheduleOnce(function() { temporary function 2 }, 0.5);
In this case, the temporary function 1 will be removed from reserved slot 0.
And temporary function 2 will be set to reserved slot 0 of this object.
If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked,
crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce.
[Solution] Because one schedule corresponds to one JSScheduleWrapper, we root
the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it
at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
// Remove touch object from global hash table and unroot it.
|
fixed #1617: Some improvements for JS Bindings.
1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff
[Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android.
Then cast it to unsigned int, the value will be 0. The schedule will not be able to work.
I don't know why this occurs only on android.
[Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++.
2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject.
3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks.
4) Improvments for JSTouchDelegate. Added four functions as follows:
// Set the touch delegate to map by using the key (pJSObj).
static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate);
// Get the touch delegate by the key (pJSObj).
static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj);
// Remove the delegate by the key (pJSObj).
static void removeDelegateForJSObject(JSObject* pJSObj);
void unregisterTouchDelegate();
And exported cc.unregisterTouchDelegate(); to js.
Fix a memory leak for JSTouchDelegate by making it as an autorelease object.
5) Don't add js callback function to the reserved slot of object.
[Reason]: The target object may execute more than one schedule.
Therefore, previous js callback function will be replaced
by the current one. For example:
this.scheduleOnce(function() { temporary function 1 }, 0.5);
this.scheduleOnce(function() { temporary function 2 }, 0.5);
In this case, the temporary function 1 will be removed from reserved slot 0.
And temporary function 2 will be set to reserved slot 0 of this object.
If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked,
crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce.
[Solution] Because one schedule corresponds to one JSScheduleWrapper, we root
the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it
at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
|
|
|
removeJSTouchObject(this->cx_, pTouch, jsTouch);
|
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
return 1;
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int ScriptingCore::sendEvent(ScriptEvent* evt)
|
|
|
|
{
|
|
|
|
if (NULL == evt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (evt->type)
|
|
|
|
{
|
|
|
|
case kNodeEvent:
|
|
|
|
{
|
|
|
|
return handleNodeEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kMenuClickedEvent:
|
|
|
|
{
|
|
|
|
return handleMenuClickedEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kTouchEvent:
|
|
|
|
{
|
|
|
|
return handleTouchEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kTouchesEvent:
|
|
|
|
{
|
|
|
|
return handleTouchesEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kKeypadEvent:
|
|
|
|
{
|
|
|
|
return handleKeypadEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kAccelerometerEvent:
|
|
|
|
{
|
|
|
|
return handleAccelerometerEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-19 08:44:41 +08:00
|
|
|
#pragma mark - Conversion Routines
|
2013-01-11 13:54:57 +08:00
|
|
|
JSBool jsval_to_int32( JSContext *cx, jsval vp, int32_t *outval )
|
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
JSBool ok = JS_TRUE;
|
|
|
|
double dp;
|
|
|
|
ok &= JS_ValueToNumber(cx, vp, &dp);
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
ok &= !isnan(dp);
|
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
|
|
|
|
*outval = (int32_t)dp;
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
return ok;
|
2013-01-11 13:54:57 +08:00
|
|
|
}
|
2012-08-28 10:22:36 +08:00
|
|
|
|
2013-01-11 13:54:57 +08:00
|
|
|
JSBool jsval_to_uint32( JSContext *cx, jsval vp, uint32_t *outval )
|
|
|
|
{
|
2013-01-11 18:57:33 +08:00
|
|
|
JSBool ok = JS_TRUE;
|
2013-02-22 00:34:51 +08:00
|
|
|
double dp;
|
|
|
|
ok &= JS_ValueToNumber(cx, vp, &dp);
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
ok &= !isnan(dp);
|
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
*outval = (uint32_t)dp;
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
return ok;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 13:54:57 +08:00
|
|
|
JSBool jsval_to_uint16( JSContext *cx, jsval vp, uint16_t *outval )
|
|
|
|
{
|
2013-01-11 18:57:33 +08:00
|
|
|
JSBool ok = JS_TRUE;
|
2013-02-22 00:34:51 +08:00
|
|
|
double dp;
|
|
|
|
ok &= JS_ValueToNumber(cx, vp, &dp);
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
ok &= !isnan(dp);
|
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
*outval = (uint16_t)dp;
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
return ok;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-02-27 15:16:49 +08:00
|
|
|
JSBool jsval_to_long_long(JSContext *cx, jsval vp, long long* r) {
|
|
|
|
JSObject *tmp_arg;
|
|
|
|
JSBool ok = JS_ValueToObject( cx, vp, &tmp_arg );
|
|
|
|
JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error converting value to object");
|
|
|
|
JSB_PRECONDITION2( tmp_arg && JS_IsTypedArrayObject( tmp_arg ), cx, JS_FALSE, "Not a TypedArray object");
|
|
|
|
JSB_PRECONDITION2( JS_GetTypedArrayByteLength( tmp_arg ) == sizeof(long long), cx, JS_FALSE, "Invalid Typed Array length");
|
|
|
|
|
|
|
|
uint32_t* arg_array = (uint32_t*)JS_GetArrayBufferViewData( tmp_arg );
|
|
|
|
long long ret = arg_array[0];
|
|
|
|
ret = ret << 32;
|
|
|
|
ret |= arg_array[1];
|
|
|
|
|
|
|
|
*r = ret;
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 13:54:57 +08:00
|
|
|
JSBool jsval_to_std_string(JSContext *cx, jsval v, std::string* ret) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSString *tmp = JS_ValueToString(cx, v);
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(tmp, cx, JS_FALSE, "Error processing arguments");
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
JSStringWrapper str(tmp);
|
2013-01-11 13:54:57 +08:00
|
|
|
*ret = str.get();
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsval_to_ccpoint(JSContext *cx, jsval v, Point* ret) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp;
|
|
|
|
jsval jsx, jsy;
|
|
|
|
double x, y;
|
|
|
|
JSBool ok = JS_ValueToObject(cx, v, &tmp) &&
|
|
|
|
JS_GetProperty(cx, tmp, "x", &jsx) &&
|
|
|
|
JS_GetProperty(cx, tmp, "y", &jsy) &&
|
|
|
|
JS_ValueToNumber(cx, jsx, &x) &&
|
|
|
|
JS_ValueToNumber(cx, jsy, &y);
|
2013-01-11 13:54:57 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
2013-01-11 13:54:57 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
ret->x = (float)x;
|
|
|
|
ret->y = (float)y;
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsval_to_ccacceleration(JSContext* cx,jsval v, Acceleration* ret) {
|
2012-11-14 06:58:11 +08:00
|
|
|
JSObject *tmp;
|
|
|
|
jsval jsx, jsy, jsz, jstimestamp;
|
|
|
|
double x, y, timestamp, z;
|
|
|
|
JSBool ok = JS_ValueToObject(cx, v, &tmp) &&
|
|
|
|
JS_GetProperty(cx, tmp, "x", &jsx) &&
|
|
|
|
JS_GetProperty(cx, tmp, "y", &jsy) &&
|
|
|
|
JS_GetProperty(cx, tmp, "z", &jsz) &&
|
|
|
|
JS_GetProperty(cx, tmp, "timestamp", &jstimestamp) &&
|
|
|
|
JS_ValueToNumber(cx, jsx, &x) &&
|
|
|
|
JS_ValueToNumber(cx, jsy, &y) &&
|
|
|
|
JS_ValueToNumber(cx, jsz, &z) &&
|
|
|
|
JS_ValueToNumber(cx, jstimestamp, ×tamp);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
ret->x = x;
|
|
|
|
ret->y = y;
|
|
|
|
ret->z = z;
|
|
|
|
ret->timestamp = timestamp;
|
|
|
|
return JS_TRUE;
|
2012-11-14 06:58:11 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc, Array** ret)
|
2012-12-04 10:52:49 +08:00
|
|
|
{
|
2013-01-11 18:57:33 +08:00
|
|
|
JSBool ok = JS_TRUE;
|
2013-06-20 14:33:59 +08:00
|
|
|
Array* pArray = Array::create();
|
2012-12-04 10:52:49 +08:00
|
|
|
for( int i=0; i < argc; i++ )
|
|
|
|
{
|
|
|
|
double num = 0.0;
|
|
|
|
// optimization: JS_ValueToNumber is expensive. And can convert an string like "12" to a number
|
|
|
|
if( JSVAL_IS_NUMBER(*vp)) {
|
2013-01-11 18:57:33 +08:00
|
|
|
ok &= JS_ValueToNumber(cx, *vp, &num );
|
2012-12-04 10:52:49 +08:00
|
|
|
if (!ok) {
|
|
|
|
break;
|
|
|
|
}
|
2013-06-20 14:33:59 +08:00
|
|
|
pArray->addObject(Integer::create((int)num));
|
2012-12-04 10:52:49 +08:00
|
|
|
}
|
2012-11-30 21:12:14 +08:00
|
|
|
else if (JSVAL_IS_STRING(*vp))
|
|
|
|
{
|
|
|
|
JSStringWrapper str(JSVAL_TO_STRING(*vp), cx);
|
2013-06-20 14:33:59 +08:00
|
|
|
pArray->addObject(String::create(str));
|
2012-12-04 10:52:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
js_proxy_t* p;
|
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(*vp);
|
2013-06-04 12:53:42 +08:00
|
|
|
p = jsb_get_js_proxy(obj);
|
2012-12-04 10:52:49 +08:00
|
|
|
if (p) {
|
2013-06-20 14:33:59 +08:00
|
|
|
pArray->addObject((Object*)p->ptr);
|
2012-12-04 10:52:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// next
|
|
|
|
vp++;
|
|
|
|
}
|
2013-01-11 13:54:57 +08:00
|
|
|
*ret = pArray;
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
2013-01-11 13:54:57 +08:00
|
|
|
return ok;
|
2012-11-30 21:12:14 +08:00
|
|
|
}
|
2012-11-14 06:58:11 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsval_to_ccrect(JSContext *cx, jsval v, Rect* ret) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp;
|
|
|
|
jsval jsx, jsy, jswidth, jsheight;
|
|
|
|
double x, y, width, height;
|
|
|
|
JSBool ok = JS_ValueToObject(cx, v, &tmp) &&
|
|
|
|
JS_GetProperty(cx, tmp, "x", &jsx) &&
|
|
|
|
JS_GetProperty(cx, tmp, "y", &jsy) &&
|
|
|
|
JS_GetProperty(cx, tmp, "width", &jswidth) &&
|
|
|
|
JS_GetProperty(cx, tmp, "height", &jsheight) &&
|
|
|
|
JS_ValueToNumber(cx, jsx, &x) &&
|
|
|
|
JS_ValueToNumber(cx, jsy, &y) &&
|
|
|
|
JS_ValueToNumber(cx, jswidth, &width) &&
|
|
|
|
JS_ValueToNumber(cx, jsheight, &height);
|
2013-01-11 18:57:33 +08:00
|
|
|
|
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
ret->origin.x = x;
|
|
|
|
ret->origin.y = y;
|
|
|
|
ret->size.width = width;
|
|
|
|
ret->size.height = height;
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsval_to_ccsize(JSContext *cx, jsval v, Size* ret) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp;
|
|
|
|
jsval jsw, jsh;
|
|
|
|
double w, h;
|
|
|
|
JSBool ok = JS_ValueToObject(cx, v, &tmp) &&
|
|
|
|
JS_GetProperty(cx, tmp, "width", &jsw) &&
|
|
|
|
JS_GetProperty(cx, tmp, "height", &jsh) &&
|
|
|
|
JS_ValueToNumber(cx, jsw, &w) &&
|
|
|
|
JS_ValueToNumber(cx, jsh, &h);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
ret->width = w;
|
|
|
|
ret->height = h;
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
JSBool jsval_to_cccolor4b(JSContext *cx, jsval v, Color4B* ret) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp;
|
|
|
|
jsval jsr, jsg, jsb, jsa;
|
|
|
|
double r, g, b, a;
|
|
|
|
JSBool ok = JS_ValueToObject(cx, v, &tmp) &&
|
|
|
|
JS_GetProperty(cx, tmp, "r", &jsr) &&
|
|
|
|
JS_GetProperty(cx, tmp, "g", &jsg) &&
|
|
|
|
JS_GetProperty(cx, tmp, "b", &jsb) &&
|
|
|
|
JS_GetProperty(cx, tmp, "a", &jsa) &&
|
|
|
|
JS_ValueToNumber(cx, jsr, &r) &&
|
|
|
|
JS_ValueToNumber(cx, jsg, &g) &&
|
|
|
|
JS_ValueToNumber(cx, jsb, &b) &&
|
|
|
|
JS_ValueToNumber(cx, jsa, &a);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
|
|
|
|
ret->r = r;
|
|
|
|
ret->g = g;
|
|
|
|
ret->b = b;
|
|
|
|
ret->a = a;
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
JSBool jsval_to_cccolor4f(JSContext *cx, jsval v, Color4F* ret) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp;
|
|
|
|
jsval jsr, jsg, jsb, jsa;
|
|
|
|
double r, g, b, a;
|
|
|
|
JSBool ok = JS_ValueToObject(cx, v, &tmp) &&
|
|
|
|
JS_GetProperty(cx, tmp, "r", &jsr) &&
|
|
|
|
JS_GetProperty(cx, tmp, "g", &jsg) &&
|
|
|
|
JS_GetProperty(cx, tmp, "b", &jsb) &&
|
|
|
|
JS_GetProperty(cx, tmp, "a", &jsa) &&
|
|
|
|
JS_ValueToNumber(cx, jsr, &r) &&
|
|
|
|
JS_ValueToNumber(cx, jsg, &g) &&
|
|
|
|
JS_ValueToNumber(cx, jsb, &b) &&
|
|
|
|
JS_ValueToNumber(cx, jsa, &a);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
ret->r = r;
|
|
|
|
ret->g = g;
|
|
|
|
ret->b = b;
|
|
|
|
ret->a = a;
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
JSBool jsval_to_cccolor3b(JSContext *cx, jsval v, Color3B* ret) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp;
|
|
|
|
jsval jsr, jsg, jsb;
|
|
|
|
double r, g, b;
|
|
|
|
JSBool ok = JS_ValueToObject(cx, v, &tmp) &&
|
|
|
|
JS_GetProperty(cx, tmp, "r", &jsr) &&
|
|
|
|
JS_GetProperty(cx, tmp, "g", &jsg) &&
|
|
|
|
JS_GetProperty(cx, tmp, "b", &jsb) &&
|
|
|
|
JS_ValueToNumber(cx, jsr, &r) &&
|
|
|
|
JS_ValueToNumber(cx, jsg, &g) &&
|
|
|
|
JS_ValueToNumber(cx, jsb, &b);
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 18:57:33 +08:00
|
|
|
ret->r = r;
|
|
|
|
ret->g = g;
|
|
|
|
ret->b = b;
|
2013-05-08 08:22:25 +08:00
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsval_to_ccarray_of_CCPoint(JSContext* cx, jsval v, Point **points, int *numPoints) {
|
2012-10-19 11:15:23 +08:00
|
|
|
// Parsing sequence
|
|
|
|
JSObject *jsobj;
|
|
|
|
JSBool ok = JS_ValueToObject( cx, v, &jsobj );
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error converting value to object");
|
2013-02-22 00:34:51 +08:00
|
|
|
JSB_PRECONDITION2( jsobj && JS_IsArrayObject( cx, jsobj), cx, JS_FALSE, "Object must be an array");
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-10-19 11:15:23 +08:00
|
|
|
uint32_t len;
|
|
|
|
JS_GetArrayLength(cx, jsobj, &len);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
Point *array = (Point*)malloc( sizeof(Point) * len);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-10-19 11:15:23 +08:00
|
|
|
for( uint32_t i=0; i< len;i++ ) {
|
|
|
|
jsval valarg;
|
|
|
|
JS_GetElement(cx, jsobj, i, &valarg);
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2013-01-11 13:54:57 +08:00
|
|
|
ok = jsval_to_ccpoint(cx, valarg, &array[i]);
|
2013-01-11 18:57:33 +08:00
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
2012-10-19 11:15:23 +08:00
|
|
|
}
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-10-19 11:15:23 +08:00
|
|
|
*numPoints = len;
|
|
|
|
*points = array;
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2012-10-19 11:15:23 +08:00
|
|
|
return JS_TRUE;
|
2012-10-12 08:57:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsval_to_ccarray(JSContext* cx, jsval v, Array** ret) {
|
2013-01-11 18:57:33 +08:00
|
|
|
JSObject *jsobj;
|
|
|
|
JSBool ok = JS_ValueToObject( cx, v, &jsobj );
|
|
|
|
JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error converting value to object");
|
2013-02-22 00:34:51 +08:00
|
|
|
JSB_PRECONDITION2( jsobj && JS_IsArrayObject( cx, jsobj), cx, JS_FALSE, "Object must be an array");
|
2013-01-11 18:57:33 +08:00
|
|
|
|
|
|
|
uint32_t len = 0;
|
|
|
|
JS_GetArrayLength(cx, jsobj, &len);
|
2013-06-20 14:33:59 +08:00
|
|
|
Array* arr = Array::createWithCapacity(len);
|
2013-01-11 18:57:33 +08:00
|
|
|
for (uint32_t i=0; i < len; i++) {
|
2013-03-13 18:10:54 +08:00
|
|
|
jsval value;
|
|
|
|
if (JS_GetElement(cx, jsobj, i, &value)) {
|
|
|
|
if (value.isObject())
|
|
|
|
{
|
|
|
|
js_proxy_t *proxy;
|
|
|
|
JSObject *tmp = JSVAL_TO_OBJECT(value);
|
2013-06-04 12:53:42 +08:00
|
|
|
proxy = jsb_get_js_proxy(tmp);
|
2013-06-20 14:33:59 +08:00
|
|
|
cocos2d::Object* cobj = (cocos2d::Object *)(proxy ? proxy->ptr : NULL);
|
2013-03-13 18:10:54 +08:00
|
|
|
// Don't test it.
|
|
|
|
//TEST_NATIVE_OBJECT(cx, cobj)
|
|
|
|
if (cobj) {
|
|
|
|
// It's a native js object.
|
|
|
|
arr->addObject(cobj);
|
|
|
|
}
|
|
|
|
else if (!JS_IsArrayObject(cx, tmp)){
|
|
|
|
// It's a normal js object.
|
2013-06-20 14:33:59 +08:00
|
|
|
Dictionary* dictVal = NULL;
|
2013-03-13 18:10:54 +08:00
|
|
|
JSBool ok = jsval_to_ccdictionary(cx, value, &dictVal);
|
|
|
|
if (ok) {
|
|
|
|
arr->addObject(dictVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// It's a js array object.
|
2013-06-20 14:33:59 +08:00
|
|
|
Array* arrVal = NULL;
|
2013-03-13 18:10:54 +08:00
|
|
|
JSBool ok = jsval_to_ccarray(cx, value, &arrVal);
|
|
|
|
if (ok) {
|
|
|
|
arr->addObject(arrVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_STRING(value)) {
|
|
|
|
JSStringWrapper valueWapper(JSVAL_TO_STRING(value), cx);
|
2013-06-20 14:33:59 +08:00
|
|
|
arr->addObject(String::create(valueWapper.get()));
|
2013-03-13 18:10:54 +08:00
|
|
|
// CCLOG("iterate array: value = %s", valueWapper.get().c_str());
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_NUMBER(value)) {
|
|
|
|
double number = 0.0;
|
|
|
|
JSBool ok = JS_ValueToNumber(cx, value, &number);
|
|
|
|
if (ok) {
|
2013-06-20 14:33:59 +08:00
|
|
|
arr->addObject(Double::create(number));
|
2013-03-13 18:10:54 +08:00
|
|
|
// CCLOG("iterate array: value = %lf", number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_BOOLEAN(value)) {
|
|
|
|
JSBool boolVal = JS_FALSE;
|
|
|
|
JSBool ok = JS_ValueToBoolean(cx, value, &boolVal);
|
|
|
|
if (ok) {
|
2013-06-20 14:33:59 +08:00
|
|
|
arr->addObject(Bool::create(boolVal));
|
2013-03-13 18:10:54 +08:00
|
|
|
// CCLOG("iterate object: value = %d", boolVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "not supported type");
|
2013-03-13 18:10:54 +08:00
|
|
|
}
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-11 18:57:33 +08:00
|
|
|
*ret = arr;
|
|
|
|
return JS_TRUE;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2012-09-19 09:44:21 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
jsval ccarray_to_jsval(JSContext* cx, Array *arr)
|
2013-03-13 18:10:54 +08:00
|
|
|
{
|
2012-11-01 02:08:37 +08:00
|
|
|
JSObject *jsretArr = JS_NewArrayObject(cx, 0, NULL);
|
2013-03-16 21:14:39 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
Object* obj;
|
2013-05-22 11:56:47 +08:00
|
|
|
int i = 0;
|
|
|
|
CCARRAY_FOREACH(arr, obj)
|
|
|
|
{
|
|
|
|
jsval arrElement;
|
|
|
|
|
|
|
|
//First, check whether object is associated with js object.
|
2013-06-20 14:33:59 +08:00
|
|
|
js_proxy_t* jsproxy = js_get_or_create_proxy<cocos2d::Object>(cx, obj);
|
2013-05-22 11:56:47 +08:00
|
|
|
if (jsproxy) {
|
|
|
|
arrElement = OBJECT_TO_JSVAL(jsproxy->obj);
|
|
|
|
}
|
|
|
|
else {
|
2013-06-20 14:33:59 +08:00
|
|
|
String* strVal = NULL;
|
|
|
|
Dictionary* dictVal = NULL;
|
|
|
|
Array* arrVal = NULL;
|
|
|
|
Double* doubleVal = NULL;
|
|
|
|
Bool* boolVal = NULL;
|
|
|
|
Float* floatVal = NULL;
|
|
|
|
Integer* intVal = NULL;
|
2013-05-22 11:56:47 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
if((strVal = dynamic_cast<cocos2d::String *>(obj))) {
|
2013-05-22 11:56:47 +08:00
|
|
|
arrElement = c_string_to_jsval(cx, strVal->getCString());
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((dictVal = dynamic_cast<cocos2d::Dictionary*>(obj))) {
|
2013-05-22 11:56:47 +08:00
|
|
|
arrElement = ccdictionary_to_jsval(cx, dictVal);
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((arrVal = dynamic_cast<cocos2d::Array*>(obj))) {
|
2013-05-22 11:56:47 +08:00
|
|
|
arrElement = ccarray_to_jsval(cx, arrVal);
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((doubleVal = dynamic_cast<Double*>(obj))) {
|
2013-05-22 11:56:47 +08:00
|
|
|
arrElement = DOUBLE_TO_JSVAL(doubleVal->getValue());
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((floatVal = dynamic_cast<Float*>(obj))) {
|
2013-05-22 11:56:47 +08:00
|
|
|
arrElement = DOUBLE_TO_JSVAL(floatVal->getValue());
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((intVal = dynamic_cast<Integer*>(obj))) {
|
2013-05-22 11:56:47 +08:00
|
|
|
arrElement = INT_TO_JSVAL(intVal->getValue());
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((boolVal = dynamic_cast<Bool*>(obj))) {
|
2013-05-22 11:56:47 +08:00
|
|
|
arrElement = BOOLEAN_TO_JSVAL(boolVal->getValue() ? JS_TRUE : JS_FALSE);
|
|
|
|
} else {
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "the type isn't suppored.");
|
2013-03-13 18:10:54 +08:00
|
|
|
}
|
2013-03-16 21:14:39 +08:00
|
|
|
}
|
2013-05-22 11:56:47 +08:00
|
|
|
if(!JS_SetElement(cx, jsretArr, i, &arrElement)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
2012-09-19 09:44:21 +08:00
|
|
|
}
|
2012-11-01 02:08:37 +08:00
|
|
|
return OBJECT_TO_JSVAL(jsretArr);
|
2012-09-19 09:44:21 +08:00
|
|
|
}
|
2012-08-28 10:22:36 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
jsval ccdictionary_to_jsval(JSContext* cx, Dictionary* dict)
|
2012-11-06 16:11:45 +08:00
|
|
|
{
|
|
|
|
JSObject* jsRet = JS_NewObject(cx, NULL, NULL, NULL);
|
2013-06-20 14:33:59 +08:00
|
|
|
DictElement* pElement = NULL;
|
2012-11-06 16:11:45 +08:00
|
|
|
CCDICT_FOREACH(dict, pElement)
|
|
|
|
{
|
|
|
|
jsval dictElement;
|
2013-06-20 14:33:59 +08:00
|
|
|
Object* obj = pElement->getObject();
|
2013-03-13 18:10:54 +08:00
|
|
|
//First, check whether object is associated with js object.
|
2013-06-20 14:33:59 +08:00
|
|
|
js_proxy_t* jsproxy = js_get_or_create_proxy<cocos2d::Object>(cx, obj);
|
2013-03-13 18:10:54 +08:00
|
|
|
if (jsproxy) {
|
|
|
|
dictElement = OBJECT_TO_JSVAL(jsproxy->obj);
|
|
|
|
}
|
|
|
|
else {
|
2013-06-20 14:33:59 +08:00
|
|
|
String* strVal = NULL;
|
|
|
|
Dictionary* dictVal = NULL;
|
|
|
|
Array* arrVal = NULL;
|
|
|
|
Double* doubleVal = NULL;
|
|
|
|
Bool* boolVal = NULL;
|
|
|
|
Float* floatVal = NULL;
|
|
|
|
Integer* intVal = NULL;
|
2013-03-13 18:10:54 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
if((strVal = dynamic_cast<cocos2d::String *>(obj))) {
|
2013-03-13 18:10:54 +08:00
|
|
|
dictElement = c_string_to_jsval(cx, strVal->getCString());
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((dictVal = dynamic_cast<Dictionary*>(obj))) {
|
2013-03-13 18:10:54 +08:00
|
|
|
dictElement = ccdictionary_to_jsval(cx, dictVal);
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((arrVal = dynamic_cast<Array*>(obj))) {
|
2013-03-13 18:10:54 +08:00
|
|
|
dictElement = ccarray_to_jsval(cx, arrVal);
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((doubleVal = dynamic_cast<Double*>(obj))) {
|
2013-03-13 18:10:54 +08:00
|
|
|
dictElement = DOUBLE_TO_JSVAL(doubleVal->getValue());
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((floatVal = dynamic_cast<Float*>(obj))) {
|
2013-03-13 18:10:54 +08:00
|
|
|
dictElement = DOUBLE_TO_JSVAL(floatVal->getValue());
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((intVal = dynamic_cast<Integer*>(obj))) {
|
2013-03-13 18:10:54 +08:00
|
|
|
dictElement = INT_TO_JSVAL(intVal->getValue());
|
2013-06-20 14:33:59 +08:00
|
|
|
} else if ((boolVal = dynamic_cast<Bool*>(obj))) {
|
2013-03-13 18:10:54 +08:00
|
|
|
dictElement = BOOLEAN_TO_JSVAL(boolVal->getValue() ? JS_TRUE : JS_FALSE);
|
|
|
|
} else {
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "the type isn't suppored.");
|
2013-03-13 18:10:54 +08:00
|
|
|
}
|
2012-11-06 16:11:45 +08:00
|
|
|
}
|
|
|
|
const char* key = pElement->getStrKey();
|
|
|
|
if (key && strlen(key) > 0)
|
|
|
|
{
|
|
|
|
JS_SetProperty(cx, jsRet, key, &dictElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OBJECT_TO_JSVAL(jsRet);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsval_to_ccdictionary(JSContext* cx, jsval v, Dictionary** ret) {
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-02-25 14:20:38 +08:00
|
|
|
if(JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v))
|
|
|
|
{
|
|
|
|
*ret = NULL;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2013-03-13 18:10:54 +08:00
|
|
|
|
|
|
|
JSObject* tmp = JSVAL_TO_OBJECT(v);
|
|
|
|
if (!tmp) {
|
|
|
|
LOGD("jsval_to_ccdictionary: the jsval is not an object.");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2013-02-25 14:20:38 +08:00
|
|
|
|
2013-03-13 18:10:54 +08:00
|
|
|
JSObject* it = JS_NewPropertyIterator(cx, tmp);
|
2013-06-20 14:33:59 +08:00
|
|
|
Dictionary* dict = NULL;
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-03-13 18:10:54 +08:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
jsid idp;
|
2012-11-07 03:03:07 +08:00
|
|
|
jsval key;
|
2013-03-13 18:10:54 +08:00
|
|
|
if (! JS_NextProperty(cx, it, &idp) || ! JS_IdToValue(cx, idp, &key)) {
|
|
|
|
return JS_FALSE; // error
|
2012-11-07 03:03:07 +08:00
|
|
|
}
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-03-13 18:10:54 +08:00
|
|
|
if (key == JSVAL_VOID) {
|
|
|
|
break; // end of iteration
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JSVAL_IS_STRING(key)) {
|
|
|
|
continue; // ignore integer properties
|
|
|
|
}
|
|
|
|
|
|
|
|
JSStringWrapper keyWrapper(JSVAL_TO_STRING(key), cx);
|
2012-11-07 03:03:07 +08:00
|
|
|
if(!dict) {
|
2013-06-20 14:33:59 +08:00
|
|
|
dict = Dictionary::create();
|
2012-11-07 03:03:07 +08:00
|
|
|
}
|
2013-03-13 18:10:54 +08:00
|
|
|
|
|
|
|
jsval value;
|
|
|
|
JS_GetPropertyById(cx, tmp, idp, &value);
|
|
|
|
if (value.isObject())
|
|
|
|
{
|
|
|
|
js_proxy_t *proxy;
|
|
|
|
JSObject *tmp = JSVAL_TO_OBJECT(value);
|
2013-06-04 12:53:42 +08:00
|
|
|
proxy = jsb_get_js_proxy(tmp);
|
2013-06-20 14:33:59 +08:00
|
|
|
cocos2d::Object* cobj = (cocos2d::Object *)(proxy ? proxy->ptr : NULL);
|
2013-03-13 18:10:54 +08:00
|
|
|
// Don't test it.
|
|
|
|
//TEST_NATIVE_OBJECT(cx, cobj)
|
|
|
|
if (cobj) {
|
|
|
|
// It's a native <-> js glue object.
|
|
|
|
dict->setObject(cobj, keyWrapper.get());
|
|
|
|
}
|
|
|
|
else if (!JS_IsArrayObject(cx, tmp)){
|
|
|
|
// It's a normal js object.
|
2013-06-20 14:33:59 +08:00
|
|
|
Dictionary* dictVal = NULL;
|
2013-03-13 18:10:54 +08:00
|
|
|
JSBool ok = jsval_to_ccdictionary(cx, value, &dictVal);
|
|
|
|
if (ok) {
|
|
|
|
dict->setObject(dictVal, keyWrapper.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// It's a js array object.
|
2013-06-20 14:33:59 +08:00
|
|
|
Array* arrVal = NULL;
|
2013-03-13 18:10:54 +08:00
|
|
|
JSBool ok = jsval_to_ccarray(cx, value, &arrVal);
|
|
|
|
if (ok) {
|
|
|
|
dict->setObject(arrVal, keyWrapper.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_STRING(value)) {
|
|
|
|
JSStringWrapper valueWapper(JSVAL_TO_STRING(value), cx);
|
2013-06-20 14:33:59 +08:00
|
|
|
dict->setObject(String::create(valueWapper.get()), keyWrapper.get());
|
2013-03-13 18:10:54 +08:00
|
|
|
// CCLOG("iterate object: key = %s, value = %s", keyWrapper.get().c_str(), valueWapper.get().c_str());
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_NUMBER(value)) {
|
|
|
|
double number = 0.0;
|
|
|
|
JSBool ok = JS_ValueToNumber(cx, value, &number);
|
|
|
|
if (ok) {
|
2013-06-20 14:33:59 +08:00
|
|
|
dict->setObject(Double::create(number), keyWrapper.get());
|
2013-03-13 18:10:54 +08:00
|
|
|
// CCLOG("iterate object: key = %s, value = %lf", keyWrapper.get().c_str(), number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_BOOLEAN(value)) {
|
|
|
|
JSBool boolVal = JS_FALSE;
|
|
|
|
JSBool ok = JS_ValueToBoolean(cx, value, &boolVal);
|
|
|
|
if (ok) {
|
2013-06-20 14:33:59 +08:00
|
|
|
dict->setObject(Bool::create(boolVal), keyWrapper.get());
|
2013-03-13 18:10:54 +08:00
|
|
|
// CCLOG("iterate object: key = %s, value = %d", keyWrapper.get().c_str(), boolVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "not supported type");
|
2013-03-13 18:10:54 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-22 00:33:37 +08:00
|
|
|
|
2013-01-11 13:54:57 +08:00
|
|
|
*ret = dict;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
JSBool jsval_to_ccaffinetransform(JSContext* cx, jsval v, AffineTransform* ret)
|
2013-04-18 13:50:58 +08:00
|
|
|
{
|
|
|
|
JSObject *tmp;
|
|
|
|
jsval jsa, jsb, jsc, jsd, jstx, jsty;
|
|
|
|
double a, b, c, d, tx, ty;
|
|
|
|
JSBool ok = JS_ValueToObject(cx, v, &tmp) &&
|
|
|
|
JS_GetProperty(cx, tmp, "a", &jsa) &&
|
|
|
|
JS_GetProperty(cx, tmp, "b", &jsb) &&
|
|
|
|
JS_GetProperty(cx, tmp, "c", &jsc) &&
|
|
|
|
JS_GetProperty(cx, tmp, "d", &jsd) &&
|
|
|
|
JS_GetProperty(cx, tmp, "tx", &jstx) &&
|
|
|
|
JS_GetProperty(cx, tmp, "ty", &jsty) &&
|
|
|
|
JS_ValueToNumber(cx, jsa, &a) &&
|
|
|
|
JS_ValueToNumber(cx, jsb, &b) &&
|
|
|
|
JS_ValueToNumber(cx, jsc, &c) &&
|
|
|
|
JS_ValueToNumber(cx, jsd, &d) &&
|
|
|
|
JS_ValueToNumber(cx, jstx, &tx) &&
|
|
|
|
JS_ValueToNumber(cx, jsty, &ty);
|
|
|
|
|
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
*ret = AffineTransformMake(a, b, c, d, tx, ty);
|
2013-04-18 13:50:58 +08:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-11 13:54:57 +08:00
|
|
|
// From native type to jsval
|
|
|
|
jsval int32_to_jsval( JSContext *cx, int32_t number )
|
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
return INT_TO_JSVAL(number);
|
2013-01-11 13:54:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
jsval uint32_to_jsval( JSContext *cx, uint32_t number )
|
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
return UINT_TO_JSVAL(number);
|
2012-11-07 03:03:07 +08:00
|
|
|
}
|
|
|
|
|
2012-08-28 10:22:36 +08:00
|
|
|
jsval long_long_to_jsval(JSContext* cx, long long v) {
|
|
|
|
JSObject *tmp = JS_NewUint32Array(cx, 2);
|
2013-02-27 15:16:49 +08:00
|
|
|
uint32_t *data = (uint32_t *)JS_GetArrayBufferViewData(tmp);
|
2012-08-28 10:22:36 +08:00
|
|
|
data[0] = ((uint32_t *)(&v))[0];
|
|
|
|
data[1] = ((uint32_t *)(&v))[1];
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval std_string_to_jsval(JSContext* cx, const std::string& v) {
|
2013-03-22 12:01:08 +08:00
|
|
|
return c_string_to_jsval(cx, v.c_str());
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-04-17 16:08:48 +08:00
|
|
|
jsval c_string_to_jsval(JSContext* cx, const char* v, size_t length /* = -1 */) {
|
2013-03-22 12:01:08 +08:00
|
|
|
if (v == NULL) {
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
jsval ret = JSVAL_NULL;
|
2013-04-17 17:05:39 +08:00
|
|
|
int utf16_size = 0;
|
2013-04-17 16:08:48 +08:00
|
|
|
jschar* strUTF16 = (jschar*)cc_utf8_to_utf16(v, length, &utf16_size);
|
|
|
|
|
|
|
|
if (strUTF16 && utf16_size > 0) {
|
|
|
|
JSString* str = JS_NewUCStringCopyN(cx, strUTF16, utf16_size);
|
2013-03-22 12:01:08 +08:00
|
|
|
if (str) {
|
|
|
|
ret = STRING_TO_JSVAL(str);
|
|
|
|
}
|
|
|
|
delete[] strUTF16;
|
|
|
|
}
|
|
|
|
return ret;
|
2012-08-28 10:22:36 +08:00
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval ccpoint_to_jsval(JSContext* cx, const Point& v) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_DefineProperty(cx, tmp, "x", DOUBLE_TO_JSVAL(v.x), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "y", DOUBLE_TO_JSVAL(v.y), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval ccacceleration_to_jsval(JSContext* cx, const Acceleration& v) {
|
2012-11-14 06:58:11 +08:00
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_DefineProperty(cx, tmp, "x", DOUBLE_TO_JSVAL(v.x), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "y", DOUBLE_TO_JSVAL(v.y), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "z", DOUBLE_TO_JSVAL(v.z), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "timestamp", DOUBLE_TO_JSVAL(v.timestamp), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval ccrect_to_jsval(JSContext* cx, const Rect& v) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_DefineProperty(cx, tmp, "x", DOUBLE_TO_JSVAL(v.origin.x), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "y", DOUBLE_TO_JSVAL(v.origin.y), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "width", DOUBLE_TO_JSVAL(v.size.width), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "height", DOUBLE_TO_JSVAL(v.size.height), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval ccsize_to_jsval(JSContext* cx, const Size& v) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_DefineProperty(cx, tmp, "width", DOUBLE_TO_JSVAL(v.width), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "height", DOUBLE_TO_JSVAL(v.height), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval cccolor4b_to_jsval(JSContext* cx, const Color4B& v) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_DefineProperty(cx, tmp, "r", INT_TO_JSVAL(v.r), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "g", INT_TO_JSVAL(v.g), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "b", INT_TO_JSVAL(v.g), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "a", INT_TO_JSVAL(v.g), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval cccolor4f_to_jsval(JSContext* cx, const Color4F& v) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_DefineProperty(cx, tmp, "r", DOUBLE_TO_JSVAL(v.r), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "g", DOUBLE_TO_JSVAL(v.g), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "b", DOUBLE_TO_JSVAL(v.g), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "a", DOUBLE_TO_JSVAL(v.g), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval cccolor3b_to_jsval(JSContext* cx, const Color3B& v) {
|
2012-08-28 10:22:36 +08:00
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_DefineProperty(cx, tmp, "r", INT_TO_JSVAL(v.r), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "g", INT_TO_JSVAL(v.g), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "b", INT_TO_JSVAL(v.g), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
2012-10-19 08:44:41 +08:00
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval ccaffinetransform_to_jsval(JSContext* cx, const AffineTransform& t)
|
2013-04-18 13:50:58 +08:00
|
|
|
{
|
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_DefineProperty(cx, tmp, "a", DOUBLE_TO_JSVAL(t.a), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "b", DOUBLE_TO_JSVAL(t.b), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "c", DOUBLE_TO_JSVAL(t.c), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "d", DOUBLE_TO_JSVAL(t.d), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "tx", DOUBLE_TO_JSVAL(t.tx), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
|
|
|
|
JS_DefineProperty(cx, tmp, "ty", DOUBLE_TO_JSVAL(t.ty), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
jsval FontDefinition_to_jsval(JSContext* cx, const FontDefinition& t)
|
|
|
|
{
|
|
|
|
JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!tmp) return JSVAL_NULL;
|
|
|
|
JSBool ok = JS_TRUE;
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "fontName", std_string_to_jsval(cx, t._fontName), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "fontSize", int32_to_jsval(cx, t._fontSize), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "fontAlignmentH", int32_to_jsval(cx, t._alignment), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "fontAlignmentV", int32_to_jsval(cx, t._vertAlignment), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "fontFillColor", cccolor3b_to_jsval(cx, t._fontFillColor), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "fontDimensions", ccsize_to_jsval(cx, t._dimensions), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
// Shadow
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "shadowEnabled", BOOLEAN_TO_JSVAL(t._shadow._shadowEnabled), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "shadowOffset", ccsize_to_jsval(cx, t._shadow._shadowOffset), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "shadowBlur", DOUBLE_TO_JSVAL(t._shadow._shadowBlur), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "shadowOpacity", DOUBLE_TO_JSVAL(t._shadow._shadowOpacity), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
// Stroke
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "strokeEnabled", BOOLEAN_TO_JSVAL(t._stroke._strokeEnabled), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "strokeColor", cccolor3b_to_jsval(cx, t._stroke._strokeColor), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
ok &= JS_DefineProperty(cx, tmp, "strokeSize", DOUBLE_TO_JSVAL(t._stroke._strokeSize), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
return OBJECT_TO_JSVAL(tmp);
|
|
|
|
}
|
|
|
|
return JSVAL_NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-16 03:14:57 +08:00
|
|
|
#pragma mark - Debug
|
|
|
|
|
2013-01-17 06:02:05 +08:00
|
|
|
void SimpleRunLoop::update(float dt) {
|
2013-06-26 14:41:19 +08:00
|
|
|
std::lock_guard<std::mutex> lk(g_qMutex);
|
|
|
|
|
2013-06-21 22:29:48 +08:00
|
|
|
while (g_queue.size() > 0) {
|
|
|
|
vector<string>::iterator first = g_queue.begin();
|
2013-02-22 00:34:51 +08:00
|
|
|
string str = *first;
|
|
|
|
ScriptingCore::getInstance()->debugProcessInput(str);
|
2013-06-21 22:29:48 +08:00
|
|
|
g_queue.erase(first);
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
2013-01-17 06:02:05 +08:00
|
|
|
}
|
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
void ScriptingCore::debugProcessInput(string str) {
|
2013-02-22 00:34:51 +08:00
|
|
|
JSString* jsstr = JS_NewStringCopyZ(cx_, str.c_str());
|
|
|
|
jsval argv[3] = {
|
|
|
|
STRING_TO_JSVAL(jsstr),
|
|
|
|
frame,
|
|
|
|
script
|
|
|
|
};
|
|
|
|
jsval outval;
|
|
|
|
JSAutoCompartment ac(cx_, debugGlobal_);
|
|
|
|
JS_CallFunctionName(cx_, debugGlobal_, "processInput", 3, argv, &outval);
|
2013-01-12 06:01:36 +08:00
|
|
|
}
|
|
|
|
|
2012-11-16 03:14:57 +08:00
|
|
|
void ScriptingCore::enableDebugger() {
|
2013-02-22 00:34:51 +08:00
|
|
|
if (debugGlobal_ == NULL) {
|
|
|
|
debugGlobal_ = NewGlobalObject(cx_, true);
|
|
|
|
JS_WrapObject(cx_, &debugGlobal_);
|
|
|
|
JSAutoCompartment ac(cx_, debugGlobal_);
|
|
|
|
// these are used in the debug program
|
|
|
|
JS_DefineFunction(cx_, debugGlobal_, "log", ScriptingCore::log, 0, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx_, debugGlobal_, "_bufferWrite", JSBDebug_BufferWrite, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx_, debugGlobal_, "_bufferRead", JSBDebug_BufferRead, 0, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx_, debugGlobal_, "_lockVM", JSBDebug_LockExecution, 2, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx_, debugGlobal_, "_unlockVM", JSBDebug_UnlockExecution, 0, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
|
|
|
|
runScript("jsb_debugger.js", debugGlobal_);
|
|
|
|
|
|
|
|
// prepare the debugger
|
|
|
|
jsval argv = OBJECT_TO_JSVAL(global_);
|
|
|
|
jsval outval;
|
|
|
|
JSBool ok = JS_CallFunctionName(cx_, debugGlobal_, "_prepareDebugger", 1, &argv, &outval);
|
|
|
|
if (!ok) {
|
|
|
|
JS_ReportPendingException(cx_);
|
|
|
|
}
|
|
|
|
// define the start debugger function
|
|
|
|
JS_DefineFunction(cx_, global_, "startDebugger", JSBDebug_StartDebugger, 3, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
// start bg thread
|
2013-06-26 14:41:19 +08:00
|
|
|
|
|
|
|
auto t = std::thread(&serverEntryPoint);
|
|
|
|
t.detach();
|
2013-02-22 00:34:51 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Scheduler* scheduler = Director::getInstance()->getScheduler();
|
2013-02-22 00:34:51 +08:00
|
|
|
scheduler->scheduleUpdateForTarget(this->runLoop, 0, false);
|
|
|
|
}
|
2012-11-16 03:14:57 +08:00
|
|
|
}
|
2012-10-19 08:44:41 +08:00
|
|
|
|
2012-11-16 03:14:57 +08:00
|
|
|
JSBool jsStartDebugger(JSContext* cx, unsigned argc, jsval* vp)
|
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
JSObject* debugGlobal = ScriptingCore::getInstance()->getDebugGlobal();
|
|
|
|
if (argc >= 2) {
|
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
|
|
|
jsval out;
|
|
|
|
JS_WrapObject(cx, &debugGlobal);
|
|
|
|
JSAutoCompartment ac(cx, debugGlobal);
|
|
|
|
JS_CallFunctionName(cx, debugGlobal, "_startDebugger", argc, argv, &out);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
2012-11-16 03:14:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool jsGetScript(JSContext* cx, unsigned argc, jsval* vp)
|
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
|
|
|
if (argc == 1 && argv[0].isString()) {
|
|
|
|
JSString* str = argv[0].toString();
|
|
|
|
JSStringWrapper wrapper(str);
|
|
|
|
JSScript* script = filename_script[(char *)wrapper];
|
|
|
|
if (script) {
|
|
|
|
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL((JSObject*)script));
|
|
|
|
} else {
|
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
2012-11-16 03:14:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* NewGlobalObject(JSContext* cx, bool debug)
|
2012-10-19 08:44:41 +08:00
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
JSObject* glob = JS_NewGlobalObject(cx, &global_class, NULL);
|
|
|
|
if (!glob) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
JSAutoCompartment ac(cx, glob);
|
|
|
|
JSBool ok = JS_TRUE;
|
|
|
|
ok = JS_InitStandardClasses(cx, glob);
|
|
|
|
if (ok)
|
|
|
|
JS_InitReflect(cx, glob);
|
|
|
|
if (ok && debug)
|
|
|
|
ok = JS_DefineDebuggerObject(cx, glob);
|
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
2012-10-19 08:44:41 +08:00
|
|
|
|
2012-10-19 11:15:23 +08:00
|
|
|
return glob;
|
2012-10-19 08:44:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
JSBool jsb_set_reserved_slot(JSObject *obj, uint32_t idx, jsval value)
|
2012-10-19 08:44:41 +08:00
|
|
|
{
|
2013-01-12 06:01:36 +08:00
|
|
|
JSClass *klass = JS_GetClass(obj);
|
|
|
|
unsigned int slots = JSCLASS_RESERVED_SLOTS(klass);
|
|
|
|
if( idx >= slots )
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JS_SetReservedSlot(obj, idx, value);
|
|
|
|
|
2012-10-19 11:15:23 +08:00
|
|
|
return JS_TRUE;
|
2012-10-19 08:44:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
JSBool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret)
|
2012-10-19 08:44:41 +08:00
|
|
|
{
|
2013-01-12 06:01:36 +08:00
|
|
|
JSClass *klass = JS_GetClass(obj);
|
|
|
|
unsigned int slots = JSCLASS_RESERVED_SLOTS(klass);
|
|
|
|
if( idx >= slots )
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
ret = JS_GetReservedSlot(obj, idx);
|
|
|
|
|
2012-10-19 11:15:23 +08:00
|
|
|
return JS_TRUE;
|
2012-10-19 08:44:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-03 22:31:24 +08:00
|
|
|
js_proxy_t* jsb_new_proxy(void* nativeObj, JSObject* jsObj)
|
|
|
|
{
|
|
|
|
js_proxy_t* p;
|
|
|
|
JS_NEW_PROXY(p, nativeObj, jsObj);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
js_proxy_t* jsb_get_native_proxy(void* nativeObj)
|
|
|
|
{
|
|
|
|
js_proxy_t* p;
|
|
|
|
JS_GET_PROXY(p, nativeObj);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
js_proxy_t* jsb_get_js_proxy(JSObject* jsObj)
|
|
|
|
{
|
|
|
|
js_proxy_t* p;
|
|
|
|
JS_GET_NATIVE_PROXY(p, jsObj);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void jsb_remove_proxy(js_proxy_t* nativeProxy, js_proxy_t* jsProxy)
|
|
|
|
{
|
|
|
|
JS_REMOVE_PROXY(nativeProxy, jsProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
//#pragma mark - Debugger
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
JSBool JSBDebug_StartDebugger(JSContext* cx, unsigned argc, jsval* vp)
|
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
JSObject* debugGlobal = ScriptingCore::getInstance()->getDebugGlobal();
|
|
|
|
if (argc >= 2) {
|
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
|
|
|
jsval out;
|
|
|
|
JS_WrapObject(cx, &debugGlobal);
|
|
|
|
JSAutoCompartment ac(cx, debugGlobal);
|
|
|
|
JS_CallFunctionName(cx, debugGlobal, "_startDebugger", argc, argv, &out);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
2013-01-12 06:01:36 +08:00
|
|
|
}
|
2012-10-20 00:54:21 +08:00
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
JSBool JSBDebug_BufferRead(JSContext* cx, unsigned argc, jsval* vp)
|
|
|
|
{
|
|
|
|
if (argc == 0) {
|
2013-02-22 00:34:51 +08:00
|
|
|
JSString* str;
|
|
|
|
// this is safe because we're already inside a lock (from clearBuffers)
|
|
|
|
if (vmLock) {
|
2013-06-26 14:41:19 +08:00
|
|
|
g_rwMutex.lock();
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
|
|
|
str = JS_NewStringCopyZ(cx, inData.c_str());
|
|
|
|
inData.clear();
|
|
|
|
if (vmLock) {
|
2013-06-26 14:41:19 +08:00
|
|
|
g_rwMutex.unlock();
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
|
|
|
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(str));
|
2013-01-12 06:01:36 +08:00
|
|
|
} else {
|
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_NULL);
|
2012-10-19 11:15:23 +08:00
|
|
|
}
|
|
|
|
return JS_TRUE;
|
2012-10-19 08:44:41 +08:00
|
|
|
}
|
|
|
|
|
2013-05-14 12:08:59 +08:00
|
|
|
static void _clientSocketWriteAndClearString(std::string& s) {
|
|
|
|
#if JSB_DEBUGGER_OUTPUT_STDOUT
|
|
|
|
write(STDOUT_FILENO, s.c_str(), s.length());
|
|
|
|
#endif
|
|
|
|
write(clientSocket, s.c_str(), s.length());
|
|
|
|
s.clear();
|
|
|
|
}
|
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
JSBool JSBDebug_BufferWrite(JSContext* cx, unsigned argc, jsval* vp)
|
2012-10-19 08:44:41 +08:00
|
|
|
{
|
2012-10-19 11:15:23 +08:00
|
|
|
if (argc == 1) {
|
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
2013-03-08 14:34:59 +08:00
|
|
|
JSStringWrapper strWrapper(argv[0]);
|
2013-02-22 00:34:51 +08:00
|
|
|
// this is safe because we're already inside a lock (from clearBuffers)
|
2013-02-27 15:16:49 +08:00
|
|
|
outData.append(strWrapper.get());
|
2013-05-14 12:08:59 +08:00
|
|
|
_clientSocketWriteAndClearString(outData);
|
2012-10-19 11:15:23 +08:00
|
|
|
}
|
|
|
|
return JS_TRUE;
|
2012-10-19 08:44:41 +08:00
|
|
|
}
|
2012-11-30 22:00:30 +08:00
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
// this should lock the execution of the running thread, waiting for a signal
|
|
|
|
JSBool JSBDebug_LockExecution(JSContext* cx, unsigned argc, jsval* vp)
|
2012-12-04 10:52:49 +08:00
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
if (argc == 2) {
|
|
|
|
printf("locking vm\n");
|
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
|
|
|
frame = argv[0];
|
|
|
|
script = argv[1];
|
|
|
|
vmLock = true;
|
|
|
|
while (vmLock) {
|
|
|
|
// try to read the input, if there's anything
|
2013-06-26 14:41:19 +08:00
|
|
|
g_qMutex.lock();
|
2013-06-21 22:29:48 +08:00
|
|
|
while (g_queue.size() > 0) {
|
|
|
|
vector<string>::iterator first = g_queue.begin();
|
2013-02-22 00:34:51 +08:00
|
|
|
string str = *first;
|
|
|
|
ScriptingCore::getInstance()->debugProcessInput(str);
|
2013-06-21 22:29:48 +08:00
|
|
|
g_queue.erase(first);
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
2013-06-26 14:41:19 +08:00
|
|
|
g_qMutex.unlock();
|
2013-06-26 14:55:20 +08:00
|
|
|
std::this_thread::yield();
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
|
|
|
printf("vm unlocked\n");
|
|
|
|
frame = JSVAL_NULL;
|
|
|
|
script = JSVAL_NULL;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
JS_ReportError(cx, "invalid call to _lockVM");
|
|
|
|
return JS_FALSE;
|
2012-12-04 10:52:49 +08:00
|
|
|
}
|
|
|
|
|
2013-01-12 06:01:36 +08:00
|
|
|
JSBool JSBDebug_UnlockExecution(JSContext* cx, unsigned argc, jsval* vp)
|
2012-12-04 10:52:49 +08:00
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
vmLock = false;
|
|
|
|
return JS_TRUE;
|
2013-01-12 06:01:36 +08:00
|
|
|
}
|
2012-12-04 10:52:49 +08:00
|
|
|
|
2013-06-26 14:41:19 +08:00
|
|
|
static void processInput(string data) {
|
|
|
|
std::lock_guard<std::mutex> lk(g_qMutex);
|
2013-06-21 22:29:48 +08:00
|
|
|
g_queue.push_back(string(data));
|
2013-01-12 06:01:36 +08:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:41:19 +08:00
|
|
|
static void clearBuffers() {
|
|
|
|
std::lock_guard<std::mutex> lk(g_rwMutex);
|
|
|
|
// only process input if there's something and we're not locked
|
|
|
|
if (inData.length() > 0) {
|
|
|
|
processInput(inData);
|
|
|
|
inData.clear();
|
|
|
|
}
|
|
|
|
if (outData.length() > 0) {
|
|
|
|
_clientSocketWriteAndClearString(outData);
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
2013-01-12 06:01:36 +08:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:41:19 +08:00
|
|
|
static void serverEntryPoint(void)
|
2013-01-12 06:01:36 +08:00
|
|
|
{
|
2013-02-22 00:34:51 +08:00
|
|
|
// start a server, accept the connection and keep reading data from it
|
|
|
|
struct addrinfo hints, *result, *rp;
|
|
|
|
int s;
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2013-06-05 01:31:53 +08:00
|
|
|
hints.ai_family = AF_INET; // IPv4
|
|
|
|
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
|
|
|
|
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
|
2013-02-22 00:34:51 +08:00
|
|
|
|
|
|
|
stringstream portstr;
|
|
|
|
portstr << JSB_DEBUGGER_PORT;
|
2013-06-05 01:31:53 +08:00
|
|
|
|
|
|
|
int err;
|
|
|
|
if ((err = getaddrinfo(NULL, portstr.str().c_str(), &hints, &result)) != 0) {
|
|
|
|
LOGD("getaddrinfo error : %s\n", gai_strerror(err));
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
|
|
if ((s = socket(rp->ai_family, rp->ai_socktype, 0)) < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int optval = 1;
|
|
|
|
if ((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof(optval))) < 0) {
|
|
|
|
close(s);
|
2013-05-14 12:11:19 +08:00
|
|
|
TRACE_DEBUGGER_SERVER("debug server : error setting socket option SO_REUSEADDR");
|
2013-06-26 14:41:19 +08:00
|
|
|
return;
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
2013-05-14 12:11:19 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
if ((setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval))) < 0) {
|
|
|
|
close(s);
|
|
|
|
TRACE_DEBUGGER_SERVER("debug server : error setting socket option SO_NOSIGPIPE");
|
2013-06-26 14:41:19 +08:00
|
|
|
return;
|
2013-05-14 12:11:19 +08:00
|
|
|
}
|
|
|
|
#endif //(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
2013-02-22 00:34:51 +08:00
|
|
|
if ((::bind(s, rp->ai_addr, rp->ai_addrlen)) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
}
|
|
|
|
if (s < 0 || rp == NULL) {
|
2013-05-14 11:48:49 +08:00
|
|
|
TRACE_DEBUGGER_SERVER("debug server : error creating/binding socket");
|
2013-06-26 14:41:19 +08:00
|
|
|
return;
|
2013-02-22 00:34:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
|
|
|
|
listen(s, 1);
|
2013-05-14 11:48:49 +08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
clientSocket = accept(s, NULL, NULL);
|
|
|
|
if (clientSocket < 0)
|
|
|
|
{
|
|
|
|
TRACE_DEBUGGER_SERVER("debug server : error on accept");
|
2013-06-26 14:41:19 +08:00
|
|
|
return;
|
2013-05-14 11:48:49 +08:00
|
|
|
} else {
|
|
|
|
// read/write data
|
|
|
|
TRACE_DEBUGGER_SERVER("debug server : client connected");
|
2013-02-22 00:34:51 +08:00
|
|
|
char buf[256];
|
|
|
|
int readBytes;
|
|
|
|
while ((readBytes = read(clientSocket, buf, 256)) > 0) {
|
|
|
|
buf[readBytes] = '\0';
|
2013-05-14 11:48:49 +08:00
|
|
|
TRACE_DEBUGGER_SERVER("debug server : received command >%s", buf);
|
2013-02-22 00:34:51 +08:00
|
|
|
// no other thread is using this
|
|
|
|
inData.append(buf);
|
|
|
|
// process any input, send any output
|
|
|
|
clearBuffers();
|
|
|
|
} // while(read)
|
2013-05-14 11:48:49 +08:00
|
|
|
close(clientSocket);
|
|
|
|
}
|
|
|
|
} // while(true)
|
2012-11-30 22:00:30 +08:00
|
|
|
}
|
2013-05-08 08:22:25 +08:00
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
static Color3B getColorFromJSObject(JSContext *cx, JSObject *colorObject)
|
2013-05-08 08:22:25 +08:00
|
|
|
{
|
|
|
|
jsval jsr;
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B out;
|
2013-05-08 08:22:25 +08:00
|
|
|
JS_GetProperty(cx, colorObject, "r", &jsr);
|
|
|
|
double fontR = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &fontR);
|
|
|
|
|
|
|
|
JS_GetProperty(cx, colorObject, "g", &jsr);
|
|
|
|
double fontG = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &fontG);
|
|
|
|
|
|
|
|
JS_GetProperty(cx, colorObject, "b", &jsr);
|
|
|
|
double fontB = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &fontB);
|
|
|
|
|
|
|
|
// the out
|
|
|
|
out.r = (unsigned char)fontR;
|
|
|
|
out.g = (unsigned char)fontG;
|
|
|
|
out.b = (unsigned char)fontB;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
Size getSizeFromJSObject(JSContext *cx, JSObject *sizeObject)
|
2013-05-08 08:22:25 +08:00
|
|
|
{
|
|
|
|
jsval jsr;
|
2013-06-20 14:33:59 +08:00
|
|
|
Size out;
|
2013-05-08 08:22:25 +08:00
|
|
|
JS_GetProperty(cx, sizeObject, "width", &jsr);
|
|
|
|
double width = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &width);
|
|
|
|
|
|
|
|
JS_GetProperty(cx, sizeObject, "height", &jsr);
|
|
|
|
double height = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &height);
|
|
|
|
|
|
|
|
|
|
|
|
// the out
|
|
|
|
out.width = width;
|
|
|
|
out.height = height;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:18:57 +08:00
|
|
|
JSBool jsval_to_FontDefinition( JSContext *cx, jsval vp, FontDefinition *out )
|
2013-05-08 08:22:25 +08:00
|
|
|
{
|
|
|
|
JSObject *jsobj;
|
|
|
|
|
|
|
|
if( ! JS_ValueToObject( cx, vp, &jsobj ) )
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JSB_PRECONDITION( jsobj, "Not a valid JS object");
|
|
|
|
|
|
|
|
// defaul values
|
|
|
|
const char * defautlFontName = "Arial";
|
|
|
|
const int defaultFontSize = 32;
|
2013-06-20 14:33:59 +08:00
|
|
|
TextAlignment defaultTextAlignment = kTextAlignmentLeft;
|
|
|
|
VerticalTextAlignment defaultTextVAlignment = kVerticalTextAlignmentTop;
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
// by default shadow and stroke are off
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_shadow._shadowEnabled = false;
|
|
|
|
out->_stroke._strokeEnabled = false;
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
// white text by default
|
2013-07-08 18:11:32 +08:00
|
|
|
out->_fontFillColor = Color3B::WHITE;
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
// font name
|
|
|
|
jsval jsr;
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "fontName", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
JS_ValueToString(cx, jsr);
|
|
|
|
JSStringWrapper wrapper(jsr);
|
|
|
|
if ( wrapper )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_fontName = (char*)wrapper;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_fontName = defautlFontName;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// font size
|
|
|
|
JSBool hasProperty;
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "fontSize", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "fontSize", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
double fontSize = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &fontSize);
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_fontSize = fontSize;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_fontSize = defaultFontSize;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// font alignment horizontal
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "fontAlignmentH", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "fontAlignmentH", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
double fontAlign = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &fontAlign);
|
2013-06-20 14:33:59 +08:00
|
|
|
out->_alignment = (TextAlignment)(int)fontAlign;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_alignment = defaultTextAlignment;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// font alignment vertical
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "fontAlignmentV", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "fontAlignmentV", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
double fontAlign = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &fontAlign);
|
2013-06-20 14:33:59 +08:00
|
|
|
out->_vertAlignment = (VerticalTextAlignment)(int)fontAlign;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_vertAlignment = defaultTextVAlignment;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// font fill color
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "fontFillColor", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "fontFillColor", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
JSObject *jsobjColor;
|
|
|
|
if( ! JS_ValueToObject( cx, jsr, &jsobjColor ) )
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_fontFillColor = getColorFromJSObject(cx, jsobjColor);
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// font rendering box dimensions
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "fontDimensions", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "fontDimensions", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
JSObject *jsobjSize;
|
|
|
|
if( ! JS_ValueToObject( cx, jsr, &jsobjSize ) )
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_dimensions = getSizeFromJSObject(cx, jsobjSize);
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// shadow
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "shadowEnabled", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "shadowEnabled", &jsr);
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_shadow._shadowEnabled = ToBoolean(jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( out->_shadow._shadowEnabled )
|
2013-05-08 08:22:25 +08:00
|
|
|
{
|
|
|
|
// default shadow values
|
2013-06-20 14:33:59 +08:00
|
|
|
out->_shadow._shadowOffset = Size(5, 5);
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_shadow._shadowBlur = 1;
|
|
|
|
out->_shadow._shadowOpacity = 1;
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
// shado offset
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "shadowOffset", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "shadowOffset", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
JSObject *jsobjShadowOffset;
|
|
|
|
if( ! JS_ValueToObject( cx, jsr, &jsobjShadowOffset ) )
|
|
|
|
return JS_FALSE;
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_shadow._shadowOffset = getSizeFromJSObject(cx, jsobjShadowOffset);
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// shadow blur
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "shadowBlur", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "shadowBlur", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
double shadowBlur = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &shadowBlur);
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_shadow._shadowBlur = shadowBlur;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// shadow intensity
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "shadowOpacity", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "shadowOpacity", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
double shadowOpacity = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &shadowOpacity);
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_shadow._shadowOpacity = shadowOpacity;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stroke
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "strokeEnabled", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "strokeEnabled", &jsr);
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_stroke._strokeEnabled = ToBoolean(jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( out->_stroke._strokeEnabled )
|
2013-05-08 08:22:25 +08:00
|
|
|
{
|
|
|
|
// default stroke values
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_stroke._strokeSize = 1;
|
2013-07-08 18:11:32 +08:00
|
|
|
out->_stroke._strokeColor = Color3B::BLUE;
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
// stroke color
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "strokeColor", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "strokeColor", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
|
|
|
|
JSObject *jsobjStrokeColor;
|
|
|
|
if( ! JS_ValueToObject( cx, jsr, &jsobjStrokeColor ) )
|
|
|
|
return JS_FALSE;
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_stroke._strokeColor = getColorFromJSObject(cx, jsobjStrokeColor);
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// stroke size
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_HasProperty(cx, jsobj, "strokeSize", &hasProperty);
|
2013-05-08 08:22:25 +08:00
|
|
|
if ( hasProperty )
|
|
|
|
{
|
2013-05-11 05:15:02 +08:00
|
|
|
JS_GetProperty(cx, jsobj, "strokeSize", &jsr);
|
2013-05-08 08:22:25 +08:00
|
|
|
double strokeSize = 0.0;
|
|
|
|
JS_ValueToNumber(cx, jsr, &strokeSize);
|
2013-06-15 14:03:30 +08:00
|
|
|
out->_stroke._strokeSize = strokeSize;
|
2013-05-08 08:22:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we are done here
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|