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"
|
|
|
|
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#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;
|
|
|
|
int strip;
|
|
|
|
int status;
|
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
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpBlock (DumpState *D, const void *b, size_t size) {
|
2015-10-08 18:53:49 +03:00
|
|
|
if (D->status == 0 && size > 0) {
|
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);
|
|
|
|
}
|
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
|
|
|
/*
|
|
|
|
** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6"
|
|
|
|
** rounds up the division.)
|
|
|
|
*/
|
|
|
|
#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7)
|
2017-06-27 17:21:12 +03:00
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpSize (DumpState *D, size_t x) {
|
2017-06-27 17:21:12 +03:00
|
|
|
lu_byte buff[DIBS];
|
|
|
|
int n = 0;
|
|
|
|
do {
|
|
|
|
buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */
|
|
|
|
x >>= 7;
|
|
|
|
} while (x != 0);
|
|
|
|
buff[DIBS - 1] |= 0x80; /* mark last byte */
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpVector(D, buff + DIBS - n, n);
|
2017-06-27 17:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpInt (DumpState *D, int x) {
|
|
|
|
dumpSize(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 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
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpString (DumpState *D, const TString *s) {
|
2014-03-10 21:56:32 +04:00
|
|
|
if (s == NULL)
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpSize(D, 0);
|
2014-03-10 21:56:32 +04:00
|
|
|
else {
|
2017-06-27 17:21:12 +03:00
|
|
|
size_t size = tsslen(s);
|
2015-03-30 18:43:51 +03:00
|
|
|
const char *str = getstr(s);
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpSize(D, size + 1);
|
|
|
|
dumpVector(D, str, size);
|
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);
|
|
|
|
dumpVector(D, f->code, 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
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
|
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++)
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpFunction(D, f->p[i], f->source);
|
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);
|
|
|
|
dumpVector(D, f->lineinfo, 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);
|
2017-06-27 14:35:31 +03:00
|
|
|
for (i = 0; i < n; i++) {
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpInt(D, f->abslineinfo[i].pc);
|
|
|
|
dumpInt(D, f->abslineinfo[i].line);
|
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
|
|
|
|
2020-02-27 21:17:44 +03:00
|
|
|
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
|
2014-06-18 22:35:43 +04:00
|
|
|
if (D->strip || f->source == psource)
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpString(D, NULL); /* no debug info or same source as its parent */
|
2014-06-18 22:35:43 +04:00
|
|
|
else
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpString(D, f->source);
|
|
|
|
dumpInt(D, f->linedefined);
|
|
|
|
dumpInt(D, f->lastlinedefined);
|
|
|
|
dumpByte(D, f->numparams);
|
|
|
|
dumpByte(D, f->is_vararg);
|
|
|
|
dumpByte(D, f->maxstacksize);
|
|
|
|
dumpCode(D, f);
|
|
|
|
dumpConstants(D, f);
|
|
|
|
dumpUpvalues(D, f);
|
|
|
|
dumpProtos(D, f);
|
|
|
|
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
|
|
|
*/
|
2014-03-10 21:56:32 +04:00
|
|
|
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
|
|
|
|
int strip) {
|
|
|
|
DumpState D;
|
|
|
|
D.L = L;
|
|
|
|
D.writer = w;
|
|
|
|
D.data = data;
|
|
|
|
D.strip = strip;
|
|
|
|
D.status = 0;
|
2020-02-27 21:17:44 +03:00
|
|
|
dumpHeader(&D);
|
|
|
|
dumpByte(&D, f->sizeupvalues);
|
|
|
|
dumpFunction(&D, f, NULL);
|
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
|
|
|
|