2002-10-26 01:31:28 +04:00
|
|
|
/*
|
2018-08-23 20:26:12 +03:00
|
|
|
** $Id: ldump.c $
|
2005-11-16 14:55:07 +03:00
|
|
|
** save precompiled Lua chunks
|
2002-10-26 01:31:28 +04:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2002-12-04 20:38:31 +03:00
|
|
|
#define ldump_c
|
2004-05-01 00:13:38 +04:00
|
|
|
#define LUA_CORE
|
2002-12-04 20:38:31 +03:00
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
2023-03-17 21:52:09 +03:00
|
|
|
#include <limits.h>
|
2014-11-02 22:19:04 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2002-10-26 01:31:28 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
2024-06-20 19:43:33 +03:00
|
|
|
#include "lapi.h"
|
2022-12-15 22:44:22 +03:00
|
|
|
#include "lgc.h"
|
2002-10-26 01:31:28 +04:00
|
|
|
#include "lobject.h"
|
|
|
|
#include "lstate.h"
|
2022-12-15 22:44:22 +03:00
|
|
|
#include "ltable.h"
|
2002-10-26 01:31:28 +04:00
|
|
|
#include "lundump.h"
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2002-10-26 01:31:28 +04:00
|
|
|
typedef struct {
|
2014-03-10 21:56:32 +04:00
|
|
|
lua_State *L;
|
|
|
|
lua_Writer writer;
|
|
|
|
void *data;
|
2024-07-27 19:32:59 +03:00
|
|
|
size_t offset; /* current position relative to beginning of dump */
|
2014-03-10 21:56:32 +04:00
|
|
|
int strip;
|
|
|
|
int status;
|
2022-12-15 22:44:22 +03:00
|
|
|
Table *h; /* table to track saved strings */
|
2024-03-13 15:16:51 +03:00
|
|
|
lua_Integer nstr; /* counter for counting saved strings */
|
2002-10-26 01:31:28 +04:00
|
|
|
} DumpState;
|
|
|
|
|
2014-02-28 20:13:01 +04:00
|
|
|
|
2014-03-11 22:56:27 +04:00
|
|
|
/*
|
2020-02-27 21:17:44 +03:00
|
|
|
** All high-level dumps go through dumpVector; you can change it to
|
2014-03-27 19:58:05 +04:00
|
|
|
** change the endianness of the result
|
2014-03-11 22:56:27 +04:00
|
|
|
*/
|
2020-02-27 21:17:44 +03:00
|
|
|
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
|
2005-11-16 14:55:07 +03:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
|
2014-03-11 22:56:27 +04:00
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2023-12-14 17:41:57 +03:00
|
|
|
/*
|
|
|
|
** Dump the block of memory pointed by 'b' with given 'size'.
|
|
|
|
** 'b' should not be NULL, except for the last call signaling the end
|
|
|
|
** of the dump.
|
|
|
|
*/
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpBlock (DumpState *D, const void *b, size_t size) {
|
2023-12-14 17:41:57 +03:00
|
|
|
if (D->status == 0) { /* do not write anything after an error */
|
2014-03-10 21:56:32 +04:00
|
|
|
lua_unlock(D->L);
|
|
|
|
D->status = (*D->writer)(D->L, b, size, D->data);
|
|
|
|
lua_lock(D->L);
|
2023-08-25 23:40:20 +03:00
|
|
|
D->offset += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-14 17:41:57 +03:00
|
|
|
/*
|
|
|
|
** Dump enough zeros to ensure that current position is a multiple of
|
|
|
|
** 'align'.
|
|
|
|
*/
|
2024-07-27 19:32:59 +03:00
|
|
|
static void dumpAlign (DumpState *D, unsigned align) {
|
|
|
|
unsigned padding = align - cast_uint(D->offset % align);
|
2023-12-14 17:41:57 +03:00
|
|
|
if (padding < align) { /* padding == align means no padding */
|
2023-08-25 23:40:20 +03:00
|
|
|
static lua_Integer paddingContent = 0;
|
2024-07-27 19:32:59 +03:00
|
|
|
lua_assert(align <= sizeof(lua_Integer));
|
2023-08-25 23:40:20 +03:00
|
|
|
dumpBlock(D, &paddingContent, padding);
|
2014-03-10 21:56:32 +04:00
|
|
|
}
|
2023-12-14 17:41:57 +03:00
|
|
|
lua_assert(D->offset % align == 0);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
#define dumpVar(D,x) dumpVector(D,&x,1)
|
2014-03-11 22:56:27 +04:00
|
|
|
|
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpByte (DumpState *D, int y) {
|
2014-03-10 21:56:32 +04:00
|
|
|
lu_byte x = (lu_byte)y;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpVar(D, x);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2023-03-17 21:52:09 +03:00
|
|
|
/*
|
2024-02-12 21:16:11 +03:00
|
|
|
** size for 'dumpVarint' buffer: each byte can store up to 7 bits.
|
|
|
|
** (The "+6" rounds up the division.)
|
2023-03-17 21:52:09 +03:00
|
|
|
*/
|
2024-03-13 15:16:51 +03:00
|
|
|
#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7)
|
2017-06-27 17:21:12 +03:00
|
|
|
|
2024-02-12 21:16:11 +03:00
|
|
|
/*
|
|
|
|
** Dumps an unsigned integer using the MSB Varint encoding
|
|
|
|
*/
|
2024-03-13 15:16:51 +03:00
|
|
|
static void dumpVarint (DumpState *D, size_t x) {
|
2017-06-27 17:21:12 +03:00
|
|
|
lu_byte buff[DIBS];
|
2024-07-27 19:32:59 +03:00
|
|
|
unsigned n = 1;
|
2024-02-12 21:16:11 +03:00
|
|
|
buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */
|
|
|
|
while ((x >>= 7) != 0) /* fill other bytes in reverse order */
|
2024-07-27 19:32:59 +03:00
|
|
|
buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80);
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpVector(D, buff + DIBS - n, n);
|
2017-06-27 17:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-13 15:16:51 +03:00
|
|
|
static void dumpSize (DumpState *D, size_t sz) {
|
|
|
|
dumpVarint(D, sz);
|
|
|
|
}
|
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpInt (DumpState *D, int x) {
|
2023-12-14 17:41:57 +03:00
|
|
|
lua_assert(x >= 0);
|
2024-03-13 15:16:51 +03:00
|
|
|
dumpVarint(D, cast(size_t, x));
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpNumber (DumpState *D, lua_Number x) {
|
|
|
|
dumpVar(D, x);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpInteger (DumpState *D, lua_Integer x) {
|
|
|
|
dumpVar(D, x);
|
2013-04-26 22:48:35 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2022-12-15 22:44:22 +03:00
|
|
|
/*
|
|
|
|
** Dump a String. First dump its "size": size==0 means NULL;
|
|
|
|
** size==1 is followed by an index and means "reuse saved string with
|
|
|
|
** that index"; size>=2 is followed by the string contents with real
|
|
|
|
** size==size-2 and means that string, which will be saved with
|
|
|
|
** the next available index.
|
|
|
|
*/
|
2023-08-30 17:26:16 +03:00
|
|
|
static void dumpString (DumpState *D, TString *ts) {
|
|
|
|
if (ts == NULL)
|
2024-03-13 15:16:51 +03:00
|
|
|
dumpSize(D, 0);
|
2014-03-10 21:56:32 +04:00
|
|
|
else {
|
2024-03-21 17:23:21 +03:00
|
|
|
TValue idx;
|
|
|
|
int tag = luaH_getstr(D->h, ts, &idx);
|
|
|
|
if (!tagisempty(tag)) { /* string already saved? */
|
2024-03-13 15:16:51 +03:00
|
|
|
dumpSize(D, 1); /* reuse a saved string */
|
|
|
|
dumpSize(D, cast_sizet(ivalue(&idx))); /* index of saved string */
|
2022-12-15 22:44:22 +03:00
|
|
|
}
|
|
|
|
else { /* must write and save the string */
|
|
|
|
TValue key, value; /* to save the string in the hash */
|
2023-08-30 17:26:16 +03:00
|
|
|
size_t size;
|
|
|
|
const char *s = getlstr(ts, size);
|
2024-03-13 15:16:51 +03:00
|
|
|
dumpSize(D, size + 2);
|
2023-11-10 18:35:48 +03:00
|
|
|
dumpVector(D, s, size + 1); /* include ending '\0' */
|
2022-12-15 22:44:22 +03:00
|
|
|
D->nstr++; /* one more saved string */
|
2023-08-30 17:26:16 +03:00
|
|
|
setsvalue(D->L, &key, ts); /* the string is the key */
|
2024-03-13 15:16:51 +03:00
|
|
|
setivalue(&value, D->nstr); /* its index is the value */
|
2023-11-08 16:02:06 +03:00
|
|
|
luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */
|
2022-12-15 22:44:22 +03:00
|
|
|
/* integer value does not need barrier */
|
|
|
|
}
|
2014-03-01 19:18:44 +04:00
|
|
|
}
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpCode (DumpState *D, const Proto *f) {
|
|
|
|
dumpInt(D, f->sizecode);
|
2023-08-25 23:40:20 +03:00
|
|
|
dumpAlign(D, sizeof(f->code[0]));
|
2023-12-14 17:41:57 +03:00
|
|
|
lua_assert(f->code != NULL);
|
2024-07-27 19:32:59 +03:00
|
|
|
dumpVector(D, f->code, cast_uint(f->sizecode));
|
2014-02-28 20:13:01 +04:00
|
|
|
}
|
2003-01-27 18:52:57 +03:00
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2023-11-13 19:11:09 +03:00
|
|
|
static void dumpFunction (DumpState *D, const Proto *f);
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpConstants (DumpState *D, const Proto *f) {
|
2014-03-10 21:56:32 +04:00
|
|
|
int i;
|
|
|
|
int n = f->sizek;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, n);
|
2014-03-10 21:56:32 +04:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
const TValue *o = &f->k[i];
|
2020-01-31 17:09:53 +03:00
|
|
|
int tt = ttypetag(o);
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpByte(D, tt);
|
2020-01-31 17:09:53 +03:00
|
|
|
switch (tt) {
|
|
|
|
case LUA_VNUMFLT:
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpNumber(D, fltvalue(o));
|
2017-11-28 14:19:07 +03:00
|
|
|
break;
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VNUMINT:
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInteger(D, ivalue(o));
|
2017-11-28 14:19:07 +03:00
|
|
|
break;
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VSHRSTR:
|
|
|
|
case LUA_VLNGSTR:
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpString(D, tsvalue(o));
|
2017-11-28 14:19:07 +03:00
|
|
|
break;
|
2020-01-31 17:09:53 +03:00
|
|
|
default:
|
|
|
|
lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
|
2014-03-10 21:56:32 +04:00
|
|
|
}
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
2014-06-18 22:35:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpProtos (DumpState *D, const Proto *f) {
|
2014-06-18 22:35:43 +04:00
|
|
|
int i;
|
|
|
|
int n = f->sizep;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, n);
|
2014-06-18 22:35:43 +04:00
|
|
|
for (i = 0; i < n; i++)
|
2022-12-20 17:14:52 +03:00
|
|
|
dumpFunction(D, f->p[i]);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpUpvalues (DumpState *D, const Proto *f) {
|
2014-03-10 21:56:32 +04:00
|
|
|
int i, n = f->sizeupvalues;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, n);
|
2014-03-10 21:56:32 +04:00
|
|
|
for (i = 0; i < n; i++) {
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpByte(D, f->upvalues[i].instack);
|
|
|
|
dumpByte(D, f->upvalues[i].idx);
|
|
|
|
dumpByte(D, f->upvalues[i].kind);
|
2014-03-10 21:56:32 +04:00
|
|
|
}
|
2009-09-28 20:32:50 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpDebug (DumpState *D, const Proto *f) {
|
2014-03-10 21:56:32 +04:00
|
|
|
int i, n;
|
|
|
|
n = (D->strip) ? 0 : f->sizelineinfo;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, n);
|
2023-12-14 17:41:57 +03:00
|
|
|
if (f->lineinfo != NULL)
|
2024-07-27 19:32:59 +03:00
|
|
|
dumpVector(D, f->lineinfo, cast_uint(n));
|
2017-06-27 14:35:31 +03:00
|
|
|
n = (D->strip) ? 0 : f->sizeabslineinfo;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, n);
|
2023-12-27 23:42:00 +03:00
|
|
|
if (n > 0) {
|
|
|
|
/* 'abslineinfo' is an array of structures of int's */
|
|
|
|
dumpAlign(D, sizeof(int));
|
2024-07-27 19:32:59 +03:00
|
|
|
dumpVector(D, f->abslineinfo, cast_uint(n));
|
2017-06-27 14:35:31 +03:00
|
|
|
}
|
2014-03-10 21:56:32 +04:00
|
|
|
n = (D->strip) ? 0 : f->sizelocvars;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, n);
|
2014-03-10 21:56:32 +04:00
|
|
|
for (i = 0; i < n; i++) {
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpString(D, f->locvars[i].varname);
|
|
|
|
dumpInt(D, f->locvars[i].startpc);
|
|
|
|
dumpInt(D, f->locvars[i].endpc);
|
2014-03-10 21:56:32 +04:00
|
|
|
}
|
|
|
|
n = (D->strip) ? 0 : f->sizeupvalues;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, n);
|
2014-03-10 21:56:32 +04:00
|
|
|
for (i = 0; i < n; i++)
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpString(D, f->upvalues[i].name);
|
2005-11-16 14:55:07 +03:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2022-12-20 17:14:52 +03:00
|
|
|
static void dumpFunction (DumpState *D, const Proto *f) {
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, f->linedefined);
|
|
|
|
dumpInt(D, f->lastlinedefined);
|
|
|
|
dumpByte(D, f->numparams);
|
2023-08-30 16:44:28 +03:00
|
|
|
dumpByte(D, f->flag);
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpByte(D, f->maxstacksize);
|
|
|
|
dumpCode(D, f);
|
|
|
|
dumpConstants(D, f);
|
|
|
|
dumpUpvalues(D, f);
|
|
|
|
dumpProtos(D, f);
|
2023-11-13 19:11:09 +03:00
|
|
|
dumpString(D, D->strip ? NULL : f->source);
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpDebug(D, f);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpHeader (DumpState *D) {
|
|
|
|
dumpLiteral(D, LUA_SIGNATURE);
|
2020-05-06 20:19:08 +03:00
|
|
|
dumpByte(D, LUAC_VERSION);
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpByte(D, LUAC_FORMAT);
|
|
|
|
dumpLiteral(D, LUAC_DATA);
|
|
|
|
dumpByte(D, sizeof(Instruction));
|
|
|
|
dumpByte(D, sizeof(lua_Integer));
|
|
|
|
dumpByte(D, sizeof(lua_Number));
|
|
|
|
dumpInteger(D, LUAC_INT);
|
|
|
|
dumpNumber(D, LUAC_NUM);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2003-01-27 18:52:57 +03:00
|
|
|
/*
|
2004-07-09 22:24:41 +04:00
|
|
|
** dump Lua function as precompiled chunk
|
2003-01-27 18:52:57 +03:00
|
|
|
*/
|
2023-12-14 17:41:57 +03:00
|
|
|
int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
|
|
|
|
int strip) {
|
2014-03-10 21:56:32 +04:00
|
|
|
DumpState D;
|
2023-12-14 17:41:57 +03:00
|
|
|
D.h = luaH_new(L); /* aux. table to keep strings already dumped */
|
|
|
|
sethvalue2s(L, L->top.p, D.h); /* anchor it */
|
|
|
|
L->top.p++;
|
2014-03-10 21:56:32 +04:00
|
|
|
D.L = L;
|
|
|
|
D.writer = w;
|
2023-08-25 23:40:20 +03:00
|
|
|
D.offset = 0;
|
2014-03-10 21:56:32 +04:00
|
|
|
D.data = data;
|
|
|
|
D.strip = strip;
|
|
|
|
D.status = 0;
|
2022-12-15 22:44:22 +03:00
|
|
|
D.nstr = 0;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpHeader(&D);
|
|
|
|
dumpByte(&D, f->sizeupvalues);
|
2022-12-20 17:14:52 +03:00
|
|
|
dumpFunction(&D, f);
|
2023-12-14 17:41:57 +03:00
|
|
|
dumpBlock(&D, NULL, 0); /* signal end of dump */
|
2014-03-10 21:56:32 +04:00
|
|
|
return D.status;
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
2014-03-10 21:56:32 +04:00
|
|
|
|