From 847cd3e0871fac1a3c2e0479981957b2f55fd172 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Mon, 9 Jun 2014 15:48:23 +0300 Subject: [PATCH] change interface to duk_hobject_pc2line_query() to suit call sites better; shaves off some code footprint and makes call sites prettier --- src/duk_bi_duktape.c | 13 +------------ src/duk_bi_error.c | 11 +---------- src/duk_error_augment.c | 15 ++++----------- src/duk_hobject.h | 2 +- src/duk_hobject_pc2line.c | 18 +++++++++++++++++- 5 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/duk_bi_duktape.c b/src/duk_bi_duktape.c index 4f3caca2..d17114cc 100644 --- a/src/duk_bi_duktape.c +++ b/src/duk_bi_duktape.c @@ -116,7 +116,6 @@ duk_ret_t duk_bi_duktape_object_act(duk_context *ctx) { duk_hthread *thr = (duk_hthread *) ctx; duk_activation *act; duk_hobject *h_func; - duk_hbuffer_fixed *pc2line; duk_uint_fast32_t pc; duk_uint_fast32_t line; duk_int_t level; @@ -140,17 +139,7 @@ duk_ret_t duk_bi_duktape_object_act(duk_context *ctx) { pc = (duk_uint_fast32_t) act->pc; duk_push_int(ctx, (int) pc); /* FIXME: typing */ - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_INT_PC2LINE); - if (duk_is_buffer(ctx, -1)) { - pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1); - DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line)); - pc = (duk_uint_fast32_t) act->pc; - line = duk_hobject_pc2line_query(pc2line, (duk_uint_fast32_t) pc); - } else { - line = 0; - } - duk_pop(ctx); - + line = duk_hobject_pc2line_query(ctx, -2, pc); duk_push_int(ctx, (int) line); /* FIXME: typing */ /* Providing access to e.g. act->lex_env would be dangerous: these diff --git a/src/duk_bi_error.c b/src/duk_bi_error.c index 39e6802a..f52b8201 100644 --- a/src/duk_bi_error.c +++ b/src/duk_bi_error.c @@ -179,16 +179,7 @@ static int duk__traceback_getter_helper(duk_context *ctx, int output_type) { duk_get_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME); #if defined(DUK_USE_PC2LINE) - duk_get_prop_stridx(ctx, -4, DUK_STRIDX_INT_PC2LINE); - if (duk_is_buffer(ctx, -1)) { - duk_hbuffer_fixed *pc2line; - pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1); - DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line)); - line = duk_hobject_pc2line_query(pc2line, (duk_uint_fast32_t) pc); - } else { - line = 0; - } - duk_pop(ctx); + line = duk_hobject_pc2line_query(ctx, -4, (duk_uint_fast32_t) pc); #else line = 0; #endif diff --git a/src/duk_error_augment.c b/src/duk_error_augment.c index 85f18532..27fe4086 100644 --- a/src/duk_error_augment.c +++ b/src/duk_error_augment.c @@ -313,7 +313,6 @@ static void duk__err_augment_builtin_throw(duk_hthread *thr, duk_hthread *thr_ca } else if (thr_callstack->callstack_top > 0) { duk_activation *act; duk_hobject *func; - duk_hbuffer *pc2line; act = thr_callstack->callstack + thr_callstack->callstack_top - 1; DUK_ASSERT(act >= thr_callstack->callstack && act < thr_callstack->callstack + thr_callstack->callstack_size); @@ -343,17 +342,11 @@ static void duk__err_augment_builtin_throw(duk_hthread *thr, duk_hthread *thr_ca duk_push_number(ctx, pc); duk_def_prop_stridx(ctx, -3, DUK_STRIDX_PC, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAGS_NO_OVERWRITE); #endif - - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_PC2LINE); - if (duk_is_buffer(ctx, -1)) { - pc2line = duk_get_hbuffer(ctx, -1); - DUK_ASSERT(pc2line != NULL); - DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC(pc2line)); - line = duk_hobject_pc2line_query((duk_hbuffer_fixed *) pc2line, (duk_uint_fast32_t) pc); - duk_push_number(ctx, (double) line); /* -> [ ... error func pc2line line ] */ /* FIXME: u32 */ - duk_def_prop_stridx(ctx, -4, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE); + line = duk_hobject_pc2line_query(ctx, -1, (duk_uint_fast32_t) pc); + if (line > 0) { + duk_push_u32(ctx, (duk_uint32_t) line); /* -> [ ... error func line ] */ + duk_def_prop_stridx(ctx, -3, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE); } - duk_pop(ctx); } else { /* Native function, no relevant lineNumber. */ } diff --git a/src/duk_hobject.h b/src/duk_hobject.h index bc879cb3..fabc1dcb 100644 --- a/src/duk_hobject.h +++ b/src/duk_hobject.h @@ -702,7 +702,7 @@ void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj); /* pc2line */ #if defined(DUK_USE_PC2LINE) void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length); -duk_uint_fast32_t duk_hobject_pc2line_query(duk_hbuffer_fixed *buf, duk_uint_fast32_t pc); +duk_uint_fast32_t duk_hobject_pc2line_query(duk_context *ctx, duk_idx_t idx_func, duk_uint_fast32_t pc); #endif /* misc */ diff --git a/src/duk_hobject_pc2line.c b/src/duk_hobject_pc2line.c index c93dca64..cc855ce3 100644 --- a/src/duk_hobject_pc2line.c +++ b/src/duk_hobject_pc2line.c @@ -126,7 +126,7 @@ void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_ * it will map to a large PC which is out of bounds and causes a zero to be * returned. */ -duk_uint_fast32_t duk_hobject_pc2line_query(duk_hbuffer_fixed *buf, duk_uint_fast32_t pc) { +static duk_uint_fast32_t duk__hobject_pc2line_query_raw(duk_hbuffer_fixed *buf, duk_uint_fast32_t pc) { duk_bitdecoder_ctx bd_ctx_alloc; duk_bitdecoder_ctx *bd_ctx = &bd_ctx_alloc; duk_uint32_t *hdr; @@ -215,4 +215,20 @@ duk_uint_fast32_t duk_hobject_pc2line_query(duk_hbuffer_fixed *buf, duk_uint_fas return 0; } +duk_uint_fast32_t duk_hobject_pc2line_query(duk_context *ctx, duk_idx_t idx_func, duk_uint_fast32_t pc) { + duk_hbuffer_fixed *pc2line; + duk_uint_fast32_t line; + + duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_PC2LINE); + pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1); + if (pc2line != NULL) { + DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line)); + line = duk__hobject_pc2line_query_raw(pc2line, (duk_uint_fast32_t) pc); + } else { + line = 0; + } + duk_pop(ctx); + + return line; +} #endif /* DUK_USE_PC2LINE */