4
4
5
5
#include "l_crowlib.h"
6
6
7
- #include "lua/crowlib.lua.h"
8
- #include "lua/asl.lua.h"
9
- #include "lua/asllib.lua.h"
10
- #include "lua/clock.lua.h"
11
- #include "lua/metro.lua.h"
12
- #include "lua/public.lua.h"
13
- #include "lua/input.lua.h"
14
- #include "lua/output.lua.h"
15
- #include "lua/ii.lua.h"
16
- #include "build/iihelp.lua.h" // generated lua stub for loading i2c modules
17
- #include "lua/calibrate.lua.h"
18
- #include "lua/sequins.lua.h"
19
- #include "lua/quote.lua.h"
20
- #include "lua/timeline.lua.h"
21
- #include "lua/hotswap.lua.h"
7
+ // Lua libs wrapped in C-headers
8
+ #include "build/crowlib.h"
9
+ #include "build/asl.h"
10
+ #include "build/asllib.h"
11
+ #include "build/clock.h"
12
+ #include "build/metro.h"
13
+ #include "build/public.h"
14
+ #include "build/input.h"
15
+ #include "build/output.h"
16
+ #include "build/ii.h"
17
+ #include "build/iihelp.h" // generated lua stub for loading i2c modules
18
+ #include "build/calibrate.h"
19
+ #include "build/sequins.h"
20
+ #include "build/quote.h"
21
+ #include "build/timeline.h"
22
+ #include "build/hotswap.h"
22
23
23
24
#include "build/ii_lualink.h" // generated C header for linking to lua
24
25
25
- static int _writer (lua_State * L , const void * p , size_t sz , void * ud );
26
- static int _load_chunk (lua_State * L , const char * code , int strip );
27
26
static int _open_lib ( lua_State * L , const struct lua_lib_locator * lib , const char * name );
27
+ static void lua_full_gc (lua_State * L );
28
28
29
29
// mark the 3rd arg 'false' if you need to debug that library
30
30
const struct lua_lib_locator Lua_libs [] =
31
- { { "lua_crowlib" , lua_crowlib , true}
32
- , { "lua_asl" , lua_asl , true}
33
- , { "lua_asllib" , lua_asllib , true}
34
- , { "lua_clock" , lua_clock , true}
35
- , { "lua_metro" , lua_metro , true}
36
- , { "lua_input" , lua_input , true}
37
- , { "lua_output" , lua_output , true}
38
- , { "lua_public" , lua_public , true}
39
- , { "lua_ii" , lua_ii , true}
40
- , { "build_iihelp" , build_iihelp , true}
41
- , { "lua_calibrate" , lua_calibrate , true}
42
- , { "lua_sequins" , lua_sequins , true}
43
- , { "lua_quote" , lua_quote , true}
44
- , { "lua_timeline" , lua_timeline , true}
45
- , { "lua_hotswap" , lua_hotswap , true}
46
- , { NULL , NULL , true}
31
+ { { "lua_crowlib" , build_crowlib_lc , true, build_crowlib_lc_len }
32
+ , { "lua_asl" , build_asl_lc , true, build_asl_lc_len }
33
+ , { "lua_asllib" , build_asllib_lc , true, build_asllib_lc_len }
34
+ , { "lua_clock" , build_clock_lc , true, build_clock_lc_len }
35
+ , { "lua_metro" , build_metro_lc , true, build_metro_lc_len }
36
+ , { "lua_input" , build_input_lc , true, build_input_lc_len }
37
+ , { "lua_output" , build_output_lc , true, build_output_lc_len }
38
+ , { "lua_public" , build_public_lc , true, build_public_lc_len }
39
+ , { "lua_ii" , build_ii_lc , true, build_ii_lc_len }
40
+ , { "build_iihelp" , build_iihelp_lc , true, build_iihelp_lc_len }
41
+ , { "lua_calibrate" , build_calibrate_lc , true, build_calibrate_lc_len }
42
+ , { "lua_sequins" , build_sequins_lc , true, build_sequins_lc_len }
43
+ , { "lua_quote" , build_quote_lc , true, build_quote_lc_len }
44
+ , { "lua_timeline" , build_timeline_lc , true, build_timeline_lc_len }
45
+ , { "lua_hotswap" , build_hotswap_lc , true, build_hotswap_lc_len }
46
+ , { NULL , NULL , true, 0 }
47
47
};
48
48
49
49
@@ -69,9 +69,18 @@ void l_bootstrap_init(lua_State* L){
69
69
// crowlib C extensions
70
70
l_crowlib_init (L );
71
71
72
+ // track all user-created globals
73
+ luaL_dostring (L ,
74
+ "_user={}\n"
75
+ "local function trace(t,k,v)\n"
76
+ "_user[k]=true\n"
77
+ "rawset(t,k,v)\n"
78
+ "end\n"
79
+ "setmetatable(_G,{ __newindex = trace })\n"
80
+ );
81
+
72
82
// perform two full garbage collection cycles for full cleanup
73
- lua_gc (L , LUA_GCCOLLECT , 1 );
74
- lua_gc (L , LUA_GCCOLLECT , 1 );
83
+ lua_full_gc (L );
75
84
}
76
85
77
86
@@ -92,19 +101,22 @@ int l_bootstrap_dofile(lua_State* L)
92
101
default :{ cname [p ++ ] = l_name [i ]; } break ;
93
102
}
94
103
}
104
+ // goto fail; // no match was found, so error out (silently?)
105
+
95
106
strcomplete :
96
107
lua_pop ( L , 1 );
97
108
switch ( _open_lib ( L , Lua_libs , cname ) ){
98
109
case -1 : goto fail ;
99
- case 1 : return 1 ;
110
+ case 1 : lua_full_gc ( L ); return 1 ;
100
111
default : break ;
101
112
}
102
113
switch ( _open_lib ( L , Lua_ii_libs , cname ) ){
103
114
case -1 : goto fail ;
104
- case 1 : return 1 ;
115
+ case 1 : lua_full_gc ( L ); return 1 ;
105
116
default : break ;
106
117
}
107
118
printf ("can't open library: %s\n" , (char * )cname );
119
+
108
120
fail :
109
121
lua_pushnil (L );
110
122
return 1 ;
@@ -117,58 +129,14 @@ int l_bootstrap_dofile(lua_State* L)
117
129
118
130
/////////// private defns
119
131
120
- // this is a somewhat arbitrary size. must be big enough for the biggest library. 8kB was too small
121
- #define SIZED_STRING_LEN 0x4000 // (16kB)
122
- struct sized_string {
123
- char data [SIZED_STRING_LEN ];
124
- int len ;
125
- };
126
-
127
- #define UNUSED (x ) (void)(sizeof(x))
128
- static int _writer (lua_State * L , const void * p , size_t sz , void * ud )
129
- {
130
- UNUSED (L );
131
- struct sized_string * chunkstr = ud ; /// need explicit cast?
132
- if (chunkstr -> len + sz >= SIZED_STRING_LEN ){
133
- printf ("chunkstr too small.\n" );
134
- return 1 ;
135
- }
136
- memcpy (& chunkstr -> data [chunkstr -> len ], p , sz );
137
- chunkstr -> len += sz ;
138
- return 0 ;
139
- }
140
- #undef UNUSED
141
-
142
- static int _load_chunk (lua_State * L , const char * code , int strip )
143
- {
144
- int retval = 0 ;
145
- struct sized_string chunkstr = {.len = 0 };
146
- { // scope lua_State to destroy it asap
147
- lua_State * LL = luaL_newstate ();
148
- if ( !LL ){ printf ("luaL_newstate failed\n" ); return 1 ; }
149
- if ( luaL_loadstring (LL , code ) ){
150
- printf ("loadstring error\n" );
151
- retval = 1 ;
152
- goto close_LL ;
153
- }
154
- if ( lua_dump (LL , _writer , & chunkstr , strip ) ){
155
- printf ("dump error\n" );
156
- retval = 1 ;
157
- goto close_LL ;
158
- }
159
- close_LL :
160
- lua_close (LL );
161
- }
162
- luaL_loadbuffer (L , chunkstr .data , chunkstr .len , chunkstr .data ); // load our compiled chunk
163
- return retval ;
164
- }
165
-
166
132
static int _open_lib ( lua_State * L , const struct lua_lib_locator * lib , const char * name )
167
133
{
168
134
uint8_t i = 0 ;
169
135
while ( lib [i ].addr_of_luacode != NULL ){
170
136
if ( !strcmp ( name , lib [i ].name ) ){ // if the strings match
171
- if ( _load_chunk (L , lib [i ].addr_of_luacode , lib [i ].stripped ) ){
137
+ if ( luaL_loadbuffer (L , (const char * )lib [i ].addr_of_luacode
138
+ , lib [i ].len
139
+ , lib [i ].name ) ){
172
140
printf ("can't load library: %s\n" , (char * )lib [i ].name );
173
141
printf ( "%s\n" , (char * )lua_tostring ( L , -1 ) );
174
142
lua_pop ( L , 1 );
@@ -186,3 +154,8 @@ static int _open_lib( lua_State *L, const struct lua_lib_locator* lib, const cha
186
154
}
187
155
return 0 ; // not found
188
156
}
157
+
158
+ static void lua_full_gc (lua_State * L ){
159
+ lua_gc (L , LUA_GCCOLLECT , 1 );
160
+ lua_gc (L , LUA_GCCOLLECT , 1 );
161
+ }
0 commit comments