From d0ab0891aa2addbb93f384372830306950cdaf29 Mon Sep 17 00:00:00 2001 From: Amir Malik Date: Tue, 31 Jan 2012 01:09:22 -0800 Subject: [PATCH] Simplify API and framework linking. Instead of NSURL, we now use an NSString to specify the path to the script. By default, set package.path in the Lua script (prior to running it) to be the Resources directory in the main bundle. The framework should be copied to the Frameworks directory in the application bundle. The install path has thus been updated to @loader_path/../Frameworks --- Lua.xcodeproj/project.pbxproj | 2 ++ Lua/Lua.h | 2 +- Lua/Lua.m | 8 +++++-- Lua/LuaScript.h | 7 ++++-- Lua/LuaScript.m | 22 +++++++++---------- .../LuaFrameworkTest/AppDelegate.m | 6 +++-- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Lua.xcodeproj/project.pbxproj b/Lua.xcodeproj/project.pbxproj index 637eb1d..da5f1d5 100644 --- a/Lua.xcodeproj/project.pbxproj +++ b/Lua.xcodeproj/project.pbxproj @@ -286,6 +286,7 @@ GCC_PREFIX_HEADER = "Lua/Lua-Prefix.pch"; HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\""; INFOPLIST_FILE = "Lua/Lua-Info.plist"; + INSTALL_PATH = "@loader_path/../Frameworks"; LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\""; OTHER_LDFLAGS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src/libluajit.a\""; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -303,6 +304,7 @@ GCC_PREFIX_HEADER = "Lua/Lua-Prefix.pch"; HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\""; INFOPLIST_FILE = "Lua/Lua-Info.plist"; + INSTALL_PATH = "@loader_path/../Frameworks"; LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\""; OTHER_LDFLAGS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src/libluajit.a\""; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/Lua/Lua.h b/Lua/Lua.h index 10f46c4..798bc07 100644 --- a/Lua/Lua.h +++ b/Lua/Lua.h @@ -12,6 +12,6 @@ @interface Lua : NSObject -+ (LuaScript *)luaScriptWithContentsOfURL:(NSURL *)aURL; ++ (LuaScript *)luaScriptWithContentsOfFile:(NSString *)path; @end diff --git a/Lua/Lua.m b/Lua/Lua.m index 386d821..3ea89f5 100644 --- a/Lua/Lua.m +++ b/Lua/Lua.m @@ -10,8 +10,12 @@ @implementation Lua -+ (LuaScript *)luaScriptWithContentsOfURL:(NSURL *)aURL { - return [[LuaScript alloc] initWithURL:aURL]; ++ (LuaScript *)luaScriptWithContentsOfFile:(NSString *)path { + LuaScript *script = [[LuaScript alloc] init]; + script.scriptPath = path; + script.packagePath = [NSString stringWithFormat:@"%@/?.lua", [[NSBundle mainBundle] resourcePath]]; + + return script; } @end diff --git a/Lua/LuaScript.h b/Lua/LuaScript.h index e7f9760..98defaa 100644 --- a/Lua/LuaScript.h +++ b/Lua/LuaScript.h @@ -9,11 +9,14 @@ #import @interface LuaScript : NSObject { - const char *scriptPath; + NSString *scriptPath; + NSString *packagePath; } -- (id)initWithURL:(NSURL *)aURL; - (void)run; - (id)callFunction:(NSString *)aName withArguments:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; +@property (nonatomic, retain) NSString *scriptPath; +@property (nonatomic, retain) NSString *packagePath; + @end diff --git a/Lua/LuaScript.m b/Lua/LuaScript.m index e944028..bf7541c 100644 --- a/Lua/LuaScript.m +++ b/Lua/LuaScript.m @@ -14,6 +14,8 @@ @implementation LuaScript +@synthesize scriptPath, packagePath; + lua_State *L; - (id)init { @@ -22,20 +24,13 @@ - (id)init { if(L == NULL) { printf("error initing lua!\n"); + return nil; } } return self; } -- (id)initWithURL:(NSURL *)aURL { - if(self = [self init]) { - scriptPath = [[aURL absoluteString] cStringUsingEncoding:NSUTF8StringEncoding]; - } - - return self; -} - - (void)dealloc { lua_close(L); } @@ -47,15 +42,18 @@ - (void)run { lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); // top of the stack const char *path = lua_tostring(L, -1); - NSMutableString *newPath = [NSMutableString stringWithCString:path encoding:NSUTF8StringEncoding]; - [newPath appendFormat:@";%@", @"/Users/amir/src/lua-playground/?.lua"]; + + //NSMutableString *newPath = [NSMutableString stringWithCString:path encoding:NSUTF8StringEncoding]; + //[newPath appendFormat:@";%@", packagePath]; + NSString *newPath = [NSString stringWithFormat:@"%@;", packagePath]; + lua_pop(L, 1); lua_pushstring(L, [newPath cStringUsingEncoding:NSUTF8StringEncoding]); lua_setfield(L, -2, "path"); lua_pop(L, 1); - if(luaL_dofile(L, scriptPath) == 1) { - NSLog(@"error running lua script: %s", scriptPath); + if(luaL_dofile(L, [scriptPath cStringUsingEncoding:NSUTF8StringEncoding]) == 1) { + NSLog(@"error running lua script: %@", scriptPath); } } diff --git a/LuaFrameworkTest/LuaFrameworkTest/AppDelegate.m b/LuaFrameworkTest/LuaFrameworkTest/AppDelegate.m index 1219903..7a998dd 100644 --- a/LuaFrameworkTest/LuaFrameworkTest/AppDelegate.m +++ b/LuaFrameworkTest/LuaFrameworkTest/AppDelegate.m @@ -13,9 +13,11 @@ @implementation AppDelegate @synthesize window = _window; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { - NSURL *scriptURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"lua"]]; + LuaScript *script = [Lua luaScriptWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"lua"]]; + + // override package.path (default is Resources dir inside the main bundle) + //script.packagePath = @"/foo/bar/?.lua"; - LuaScript *script = [Lua luaScriptWithContentsOfURL:scriptURL]; [script run]; // prime the script to define globals // call a function with one argument, ignoring return value