candle 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.md +4 -0
  3. data/VERSION +1 -0
  4. data/bin/candle +21 -0
  5. data/lib/candle.rb +68 -0
  6. data/lib/candle/command.rb +27 -0
  7. data/lib/candle/generators/actions.rb +183 -0
  8. data/lib/candle/generators/blank.rb +127 -0
  9. data/lib/candle/generators/cli.rb +55 -0
  10. data/lib/candle/generators/help.rb +47 -0
  11. data/lib/candle/generators/jam.rb +80 -0
  12. data/lib/candle/generators/lua/scripts/AppDelegate.lua +36 -0
  13. data/lib/candle/generators/lua/scripts/tests/init.lua +6 -0
  14. data/lib/candle/generators/lua/scripts/tests/someTest.lua +12 -0
  15. data/lib/candle/generators/lua/wax/ProtocolLoader.h +12 -0
  16. data/lib/candle/generators/lua/wax/bin/hammer +157 -0
  17. data/lib/candle/generators/lua/wax/bin/update-xibs +131 -0
  18. data/lib/candle/generators/lua/wax/bin/waxsim +0 -0
  19. data/lib/candle/generators/lua/wax/lib/build-scripts/compile-stdlib.sh +14 -0
  20. data/lib/candle/generators/lua/wax/lib/build-scripts/copy-scripts.sh +58 -0
  21. data/lib/candle/generators/lua/wax/lib/build-scripts/luac.lua +80 -0
  22. data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.h +12 -0
  23. data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.m +85 -0
  24. data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.h +12 -0
  25. data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.m +251 -0
  26. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.h +14 -0
  27. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.m +240 -0
  28. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.h +54 -0
  29. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.m +304 -0
  30. data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.h +9 -0
  31. data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.m +273 -0
  32. data/lib/candle/generators/lua/wax/lib/extensions/json/Rakefile +10 -0
  33. data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.c +304 -0
  34. data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.h +11 -0
  35. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl-1.0.9.tar.gz +0 -0
  36. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_common.h +85 -0
  37. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_gen.h +159 -0
  38. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_parse.h +193 -0
  39. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl.c +159 -0
  40. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.c +65 -0
  41. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.h +50 -0
  42. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.c +119 -0
  43. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.h +73 -0
  44. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_bytestack.h +85 -0
  45. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_common.h +85 -0
  46. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.c +188 -0
  47. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.h +50 -0
  48. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.c +322 -0
  49. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.h +159 -0
  50. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.c +737 -0
  51. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.h +133 -0
  52. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parse.h +193 -0
  53. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.c +448 -0
  54. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.h +82 -0
  55. data/lib/candle/generators/lua/wax/lib/lua/lapi.c +1087 -0
  56. data/lib/candle/generators/lua/wax/lib/lua/lapi.h +16 -0
  57. data/lib/candle/generators/lua/wax/lib/lua/lauxlib.c +652 -0
  58. data/lib/candle/generators/lua/wax/lib/lua/lauxlib.h +174 -0
  59. data/lib/candle/generators/lua/wax/lib/lua/lbaselib.c +653 -0
  60. data/lib/candle/generators/lua/wax/lib/lua/lcode.c +839 -0
  61. data/lib/candle/generators/lua/wax/lib/lua/lcode.h +76 -0
  62. data/lib/candle/generators/lua/wax/lib/lua/ldblib.c +397 -0
  63. data/lib/candle/generators/lua/wax/lib/lua/ldebug.c +638 -0
  64. data/lib/candle/generators/lua/wax/lib/lua/ldebug.h +33 -0
  65. data/lib/candle/generators/lua/wax/lib/lua/ldo.c +518 -0
  66. data/lib/candle/generators/lua/wax/lib/lua/ldo.h +57 -0
  67. data/lib/candle/generators/lua/wax/lib/lua/ldump.c +164 -0
  68. data/lib/candle/generators/lua/wax/lib/lua/lfunc.c +174 -0
  69. data/lib/candle/generators/lua/wax/lib/lua/lfunc.h +34 -0
  70. data/lib/candle/generators/lua/wax/lib/lua/lgc.c +711 -0
  71. data/lib/candle/generators/lua/wax/lib/lua/lgc.h +110 -0
  72. data/lib/candle/generators/lua/wax/lib/lua/linit.c +38 -0
  73. data/lib/candle/generators/lua/wax/lib/lua/liolib.c +553 -0
  74. data/lib/candle/generators/lua/wax/lib/lua/llex.c +461 -0
  75. data/lib/candle/generators/lua/wax/lib/lua/llex.h +81 -0
  76. data/lib/candle/generators/lua/wax/lib/lua/llimits.h +128 -0
  77. data/lib/candle/generators/lua/wax/lib/lua/lmathlib.c +263 -0
  78. data/lib/candle/generators/lua/wax/lib/lua/lmem.c +86 -0
  79. data/lib/candle/generators/lua/wax/lib/lua/lmem.h +49 -0
  80. data/lib/candle/generators/lua/wax/lib/lua/loadlib.c +666 -0
  81. data/lib/candle/generators/lua/wax/lib/lua/lobject.c +214 -0
  82. data/lib/candle/generators/lua/wax/lib/lua/lobject.h +381 -0
  83. data/lib/candle/generators/lua/wax/lib/lua/lopcodes.c +102 -0
  84. data/lib/candle/generators/lua/wax/lib/lua/lopcodes.h +268 -0
  85. data/lib/candle/generators/lua/wax/lib/lua/loslib.c +243 -0
  86. data/lib/candle/generators/lua/wax/lib/lua/lparser.c +1339 -0
  87. data/lib/candle/generators/lua/wax/lib/lua/lparser.h +82 -0
  88. data/lib/candle/generators/lua/wax/lib/lua/lstate.c +214 -0
  89. data/lib/candle/generators/lua/wax/lib/lua/lstate.h +169 -0
  90. data/lib/candle/generators/lua/wax/lib/lua/lstring.c +111 -0
  91. data/lib/candle/generators/lua/wax/lib/lua/lstring.h +31 -0
  92. data/lib/candle/generators/lua/wax/lib/lua/lstrlib.c +869 -0
  93. data/lib/candle/generators/lua/wax/lib/lua/ltable.c +588 -0
  94. data/lib/candle/generators/lua/wax/lib/lua/ltable.h +40 -0
  95. data/lib/candle/generators/lua/wax/lib/lua/ltablib.c +287 -0
  96. data/lib/candle/generators/lua/wax/lib/lua/ltm.c +75 -0
  97. data/lib/candle/generators/lua/wax/lib/lua/ltm.h +54 -0
  98. data/lib/candle/generators/lua/wax/lib/lua/lua.h +388 -0
  99. data/lib/candle/generators/lua/wax/lib/lua/luaconf.h +753 -0
  100. data/lib/candle/generators/lua/wax/lib/lua/lualib.h +53 -0
  101. data/lib/candle/generators/lua/wax/lib/lua/lundump.c +227 -0
  102. data/lib/candle/generators/lua/wax/lib/lua/lundump.h +36 -0
  103. data/lib/candle/generators/lua/wax/lib/lua/lvm.c +763 -0
  104. data/lib/candle/generators/lua/wax/lib/lua/lvm.h +36 -0
  105. data/lib/candle/generators/lua/wax/lib/lua/lzio.c +82 -0
  106. data/lib/candle/generators/lua/wax/lib/lua/lzio.h +67 -0
  107. data/lib/candle/generators/lua/wax/lib/lua/print.c +227 -0
  108. data/lib/candle/generators/lua/wax/lib/project.rake +159 -0
  109. data/lib/candle/generators/lua/wax/lib/stdlib/enums.lua +396 -0
  110. data/lib/candle/generators/lua/wax/lib/stdlib/ext/http.lua +43 -0
  111. data/lib/candle/generators/lua/wax/lib/stdlib/ext/init.lua +4 -0
  112. data/lib/candle/generators/lua/wax/lib/stdlib/ext/number.lua +21 -0
  113. data/lib/candle/generators/lua/wax/lib/stdlib/ext/string.lua +97 -0
  114. data/lib/candle/generators/lua/wax/lib/stdlib/ext/table.lua +165 -0
  115. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/WaxServer.lua +49 -0
  116. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/autoload.lua +10 -0
  117. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/base64.lua +64 -0
  118. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/bit.lua +274 -0
  119. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/cache.lua +73 -0
  120. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/callback.lua +22 -0
  121. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/frame.lua +76 -0
  122. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/init.lua +78 -0
  123. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/pickView.lua +54 -0
  124. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/time.lua +102 -0
  125. data/lib/candle/generators/lua/wax/lib/stdlib/init.lua +18 -0
  126. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/init.lua +2 -0
  127. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luamock.lua +84 -0
  128. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luaspec.lua +377 -0
  129. data/lib/candle/generators/lua/wax/lib/stdlib/repl.lua +9 -0
  130. data/lib/candle/generators/lua/wax/lib/stdlib/structs.lua +11 -0
  131. data/lib/candle/generators/lua/wax/lib/stdlib/waxClass.lua +42 -0
  132. data/lib/candle/generators/lua/wax/lib/wax.h +16 -0
  133. data/lib/candle/generators/lua/wax/lib/wax.m +260 -0
  134. data/lib/candle/generators/lua/wax/lib/wax_class.h +18 -0
  135. data/lib/candle/generators/lua/wax/lib/wax_class.m +190 -0
  136. data/lib/candle/generators/lua/wax/lib/wax_gc.h +20 -0
  137. data/lib/candle/generators/lua/wax/lib/wax_gc.m +56 -0
  138. data/lib/candle/generators/lua/wax/lib/wax_helpers.h +102 -0
  139. data/lib/candle/generators/lua/wax/lib/wax_helpers.m +870 -0
  140. data/lib/candle/generators/lua/wax/lib/wax_instance.h +34 -0
  141. data/lib/candle/generators/lua/wax/lib/wax_instance.m +810 -0
  142. data/lib/candle/generators/lua/wax/lib/wax_server.h +47 -0
  143. data/lib/candle/generators/lua/wax/lib/wax_server.m +252 -0
  144. data/lib/candle/generators/lua/wax/lib/wax_stdlib.h +3 -0
  145. data/lib/candle/generators/lua/wax/lib/wax_struct.h +26 -0
  146. data/lib/candle/generators/lua/wax/lib/wax_struct.m +335 -0
  147. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.pbxproj +836 -0
  148. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  149. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/xcuserdata/eiffel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  150. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/WaxApplication.xcscheme +86 -0
  151. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/xcschememanagement.plist +22 -0
  152. data/lib/candle/generators/templates/blank/WaxApplication/Default-568h@2x.png +0 -0
  153. data/lib/candle/generators/templates/blank/WaxApplication/Default.png +0 -0
  154. data/lib/candle/generators/templates/blank/WaxApplication/Default@2x.png +0 -0
  155. data/lib/candle/generators/templates/blank/WaxApplication/ProtocolLoader.h +12 -0
  156. data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Info.plist.tt +38 -0
  157. data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Prefix.pch.tt +14 -0
  158. data/lib/candle/generators/templates/blank/WaxApplication/en.lproj/InfoPlist.strings +2 -0
  159. data/lib/candle/generators/templates/blank/WaxApplication/main.m.tt +20 -0
  160. data/lib/candle/tasks.rb +22 -0
  161. data/lib/candle/utility.rb +30 -0
  162. data/lib/candle/version.rb +9 -0
  163. data/lib/candle/view.rb +48 -0
  164. metadata +582 -0
