3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
23 #include "lj_bcdump.h"
26 /* -- Load Lua source code and bytecode ----------------------------------- */
28 static TValue
*cpparser(lua_State
*L
, lua_CFunction dummy
, void *ud
)
30 LexState
*ls
= (LexState
*)ud
;
35 cframe_errfunc(L
->cframe
) = -1; /* Inherit error function. */
36 bc
= lj_lex_setup(L
, ls
);
39 const char *mode
= ls
->mode
;
41 while ((c
= *mode
++)) {
42 if (c
== (bc
? 'b' : 't')) xmode
= 0;
43 if (c
== (LJ_FR2
? 'W' : 'X')) ls
->fr2
= !LJ_FR2
;
46 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_XMODE
));
47 lj_err_throw(L
, LUA_ERRSYNTAX
);
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
);
56 /* Non-native generation returns a dumpable, but non-runnable prototype. */
57 setprotoV(L
, L
->top
++, pt
);
62 LUA_API
int lua_loadx(lua_State
*L
, lua_Reader reader
, void *data
,
63 const char *chunkname
, const char *mode
)
69 ls
.chunkarg
= chunkname
? chunkname
: "?";
71 lj_buf_init(L
, &ls
.sb
);
72 status
= lj_vm_cpcall(L
, NULL
, &ls
, cpparser
);
73 lj_lex_cleanup(L
, &ls
);
78 LUA_API
int lua_load(lua_State
*L
, lua_Reader reader
, void *data
,
79 const char *chunkname
)
81 return lua_loadx(L
, reader
, data
, chunkname
, NULL
);
84 typedef struct FileReaderCtx
{
86 char buf
[LUAL_BUFFERSIZE
];
89 static const char *reader_file(lua_State
*L
, void *ud
, size_t *size
)
91 FileReaderCtx
*ctx
= (FileReaderCtx
*)ud
;
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
;
98 LUALIB_API
int luaL_loadfilex(lua_State
*L
, const char *filename
,
103 const char *chunkname
;
105 ctx
.fp
= fopen(filename
, "rb");
106 if (ctx
.fp
== NULL
) {
107 lua_pushfstring(L
, "cannot open %s: %s", filename
, strerror(errno
));
110 chunkname
= lua_pushfstring(L
, "@%s", filename
);
113 chunkname
= "=stdin";
115 status
= lua_loadx(L
, reader_file
, &ctx
, chunkname
, mode
);
116 if (ferror(ctx
.fp
)) {
117 L
->top
-= filename
? 2 : 1;
118 lua_pushfstring(L
, "cannot read %s: %s", chunkname
+1, strerror(errno
));
125 copyTV(L
, L
->top
-1, L
->top
);
131 LUALIB_API
int luaL_loadfile(lua_State
*L
, const char *filename
)
133 return luaL_loadfilex(L
, filename
, NULL
);
136 typedef struct StringReaderCtx
{
141 static const char *reader_string(lua_State
*L
, void *ud
, size_t *size
)
143 StringReaderCtx
*ctx
= (StringReaderCtx
*)ud
;
145 if (ctx
->size
== 0) return NULL
;
151 LUALIB_API
int luaL_loadbufferx(lua_State
*L
, const char *buf
, size_t size
,
152 const char *name
, const char *mode
)
157 return lua_loadx(L
, reader_string
, &ctx
, name
, mode
);
160 LUALIB_API
int luaL_loadbuffer(lua_State
*L
, const char *buf
, size_t size
,
163 return luaL_loadbufferx(L
, buf
, size
, name
, NULL
);
166 LUALIB_API
int luaL_loadstring(lua_State
*L
, const char *s
)
168 return luaL_loadbuffer(L
, s
, strlen(s
), s
);
171 /* -- Dump bytecode ------------------------------------------------------- */
173 LUA_API
int lua_dump(lua_State
*L
, lua_Writer writer
, void *data
)
175 cTValue
*o
= L
->top
-1;
176 uint32_t flags
= LJ_FR2
*BCDUMP_F_FR2
; /* Default mode for legacy C API. */
177 lj_checkapi(L
->top
> L
->base
, "top slot empty");
178 if (tvisfunc(o
) && isluafunc(funcV(o
)))
179 return lj_bcwrite(L
, funcproto(funcV(o
)), writer
, data
, flags
);