Permalink
Browse files

Don't allow multiple threads to call callFunction:withArguments:

  • Loading branch information...
1 parent c5fb7a0 commit 723374ad354b58f7bcc79b5c303d4800fca46f26 @ammmir committed Aug 8, 2012
Showing with 38 additions and 36 deletions.
  1. +38 −36 Lua/LuaScript.m
View
@@ -168,45 +168,47 @@ - (id)popObjectFromLua {
}
- (id)callFunction:(NSString *)aName withArguments:(id)firstObj, ... {
- va_list args;
- va_start(args, firstObj);
- int count = 0;
-
- lua_getglobal(L, [aName cStringUsingEncoding:NSUTF8StringEncoding]);
-
- for(id arg = firstObj; arg != nil; arg = va_arg(args, id)) {
- count++;
- [self pushObjectToLua:arg withArrayIndex:-1 withTableKey:nil];
- }
-
- va_end(args);
-
- int err;
-
- if((err = lua_pcall(L, count, 1, 0)) != 0) {
- switch(err) {
- case LUA_ERRRUN:
- NSLog(@"Lua: runtime error");
- break;
-
- case LUA_ERRMEM:
- NSLog(@"Lua: memory allocation error");
- break;
-
- case LUA_ERRERR:
- NSLog(@"Lua: error handler error");
- break;
-
- default:
- NSLog(@"Lua: unknown error");
- return nil;
+ @synchronized(self) {
+ va_list args;
+ va_start(args, firstObj);
+ int count = 0;
+
+ lua_getglobal(L, [aName cStringUsingEncoding:NSUTF8StringEncoding]);
+
+ for(id arg = firstObj; arg != nil; arg = va_arg(args, id)) {
+ count++;
+ [self pushObjectToLua:arg withArrayIndex:-1 withTableKey:nil];
}
- NSLog(@"Lua: %s", lua_tostring(L, -1));
- return nil;
+ va_end(args);
+
+ int err;
+
+ if((err = lua_pcall(L, count, 1, 0)) != 0) {
+ switch(err) {
+ case LUA_ERRRUN:
+ NSLog(@"Lua: runtime error");
+ break;
+
+ case LUA_ERRMEM:
+ NSLog(@"Lua: memory allocation error");
+ break;
+
+ case LUA_ERRERR:
+ NSLog(@"Lua: error handler error");
+ break;
+
+ default:
+ NSLog(@"Lua: unknown error");
+ return nil;
+ }
+
+ NSLog(@"Lua: %s", lua_tostring(L, -1));
+ return nil;
+ }
+
+ return [self popObjectFromLua];
}
-
- return [self popObjectFromLua];
}
@end

0 comments on commit 723374a

Please sign in to comment.