Хорошо, я не понимаю этого на 100%, но я верю, что это как-то связано с тем, что я вызывал doFileThreaded из другого исполняемого скрипта lua. Казалось бы, хотя lua_State отличается от того факта, что я вызвал luaL_loadfile из функции, которая уже была вызвана Lua, это все равно каким-то образом вызывало «попытку уступить через границу вызова метаметода / C» * *
Мое решение состояло в том, чтобы сохранить имена файлов, которые я хотел запустить как строки, в массив, а затем вызвать lua_newthread в doThread.
- (void) doFileThreaded:(NSString*)filename {
NSString* path = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
//Add to queue
[threads addObject:path];
}
- (void) doThreads {
//Will run through the threads in the order they've been queued.
//Cooperative Multitasking
while ( threads.count > 0 ) {
//If there is no thread start one
if ( threadState == NULL ) {
threadState = lua_newthread(luaState);
const char* file = [[threads objectAtIndex:0] UTF8String];
if ( luaL_loadfile(threadState,file) != 0 ) {
NSLog(@"%s\n", lua_tostring(threadState,-1));
[threads removeObjectAtIndex:0];
threadState = NULL;
return;
}
//Return because we don't want to resume the file right away
//return;
}
//Resume will run a thread until it yeilds execution
switch ( lua_resume(threadState, 0) ) {
case LUA_YIELD:
return;
case 0:
//New Thread
NSLog(@"Thread Returned");
threadState = NULL;
[threads removeObjectAtIndex:0];
break;
case LUA_ERRMEM:
NSLog(@"LUA_ERRMEM");
NSLog(@"%s\n", lua_tostring(threadState,-1));
[threads removeObjectAtIndex:0];
break;
case LUA_ERRERR:
NSLog(@"LUA_ERRERR: error while running the error handler function");
NSLog(@"%s\n", lua_tostring(threadState,-1));
[threads removeObjectAtIndex:0];
break;
case LUA_ERRRUN:
NSLog(@"LUA_ERRRUN: a runtime error.");
NSLog(@"%s\n", lua_tostring(threadState,-1));
[threads removeObjectAtIndex:0];
break;
default:
NSLog(@"Error Executing Threaded Script!");
NSLog(@"%s\n", lua_tostring(threadState,-1));
[threads removeObjectAtIndex:0];
break;
}
}
}
Ух это было настоящей болью для отладки. НО ЭТО РАБОТАЕТ !!!!!