void PushWeakObject(duk_context* ctx, Object* object) { if (!object) { duk_push_null(ctx); return; } duk_push_heap_stash(ctx); // Check if the wrapper for the object already exists in stash // This is required so that comparisons of object references (e.g. against the me property) work properly if (duk_has_prop_index(ctx, -1, (size_t)object)) { duk_get_prop_index(ctx, -1, (size_t)object); WeakPtr<Object>* oldPtr = GetWeakPtr(ctx, -1); if (oldPtr && oldPtr->Get() == object) { duk_remove(ctx, -2); // Remove stash return; } else duk_pop(ctx); // Valid existing wrapper not found } duk_push_object(ctx); WeakPtr<Object>* ptr = new WeakPtr<Object>(object); duk_push_pointer(ctx, ptr); duk_put_prop_string(ctx, -2, "\xff""weak"); duk_push_c_function(ctx, WeakPtr_Finalizer, 1); duk_set_finalizer(ctx, -2); // Set prototype. If not found, use base class prototype (e.g. IComponent) duk_get_global_string(ctx, object->GetTypeName().CString()); if (!duk_is_object(ctx, -1)) { duk_pop(ctx); duk_get_global_string(ctx, object->GetTypeInfo()->GetBaseTypeInfo()->GetTypeName().CString()); } duk_get_prop_string(ctx, -1, "prototype"); duk_set_prototype(ctx, -3); duk_pop(ctx); // Proxied property access handling for scene, entity & component if (object->GetType() == Scene::GetTypeStatic()) SetupProxy(ctx, SceneProxyFunctions); if (object->GetType() == Entity::GetTypeStatic()) SetupProxy(ctx, EntityProxyFunctions); else if (dynamic_cast<IComponent*>(object)) SetupProxy(ctx, ComponentProxyFunctions); // Store to stash duk_dup(ctx, -1); duk_put_prop_index(ctx, -3, (size_t)object); duk_remove(ctx, -2); // Remove stash }
/* duk_has_prop_index(), DUK_INVALID_INDEX */ static duk_ret_t test_3c(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_has_prop_index(ctx, DUK_INVALID_INDEX, 123); printf("obj[123] -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* duk_has_prop_index(), success cases */ static duk_ret_t test_3a(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_has_prop_index(ctx, 0, 31337); printf("obj[31337] -> rc=%d\n", (int) rc); rc = duk_has_prop_index(ctx, 0, 123); printf("obj[123] -> rc=%d\n", (int) rc); rc = duk_has_prop_index(ctx, 1, 31337); printf("arr[31337] -> rc=%d\n", (int) rc); rc = duk_has_prop_index(ctx, 1, 2); printf("arr[2] -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* duk_has_prop_index(), DUK_INVALID_INDEX */ int test_3c(duk_context *ctx) { int rc; prep(ctx); rc = duk_has_prop_index(ctx, DUK_INVALID_INDEX, 123); printf("obj[123] -> rc=%d\n", rc); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
/* duk_has_prop_index(), success cases */ int test_3a(duk_context *ctx) { int rc; prep(ctx); rc = duk_has_prop_index(ctx, 0, 31337); printf("obj[31337] -> rc=%d\n", rc); rc = duk_has_prop_index(ctx, 0, 123); printf("obj[123] -> rc=%d\n", rc); rc = duk_has_prop_index(ctx, 1, 31337); printf("arr[31337] -> rc=%d\n", rc); rc = duk_has_prop_index(ctx, 1, 2); printf("arr[2] -> rc=%d\n", rc); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
/* duk_has_prop_index(), invalid index */ static duk_ret_t test_3b(duk_context *ctx, void *udata) { duk_ret_t rc; (void) udata; prep(ctx); rc = duk_has_prop_index(ctx, 234, 123); printf("obj[123] -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t test_func(duk_context *ctx, void *udata) { (void) udata; if (ctx) { printf("dummy - return here\n"); fflush(stdout); return 0; } /* Up-to-date for Duktape 1.3.0, alphabetical order: * $ cd website/api; ls *.yaml */ (void) duk_alloc_raw(ctx, 0); (void) duk_alloc(ctx, 0); (void) duk_base64_decode(ctx, 0); (void) duk_base64_encode(ctx, 0); (void) duk_buffer_to_string(ctx, 0); (void) duk_call_method(ctx, 0); (void) duk_call_prop(ctx, 0, 0); (void) duk_call(ctx, 0); (void) duk_char_code_at(ctx, 0, 0); (void) duk_check_stack_top(ctx, 0); (void) duk_check_stack(ctx, 0); (void) duk_check_type_mask(ctx, 0, 0); (void) duk_check_type(ctx, 0, 0); (void) duk_compact(ctx, 0); (void) duk_compile_lstring_filename(ctx, 0, "dummy", 0); (void) duk_compile_lstring(ctx, 0, "dummy", 0); (void) duk_compile_string_filename(ctx, 0, "dummy"); (void) duk_compile_string(ctx, 0, "dummy"); (void) duk_compile(ctx, 0); (void) duk_concat(ctx, 0); (void) duk_config_buffer(ctx, 0, NULL, 0); (void) duk_copy(ctx, 0, 0); (void) duk_create_heap_default(); (void) duk_create_heap(NULL, NULL, NULL, NULL, NULL); (void) duk_debugger_attach(ctx, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); (void) duk_debugger_cooperate(ctx); (void) duk_debugger_detach(ctx); (void) duk_debugger_notify(ctx, 0); (void) duk_debugger_pause(ctx); (void) duk_decode_string(ctx, 0, NULL, NULL); (void) duk_def_prop(ctx, 0, 0); (void) duk_del_prop_index(ctx, 0, 0); (void) duk_del_prop_string(ctx, 0, "dummy"); (void) duk_del_prop(ctx, 0); (void) duk_destroy_heap(ctx); (void) duk_dump_function(ctx); (void) duk_dup_top(ctx); (void) duk_dup(ctx, 0); (void) duk_enum(ctx, 0, 0); (void) duk_equals(ctx, 0, 0); duk_error_va(ctx, 0, NULL, NULL); duk_error(ctx, 0, "dummy"); /* (void) cast won't work without variadic macros */ (void) duk_eval_lstring_noresult(ctx, "dummy", 0); (void) duk_eval_lstring(ctx, "dummy", 0); (void) duk_eval_noresult(ctx); (void) duk_eval_string_noresult(ctx, "dummy"); (void) duk_eval_string(ctx, "dummy"); (void) duk_eval(ctx); (void) duk_fatal(ctx, "dummy"); (void) duk_free_raw(ctx, NULL); (void) duk_free(ctx, NULL); (void) duk_gc(ctx, 0); (void) duk_get_boolean(ctx, 0); (void) duk_get_buffer_data(ctx, 0, NULL); (void) duk_get_buffer(ctx, 0, NULL); (void) duk_get_c_function(ctx, 0); (void) duk_get_context(ctx, 0); (void) duk_get_current_magic(ctx); (void) duk_get_error_code(ctx, 0); (void) duk_get_finalizer(ctx, 0); (void) duk_get_global_string(ctx, 0); (void) duk_get_heapptr(ctx, 0); (void) duk_get_int(ctx, 0); (void) duk_get_length(ctx, 0); (void) duk_get_lstring(ctx, 0, NULL); (void) duk_get_magic(ctx, 0); (void) duk_get_memory_functions(ctx, NULL); (void) duk_get_number(ctx, 0); (void) duk_get_pointer(ctx, 0); (void) duk_get_prop_index(ctx, 0, 0); (void) duk_get_prop_string(ctx, 0, "dummy"); (void) duk_get_prop(ctx, 0); (void) duk_get_prototype(ctx, 0); (void) duk_get_string(ctx, 0); (void) duk_get_top_index(ctx); (void) duk_get_top(ctx); (void) duk_get_type_mask(ctx, 0); (void) duk_get_type(ctx, 0); (void) duk_get_uint(ctx, 0); (void) duk_has_prop_index(ctx, 0, 0); (void) duk_has_prop_string(ctx, 0, "dummy"); (void) duk_has_prop(ctx, 0); (void) duk_hex_decode(ctx, 0); (void) duk_hex_encode(ctx, 0); (void) duk_insert(ctx, 0); (void) duk_instanceof(ctx, 0, 0); (void) duk_is_array(ctx, 0); (void) duk_is_boolean(ctx, 0); (void) duk_is_bound_function(ctx, 0); (void) duk_is_buffer(ctx, 0); (void) duk_is_callable(ctx, 0); (void) duk_is_c_function(ctx, 0); (void) duk_is_constructor_call(ctx); (void) duk_is_dynamic_buffer(ctx, 0); (void) duk_is_ecmascript_function(ctx, 0); (void) duk_is_error(ctx, 0); (void) duk_is_eval_error(ctx, 0); (void) duk_is_fixed_buffer(ctx, 0); (void) duk_is_function(ctx, 0); (void) duk_is_lightfunc(ctx, 0); (void) duk_is_nan(ctx, 0); (void) duk_is_null_or_undefined(ctx, 0); (void) duk_is_null(ctx, 0); (void) duk_is_number(ctx, 0); (void) duk_is_object_coercible(ctx, 0); (void) duk_is_object(ctx, 0); (void) duk_is_pointer(ctx, 0); (void) duk_is_primitive(ctx, 0); (void) duk_is_range_error(ctx, 0); (void) duk_is_reference_error(ctx, 0); (void) duk_is_strict_call(ctx); (void) duk_is_string(ctx, 0); (void) duk_is_syntax_error(ctx, 0); (void) duk_is_thread(ctx, 0); (void) duk_is_type_error(ctx, 0); (void) duk_is_undefined(ctx, 0); (void) duk_is_uri_error(ctx, 0); (void) duk_is_valid_index(ctx, 0); (void) duk_join(ctx, 0); (void) duk_json_decode(ctx, 0); (void) duk_json_encode(ctx, 0); (void) duk_load_function(ctx); (void) duk_map_string(ctx, 0, NULL, NULL); (void) duk_new(ctx, 0); (void) duk_next(ctx, 0, 0); (void) duk_normalize_index(ctx, 0); (void) duk_pcall_method(ctx, 0); (void) duk_pcall_prop(ctx, 0, 0); (void) duk_pcall(ctx, 0); (void) duk_pcompile_lstring_filename(ctx, 0, "dummy", 0); (void) duk_pcompile_lstring(ctx, 0, "dummy", 0); (void) duk_pcompile_string_filename(ctx, 0, "dummy"); (void) duk_pcompile_string(ctx, 0, "dummy"); (void) duk_pcompile(ctx, 0); (void) duk_peval_lstring_noresult(ctx, "dummy", 0); (void) duk_peval_lstring(ctx, "dummy", 0); (void) duk_peval_noresult(ctx); (void) duk_peval_string_noresult(ctx, "dummy"); (void) duk_peval_string(ctx, "dummy"); (void) duk_peval(ctx); (void) duk_pnew(ctx, 0); (void) duk_pop_2(ctx); (void) duk_pop_3(ctx); (void) duk_pop_n(ctx, 0); (void) duk_pop(ctx); (void) duk_push_array(ctx); (void) duk_push_boolean(ctx, 0); (void) duk_push_buffer_object(ctx, 0, 0, 0, 0); (void) duk_push_buffer(ctx, 0, 0); (void) duk_push_c_function(ctx, NULL, 0); (void) duk_push_c_lightfunc(ctx, NULL, 0, 0, 0); (void) duk_push_context_dump(ctx); (void) duk_push_current_function(ctx); (void) duk_push_current_thread(ctx); (void) duk_push_dynamic_buffer(ctx, 0); (void) duk_push_error_object_va(ctx, 0, NULL, NULL); (void) duk_push_error_object(ctx, 0, "dummy"); (void) duk_push_external_buffer(ctx); (void) duk_push_false(ctx); (void) duk_push_fixed_buffer(ctx, 0); (void) duk_push_global_object(ctx); (void) duk_push_global_stash(ctx); (void) duk_push_heap_stash(ctx); (void) duk_push_heapptr(ctx, NULL); (void) duk_push_int(ctx, 0); (void) duk_push_lstring(ctx, "dummy", 0); (void) duk_push_nan(ctx); (void) duk_push_null(ctx); (void) duk_push_number(ctx, 0.0); (void) duk_push_object(ctx); (void) duk_push_pointer(ctx, NULL); (void) duk_push_sprintf(ctx, "dummy"); (void) duk_push_string(ctx, "dummy"); (void) duk_push_this(ctx); (void) duk_push_thread_new_globalenv(ctx); (void) duk_push_thread_stash(ctx, NULL); (void) duk_push_thread(ctx); (void) duk_push_true(ctx); (void) duk_push_uint(ctx, 0); (void) duk_push_undefined(ctx); (void) duk_push_vsprintf(ctx, "dummy", NULL); (void) duk_put_function_list(ctx, 0, NULL); (void) duk_put_global_string(ctx, NULL); (void) duk_put_number_list(ctx, 0, NULL); (void) duk_put_prop_index(ctx, 0, 0); (void) duk_put_prop_string(ctx, 0, "dummy"); (void) duk_put_prop(ctx, 0); (void) duk_realloc_raw(ctx, NULL, 0); (void) duk_realloc(ctx, NULL, 0); (void) duk_remove(ctx, 0); (void) duk_replace(ctx, 0); (void) duk_require_boolean(ctx, 0); (void) duk_require_buffer_data(ctx, 0, NULL); (void) duk_require_buffer(ctx, 0, NULL); (void) duk_require_c_function(ctx, 0); (void) duk_require_callable(ctx, 0); (void) duk_require_context(ctx, 0); (void) duk_require_function(ctx, 0); (void) duk_require_heapptr(ctx, 0); (void) duk_require_int(ctx, 0); (void) duk_require_lstring(ctx, 0, NULL); (void) duk_require_normalize_index(ctx, 0); (void) duk_require_null(ctx, 0); (void) duk_require_number(ctx, 0); (void) duk_require_object_coercible(ctx, 0); (void) duk_require_pointer(ctx, 0); (void) duk_require_stack_top(ctx, 0); (void) duk_require_stack(ctx, 0); (void) duk_require_string(ctx, 0); (void) duk_require_top_index(ctx); (void) duk_require_type_mask(ctx, 0, 0); (void) duk_require_uint(ctx, 0); (void) duk_require_undefined(ctx, 0); (void) duk_require_valid_index(ctx, 0); (void) duk_resize_buffer(ctx, 0, 0); (void) duk_safe_call(ctx, NULL, NULL, 0, 0); (void) duk_safe_to_lstring(ctx, 0, NULL); (void) duk_safe_to_string(ctx, 0); (void) duk_set_finalizer(ctx, 0); (void) duk_set_global_object(ctx); (void) duk_set_magic(ctx, 0, 0); (void) duk_set_prototype(ctx, 0); (void) duk_set_top(ctx, 0); (void) duk_steal_buffer(ctx, 0, NULL); (void) duk_strict_equals(ctx, 0, 0); (void) duk_substring(ctx, 0, 0, 0); (void) duk_swap_top(ctx, 0); (void) duk_swap(ctx, 0, 0); (void) duk_throw(ctx); (void) duk_to_boolean(ctx, 0); (void) duk_to_buffer(ctx, 0, NULL); (void) duk_to_defaultvalue(ctx, 0, 0); (void) duk_to_dynamic_buffer(ctx, 0, NULL); (void) duk_to_fixed_buffer(ctx, 0, NULL); (void) duk_to_int32(ctx, 0); (void) duk_to_int(ctx, 0); (void) duk_to_lstring(ctx, 0, NULL); (void) duk_to_null(ctx, 0); (void) duk_to_number(ctx, 0); (void) duk_to_object(ctx, 0); (void) duk_to_pointer(ctx, 0); (void) duk_to_primitive(ctx, 0, 0); (void) duk_to_string(ctx, 0); (void) duk_to_uint16(ctx, 0); (void) duk_to_uint32(ctx, 0); (void) duk_to_uint(ctx, 0); (void) duk_to_undefined(ctx, 0); (void) duk_trim(ctx, 0); (void) duk_xcopy_top(ctx, NULL, 0); (void) duk_xmove_top(ctx, NULL, 0); printf("never here\n"); fflush(stdout); return 0; }
//duk_bool_t duk_has_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index); duk_bool_t aperl_duk_has_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { duk_bool_t ret = duk_has_prop_index(ctx, obj_index, arr_index); return ret; }
DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx) { duk_idx_t nargs; duk_bool_t have_acc; duk_uint32_t i, len; duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for reduce, -1 for reduceRight */ /* We're a varargs function because we need to detect whether * initialValue was given or not. */ nargs = duk_get_top(ctx); DUK_DDD(DUK_DDDPRINT("nargs=%ld", (long) nargs)); duk_set_top(ctx, 2); len = duk__push_this_obj_len_u32(ctx); if (!duk_is_callable(ctx, 0)) { goto type_error; } /* stack[0] = callback fn * stack[1] = initialValue * stack[2] = object (coerced this) * stack[3] = length (not needed, but not popped above) * stack[4] = accumulator */ have_acc = 0; if (nargs >= 2) { duk_dup(ctx, 1); have_acc = 1; } DUK_DDD(DUK_DDDPRINT("have_acc=%ld, acc=%!T", (long) have_acc, (duk_tval *) duk_get_tval(ctx, 3))); /* For len == 0, i is initialized to len - 1 which underflows. * The condition (i < len) will then exit the for-loop on the * first round which is correct. Similarly, loop termination * happens by i underflowing. */ for (i = (idx_step >= 0 ? 0 : len - 1); i < len; /* i >= 0 would always be true */ i += idx_step) { DUK_DDD(DUK_DDDPRINT("i=%ld, len=%ld, have_acc=%ld, top=%ld, acc=%!T", (long) i, (long) len, (long) have_acc, (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, 4))); DUK_ASSERT((have_acc && duk_get_top(ctx) == 5) || (!have_acc && duk_get_top(ctx) == 4)); if (!duk_has_prop_index(ctx, 2, (duk_uarridx_t) i)) { continue; } if (!have_acc) { DUK_ASSERT_TOP(ctx, 4); duk_get_prop_index(ctx, 2, (duk_uarridx_t) i); have_acc = 1; DUK_ASSERT_TOP(ctx, 5); } else { DUK_ASSERT_TOP(ctx, 5); duk_dup(ctx, 0); duk_dup(ctx, 4); duk_get_prop_index(ctx, 2, (duk_uarridx_t) i); duk_push_u32(ctx, i); duk_dup(ctx, 2); DUK_DDD(DUK_DDDPRINT("calling reduce function: func=%!T, prev=%!T, curr=%!T, idx=%!T, obj=%!T", (duk_tval *) duk_get_tval(ctx, -5), (duk_tval *) duk_get_tval(ctx, -4), (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1))); duk_call(ctx, 4); DUK_DDD(DUK_DDDPRINT("-> result: %!T", (duk_tval *) duk_get_tval(ctx, -1))); duk_replace(ctx, 4); DUK_ASSERT_TOP(ctx, 5); } } if (!have_acc) { goto type_error; } DUK_ASSERT_TOP(ctx, 5); return 1; type_error: return DUK_RET_TYPE_ERROR; }
int duk_bi_array_prototype_reduce_shared(duk_context *ctx) { int nargs; int have_acc; int i, len; int idx_step = duk_get_magic(ctx); /* idx_step is +1 for reduce, -1 for reduceRight */ /* We're a varargs function because we need to detect whether * initialValue was given or not. */ nargs = duk_get_top(ctx); DUK_DDD(DUK_DDDPRINT("nargs=%d", nargs)); duk_set_top(ctx, 2); len = duk__push_this_obj_len_u32(ctx); if (!duk_is_callable(ctx, 0)) { goto type_error; } /* stack[0] = callback fn * stack[1] = initialValue * stack[2] = object (coerced this) * stack[3] = length (not needed, but not popped above) * stack[4] = accumulator */ have_acc = 0; if (nargs >= 2) { duk_dup(ctx, 1); have_acc = 1; } DUK_DDD(DUK_DDDPRINT("have_acc=%d, acc=%!T", have_acc, duk_get_tval(ctx, 3))); for (i = (idx_step >= 0 ? 0 : len - 1); i >= 0 && i < len; i += idx_step) { DUK_DDD(DUK_DDDPRINT("i=%d, len=%d, have_acc=%d, top=%d, acc=%!T", i, len, have_acc, duk_get_top(ctx), duk_get_tval(ctx, 4))); DUK_ASSERT((have_acc && duk_get_top(ctx) == 5) || (!have_acc && duk_get_top(ctx) == 4)); if (!duk_has_prop_index(ctx, 2, i)) { continue; } if (!have_acc) { DUK_ASSERT_TOP(ctx, 4); duk_get_prop_index(ctx, 2, i); have_acc = 1; DUK_ASSERT_TOP(ctx, 5); } else { DUK_ASSERT_TOP(ctx, 5); duk_dup(ctx, 0); duk_dup(ctx, 4); duk_get_prop_index(ctx, 2, i); duk_push_int(ctx, i); /* FIXME: type */ duk_dup(ctx, 2); DUK_DDD(DUK_DDDPRINT("calling reduce function: func=%!T, prev=%!T, curr=%!T, idx=%!T, obj=%!T", duk_get_tval(ctx, -5), duk_get_tval(ctx, -4), duk_get_tval(ctx, -3), duk_get_tval(ctx, -2), duk_get_tval(ctx, -1))); duk_call(ctx, 4); DUK_DDD(DUK_DDDPRINT("-> result: %!T", duk_get_tval(ctx, -1))); duk_replace(ctx, 4); DUK_ASSERT_TOP(ctx, 5); } } if (!have_acc) { goto type_error; } DUK_ASSERT_TOP(ctx, 5); return 1; type_error: return DUK_RET_TYPE_ERROR; }