duk_ret_t duk_bi_duktape_object_line(duk_context *ctx) { duk_hthread *thr = (duk_hthread *) ctx; duk_activation *act_caller; duk_hobject *h_func; duk_hbuffer_fixed *pc2line; duk_uint_fast32_t pc; duk_uint_fast32_t line; if (thr->callstack_top < 2) { return 0; } act_caller = thr->callstack + thr->callstack_top - 2; h_func = act_caller->func; DUK_ASSERT(h_func != NULL); if (!DUK_HOBJECT_HAS_COMPILEDFUNCTION(h_func)) { return 0; } /* FIXME: shared helper */ duk_push_hobject(ctx, h_func); duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_PC2LINE); if (duk_is_buffer(ctx, -1)) { pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1); DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line)); pc = (duk_uint_fast32_t) act_caller->pc; line = duk_hobject_pc2line_query(pc2line, (duk_uint_fast32_t) pc); } else { line = 0; } duk_push_int(ctx, (int) line); /* FIXME: typing */ return 1; }
static duk_ret_t dukzip_zip_write(duk_context *ctx) { int res = ZIP_OK; zipFile archive = dukzip_zip_from_this(ctx); if (duk_is_string(ctx, 0)) { int outputl = 0; const char *output = duk_get_lstring(ctx, 0, &outputl); res = zipWriteInFileInZip(archive, output, outputl); } else if (duk_is_buffer(ctx, 0) || duk_is_object(ctx, 0)) { int outputl = 0; void *output = duk_require_buffer_data(ctx, 0, &outputl); res = zipWriteInFileInZip(archive, output, outputl); } else { duk_error(ctx, DUK_ERR_TYPE_ERROR, "unable to write argument to zip file (supported types: string, buffer)"); return -1; } if (res == ZIP_OK) { duk_push_true(ctx); } else { duk_push_false(ctx); } return 1; }
static int fileio_readfile(duk_context *ctx) { const char *filename = duk_to_string(ctx, 0); fileio_push_file_buffer(ctx, filename); if (!duk_is_buffer(ctx, -1)) { return DUK_RET_ERROR; } return 1; }
duk_ret_t duk_bi_duktape_object_act(duk_context *ctx) { duk_hthread *thr = (duk_hthread *) ctx; duk_activation *act; duk_hobject *h_func; duk_hbuffer_fixed *pc2line; duk_uint_fast32_t pc; duk_uint_fast32_t line; duk_int_t level; /* -1 = top callstack entry, callstack[callstack_top - 1] * -callstack_top = bottom callstack entry, callstack[0] */ level = duk_to_int(ctx, 0); if (level >= 0 || -level > (duk_int_t) thr->callstack_top) { return 0; } DUK_ASSERT(level >= -((duk_int_t) thr->callstack_top) && level <= -1); act = thr->callstack + thr->callstack_top + level; duk_push_object(ctx); h_func = act->func; DUK_ASSERT(h_func != NULL); duk_push_hobject(ctx, h_func); pc = (duk_uint_fast32_t) act->pc; duk_push_int(ctx, (int) pc); /* FIXME: typing */ duk_get_prop_stridx(ctx, -2, DUK_STRIDX_INT_PC2LINE); if (duk_is_buffer(ctx, -1)) { pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1); DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line)); pc = (duk_uint_fast32_t) act->pc; line = duk_hobject_pc2line_query(pc2line, (duk_uint_fast32_t) pc); } else { line = 0; } duk_pop(ctx); duk_push_int(ctx, (int) line); /* FIXME: typing */ /* Providing access to e.g. act->lex_env would be dangerous: these * internal structures must never be accessible to the application. * Duktape relies on them having consistent data, and this consistency * is only asserted for, not checked for. */ /* [ level obj func pc line ] */ /* FIXME: version specific array format instead? */ duk_def_prop_stridx(ctx, -4, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WEC); duk_def_prop_stridx(ctx, -3, DUK_STRIDX_PC, DUK_PROPDESC_FLAGS_WEC); duk_def_prop_stridx(ctx, -2, DUK_STRIDX_LC_FUNCTION, DUK_PROPDESC_FLAGS_WEC); return 1; }
DUK_INTERNAL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context *ctx) { duk_hthread *thr = (duk_hthread *) ctx; duk_hobject *h; duk_bool_t is_freeze; DUK_ASSERT_TOP(ctx, 1); is_freeze = (duk_bool_t) duk_get_current_magic(ctx); if (duk_is_buffer(ctx, 0)) { /* Plain buffer: already sealed, but not frozen (and can't be frozen * because index properties can't be made non-writable. */ if (is_freeze) { goto fail_cannot_freeze; } return 1; } else if (duk_is_lightfunc(ctx, 0)) { /* Lightfunc: already sealed and frozen, success. */ return 1; } #if 0 /* Seal/freeze are quite rare in practice so it'd be nice to get the * correct behavior simply via automatic promotion (at the cost of some * memory churn). However, the promoted objects don't behave the same, * e.g. promoted lightfuncs are extensible. */ h = duk_require_hobject_promote_mask(ctx, 0, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER); #endif h = duk_get_hobject(ctx, 0); if (h == NULL) { /* ES2015 Sections 19.1.2.5, 19.1.2.17 */ return 1; } if (is_freeze && DUK_HOBJECT_IS_BUFOBJ(h)) { /* Buffer objects cannot be frozen because there's no internal * support for making virtual array indices non-writable. */ DUK_DD(DUK_DDPRINT("cannot freeze a buffer object")); goto fail_cannot_freeze; } duk_hobject_object_seal_freeze_helper(thr, h, is_freeze); /* Sealed and frozen objects cannot gain any more properties, * so this is a good time to compact them. */ duk_hobject_compact_props(thr, h); return 1; fail_cannot_freeze: DUK_DCERROR_TYPE_INVALID_ARGS(thr); /* XXX: proper error message */ }
int duk_bi_buffer_constructor(duk_context *ctx) { if (duk_get_top(ctx) == 0) { (void) duk_push_fixed_buffer(ctx, 0); } else { duk_to_buffer(ctx, 0, NULL); } DUK_ASSERT(duk_is_buffer(ctx, 0)); duk_set_top(ctx, 1); if (duk_is_constructor_call(ctx)) { duk_push_object_helper(ctx, DUK_HOBJECT_FLAG_EXTENSIBLE | DUK_HOBJECT_FLAG_SPECIAL_BUFFEROBJ | DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), DUK_BIDX_BUFFER_PROTOTYPE); /* Buffer object internal value is immutable */ duk_dup(ctx, 0); duk_def_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); } /* Note: unbalanced stack on purpose */ return 1; }
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; }
void test(duk_context *ctx) { duk_idx_t i, n; /* * push test values */ /* 0 */ duk_push_undefined(ctx); /* 1 */ duk_push_null(ctx); /* 2 */ duk_push_true(ctx); /* 3 */ duk_push_false(ctx); /* 4 */ duk_push_int(ctx, 123); /* 5 */ duk_push_number(ctx, 123.4); /* 6 */ duk_push_nan(ctx); /* 7 */ duk_push_number(ctx, INFINITY); /* 8 */ duk_push_number(ctx, -INFINITY); /* 9 */ duk_push_string(ctx, ""); /* 10 */ duk_push_string(ctx, "foo"); /* 11 */ duk_push_object(ctx); /* 12 */ duk_push_array(ctx); /* 13 */ duk_push_c_function(ctx, my_c_func, DUK_VARARGS); /* 14 */ duk_push_string(ctx, "(function() { print('hello'); })"); duk_eval(ctx); /* 15 */ duk_push_string(ctx, "escape.bind(null, 'foo')"); duk_eval(ctx); /* 16 */ duk_push_thread(ctx); /* 17 */ duk_push_buffer(ctx, 1024, 0 /*dynamic*/); /* 18 */ duk_push_buffer(ctx, 1024, 1 /*dynamic*/); /* 19 */ duk_push_pointer(ctx, (void *) 0xf00); /* * call checkers for each */ n = duk_get_top(ctx); for (i = 0; i < n; i++) { printf("%02ld: ", (long) i); printf(" und=%d", (int) duk_is_undefined(ctx, i)); printf(" null=%d", (int) duk_is_null(ctx, i)); printf(" noru=%d", (int) duk_is_null_or_undefined(ctx, i)); printf(" bool=%d", (int) duk_is_boolean(ctx, i)); printf(" num=%d", (int) duk_is_number(ctx, i)); printf(" nan=%d", (int) duk_is_nan(ctx, i)); printf(" str=%d", (int) duk_is_string(ctx, i)); printf(" obj=%d", (int) duk_is_object(ctx, i)); printf(" arr=%d", (int) duk_is_array(ctx, i)); printf(" fun=%d", (int) duk_is_function(ctx, i)); printf(" cfun=%d", (int) duk_is_c_function(ctx, i)); printf(" efun=%d", (int) duk_is_ecmascript_function(ctx, i)); printf(" bfun=%d", (int) duk_is_bound_function(ctx, i)); printf(" call=%d", (int) duk_is_callable(ctx, i)); printf(" thr=%d", (int) duk_is_thread(ctx, i)); printf(" buf=%d", (int) duk_is_buffer(ctx, i)); printf(" dyn=%d", (int) duk_is_dynamic_buffer(ctx, i)); printf(" fix=%d", (int) duk_is_fixed_buffer(ctx, i)); printf(" ptr=%d", (int) duk_is_pointer(ctx, i)); printf(" prim=%d", (int) duk_is_primitive(ctx, i)); printf(" objcoerc=%d", (int) duk_is_object_coercible(ctx, i)); printf("\n"); } }
int duk_bi_buffer_constructor(duk_context *ctx) { duk_size_t buf_size; duk_small_int_t buf_dynamic; duk_uint8_t *buf_data; const duk_uint8_t *src_data; duk_hobject *h_obj; /* * Constructor arguments are currently somewhat compatible with * (keep it that way if possible): * * http://nodejs.org/api/buffer.html * */ buf_dynamic = duk_get_boolean(ctx, 1); /* default to false */ switch (duk_get_type(ctx, 0)) { case DUK_TYPE_NUMBER: /* new buffer of specified size */ buf_size = (duk_size_t) duk_to_int(ctx, 0); (void) duk_push_buffer(ctx, buf_size, buf_dynamic); break; case DUK_TYPE_BUFFER: /* return input buffer, converted to a Buffer object if called as a * constructor (no change if called as a function). */ duk_set_top(ctx, 1); break; case DUK_TYPE_STRING: /* new buffer with string contents */ src_data = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &buf_size); DUK_ASSERT(src_data != NULL); /* even for zero-length string */ buf_data = (duk_uint8_t *) duk_push_buffer(ctx, buf_size, buf_dynamic); DUK_MEMCPY((void *) buf_data, (const void *) src_data, (size_t) buf_size); break; case DUK_TYPE_OBJECT: /* Buffer object: get the plain buffer inside. If called as as * constructor, a new Buffer object pointing to the same plain * buffer is created below. */ h_obj = duk_get_hobject(ctx, 0); DUK_ASSERT(h_obj != NULL); if (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) != DUK_HOBJECT_CLASS_BUFFER) { return DUK_RET_TYPE_ERROR; } duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_VALUE); DUK_ASSERT(duk_is_buffer(ctx, -1)); break; case DUK_TYPE_NONE: default: return DUK_RET_TYPE_ERROR; } /* stack is unbalanced, but: [ <something> buf ] */ if (duk_is_constructor_call(ctx)) { duk_push_object_helper(ctx, DUK_HOBJECT_FLAG_EXTENSIBLE | DUK_HOBJECT_FLAG_EXOTIC_BUFFEROBJ | DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), DUK_BIDX_BUFFER_PROTOTYPE); /* Buffer object internal value is immutable */ duk_dup(ctx, -2); duk_def_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); } /* Note: unbalanced stack on purpose */ return 1; }
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 */ }
//duk_bool_t duk_is_buffer(duk_context *ctx, duk_idx_t index); duk_bool_t aperl_duk_is_buffer(duk_context *ctx, duk_idx_t index) { duk_bool_t ret = duk_is_buffer(ctx, index); return ret; }
static void duk__err_augment_builtin_throw(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, int line, int noblame_fileline, duk_hobject *obj) { duk_context *ctx = (duk_context *) thr; #ifdef DUK_USE_ASSERTIONS duk_int_t entry_top; #endif #ifdef DUK_USE_ASSERTIONS entry_top = duk_get_top(ctx); #endif DUK_ASSERT(obj != NULL); DUK_UNREF(obj); /* unreferenced w/o tracebacks */ DUK_UNREF(ctx); /* unreferenced w/ tracebacks */ #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_DDD(DUK_DDDPRINT("error value already has a 'tracedata' property, not modifying it")); } else { duk__add_traceback(thr, thr_callstack, 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, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE); duk_push_int(ctx, line); duk_def_prop_stridx(ctx, -2, 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); /* [ ... error func ] */ duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME); duk_def_prop_stridx(ctx, -3, 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, -3, 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); /* -> [ ... error func pc2line line ] */ /* FIXME: u32 */ duk_def_prop_stridx(ctx, -4, 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 */ #ifdef DUK_USE_ASSERTIONS DUK_ASSERT(duk_get_top(ctx) == entry_top); #endif }
static int duk__traceback_getter_helper(duk_context *ctx, int output_type) { duk_hthread *thr = (duk_hthread *) ctx; int idx_td; int i; const char *str_tailcalled = " tailcalled"; const char *str_strict = " strict"; const char *str_construct = " construct"; const char *str_prevyield = " preventsyield"; const char *str_directeval = " directeval"; const char *str_empty = ""; DUK_ASSERT_TOP(ctx, 0); /* fixed arg count */ duk_push_this(ctx); duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TRACEDATA); idx_td = duk_get_top_index(ctx); duk_push_hstring_stridx(ctx, DUK_STRIDX_NEWLINE_TAB); duk_push_this(ctx); duk_to_string(ctx, -1); /* [ ... this tracedata sep ToString(this) ] */ /* FIXME: skip null filename? */ if (duk_check_type(ctx, idx_td, DUK_TYPE_OBJECT)) { int t; /* Current tracedata contains 2 entries per callstack entry. */ for (i = 0; ; i += 2) { int pc; int line; int flags; double d; const char *funcname; duk_hobject *h_func; duk_hstring *h_name; duk_hbuffer_fixed *pc2line; duk_require_stack(ctx, 5); duk_get_prop_index(ctx, idx_td, i); duk_get_prop_index(ctx, idx_td, i + 1); d = duk_to_number(ctx, -1); pc = (int) fmod(d, DUK_DOUBLE_2TO32); flags = (int) floor(d / DUK_DOUBLE_2TO32); t = duk_get_type(ctx, -2); if (t == DUK_TYPE_OBJECT) { /* * Ecmascript/native function call */ /* [ ... v1(func) v2(pc+flags) ] */ h_func = duk_get_hobject(ctx, -2); DUK_ASSERT(h_func != NULL); duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); duk_get_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME); duk_get_prop_stridx(ctx, -4, DUK_STRIDX_INT_PC2LINE); if (duk_is_buffer(ctx, -1)) { pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1); DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line)); line = duk_hobject_pc2line_query(pc2line, (duk_uint_fast32_t) pc); } else { line = 0; } duk_pop(ctx); /* [ ... v1 v2 name filename ] */ if (output_type == DUK__OUTPUT_TYPE_FILENAME) { return 1; } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { duk_push_int(ctx, line); return 1; } h_name = duk_get_hstring(ctx, -2); /* may be NULL */ funcname = (h_name == NULL || h_name == DUK_HTHREAD_STRING_EMPTY_STRING(thr)) ? "anon" : (const char *) DUK_HSTRING_GET_DATA(h_name); if (DUK_HOBJECT_HAS_NATIVEFUNCTION(h_func)) { duk_push_sprintf(ctx, "%s %s native%s%s%s%s%s", funcname, duk_get_string(ctx, -1), (flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty, (flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty, (flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty, (flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty, (flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty); } else { duk_push_sprintf(ctx, "%s %s:%d%s%s%s%s%s", funcname, duk_get_string(ctx, -1), line, (flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty, (flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty, (flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty, (flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty, (flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty); } duk_replace(ctx, -5); /* [ ... v1 v2 name filename str ] -> [ ... str v2 name filename ] */ duk_pop_n(ctx, 3); /* -> [ ... str ] */ } else if (t == DUK_TYPE_STRING) { /* * __FILE__ / __LINE__ entry, here 'pc' is line number directly. * Sometimes __FILE__ / __LINE__ is reported as the source for * the error (fileName, lineNumber), sometimes not. */ /* [ ... v1(filename) v2(line+flags) ] */ if (!(flags & DUK_TB_FLAG_NOBLAME_FILELINE)) { if (output_type == DUK__OUTPUT_TYPE_FILENAME) { duk_pop(ctx); return 1; } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { duk_push_int(ctx, pc); return 1; } } duk_push_sprintf(ctx, "%s:%d", duk_get_string(ctx, -2), pc); duk_replace(ctx, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */ duk_pop(ctx); /* -> [ ... str ] */ } else { /* unknown, ignore */ duk_pop_2(ctx); break; } } if (i >= DUK_USE_TRACEBACK_DEPTH * 2) { /* Possibly truncated; there is no explicit truncation * marker so this is the best we can do. */ duk_push_hstring_stridx(ctx, DUK_STRIDX_BRACKETED_ELLIPSIS); } } /* [ ... this tracedata sep ToString(this) str1 ... strN ] */ if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) { return 0; } else { duk_join(ctx, duk_get_top(ctx) - (idx_td + 2) /*count, not including sep*/); return 1; } }
duk_bool_t dschema_is_data(duk_context* ctx, duk_idx_t index) { return duk_is_string(ctx, index) || duk_is_buffer(ctx, index); }
static duk_ret_t dukzip_zip_add(duk_context *ctx) { zip_fileinfo zi = {0}; int res = ZIP_OK; zipFile archive = dukzip_zip_from_this(ctx); const char *filename = ""; duk_int_t level = Z_DEFAULT_COMPRESSION; duk_int_t method = Z_DEFLATED; const char *comment = ""; int datalen = 0; void *data = NULL; if (duk_is_object(ctx, 0)) { dukzip_zip_checkoptions(ctx, 0, &filename, &level, &method, &comment); duk_get_prop_string(ctx, 0, "data"); if (duk_is_string(ctx, -1)) { data = (void *)duk_get_lstring(ctx, -1, &datalen); } else if (duk_is_buffer(ctx, -1) || duk_is_object(ctx, -1)) { data = duk_require_buffer_data(ctx, -1, &datalen); } else { duk_error(ctx, DUK_ERR_TYPE_ERROR, "unable to write data to zip file (supported types: string, buffer)"); return -1; } } else { filename = duk_require_string(ctx, 0); if (duk_is_string(ctx, 1)) { data = (void *)duk_get_lstring(ctx, 1, &datalen); } else if (duk_is_buffer(ctx, 1) || duk_is_object(ctx, 1)) { data = duk_require_buffer_data(ctx, 1, &datalen); } else { duk_error(ctx, DUK_ERR_TYPE_ERROR, "unable to write argument to zip file (supported types: string, buffer)"); return -1; } if (duk_is_number(ctx, 2)) { level = duk_get_int(ctx, 2); } /* push dummy to normalize stack */ duk_push_string(ctx, "dummy"); } res = zipOpenNewFileInZip64(archive, filename, &zi, NULL, 0, NULL, 0, comment, method, level, 1); if (res != ZIP_OK) { goto error; } res = zipWriteInFileInZip(archive, data, datalen); if (res != ZIP_OK) { goto error; } res = zipCloseFileInZip(archive); duk_pop(ctx); /* pop buffer (or dummy) from stack */ if (res == ZIP_OK) { duk_push_true(ctx); } else { duk_push_false(ctx); } return 1; error: zipCloseFileInZip(archive); duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "could not write file '%s'", filename); return -1; }
/* Push file as a string. */ void fileio_push_file_string(duk_context *ctx, const char *filename) { fileio_push_file_buffer(ctx, filename); if (duk_is_buffer(ctx, -1)) { duk_to_string(ctx, -1); } }