Ccluaobjcbridge is a "bridge" between cocos2d-x engine and objective-C. laoliu's quick-cocos2d-x is encapsulated in its framework and encapsulated into luaoc class:
luaoc.callStaticMethod = CCLuaObjcBridge.callStaticMethod
The function prototype is as follows:
-- [Call the static method in objective-c @ Param string classname class name @ Param string methodname method name @ Param table ARGs counts] function luaoc. callstaticmethod (classname, methodname, argS) End
Suppose there are the following objective-C static methods:
#import <Foundation/Foundation.h>@interface SdkApi : NSObject+ (void)init:(NSDictionary*)dict;@end
When you try to use the following sequence number call, the error "invalid key to 'Next'" will be reported:
luaoc.callStaticMethod("SdkApi", "init", {"platform", true})
Very strange error. debug it step by step, go to ccluaobjcbridge. Mm, and locate the lua_next line of code:
if (hasArguments) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; lua_pushnil(L); while (lua_next(L, -2)) { NSString *key = [NSString stringWithCString:lua_tostring(L, -2) encoding:NSUTF8StringEncoding]; switch (lua_type(L, -1)) { case LUA_TNUMBER: [dict setObject:[NSNumber numberWithFloat:lua_tonumber(L, -1)] forKey:key]; break; case LUA_TBOOLEAN: [dict setObject:[NSNumber numberWithBool:lua_toboolean(L, -1)] forKey:key]; break; case LUA_TSTRING: [dict setObject:[NSString stringWithCString:lua_tostring(L, -1) encoding:NSUTF8StringEncoding] forKey:key]; break; case LUA_TFUNCTION: int functionId = retainLuaFunction(L, -1, NULL); [dict setObject:[NSNumber numberWithInt:functionId] forKey:key]; break; } lua_pop(L, 1); } [invocation setArgument:&dict atIndex:2]; [invocation invoke]; } else { [invocation invoke]; }
After reading the code roughly, I did not find any exceptions. So I checked the official documents and finally found out:
int lua_next (lua_State *L, int index);Pops a key from the stack, and pushes a key–value pair from the table at the given index (the "next" pair after the given key). If there are no more elements in the table, then lua_next returns 0 (and pushes nothing).A typical traversal looks like this: /* table is in the stack at index ‘t‘ */ lua_pushnil(L); /* first key */ while (lua_next(L, t) != 0) { /* uses ‘key‘ (at index -2) and ‘value‘ (at index -1) */ printf("%s - %s\n", lua_typename(L, lua_type(L, -2)), lua_typename(L, lua_type(L, -1))); /* removes ‘value‘; keeps ‘key‘ for next iteration */ lua_pop(L, 1); }While traversing a table, do not call lua_tolstring directly on a key, unless you know that the key is actually a string. Recall that lua_tolstring may change the value at the given index; this confuses the next call to lua_next.See function next for the caveats of modifying the table during its traversal.
In general, when traversing a table, unless you know that the key is of the string type, do not perform the lua_tolstring operation on the key directly. This is because the lua_tolstring operation may change the value at the specified index, this obfuscated the next call to lua_next.
In short, lua_tolstring may damage the original structure of the table, so do not perform the lua_tolstring operation on the key during traversal.
Lua_tostring finally calls lua_tolstring, so this is the problem.
To avoid this error, you can solve it without passing the index array, for example:
luaoc.callStaticMethod("SdkApi", "init", {platform = "platform", debug = true})
When ccluaobjcbridge calls the objective-C method to pass the index array, the invalid key to & #39; next & #39; error debugging is reported.