Skip to content

Commit c7142cb

Browse files
committed
duk_is_callable/constructable optimizations
* Slightly faster way of checking callability/constructability. * Add internal (currently unused) duk_is_callable_tval() helper.
1 parent aabf2d4 commit c7142cb

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

src-input/duk_api_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ DUK_INTERNAL_DECL duk_tval *duk_get_borrowed_this_tval(duk_hthread *thr);
9696

9797
DUK_INTERNAL_DECL duk_bool_t duk_is_string_notsymbol(duk_hthread *thr, duk_idx_t idx);
9898

99+
DUK_INTERNAL_DECL duk_bool_t duk_is_callable_tval(duk_hthread *thr, duk_tval *tv);
100+
99101
DUK_INTERNAL_DECL duk_hstring *duk_get_hstring(duk_hthread *thr, duk_idx_t idx);
100102
DUK_INTERNAL_DECL duk_hstring *duk_get_hstring_notsymbol(duk_hthread *thr, duk_idx_t idx);
101103
DUK_INTERNAL_DECL const char *duk_get_string_notsymbol(duk_hthread *thr, duk_idx_t idx);

src-input/duk_api_stack.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3571,7 +3571,7 @@ DUK_INTERNAL const char *duk_get_type_name(duk_hthread *thr, duk_idx_t idx) {
35713571

35723572
return duk__type_names[type_tag];
35733573
}
3574-
#endif
3574+
#endif /* DUK_USE_VERBOSE_ERRORS && DUK_USE_PARANOID_ERRORS */
35753575

35763576
DUK_INTERNAL duk_small_uint_t duk_get_class_number(duk_hthread *thr, duk_idx_t idx) {
35773577
duk_tval *tv;
@@ -3809,12 +3809,31 @@ DUK_EXTERNAL duk_bool_t duk_is_function(duk_hthread *thr, duk_idx_t idx) {
38093809
DUK_ASSERT_CTX_VALID(thr);
38103810

38113811
tv = duk_get_tval_or_unused(thr, idx);
3812+
if (DUK_TVAL_IS_OBJECT(tv)) {
3813+
duk_hobject *h;
3814+
h = DUK_TVAL_GET_OBJECT(tv);
3815+
DUK_ASSERT(h != NULL);
3816+
return DUK_HOBJECT_HAS_CALLABLE(h) ? 1 : 0;
3817+
}
38123818
if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
38133819
return 1;
38143820
}
3815-
return duk__obj_flag_any_default_false(thr,
3816-
idx,
3817-
DUK_HOBJECT_FLAG_CALLABLE);
3821+
return 0;
3822+
}
3823+
3824+
DUK_INTERNAL duk_bool_t duk_is_callable_tval(duk_hthread *thr, duk_tval *tv) {
3825+
DUK_ASSERT_CTX_VALID(thr);
3826+
3827+
if (DUK_TVAL_IS_OBJECT(tv)) {
3828+
duk_hobject *h;
3829+
h = DUK_TVAL_GET_OBJECT(tv);
3830+
DUK_ASSERT(h != NULL);
3831+
return DUK_HOBJECT_HAS_CALLABLE(h) ? 1 : 0;
3832+
}
3833+
if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
3834+
return 1;
3835+
}
3836+
return 0;
38183837
}
38193838

38203839
DUK_EXTERNAL duk_bool_t duk_is_constructable(duk_hthread *thr, duk_idx_t idx) {
@@ -3823,12 +3842,16 @@ DUK_EXTERNAL duk_bool_t duk_is_constructable(duk_hthread *thr, duk_idx_t idx) {
38233842
DUK_ASSERT_CTX_VALID(thr);
38243843

38253844
tv = duk_get_tval_or_unused(thr, idx);
3845+
if (DUK_TVAL_IS_OBJECT(tv)) {
3846+
duk_hobject *h;
3847+
h = DUK_TVAL_GET_OBJECT(tv);
3848+
DUK_ASSERT(h != NULL);
3849+
return DUK_HOBJECT_HAS_CONSTRUCTABLE(h) ? 1 : 0;
3850+
}
38263851
if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
38273852
return 1;
38283853
}
3829-
return duk__obj_flag_any_default_false(thr,
3830-
idx,
3831-
DUK_HOBJECT_FLAG_CONSTRUCTABLE);
3854+
return 0;
38323855
}
38333856

38343857
DUK_EXTERNAL duk_bool_t duk_is_c_function(duk_hthread *thr, duk_idx_t idx) {

0 commit comments

Comments
 (0)