Permalink
Browse files

Add LuaScript property for package.cpath with a sane default.

  • Loading branch information...
ammmir committed Feb 2, 2012
1 parent fb2985c commit 00e33f2a8e1c73bd273625025ca4b0750ff5365b
Showing with 19 additions and 1 deletion.
  1. +1 −0 Lua/Lua.m
  2. +2 −0 Lua/LuaScript.h
  3. +13 −1 Lua/LuaScript.m
  4. +3 −0 LuaFrameworkTest/LuaFrameworkTest/AppDelegate.m
View
@@ -14,6 +14,7 @@ + (LuaScript *)luaScriptWithContentsOfFile:(NSString *)path {
LuaScript *script = [[LuaScript alloc] init];
script.scriptPath = path;
script.packagePath = [NSString stringWithFormat:@"%@/?.lua", [[NSBundle mainBundle] resourcePath]];
+ script.packageCpath = [NSString stringWithFormat:@"%@/?.so", [[NSBundle mainBundle] resourcePath]];
return script;
}
View
@@ -11,12 +11,14 @@
@interface LuaScript : NSObject {
NSString *scriptPath;
NSString *packagePath;
+ NSString *packageCpath;
}
- (void)run;
- (id)callFunction:(NSString *)aName withArguments:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
@property (nonatomic, retain) NSString *scriptPath;
@property (nonatomic, retain) NSString *packagePath;
+@property (nonatomic, retain) NSString *packageCpath;
@end
View
@@ -14,7 +14,7 @@
@implementation LuaScript
-@synthesize scriptPath, packagePath;
+@synthesize scriptPath, packagePath, packageCpath;
lua_State *L;
@@ -52,6 +52,18 @@ - (void)run {
lua_setfield(L, -2, "path");
lua_pop(L, 1);
+ // set package.cpath for native modules
+ lua_getglobal(L, "package");
+ lua_getfield(L, -1, "cpath"); // top of the stack
+ path = lua_tostring(L, -1);
+
+ newPath = [NSString stringWithFormat:@"%@;", packagePath];
+
+ lua_pop(L, 1);
+ lua_pushstring(L, [newPath cStringUsingEncoding:NSUTF8StringEncoding]);
+ lua_setfield(L, -2, "cpath");
+ lua_pop(L, 1);
+
if(luaL_dofile(L, [scriptPath cStringUsingEncoding:NSUTF8StringEncoding]) == 1) {
NSLog(@"error running lua script: %@", scriptPath);
}
@@ -18,6 +18,9 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// override package.path (default is Resources dir inside the main bundle)
//script.packagePath = @"/foo/bar/?.lua";
+ // override package.cpath for native C modules (default is Resources dir inside the main bundle)
+ //script.packageCpath = @"/foo/bar/?.so";
+
[script run]; // prime the script to define globals
// call a function with one argument, ignoring return value

0 comments on commit 00e33f2

Please sign in to comment.