mirror of https://github.com/axmolengine/axmol.git
Merge pull request #4445 from dumganhar/iss2790-template-container
issue #2790: Fix of JSB project broken after using Vector<T>
This commit is contained in:
commit
0d30f2bbbb
|
@ -86,13 +86,14 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu
|
|||
ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+
|
||||
*/
|
||||
|
||||
ValueMap& metadataDict = dictionary["metadata"].asValueMap();
|
||||
|
||||
ValueMap& framesDict = dictionary["frames"].asValueMap();
|
||||
int format = 0;
|
||||
|
||||
// get the format
|
||||
if (!metadataDict.empty())
|
||||
if (dictionary.find("metadata") != dictionary.end())
|
||||
{
|
||||
ValueMap& metadataDict = dictionary["metadata"].asValueMap();
|
||||
format = metadataDict["format"].asInt();
|
||||
}
|
||||
|
||||
|
@ -230,9 +231,9 @@ void SpriteFrameCache::addSpriteFramesWithFile(const std::string& pszPlist)
|
|||
|
||||
string texturePath("");
|
||||
|
||||
ValueMap& metadataDict = dict["metadata"].asValueMap();
|
||||
if (!metadataDict.empty())
|
||||
if (dict.find("metadata") != dict.end())
|
||||
{
|
||||
ValueMap& metadataDict = dict["metadata"].asValueMap();
|
||||
// try to read texture file name from meta data
|
||||
texturePath = metadataDict["textureFileName"].asString();
|
||||
}
|
||||
|
|
|
@ -1480,21 +1480,21 @@ JSBool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret)
|
|||
|
||||
js_proxy_t* jsb_new_proxy(void* nativeObj, JSObject* jsObj)
|
||||
{
|
||||
js_proxy_t* p;
|
||||
js_proxy_t* p = nullptr;
|
||||
JS_NEW_PROXY(p, nativeObj, jsObj);
|
||||
return p;
|
||||
}
|
||||
|
||||
js_proxy_t* jsb_get_native_proxy(void* nativeObj)
|
||||
{
|
||||
js_proxy_t* p;
|
||||
js_proxy_t* p = nullptr;
|
||||
JS_GET_PROXY(p, nativeObj);
|
||||
return p;
|
||||
}
|
||||
|
||||
js_proxy_t* jsb_get_js_proxy(JSObject* jsObj)
|
||||
{
|
||||
js_proxy_t* p;
|
||||
js_proxy_t* p = nullptr;
|
||||
JS_GET_NATIVE_PROXY(p, jsObj);
|
||||
return p;
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
d725d2de77c275c86fc8739e13fdf31684cacf74
|
||||
cd3a15ee39c63c93b40e323eda6fa43f584742aa
|
|
@ -508,6 +508,71 @@ JSBool jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc, Array** r
|
|||
return ok;
|
||||
}
|
||||
|
||||
JSBool jsvals_variadic_to_ccvaluevector( JSContext *cx, jsval *vp, int argc, cocos2d::ValueVector* ret)
|
||||
{
|
||||
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
jsval value = *vp;
|
||||
if (value.isObject())
|
||||
{
|
||||
JSObject* jsobj = JSVAL_TO_OBJECT(value);
|
||||
CCASSERT(jsb_get_js_proxy(jsobj) == nullptr, "Native object should be added!");
|
||||
|
||||
if (!JS_IsArrayObject(cx, jsobj))
|
||||
{
|
||||
// It's a normal js object.
|
||||
ValueMap dictVal;
|
||||
JSBool ok = jsval_to_ccvaluemap(cx, value, &dictVal);
|
||||
if (ok)
|
||||
{
|
||||
ret->push_back(Value(dictVal));
|
||||
}
|
||||
}
|
||||
else {
|
||||
// It's a js array object.
|
||||
ValueVector arrVal;
|
||||
JSBool ok = jsval_to_ccvaluevector(cx, value, &arrVal);
|
||||
if (ok)
|
||||
{
|
||||
ret->push_back(Value(arrVal));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_STRING(value))
|
||||
{
|
||||
JSStringWrapper valueWapper(JSVAL_TO_STRING(value), cx);
|
||||
ret->push_back(Value(valueWapper.get()));
|
||||
}
|
||||
else if (JSVAL_IS_NUMBER(value))
|
||||
{
|
||||
double number = 0.0;
|
||||
JSBool ok = JS_ValueToNumber(cx, value, &number);
|
||||
if (ok)
|
||||
{
|
||||
ret->push_back(Value(number));
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_BOOLEAN(value))
|
||||
{
|
||||
JSBool boolVal = JS_FALSE;
|
||||
JSBool ok = JS_ValueToBoolean(cx, value, &boolVal);
|
||||
if (ok)
|
||||
{
|
||||
ret->push_back(Value(boolVal));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CCASSERT(false, "not supported type");
|
||||
}
|
||||
// next
|
||||
vp++;
|
||||
}
|
||||
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool jsval_to_ccrect(JSContext *cx, jsval v, Rect* ret) {
|
||||
JSObject *tmp;
|
||||
JS::RootedValue jsx(cx);
|
||||
|
@ -658,7 +723,8 @@ JSBool jsval_to_ccarray_of_CCPoint(JSContext* cx, jsval v, Point **points, int *
|
|||
}
|
||||
|
||||
|
||||
JSBool jsval_to_ccarray(JSContext* cx, jsval v, Array** ret) {
|
||||
JSBool jsval_to_ccarray(JSContext* cx, jsval v, Array** ret)
|
||||
{
|
||||
JSObject *jsobj;
|
||||
JSBool ok = v.isObject() && JS_ValueToObject( cx, v, &jsobj );
|
||||
JSB_PRECONDITION3( ok, cx, JS_FALSE, "Error converting value to object");
|
||||
|
@ -717,7 +783,7 @@ JSBool jsval_to_ccarray(JSContext* cx, jsval v, Array** ret) {
|
|||
JSBool ok = JS_ValueToBoolean(cx, value, &boolVal);
|
||||
if (ok) {
|
||||
arr->addObject(Bool::create(boolVal));
|
||||
// CCLOG("iterate object: value = %d", boolVal);
|
||||
// CCLOG("iterate object: value = %d", boolVal);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -729,6 +795,317 @@ JSBool jsval_to_ccarray(JSContext* cx, jsval v, Array** ret) {
|
|||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool jsval_to_ccvalue(JSContext* cx, jsval v, cocos2d::Value* ret)
|
||||
{
|
||||
if (v.isObject())
|
||||
{
|
||||
JSObject* jsobj = JSVAL_TO_OBJECT(v);
|
||||
CCASSERT(jsb_get_js_proxy(jsobj) == nullptr, "Native object should be added!");
|
||||
if (!JS_IsArrayObject(cx, jsobj))
|
||||
{
|
||||
// It's a normal js object.
|
||||
ValueMap dictVal;
|
||||
JSBool ok = jsval_to_ccvaluemap(cx, v, &dictVal);
|
||||
if (ok)
|
||||
{
|
||||
*ret = Value(dictVal);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// It's a js array object.
|
||||
ValueVector arrVal;
|
||||
JSBool ok = jsval_to_ccvaluevector(cx, v, &arrVal);
|
||||
if (ok)
|
||||
{
|
||||
*ret = Value(arrVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_STRING(v))
|
||||
{
|
||||
JSStringWrapper valueWapper(JSVAL_TO_STRING(v), cx);
|
||||
*ret = Value(valueWapper.get());
|
||||
}
|
||||
else if (JSVAL_IS_NUMBER(v))
|
||||
{
|
||||
double number = 0.0;
|
||||
JSBool ok = JS_ValueToNumber(cx, v, &number);
|
||||
if (ok) {
|
||||
*ret = Value(number);
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_BOOLEAN(v))
|
||||
{
|
||||
JSBool boolVal = JS_FALSE;
|
||||
JSBool ok = JS_ValueToBoolean(cx, v, &boolVal);
|
||||
if (ok) {
|
||||
*ret = Value(boolVal);
|
||||
}
|
||||
}
|
||||
else {
|
||||
CCASSERT(false, "not supported type");
|
||||
}
|
||||
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool jsval_to_ccvaluemap(JSContext* cx, jsval v, cocos2d::ValueMap* ret)
|
||||
{
|
||||
if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v))
|
||||
{
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSObject* tmp = JSVAL_TO_OBJECT(v);
|
||||
if (!tmp) {
|
||||
CCLOG("%s", "jsval_to_ccvaluemap: the jsval is not an object.");
|
||||
return JS_FALSE;
|
||||
}
|
||||
|
||||
JSObject* it = JS_NewPropertyIterator(cx, tmp);
|
||||
|
||||
ValueMap& dict = *ret;
|
||||
|
||||
while (true)
|
||||
{
|
||||
jsid idp;
|
||||
jsval key;
|
||||
if (! JS_NextProperty(cx, it, &idp) || ! JS_IdToValue(cx, idp, &key)) {
|
||||
return JS_FALSE; // error
|
||||
}
|
||||
|
||||
if (key == JSVAL_VOID) {
|
||||
break; // end of iteration
|
||||
}
|
||||
|
||||
if (!JSVAL_IS_STRING(key)) {
|
||||
continue; // ignore integer properties
|
||||
}
|
||||
|
||||
JSStringWrapper keyWrapper(JSVAL_TO_STRING(key), cx);
|
||||
|
||||
JS::RootedValue value(cx);
|
||||
JS_GetPropertyById(cx, tmp, idp, &value);
|
||||
if (value.isObject())
|
||||
{
|
||||
JSObject* jsobj = JSVAL_TO_OBJECT(value);
|
||||
CCASSERT(jsb_get_js_proxy(jsobj) == nullptr, "Native object should be added!");
|
||||
if (!JS_IsArrayObject(cx, jsobj))
|
||||
{
|
||||
// It's a normal js object.
|
||||
ValueMap dictVal;
|
||||
JSBool ok = jsval_to_ccvaluemap(cx, value, &dictVal);
|
||||
if (ok)
|
||||
{
|
||||
dict[keyWrapper.get()] = Value(dictVal);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// It's a js array object.
|
||||
ValueVector arrVal;
|
||||
JSBool ok = jsval_to_ccvaluevector(cx, value, &arrVal);
|
||||
if (ok)
|
||||
{
|
||||
dict[keyWrapper.get()] = Value(arrVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_STRING(value))
|
||||
{
|
||||
JSStringWrapper valueWapper(JSVAL_TO_STRING(value), cx);
|
||||
dict[keyWrapper.get()] = Value(valueWapper.get());
|
||||
// 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) {
|
||||
dict[keyWrapper.get()] = Value(number);
|
||||
// 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) {
|
||||
dict[keyWrapper.get()] = Value(boolVal);
|
||||
// CCLOG("iterate object: key = %s, value = %d", keyWrapper.get().c_str(), boolVal);
|
||||
}
|
||||
}
|
||||
else {
|
||||
CCASSERT(false, "not supported type");
|
||||
}
|
||||
}
|
||||
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool jsval_to_ccintvaluemap(JSContext* cx, jsval v, cocos2d::IntValueMap* ret)
|
||||
{
|
||||
if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v))
|
||||
{
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSObject* tmp = JSVAL_TO_OBJECT(v);
|
||||
if (!tmp) {
|
||||
CCLOG("%s", "jsval_to_ccvaluemap: the jsval is not an object.");
|
||||
return JS_FALSE;
|
||||
}
|
||||
|
||||
JSObject* it = JS_NewPropertyIterator(cx, tmp);
|
||||
|
||||
IntValueMap& dict = *ret;
|
||||
|
||||
while (true)
|
||||
{
|
||||
jsid idp;
|
||||
jsval key;
|
||||
if (! JS_NextProperty(cx, it, &idp) || ! JS_IdToValue(cx, idp, &key)) {
|
||||
return JS_FALSE; // error
|
||||
}
|
||||
|
||||
if (key == JSVAL_VOID) {
|
||||
break; // end of iteration
|
||||
}
|
||||
|
||||
if (!JSVAL_IS_STRING(key)) {
|
||||
continue; // ignore integer properties
|
||||
}
|
||||
|
||||
int keyVal = JSVAL_TO_INT(key);
|
||||
|
||||
JS::RootedValue value(cx);
|
||||
JS_GetPropertyById(cx, tmp, idp, &value);
|
||||
if (value.isObject())
|
||||
{
|
||||
JSObject* jsobj = JSVAL_TO_OBJECT(value);
|
||||
CCASSERT(jsb_get_js_proxy(jsobj) == nullptr, "Native object should be added!");
|
||||
if (!JS_IsArrayObject(cx, jsobj))
|
||||
{
|
||||
// It's a normal js object.
|
||||
ValueMap dictVal;
|
||||
JSBool ok = jsval_to_ccvaluemap(cx, value, &dictVal);
|
||||
if (ok)
|
||||
{
|
||||
dict[keyVal] = Value(dictVal);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// It's a js array object.
|
||||
ValueVector arrVal;
|
||||
JSBool ok = jsval_to_ccvaluevector(cx, value, &arrVal);
|
||||
if (ok)
|
||||
{
|
||||
dict[keyVal] = Value(arrVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_STRING(value))
|
||||
{
|
||||
JSStringWrapper valueWapper(JSVAL_TO_STRING(value), cx);
|
||||
dict[keyVal] = Value(valueWapper.get());
|
||||
}
|
||||
else if (JSVAL_IS_NUMBER(value))
|
||||
{
|
||||
double number = 0.0;
|
||||
JSBool ok = JS_ValueToNumber(cx, value, &number);
|
||||
if (ok) {
|
||||
dict[keyVal] = Value(number);
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_BOOLEAN(value))
|
||||
{
|
||||
JSBool boolVal = JS_FALSE;
|
||||
JSBool ok = JS_ValueToBoolean(cx, value, &boolVal);
|
||||
if (ok) {
|
||||
dict[keyVal] = Value(boolVal);
|
||||
}
|
||||
}
|
||||
else {
|
||||
CCASSERT(false, "not supported type");
|
||||
}
|
||||
}
|
||||
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool jsval_to_ccvaluevector(JSContext* cx, jsval v, cocos2d::ValueVector* ret)
|
||||
{
|
||||
JSObject *jsobj;
|
||||
JSBool ok = v.isObject() && JS_ValueToObject( cx, v, &jsobj );
|
||||
JSB_PRECONDITION3( ok, cx, JS_FALSE, "Error converting value to object");
|
||||
JSB_PRECONDITION3( jsobj && JS_IsArrayObject( cx, jsobj), cx, JS_FALSE, "Object must be an array");
|
||||
|
||||
uint32_t len = 0;
|
||||
JS_GetArrayLength(cx, jsobj, &len);
|
||||
|
||||
for (uint32_t i=0; i < len; i++)
|
||||
{
|
||||
jsval value;
|
||||
if (JS_GetElement(cx, jsobj, i, &value))
|
||||
{
|
||||
if (value.isObject())
|
||||
{
|
||||
JSObject* jsobj = JSVAL_TO_OBJECT(value);
|
||||
CCASSERT(jsb_get_js_proxy(jsobj) == nullptr, "Native object should be added!");
|
||||
|
||||
if (!JS_IsArrayObject(cx, jsobj))
|
||||
{
|
||||
// It's a normal js object.
|
||||
ValueMap dictVal;
|
||||
JSBool ok = jsval_to_ccvaluemap(cx, value, &dictVal);
|
||||
if (ok)
|
||||
{
|
||||
ret->push_back(Value(dictVal));
|
||||
}
|
||||
}
|
||||
else {
|
||||
// It's a js array object.
|
||||
ValueVector arrVal;
|
||||
JSBool ok = jsval_to_ccvaluevector(cx, value, &arrVal);
|
||||
if (ok)
|
||||
{
|
||||
ret->push_back(Value(arrVal));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_STRING(value))
|
||||
{
|
||||
JSStringWrapper valueWapper(JSVAL_TO_STRING(value), cx);
|
||||
ret->push_back(Value(valueWapper.get()));
|
||||
}
|
||||
else if (JSVAL_IS_NUMBER(value))
|
||||
{
|
||||
double number = 0.0;
|
||||
JSBool ok = JS_ValueToNumber(cx, value, &number);
|
||||
if (ok)
|
||||
{
|
||||
ret->push_back(Value(number));
|
||||
}
|
||||
}
|
||||
else if (JSVAL_IS_BOOLEAN(value))
|
||||
{
|
||||
JSBool boolVal = JS_FALSE;
|
||||
JSBool ok = JS_ValueToBoolean(cx, value, &boolVal);
|
||||
if (ok)
|
||||
{
|
||||
ret->push_back(Value(boolVal));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CCASSERT(false, "not supported type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
// native --> jsval
|
||||
|
||||
jsval ccarray_to_jsval(JSContext* cx, Array *arr)
|
||||
{
|
||||
|
@ -872,7 +1249,7 @@ JSBool jsval_to_ccdictionary(JSContext* cx, jsval v, Dictionary** ret)
|
|||
if (value.isObject())
|
||||
{
|
||||
js_proxy_t *proxy;
|
||||
JSObject *tmp = JSVAL_TO_OBJECT(value);
|
||||
tmp = JSVAL_TO_OBJECT(value);
|
||||
proxy = jsb_get_js_proxy(tmp);
|
||||
cocos2d::Object* cobj = (cocos2d::Object *)(proxy ? proxy->ptr : NULL);
|
||||
// Don't test it.
|
||||
|
@ -1467,3 +1844,180 @@ jsval CGPoint_to_jsval( JSContext *cx, cpVect p)
|
|||
return OBJECT_TO_JSVAL(typedArray);
|
||||
#endif // ! JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
|
||||
}
|
||||
|
||||
jsval ccvalue_to_jsval(JSContext* cx, const cocos2d::Value& v)
|
||||
{
|
||||
jsval ret = JSVAL_NULL;
|
||||
const Value& obj = v;
|
||||
|
||||
switch (obj.getType())
|
||||
{
|
||||
case Value::Type::BOOLEAN:
|
||||
ret = BOOLEAN_TO_JSVAL(obj.asBool());
|
||||
break;
|
||||
case Value::Type::FLOAT:
|
||||
case Value::Type::DOUBLE:
|
||||
ret = DOUBLE_TO_JSVAL(obj.asDouble());
|
||||
break;
|
||||
case Value::Type::INTEGER:
|
||||
ret = INT_TO_JSVAL(obj.asInt());
|
||||
break;
|
||||
case Value::Type::STRING:
|
||||
ret = std_string_to_jsval(cx, obj.asString());
|
||||
break;
|
||||
case Value::Type::VECTOR:
|
||||
ret = ccvaluevector_to_jsval(cx, obj.asValueVector());
|
||||
break;
|
||||
case Value::Type::MAP:
|
||||
ret = ccvaluemap_to_jsval(cx, obj.asValueMap());
|
||||
break;
|
||||
case Value::Type::INT_KEY_MAP:
|
||||
ret = ccintvaluemap_to_jsval(cx, obj.asIntKeyMap());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
jsval ccvaluemap_to_jsval(JSContext* cx, const cocos2d::ValueMap& v)
|
||||
{
|
||||
JSObject* jsRet = JS_NewObject(cx, NULL, NULL, NULL);
|
||||
|
||||
for (auto iter = v.begin(); iter != v.end(); ++iter)
|
||||
{
|
||||
JS::RootedValue dictElement(cx);
|
||||
|
||||
std::string key = iter->first;
|
||||
const Value& obj = iter->second;
|
||||
|
||||
switch (obj.getType())
|
||||
{
|
||||
case Value::Type::BOOLEAN:
|
||||
dictElement = BOOLEAN_TO_JSVAL(obj.asBool());
|
||||
break;
|
||||
case Value::Type::FLOAT:
|
||||
case Value::Type::DOUBLE:
|
||||
dictElement = DOUBLE_TO_JSVAL(obj.asDouble());
|
||||
break;
|
||||
case Value::Type::INTEGER:
|
||||
dictElement = INT_TO_JSVAL(obj.asInt());
|
||||
break;
|
||||
case Value::Type::STRING:
|
||||
dictElement = std_string_to_jsval(cx, obj.asString());
|
||||
break;
|
||||
case Value::Type::VECTOR:
|
||||
dictElement = ccvaluevector_to_jsval(cx, obj.asValueVector());
|
||||
break;
|
||||
case Value::Type::MAP:
|
||||
dictElement = ccvaluemap_to_jsval(cx, obj.asValueMap());
|
||||
break;
|
||||
case Value::Type::INT_KEY_MAP:
|
||||
dictElement = ccintvaluemap_to_jsval(cx, obj.asIntKeyMap());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!key.empty())
|
||||
{
|
||||
JS_SetProperty(cx, jsRet, key.c_str(), dictElement);
|
||||
}
|
||||
}
|
||||
return OBJECT_TO_JSVAL(jsRet);
|
||||
}
|
||||
|
||||
jsval ccintvaluemap_to_jsval(JSContext* cx, const cocos2d::IntValueMap& v)
|
||||
{
|
||||
JSObject* jsRet = JS_NewObject(cx, NULL, NULL, NULL);
|
||||
|
||||
for (auto iter = v.begin(); iter != v.end(); ++iter)
|
||||
{
|
||||
JS::RootedValue dictElement(cx);
|
||||
std::stringstream keyss;
|
||||
keyss << iter->first;
|
||||
std::string key = keyss.str();
|
||||
|
||||
const Value& obj = iter->second;
|
||||
|
||||
switch (obj.getType())
|
||||
{
|
||||
case Value::Type::BOOLEAN:
|
||||
dictElement = BOOLEAN_TO_JSVAL(obj.asBool());
|
||||
break;
|
||||
case Value::Type::FLOAT:
|
||||
case Value::Type::DOUBLE:
|
||||
dictElement = DOUBLE_TO_JSVAL(obj.asDouble());
|
||||
break;
|
||||
case Value::Type::INTEGER:
|
||||
dictElement = INT_TO_JSVAL(obj.asInt());
|
||||
break;
|
||||
case Value::Type::STRING:
|
||||
dictElement = std_string_to_jsval(cx, obj.asString());
|
||||
break;
|
||||
case Value::Type::VECTOR:
|
||||
dictElement = ccvaluevector_to_jsval(cx, obj.asValueVector());
|
||||
break;
|
||||
case Value::Type::MAP:
|
||||
dictElement = ccvaluemap_to_jsval(cx, obj.asValueMap());
|
||||
break;
|
||||
case Value::Type::INT_KEY_MAP:
|
||||
dictElement = ccintvaluemap_to_jsval(cx, obj.asIntKeyMap());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!key.empty())
|
||||
{
|
||||
JS_SetProperty(cx, jsRet, key.c_str(), dictElement);
|
||||
}
|
||||
}
|
||||
return OBJECT_TO_JSVAL(jsRet);
|
||||
}
|
||||
|
||||
jsval ccvaluevector_to_jsval(JSContext* cx, const cocos2d::ValueVector& v)
|
||||
{
|
||||
JSObject *jsretArr = JS_NewArrayObject(cx, 0, NULL);
|
||||
|
||||
int i = 0;
|
||||
for (const auto& obj : v)
|
||||
{
|
||||
jsval arrElement;
|
||||
|
||||
switch (obj.getType())
|
||||
{
|
||||
case Value::Type::BOOLEAN:
|
||||
arrElement = BOOLEAN_TO_JSVAL(obj.asBool());
|
||||
break;
|
||||
case Value::Type::FLOAT:
|
||||
case Value::Type::DOUBLE:
|
||||
arrElement = DOUBLE_TO_JSVAL(obj.asDouble());
|
||||
break;
|
||||
case Value::Type::INTEGER:
|
||||
arrElement = INT_TO_JSVAL(obj.asInt());
|
||||
break;
|
||||
case Value::Type::STRING:
|
||||
arrElement = std_string_to_jsval(cx, obj.asString());
|
||||
break;
|
||||
case Value::Type::VECTOR:
|
||||
arrElement = ccvaluevector_to_jsval(cx, obj.asValueVector());
|
||||
break;
|
||||
case Value::Type::MAP:
|
||||
arrElement = ccvaluemap_to_jsval(cx, obj.asValueMap());
|
||||
break;
|
||||
case Value::Type::INT_KEY_MAP:
|
||||
arrElement = ccintvaluemap_to_jsval(cx, obj.asIntKeyMap());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!JS_SetElement(cx, jsretArr, i, &arrElement)) {
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return OBJECT_TO_JSVAL(jsretArr);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "jsapi.h"
|
||||
#include "js_bindings_core.h"
|
||||
#include "cocos2d.h"
|
||||
#include "spidermonkey_specifics.h"
|
||||
|
||||
JSBool jsval_to_opaque( JSContext *cx, jsval vp, void **out );
|
||||
JSBool jsval_to_int( JSContext *cx, jsval vp, int *out);
|
||||
|
@ -44,9 +45,75 @@ JSBool jsval_to_ccarray(JSContext* cx, jsval v, cocos2d::Array** ret);
|
|||
JSBool jsval_to_ccdictionary(JSContext* cx, jsval v, cocos2d::Dictionary** ret);
|
||||
JSBool jsval_to_ccacceleration(JSContext* cx,jsval v, cocos2d::Acceleration* ret);
|
||||
JSBool jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc, cocos2d::Array** ret);
|
||||
|
||||
// forward declaration
|
||||
js_proxy_t* jsb_get_js_proxy(JSObject* jsObj);
|
||||
|
||||
template <class T>
|
||||
JSBool jsvals_variadic_to_ccvector( JSContext *cx, jsval *vp, int argc, cocos2d::Vector<T>* ret)
|
||||
{
|
||||
JSBool ok = JS_TRUE;
|
||||
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
js_proxy_t* p;
|
||||
JSObject* obj = JSVAL_TO_OBJECT(*vp);
|
||||
p = jsb_get_js_proxy(obj);
|
||||
CCASSERT(p, "Native object not found!");
|
||||
if (p) {
|
||||
ret->pushBack((T)p->ptr);
|
||||
}
|
||||
|
||||
// next
|
||||
vp++;
|
||||
}
|
||||
|
||||
JSB_PRECONDITION3(ok, cx, JS_FALSE, "Error processing arguments");
|
||||
return ok;
|
||||
}
|
||||
|
||||
JSBool jsvals_variadic_to_ccvaluevector( JSContext *cx, jsval *vp, int argc, cocos2d::ValueVector* ret);
|
||||
|
||||
JSBool jsval_to_ccaffinetransform(JSContext* cx, jsval v, cocos2d::AffineTransform* ret);
|
||||
JSBool jsval_to_FontDefinition( JSContext *cx, jsval vp, cocos2d::FontDefinition* ret );
|
||||
|
||||
template <class T>
|
||||
JSBool jsval_to_ccvector(JSContext* cx, jsval v, cocos2d::Vector<T>* ret)
|
||||
{
|
||||
JSObject *jsobj;
|
||||
JSBool ok = v.isObject() && JS_ValueToObject( cx, v, &jsobj );
|
||||
JSB_PRECONDITION3( ok, cx, JS_FALSE, "Error converting value to object");
|
||||
JSB_PRECONDITION3( jsobj && JS_IsArrayObject( cx, jsobj), cx, JS_FALSE, "Object must be an array");
|
||||
|
||||
uint32_t len = 0;
|
||||
JS_GetArrayLength(cx, jsobj, &len);
|
||||
|
||||
for (uint32_t i=0; i < len; i++)
|
||||
{
|
||||
jsval value;
|
||||
if (JS_GetElement(cx, jsobj, i, &value))
|
||||
{
|
||||
CCASSERT(value.isObject(), "the element in Vector isn't a native object.");
|
||||
|
||||
js_proxy_t *proxy;
|
||||
JSObject *tmp = JSVAL_TO_OBJECT(value);
|
||||
proxy = jsb_get_js_proxy(tmp);
|
||||
T cobj = (T)(proxy ? proxy->ptr : nullptr);
|
||||
if (cobj)
|
||||
{
|
||||
ret->pushBack(cobj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool jsval_to_ccvalue(JSContext* cx, jsval v, cocos2d::Value* ret);
|
||||
JSBool jsval_to_ccvaluemap(JSContext* cx, jsval v, cocos2d::ValueMap* ret);
|
||||
JSBool jsval_to_ccintvaluemap(JSContext* cx, jsval v, cocos2d::IntValueMap* ret);
|
||||
JSBool jsval_to_ccvaluevector(JSContext* cx, jsval v, cocos2d::ValueVector* ret);
|
||||
|
||||
// from native
|
||||
jsval int32_to_jsval( JSContext *cx, int32_t l);
|
||||
jsval uint32_to_jsval( JSContext *cx, uint32_t number );
|
||||
|
@ -73,5 +140,38 @@ jsval CGPoint_to_jsval( JSContext *cx, cpVect p );
|
|||
#define cpVect_to_jsval CGPoint_to_jsval
|
||||
#define jsval_to_cpVect jsval_to_CGPoint
|
||||
|
||||
|
||||
template<class T>
|
||||
js_proxy_t *js_get_or_create_proxy(JSContext *cx, T *native_obj);
|
||||
|
||||
template <class T>
|
||||
jsval ccvector_to_jsval(JSContext* cx, const cocos2d::Vector<T>& v)
|
||||
{
|
||||
JSObject *jsretArr = JS_NewArrayObject(cx, 0, NULL);
|
||||
|
||||
int i = 0;
|
||||
for (const auto& obj : v)
|
||||
{
|
||||
jsval arrElement;
|
||||
|
||||
//First, check whether object is associated with js object.
|
||||
js_proxy_t* jsproxy = js_get_or_create_proxy<cocos2d::Object>(cx, obj);
|
||||
if (jsproxy) {
|
||||
arrElement = OBJECT_TO_JSVAL(jsproxy->obj);
|
||||
}
|
||||
|
||||
if (!JS_SetElement(cx, jsretArr, i, &arrElement)) {
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return OBJECT_TO_JSVAL(jsretArr);
|
||||
}
|
||||
|
||||
jsval ccvalue_to_jsval(JSContext* cx, const cocos2d::Value& v);
|
||||
jsval ccvaluemap_to_jsval(JSContext* cx, const cocos2d::ValueMap& v);
|
||||
jsval ccintvaluemap_to_jsval(JSContext* cx, const cocos2d::IntValueMap& v);
|
||||
jsval ccvaluevector_to_jsval(JSContext* cx, const cocos2d::ValueVector& v);
|
||||
|
||||
#endif /* __JS_MANUAL_CONVERSIONS_H__ */
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 3e215fc4865a75e25db6f45115350ef2d7a256ca
|
||||
Subproject commit eb2bc8934b01f6cdc7661648f883e2e6374a5cf7
|
|
@ -28,7 +28,7 @@ if [ "$GEN_JSB"x = "YES"x ]; then
|
|||
fi
|
||||
export NDK_ROOT=$HOME/bin/android-ndk
|
||||
cd $COCOS2DX_ROOT/tools/travis-scripts
|
||||
# ./generate-jsbindings.sh
|
||||
./generate-jsbindings.sh
|
||||
elif [ "$PLATFORM"x = "android"x ]; then
|
||||
export NDK_ROOT=$HOME/bin/android-ndk
|
||||
|
||||
|
@ -50,7 +50,7 @@ elif [ "$PLATFORM"x = "android"x ]; then
|
|||
# Build all samples
|
||||
echo "Building all samples ..."
|
||||
cd $COCOS2DX_ROOT/build
|
||||
./android-build.py -n "NDK_BUG=0 -j10" hellocpp testcpp simplegame
|
||||
./android-build.py -n "NDK_BUG=0 -j10" hellocpp testcpp simplegame testjavascript
|
||||
|
||||
# Build template
|
||||
# echo "Building template ..."
|
||||
|
|
Loading…
Reference in New Issue