| 1 |
/* |
| 2 |
** Load and dump code. |
| 3 |
** Copyright (C) 2005-2025 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_buf.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) { |
| 38 |
int xmode = 1; |
| 39 |
const char *mode = ls->mode; |
| 40 |
char c; |
| 41 |
while ((c = *mode++)) { |
| 42 |
if (c == (bc ? 'b' : 't')) xmode = 0; |
| 43 |
if (c == (LJ_FR2 ? 'W' : 'X')) ls->fr2 = !LJ_FR2; |
| 44 |
} |
| 45 |
if (xmode) { |
| 46 |
setstrV(L, L->top++, lj_err_str(L, LJ_ERR_XMODE)); |
| 47 |
lj_err_throw(L, LUA_ERRSYNTAX); |
| 48 |
} |
| 49 |
} |
| 50 |
pt = bc ? lj_bcread(ls) : lj_parse(ls); |
| 51 |
if (ls->fr2 == LJ_FR2) { |
| 52 |
fn = lj_func_newL_empty(L, pt, tabref(L->env)); |
| 53 |
/* Don't combine above/below into one statement. */ |
| 54 |
setfuncV(L, L->top++, fn); |
| 55 |
} else { |
| 56 |
/* Non-native generation returns a dumpable, but non-runnable prototype. */ |
| 57 |
setprotoV(L, L->top++, pt); |
| 58 |
} |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
LUA_API int lua_loadx(lua_State *L, lua_Reader reader, void *data, |
| 63 |
const char *chunkname, const char *mode) |
| 64 |
{ |
| 65 |
LexState ls; |
| 66 |
int status; |
| 67 |
ls.rfunc = reader; |
| 68 |
ls.rdata = data; |
| 69 |
ls.chunkarg = chunkname ? chunkname : "?"; |
| 70 |
ls.mode = mode; |
| 71 |
lj_buf_init(L, &ls.sb); |
| 72 |
status = lj_vm_cpcall(L, NULL, &ls, cpparser); |
| 73 |
lj_lex_cleanup(L, &ls); |
| 74 |
lj_gc_check(L); |
| 75 |
return status; |
| 76 |
} |
| 77 |
|
| 78 |
LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data, |
| 79 |
const char *chunkname) |
| 80 |
{ |
| 81 |
return lua_loadx(L, reader, data, chunkname, NULL); |
| 82 |
} |
| 83 |
|
| 84 |
typedef struct FileReaderCtx { |
| 85 |
FILE *fp; |
| 86 |
char buf[LUAL_BUFFERSIZE]; |
| 87 |
} FileReaderCtx; |
| 88 |
|
| 89 |
static const char *reader_file(lua_State *L, void *ud, size_t *size) |
| 90 |
{ |
| 91 |
FileReaderCtx *ctx = (FileReaderCtx *)ud; |
| 92 |
UNUSED(L); |
| 93 |
if (feof(ctx->fp)) return NULL; |
| 94 |
*size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp); |
| 95 |
return *size > 0 ? ctx->buf : NULL; |
| 96 |
} |
| 97 |
|
| 98 |
LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename, |
| 99 |
const char *mode) |
| 100 |
{ |
| 101 |
FileReaderCtx ctx; |
| 102 |
int status; |
| 103 |
const char *chunkname; |
| 104 |
int err = 0; |
| 105 |
if (filename) { |
| 106 |
chunkname = lua_pushfstring(L, "@%s", filename); |
| 107 |
ctx.fp = fopen(filename, "rb"); |
| 108 |
if (ctx.fp == NULL) { |
| 109 |
L->top--; |
| 110 |
lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno)); |
| 111 |
return LUA_ERRFILE; |
| 112 |
} |
| 113 |
} else { |
| 114 |
ctx.fp = stdin; |
| 115 |
chunkname = "=stdin"; |
| 116 |
} |
| 117 |
status = lua_loadx(L, reader_file, &ctx, chunkname, mode); |
| 118 |
if (ferror(ctx.fp)) err = errno; |
| 119 |
if (filename) { |
| 120 |
fclose(ctx.fp); |
| 121 |
L->top--; |
| 122 |
copyTV(L, L->top-1, L->top); |
| 123 |
} |
| 124 |
if (err) { |
| 125 |
L->top--; |
| 126 |
lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(err)); |
| 127 |
return LUA_ERRFILE; |
| 128 |
} |
| 129 |
return status; |
| 130 |
} |
| 131 |
|
| 132 |
LUALIB_API int luaL_loadfile(lua_State *L, const char *filename) |
| 133 |
{ |
| 134 |
return luaL_loadfilex(L, filename, NULL); |
| 135 |
} |
| 136 |
|
| 137 |
typedef struct StringReaderCtx { |
| 138 |
const char *str; |
| 139 |
size_t size; |
| 140 |
} StringReaderCtx; |
| 141 |
|
| 142 |
static const char *reader_string(lua_State *L, void *ud, size_t *size) |
| 143 |
{ |
| 144 |
StringReaderCtx *ctx = (StringReaderCtx *)ud; |
| 145 |
UNUSED(L); |
| 146 |
if (ctx->size == 0) return NULL; |
| 147 |
*size = ctx->size; |
| 148 |
ctx->size = 0; |
| 149 |
return ctx->str; |
| 150 |
} |
| 151 |
|
| 152 |
LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buf, size_t size, |
| 153 |
const char *name, const char *mode) |
| 154 |
{ |
| 155 |
StringReaderCtx ctx; |
| 156 |
ctx.str = buf; |
| 157 |
ctx.size = size; |
| 158 |
return lua_loadx(L, reader_string, &ctx, name, mode); |
| 159 |
} |
| 160 |
|
| 161 |
LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size, |
| 162 |
const char *name) |
| 163 |
{ |
| 164 |
return luaL_loadbufferx(L, buf, size, name, NULL); |
| 165 |
} |
| 166 |
|
| 167 |
LUALIB_API int luaL_loadstring(lua_State *L, const char *s) |
| 168 |
{ |
| 169 |
return luaL_loadbuffer(L, s, strlen(s), s); |
| 170 |
} |
| 171 |
|
| 172 |
/* -- Dump bytecode ------------------------------------------------------- */ |
| 173 |
|
| 174 |
LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data) |
| 175 |
{ |
| 176 |
cTValue *o = L->top-1; |
| 177 |
uint32_t flags = LJ_FR2*BCDUMP_F_FR2; /* Default mode for legacy C API. */ |
| 178 |
lj_checkapi(L->top > L->base, "top slot empty"); |
| 179 |
if (tvisfunc(o) && isluafunc(funcV(o))) |
| 180 |
return lj_bcwrite(L, funcproto(funcV(o)), writer, data, flags); |
| 181 |
else |
| 182 |
return 1; |
| 183 |
} |
| 184 |
|
| 185 |
/* -- Luajittex needs this one because it's faster than make it with Lua -- */ |
| 186 |
LUA_API int RESERVED_lua_dump(lua_State *L, lua_Writer writer, void *data, int strip) |
| 187 |
{ |
| 188 |
cTValue *o = L->top-1; |
| 189 |
lj_checkapi(L->top > L->base, "top slot empty"); |
| 190 |
if (tvisfunc(o) && isluafunc(funcV(o))) |
| 191 |
return lj_bcwrite(L, funcproto(funcV(o)), writer, data, strip); |
| 192 |
else |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
/* -- Luajittex needs this one because it overloads loadfile -- */ |
| 197 |
LUALIB_API int RESERVED_load_aux_JIT(lua_State *L, int status, int envarg) |
| 198 |
{ |
| 199 |
if (status == 0) { |
| 200 |
if (tvistab(L->base+envarg-1)) { |
| 201 |
GCfunc *fn = funcV(L->top-1); |
| 202 |
GCtab *t = tabV(L->base+envarg-1); |
| 203 |
setgcref(fn->c.env, obj2gco(t)); |
| 204 |
lj_gc_objbarrier(L, fn, t); |
| 205 |
} |
| 206 |
return 1; |
| 207 |
} else { |
| 208 |
setnilV(L->top-2); |
| 209 |
return 2; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|