2011-06-12 22:58:13 +00:00
|
|
|
/*
|
|
|
|
** Bytecode writer.
|
2015-01-05 22:59:31 +00:00
|
|
|
** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
|
2011-06-12 22:58:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define lj_bcwrite_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lj_obj.h"
|
|
|
|
#include "lj_gc.h"
|
2013-02-27 16:11:31 +00:00
|
|
|
#include "lj_buf.h"
|
2011-06-12 22:58:13 +00:00
|
|
|
#include "lj_bc.h"
|
|
|
|
#if LJ_HASFFI
|
|
|
|
#include "lj_ctype.h"
|
|
|
|
#endif
|
|
|
|
#if LJ_HASJIT
|
|
|
|
#include "lj_dispatch.h"
|
|
|
|
#include "lj_jit.h"
|
|
|
|
#endif
|
2013-05-13 08:15:07 +00:00
|
|
|
#include "lj_strfmt.h"
|
2011-06-12 22:58:13 +00:00
|
|
|
#include "lj_bcdump.h"
|
|
|
|
#include "lj_vm.h"
|
|
|
|
|
|
|
|
/* Context for bytecode writer. */
|
|
|
|
typedef struct BCWriteCtx {
|
|
|
|
SBuf sb; /* Output buffer. */
|
|
|
|
GCproto *pt; /* Root prototype. */
|
|
|
|
lua_Writer wfunc; /* Writer callback. */
|
|
|
|
void *wdata; /* Writer callback data. */
|
|
|
|
int strip; /* Strip debug info. */
|
|
|
|
int status; /* Status from writer callback. */
|
|
|
|
} BCWriteCtx;
|
|
|
|
|
|
|
|
/* -- Bytecode writer ----------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Write a single constant key/value of a template table. */
|
|
|
|
static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
|
|
|
|
{
|
2013-02-28 12:37:56 +00:00
|
|
|
char *p = lj_buf_more(&ctx->sb, 1+10);
|
2011-06-12 22:58:13 +00:00
|
|
|
if (tvisstr(o)) {
|
|
|
|
const GCstr *str = strV(o);
|
|
|
|
MSize len = str->len;
|
2013-02-28 12:37:56 +00:00
|
|
|
p = lj_buf_more(&ctx->sb, 5+len);
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, BCDUMP_KTAB_STR+len);
|
2013-02-27 20:17:27 +00:00
|
|
|
p = lj_buf_wmem(p, strdata(str), len);
|
2011-06-12 22:58:13 +00:00
|
|
|
} else if (tvisint(o)) {
|
2013-02-27 20:17:27 +00:00
|
|
|
*p++ = BCDUMP_KTAB_INT;
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, intV(o));
|
2011-06-12 22:58:13 +00:00
|
|
|
} else if (tvisnum(o)) {
|
|
|
|
if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */
|
|
|
|
lua_Number num = numV(o);
|
|
|
|
int32_t k = lj_num2int(num);
|
|
|
|
if (num == (lua_Number)k) { /* -0 is never a constant. */
|
2013-02-27 20:17:27 +00:00
|
|
|
*p++ = BCDUMP_KTAB_INT;
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, k);
|
2013-02-27 20:17:27 +00:00
|
|
|
setsbufP(&ctx->sb, p);
|
2011-06-12 22:58:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-02-27 20:17:27 +00:00
|
|
|
*p++ = BCDUMP_KTAB_NUM;
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, o->u32.lo);
|
|
|
|
p = lj_strfmt_wuleb128(p, o->u32.hi);
|
2011-06-12 22:58:13 +00:00
|
|
|
} else {
|
|
|
|
lua_assert(tvispri(o));
|
2013-02-27 20:17:27 +00:00
|
|
|
*p++ = BCDUMP_KTAB_NIL+~itype(o);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
2013-02-27 20:17:27 +00:00
|
|
|
setsbufP(&ctx->sb, p);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a template table. */
|
2013-02-27 20:17:27 +00:00
|
|
|
static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t)
|
2011-06-12 22:58:13 +00:00
|
|
|
{
|
|
|
|
MSize narray = 0, nhash = 0;
|
|
|
|
if (t->asize > 0) { /* Determine max. length of array part. */
|
|
|
|
ptrdiff_t i;
|
|
|
|
TValue *array = tvref(t->array);
|
|
|
|
for (i = (ptrdiff_t)t->asize-1; i >= 0; i--)
|
|
|
|
if (!tvisnil(&array[i]))
|
|
|
|
break;
|
|
|
|
narray = (MSize)(i+1);
|
|
|
|
}
|
|
|
|
if (t->hmask > 0) { /* Count number of used hash slots. */
|
|
|
|
MSize i, hmask = t->hmask;
|
|
|
|
Node *node = noderef(t->node);
|
|
|
|
for (i = 0; i <= hmask; i++)
|
|
|
|
nhash += !tvisnil(&node[i].val);
|
|
|
|
}
|
|
|
|
/* Write number of array slots and hash slots. */
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, narray);
|
|
|
|
p = lj_strfmt_wuleb128(p, nhash);
|
2013-02-27 20:17:27 +00:00
|
|
|
setsbufP(&ctx->sb, p);
|
2011-06-12 22:58:13 +00:00
|
|
|
if (narray) { /* Write array entries (may contain nil). */
|
|
|
|
MSize i;
|
|
|
|
TValue *o = tvref(t->array);
|
|
|
|
for (i = 0; i < narray; i++, o++)
|
|
|
|
bcwrite_ktabk(ctx, o, 1);
|
|
|
|
}
|
|
|
|
if (nhash) { /* Write hash entries. */
|
|
|
|
MSize i = nhash;
|
|
|
|
Node *node = noderef(t->node) + t->hmask;
|
|
|
|
for (;; node--)
|
|
|
|
if (!tvisnil(&node->val)) {
|
|
|
|
bcwrite_ktabk(ctx, &node->key, 0);
|
|
|
|
bcwrite_ktabk(ctx, &node->val, 1);
|
|
|
|
if (--i == 0) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write GC constants of a prototype. */
|
|
|
|
static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
|
|
|
|
{
|
|
|
|
MSize i, sizekgc = pt->sizekgc;
|
|
|
|
GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;
|
|
|
|
for (i = 0; i < sizekgc; i++, kr++) {
|
|
|
|
GCobj *o = gcref(*kr);
|
|
|
|
MSize tp, need = 1;
|
2013-02-27 20:17:27 +00:00
|
|
|
char *p;
|
2011-06-12 22:58:13 +00:00
|
|
|
/* Determine constant type and needed size. */
|
|
|
|
if (o->gch.gct == ~LJ_TSTR) {
|
|
|
|
tp = BCDUMP_KGC_STR + gco2str(o)->len;
|
|
|
|
need = 5+gco2str(o)->len;
|
|
|
|
} else if (o->gch.gct == ~LJ_TPROTO) {
|
|
|
|
lua_assert((pt->flags & PROTO_CHILD));
|
|
|
|
tp = BCDUMP_KGC_CHILD;
|
|
|
|
#if LJ_HASFFI
|
|
|
|
} else if (o->gch.gct == ~LJ_TCDATA) {
|
2012-07-03 11:19:32 +00:00
|
|
|
CTypeID id = gco2cd(o)->ctypeid;
|
2011-06-12 22:58:13 +00:00
|
|
|
need = 1+4*5;
|
|
|
|
if (id == CTID_INT64) {
|
|
|
|
tp = BCDUMP_KGC_I64;
|
|
|
|
} else if (id == CTID_UINT64) {
|
|
|
|
tp = BCDUMP_KGC_U64;
|
|
|
|
} else {
|
|
|
|
lua_assert(id == CTID_COMPLEX_DOUBLE);
|
|
|
|
tp = BCDUMP_KGC_COMPLEX;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
lua_assert(o->gch.gct == ~LJ_TTAB);
|
|
|
|
tp = BCDUMP_KGC_TAB;
|
2012-06-28 13:13:26 +00:00
|
|
|
need = 1+2*5;
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
/* Write constant type. */
|
2013-02-28 12:37:56 +00:00
|
|
|
p = lj_buf_more(&ctx->sb, need);
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, tp);
|
2011-06-12 22:58:13 +00:00
|
|
|
/* Write constant data (if any). */
|
|
|
|
if (tp >= BCDUMP_KGC_STR) {
|
2013-02-27 20:17:27 +00:00
|
|
|
p = lj_buf_wmem(p, strdata(gco2str(o)), gco2str(o)->len);
|
2011-06-12 22:58:13 +00:00
|
|
|
} else if (tp == BCDUMP_KGC_TAB) {
|
2013-02-27 20:17:27 +00:00
|
|
|
bcwrite_ktab(ctx, p, gco2tab(o));
|
|
|
|
continue;
|
2011-06-12 22:58:13 +00:00
|
|
|
#if LJ_HASFFI
|
|
|
|
} else if (tp != BCDUMP_KGC_CHILD) {
|
2013-02-27 20:17:27 +00:00
|
|
|
cTValue *q = (TValue *)cdataptr(gco2cd(o));
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, q[0].u32.lo);
|
|
|
|
p = lj_strfmt_wuleb128(p, q[0].u32.hi);
|
2011-06-12 22:58:13 +00:00
|
|
|
if (tp == BCDUMP_KGC_COMPLEX) {
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, q[1].u32.lo);
|
|
|
|
p = lj_strfmt_wuleb128(p, q[1].u32.hi);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-02-27 20:17:27 +00:00
|
|
|
setsbufP(&ctx->sb, p);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write number constants of a prototype. */
|
|
|
|
static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
|
|
|
|
{
|
|
|
|
MSize i, sizekn = pt->sizekn;
|
|
|
|
cTValue *o = mref(pt->k, TValue);
|
2013-02-28 12:37:56 +00:00
|
|
|
char *p = lj_buf_more(&ctx->sb, 10*sizekn);
|
2011-06-12 22:58:13 +00:00
|
|
|
for (i = 0; i < sizekn; i++, o++) {
|
|
|
|
int32_t k;
|
|
|
|
if (tvisint(o)) {
|
|
|
|
k = intV(o);
|
|
|
|
goto save_int;
|
|
|
|
} else {
|
|
|
|
/* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */
|
|
|
|
if (!LJ_DUALNUM) { /* Narrow number constants to integers. */
|
|
|
|
lua_Number num = numV(o);
|
|
|
|
k = lj_num2int(num);
|
|
|
|
if (num == (lua_Number)k) { /* -0 is never a constant. */
|
|
|
|
save_int:
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k&0x80000000u));
|
2013-02-27 20:17:27 +00:00
|
|
|
if (k < 0)
|
|
|
|
p[-1] = (p[-1] & 7) | ((k>>27) & 0x18);
|
2011-06-12 22:58:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
|
2013-02-27 20:17:27 +00:00
|
|
|
if (o->u32.lo >= 0x80000000u)
|
|
|
|
p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18);
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, o->u32.hi);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-27 20:17:27 +00:00
|
|
|
setsbufP(&ctx->sb, p);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write bytecode instructions. */
|
2013-02-27 20:17:27 +00:00
|
|
|
static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt)
|
2011-06-12 22:58:13 +00:00
|
|
|
{
|
|
|
|
MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */
|
|
|
|
#if LJ_HASJIT
|
2013-02-27 20:17:27 +00:00
|
|
|
uint8_t *q = (uint8_t *)p;
|
2011-06-12 22:58:13 +00:00
|
|
|
#endif
|
2013-02-27 20:17:27 +00:00
|
|
|
p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
|
|
|
|
UNUSED(ctx);
|
2011-06-12 22:58:13 +00:00
|
|
|
#if LJ_HASJIT
|
|
|
|
/* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
|
|
|
|
if ((pt->flags & PROTO_ILOOP) || pt->trace) {
|
2013-02-28 12:37:56 +00:00
|
|
|
jit_State *J = L2J(sbufL(&ctx->sb));
|
2011-06-12 22:58:13 +00:00
|
|
|
MSize i;
|
2013-02-27 20:17:27 +00:00
|
|
|
for (i = 0; i < nbc; i++, q += sizeof(BCIns)) {
|
|
|
|
BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)];
|
2011-06-12 22:58:13 +00:00
|
|
|
if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
|
|
|
|
op == BC_JFORI) {
|
2013-02-27 20:17:27 +00:00
|
|
|
q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
|
2011-06-12 22:58:13 +00:00
|
|
|
} else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
|
2013-02-27 20:17:27 +00:00
|
|
|
BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8);
|
2011-06-12 22:58:13 +00:00
|
|
|
BCIns ins = traceref(J, rd)->startins;
|
2013-02-27 20:17:27 +00:00
|
|
|
q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL);
|
|
|
|
q[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins);
|
|
|
|
q[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-02-27 20:17:27 +00:00
|
|
|
return p;
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write prototype. */
|
|
|
|
static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
|
|
|
|
{
|
|
|
|
MSize sizedbg = 0;
|
2013-02-27 20:17:27 +00:00
|
|
|
char *p;
|
2011-06-12 22:58:13 +00:00
|
|
|
|
|
|
|
/* Recursively write children of prototype. */
|
|
|
|
if ((pt->flags & PROTO_CHILD)) {
|
|
|
|
ptrdiff_t i, n = pt->sizekgc;
|
|
|
|
GCRef *kr = mref(pt->k, GCRef) - 1;
|
|
|
|
for (i = 0; i < n; i++, kr--) {
|
|
|
|
GCobj *o = gcref(*kr);
|
|
|
|
if (o->gch.gct == ~LJ_TPROTO)
|
|
|
|
bcwrite_proto(ctx, gco2pt(o));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start writing the prototype info to a buffer. */
|
2013-02-28 12:37:56 +00:00
|
|
|
p = lj_buf_need(&ctx->sb,
|
2013-02-27 20:17:27 +00:00
|
|
|
5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
|
|
|
|
p += 5; /* Leave room for final size. */
|
2011-06-12 22:58:13 +00:00
|
|
|
|
|
|
|
/* Write prototype header. */
|
2013-02-27 20:17:27 +00:00
|
|
|
*p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));
|
|
|
|
*p++ = pt->numparams;
|
|
|
|
*p++ = pt->framesize;
|
|
|
|
*p++ = pt->sizeuv;
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, pt->sizekgc);
|
|
|
|
p = lj_strfmt_wuleb128(p, pt->sizekn);
|
|
|
|
p = lj_strfmt_wuleb128(p, pt->sizebc-1);
|
2011-06-12 22:58:13 +00:00
|
|
|
if (!ctx->strip) {
|
2011-06-13 01:22:10 +00:00
|
|
|
if (proto_lineinfo(pt))
|
|
|
|
sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, sizedbg);
|
2011-06-12 22:58:13 +00:00
|
|
|
if (sizedbg) {
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, pt->firstline);
|
|
|
|
p = lj_strfmt_wuleb128(p, pt->numline);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write bytecode instructions and upvalue refs. */
|
2013-02-27 20:17:27 +00:00
|
|
|
p = bcwrite_bytecode(ctx, p, pt);
|
|
|
|
p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2);
|
|
|
|
setsbufP(&ctx->sb, p);
|
2011-06-12 22:58:13 +00:00
|
|
|
|
|
|
|
/* Write constants. */
|
|
|
|
bcwrite_kgc(ctx, pt);
|
|
|
|
bcwrite_knum(ctx, pt);
|
|
|
|
|
|
|
|
/* Write debug info, if not stripped. */
|
|
|
|
if (sizedbg) {
|
2013-02-28 12:37:56 +00:00
|
|
|
p = lj_buf_more(&ctx->sb, sizedbg);
|
2013-02-27 20:17:27 +00:00
|
|
|
p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg);
|
|
|
|
setsbufP(&ctx->sb, p);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pass buffer to writer function. */
|
|
|
|
if (ctx->status == 0) {
|
2013-02-27 20:17:27 +00:00
|
|
|
MSize n = sbuflen(&ctx->sb) - 5;
|
2012-08-26 17:41:35 +00:00
|
|
|
MSize nn = (lj_fls(n)+8)*9 >> 6;
|
2013-02-27 20:17:27 +00:00
|
|
|
char *q = sbufB(&ctx->sb) + (5 - nn);
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(q, n); /* Fill in final size. */
|
2013-02-27 20:17:27 +00:00
|
|
|
lua_assert(p == sbufB(&ctx->sb) + 5);
|
2013-02-28 12:37:56 +00:00
|
|
|
ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write header of bytecode dump. */
|
|
|
|
static void bcwrite_header(BCWriteCtx *ctx)
|
|
|
|
{
|
|
|
|
GCstr *chunkname = proto_chunkname(ctx->pt);
|
|
|
|
const char *name = strdata(chunkname);
|
|
|
|
MSize len = chunkname->len;
|
2013-02-28 12:37:56 +00:00
|
|
|
char *p = lj_buf_need(&ctx->sb, 5+5+len);
|
2013-02-27 20:17:27 +00:00
|
|
|
*p++ = BCDUMP_HEAD1;
|
|
|
|
*p++ = BCDUMP_HEAD2;
|
|
|
|
*p++ = BCDUMP_HEAD3;
|
|
|
|
*p++ = BCDUMP_VERSION;
|
|
|
|
*p++ = (ctx->strip ? BCDUMP_F_STRIP : 0) +
|
2015-01-03 14:04:38 +00:00
|
|
|
LJ_BE*BCDUMP_F_BE +
|
|
|
|
((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0) +
|
|
|
|
LJ_FR2*BCDUMP_F_FR2;
|
2011-06-12 22:58:13 +00:00
|
|
|
if (!ctx->strip) {
|
2013-05-13 08:15:07 +00:00
|
|
|
p = lj_strfmt_wuleb128(p, len);
|
2013-02-27 20:17:27 +00:00
|
|
|
p = lj_buf_wmem(p, name, len);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
2013-02-28 12:37:56 +00:00
|
|
|
ctx->status = ctx->wfunc(sbufL(&ctx->sb), sbufB(&ctx->sb),
|
2013-02-27 20:17:27 +00:00
|
|
|
(MSize)(p - sbufB(&ctx->sb)), ctx->wdata);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write footer of bytecode dump. */
|
|
|
|
static void bcwrite_footer(BCWriteCtx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->status == 0) {
|
|
|
|
uint8_t zero = 0;
|
2013-02-28 12:37:56 +00:00
|
|
|
ctx->status = ctx->wfunc(sbufL(&ctx->sb), &zero, 1, ctx->wdata);
|
2011-06-12 22:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Protected callback for bytecode writer. */
|
|
|
|
static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
|
|
|
|
{
|
|
|
|
BCWriteCtx *ctx = (BCWriteCtx *)ud;
|
2013-02-28 12:37:56 +00:00
|
|
|
UNUSED(L); UNUSED(dummy);
|
|
|
|
lj_buf_need(&ctx->sb, 1024); /* Avoids resize for most prototypes. */
|
2011-06-12 22:58:13 +00:00
|
|
|
bcwrite_header(ctx);
|
|
|
|
bcwrite_proto(ctx, ctx->pt);
|
|
|
|
bcwrite_footer(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write bytecode for a prototype. */
|
|
|
|
int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,
|
|
|
|
int strip)
|
|
|
|
{
|
|
|
|
BCWriteCtx ctx;
|
|
|
|
int status;
|
|
|
|
ctx.pt = pt;
|
|
|
|
ctx.wfunc = writer;
|
|
|
|
ctx.wdata = data;
|
|
|
|
ctx.strip = strip;
|
|
|
|
ctx.status = 0;
|
2013-02-28 12:37:56 +00:00
|
|
|
lj_buf_init(L, &ctx.sb);
|
2011-06-12 22:58:13 +00:00
|
|
|
status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);
|
|
|
|
if (status == 0) status = ctx.status;
|
2013-02-28 12:37:56 +00:00
|
|
|
lj_buf_free(G(sbufL(&ctx.sb)), &ctx.sb);
|
2011-06-12 22:58:13 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|