@@ -0,0 +1,260 @@
1
+ //
2
+ // ObjLua.m
3
+ // Lua
4
+ //
5
+ // Created by ProbablyInteractive on 5/27/09.
6
+ // Copyright 2009 Probably Interactive. All rights reserved.
7
+ //
8
+
9
+ #import "ProtocolLoader.h"
10
+
11
+ #import "wax.h"
12
+ #import "wax_class.h"
13
+ #import "wax_instance.h"
14
+ #import "wax_struct.h"
15
+ #import "wax_helpers.h"
16
+ #import "wax_gc.h"
17
+ #import "wax_server.h"
18
+ #import "wax_stdlib.h"
19
+
20
+ #import "lauxlib.h"
21
+ #import "lobject.h"
22
+ #import "lualib.h"
23
+
24
+ static void addGlobals(lua_State *L);
25
+ static int waxRoot(lua_State *L);
26
+ static int waxPrint(lua_State *L);
27
+ static int tolua(lua_State *L);
28
+ static int toobjc(lua_State *L);
29
+ static int exitApp(lua_State *L);
30
+ static int objcDebug(lua_State *L);
31
+
32
+ static lua_State *currentL;
33
+ lua_State *wax_currentLuaState() {
34
+
35
+ if (!currentL)
36
+ currentL = lua_open();
37
+
38
+ return currentL;
39
+ }
40
+
41
+ void uncaughtExceptionHandler(NSException *e) {
42
+ NSLog(@"ERROR: Uncaught exception %@", [e description]);
43
+ lua_State *L = wax_currentLuaState();
44
+
45
+ if (L) {
46
+ wax_getStackTrace(L);
47
+ const char *stackTrace = luaL_checkstring(L, -1);
48
+ NSLog(@"%s", stackTrace);
49
+ lua_pop(L, -1); // remove the stackTrace
50
+ }
51
+ }
52
+
53
+ int wax_panic(lua_State *L) {
54
+ printf("Lua panicked and quit: %s\n", luaL_checkstring(L, -1));
55
+ exit(EXIT_FAILURE);
56
+ }
57
+
58
+ lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
59
+
60
+ void wax_setup() {
61
+ NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
62
+
63
+ NSFileManager *fileManager = [NSFileManager defaultManager];
64
+ [fileManager changeCurrentDirectoryPath:[[NSBundle mainBundle] bundlePath]];
65
+
66
+ lua_State *L = wax_currentLuaState();
67
+ lua_atpanic(L, &wax_panic);
68
+
69
+ luaL_openlibs(L);
70
+
71
+ luaopen_wax_class(L);
72
+ luaopen_wax_instance(L);
73
+ luaopen_wax_struct(L);
74
+
75
+ addGlobals(L);
76
+
77
+ [wax_gc start];
78
+ }
79
+
80
+ void wax_start(char* initScript, lua_CFunction extensionFunction, ...) {
81
+ wax_setup();
82
+
83
+ lua_State *L = wax_currentLuaState();
84
+
85
+ // Load extentions
86
+ // ---------------
87
+ if (extensionFunction) {
88
+ extensionFunction(L);
89
+
90
+ va_list ap;
91
+ va_start(ap, extensionFunction);
92
+ while((extensionFunction = va_arg(ap, lua_CFunction))) extensionFunction(L);
93
+
94
+ va_end(ap);
95
+ }
96
+
97
+ // Load stdlib
98
+ // ---------------
99
+ #ifdef WAX_STDLIB
100
+ // If the stdlib was autogenerated and included in the source, load
101
+ char stdlib[] = WAX_STDLIB;
102
+ size_t stdlibSize = sizeof(stdlib);
103
+ #else
104
+ char stdlib[] = "require 'wax'";
105
+ size_t stdlibSize = strlen(stdlib);
106
+ #endif
107
+
108
+ if (luaL_loadbuffer(L, stdlib, stdlibSize, "loading wax stdlib") || lua_pcall(L, 0, LUA_MULTRET, 0)) {
109
+ fprintf(stderr,"Error opening wax scripts: %s\n", lua_tostring(L,-1));
110
+ }
111
+
112
+ // Run Tests or the REPL?
113
+ // ----------------------
114
+ NSDictionary *env = [[NSProcessInfo processInfo] environment];
115
+ if ([[env objectForKey:@"WAX_TEST"] isEqual:@"YES"]) {
116
+ printf("Running Tests\n");
117
+ if (luaL_dostring(L, "require 'tests'") != 0) {
118
+ fprintf(stderr,"Fatal error running tests: %s\n", lua_tostring(L,-1));
119
+ }
120
+ exit(1);
121
+ }
122
+ else if ([[env objectForKey:@"WAX_REPL"] isEqual:@"YES"]) {
123
+ printf("Starting REPL\n");
124
+ if (luaL_dostring(L, "require 'wax.repl'") != 0) {
125
+ fprintf(stderr,"Fatal error starting the REPL: %s\n", lua_tostring(L,-1));
126
+ }
127
+ exit(1);
128
+ }
129
+ else {
130
+ // Load app
131
+ char appLoadString[512];
132
+ snprintf(appLoadString, sizeof(appLoadString), "local f = '%s' require(f:gsub('%%.[^.]*$', ''))", initScript); // Strip the extension off the file.
133
+ if (luaL_dostring(L, appLoadString) != 0) {
134
+ fprintf(stderr,"Error opening wax scripts: %s\n", lua_tostring(L,-1));
135
+ }
136
+ }
137
+
138
+ }
139
+
140
+ void wax_startWithServer() {
141
+ wax_setup();
142
+ [wax_server class]; // You need to load the class somehow via the wax.framework
143
+ lua_State *L = wax_currentLuaState();
144
+
145
+ // Load all the wax lua scripts
146
+ if (luaL_dofile(L, WAX_SCRIPTS_DIR "/scripts/wax/init.lua") != 0) {
147
+ fprintf(stderr,"Fatal error opening wax scripts: %s\n", lua_tostring(L,-1));
148
+ }
149
+
150
+ Class WaxServer = objc_getClass("WaxServer");
151
+ if (!WaxServer) [NSException raise:@"Wax Server Error" format:@"Could load Wax Server"];
152
+
153
+ [WaxServer start];
154
+ }
155
+
156
+ void wax_end() {
157
+ [wax_gc stop];
158
+ lua_close(wax_currentLuaState());
159
+ currentL = 0;
160
+ }
161
+
162
+ static void addGlobals(lua_State *L) {
163
+ lua_getglobal(L, "wax");
164
+ if (lua_isnil(L, -1)) {
165
+ lua_pop(L, 1); // Get rid of the nil
166
+ lua_newtable(L);
167
+ lua_pushvalue(L, -1);
168
+ lua_setglobal(L, "wax");
169
+ }
170
+
171
+ lua_pushnumber(L, WAX_VERSION);
172
+ lua_setfield(L, -2, "version");
173
+
174
+ lua_pushstring(L, [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] UTF8String]);
175
+ lua_setfield(L, -2, "appVersion");
176
+
177
+ lua_pushcfunction(L, waxRoot);
178
+ lua_setfield(L, -2, "root");
179
+
180
+ lua_pushcfunction(L, waxPrint);
181
+ lua_setfield(L, -2, "print");
182
+
183
+ #ifdef DEBUG
184
+ lua_pushboolean(L, YES);
185
+ lua_setfield(L, -2, "isDebug");
186
+ #endif
187
+
188
+ lua_pop(L, 1); // pop the wax global off
189
+
190
+
191
+ lua_pushcfunction(L, tolua);
192
+ lua_setglobal(L, "tolua");
193
+
194
+ lua_pushcfunction(L, toobjc);
195
+ lua_setglobal(L, "toobjc");
196
+
197
+ lua_pushcfunction(L, exitApp);
198
+ lua_setglobal(L, "exitApp");
199
+
200
+ lua_pushstring(L, [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] UTF8String]);
201
+ lua_setglobal(L, "NSDocumentDirectory");
202
+
203
+ lua_pushstring(L, [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] UTF8String]);
204
+ lua_setglobal(L, "NSLibraryDirectory");
205
+
206
+ NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
207
+ lua_pushstring(L, [cachePath UTF8String]);
208
+ lua_setglobal(L, "NSCacheDirectory");
209
+
210
+ NSError *error = nil;
211
+ [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes: nil error:&error];
212
+ if (error) {
213
+ wax_log(LOG_DEBUG, @"Error creating cache path. %@", [error localizedDescription]);
214
+ }
215
+ }
216
+
217
+ static int waxPrint(lua_State *L) {
218
+ NSLog(@"%s", luaL_checkstring(L, 1));
219
+ return 0;
220
+ }
221
+
222
+ static int waxRoot(lua_State *L) {
223
+ luaL_Buffer b;
224
+ luaL_buffinit(L, &b);
225
+ luaL_addstring(&b, WAX_SCRIPTS_DIR);
226
+
227
+ for (int i = 1; i <= lua_gettop(L); i++) {
228
+ luaL_addstring(&b, "/");
229
+ luaL_addstring(&b, luaL_checkstring(L, i));
230
+ }
231
+
232
+ luaL_pushresult(&b);
233
+
234
+ return 1;
235
+ }
236
+
237
+ static int tolua(lua_State *L) {
238
+ if (lua_isuserdata(L, 1)) { // If it's not userdata... it's already lua!
239
+ wax_instance_userdata *instanceUserdata = (wax_instance_userdata *)luaL_checkudata(L, 1, WAX_INSTANCE_METATABLE_NAME);
240
+ wax_fromInstance(L, instanceUserdata->instance);
241
+ }
242
+
243
+ return 1;
244
+ }
245
+
246
+ static int toobjc(lua_State *L) {
247
+ id *instancePointer = wax_copyToObjc(L, "@", 1, nil);
248
+ id instance = *(id *)instancePointer;
249
+
250
+ wax_instance_create(L, instance, NO);
251
+
252
+ if (instancePointer) free(instancePointer);
253
+
254
+ return 1;
255
+ }
256
+
257
+ static int exitApp(lua_State *L) {
258
+ exit(0);
259
+ return 0;
260
+ }
@@ -0,0 +1,18 @@
1
+ //
2
+ // wax_class.h
3
+ // Lua
4
+ //
5
+ // Created by ProbablyInteractive on 5/20/09.
6
+ // Copyright 2009 Probably Interactive. All rights reserved.
7
+ //
8
+
9
+ #import <Foundation/Foundation.h>
10
+ #import <objc/runtime.h>
11
+ #import <objc/message.h>
12
+
13
+ #import "lua.h"
14
+
15
+ #define WAX_CLASS_METATABLE_NAME "wax.class"
16
+ #define WAX_CLASS_INSTANCE_USERDATA_IVAR_NAME "wax_instance_userdata"
17
+
18
+ int luaopen_wax_class(lua_State *L);
@@ -0,0 +1,190 @@
1
+ //
2
+ // wax_class.m
3
+ // Lua
4
+ //
5
+ // Created by ProbablyInteractive on 5/20/09.
6
+ // Copyright 2009 Probably Interactive. All rights reserved.
7
+ //
8
+
9
+ #import "wax_class.h"
10
+
11
+ #import "wax.h"
12
+ #import "wax_instance.h"
13
+ #import "wax_helpers.h"
14
+
15
+ #import "lua.h"
16
+ #import "lauxlib.h"
17
+
18
+ static int __index(lua_State *L);
19
+ static int __call(lua_State *L);
20
+
21
+ static int addProtocols(lua_State *L);
22
+ static int name(lua_State *L);
23
+ static id alloc(id self, SEL _cmd);
24
+ static id allocWithZone(id self, SEL _cmd, NSZone *);
25
+ static id valueForUndefinedKey(id self, SEL cmd, NSString *key);
26
+ static void setValueForUndefinedKey(id self, SEL cmd, id value, NSString *key);
27
+
28
+ static const struct luaL_Reg MetaMethods[] = {
29
+ {"__index", __index},
30
+ {"__call", __call},
31
+ {NULL, NULL}
32
+ };
33
+
34
+ static const struct luaL_Reg Methods[] = {
35
+ {"addProtocols", addProtocols},
36
+ {"name", name},
37
+ {NULL, NULL}
38
+ };
39
+
40
+ int luaopen_wax_class(lua_State *L) {
41
+ BEGIN_STACK_MODIFY(L);
42
+
43
+ luaL_newmetatable(L, WAX_CLASS_METATABLE_NAME);
44
+ luaL_register(L, NULL, MetaMethods);
45
+ luaL_register(L, WAX_CLASS_METATABLE_NAME, Methods);
46
+
47
+ // Set the metatable for the module
48
+ luaL_getmetatable(L, WAX_CLASS_METATABLE_NAME);
49
+ lua_setmetatable(L, -2);
50
+
51
+ END_STACK_MODIFY(L, 0)
52
+
53
+ return 1;
54
+ }
55
+
56
+
57
+ // Finds an ObjC class
58
+ static int __index(lua_State *L) {
59
+ const char *className = luaL_checkstring(L, 2);
60
+ Class klass = objc_getClass(className);
61
+ if (klass) {
62
+ wax_instance_create(L, klass, YES);
63
+ }
64
+ else {
65
+ lua_pushnil(L);
66
+ }
67
+
68
+ return 1;
69
+ }
70
+
71
+ // Creates a new ObjC class
72
+ static int __call(lua_State *L) {
73
+ const char *className = luaL_checkstring(L, 2);
74
+ Class klass = objc_getClass(className);
75
+
76
+ if (!klass) {
77
+ Class superClass;
78
+ if (lua_isuserdata(L, 3)) {
79
+ wax_instance_userdata *instanceUserdata = (wax_instance_userdata *)luaL_checkudata(L, 3, WAX_INSTANCE_METATABLE_NAME);
80
+ superClass = instanceUserdata->instance;
81
+ }
82
+ else if (lua_isnoneornil(L, 3)) {
83
+ superClass = [NSObject class];
84
+ }
85
+ else {
86
+ const char *superClassName = luaL_checkstring(L, 3);
87
+ superClass = objc_getClass(superClassName);
88
+ }
89
+
90
+ if (!superClass) {
91
+ luaL_error(L, "Failed to create '%s'. Unknown superclass \"%s\" received.", className, luaL_checkstring(L, 3));
92
+ }
93
+
94
+ klass = objc_allocateClassPair(superClass, className, 0);
95
+ NSUInteger size;
96
+ NSUInteger alignment;
97
+ NSGetSizeAndAlignment("*", &size, &alignment);
98
+ class_addIvar(klass, WAX_CLASS_INSTANCE_USERDATA_IVAR_NAME, size, alignment, "*"); // Holds a reference to the lua userdata
99
+ objc_registerClassPair(klass);
100
+
101
+ // Make Key-Value complient
102
+ class_addMethod(klass, @selector(setValue:forUndefinedKey:), (IMP)setValueForUndefinedKey, "v@:@@");
103
+ class_addMethod(klass, @selector(valueForUndefinedKey:), (IMP)valueForUndefinedKey, "@@:@");
104
+
105
+ id metaclass = object_getClass(klass);
106
+
107
+ // So objects created in ObjC will get an associated lua object
108
+ // Store the original allocWithZone implementation in case something secret goes on in there.
109
+ // Calls to `alloc` always are end up calling `allocWithZone:` so we don't bother handling alloc here.
110
+ Method m = class_getInstanceMethod(metaclass, @selector(allocWithZone:));
111
+
112
+ // If we the method has already been swizzled (by the class's super, then
113
+ // just leave it up to the super!
114
+ if (method_getImplementation(m) != (IMP)allocWithZone) {
115
+ class_addMethod(metaclass, @selector(wax_originalAllocWithZone:), method_getImplementation(m), method_getTypeEncoding(m));
116
+ class_addMethod(metaclass, @selector(allocWithZone:), (IMP)allocWithZone, "@@:^{_NSZone=}");
117
+ }
118
+ }
119
+
120
+ wax_instance_create(L, klass, YES);
121
+
122
+ return 1;
123
+ }
124
+
125
+ static int addProtocols(lua_State *L) {
126
+ wax_instance_userdata *instanceUserdata = (wax_instance_userdata *)luaL_checkudata(L, 1, WAX_INSTANCE_METATABLE_NAME);
127
+
128
+ if (!instanceUserdata->isClass) {
129
+ luaL_error(L, "ERROR: Can only set a protocol on a class (You are trying to set one on an instance)");
130
+ return 0;
131
+ }
132
+
133
+ for (int i = 2; i <= lua_gettop(L); i++) {
134
+ const char *protocolName = luaL_checkstring(L, i);
135
+ Protocol *protocol = objc_getProtocol(protocolName);
136
+ if (!protocol) luaL_error(L, "Could not find protocol named '%s'\nHint: Sometimes the runtime cannot automatically find a protocol. Try adding it (via xCode) to the file ProtocolLoader.h", protocolName);
137
+ class_addProtocol(instanceUserdata->instance, protocol);
138
+ }
139
+
140
+ return 0;
141
+ }
142
+
143
+ static int name(lua_State *L) {
144
+ wax_instance_userdata *instanceUserdata = (wax_instance_userdata *)luaL_checkudata(L, 1, WAX_INSTANCE_METATABLE_NAME);
145
+ lua_pushstring(L, [NSStringFromClass([instanceUserdata->instance class]) UTF8String]);
146
+ return 1;
147
+ }
148
+
149
+ static id allocWithZone(id self, SEL _cmd, NSZone *zone) {
150
+ lua_State *L = wax_currentLuaState();
151
+ BEGIN_STACK_MODIFY(L);
152
+
153
+ id instance = [self wax_originalAllocWithZone:zone];
154
+ object_setInstanceVariable(instance, WAX_CLASS_INSTANCE_USERDATA_IVAR_NAME, @"YEAP");
155
+
156
+ END_STACK_MODIFY(L, 0);
157
+
158
+ return instance;
159
+ }
160
+
161
+
162
+ static void setValueForUndefinedKey(id self, SEL cmd, id value, NSString *key) {
163
+ lua_State *L = wax_currentLuaState();
164
+
165
+ BEGIN_STACK_MODIFY(L);
166
+
167
+ wax_instance_pushUserdata(L, self);
168
+ wax_fromObjc(L, "@", &value);
169
+ lua_setfield(L, -2, [key UTF8String]);
170
+
171
+ END_STACK_MODIFY(L, 0);
172
+ }
173
+
174
+ static id valueForUndefinedKey(id self, SEL cmd, NSString *key) {
175
+ lua_State *L = wax_currentLuaState();
176
+ id result = nil;
177
+
178
+ BEGIN_STACK_MODIFY(L);
179
+
180
+ wax_instance_pushUserdata(L, self);
181
+ lua_getfield(L, -1, [key UTF8String]);
182
+
183
+ id *keyValue = wax_copyToObjc(L, "@", -1, nil);
184
+ result = *keyValue;
185
+ free(keyValue);
186
+
187
+ END_STACK_MODIFY(L, 0);
188
+
189
+ return result;
190
+ }