DUK_EXTERNAL duk_bool_t duk_get_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { DUK_ASSERT_CTX_VALID(ctx); obj_index = duk_require_normalize_index(ctx, obj_index); duk_push_uarridx(ctx, arr_index); return duk_get_prop(ctx, obj_index); }
DUK_EXTERNAL duk_bool_t duk_get_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { DUK_ASSERT_CTX_VALID(ctx); DUK_ASSERT(key != NULL); obj_index = duk_require_normalize_index(ctx, obj_index); duk_push_string(ctx, key); return duk_get_prop(ctx, obj_index); }
DUK_EXTERNAL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { DUK_ASSERT_CTX_VALID(ctx); obj_index = duk_require_normalize_index(ctx, obj_index); duk_push_uarridx(ctx, arr_index); duk_swap_top(ctx, -2); /* [val key] -> [key val] */ return duk_put_prop(ctx, obj_index); }
static void setup_arraybuffer(duk_context *ctx, duk_idx_t idx) { idx = duk_require_normalize_index(ctx, idx); duk_eval_string(ctx, "(function (plain_buffer) {\n" " return new ArrayBuffer(plain_buffer);\n" "})\n"); duk_dup(ctx, idx); duk_call(ctx, 1); }
void DuktapeContext::set(JNIEnv *env, jstring name, jobject object, jobjectArray methods) { CHECK_STACK(m_context); duk_push_global_object(m_context); const JString instanceName(env, name); if (duk_has_prop_string(m_context, -1, instanceName)) { duk_pop(m_context); queueIllegalArgumentException(env, "A global object called " + instanceName.str() + " already exists"); return; } const duk_idx_t objIndex = duk_require_normalize_index(m_context, duk_push_object(m_context)); // Hook up a finalizer to decrement the refcount and clean up our JavaMethods. duk_push_c_function(m_context, javaObjectFinalizer, 1); duk_set_finalizer(m_context, objIndex); const jsize numMethods = env->GetArrayLength(methods); for (jsize i = 0; i < numMethods; ++i) { jobject method = env->GetObjectArrayElement(methods, i); const jmethodID getName = env->GetMethodID(env->GetObjectClass(method), "getName", "()Ljava/lang/String;"); const JString methodName(env, static_cast<jstring>(env->CallObjectMethod(method, getName))); std::unique_ptr<JavaMethod> javaMethod; try { javaMethod.reset(new JavaMethod(m_javaValues, env, method)); } catch (const std::invalid_argument& e) { queueIllegalArgumentException(env, "In bound method \"" + instanceName.str() + "." + methodName.str() + "\": " + e.what()); // Pop the object being bound and the duktape global object. duk_pop_2(m_context); return; } // Use VARARGS here to allow us to manually validate that the proper number of arguments are // given in the call. If we specify the actual number of arguments needed, Duktape will try to // be helpful by discarding extra or providing missing arguments. That's not quite what we want. // See http://duktape.org/api.html#duk_push_c_function for details. const duk_idx_t func = duk_push_c_function(m_context, javaMethodHandler, DUK_VARARGS); duk_push_pointer(m_context, javaMethod.release()); duk_put_prop_string(m_context, func, JAVA_METHOD_PROP_NAME); // Add this method to the bound object. duk_put_prop_string(m_context, objIndex, methodName); } // Keep a reference in JavaScript to the object being bound. duk_push_pointer(m_context, env->NewGlobalRef(object)); duk_put_prop_string(m_context, objIndex, JAVA_THIS_PROP_NAME); // Make our bound Java object a property of the Duktape global object (so it's a JS global). duk_put_prop_string(m_context, -2, instanceName); // Pop the Duktape global object off the stack. duk_pop(m_context); }
static void setup_typedarray(duk_context *ctx, duk_idx_t idx, const char *name) { idx = duk_require_normalize_index(ctx, idx); duk_push_sprintf(ctx, "(function (plain_buffer) {\n" " return new %s(new ArrayBuffer(plain_buffer));\n" "})\n", name); duk_eval(ctx); duk_dup(ctx, idx); duk_call(ctx, 1); }
DUK_INTERNAL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) { duk_hthread *thr = (duk_hthread *) ctx; DUK_ASSERT_CTX_VALID(ctx); DUK_ASSERT_DISABLE(stridx >= 0); DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); obj_index = duk_require_normalize_index(ctx, obj_index); duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); return duk_get_prop(ctx, obj_index); }
static void setup_nodejs_buffer_slice(duk_context *ctx, duk_idx_t idx, duk_int_t start, duk_int_t end) { idx = duk_require_normalize_index(ctx, idx); duk_eval_string(ctx, "(function (plain_buffer, start, end) {\n" " return Buffer(plain_buffer).slice(start, end);\n" "})\n"); duk_dup(ctx, idx); duk_push_int(ctx, start); duk_push_int(ctx, end); duk_call(ctx, 3); }
int req_norm_idx(duk_context *ctx) { int idx = duk_get_int(ctx, -1); int norm_idx; duk_pop(ctx); printf("req_norm_idx: top %d after popping arg\n", duk_get_top(ctx)); norm_idx = duk_require_normalize_index(ctx, idx); duk_push_int(ctx, norm_idx); return 1; }
static void setup_typedarray_slice(duk_context *ctx, duk_idx_t idx, const char *name, duk_int_t start, duk_int_t end) { idx = duk_require_normalize_index(ctx, idx); duk_push_sprintf(ctx, "(function (plain_buffer, start, length) {\n" " return new %s(new ArrayBuffer(plain_buffer), start, length);\n" "})\n", name); duk_eval(ctx); duk_dup(ctx, idx); duk_push_int(ctx, start); duk_push_int(ctx, end); duk_call(ctx, 3); }
DUK_EXTERNAL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_index, const duk_function_list_entry *funcs) { const duk_function_list_entry *ent = funcs; DUK_ASSERT_CTX_VALID(ctx); obj_index = duk_require_normalize_index(ctx, obj_index); if (ent != NULL) { while (ent->key != NULL) { duk_push_c_function(ctx, ent->value, ent->nargs); duk_put_prop_string(ctx, obj_index, ent->key); ent++; } } }
DUK_EXTERNAL void duk_put_number_list(duk_context *ctx, duk_idx_t obj_index, const duk_number_list_entry *numbers) { const duk_number_list_entry *ent = numbers; DUK_ASSERT_CTX_VALID(ctx); obj_index = duk_require_normalize_index(ctx, obj_index); if (ent != NULL) { while (ent->key != NULL) { duk_push_number(ctx, ent->value); duk_put_prop_string(ctx, obj_index, ent->key); ent++; } } }
DUK_EXTERNAL void duk_call_prop(duk_context *ctx, duk_idx_t obj_idx, duk_idx_t nargs) { /* * XXX: if duk_handle_call() took values through indices, this could be * made much more sensible. However, duk_handle_call() needs to fudge * the 'this' and 'func' values to handle bound function chains, which * is now done "in-place", so this is not a trivial change. */ DUK_ASSERT_CTX_VALID(ctx); obj_idx = duk_require_normalize_index(ctx, obj_idx); /* make absolute */ duk__call_prop_prep_stack(ctx, obj_idx, nargs); duk_call_method(ctx, nargs); }
DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_context *ctx, void *udata) { duk_idx_t obj_idx; duk_idx_t nargs; duk__pcall_prop_args *args; DUK_ASSERT_CTX_VALID(ctx); DUK_ASSERT(udata != NULL); args = (duk__pcall_prop_args *) udata; obj_idx = args->obj_idx; nargs = args->nargs; obj_idx = duk_require_normalize_index(ctx, obj_idx); /* make absolute */ duk__call_prop_prep_stack(ctx, obj_idx, nargs); duk_call_method(ctx, nargs); return 1; }
void duk_module_node_init(duk_context *ctx) { /* * Stack: [ ... options ] => [ ... ] */ duk_idx_t options_idx; duk_require_object_coercible(ctx, -1); /* error before setting up requireCache */ options_idx = duk_require_normalize_index(ctx, -1); /* Initialize the require cache to a fresh object. */ duk_push_global_stash(ctx); duk_push_bare_object(ctx); duk_put_prop_string(ctx, -2, "\xff" "requireCache"); duk_pop(ctx); /* Stash callbacks for later use. User code can overwrite them later * on directly by accessing the global stash. */ duk_push_global_stash(ctx); duk_get_prop_string(ctx, options_idx, "resolve"); duk_require_function(ctx, -1); duk_put_prop_string(ctx, -2, "\xff" "modResolve"); duk_get_prop_string(ctx, options_idx, "load"); duk_require_function(ctx, -1); duk_put_prop_string(ctx, -2, "\xff" "modLoad"); duk_pop(ctx); /* Stash main module. */ duk_push_global_stash(ctx); duk_push_undefined(ctx); duk_put_prop_string(ctx, -2, "\xff" "mainModule"); duk_pop(ctx); /* register `require` as a global function. */ duk_push_global_object(ctx); duk_push_string(ctx, "require"); duk__push_require_function(ctx, ""); duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_SET_WRITABLE | DUK_DEFPROP_SET_CONFIGURABLE); duk_pop(ctx); duk_pop(ctx); /* pop argument */ }
bytearray_t* duk_require_sphere_bytearray(duk_context* ctx, duk_idx_t index) { bytearray_t* array; const char* type; index = duk_require_normalize_index(ctx, index); duk_require_object_coercible(ctx, index); if (!duk_get_prop_string(ctx, index, "\xFF" "sphere_type")) goto on_error; type = duk_get_string(ctx, -1); duk_pop(ctx); if (strcmp(type, "bytearray") != 0) goto on_error; duk_get_prop_string(ctx, index, "\xFF" "ptr"); array = duk_get_pointer(ctx, -1); duk_pop(ctx); return array; on_error: duk_error_ni(ctx, -1, DUK_ERR_TYPE_ERROR, "Not a Sphere byte array"); }
font_t* duk_require_sphere_font(duk_context* ctx, duk_idx_t index) { font_t* font; const char* type; index = duk_require_normalize_index(ctx, index); duk_require_object_coercible(ctx, index); if (!duk_get_prop_string(ctx, index, "\xFF" "sphere_type")) goto on_error; type = duk_get_string(ctx, -1); duk_pop(ctx); if (strcmp(type, "font") != 0) goto on_error; duk_get_prop_string(ctx, index, "\xFF" "ptr"); font = duk_get_pointer(ctx, -1); duk_pop(ctx); return font; on_error: duk_error_ni(ctx, -1, DUK_ERR_TYPE_ERROR, "Object is not a Sphere font"); }
DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_context *ctx) { duk_idx_t obj_index; duk_idx_t nargs; /* Get the original arguments. Note that obj_index may be a relative * index so the stack must have the same top when we use it. */ DUK_ASSERT_CTX_VALID(ctx); obj_index = (duk_idx_t) duk_get_int(ctx, -2); nargs = (duk_idx_t) duk_get_int(ctx, -1); duk_pop_2(ctx); obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */ duk__call_prop_prep_stack(ctx, obj_index, nargs); duk_call_method(ctx, nargs); return 1; }
void duk_to_fixed_buffer(duk_context *ctx, duk_idx_t index) { duk_hbuffer_dynamic *h_src; duk_uint8_t *data; duk_size_t size; index = duk_require_normalize_index(ctx, index); h_src = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, index); DUK_ASSERT(h_src != NULL); if (!DUK_HBUFFER_HAS_DYNAMIC(h_src)) { return; } size = DUK_HBUFFER_GET_SIZE(h_src); data = (duk_uint8_t *) duk_push_fixed_buffer(ctx, size); if (size > 0U) { DUK_ASSERT(data != NULL); DUK_MEMCPY(data, DUK_HBUFFER_DYNAMIC_GET_CURR_DATA_PTR(h_src), size); } duk_replace(ctx, index); }
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; }
static duk_ret_t test_1a(duk_context *ctx) { duk_require_normalize_index(ctx, -3); return 0; }
//duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t index); duk_idx_t aperl_duk_require_normalize_index(duk_context *ctx, duk_idx_t index) { duk_idx_t ret = duk_require_normalize_index(ctx, index); return ret; }
void duk_err_augment_error(duk_hthread *thr, duk_hthread *thr_callstack, int err_index, const char *filename, int line, int noblame_fileline) { duk_context *ctx = (duk_context *) thr; duk_hobject *obj; DUK_ASSERT(thr != NULL); DUK_ASSERT(thr_callstack != NULL); DUK_ASSERT(ctx != NULL); err_index = duk_require_normalize_index(ctx, err_index); /* * Criteria for augmenting: * * - augmentation enabled in build (naturally) * - error value is an extensible object * - error value internal prototype chain contains the built-in * Error prototype object (i.e. 'val instanceof Error') */ obj = duk_require_hobject(ctx, err_index); if (!obj) { DUK_DDDPRINT("error value not an object, not augmented"); return; } if (!DUK_HOBJECT_HAS_EXTENSIBLE(obj)) { DUK_DDDPRINT("error value not extensible, not augmented"); return; } if (!duk_hobject_prototype_chain_contains(thr, obj, thr->builtins[DUK_BIDX_ERROR_PROTOTYPE])) { DUK_DDDPRINT("error value not inherited from Error, not augmented"); return; } /* Yes, augment error. */ #ifdef DUK_USE_TRACEBACKS /* * If tracebacks are enabled, the 'tracedata' property is the only * thing we need: 'fileName' and 'lineNumber' are virtual properties * which use 'tracedata'. */ if (duk_hobject_hasprop_raw(thr, obj, DUK_HTHREAD_STRING_TRACEDATA(thr))) { DUK_DDDPRINT("error value already has a 'traceback' property, not modifying it"); } else { add_traceback(thr, thr_callstack, obj, err_index, filename, line, noblame_fileline); } #else /* * If tracebacks are disabled, 'fileName' and 'lineNumber' are added * as plain own properties. Since Error.prototype has accessors of * the same name, we need to define own properties directly (cannot * just use e.g. duk_put_prop_stridx). Existing properties are not * overwritten in case they already exist. */ if (filename && !noblame_fileline) { /* FIXME: file/line is disabled in minimal builds, so disable this too * when appropriate. */ duk_push_string(ctx, filename); duk_def_prop_stridx(ctx, err_index, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE); duk_push_int(ctx, line); duk_def_prop_stridx(ctx, err_index, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE); } 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); func = act->func; if (func) { int pc; duk_uint32_t line; /* PC points to next instruction, find offending PC. Note that * PC == 0 for native code. */ pc = act->pc; if (pc > 0) { pc--; } DUK_ASSERT(pc >= 0 && (double) pc < DUK_DOUBLE_2TO32); /* assume PC is at most 32 bits and non-negative */ act = NULL; /* invalidated by pushes, so get out of the way */ duk_push_hobject(ctx, func); duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME); duk_def_prop_stridx(ctx, err_index, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE); if (DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) { #if 0 duk_push_number(ctx, pc); duk_def_prop_stridx(ctx, err_index, 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); /* FIXME: u32 */ duk_def_prop_stridx(ctx, err_index, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE); } duk_pop(ctx); } else { /* Native function, no relevant lineNumber. */ } duk_pop(ctx); } } #endif /* DUK_USE_TRACEBACKS */ }
static duk_ret_t test_1a(duk_context *ctx, void *udata) { (void) udata; duk_require_normalize_index(ctx, -3); return 0; }
DUK_EXTERNAL void duk_new(duk_context *ctx, duk_idx_t nargs) { /* * There are two [[Construct]] operations in the specification: * * - E5 Section 13.2.2: for Function objects * - E5 Section 15.3.4.5.2: for "bound" Function objects * * The chain of bound functions is resolved in Section 15.3.4.5.2, * with arguments "piling up" until the [[Construct]] internal * method is called on the final, actual Function object. Note * that the "prototype" property is looked up *only* from the * final object, *before* calling the constructor. * * Currently we follow the bound function chain here to get the * "prototype" property value from the final, non-bound function. * However, we let duk_handle_call() handle the argument "piling" * when the constructor is called. The bound function chain is * thus now processed twice. * * When constructing new Array instances, an unnecessary object is * created and discarded now: the standard [[Construct]] creates an * object, and calls the Array constructor. The Array constructor * returns an Array instance, which is used as the result value for * the "new" operation; the object created before the Array constructor * call is discarded. * * This would be easy to fix, e.g. by knowing that the Array constructor * will always create a replacement object and skip creating the fallback * object in that case. * * Note: functions called via "new" need to know they are called as a * constructor. For instance, built-in constructors behave differently * depending on how they are called. */ /* XXX: merge this with duk_js_call.c, as this function implements * core semantics (or perhaps merge the two files altogether). */ duk_hthread *thr = (duk_hthread *) ctx; duk_hobject *proto; duk_hobject *cons; duk_hobject *fallback; duk_idx_t idx_cons; duk_small_uint_t call_flags; DUK_ASSERT_CTX_VALID(ctx); /* [... constructor arg1 ... argN] */ idx_cons = duk_require_normalize_index(ctx, -nargs - 1); DUK_DDD(DUK_DDDPRINT("top=%ld, nargs=%ld, idx_cons=%ld", (long) duk_get_top(ctx), (long) nargs, (long) idx_cons)); /* XXX: code duplication */ /* * Figure out the final, non-bound constructor, to get "prototype" * property. */ duk_dup(ctx, idx_cons); for (;;) { cons = duk_get_hobject(ctx, -1); if (cons == NULL || !DUK_HOBJECT_HAS_CONSTRUCTABLE(cons)) { /* Checking constructability from anything else than the * initial constructor is not strictly necessary, but a * nice sanity check. */ goto not_constructable; } if (!DUK_HOBJECT_HAS_BOUNDFUNC(cons)) { break; } duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); /* -> [... cons target] */ duk_remove(ctx, -2); /* -> [... target] */ } DUK_ASSERT(cons != NULL && !DUK_HOBJECT_HAS_BOUNDFUNC(cons)); /* [... constructor arg1 ... argN final_cons] */ /* * Create "fallback" object to be used as the object instance, * unless the constructor returns a replacement value. * Its internal prototype needs to be set based on "prototype" * property of the constructor. */ duk_push_object(ctx); /* class Object, extensible */ /* [... constructor arg1 ... argN final_cons fallback] */ duk_get_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE); proto = duk_get_hobject(ctx, -1); if (!proto) { DUK_DDD(DUK_DDDPRINT("constructor has no 'prototype' property, or value not an object " "-> leave standard Object prototype as fallback prototype")); } else { DUK_DDD(DUK_DDDPRINT("constructor has 'prototype' property with object value " "-> set fallback prototype to that value: %!iO", (duk_heaphdr *) proto)); fallback = duk_get_hobject(ctx, -2); DUK_ASSERT(fallback != NULL); DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, fallback, proto); } duk_pop(ctx); /* [... constructor arg1 ... argN final_cons fallback] */ /* * Manipulate callstack for the call. */ duk_dup_top(ctx); duk_insert(ctx, idx_cons + 1); /* use fallback as 'this' value */ duk_insert(ctx, idx_cons); /* also stash it before constructor, * in case we need it (as the fallback value) */ duk_pop(ctx); /* pop final_cons */ /* [... fallback constructor fallback(this) arg1 ... argN]; * Note: idx_cons points to first 'fallback', not 'constructor'. */ DUK_DDD(DUK_DDDPRINT("before call, idx_cons+1 (constructor) -> %!T, idx_cons+2 (fallback/this) -> %!T, " "nargs=%ld, top=%ld", (duk_tval *) duk_get_tval(ctx, idx_cons + 1), (duk_tval *) duk_get_tval(ctx, idx_cons + 2), (long) nargs, (long) duk_get_top(ctx))); /* * Call the constructor function (called in "constructor mode"). */ call_flags = DUK_CALL_FLAG_CONSTRUCTOR_CALL; /* not protected, respect reclimit, is a constructor call */ duk_handle_call_unprotected(thr, /* thread */ nargs, /* num_stack_args */ call_flags); /* call_flags */ /* [... fallback retval] */ DUK_DDD(DUK_DDDPRINT("constructor call finished, fallback=%!iT, retval=%!iT", (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1))); /* * Determine whether to use the constructor return value as the created * object instance or not. */ if (duk_is_object(ctx, -1)) { duk_remove(ctx, -2); } else { duk_pop(ctx); } /* * Augment created errors upon creation (not when they are thrown or * rethrown). __FILE__ and __LINE__ are not desirable here; the call * stack reflects the caller which is correct. */ #ifdef DUK_USE_AUGMENT_ERROR_CREATE duk_hthread_sync_currpc(thr); duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/); #endif /* [... retval] */ return; not_constructable: #if defined(DUK_USE_VERBOSE_ERRORS) #if defined(DUK_USE_PARANOID_ERRORS) DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "%s not constructable", duk_get_type_name(ctx, -1)); #else DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "%s not constructable", duk_push_string_readable(ctx, -1)); #endif #else DUK_ERROR_TYPE(thr, "not constructable"); #endif }