From d6defcba148a2916c1e5ac5c6c2f104a7743e071 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Thu, 27 Oct 2016 22:20:55 +0300 Subject: [PATCH] Return 1 from duk_push_boolean() and variants --- src-input/duk_api_public.h.in | 9 ++++++--- src-input/duk_api_stack.c | 9 ++++++--- src-input/duk_bi_array.c | 10 ++++------ src-input/duk_bi_buffer.c | 11 ++++------- src-input/duk_bi_duktape.c | 3 +-- src-input/duk_bi_encoding.c | 6 ++---- src-input/duk_bi_global.c | 6 ++---- src-input/duk_bi_object.c | 27 +++++++++++---------------- src-input/duk_bi_reflect.c | 9 +++------ src-input/duk_bi_regexp.c | 4 +--- src-input/duk_hobject_props.c | 3 +-- 11 files changed, 41 insertions(+), 56 deletions(-) diff --git a/src-input/duk_api_public.h.in b/src-input/duk_api_public.h.in index b2332c2c..2144db0b 100644 --- a/src-input/duk_api_public.h.in +++ b/src-input/duk_api_public.h.in @@ -446,11 +446,14 @@ DUK_EXTERNAL_DECL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ * Note: duk_dup() is technically a push. */ +/* FIXME: change the undefined, null, etc pushers to 'return 1' to facilitate + * C tail calls? + */ DUK_EXTERNAL_DECL void duk_push_undefined(duk_context *ctx); DUK_EXTERNAL_DECL void duk_push_null(duk_context *ctx); -DUK_EXTERNAL_DECL void duk_push_boolean(duk_context *ctx, duk_bool_t val); -DUK_EXTERNAL_DECL void duk_push_true(duk_context *ctx); -DUK_EXTERNAL_DECL void duk_push_false(duk_context *ctx); +DUK_EXTERNAL_DECL duk_ret_t duk_push_boolean(duk_context *ctx, duk_bool_t val); +DUK_EXTERNAL_DECL duk_ret_t duk_push_true(duk_context *ctx); +DUK_EXTERNAL_DECL duk_ret_t duk_push_false(duk_context *ctx); DUK_EXTERNAL_DECL void duk_push_number(duk_context *ctx, duk_double_t val); DUK_EXTERNAL_DECL void duk_push_nan(duk_context *ctx); DUK_EXTERNAL_DECL void duk_push_int(duk_context *ctx, duk_int_t val); diff --git a/src-input/duk_api_stack.c b/src-input/duk_api_stack.c index 71739dca..908cead8 100644 --- a/src-input/duk_api_stack.c +++ b/src-input/duk_api_stack.c @@ -3155,7 +3155,7 @@ DUK_EXTERNAL void duk_push_null(duk_context *ctx) { DUK_TVAL_SET_NULL(tv_slot); } -DUK_EXTERNAL void duk_push_boolean(duk_context *ctx, duk_bool_t val) { +DUK_EXTERNAL duk_ret_t duk_push_boolean(duk_context *ctx, duk_bool_t val) { duk_hthread *thr; duk_tval *tv_slot; duk_small_int_t b; @@ -3166,9 +3166,10 @@ DUK_EXTERNAL void duk_push_boolean(duk_context *ctx, duk_bool_t val) { b = (val ? 1 : 0); /* ensure value is 1 or 0 (not other non-zero) */ tv_slot = thr->valstack_top++; DUK_TVAL_SET_BOOLEAN(tv_slot, b); + return 1; } -DUK_EXTERNAL void duk_push_true(duk_context *ctx) { +DUK_EXTERNAL duk_ret_t duk_push_true(duk_context *ctx) { duk_hthread *thr; duk_tval *tv_slot; @@ -3177,9 +3178,10 @@ DUK_EXTERNAL void duk_push_true(duk_context *ctx) { DUK__CHECK_SPACE(); tv_slot = thr->valstack_top++; DUK_TVAL_SET_BOOLEAN_TRUE(tv_slot); + return 1; } -DUK_EXTERNAL void duk_push_false(duk_context *ctx) { +DUK_EXTERNAL duk_ret_t duk_push_false(duk_context *ctx) { duk_hthread *thr; duk_tval *tv_slot; @@ -3188,6 +3190,7 @@ DUK_EXTERNAL void duk_push_false(duk_context *ctx) { DUK__CHECK_SPACE(); tv_slot = thr->valstack_top++; DUK_TVAL_SET_BOOLEAN_FALSE(tv_slot); + return 1; } /* normalize NaN which may not match our canonical internal NaN */ diff --git a/src-input/duk_bi_array.c b/src-input/duk_bi_array.c index f3792829..b5b870fa 100644 --- a/src-input/duk_bi_array.c +++ b/src-input/duk_bi_array.c @@ -182,8 +182,7 @@ DUK_INTERNAL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx) { duk_hobject *h; h = duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_ARRAY); - duk_push_boolean(ctx, (h != NULL)); - return 1; + return duk_push_boolean(ctx, (h != NULL)); } /* @@ -1495,12 +1494,11 @@ DUK_INTERNAL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx) { switch (iter_type) { case DUK__ITER_EVERY: - duk_push_true(ctx); - break; + return duk_push_true(ctx); case DUK__ITER_SOME: - duk_push_false(ctx); - break; + return duk_push_false(ctx); case DUK__ITER_FOREACH: + /* FIXME */ duk_push_undefined(ctx); break; case DUK__ITER_MAP: diff --git a/src-input/duk_bi_buffer.c b/src-input/duk_bi_buffer.c index 2e9c287c..2df6016d 100644 --- a/src-input/duk_bi_buffer.c +++ b/src-input/duk_bi_buffer.c @@ -1090,8 +1090,7 @@ DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx) { if (h_obj != NULL && DUK_HOBJECT_IS_BUFOBJ(h_obj)) { ret = ((duk_hbufobj *) h_obj)->is_view; } - duk_push_boolean(ctx, ret); - return 1; + return duk_push_boolean(ctx, ret); } #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ @@ -1298,7 +1297,7 @@ DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx) { duk_push_int(ctx, comp_res); } else { /* equals */ - duk_push_boolean(ctx, (comp_res == 0)); + return duk_push_boolean(ctx, (comp_res == 0)); } return 1; @@ -2059,8 +2058,7 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx) { encoding = duk_to_string(ctx, 0); DUK_ASSERT(duk_is_string(ctx, 0)); /* guaranteed by duk_to_string() */ - duk_push_boolean(ctx, DUK_STRCMP(encoding, "utf8") == 0); - return 1; + return duk_push_boolean(ctx, DUK_STRCMP(encoding, "utf8") == 0); } #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ @@ -2089,8 +2087,7 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx) { } } - duk_push_boolean(ctx, ret); - return 1; + return duk_push_boolean(ctx, ret); } #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ diff --git a/src-input/duk_bi_duktape.c b/src-input/duk_bi_duktape.c index 21a0cfd8..30102897 100644 --- a/src-input/duk_bi_duktape.c +++ b/src-input/duk_bi_duktape.c @@ -197,8 +197,7 @@ DUK_INTERNAL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx) { /* XXX: Not sure what the best return value would be in the API. * Return a boolean for now. Note that rc == 0 is success (true). */ - duk_push_boolean(ctx, !rc); - return 1; + return duk_push_boolean(ctx, !rc); #else DUK_UNREF(ctx); return 0; diff --git a/src-input/duk_bi_encoding.c b/src-input/duk_bi_encoding.c index 74b22b8c..dcd36820 100644 --- a/src-input/duk_bi_encoding.c +++ b/src-input/duk_bi_encoding.c @@ -490,11 +490,9 @@ DUK_INTERNAL duk_ret_t duk_bi_textdecoder_prototype_shared_getter(duk_context *c duk_push_string(ctx, "utf-8"); break; case 1: - duk_push_boolean(ctx, dec_ctx->fatal); - break; + return duk_push_boolean(ctx, dec_ctx->fatal); default: - duk_push_boolean(ctx, dec_ctx->ignore_bom); - break; + return duk_push_boolean(ctx, dec_ctx->ignore_bom); } return 1; diff --git a/src-input/duk_bi_global.c b/src-input/duk_bi_global.c index 0c7321b9..96c81ab8 100644 --- a/src-input/duk_bi_global.c +++ b/src-input/duk_bi_global.c @@ -666,14 +666,12 @@ DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx) { DUK_INTERNAL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx) { duk_double_t d = duk_to_number(ctx, 0); - duk_push_boolean(ctx, DUK_ISNAN(d)); - return 1; + return duk_push_boolean(ctx, DUK_ISNAN(d)); } DUK_INTERNAL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx) { duk_double_t d = duk_to_number(ctx, 0); - duk_push_boolean(ctx, DUK_ISFINITE(d)); - return 1; + return duk_push_boolean(ctx, DUK_ISFINITE(d)); } /* diff --git a/src-input/duk_bi_object.c b/src-input/duk_bi_object.c index b0966dcf..e521b2e8 100644 --- a/src-input/duk_bi_object.c +++ b/src-input/duk_bi_object.c @@ -238,22 +238,21 @@ DUK_INTERNAL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_context *ctx) { duk_hobject *h; duk_bool_t is_frozen; - duk_bool_t rc; duk_uint_t mask; + duk_bool_t val; is_frozen = duk_get_current_magic(ctx); mask = duk_get_type_mask(ctx, 0); if (mask & (DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER)) { DUK_ASSERT(is_frozen == 0 || is_frozen == 1); - duk_push_boolean(ctx, (mask & DUK_TYPE_MASK_LIGHTFUNC) ? - 1 : /* lightfunc always frozen and sealed */ - (is_frozen ^ 1)); /* buffer sealed but not frozen (index props writable) */ + val = (mask & DUK_TYPE_MASK_LIGHTFUNC) ? + 1 : /* lightfunc always frozen and sealed */ + (is_frozen ^ 1); /* buffer sealed but not frozen (index props writable) */ } else { h = duk_require_hobject(ctx, 0); - rc = duk_hobject_object_is_sealed_frozen_helper((duk_hthread *) ctx, h, is_frozen /*is_frozen*/); - duk_push_boolean(ctx, rc); + val = duk_hobject_object_is_sealed_frozen_helper((duk_hthread *) ctx, h, is_frozen /*is_frozen*/); } - return 1; + return duk_push_boolean(ctx, val); } #endif /* DUK_USE_OBJECT_BUILTIN */ @@ -289,8 +288,7 @@ DUK_INTERNAL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx) h_v = duk_get_hobject(ctx, 0); if (!h_v) { - duk_push_false(ctx); /* XXX: tail call: return duk_push_false(ctx) */ - return 1; + return duk_push_false(ctx); } h_obj = duk_push_this_coercible_to_object(ctx); @@ -299,8 +297,7 @@ DUK_INTERNAL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx) /* E5.1 Section 15.2.4.6, step 3.a, lookup proto once before compare. * Prototype loops should cause an error to be thrown. */ - duk_push_boolean(ctx, duk_hobject_prototype_chain_contains(thr, DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_v), h_obj, 0 /*ignore_loop*/)); - return 1; + return duk_push_boolean(ctx, duk_hobject_prototype_chain_contains(thr, DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_v), h_obj, 0 /*ignore_loop*/)); } #endif /* DUK_USE_OBJECT_BUILTIN */ @@ -463,8 +460,7 @@ DUK_INTERNAL duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx) { if (magic != 2) { DUK_DCERROR_TYPE_INVALID_ARGS(thr); } else { - duk_push_false(ctx); - return 1; + return duk_push_false(ctx); } } #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */ @@ -550,7 +546,7 @@ DUK_INTERNAL duk_ret_t duk_bi_object_constructor_define_property(duk_context *ct duk_push_hobject(ctx, obj); } else { /* Reflect.defineProperty(): return success/fail. */ - duk_push_boolean(ctx, ret); + return duk_push_boolean(ctx, ret); } return 1; } @@ -568,8 +564,7 @@ DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is_extensible(duk_context *ctx) duk_hobject *h; h = duk_require_hobject_accept_mask(ctx, 0, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER); - duk_push_boolean(ctx, h != NULL && DUK_HOBJECT_HAS_EXTENSIBLE(h)); - return 1; + return duk_push_boolean(ctx, h != NULL && DUK_HOBJECT_HAS_EXTENSIBLE(h)); } #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */ diff --git a/src-input/duk_bi_reflect.c b/src-input/duk_bi_reflect.c index 81ff6120..ecc0d705 100644 --- a/src-input/duk_bi_reflect.c +++ b/src-input/duk_bi_reflect.c @@ -26,8 +26,7 @@ DUK_INTERNAL duk_ret_t duk_bi_reflect_object_delete_property(duk_context *ctx) { tv_obj = DUK_GET_TVAL_POSIDX(ctx, 0); tv_key = DUK_GET_TVAL_POSIDX(ctx, 1); ret = duk_hobject_delprop(thr, tv_obj, tv_key, 0 /*throw_flag*/); - duk_push_boolean(ctx, ret); - return 1; + return duk_push_boolean(ctx, ret); } DUK_INTERNAL duk_ret_t duk_bi_reflect_object_get(duk_context *ctx) { @@ -74,8 +73,7 @@ DUK_INTERNAL duk_ret_t duk_bi_reflect_object_has(duk_context *ctx) { tv_obj = DUK_GET_TVAL_POSIDX(ctx, 0); tv_key = DUK_GET_TVAL_POSIDX(ctx, 1); ret = duk_hobject_hasprop(thr, tv_obj, tv_key); - duk_push_boolean(ctx, ret); - return 1; + return duk_push_boolean(ctx, ret); } DUK_INTERNAL duk_ret_t duk_bi_reflect_object_set(duk_context *ctx) { @@ -105,7 +103,6 @@ DUK_INTERNAL duk_ret_t duk_bi_reflect_object_set(duk_context *ctx) { tv_key = DUK_GET_TVAL_POSIDX(ctx, 1); tv_val = DUK_GET_TVAL_POSIDX(ctx, 2); ret = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, 0 /*throw_flag*/); - duk_push_boolean(ctx, ret); - return 1; + return duk_push_boolean(ctx, ret); } #endif /* DUK_USE_REFLECT_BUILTIN */ diff --git a/src-input/duk_bi_regexp.c b/src-input/duk_bi_regexp.c index 4eee005e..4a3c32e3 100644 --- a/src-input/duk_bi_regexp.c +++ b/src-input/duk_bi_regexp.c @@ -112,9 +112,7 @@ DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx) { /* [ result ] */ - duk_push_boolean(ctx, (duk_is_null(ctx, -1) ? 0 : 1)); - - return 1; + return duk_push_boolean(ctx, (duk_is_null(ctx, -1) ? 0 : 1)); } DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx) { diff --git a/src-input/duk_hobject_props.c b/src-input/duk_hobject_props.c index 31bc8c84..6116d8d6 100644 --- a/src-input/duk_hobject_props.c +++ b/src-input/duk_hobject_props.c @@ -5911,8 +5911,7 @@ DUK_INTERNAL duk_bool_t duk_hobject_object_ownprop_helper(duk_context *ctx, duk_ ret = duk_hobject_get_own_propdesc(thr, h_obj, h_v, &desc, 0 /*flags*/); /* don't push value */ - duk_push_boolean(ctx, ret && ((desc.flags & required_desc_flags) == required_desc_flags)); - return 1; + return duk_push_boolean(ctx, ret && ((desc.flags & required_desc_flags) == required_desc_flags)); } /*