diff options
author | Mike Pall <mike> | 2013-02-28 13:37:56 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2013-02-28 13:43:37 +0100 |
commit | 9ec869b3620e9508b2c17ad67285c5f54ab12e88 (patch) | |
tree | 71d1e2186134d506a78bccb186ab28df0495ba55 /src/lj_bcwrite.c | |
parent | 3c0157f4262813efd0b0d8284810ff5a9e71d96a (diff) | |
download | luajit-9ec869b3620e9508b2c17ad67285c5f54ab12e88.tar.gz luajit-9ec869b3620e9508b2c17ad67285c5f54ab12e88.tar.bz2 luajit-9ec869b3620e9508b2c17ad67285c5f54ab12e88.zip |
String buffer refactoring, part 4.
Add lua_State pointer to SBuf for buffer resizing.
Diffstat (limited to 'src/lj_bcwrite.c')
-rw-r--r-- | src/lj_bcwrite.c | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/src/lj_bcwrite.c b/src/lj_bcwrite.c index 71c86581..b3289a13 100644 --- a/src/lj_bcwrite.c +++ b/src/lj_bcwrite.c | |||
@@ -24,7 +24,6 @@ | |||
24 | /* Context for bytecode writer. */ | 24 | /* Context for bytecode writer. */ |
25 | typedef struct BCWriteCtx { | 25 | typedef struct BCWriteCtx { |
26 | SBuf sb; /* Output buffer. */ | 26 | SBuf sb; /* Output buffer. */ |
27 | lua_State *L; /* Lua state. */ | ||
28 | GCproto *pt; /* Root prototype. */ | 27 | GCproto *pt; /* Root prototype. */ |
29 | lua_Writer wfunc; /* Writer callback. */ | 28 | lua_Writer wfunc; /* Writer callback. */ |
30 | void *wdata; /* Writer callback data. */ | 29 | void *wdata; /* Writer callback data. */ |
@@ -37,11 +36,11 @@ typedef struct BCWriteCtx { | |||
37 | /* Write a single constant key/value of a template table. */ | 36 | /* Write a single constant key/value of a template table. */ |
38 | static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow) | 37 | static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow) |
39 | { | 38 | { |
40 | char *p = lj_buf_more(ctx->L, &ctx->sb, 1+10); | 39 | char *p = lj_buf_more(&ctx->sb, 1+10); |
41 | if (tvisstr(o)) { | 40 | if (tvisstr(o)) { |
42 | const GCstr *str = strV(o); | 41 | const GCstr *str = strV(o); |
43 | MSize len = str->len; | 42 | MSize len = str->len; |
44 | p = lj_buf_more(ctx->L, &ctx->sb, 5+len); | 43 | p = lj_buf_more(&ctx->sb, 5+len); |
45 | p = lj_buf_wuleb128(p, BCDUMP_KTAB_STR+len); | 44 | p = lj_buf_wuleb128(p, BCDUMP_KTAB_STR+len); |
46 | p = lj_buf_wmem(p, strdata(str), len); | 45 | p = lj_buf_wmem(p, strdata(str), len); |
47 | } else if (tvisint(o)) { | 46 | } else if (tvisint(o)) { |
@@ -143,7 +142,7 @@ static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt) | |||
143 | need = 1+2*5; | 142 | need = 1+2*5; |
144 | } | 143 | } |
145 | /* Write constant type. */ | 144 | /* Write constant type. */ |
146 | p = lj_buf_more(ctx->L, &ctx->sb, need); | 145 | p = lj_buf_more(&ctx->sb, need); |
147 | p = lj_buf_wuleb128(p, tp); | 146 | p = lj_buf_wuleb128(p, tp); |
148 | /* Write constant data (if any). */ | 147 | /* Write constant data (if any). */ |
149 | if (tp >= BCDUMP_KGC_STR) { | 148 | if (tp >= BCDUMP_KGC_STR) { |
@@ -171,7 +170,7 @@ static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt) | |||
171 | { | 170 | { |
172 | MSize i, sizekn = pt->sizekn; | 171 | MSize i, sizekn = pt->sizekn; |
173 | cTValue *o = mref(pt->k, TValue); | 172 | cTValue *o = mref(pt->k, TValue); |
174 | char *p = lj_buf_more(ctx->L, &ctx->sb, 10*sizekn); | 173 | char *p = lj_buf_more(&ctx->sb, 10*sizekn); |
175 | for (i = 0; i < sizekn; i++, o++) { | 174 | for (i = 0; i < sizekn; i++, o++) { |
176 | int32_t k; | 175 | int32_t k; |
177 | if (tvisint(o)) { | 176 | if (tvisint(o)) { |
@@ -211,7 +210,7 @@ static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt) | |||
211 | #if LJ_HASJIT | 210 | #if LJ_HASJIT |
212 | /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */ | 211 | /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */ |
213 | if ((pt->flags & PROTO_ILOOP) || pt->trace) { | 212 | if ((pt->flags & PROTO_ILOOP) || pt->trace) { |
214 | jit_State *J = L2J(ctx->L); | 213 | jit_State *J = L2J(sbufL(&ctx->sb)); |
215 | MSize i; | 214 | MSize i; |
216 | for (i = 0; i < nbc; i++, q += sizeof(BCIns)) { | 215 | for (i = 0; i < nbc; i++, q += sizeof(BCIns)) { |
217 | BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)]; | 216 | BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)]; |
@@ -249,7 +248,7 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) | |||
249 | } | 248 | } |
250 | 249 | ||
251 | /* Start writing the prototype info to a buffer. */ | 250 | /* Start writing the prototype info to a buffer. */ |
252 | p = lj_buf_need(ctx->L, &ctx->sb, | 251 | p = lj_buf_need(&ctx->sb, |
253 | 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2); | 252 | 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2); |
254 | p += 5; /* Leave room for final size. */ | 253 | p += 5; /* Leave room for final size. */ |
255 | 254 | ||
@@ -282,7 +281,7 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) | |||
282 | 281 | ||
283 | /* Write debug info, if not stripped. */ | 282 | /* Write debug info, if not stripped. */ |
284 | if (sizedbg) { | 283 | if (sizedbg) { |
285 | p = lj_buf_more(ctx->L, &ctx->sb, sizedbg); | 284 | p = lj_buf_more(&ctx->sb, sizedbg); |
286 | p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg); | 285 | p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg); |
287 | setsbufP(&ctx->sb, p); | 286 | setsbufP(&ctx->sb, p); |
288 | } | 287 | } |
@@ -294,7 +293,7 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) | |||
294 | char *q = sbufB(&ctx->sb) + (5 - nn); | 293 | char *q = sbufB(&ctx->sb) + (5 - nn); |
295 | p = lj_buf_wuleb128(q, n); /* Fill in final size. */ | 294 | p = lj_buf_wuleb128(q, n); /* Fill in final size. */ |
296 | lua_assert(p == sbufB(&ctx->sb) + 5); | 295 | lua_assert(p == sbufB(&ctx->sb) + 5); |
297 | ctx->status = ctx->wfunc(ctx->L, q, nn+n, ctx->wdata); | 296 | ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata); |
298 | } | 297 | } |
299 | } | 298 | } |
300 | 299 | ||
@@ -304,7 +303,7 @@ static void bcwrite_header(BCWriteCtx *ctx) | |||
304 | GCstr *chunkname = proto_chunkname(ctx->pt); | 303 | GCstr *chunkname = proto_chunkname(ctx->pt); |
305 | const char *name = strdata(chunkname); | 304 | const char *name = strdata(chunkname); |
306 | MSize len = chunkname->len; | 305 | MSize len = chunkname->len; |
307 | char *p = lj_buf_need(ctx->L, &ctx->sb, 5+5+len); | 306 | char *p = lj_buf_need(&ctx->sb, 5+5+len); |
308 | *p++ = BCDUMP_HEAD1; | 307 | *p++ = BCDUMP_HEAD1; |
309 | *p++ = BCDUMP_HEAD2; | 308 | *p++ = BCDUMP_HEAD2; |
310 | *p++ = BCDUMP_HEAD3; | 309 | *p++ = BCDUMP_HEAD3; |
@@ -316,7 +315,7 @@ static void bcwrite_header(BCWriteCtx *ctx) | |||
316 | p = lj_buf_wuleb128(p, len); | 315 | p = lj_buf_wuleb128(p, len); |
317 | p = lj_buf_wmem(p, name, len); | 316 | p = lj_buf_wmem(p, name, len); |
318 | } | 317 | } |
319 | ctx->status = ctx->wfunc(ctx->L, sbufB(&ctx->sb), | 318 | ctx->status = ctx->wfunc(sbufL(&ctx->sb), sbufB(&ctx->sb), |
320 | (MSize)(p - sbufB(&ctx->sb)), ctx->wdata); | 319 | (MSize)(p - sbufB(&ctx->sb)), ctx->wdata); |
321 | } | 320 | } |
322 | 321 | ||
@@ -325,7 +324,7 @@ static void bcwrite_footer(BCWriteCtx *ctx) | |||
325 | { | 324 | { |
326 | if (ctx->status == 0) { | 325 | if (ctx->status == 0) { |
327 | uint8_t zero = 0; | 326 | uint8_t zero = 0; |
328 | ctx->status = ctx->wfunc(ctx->L, &zero, 1, ctx->wdata); | 327 | ctx->status = ctx->wfunc(sbufL(&ctx->sb), &zero, 1, ctx->wdata); |
329 | } | 328 | } |
330 | } | 329 | } |
331 | 330 | ||
@@ -333,8 +332,8 @@ static void bcwrite_footer(BCWriteCtx *ctx) | |||
333 | static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud) | 332 | static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud) |
334 | { | 333 | { |
335 | BCWriteCtx *ctx = (BCWriteCtx *)ud; | 334 | BCWriteCtx *ctx = (BCWriteCtx *)ud; |
336 | UNUSED(dummy); | 335 | UNUSED(L); UNUSED(dummy); |
337 | lj_buf_need(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */ | 336 | lj_buf_need(&ctx->sb, 1024); /* Avoids resize for most prototypes. */ |
338 | bcwrite_header(ctx); | 337 | bcwrite_header(ctx); |
339 | bcwrite_proto(ctx, ctx->pt); | 338 | bcwrite_proto(ctx, ctx->pt); |
340 | bcwrite_footer(ctx); | 339 | bcwrite_footer(ctx); |
@@ -347,16 +346,15 @@ int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data, | |||
347 | { | 346 | { |
348 | BCWriteCtx ctx; | 347 | BCWriteCtx ctx; |
349 | int status; | 348 | int status; |
350 | ctx.L = L; | ||
351 | ctx.pt = pt; | 349 | ctx.pt = pt; |
352 | ctx.wfunc = writer; | 350 | ctx.wfunc = writer; |
353 | ctx.wdata = data; | 351 | ctx.wdata = data; |
354 | ctx.strip = strip; | 352 | ctx.strip = strip; |
355 | ctx.status = 0; | 353 | ctx.status = 0; |
356 | lj_buf_init(&ctx.sb); | 354 | lj_buf_init(L, &ctx.sb); |
357 | status = lj_vm_cpcall(L, NULL, &ctx, cpwriter); | 355 | status = lj_vm_cpcall(L, NULL, &ctx, cpwriter); |
358 | if (status == 0) status = ctx.status; | 356 | if (status == 0) status = ctx.status; |
359 | lj_buf_free(G(ctx.L), &ctx.sb); | 357 | lj_buf_free(G(sbufL(&ctx.sb)), &ctx.sb); |
360 | return status; | 358 | return status; |
361 | } | 359 | } |
362 | 360 | ||