2002-10-26 01:31:28 +04:00
|
|
|
/*
|
2015-03-30 18:43:51 +03:00
|
|
|
** $Id: ldump.c,v 2.35 2015/01/16 16:54:37 roberto Exp roberto $
|
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"
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
/*
|
|
|
|
** 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
|
|
|
*/
|
2014-02-28 20:13:01 +04:00
|
|
|
#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
|
2005-11-16 14:55:07 +03:00
|
|
|
|
2014-03-11 22:56:27 +04:00
|
|
|
#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpBlock (const void *b, size_t size, DumpState *D) {
|
|
|
|
if (D->status == 0) {
|
|
|
|
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
|
|
|
|
2014-03-11 22:56:27 +04:00
|
|
|
#define DumpVar(x,D) DumpVector(&x,1,D)
|
|
|
|
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
static void DumpByte (int y, DumpState *D) {
|
|
|
|
lu_byte x = (lu_byte)y;
|
|
|
|
DumpVar(x, D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpInt (int x, DumpState *D) {
|
|
|
|
DumpVar(x, D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpNumber (lua_Number x, DumpState *D) {
|
|
|
|
DumpVar(x, D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpInteger (lua_Integer x, DumpState *D) {
|
|
|
|
DumpVar(x, D);
|
2013-04-26 22:48:35 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpString (const TString *s, DumpState *D) {
|
|
|
|
if (s == NULL)
|
|
|
|
DumpByte(0, D);
|
|
|
|
else {
|
2015-01-16 19:54:37 +03:00
|
|
|
size_t size = tsslen(s) + 1; /* include trailing '\0' */
|
2015-03-30 18:43:51 +03:00
|
|
|
const char *str = getstr(s);
|
2014-03-10 21:56:32 +04:00
|
|
|
if (size < 0xFF)
|
2014-04-01 18:39:55 +04:00
|
|
|
DumpByte(cast_int(size), D);
|
2014-03-10 21:56:32 +04:00
|
|
|
else {
|
|
|
|
DumpByte(0xFF, D);
|
|
|
|
DumpVar(size, D);
|
|
|
|
}
|
2015-03-30 18:43:51 +03:00
|
|
|
DumpVector(str, size - 1, D); /* no need to save '\0' */
|
2014-03-01 19:18:44 +04:00
|
|
|
}
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpCode (const Proto *f, DumpState *D) {
|
|
|
|
DumpInt(f->sizecode, D);
|
|
|
|
DumpVector(f->code, f->sizecode, D);
|
2014-02-28 20:13:01 +04:00
|
|
|
}
|
2003-01-27 18:52:57 +03:00
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2014-06-18 22:35:43 +04:00
|
|
|
static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpConstants (const Proto *f, DumpState *D) {
|
|
|
|
int i;
|
|
|
|
int n = f->sizek;
|
|
|
|
DumpInt(n, D);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
const TValue *o = &f->k[i];
|
|
|
|
DumpByte(ttype(o), D);
|
|
|
|
switch (ttype(o)) {
|
|
|
|
case LUA_TNIL:
|
|
|
|
break;
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
DumpByte(bvalue(o), D);
|
|
|
|
break;
|
|
|
|
case LUA_TNUMFLT:
|
|
|
|
DumpNumber(fltvalue(o), D);
|
|
|
|
break;
|
|
|
|
case LUA_TNUMINT:
|
|
|
|
DumpInteger(ivalue(o), D);
|
|
|
|
break;
|
|
|
|
case LUA_TSHRSTR:
|
|
|
|
case LUA_TLNGSTR:
|
2014-07-18 17:36:14 +04:00
|
|
|
DumpString(tsvalue(o), D);
|
2014-03-10 21:56:32 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_assert(0);
|
|
|
|
}
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
2014-06-18 22:35:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void DumpProtos (const Proto *f, DumpState *D) {
|
|
|
|
int i;
|
|
|
|
int n = f->sizep;
|
2014-03-10 21:56:32 +04:00
|
|
|
DumpInt(n, D);
|
2014-06-18 22:35:43 +04:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
DumpFunction(f->p[i], f->source, D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpUpvalues (const Proto *f, DumpState *D) {
|
|
|
|
int i, n = f->sizeupvalues;
|
|
|
|
DumpInt(n, D);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
DumpByte(f->upvalues[i].instack, D);
|
|
|
|
DumpByte(f->upvalues[i].idx, D);
|
|
|
|
}
|
2009-09-28 20:32:50 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpDebug (const Proto *f, DumpState *D) {
|
|
|
|
int i, n;
|
|
|
|
n = (D->strip) ? 0 : f->sizelineinfo;
|
|
|
|
DumpInt(n, D);
|
|
|
|
DumpVector(f->lineinfo, n, D);
|
|
|
|
n = (D->strip) ? 0 : f->sizelocvars;
|
|
|
|
DumpInt(n, D);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
DumpString(f->locvars[i].varname, D);
|
|
|
|
DumpInt(f->locvars[i].startpc, D);
|
|
|
|
DumpInt(f->locvars[i].endpc, D);
|
|
|
|
}
|
|
|
|
n = (D->strip) ? 0 : f->sizeupvalues;
|
|
|
|
DumpInt(n, D);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
DumpString(f->upvalues[i].name, D);
|
2005-11-16 14:55:07 +03:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
2014-06-18 22:35:43 +04:00
|
|
|
static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
|
|
|
|
if (D->strip || f->source == psource)
|
|
|
|
DumpString(NULL, D); /* no debug info or same source as its parent */
|
|
|
|
else
|
|
|
|
DumpString(f->source, D);
|
2014-03-10 21:56:32 +04:00
|
|
|
DumpInt(f->linedefined, D);
|
|
|
|
DumpInt(f->lastlinedefined, D);
|
|
|
|
DumpByte(f->numparams, D);
|
|
|
|
DumpByte(f->is_vararg, D);
|
|
|
|
DumpByte(f->maxstacksize, D);
|
|
|
|
DumpCode(f, D);
|
|
|
|
DumpConstants(f, D);
|
|
|
|
DumpUpvalues(f, D);
|
2014-06-18 22:35:43 +04:00
|
|
|
DumpProtos(f, D);
|
2014-03-10 21:56:32 +04:00
|
|
|
DumpDebug(f, D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void DumpHeader (DumpState *D) {
|
2014-03-11 22:05:46 +04:00
|
|
|
DumpLiteral(LUA_SIGNATURE, D);
|
2014-03-10 21:56:32 +04:00
|
|
|
DumpByte(LUAC_VERSION, D);
|
|
|
|
DumpByte(LUAC_FORMAT, D);
|
2014-03-11 22:05:46 +04:00
|
|
|
DumpLiteral(LUAC_DATA, D);
|
2014-03-10 21:56:32 +04:00
|
|
|
DumpByte(sizeof(int), D);
|
|
|
|
DumpByte(sizeof(size_t), D);
|
|
|
|
DumpByte(sizeof(Instruction), D);
|
|
|
|
DumpByte(sizeof(lua_Integer), D);
|
|
|
|
DumpByte(sizeof(lua_Number), D);
|
|
|
|
DumpInteger(LUAC_INT, D);
|
|
|
|
DumpNumber(LUAC_NUM, D);
|
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;
|
|
|
|
DumpHeader(&D);
|
|
|
|
DumpByte(f->sizeupvalues, &D);
|
2014-06-18 22:35:43 +04:00
|
|
|
DumpFunction(f, NULL, &D);
|
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
|
|
|
|