From eb44d2a1385304bfa65357c5eec178a0eeaf954a Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Sun, 26 Jan 2014 18:02:58 +0200 Subject: [PATCH] type cleanup --- src/duk_bi_regexp.c | 77 ++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/src/duk_bi_regexp.c b/src/duk_bi_regexp.c index 0c311361..f936bd20 100644 --- a/src/duk_bi_regexp.c +++ b/src/duk_bi_regexp.c @@ -17,7 +17,7 @@ static void get_this_regexp(duk_context *ctx) { } /* FIXME: much to improve (code size) */ -int duk_bi_regexp_constructor(duk_context *ctx) { +duk_ret_t duk_bi_regexp_constructor(duk_context *ctx) { duk_hthread *thr = (duk_hthread *) ctx; duk_hobject *h_pattern; @@ -96,7 +96,7 @@ int duk_bi_regexp_constructor(duk_context *ctx) { return 1; } -int duk_bi_regexp_prototype_exec(duk_context *ctx) { +duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx) { get_this_regexp(ctx); /* [ regexp input ] */ @@ -108,7 +108,7 @@ int duk_bi_regexp_prototype_exec(duk_context *ctx) { return 1; } -int duk_bi_regexp_prototype_test(duk_context *ctx) { +duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx) { get_this_regexp(ctx); /* [ regexp input ] */ @@ -123,9 +123,32 @@ int duk_bi_regexp_prototype_test(duk_context *ctx) { return 1; } -int duk_bi_regexp_prototype_to_string(duk_context *ctx) { +duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx) { duk_hstring *h_bc; - int re_flags; + duk_small_int_t re_flags; + +#if 0 + /* A little tricky string approach to provide the flags string. + * This depends on the specific flag values in duk_regexp.h, + * which needs to be asserted for. In practice this doesn't + * produce more compact code than the easier approach in use. + */ + + const char *flag_strings = "gim\0gi\0gm\0g\0"; + duk_uint8_t flag_offsets[8] = { + (duk_uint8_t) 3, /* flags: "" */ + (duk_uint8_t) 10, /* flags: "g" */ + (duk_uint8_t) 5, /* flags: "i" */ + (duk_uint8_t) 4, /* flags: "gi" */ + (duk_uint8_t) 2, /* flags: "m" */ + (duk_uint8_t) 7, /* flags: "gm" */ + (duk_uint8_t) 1, /* flags: "im" */ + (duk_uint8_t) 0, /* flags: "gim" */ + }; + DUK_ASSERT(DUK_RE_FLAG_GLOBAL == 1); + DUK_ASSERT(DUK_RE_FLAG_IGNORE_CASE == 2); + DUK_ASSERT(DUK_RE_FLAG_MULTILINE == 4); +#endif get_this_regexp(ctx); @@ -138,58 +161,54 @@ int duk_bi_regexp_prototype_to_string(duk_context *ctx) { DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(h_bc) >= 1); DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h_bc) >= 1); DUK_ASSERT(DUK_HSTRING_GET_DATA(h_bc)[0] < 0x80); - re_flags = (int) DUK_HSTRING_GET_DATA(h_bc)[0]; + re_flags = (duk_small_int_t) DUK_HSTRING_GET_DATA(h_bc)[0]; /* [ regexp source bytecode ] */ +#if 1 + /* This is a cleaner approach and also produces smaller code than + * the other alternative. + */ duk_push_sprintf(ctx, "/%s/%s%s%s", duk_get_string(ctx, -2), (re_flags & DUK_RE_FLAG_GLOBAL) ? "g" : "", (re_flags & DUK_RE_FLAG_IGNORE_CASE) ? "i" : "", (re_flags & DUK_RE_FLAG_MULTILINE) ? "m" : ""); +#else + /* This should not be necessary because no-one should tamper with the + * regexp bytecode, but is prudent to avoid potential segfaults if that + * were to happen for some reason. + */ + re_flags &= 0x07; + DUK_ASSERT(re_flags >= 0 && re_flags <= 7); /* three flags */ + duk_push_sprintf(ctx, "/%s/%s", + duk_get_string(ctx, -2), + flag_strings + flag_offsets[re_flags]); +#endif return 1; } #else /* DUK_USE_REGEXP_SUPPORT */ -int duk_bi_regexp_constructor(duk_context *ctx) { +duk_ret_t duk_bi_regexp_constructor(duk_context *ctx) { DUK_UNREF(ctx); return DUK_RET_UNSUPPORTED_ERROR; } -int duk_bi_regexp_prototype_exec(duk_context *ctx) { +duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx) { DUK_UNREF(ctx); return DUK_RET_UNSUPPORTED_ERROR; } -int duk_bi_regexp_prototype_test(duk_context *ctx) { +duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx) { DUK_UNREF(ctx); return DUK_RET_UNSUPPORTED_ERROR; } -int duk_bi_regexp_prototype_to_string(duk_context *ctx) { +duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx) { DUK_UNREF(ctx); return DUK_RET_UNSUPPORTED_ERROR; } #endif /* DUK_USE_REGEXP_SUPPORT */ - -/* - -could also map flag values as follows: - -"gim\0gi\0gm\0g\0" - -flags desc offset in above string -0 (none) 3 -1 g 11 -2 i 5 -3 gi 4 -4 m 2 -5 gm 7 -6 im 1 -7 gim 0 - -*/ -