summaryrefslogtreecommitdiff
path: root/src/lib_aux.c
diff options
context:
space:
mode:
authorMike Pall <mike>2012-09-21 16:32:24 +0200
committerMike Pall <mike>2012-09-21 16:32:24 +0200
commit3dceaa9a7422497aab88dac7c58467c101c5f785 (patch)
tree696564bf8ba4ee3b6a7ffd47cdc8ef1e33d7cde9 /src/lib_aux.c
parent98f05808fac3b2c439034b9eca55e7e9492e3a9f (diff)
downloadluajit-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/lib_aux.c')
-rw-r--r--src/lib_aux.c77
1 files changed, 0 insertions, 77 deletions
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
238typedef struct FileReaderCtx {
239 FILE *fp;
240 char buf[LUAL_BUFFERSIZE];
241} FileReaderCtx;
242
243static 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
252LUALIB_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
284typedef struct StringReaderCtx {
285 const char *str;
286 size_t size;
287} StringReaderCtx;
288
289static 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
299LUALIB_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
308LUALIB_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
315static int panic(lua_State *L) 238static int panic(lua_State *L)