Skip to content

Commit

Permalink
Simplify API and framework linking.
Browse files Browse the repository at this point in the history
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
ammmir committed Jan 31, 2012
1 parent dc2846d commit d0ab089
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Lua.xcodeproj/project.pbxproj
Expand Up @@ -286,6 +286,7 @@
GCC_PREFIX_HEADER = "Lua/Lua-Prefix.pch"; GCC_PREFIX_HEADER = "Lua/Lua-Prefix.pch";
HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\""; HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\"";
INFOPLIST_FILE = "Lua/Lua-Info.plist"; INFOPLIST_FILE = "Lua/Lua-Info.plist";
INSTALL_PATH = "@loader_path/../Frameworks";
LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\""; LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\"";
OTHER_LDFLAGS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src/libluajit.a\""; OTHER_LDFLAGS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src/libluajit.a\"";
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -303,6 +304,7 @@
GCC_PREFIX_HEADER = "Lua/Lua-Prefix.pch"; GCC_PREFIX_HEADER = "Lua/Lua-Prefix.pch";
HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\""; HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\"";
INFOPLIST_FILE = "Lua/Lua-Info.plist"; INFOPLIST_FILE = "Lua/Lua-Info.plist";
INSTALL_PATH = "@loader_path/../Frameworks";
LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\""; LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src\"";
OTHER_LDFLAGS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src/libluajit.a\""; OTHER_LDFLAGS = "\"$(SRCROOT)/../LuaJIT-2.0.0-beta9/src/libluajit.a\"";
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
2 changes: 1 addition & 1 deletion Lua/Lua.h
Expand Up @@ -12,6 +12,6 @@


@interface Lua : NSObject @interface Lua : NSObject


+ (LuaScript *)luaScriptWithContentsOfURL:(NSURL *)aURL; + (LuaScript *)luaScriptWithContentsOfFile:(NSString *)path;


@end @end
8 changes: 6 additions & 2 deletions Lua/Lua.m
Expand Up @@ -10,8 +10,12 @@


@implementation Lua @implementation Lua


+ (LuaScript *)luaScriptWithContentsOfURL:(NSURL *)aURL { + (LuaScript *)luaScriptWithContentsOfFile:(NSString *)path {
return [[LuaScript alloc] initWithURL:aURL]; LuaScript *script = [[LuaScript alloc] init];
script.scriptPath = path;
script.packagePath = [NSString stringWithFormat:@"%@/?.lua", [[NSBundle mainBundle] resourcePath]];

return script;
} }


@end @end
7 changes: 5 additions & 2 deletions Lua/LuaScript.h
Expand Up @@ -9,11 +9,14 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>


@interface LuaScript : NSObject { @interface LuaScript : NSObject {
const char *scriptPath; NSString *scriptPath;
NSString *packagePath;
} }


- (id)initWithURL:(NSURL *)aURL;
- (void)run; - (void)run;
- (id)callFunction:(NSString *)aName withArguments:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; - (id)callFunction:(NSString *)aName withArguments:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;


@property (nonatomic, retain) NSString *scriptPath;
@property (nonatomic, retain) NSString *packagePath;

@end @end
22 changes: 10 additions & 12 deletions Lua/LuaScript.m
Expand Up @@ -14,6 +14,8 @@


@implementation LuaScript @implementation LuaScript


@synthesize scriptPath, packagePath;

lua_State *L; lua_State *L;


- (id)init { - (id)init {
Expand All @@ -22,20 +24,13 @@ - (id)init {


if(L == NULL) { if(L == NULL) {
printf("error initing lua!\n"); printf("error initing lua!\n");
return nil;
} }
} }


return self; return self;
} }


- (id)initWithURL:(NSURL *)aURL {
if(self = [self init]) {
scriptPath = [[aURL absoluteString] cStringUsingEncoding:NSUTF8StringEncoding];
}

return self;
}

- (void)dealloc { - (void)dealloc {
lua_close(L); lua_close(L);
} }
Expand All @@ -47,15 +42,18 @@ - (void)run {
lua_getglobal(L, "package"); lua_getglobal(L, "package");
lua_getfield(L, -1, "path"); // top of the stack lua_getfield(L, -1, "path"); // top of the stack
const char *path = lua_tostring(L, -1); 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_pop(L, 1);
lua_pushstring(L, [newPath cStringUsingEncoding:NSUTF8StringEncoding]); lua_pushstring(L, [newPath cStringUsingEncoding:NSUTF8StringEncoding]);
lua_setfield(L, -2, "path"); lua_setfield(L, -2, "path");
lua_pop(L, 1); lua_pop(L, 1);


if(luaL_dofile(L, scriptPath) == 1) { if(luaL_dofile(L, [scriptPath cStringUsingEncoding:NSUTF8StringEncoding]) == 1) {
NSLog(@"error running lua script: %s", scriptPath); NSLog(@"error running lua script: %@", scriptPath);
} }
} }


Expand Down
6 changes: 4 additions & 2 deletions LuaFrameworkTest/LuaFrameworkTest/AppDelegate.m
Expand Up @@ -13,9 +13,11 @@ @implementation AppDelegate
@synthesize window = _window; @synthesize window = _window;


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { - (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 [script run]; // prime the script to define globals


// call a function with one argument, ignoring return value // call a function with one argument, ignoring return value
Expand Down

0 comments on commit d0ab089

Please sign in to comment.