diff --git a/cocos/scripting/lua-bindings/manual/LuaBasicConversions.cpp b/cocos/scripting/lua-bindings/manual/LuaBasicConversions.cpp index 634baf4e97..2e726a4b83 100644 --- a/cocos/scripting/lua-bindings/manual/LuaBasicConversions.cpp +++ b/cocos/scripting/lua-bindings/manual/LuaBasicConversions.cpp @@ -1184,7 +1184,7 @@ bool luaval_to_array(lua_State* L,int lo, __Array** outValue, const char* funcNa } } } - else if(lua_isstring(L, -1)) + else if(lua_type(L, -1) == LUA_TSTRING) { std::string stringValue = ""; if(luaval_to_std_string(L, -1, &stringValue) ) @@ -1192,7 +1192,7 @@ bool luaval_to_array(lua_State* L,int lo, __Array** outValue, const char* funcNa arr->addObject(String::create(stringValue)); } } - else if(lua_isboolean(L, -1)) + else if(lua_type(L, -1) == LUA_TBOOLEAN) { bool boolVal = false; if (luaval_to_boolean(L, -1, &boolVal)) @@ -1200,7 +1200,7 @@ bool luaval_to_array(lua_State* L,int lo, __Array** outValue, const char* funcNa arr->addObject(Bool::create(boolVal)); } } - else if(lua_isnumber(L, -1)) + else if(lua_type(L, -1) == LUA_TNUMBER) { arr->addObject(Double::create(tolua_tonumber(L, -1, 0))); } @@ -1288,21 +1288,21 @@ bool luaval_to_dictionary(lua_State* L,int lo, __Dictionary** outValue, const ch } } } - else if(lua_isstring(L, -1)) + else if(lua_type(L, -1) == LUA_TSTRING) { if(luaval_to_std_string(L, -1, &stringValue)) { dict->setObject(String::create(stringValue), stringKey); } } - else if(lua_isboolean(L, -1)) + else if(lua_type(L, -1) == LUA_TBOOLEAN) { if (luaval_to_boolean(L, -1, &boolVal)) { dict->setObject(Bool::create(boolVal),stringKey); } } - else if(lua_isnumber(L, -1)) + else if(lua_type(L, -1) == LUA_TNUMBER) { dict->setObject(Double::create(tolua_tonumber(L, -1, 0)),stringKey); } @@ -1389,7 +1389,7 @@ bool luavals_variadic_to_array(lua_State* L,int argc, __Array** ret) for (int i = 0; i < argc; i++) { double num = 0.0; - if (lua_isnumber(L, i + 2)) + if (lua_type(L, i + 2) == LUA_TNUMBER ) { ok &= luaval_to_number(L, i + 2, &num); if (!ok) @@ -1397,7 +1397,7 @@ bool luavals_variadic_to_array(lua_State* L,int argc, __Array** ret) array->addObject(Integer::create((int)num)); } - else if (lua_isstring(L, i + 2)) + else if (lua_type(L, i + 2) == LUA_TSTRING ) { std::string str = lua_tostring(L, i + 2); array->addObject(String::create(str)); @@ -1453,7 +1453,7 @@ bool luavals_variadic_to_ccvaluevector(lua_State* L, int argc, cocos2d::ValueVec } } } - else if(lua_isstring(L, i + 2)) + else if(lua_type(L, i + 2) == LUA_TSTRING ) { std::string stringValue = ""; if(luaval_to_std_string(L, i + 2, &stringValue) ) @@ -1469,7 +1469,7 @@ bool luavals_variadic_to_ccvaluevector(lua_State* L, int argc, cocos2d::ValueVec ret->push_back(Value(boolVal)); } } - else if(lua_isnumber(L, i + 2)) + else if(lua_type(L, i + 2) == LUA_TNUMBER ) { ret->push_back(Value(tolua_tonumber(L, i + 2, 0))); } @@ -1515,7 +1515,7 @@ bool luaval_to_ccvalue(lua_State* L, int lo, cocos2d::Value* ret, const char* fu } } } - else if (tolua_isstring(L, lo, 0, &tolua_err)) + else if ((lua_type(L, lo) == LUA_TSTRING) && tolua_isstring(L, lo, 0, &tolua_err)) { std::string stringValue = ""; if (luaval_to_std_string(L, lo, &stringValue)) @@ -1523,7 +1523,7 @@ bool luaval_to_ccvalue(lua_State* L, int lo, cocos2d::Value* ret, const char* fu *ret = Value(stringValue); } } - else if (tolua_isboolean(L, lo, 0, &tolua_err)) + else if ((lua_type(L, lo) == LUA_TBOOLEAN) && tolua_isboolean(L, lo, 0, &tolua_err)) { bool boolVal = false; if (luaval_to_boolean(L, lo, &boolVal)) @@ -1531,7 +1531,7 @@ bool luaval_to_ccvalue(lua_State* L, int lo, cocos2d::Value* ret, const char* fu *ret = Value(boolVal); } } - else if (tolua_isnumber(L, lo, 0, &tolua_err)) + else if ((lua_type(L, lo) == LUA_TNUMBER) && tolua_isnumber(L, lo, 0, &tolua_err)) { *ret = Value(tolua_tonumber(L, lo, 0)); } @@ -1595,21 +1595,21 @@ bool luaval_to_ccvaluemap(lua_State* L, int lo, cocos2d::ValueMap* ret, const ch } } } - else if(lua_isstring(L, -1)) + else if(lua_type(L, -1) == LUA_TSTRING) { if(luaval_to_std_string(L, -1, &stringValue)) { dict[stringKey] = Value(stringValue); } } - else if(lua_isboolean(L, -1)) + else if(lua_type(L, -1) == LUA_TBOOLEAN) { if (luaval_to_boolean(L, -1, &boolVal)) { dict[stringKey] = Value(boolVal); } } - else if(lua_isnumber(L, -1)) + else if(lua_type(L, -1) == LUA_TNUMBER) { dict[stringKey] = Value(tolua_tonumber(L, -1, 0)); } @@ -1683,21 +1683,21 @@ bool luaval_to_ccvaluemapintkey(lua_State* L, int lo, cocos2d::ValueMapIntKey* r } } } - else if(lua_isstring(L, -1)) + else if(lua_type(L, -1) == LUA_TSTRING) { if(luaval_to_std_string(L, -1, &stringValue)) { dict[intKey] = Value(stringValue); } } - else if(lua_isboolean(L, -1)) + else if(lua_type(L, -1) == LUA_TBOOLEAN) { if (luaval_to_boolean(L, -1, &boolVal)) { dict[intKey] = Value(boolVal); } } - else if(lua_isnumber(L, -1)) + else if(lua_type(L, -1) == LUA_TNUMBER) { dict[intKey] = Value(tolua_tonumber(L, -1, 0)); } @@ -1764,7 +1764,7 @@ bool luaval_to_ccvaluevector(lua_State* L, int lo, cocos2d::ValueVector* ret, co } } } - else if(lua_isstring(L, -1)) + else if(lua_type(L, -1) == LUA_TSTRING) { std::string stringValue = ""; if(luaval_to_std_string(L, -1, &stringValue) ) @@ -1772,7 +1772,7 @@ bool luaval_to_ccvaluevector(lua_State* L, int lo, cocos2d::ValueVector* ret, co ret->push_back(Value(stringValue)); } } - else if(lua_isboolean(L, -1)) + else if(lua_type(L, -1) == LUA_TBOOLEAN) { bool boolVal = false; if (luaval_to_boolean(L, -1, &boolVal)) @@ -1780,7 +1780,7 @@ bool luaval_to_ccvaluevector(lua_State* L, int lo, cocos2d::ValueVector* ret, co ret->push_back(Value(boolVal)); } } - else if(lua_isnumber(L, -1)) + else if(lua_type(L, -1) == LUA_TNUMBER) { ret->push_back(Value(tolua_tonumber(L, -1, 0))); } diff --git a/tests/lua-tests/project/Classes/lua_test_bindings.cpp b/tests/lua-tests/project/Classes/lua_test_bindings.cpp index 723af93e4d..5ac8be6c76 100644 --- a/tests/lua-tests/project/Classes/lua_test_bindings.cpp +++ b/tests/lua-tests/project/Classes/lua_test_bindings.cpp @@ -288,6 +288,49 @@ void DrawNode3D::setBlendFunc(const BlendFunc &blendFunc) { _blendFunc = blendFunc; } + +/** + @since v3.3rc1 + This class is used to check if the the value type judgement in table is correct or not. + eg: + If call `create` by passing {index1 = 111, index2 = 112, index3 = 113} from lua, + the type 111,112,113 would be judged as string type before 3.3rc1 + **/ +class ValueTypeJudgeInTable:public Node +{ +public: + static ValueTypeJudgeInTable* create(ValueMap valueMap); +}; + +ValueTypeJudgeInTable* ValueTypeJudgeInTable::create(ValueMap valueMap) +{ + ValueTypeJudgeInTable* ret = new (std::nothrow) ValueTypeJudgeInTable(); + if (ret) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } + + int index = 0; + for (auto iter : valueMap) + { + Value::Type type = iter.second.getType(); + if (type == Value::Type::STRING) { + CCLOG("The type of index %d is string", index); + } + + if (type == Value::Type::INTEGER || type == Value::Type::DOUBLE || type == Value::Type::FLOAT || type == Value::Type::BYTE) { + CCLOG("The type of index %d is number", index); + } + + ++index; + } + + return ret; +} NS_CC_END int lua_cocos2dx_DrawNode3D_getBlendFunc(lua_State* L) @@ -610,12 +653,61 @@ int lua_register_cocos2dx_DrawNode3D(lua_State* L) return 1; } +int lua_cocos2dx_ValueTypeJudgeInTable_create(lua_State* L) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(L,1,"cc.ValueTypeJudgeInTable",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(L) - 1; + + if (argc == 1) + { + cocos2d::ValueMap arg0; + ok &= luaval_to_ccvaluemap(L, 2, &arg0, "cc.ValueTypeJudgeInTable:create"); + if(!ok) + return 0; + cocos2d::ValueTypeJudgeInTable* ret = cocos2d::ValueTypeJudgeInTable::create(arg0); + object_to_luaval(L, "cc.ValueTypeJudgeInTable",(cocos2d::ValueTypeJudgeInTable*)ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "cc.ValueTypeJudgeInTable:create",argc, 1); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L, "#ferror in function 'lua_cocos2dx_ValueTypeJudgeInTable_create'.",&tolua_err); +#endif + return 0; +} + +int lua_register_cocos2dx_ValueTypeJudgeInTable(lua_State* L) +{ + tolua_usertype(L,"cc.ValueTypeJudgeInTable"); + tolua_cclass(L,"ValueTypeJudgeInTable","cc.ValueTypeJudgeInTable","cc.Node",nullptr); + + tolua_beginmodule(L,"ValueTypeJudgeInTable"); + tolua_function(L,"create", lua_cocos2dx_ValueTypeJudgeInTable_create); + tolua_endmodule(L); + std::string typeName = typeid(cocos2d::ValueTypeJudgeInTable).name(); + g_luaType[typeName] = "cc.ValueTypeJudgeInTable"; + g_typeCast["ValueTypeJudgeInTable"] = "cc.ValueTypeJudgeInTable"; + return 1; +} + int register_test_binding(lua_State* L) { tolua_open(L); tolua_module(L, "cc", 0); tolua_beginmodule(L, "cc"); lua_register_cocos2dx_DrawNode3D(L); + lua_register_cocos2dx_ValueTypeJudgeInTable(L); tolua_endmodule(L); return 0; } diff --git a/tests/lua-tests/src/BugsTest/BugsTest.lua b/tests/lua-tests/src/BugsTest/BugsTest.lua index 8a000ca05a..648697281c 100644 --- a/tests/lua-tests/src/BugsTest/BugsTest.lua +++ b/tests/lua-tests/src/BugsTest/BugsTest.lua @@ -13,7 +13,8 @@ local testNames = { "Bug-899", "Bug-914", "Bug-1159", - "Bug-1174" + "Bug-1174", + "Bug-value-type-judge-in-table" } local function CreateBugsTestBackMenuItem(pLayer) @@ -554,6 +555,24 @@ local function BugTest1174() return pLayer end +--BugTestValueTypeJudgeInTable +local function BugTestValueTypeJudgeInTable() + local layer = cc.Layer:create() + local label = cc.Label:createWithTTF("Value Type Judge Error in the table:number -> string", "fonts/arial.ttf", 24) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(VisibleRect:center().x, VisibleRect:top().y - 40)) + layer:addChild(label) + local outLable = cc.Label:createWithTTF("You should see the following output in the console: \n The type of index 0 is number \n The type of index 1 is number \n The type of index 2 is number", "fonts/arial.ttf", 18) + outLable:setAnchorPoint(cc.p(0.5, 0.5)) + outLable:setPosition(cc.p(VisibleRect:center().x, VisibleRect:top().y - 160)) + layer:addChild(outLable) + + local valueTypeJudge = cc.ValueTypeJudgeInTable:create({index1 = 111, index2 = 112, index3 = 113}) + layer:addChild(valueTypeJudge) + + return layer +end + local CreateBugsTestTable = { BugTest350, BugTest422, @@ -564,6 +583,7 @@ local CreateBugsTestTable = { BugTest914, BugTest1159, BugTest1174, + BugTestValueTypeJudgeInTable, } local function CreateBugsTestScene(nBugNo)