diff options
Diffstat (limited to 'src/lj_bcwrite.c')
-rw-r--r-- | src/lj_bcwrite.c | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/lj_bcwrite.c b/src/lj_bcwrite.c index dd38289e..a8c310b8 100644 --- a/src/lj_bcwrite.c +++ b/src/lj_bcwrite.c | |||
@@ -29,8 +29,17 @@ typedef struct BCWriteCtx { | |||
29 | void *wdata; /* Writer callback data. */ | 29 | void *wdata; /* Writer callback data. */ |
30 | int strip; /* Strip debug info. */ | 30 | int strip; /* Strip debug info. */ |
31 | int status; /* Status from writer callback. */ | 31 | int status; /* Status from writer callback. */ |
32 | #ifdef LUA_USE_ASSERT | ||
33 | global_State *g; | ||
34 | #endif | ||
32 | } BCWriteCtx; | 35 | } BCWriteCtx; |
33 | 36 | ||
37 | #ifdef LUA_USE_ASSERT | ||
38 | #define lj_assertBCW(c, ...) lj_assertG_(ctx->g, (c), __VA_ARGS__) | ||
39 | #else | ||
40 | #define lj_assertBCW(c, ...) ((void)ctx) | ||
41 | #endif | ||
42 | |||
34 | /* -- Bytecode writer ----------------------------------------------------- */ | 43 | /* -- Bytecode writer ----------------------------------------------------- */ |
35 | 44 | ||
36 | /* Write a single constant key/value of a template table. */ | 45 | /* Write a single constant key/value of a template table. */ |
@@ -61,7 +70,7 @@ static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow) | |||
61 | p = lj_strfmt_wuleb128(p, o->u32.lo); | 70 | p = lj_strfmt_wuleb128(p, o->u32.lo); |
62 | p = lj_strfmt_wuleb128(p, o->u32.hi); | 71 | p = lj_strfmt_wuleb128(p, o->u32.hi); |
63 | } else { | 72 | } else { |
64 | lua_assert(tvispri(o)); | 73 | lj_assertBCW(tvispri(o), "unhandled type %d", itype(o)); |
65 | *p++ = BCDUMP_KTAB_NIL+~itype(o); | 74 | *p++ = BCDUMP_KTAB_NIL+~itype(o); |
66 | } | 75 | } |
67 | setsbufP(&ctx->sb, p); | 76 | setsbufP(&ctx->sb, p); |
@@ -121,7 +130,7 @@ static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt) | |||
121 | tp = BCDUMP_KGC_STR + gco2str(o)->len; | 130 | tp = BCDUMP_KGC_STR + gco2str(o)->len; |
122 | need = 5+gco2str(o)->len; | 131 | need = 5+gco2str(o)->len; |
123 | } else if (o->gch.gct == ~LJ_TPROTO) { | 132 | } else if (o->gch.gct == ~LJ_TPROTO) { |
124 | lua_assert((pt->flags & PROTO_CHILD)); | 133 | lj_assertBCW((pt->flags & PROTO_CHILD), "prototype has unexpected child"); |
125 | tp = BCDUMP_KGC_CHILD; | 134 | tp = BCDUMP_KGC_CHILD; |
126 | #if LJ_HASFFI | 135 | #if LJ_HASFFI |
127 | } else if (o->gch.gct == ~LJ_TCDATA) { | 136 | } else if (o->gch.gct == ~LJ_TCDATA) { |
@@ -132,12 +141,14 @@ static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt) | |||
132 | } else if (id == CTID_UINT64) { | 141 | } else if (id == CTID_UINT64) { |
133 | tp = BCDUMP_KGC_U64; | 142 | tp = BCDUMP_KGC_U64; |
134 | } else { | 143 | } else { |
135 | lua_assert(id == CTID_COMPLEX_DOUBLE); | 144 | lj_assertBCW(id == CTID_COMPLEX_DOUBLE, |
145 | "bad cdata constant CTID %d", id); | ||
136 | tp = BCDUMP_KGC_COMPLEX; | 146 | tp = BCDUMP_KGC_COMPLEX; |
137 | } | 147 | } |
138 | #endif | 148 | #endif |
139 | } else { | 149 | } else { |
140 | lua_assert(o->gch.gct == ~LJ_TTAB); | 150 | lj_assertBCW(o->gch.gct == ~LJ_TTAB, |
151 | "bad constant GC type %d", o->gch.gct); | ||
141 | tp = BCDUMP_KGC_TAB; | 152 | tp = BCDUMP_KGC_TAB; |
142 | need = 1+2*5; | 153 | need = 1+2*5; |
143 | } | 154 | } |
@@ -289,7 +300,7 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) | |||
289 | MSize nn = (lj_fls(n)+8)*9 >> 6; | 300 | MSize nn = (lj_fls(n)+8)*9 >> 6; |
290 | char *q = sbufB(&ctx->sb) + (5 - nn); | 301 | char *q = sbufB(&ctx->sb) + (5 - nn); |
291 | p = lj_strfmt_wuleb128(q, n); /* Fill in final size. */ | 302 | p = lj_strfmt_wuleb128(q, n); /* Fill in final size. */ |
292 | lua_assert(p == sbufB(&ctx->sb) + 5); | 303 | lj_assertBCW(p == sbufB(&ctx->sb) + 5, "bad ULEB128 write"); |
293 | ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata); | 304 | ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata); |
294 | } | 305 | } |
295 | } | 306 | } |
@@ -349,6 +360,9 @@ int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data, | |||
349 | ctx.wdata = data; | 360 | ctx.wdata = data; |
350 | ctx.strip = strip; | 361 | ctx.strip = strip; |
351 | ctx.status = 0; | 362 | ctx.status = 0; |
363 | #ifdef LUA_USE_ASSERT | ||
364 | ctx.g = G(L); | ||
365 | #endif | ||
352 | lj_buf_init(L, &ctx.sb); | 366 | lj_buf_init(L, &ctx.sb); |
353 | status = lj_vm_cpcall(L, NULL, &ctx, cpwriter); | 367 | status = lj_vm_cpcall(L, NULL, &ctx, cpwriter); |
354 | if (status == 0) status = ctx.status; | 368 | if (status == 0) status = ctx.status; |