-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathwax.m
260 lines (201 loc) · 6.95 KB
/
wax.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// ObjLua.m
// Lua
//
// Created by ProbablyInteractive on 5/27/09.
// Copyright 2009 Probably Interactive. All rights reserved.
//
#import "ProtocolLoader.h"
#import "wax.h"
#import "wax_class.h"
#import "wax_instance.h"
#import "wax_struct.h"
#import "wax_helpers.h"
#import "wax_gc.h"
#import "wax_server.h"
#import "wax_stdlib.h"
#import "lauxlib.h"
#import "lobject.h"
#import "lualib.h"
static void addGlobals(lua_State *L);
static int waxRoot(lua_State *L);
static int waxPrint(lua_State *L);
static int tolua(lua_State *L);
static int toobjc(lua_State *L);
static int exitApp(lua_State *L);
static int objcDebug(lua_State *L);
static lua_State *currentL;
lua_State *wax_currentLuaState() {
if (!currentL)
currentL = lua_open();
return currentL;
}
void uncaughtExceptionHandler(NSException *e) {
NSLog(@"ERROR: Uncaught exception %@", [e description]);
lua_State *L = wax_currentLuaState();
if (L) {
wax_getStackTrace(L);
const char *stackTrace = luaL_checkstring(L, -1);
NSLog(@"%s", stackTrace);
lua_pop(L, -1); // remove the stackTrace
}
}
int wax_panic(lua_State *L) {
printf("Lua panicked and quit: %s\n", luaL_checkstring(L, -1));
exit(EXIT_FAILURE);
}
lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
void wax_setup() {
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager changeCurrentDirectoryPath:[[NSBundle mainBundle] bundlePath]];
lua_State *L = wax_currentLuaState();
lua_atpanic(L, &wax_panic);
luaL_openlibs(L);
luaopen_wax_class(L);
luaopen_wax_instance(L);
luaopen_wax_struct(L);
addGlobals(L);
[wax_gc start];
}
void wax_start(char* initScript, lua_CFunction extensionFunction, ...) {
wax_setup();
lua_State *L = wax_currentLuaState();
// Load extentions
// ---------------
if (extensionFunction) {
extensionFunction(L);
va_list ap;
va_start(ap, extensionFunction);
while((extensionFunction = va_arg(ap, lua_CFunction))) extensionFunction(L);
va_end(ap);
}
// Load stdlib
// ---------------
#ifdef WAX_STDLIB
// If the stdlib was autogenerated and included in the source, load
char stdlib[] = WAX_STDLIB;
size_t stdlibSize = sizeof(stdlib);
#else
char stdlib[] = "require 'wax'";
size_t stdlibSize = strlen(stdlib);
#endif
if (luaL_loadbuffer(L, stdlib, stdlibSize, "loading wax stdlib") || lua_pcall(L, 0, LUA_MULTRET, 0)) {
fprintf(stderr,"Error opening wax scripts: %s\n", lua_tostring(L,-1));
}
// Run Tests or the REPL?
// ----------------------
NSDictionary *env = [[NSProcessInfo processInfo] environment];
if ([[env objectForKey:@"WAX_TEST"] isEqual:@"YES"]) {
printf("Running Tests\n");
if (luaL_dostring(L, "require 'tests'") != 0) {
fprintf(stderr,"Fatal error running tests: %s\n", lua_tostring(L,-1));
}
exit(1);
}
else if ([[env objectForKey:@"WAX_REPL"] isEqual:@"YES"]) {
printf("Starting REPL\n");
if (luaL_dostring(L, "require 'wax.repl'") != 0) {
fprintf(stderr,"Fatal error starting the REPL: %s\n", lua_tostring(L,-1));
}
exit(1);
}
else {
// Load app
char appLoadString[512];
snprintf(appLoadString, sizeof(appLoadString), "local f = '%s' require(f:gsub('%%.[^.]*$', ''))", initScript); // Strip the extension off the file.
if (luaL_dostring(L, appLoadString) != 0) {
fprintf(stderr,"Error opening wax scripts: %s\n", lua_tostring(L,-1));
}
}
}
void wax_startWithServer() {
wax_setup();
[wax_server class]; // You need to load the class somehow via the wax.framework
lua_State *L = wax_currentLuaState();
// Load all the wax lua scripts
if (luaL_dofile(L, WAX_SCRIPTS_DIR "/scripts/wax/init.lua") != 0) {
fprintf(stderr,"Fatal error opening wax scripts: %s\n", lua_tostring(L,-1));
}
Class WaxServer = objc_getClass("WaxServer");
if (!WaxServer) [NSException raise:@"Wax Server Error" format:@"Could load Wax Server"];
[WaxServer start];
}
void wax_end() {
[wax_gc stop];
lua_close(wax_currentLuaState());
currentL = 0;
}
static void addGlobals(lua_State *L) {
lua_getglobal(L, "wax");
if (lua_isnil(L, -1)) {
lua_pop(L, 1); // Get rid of the nil
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "wax");
}
lua_pushnumber(L, WAX_VERSION);
lua_setfield(L, -2, "version");
lua_pushstring(L, [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] UTF8String]);
lua_setfield(L, -2, "appVersion");
lua_pushcfunction(L, waxRoot);
lua_setfield(L, -2, "root");
lua_pushcfunction(L, waxPrint);
lua_setfield(L, -2, "print");
#ifdef DEBUG
lua_pushboolean(L, YES);
lua_setfield(L, -2, "isDebug");
#endif
lua_pop(L, 1); // pop the wax global off
lua_pushcfunction(L, tolua);
lua_setglobal(L, "tolua");
lua_pushcfunction(L, toobjc);
lua_setglobal(L, "toobjc");
lua_pushcfunction(L, exitApp);
lua_setglobal(L, "exitApp");
lua_pushstring(L, [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] UTF8String]);
lua_setglobal(L, "NSDocumentDirectory");
lua_pushstring(L, [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] UTF8String]);
lua_setglobal(L, "NSLibraryDirectory");
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
lua_pushstring(L, [cachePath UTF8String]);
lua_setglobal(L, "NSCacheDirectory");
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes: nil error:&error];
if (error) {
wax_log(LOG_DEBUG, @"Error creating cache path. %@", [error localizedDescription]);
}
}
static int waxPrint(lua_State *L) {
NSLog(@"%s", luaL_checkstring(L, 1));
return 0;
}
static int waxRoot(lua_State *L) {
luaL_Buffer b;
luaL_buffinit(L, &b);
luaL_addstring(&b, WAX_SCRIPTS_DIR);
for (int i = 1; i <= lua_gettop(L); i++) {
luaL_addstring(&b, "/");
luaL_addstring(&b, luaL_checkstring(L, i));
}
luaL_pushresult(&b);
return 1;
}
static int tolua(lua_State *L) {
if (lua_isuserdata(L, 1)) { // If it's not userdata... it's already lua!
wax_instance_userdata *instanceUserdata = (wax_instance_userdata *)luaL_checkudata(L, 1, WAX_INSTANCE_METATABLE_NAME);
wax_fromInstance(L, instanceUserdata->instance);
}
return 1;
}
static int toobjc(lua_State *L) {
id *instancePointer = wax_copyToObjc(L, "@", 1, nil);
id instance = *(id *)instancePointer;
wax_instance_create(L, instance, NO);
if (instancePointer) free(instancePointer);
return 1;
}
static int exitApp(lua_State *L) {
exit(0);
return 0;
}