Permalink
Browse files

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
  • Loading branch information...
1 parent dc2846d commit d0ab0891aa2addbb93f384372830306950cdaf29 @ammmir committed Jan 31, 2012
Showing with 28 additions and 19 deletions.
  1. +2 −0 Lua.xcodeproj/project.pbxproj
  2. +1 −1 Lua/Lua.h
  3. +6 −2 Lua/Lua.m
  4. +5 −2 Lua/LuaScript.h
  5. +10 −12 Lua/LuaScript.m
  6. +4 −2 LuaFrameworkTest/LuaFrameworkTest/AppDelegate.m
@@ -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)";
View
@@ -12,6 +12,6 @@
@interface Lua : NSObject
-+ (LuaScript *)luaScriptWithContentsOfURL:(NSURL *)aURL;
++ (LuaScript *)luaScriptWithContentsOfFile:(NSString *)path;
@end
View
@@ -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
View
@@ -9,11 +9,14 @@
#import <Foundation/Foundation.h>
@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
View
@@ -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);
}
}
@@ -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

0 comments on commit d0ab089

Please sign in to comment.