19
19
#include "lj_obj.h"
20
20
#include "lj_err.h"
21
21
#include "lj_state.h"
22
23
#include "lj_lib.h"
29
/* -- I/O error handling -------------------------------------------------- */
31
LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname)
34
setboolV(L->top++, 1);
37
int en = errno; /* Lua API calls may change this value. */
40
lua_pushfstring(L, "%s: %s", fname, strerror(en));
42
lua_pushfstring(L, "%s", strerror(en));
43
setintV(L->top++, en);
49
LUALIB_API int luaL_execresult(lua_State *L, int stat)
53
if (WIFSIGNALED(stat)) {
54
stat = WTERMSIG(stat);
56
lua_pushliteral(L, "signal");
59
stat = WEXITSTATUS(stat);
61
setboolV(L->top++, 1);
64
lua_pushliteral(L, "exit");
68
setboolV(L->top++, 1);
71
lua_pushliteral(L, "exit");
73
setintV(L->top++, stat);
76
return luaL_fileresult(L, 0, NULL);
24
79
/* -- Module registration ------------------------------------------------- */
26
81
LUALIB_API const char *luaL_findtable(lua_State *L, int idx,
236
/* -- Load Lua code ------------------------------------------------------- */
238
typedef struct FileReaderCtx {
240
char buf[LUAL_BUFFERSIZE];
243
static const char *reader_file(lua_State *L, void *ud, size_t *size)
245
FileReaderCtx *ctx = (FileReaderCtx *)ud;
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;
252
LUALIB_API int luaL_loadfile(lua_State *L, const char *filename)
256
const char *chunkname;
258
ctx.fp = fopen(filename, "rb");
259
if (ctx.fp == NULL) {
260
lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno));
263
chunkname = lua_pushfstring(L, "@%s", filename);
266
chunkname = "=stdin";
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));
278
copyTV(L, L->top-1, L->top);
284
typedef struct StringReaderCtx {
289
static const char *reader_string(lua_State *L, void *ud, size_t *size)
291
StringReaderCtx *ctx = (StringReaderCtx *)ud;
293
if (ctx->size == 0) return NULL;
299
LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size,
305
return lua_load(L, reader_string, &ctx, name);
308
LUALIB_API int luaL_loadstring(lua_State *L, const char *s)
310
return luaL_loadbuffer(L, s, strlen(s), s);
313
291
/* -- Default allocator and panic function -------------------------------- */
315
293
static int panic(lua_State *L)
317
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
318
lua_tostring(L, -1));
295
const char *s = lua_tostring(L, -1);
296
fputs("PANIC: unprotected error in call to Lua API (", stderr);
297
fputs(s ? s : "?", stderr);
298
fputc(')', stderr); fputc('\n', stderr);