Hey guys,
Got running with my first plugin. I found this function which will push an arbritarily complex Objective-C object onto the lua stack.
void wax_fromInstance(lua_State *L, id instance) {
//BEGIN_STACK_MODIFY(L)
if (instance) {
if ([instance isKindOfClass:[NSString class]]) {
lua_pushstring(L, [(NSString *)instance UTF8String]);
}
else if ([instance isKindOfClass:[NSNumber class]]) {
lua_pushnumber(L, [instance doubleValue]);
}
else if ([instance isKindOfClass:[NSArray class]]) {
lua_newtable(L);
for (id obj in instance) {
int i = lua_objlen(L, -1);
wax_fromInstance(L, obj);
lua_rawseti(L, -2, i + 1);
}
}
else if ([instance isKindOfClass:[NSDictionary class]]) {
lua_newtable(L);
for (id key in instance) {
wax_fromInstance(L, key);
wax_fromInstance(L, [instance objectForKey:key]);
lua_rawset(L, -3);
}
/*}
else if ([instance isKindOfClass:[NSValue class]]) {
void *buffer = malloc(wax_sizeOfTypeDescription([instance objCType]));
[instance getValue:buffer];
wax_fromObjc(L, [instance objCType], buffer);
free(buffer);
}
else if ([instance isKindOfClass:[WaxFunction class]]) {
wax_instance_pushUserdata(L, instance);
if (lua_isnil(L, -1)) {
luaL_error(L, "Could not get userdata associated with WaxFunction");
}
lua_getfield(L, -1, "function");*/
}
else {
//wax_instance_create(L, instance, NO);
}
}
else {
lua_pushnil(L);
}
// END_STACK_MODIFY(L, 1)
} |
Use this if you have any data you want converted. Handles, NSString, NSNumber, NSArray, NSDictionary.
wax_fromInstance(L, objcobject); |
Hoan
Comments
Likes: alexzheng
My apps: http://www.yummyyellow.com
https://sites.google.com/site/xraystudiogame
It's possible to wrap these C functions and access all Objective-C runtime capabilities form Lua.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps