diff options
author | Mike Pall <mike> | 2012-09-21 16:32:24 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2012-09-21 16:32:24 +0200 |
commit | 3dceaa9a7422497aab88dac7c58467c101c5f785 (patch) | |
tree | 696564bf8ba4ee3b6a7ffd47cdc8ef1e33d7cde9 /src | |
parent | 98f05808fac3b2c439034b9eca55e7e9492e3a9f (diff) | |
download | luajit-3dceaa9a7422497aab88dac7c58467c101c5f785.tar.gz luajit-3dceaa9a7422497aab88dac7c58467c101c5f785.tar.bz2 luajit-3dceaa9a7422497aab88dac7c58467c101c5f785.zip |
Move load/dump functions to lj_load.c. Add load modes.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/Makefile.dep | 31 | ||||
-rw-r--r-- | src/lauxlib.h | 6 | ||||
-rw-r--r-- | src/lib_aux.c | 77 | ||||
-rw-r--r-- | src/lj_api.c | 44 | ||||
-rw-r--r-- | src/lj_errmsg.h | 2 | ||||
-rw-r--r-- | src/lj_lex.c | 2 | ||||
-rw-r--r-- | src/lj_lex.h | 1 | ||||
-rw-r--r-- | src/lj_load.c | 168 | ||||
-rw-r--r-- | src/ljamalg.c | 1 | ||||
-rw-r--r-- | src/lua.h | 2 |
11 files changed, 197 insertions, 139 deletions
diff --git a/src/Makefile b/src/Makefile index bfb03e9a..8b7218e7 100644 --- a/src/Makefile +++ b/src/Makefile | |||
@@ -447,7 +447,7 @@ LJLIB_C= $(LJLIB_O:.o=.c) | |||
447 | LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o \ | 447 | LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o \ |
448 | lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \ | 448 | lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \ |
449 | lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \ | 449 | lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \ |
450 | lj_api.o lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o \ | 450 | lj_api.o lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \ |
451 | lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \ | 451 | lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \ |
452 | lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \ | 452 | lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \ |
453 | lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \ | 453 | lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \ |
diff --git a/src/Makefile.dep b/src/Makefile.dep index af5d33b1..99acc044 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep | |||
@@ -41,8 +41,7 @@ lj_alloc.o: lj_alloc.c lj_def.h lua.h luaconf.h lj_arch.h lj_alloc.h | |||
41 | lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ | 41 | lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ |
42 | lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \ | 42 | lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \ |
43 | lj_meta.h lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h \ | 43 | lj_meta.h lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h \ |
44 | lj_dispatch.h lj_traceerr.h lj_vm.h lj_lex.h lj_bcdump.h lj_parse.h \ | 44 | lj_dispatch.h lj_traceerr.h lj_vm.h lj_strscan.h |
45 | lj_strscan.h | ||
46 | lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ | 45 | lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ |
47 | lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h \ | 46 | lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h \ |
48 | lj_ircall.h lj_iropt.h lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h \ | 47 | lj_ircall.h lj_iropt.h lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h \ |
@@ -125,6 +124,9 @@ lj_lex.o: lj_lex.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ | |||
125 | lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \ | 124 | lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \ |
126 | lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_bc.h \ | 125 | lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_bc.h \ |
127 | lj_dispatch.h lj_jit.h lj_ir.h lj_vm.h lj_strscan.h lj_lib.h | 126 | lj_dispatch.h lj_jit.h lj_ir.h lj_vm.h lj_strscan.h lj_lib.h |
127 | lj_load.o: lj_load.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \ | ||
128 | lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_func.h lj_frame.h \ | ||
129 | lj_bc.h lj_vm.h lj_lex.h lj_bcdump.h lj_parse.h | ||
128 | lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ | 130 | lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ |
129 | lj_gc.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.h lj_dispatch.h lj_bc.h \ | 131 | lj_gc.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.h lj_dispatch.h lj_bc.h \ |
130 | lj_traceerr.h lj_vm.h | 132 | lj_traceerr.h lj_vm.h |
@@ -193,18 +195,19 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \ | |||
193 | lj_obj.c lj_str.c lj_tab.c lj_func.c lj_udata.c lj_meta.c lj_strscan.h \ | 195 | lj_obj.c lj_str.c lj_tab.c lj_func.c lj_udata.c lj_meta.c lj_strscan.h \ |
194 | lj_debug.c lj_state.c lj_lex.h lj_alloc.h lj_dispatch.c lj_ccallback.h \ | 196 | lj_debug.c lj_state.c lj_lex.h lj_alloc.h lj_dispatch.c lj_ccallback.h \ |
195 | luajit.h lj_vmevent.c lj_vmevent.h lj_vmmath.c lj_strscan.c lj_api.c \ | 197 | luajit.h lj_vmevent.c lj_vmevent.h lj_vmmath.c lj_strscan.c lj_api.c \ |
196 | lj_bcdump.h lj_parse.h lj_lex.c lualib.h lj_parse.c lj_bcread.c \ | 198 | lj_lex.c lualib.h lj_parse.h lj_parse.c lj_bcread.c lj_bcdump.h \ |
197 | lj_bcwrite.c lj_ctype.c lj_cdata.c lj_cconv.h lj_cconv.c lj_ccall.c \ | 199 | lj_bcwrite.c lj_load.c lj_ctype.c lj_cdata.c lj_cconv.h lj_cconv.c \ |
198 | lj_ccall.h lj_ccallback.c lj_target.h lj_target_*.h lj_mcode.h \ | 200 | lj_ccall.c lj_ccall.h lj_ccallback.c lj_target.h lj_target_*.h \ |
199 | lj_carith.c lj_carith.h lj_clib.c lj_clib.h lj_cparse.c lj_cparse.h \ | 201 | lj_mcode.h lj_carith.c lj_carith.h lj_clib.c lj_clib.h lj_cparse.c \ |
200 | lj_lib.c lj_lib.h lj_ir.c lj_ircall.h lj_iropt.h lj_opt_mem.c \ | 202 | lj_cparse.h lj_lib.c lj_lib.h lj_ir.c lj_ircall.h lj_iropt.h \ |
201 | lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c lj_opt_loop.c \ | 203 | lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \ |
202 | lj_snap.h lj_opt_split.c lj_opt_sink.c lj_mcode.c lj_snap.c lj_record.c \ | 204 | lj_opt_loop.c lj_snap.h lj_opt_split.c lj_opt_sink.c lj_mcode.c \ |
203 | lj_record.h lj_ffrecord.h lj_crecord.c lj_crecord.h lj_ffrecord.c \ | 205 | lj_snap.c lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c \ |
204 | lj_recdef.h lj_asm.c lj_asm.h lj_emit_*.h lj_asm_*.h lj_trace.c \ | 206 | lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_emit_*.h \ |
205 | lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c lib_base.c lj_libdef.h \ | 207 | lj_asm_*.h lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c \ |
206 | lib_math.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c \ | 208 | lib_base.c lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c \ |
207 | lib_debug.c lib_bit.c lib_jit.c lib_ffi.c lib_init.c | 209 | lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_ffi.c \ |
210 | lib_init.c | ||
208 | luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h | 211 | luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h |
209 | host/buildvm.o: host/buildvm.c host/buildvm.h lj_def.h lua.h luaconf.h \ | 212 | host/buildvm.o: host/buildvm.c host/buildvm.h lj_def.h lua.h luaconf.h \ |
210 | lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_gc.h lj_obj.h lj_bc.h lj_ir.h \ | 213 | lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_gc.h lj_obj.h lj_bc.h lj_ir.h \ |
diff --git a/src/lauxlib.h b/src/lauxlib.h index 505a9f52..80585f64 100644 --- a/src/lauxlib.h +++ b/src/lauxlib.h | |||
@@ -75,7 +75,11 @@ LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, | |||
75 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, | 75 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, |
76 | const char *fname, int szhint); | 76 | const char *fname, int szhint); |
77 | 77 | ||
78 | 78 | /* From Lua 5.2. */ | |
79 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, | ||
80 | const char *mode); | ||
81 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, | ||
82 | const char *name, const char *mode); | ||
79 | 83 | ||
80 | 84 | ||
81 | /* | 85 | /* |
diff --git a/src/lib_aux.c b/src/lib_aux.c index f25ab65a..8fbee71b 100644 --- a/src/lib_aux.c +++ b/src/lib_aux.c | |||
@@ -233,83 +233,6 @@ LUALIB_API void luaL_unref(lua_State *L, int t, int ref) | |||
233 | } | 233 | } |
234 | } | 234 | } |
235 | 235 | ||
236 | /* -- Load Lua code ------------------------------------------------------- */ | ||
237 | |||
238 | typedef struct FileReaderCtx { | ||
239 | FILE *fp; | ||
240 | char buf[LUAL_BUFFERSIZE]; | ||
241 | } FileReaderCtx; | ||
242 | |||
243 | static const char *reader_file(lua_State *L, void *ud, size_t *size) | ||
244 | { | ||
245 | FileReaderCtx *ctx = (FileReaderCtx *)ud; | ||
246 | UNUSED(L); | ||
247 | if (feof(ctx->fp)) return NULL; | ||
248 | *size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp); | ||
249 | return *size > 0 ? ctx->buf : NULL; | ||
250 | } | ||
251 | |||
252 | LUALIB_API int luaL_loadfile(lua_State *L, const char *filename) | ||
253 | { | ||
254 | FileReaderCtx ctx; | ||
255 | int status; | ||
256 | const char *chunkname; | ||
257 | if (filename) { | ||
258 | ctx.fp = fopen(filename, "rb"); | ||
259 | if (ctx.fp == NULL) { | ||
260 | lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno)); | ||
261 | return LUA_ERRFILE; | ||
262 | } | ||
263 | chunkname = lua_pushfstring(L, "@%s", filename); | ||
264 | } else { | ||
265 | ctx.fp = stdin; | ||
266 | chunkname = "=stdin"; | ||
267 | } | ||
268 | status = lua_load(L, reader_file, &ctx, chunkname); | ||
269 | if (ferror(ctx.fp)) { | ||
270 | L->top -= filename ? 2 : 1; | ||
271 | lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno)); | ||
272 | if (filename) | ||
273 | fclose(ctx.fp); | ||
274 | return LUA_ERRFILE; | ||
275 | } | ||
276 | if (filename) { | ||
277 | L->top--; | ||
278 | copyTV(L, L->top-1, L->top); | ||
279 | fclose(ctx.fp); | ||
280 | } | ||
281 | return status; | ||
282 | } | ||
283 | |||
284 | typedef struct StringReaderCtx { | ||
285 | const char *str; | ||
286 | size_t size; | ||
287 | } StringReaderCtx; | ||
288 | |||
289 | static const char *reader_string(lua_State *L, void *ud, size_t *size) | ||
290 | { | ||
291 | StringReaderCtx *ctx = (StringReaderCtx *)ud; | ||
292 | UNUSED(L); | ||
293 | if (ctx->size == 0) return NULL; | ||
294 | *size = ctx->size; | ||
295 | ctx->size = 0; | ||
296 | return ctx->str; | ||
297 | } | ||
298 | |||
299 | LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size, | ||
300 | const char *name) | ||
301 | { | ||
302 | StringReaderCtx ctx; | ||
303 | ctx.str = buf; | ||
304 | ctx.size = size; | ||
305 | return lua_load(L, reader_string, &ctx, name); | ||
306 | } | ||
307 | |||
308 | LUALIB_API int luaL_loadstring(lua_State *L, const char *s) | ||
309 | { | ||
310 | return luaL_loadbuffer(L, s, strlen(s), s); | ||
311 | } | ||
312 | |||
313 | /* -- Default allocator and panic function -------------------------------- */ | 236 | /* -- Default allocator and panic function -------------------------------- */ |
314 | 237 | ||
315 | static int panic(lua_State *L) | 238 | static int panic(lua_State *L) |
diff --git a/src/lj_api.c b/src/lj_api.c index a9ea38d8..5a0bd07a 100644 --- a/src/lj_api.c +++ b/src/lj_api.c | |||
@@ -23,9 +23,6 @@ | |||
23 | #include "lj_frame.h" | 23 | #include "lj_frame.h" |
24 | #include "lj_trace.h" | 24 | #include "lj_trace.h" |
25 | #include "lj_vm.h" | 25 | #include "lj_vm.h" |
26 | #include "lj_lex.h" | ||
27 | #include "lj_bcdump.h" | ||
28 | #include "lj_parse.h" | ||
29 | #include "lj_strscan.h" | 26 | #include "lj_strscan.h" |
30 | 27 | ||
31 | /* -- Common helper functions --------------------------------------------- */ | 28 | /* -- Common helper functions --------------------------------------------- */ |
@@ -1138,47 +1135,6 @@ LUA_API int lua_resume(lua_State *L, int nargs) | |||
1138 | return LUA_ERRRUN; | 1135 | return LUA_ERRRUN; |
1139 | } | 1136 | } |
1140 | 1137 | ||
1141 | /* -- Load and dump Lua code ---------------------------------------------- */ | ||
1142 | |||
1143 | static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud) | ||
1144 | { | ||
1145 | LexState *ls = (LexState *)ud; | ||
1146 | GCproto *pt; | ||
1147 | GCfunc *fn; | ||
1148 | UNUSED(dummy); | ||
1149 | cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ | ||
1150 | pt = lj_lex_setup(L, ls) ? lj_bcread(ls) : lj_parse(ls); | ||
1151 | fn = lj_func_newL_empty(L, pt, tabref(L->env)); | ||
1152 | /* Don't combine above/below into one statement. */ | ||
1153 | setfuncV(L, L->top++, fn); | ||
1154 | return NULL; | ||
1155 | } | ||
1156 | |||
1157 | LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data, | ||
1158 | const char *chunkname) | ||
1159 | { | ||
1160 | LexState ls; | ||
1161 | int status; | ||
1162 | ls.rfunc = reader; | ||
1163 | ls.rdata = data; | ||
1164 | ls.chunkarg = chunkname ? chunkname : "?"; | ||
1165 | lj_str_initbuf(&ls.sb); | ||
1166 | status = lj_vm_cpcall(L, NULL, &ls, cpparser); | ||
1167 | lj_lex_cleanup(L, &ls); | ||
1168 | lj_gc_check(L); | ||
1169 | return status; | ||
1170 | } | ||
1171 | |||
1172 | LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data) | ||
1173 | { | ||
1174 | cTValue *o = L->top-1; | ||
1175 | api_checknelems(L, 1); | ||
1176 | if (tvisfunc(o) && isluafunc(funcV(o))) | ||
1177 | return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0); | ||
1178 | else | ||
1179 | return 1; | ||
1180 | } | ||
1181 | |||
1182 | /* -- GC and memory management -------------------------------------------- */ | 1138 | /* -- GC and memory management -------------------------------------------- */ |
1183 | 1139 | ||
1184 | LUA_API int lua_gc(lua_State *L, int what, int data) | 1140 | LUA_API int lua_gc(lua_State *L, int what, int data) |
diff --git a/src/lj_errmsg.h b/src/lj_errmsg.h index 28c5ca08..da40e848 100644 --- a/src/lj_errmsg.h +++ b/src/lj_errmsg.h | |||
@@ -111,6 +111,7 @@ ERRDEF(NOJIT, "JIT compiler permanently disabled by build option") | |||
111 | ERRDEF(JITOPT, "unknown or malformed optimization flag " LUA_QS) | 111 | ERRDEF(JITOPT, "unknown or malformed optimization flag " LUA_QS) |
112 | 112 | ||
113 | /* Lexer/parser errors. */ | 113 | /* Lexer/parser errors. */ |
114 | ERRDEF(XMODE, "attempt to load chunk with wrong mode") | ||
114 | ERRDEF(XNEAR, "%s near " LUA_QS) | 115 | ERRDEF(XNEAR, "%s near " LUA_QS) |
115 | ERRDEF(XELEM, "lexical element too long") | 116 | ERRDEF(XELEM, "lexical element too long") |
116 | ERRDEF(XLINES, "chunk has too many lines") | 117 | ERRDEF(XLINES, "chunk has too many lines") |
@@ -144,7 +145,6 @@ ERRDEF(XGSCOPE, "<goto %s> jumps into the scope of local " LUA_QS) | |||
144 | /* Bytecode reader errors. */ | 145 | /* Bytecode reader errors. */ |
145 | ERRDEF(BCFMT, "cannot load incompatible bytecode") | 146 | ERRDEF(BCFMT, "cannot load incompatible bytecode") |
146 | ERRDEF(BCBAD, "cannot load malformed bytecode") | 147 | ERRDEF(BCBAD, "cannot load malformed bytecode") |
147 | ERRDEF(BCHEAD, "attempt to load bytecode with extra header") | ||
148 | 148 | ||
149 | #if LJ_HASFFI | 149 | #if LJ_HASFFI |
150 | /* FFI errors. */ | 150 | /* FFI errors. */ |
diff --git a/src/lj_lex.c b/src/lj_lex.c index e464f420..b54d2a23 100644 --- a/src/lj_lex.c +++ b/src/lj_lex.c | |||
@@ -406,7 +406,7 @@ int lj_lex_setup(lua_State *L, LexState *ls) | |||
406 | ** Lua code by looking at the first char. Since this is a potential | 406 | ** Lua code by looking at the first char. Since this is a potential |
407 | ** security violation no attempt is made to echo the chunkname either. | 407 | ** security violation no attempt is made to echo the chunkname either. |
408 | */ | 408 | */ |
409 | setstrV(L, L->top++, lj_err_str(L, LJ_ERR_BCHEAD)); | 409 | setstrV(L, L->top++, lj_err_str(L, LJ_ERR_BCBAD)); |
410 | lj_err_throw(L, LUA_ERRSYNTAX); | 410 | lj_err_throw(L, LUA_ERRSYNTAX); |
411 | } | 411 | } |
412 | return 1; | 412 | return 1; |
diff --git a/src/lj_lex.h b/src/lj_lex.h index 16f35aa5..7a4fd74b 100644 --- a/src/lj_lex.h +++ b/src/lj_lex.h | |||
@@ -64,6 +64,7 @@ typedef struct LexState { | |||
64 | BCLine lastline; /* Line of last token. */ | 64 | BCLine lastline; /* Line of last token. */ |
65 | GCstr *chunkname; /* Current chunk name (interned string). */ | 65 | GCstr *chunkname; /* Current chunk name (interned string). */ |
66 | const char *chunkarg; /* Chunk name argument. */ | 66 | const char *chunkarg; /* Chunk name argument. */ |
67 | const char *mode; /* Allow loading bytecode (b) and/or source text (t). */ | ||
67 | VarInfo *vstack; /* Stack for names and extents of local variables. */ | 68 | VarInfo *vstack; /* Stack for names and extents of local variables. */ |
68 | MSize sizevstack; /* Size of variable stack. */ | 69 | MSize sizevstack; /* Size of variable stack. */ |
69 | MSize vtop; /* Top of variable stack. */ | 70 | MSize vtop; /* Top of variable stack. */ |
diff --git a/src/lj_load.c b/src/lj_load.c new file mode 100644 index 00000000..e30421e6 --- /dev/null +++ b/src/lj_load.c | |||
@@ -0,0 +1,168 @@ | |||
1 | /* | ||
2 | ** Load and dump code. | ||
3 | ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | #include <errno.h> | ||
7 | #include <stdio.h> | ||
8 | |||
9 | #define lj_load_c | ||
10 | #define LUA_CORE | ||
11 | |||
12 | #include "lua.h" | ||
13 | #include "lauxlib.h" | ||
14 | |||
15 | #include "lj_obj.h" | ||
16 | #include "lj_gc.h" | ||
17 | #include "lj_err.h" | ||
18 | #include "lj_str.h" | ||
19 | #include "lj_func.h" | ||
20 | #include "lj_frame.h" | ||
21 | #include "lj_vm.h" | ||
22 | #include "lj_lex.h" | ||
23 | #include "lj_bcdump.h" | ||
24 | #include "lj_parse.h" | ||
25 | |||
26 | /* -- Load Lua source code and bytecode ----------------------------------- */ | ||
27 | |||
28 | static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud) | ||
29 | { | ||
30 | LexState *ls = (LexState *)ud; | ||
31 | GCproto *pt; | ||
32 | GCfunc *fn; | ||
33 | int bc; | ||
34 | UNUSED(dummy); | ||
35 | cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ | ||
36 | bc = lj_lex_setup(L, ls); | ||
37 | if (ls->mode && !strchr(ls->mode, bc ? 'b' : 't')) { | ||
38 | setstrV(L, L->top++, lj_err_str(L, LJ_ERR_XMODE)); | ||
39 | lj_err_throw(L, LUA_ERRSYNTAX); | ||
40 | } | ||
41 | pt = bc ? lj_bcread(ls) : lj_parse(ls); | ||
42 | fn = lj_func_newL_empty(L, pt, tabref(L->env)); | ||
43 | /* Don't combine above/below into one statement. */ | ||
44 | setfuncV(L, L->top++, fn); | ||
45 | return NULL; | ||
46 | } | ||
47 | |||
48 | LUA_API int lua_loadx(lua_State *L, lua_Reader reader, void *data, | ||
49 | const char *chunkname, const char *mode) | ||
50 | { | ||
51 | LexState ls; | ||
52 | int status; | ||
53 | ls.rfunc = reader; | ||
54 | ls.rdata = data; | ||
55 | ls.chunkarg = chunkname ? chunkname : "?"; | ||
56 | ls.mode = mode; | ||
57 | lj_str_initbuf(&ls.sb); | ||
58 | status = lj_vm_cpcall(L, NULL, &ls, cpparser); | ||
59 | lj_lex_cleanup(L, &ls); | ||
60 | lj_gc_check(L); | ||
61 | return status; | ||
62 | } | ||
63 | |||
64 | LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data, | ||
65 | const char *chunkname) | ||
66 | { | ||
67 | return lua_loadx(L, reader, data, chunkname, NULL); | ||
68 | } | ||
69 | |||
70 | typedef struct FileReaderCtx { | ||
71 | FILE *fp; | ||
72 | char buf[LUAL_BUFFERSIZE]; | ||
73 | } FileReaderCtx; | ||
74 | |||
75 | static const char *reader_file(lua_State *L, void *ud, size_t *size) | ||
76 | { | ||
77 | FileReaderCtx *ctx = (FileReaderCtx *)ud; | ||
78 | UNUSED(L); | ||
79 | if (feof(ctx->fp)) return NULL; | ||
80 | *size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp); | ||
81 | return *size > 0 ? ctx->buf : NULL; | ||
82 | } | ||
83 | |||
84 | LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename, | ||
85 | const char *mode) | ||
86 | { | ||
87 | FileReaderCtx ctx; | ||
88 | int status; | ||
89 | const char *chunkname; | ||
90 | if (filename) { | ||
91 | ctx.fp = fopen(filename, "rb"); | ||
92 | if (ctx.fp == NULL) { | ||
93 | lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno)); | ||
94 | return LUA_ERRFILE; | ||
95 | } | ||
96 | chunkname = lua_pushfstring(L, "@%s", filename); | ||
97 | } else { | ||
98 | ctx.fp = stdin; | ||
99 | chunkname = "=stdin"; | ||
100 | } | ||
101 | status = lua_loadx(L, reader_file, &ctx, chunkname, mode); | ||
102 | if (ferror(ctx.fp)) { | ||
103 | L->top -= filename ? 2 : 1; | ||
104 | lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno)); | ||
105 | if (filename) | ||
106 | fclose(ctx.fp); | ||
107 | return LUA_ERRFILE; | ||
108 | } | ||
109 | if (filename) { | ||
110 | L->top--; | ||
111 | copyTV(L, L->top-1, L->top); | ||
112 | fclose(ctx.fp); | ||
113 | } | ||
114 | return status; | ||
115 | } | ||
116 | |||
117 | LUALIB_API int luaL_loadfile(lua_State *L, const char *filename) | ||
118 | { | ||
119 | return luaL_loadfilex(L, filename, NULL); | ||
120 | } | ||
121 | |||
122 | typedef struct StringReaderCtx { | ||
123 | const char *str; | ||
124 | size_t size; | ||
125 | } StringReaderCtx; | ||
126 | |||
127 | static const char *reader_string(lua_State *L, void *ud, size_t *size) | ||
128 | { | ||
129 | StringReaderCtx *ctx = (StringReaderCtx *)ud; | ||
130 | UNUSED(L); | ||
131 | if (ctx->size == 0) return NULL; | ||
132 | *size = ctx->size; | ||
133 | ctx->size = 0; | ||
134 | return ctx->str; | ||
135 | } | ||
136 | |||
137 | LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buf, size_t size, | ||
138 | const char *name, const char *mode) | ||
139 | { | ||
140 | StringReaderCtx ctx; | ||
141 | ctx.str = buf; | ||
142 | ctx.size = size; | ||
143 | return lua_loadx(L, reader_string, &ctx, name, mode); | ||
144 | } | ||
145 | |||
146 | LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size, | ||
147 | const char *name) | ||
148 | { | ||
149 | return luaL_loadbufferx(L, buf, size, name, NULL); | ||
150 | } | ||
151 | |||
152 | LUALIB_API int luaL_loadstring(lua_State *L, const char *s) | ||
153 | { | ||
154 | return luaL_loadbuffer(L, s, strlen(s), s); | ||
155 | } | ||
156 | |||
157 | /* -- Dump bytecode ------------------------------------------------------- */ | ||
158 | |||
159 | LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data) | ||
160 | { | ||
161 | cTValue *o = L->top-1; | ||
162 | api_check(L, L->top > L->base); | ||
163 | if (tvisfunc(o) && isluafunc(funcV(o))) | ||
164 | return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0); | ||
165 | else | ||
166 | return 1; | ||
167 | } | ||
168 | |||
diff --git a/src/ljamalg.c b/src/ljamalg.c index a64c9429..7aae6260 100644 --- a/src/ljamalg.c +++ b/src/ljamalg.c | |||
@@ -49,6 +49,7 @@ | |||
49 | #include "lj_parse.c" | 49 | #include "lj_parse.c" |
50 | #include "lj_bcread.c" | 50 | #include "lj_bcread.c" |
51 | #include "lj_bcwrite.c" | 51 | #include "lj_bcwrite.c" |
52 | #include "lj_load.c" | ||
52 | #include "lj_ctype.c" | 53 | #include "lj_ctype.c" |
53 | #include "lj_cdata.c" | 54 | #include "lj_cdata.c" |
54 | #include "lj_cconv.c" | 55 | #include "lj_cconv.c" |
@@ -344,6 +344,8 @@ LUA_API int lua_gethookcount (lua_State *L); | |||
344 | /* From Lua 5.2. */ | 344 | /* From Lua 5.2. */ |
345 | LUA_API void *lua_upvalueid (lua_State *L, int idx, int n); | 345 | LUA_API void *lua_upvalueid (lua_State *L, int idx, int n); |
346 | LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2); | 346 | LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2); |
347 | LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt, | ||
348 | const char *chunkname, const char *mode); | ||
347 | 349 | ||
348 | 350 | ||
349 | struct lua_Debug { | 351 | struct lua_Debug { |