static int es_gumbo_parse(duk_context *ctx) { duk_size_t len; const char *str = duk_to_lstring(ctx, 0, &len); es_gumbo_output_t *ego = calloc(1, sizeof(es_gumbo_output_t)); atomic_set(&ego->ego_refcount, 1); ego->ego_output = gumbo_parse_with_options(&kGumboDefaultOptions, str, len); /* for(int i = 0; i < ego->ego_output->errors.length; i++) { GumboError *ge = ego->ego_output->errors.data[i]; } */ duk_pop(ctx); duk_push_object(ctx); push_gumbo_node(ctx, ego->ego_output->document, ego); duk_put_prop_string(ctx, -2, "document"); push_gumbo_node(ctx, ego->ego_output->root, ego); duk_put_prop_string(ctx, -2, "root"); es_gumbo_output_release(ego); return 1; }
static duk_ret_t test_3(duk_context *ctx) { const char *p; duk_size_t sz; duk_set_top(ctx, 0); p = duk_to_lstring(ctx, DUK_INVALID_INDEX, &sz); printf("index DUK_INVALID_INDEX: %p, %ld\n", (void *) p, (unsigned long) sz); /* ok to print, not shown in expected case */ return 0; }
int test_2(duk_context *ctx) { const char *p; size_t sz; duk_set_top(ctx, 0); p = duk_to_lstring(ctx, 3, &sz); printf("index 3: %p, %d\n", (void *) p, (int) sz); /* ok to print, not shown in expected case */ 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; }
//const char *duk_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len); const char *aperl_duk_to_lstring(duk_context *ctx, duk_idx_t index, SV *out_len) { duk_size_t sz; const char *ret = duk_to_lstring(ctx, index, &sz); sv_setnv(out_len, sz); return ret; }
/* Log frontend shared helper, magic value indicates log level. Provides * frontend functions: trace(), debug(), info(), warn(), error(), fatal(). * This needs to have small footprint, reasonable performance, minimal * memory churn, etc. */ DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx) { duk_hthread *thr = (duk_hthread *) ctx; duk_double_t now; duk_small_int_t entry_lev = duk_get_current_magic(ctx); duk_small_int_t logger_lev; duk_int_t nargs; duk_int_t i; duk_size_t tot_len; const duk_uint8_t *arg_str; duk_size_t arg_len; duk_uint8_t *buf, *p; const duk_uint8_t *q; duk_uint8_t date_buf[DUK_BI_DATE_ISO8601_BUFSIZE]; duk_size_t date_len; duk_small_int_t rc; DUK_ASSERT(entry_lev >= 0 && entry_lev <= 5); /* XXX: sanitize to printable (and maybe ASCII) */ /* XXX: better multiline */ /* * Logger arguments are: * * magic: log level (0-5) * this: logger * stack: plain log args * * We want to minimize memory churn so a two-pass approach * is used: first pass formats arguments and computes final * string length, second pass copies strings either into a * pre-allocated and reused buffer (short messages) or into a * newly allocated fixed buffer. If the backend function plays * nice, it won't coerce the buffer to a string (and thus * intern it). */ nargs = duk_get_top(ctx); /* [ arg1 ... argN this ] */ /* * Log level check */ duk_push_this(ctx); duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_L); logger_lev = (duk_small_int_t) duk_get_int(ctx, -1); if (entry_lev < logger_lev) { return 0; } /* log level could be popped but that's not necessary */ now = duk_bi_date_get_now(ctx); duk_bi_date_format_timeval(now, date_buf); date_len = DUK_STRLEN((const char *) date_buf); duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LC_N); duk_to_string(ctx, -1); DUK_ASSERT(duk_is_string(ctx, -1)); /* [ arg1 ... argN this loggerLevel loggerName ] */ /* * Pass 1 */ /* Line format: <time> <entryLev> <loggerName>: <msg> */ tot_len = 0; tot_len += 3 + /* separators: space, space, colon */ 3 + /* level string */ date_len + /* time */ duk_get_length(ctx, -1); /* loggerName */ for (i = 0; i < nargs; i++) { /* When formatting an argument to a string, errors may happen from multiple * causes. In general we want to catch obvious errors like a toLogString() * throwing an error, but we don't currently try to catch every possible * error. In particular, internal errors (like out of memory or stack) are * not caught. Also, we expect Error toString() to not throw an error. */ if (duk_is_object(ctx, i)) { /* duk_pcall_prop() may itself throw an error, but we're content * in catching the obvious errors (like toLogString() throwing an * error). */ duk_push_hstring_stridx(ctx, DUK_STRIDX_FMT); duk_dup(ctx, i); /* [ arg1 ... argN this loggerLevel loggerName 'fmt' arg ] */ /* call: this.fmt(arg) */ rc = duk_pcall_prop(ctx, -5 /*obj_index*/, 1 /*nargs*/); if (rc) { /* Keep the error as the result (coercing it might fail below, * but we don't catch that now). */ ; } duk_replace(ctx, i); } (void) duk_to_lstring(ctx, i, &arg_len); tot_len++; /* sep (even before first one) */ tot_len += arg_len; } /* * Pass 2 */ if (tot_len <= DUK_BI_LOGGER_SHORT_MSG_LIMIT) { duk_hbuffer_dynamic *h_buf; DUK_DDD(DUK_DDDPRINT("reuse existing small log message buffer, tot_len %ld", (long) tot_len)); /* We can assert for all buffer properties because user code * never has access to heap->log_buffer. */ DUK_ASSERT(thr != NULL); DUK_ASSERT(thr->heap != NULL); h_buf = thr->heap->log_buffer; DUK_ASSERT(h_buf != NULL); DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) h_buf)); DUK_ASSERT(DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(h_buf) == DUK_BI_LOGGER_SHORT_MSG_LIMIT); /* Set buffer 'visible size' to actual message length and * push it to the stack. */ DUK_HBUFFER_SET_SIZE((duk_hbuffer *) h_buf, tot_len); duk_push_hbuffer(ctx, (duk_hbuffer *) h_buf); buf = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf); } else { DUK_DDD(DUK_DDDPRINT("use a one-off large log message buffer, tot_len %ld", (long) tot_len)); buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, tot_len); } DUK_ASSERT(buf != NULL); p = buf; DUK_MEMCPY((void *) p, (void *) date_buf, date_len); p += date_len; *p++ = (duk_uint8_t) DUK_ASC_SPACE; q = duk__log_level_strings + (entry_lev * 3); DUK_MEMCPY((void *) p, (void *) q, (duk_size_t) 3); p += 3; *p++ = (duk_uint8_t) DUK_ASC_SPACE; arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, -2, &arg_len); DUK_MEMCPY((void *) p, (const void *) arg_str, arg_len); p += arg_len; *p++ = (duk_uint8_t) DUK_ASC_COLON; for (i = 0; i < nargs; i++) { *p++ = (duk_uint8_t) DUK_ASC_SPACE; arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &arg_len); DUK_ASSERT(arg_str != NULL); DUK_MEMCPY((void *) p, (const void *) arg_str, arg_len); p += arg_len; } DUK_ASSERT(buf + tot_len == p); /* [ arg1 ... argN this loggerLevel loggerName buffer ] */ #if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_LOGGING) /* Do debugger forwarding before raw() because the raw() function * doesn't get the log level right now. */ if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { const char *log_buf; duk_size_t sz_buf; log_buf = (const char *) duk_get_buffer(ctx, -1, &sz_buf); DUK_ASSERT(log_buf != NULL); duk_debug_write_notify(thr, DUK_DBG_CMD_LOG); duk_debug_write_int(thr, (duk_int32_t) entry_lev); duk_debug_write_string(thr, (const char *) log_buf, sz_buf); duk_debug_write_eom(thr); } #endif /* Call this.raw(msg); look up through the instance allows user to override * the raw() function in the instance or in the prototype for maximum * flexibility. */ duk_push_hstring_stridx(ctx, DUK_STRIDX_RAW); duk_dup(ctx, -2); /* [ arg1 ... argN this loggerLevel loggerName buffer 'raw' buffer ] */ duk_call_prop(ctx, -6, 1); /* this.raw(buffer) */ return 0; }
static duk_ret_t test_1(duk_context *ctx) { duk_int_t i, j, n; char *ptr; duk_set_top(ctx, 0); duk_push_undefined(ctx); duk_push_null(ctx); duk_push_true(ctx); duk_push_false(ctx); duk_push_int(ctx, 1); duk_push_number(ctx, -123.456); duk_push_nan(ctx); duk_push_number(ctx, INFINITY); duk_push_number(ctx, -INFINITY); duk_push_string(ctx, ""); duk_push_string(ctx, "foo"); duk_push_lstring(ctx, "foo\0bar", 7); duk_push_object(ctx); duk_push_thread(ctx); (void) duk_push_fixed_buffer(ctx, 0); ptr = (char *) duk_push_fixed_buffer(ctx, 16); for (i = 0; i < 16; i++) { ptr[i] = (char) ('a' + i); } (void) duk_push_dynamic_buffer(ctx, 0); ptr = (char *) duk_push_dynamic_buffer(ctx, 16); for (i = 0; i < 16; i++) { ptr[i] = (char) ('a' + i); } duk_push_pointer(ctx, (void *) NULL); duk_push_pointer(ctx, (void *) 0xdeadbeef); n = duk_get_top(ctx); printf("top: %ld\n", (long) n); for (i = 0; i < n; i++) { const unsigned char *p; duk_size_t sz; duk_dup(ctx, i); sz = (duk_size_t) 0xdeadbeef; p = (const unsigned char *) duk_to_lstring(ctx, -1, &sz); printf("index %ld, string: '", (long) i); for (j = 0; j < sz; j++) { if (p[j] >= 0x20 && p[j] <= 0x7e) { printf("%c", (int) p[j]); } else { printf("\\x%02x", (int) p[j]); } } printf("', length %lu\n", (unsigned long) sz); duk_pop(ctx); duk_dup(ctx, i); sz = (duk_size_t) 0xdeadbeef; p = (const unsigned char *) duk_to_lstring(ctx, -1, NULL); printf("index %ld, string: '%s'\n", (long) i, (const char *) p); duk_pop(ctx); } return 0; }