static int format_parts_iso8601(duk_context *ctx, int *parts, int tzoffset, int flags) { char yearstr[8]; /* "-123456\0" */ char tzstr[8]; /* "+11:22\0" */ char sep = (flags & FLAG_SEP_T) ? 'T' : ' '; DUK_ASSERT(parts[IDX_MONTH] >= 1 && parts[IDX_MONTH] <= 12); DUK_ASSERT(parts[IDX_DAY] >= 1 && parts[IDX_DAY] <= 31); DUK_ASSERT(parts[IDX_YEAR] >= -999999 && parts[IDX_YEAR] <= 999999); /* Note: %06d for positive value, %07d for negative value to include * sign and 6 digits. */ DUK_SNPRINTF(yearstr, sizeof(yearstr), (parts[IDX_YEAR] >= 0 && parts[IDX_YEAR] <= 9999) ? "%04d" : ((parts[IDX_YEAR] >= 0) ? "+%06d" : "%07d"), parts[IDX_YEAR]); yearstr[sizeof(yearstr) - 1] = (char) 0; if (flags & FLAG_LOCALTIME) { /* tzoffset seconds are dropped */ if (tzoffset >= 0) { int tmp = tzoffset / 60; DUK_SNPRINTF(tzstr, sizeof(tzstr), "+%02d:%02d", tmp / 60, tmp % 60); } else { int tmp = -tzoffset / 60; DUK_SNPRINTF(tzstr, sizeof(tzstr), "-%02d:%02d", tmp / 60, tmp % 60); } tzstr[sizeof(tzstr) - 1] = (char) 0; } else { tzstr[0] = 'Z'; tzstr[1] = (char) 0; } if ((flags & FLAG_TOSTRING_DATE) && (flags & FLAG_TOSTRING_TIME)) { duk_push_sprintf(ctx, "%s-%02d-%02d%c%02d:%02d:%02d.%03d%s", yearstr, parts[IDX_MONTH], parts[IDX_DAY], sep, parts[IDX_HOUR], parts[IDX_MINUTE], parts[IDX_SECOND], parts[IDX_MILLISECOND], tzstr); } else if (flags & FLAG_TOSTRING_DATE) { duk_push_sprintf(ctx, "%s-%02d-%02d", yearstr, parts[IDX_MONTH], parts[IDX_DAY]); } else { DUK_ASSERT(flags & FLAG_TOSTRING_TIME); duk_push_sprintf(ctx, "%02d:%02d:%02d.%03d%s", parts[IDX_HOUR], parts[IDX_MINUTE], parts[IDX_SECOND], parts[IDX_MILLISECOND], tzstr); } return 1; }
void test(duk_context *ctx) { const char *fmt = "%d%d%d%d%d%d%d%d" CHARS40 CHARS40 CHARS40 CHARS40 CHARS40 CHARS40; /* The bug only manifests when the initial vsnprintf() attempt * runs out of buffer space. The initial buffer guess is * min(256, strlen(fmt) + 16). * * Here the format string is 16 + 240 bytes, so the guess will * be 256 bytes. The formatted string will be 8x5 + 240 bytes * = 280 bytes, so a retry happens. */ printf("about to push, strlen(fmt)=%d\n", (int) strlen(fmt)); fflush(stdout); duk_push_sprintf(ctx, fmt, 12345, 23456, 12345, 23456, 12345, 23456, 12345, 23456); printf("push done\n"); fflush(stdout); printf("result: %s\n", duk_get_string(ctx, -1)); fflush(stdout); }
DUK_EXTERNAL void duk_push_context_dump(duk_context *ctx) { duk_idx_t idx; duk_idx_t top; /* We don't duk_require_stack() here now, but rely on the caller having * enough space. */ top = duk_get_top(ctx); duk_push_array(ctx); for (idx = 0; idx < top; idx++) { duk_dup(ctx, idx); duk_put_prop_index(ctx, -2, idx); } /* XXX: conversion errors should not propagate outwards. * Perhaps values need to be coerced individually? */ duk_bi_json_stringify_helper(ctx, duk_get_top_index(ctx), /*idx_value*/ DUK_INVALID_INDEX, /*idx_replacer*/ DUK_INVALID_INDEX, /*idx_space*/ DUK_JSON_FLAG_EXT_CUSTOM | DUK_JSON_FLAG_ASCII_ONLY | DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/); duk_push_sprintf(ctx, "ctx: top=%ld, stack=%s", (long) top, (const char *) duk_safe_to_string(ctx, -1)); duk_replace(ctx, -3); /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */ duk_pop(ctx); DUK_ASSERT(duk_is_string(ctx, -1)); }
DUK_LOCAL void duk__add_compiler_error_line(duk_hthread *thr) { duk_context *ctx; /* Append a "(line NNN)" to the "message" property of any error * thrown during compilation. Usually compilation errors are * SyntaxErrors but they can also be out-of-memory errors and * the like. */ /* [ ... error ] */ ctx = (duk_context *) thr; DUK_ASSERT(duk_is_object(ctx, -1)); if (!(thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL)) { return; } DUK_DDD(DUK_DDDPRINT("compile error, before adding line info: %!T", (duk_tval *) duk_get_tval(ctx, -1))); if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_MESSAGE)) { duk_push_sprintf(ctx, " (line %ld)", (long) thr->compile_ctx->curr_token.start_line); duk_concat(ctx, 2); duk_put_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE); } else { duk_pop(ctx); } DUK_DDD(DUK_DDDPRINT("compile error, after adding line info: %!T", (duk_tval *) duk_get_tval(ctx, -1))); }
static duk_ret_t test_basic(duk_context *ctx) { char buf[65536 + 1024]; duk_size_t fmt_len, i; double len_sum = 0.0; for (fmt_len = 0; fmt_len <= 65536; fmt_len ++) { for (i = 0; i < fmt_len; i++) { buf[i] = 'x'; } buf[fmt_len + 0] = '%'; buf[fmt_len + 1] = 'd'; buf[fmt_len + 2] = '\0'; duk_push_sprintf(ctx, buf, 123); len_sum += (double) duk_get_length(ctx, -1); /* trivial "checksum" */ duk_pop(ctx); } /* Length sequence is 3, 4, ..., 65539 -> * (65539 + 3)/2 * (65539 - 3 + 1) = 2147713027 * * >>> res = 0 * >>> for i in xrange(0, 65536+1): * ... res += len( (('x' * i) + '%d') % 123 ) * ... * >>> res * 2147713027 */ printf("length sum: %lf\n", len_sum); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t cb_load_module(duk_context *ctx) { const char *filename; const char *module_id; module_id = duk_require_string(ctx, 0); duk_get_prop_string(ctx, 2, "filename"); filename = duk_require_string(ctx, -1); printf("load_cb: id:'%s', filename:'%s'\n", module_id, filename); if (strcmp(module_id, "pig.js") == 0) { duk_push_sprintf(ctx, "module.exports = 'you\\'re about to get eaten by %s';", module_id); } else if (strcmp(module_id, "cow.js") == 0) { duk_push_string(ctx, "module.exports = require('pig');"); } else if (strcmp(module_id, "ape.js") == 0) { duk_push_string(ctx, "module.exports = { module: module, __filename: __filename, wasLoaded: module.loaded };"); } else if (strcmp(module_id, "badger.js") == 0) { duk_push_string(ctx, "exports.foo = 123; exports.bar = 234;"); } else if (strcmp(module_id, "comment.js") == 0) { duk_push_string(ctx, "exports.foo = 123; exports.bar = 234; // comment"); } else if (strcmp(module_id, "shebang.js") == 0) { duk_push_string(ctx, "#!ignored\nexports.foo = 123; exports.bar = 234;"); } else { duk_error(ctx, DUK_ERR_TYPE_ERROR, "cannot find module: %s", module_id); } return 1; }
duk_ret_t js_gpio_toString(duk_context *ctx) { duk_push_this(ctx); /* -> stack: [ this ] */ duk_get_prop_string(ctx, 0, "\xff""\xff""data"); myGPIO *mps = duk_to_pointer(ctx, -1); duk_pop(ctx); duk_push_sprintf(ctx, "%s",mps->name); return 1; }
duk_ret_t js_myPlugin_info(duk_context *ctx) { duk_push_this(ctx); /* -> stack: [ this ] */ duk_get_prop_string(ctx, 0, "\xff""\xff""data"); myPluginStructure *mps = duk_to_pointer(ctx, -1); duk_pop(ctx); duk_push_sprintf(ctx, "{ name : %s }",mps->name); return 1; }
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_EXTERNAL void duk_debugger_attach(duk_context *ctx, duk_debug_read_function read_cb, duk_debug_write_function write_cb, duk_debug_peek_function peek_cb, duk_debug_read_flush_function read_flush_cb, duk_debug_write_flush_function write_flush_cb, duk_debug_detached_function detached_cb, void *udata) { duk_hthread *thr = (duk_hthread *) ctx; duk_heap *heap; const char *str; duk_size_t len; DUK_ASSERT(ctx != NULL); DUK_ASSERT(read_cb != NULL); DUK_ASSERT(write_cb != NULL); /* Other callbacks are optional. */ heap = thr->heap; heap->dbg_read_cb = read_cb; heap->dbg_write_cb = write_cb; heap->dbg_peek_cb = peek_cb; heap->dbg_read_flush_cb = read_flush_cb; heap->dbg_write_flush_cb = write_flush_cb; heap->dbg_detached_cb = detached_cb; heap->dbg_udata = udata; /* Start in paused state. */ heap->dbg_processing = 0; heap->dbg_paused = 1; heap->dbg_state_dirty = 1; heap->dbg_step_type = 0; heap->dbg_step_thread = NULL; heap->dbg_step_csindex = 0; heap->dbg_step_startline = 0; heap->dbg_exec_counter = 0; heap->dbg_last_counter = 0; heap->dbg_last_time = 0.0; /* Send version identification and flush right afterwards. Note that * we must write raw, unframed bytes here. */ duk_push_sprintf(ctx, "%ld %ld %s %s\n", (long) DUK_DEBUG_PROTOCOL_VERSION, (long) DUK_VERSION, (const char *) DUK_GIT_DESCRIBE, (const char *) DUK_USE_TARGET_INFO); str = duk_get_lstring(ctx, -1, &len); DUK_ASSERT(str != NULL); duk_debug_write_bytes(thr, (const duk_uint8_t *) str, len); duk_debug_write_flush(thr); duk_pop(ctx); }
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); }
static duk_ret_t cb_resolve_module(duk_context *ctx) { const char *module_id; const char *parent_id; module_id = duk_require_string(ctx, 0); parent_id = duk_require_string(ctx, 1); duk_push_sprintf(ctx, "%s.js", module_id); printf("resolve_cb: id:'%s', parent-id:'%s', resolve-to:'%s'\n", module_id, parent_id, duk_get_string(ctx, -1)); return 1; }
void test(duk_context *ctx) { const char *res; duk_push_undefined(ctx); PRINTTOP(); duk_push_null(ctx); PRINTTOP(); duk_push_true(ctx); PRINTTOP(); duk_push_false(ctx); PRINTTOP(); duk_push_boolean(ctx, -1); PRINTTOP(); duk_push_boolean(ctx, 0); PRINTTOP(); duk_push_boolean(ctx, 1); PRINTTOP(); duk_push_number(ctx, 123.4); PRINTTOP(); duk_push_int(ctx, 234); PRINTTOP(); duk_push_nan(ctx); PRINTTOP(); res = duk_push_string(ctx, "foo"); PRINTRESTOP(); res = duk_push_string(ctx, "foo\0bar\0"); PRINTRESTOP(); res = duk_push_string(ctx, ""); PRINTRESTOP(); /* pushes empty */ res = duk_push_string(ctx, NULL); PRINTRESTOP(); /* pushes a NULL */ res = duk_push_lstring(ctx, "foobar", 4); PRINTRESTOP(); res = duk_push_lstring(ctx, "foob\0\0", 6); PRINTRESTOP(); res = duk_push_lstring(ctx, "\0", 1); PRINTRESTOP(); /* pushes 1-byte string (0x00) */ res = duk_push_lstring(ctx, "\0", 0); PRINTRESTOP(); /* pushes empty */ res = duk_push_lstring(ctx, NULL, 0); PRINTRESTOP(); /* pushes empty */ res = duk_push_lstring(ctx, NULL, 10); PRINTRESTOP(); /* pushes empty */ res = duk_push_sprintf(ctx, "foo"); PRINTRESTOP(); res = duk_push_sprintf(ctx, "foo %d %s 0x%08lx", 123, "bar", (long) 0x1234cafe); PRINTRESTOP(); res = duk_push_sprintf(ctx, ""); PRINTRESTOP(); res = duk_push_sprintf(ctx, NULL); PRINTRESTOP(); res = test_vsprintf_3x_int(ctx, 2, 3, 5); PRINTRESTOP(); res = test_vsprintf_empty(ctx, 2, 3, 5); PRINTRESTOP(); res = test_vsprintf_null(ctx, 2, 3, 5); PRINTRESTOP(); duk_push_pointer(ctx, (void *) 0); PRINTTOP(); duk_push_pointer(ctx, (void *) 0xdeadbeef); PRINTTOP(); }
int duk_builtin_duk_object_addr(duk_context *ctx) { duk_tval *tv; void *p; tv = duk_get_tval(ctx, 0); if (!tv || !DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { return 0; /* undefined */ } p = (void *) DUK_TVAL_GET_HEAPHDR(tv); /* any heap allocated value (string, object, buffer) has a stable pointer */ duk_push_sprintf(ctx, "%p", p); return 1; }
const char * duk_js_error_to_string(duk_context *ctx, int index){ DEBUG_LOG("ScriptEngine","duk_js_error_to_string %d %d", index, duk_is_error(ctx, index)); if(duk_get_prop_string(ctx, index, "lineNumber")){ int lineNumber = duk_get_int(ctx, -1); duk_pop(ctx); duk_push_sprintf(ctx, " %s at javascript source %d line", duk_to_string(ctx, index), lineNumber); if(index < 0){ duk_replace(ctx, index - 1); }else{ duk_replace(ctx, index); } return duk_to_string(ctx,index); } duk_pop(ctx); return duk_to_string(ctx, index); }
// see http://duktape.org/guide.html#modules static int js_module_search(duk_context* ctx) { JSVM* vm = JSVM::GetJSVM(ctx); FileSystem* fs = vm->GetSubsystem<FileSystem>(); ResourceCache* cache = vm->GetSubsystem<ResourceCache>(); int top = duk_get_top(ctx); assert(top == 4); String moduleID = duk_to_string(ctx, 0); if (top > 1) { // require function assert(duk_is_function(ctx, 1)); } if (top > 2) { // exports assert(duk_is_object(ctx, 2)); } if (top > 3) { // module (module.id == a resolved absolute identifier for the module being loaded) assert(duk_is_object(ctx, 3)); } String pathName, fileName, extension; SplitPath(moduleID, pathName, fileName, extension); String path = moduleID; // Do we really want this? It is nice to not have to specify the Atomic path if (fileName.StartsWith("Atomic")) { path = "AtomicModules/" + path + ".js"; } else { path += ".js"; if (!cache->Exists(path)) { const Vector<String>& searchPaths = vm->GetModuleSearchPaths(); for (unsigned i = 0; i < searchPaths.Size(); i++) { String search = searchPaths[i] + path; if (cache->Exists(search)) { path = search; break; } } } } if (cache->Exists(path)) { SharedPtr<File> jsfile(cache->GetFile(path, false)); vm->SetLastModuleSearchFile(jsfile->GetFullPath()); String source; jsfile->ReadText(source); source.Append('\n'); duk_push_string(ctx, source.CString()); return 1; } else { // we're not a JS file, so check if we're a native module const Vector<String>& resourceDirs = cache->GetResourceDirs(); for (unsigned i = 0; i < resourceDirs.Size(); i++) { String pluginLibrary; // TODO: proper platform folder detection #ifdef ATOMIC_PLATFORM_WINDOWS pluginLibrary = resourceDirs.At(i) + "Plugins/Windows/x64/" + moduleID + ".dll"; #elif ATOMIC_PLATFORM_OSX pluginLibrary = resourceDirs.At(i) + "Plugins/Mac/x64/lib" + moduleID + ".dylib"; #endif if (pluginLibrary.Length() && fs->FileExists(pluginLibrary)) { // let duktape know we loaded a native module if (jsplugin_load(vm, pluginLibrary)) { duk_push_undefined(ctx); return 1; } else { duk_push_sprintf(ctx, "Failed loading native plugins: %s", pluginLibrary.CString()); duk_throw(ctx); } } } } duk_push_sprintf(ctx, "Failed loading module: %s", path.CString()); duk_throw(ctx); }
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_INTERNAL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx) { duk_tval *tv; /* * E5 Section 15.3.4.2 places few requirements on the output of * this function: * * - The result is an implementation dependent representation * of the function; in particular * * - The result must follow the syntax of a FunctionDeclaration. * In particular, the function must have a name (even in the * case of an anonymous function or a function with an empty * name). * * - Note in particular that the output does NOT need to compile * into anything useful. */ /* XXX: faster internal way to get this */ duk_push_this(ctx); tv = duk_get_tval(ctx, -1); DUK_ASSERT(tv != NULL); if (DUK_TVAL_IS_OBJECT(tv)) { duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv); const char *func_name; /* Function name: missing/undefined is mapped to empty string, * otherwise coerce to string. */ /* XXX: currently no handling for non-allowed identifier characters, * e.g. a '{' in the function name. */ duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); if (duk_is_undefined(ctx, -1)) { func_name = ""; } else { func_name = duk_to_string(ctx, -1); DUK_ASSERT(func_name != NULL); } /* Indicate function type in the function body using a dummy * directive. */ if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(obj)) { duk_push_sprintf(ctx, "function %s() {\"ecmascript\"}", (const char *) func_name); } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(obj)) { duk_push_sprintf(ctx, "function %s() {\"native\"}", (const char *) func_name); } else if (DUK_HOBJECT_HAS_BOUND(obj)) { duk_push_sprintf(ctx, "function %s() {\"bound\"}", (const char *) func_name); } else { goto type_error; } } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { duk_push_lightfunc_tostring(ctx, tv); } else { goto type_error; } return 1; type_error: return DUK_RET_TYPE_ERROR; }
DUK_LOCAL duk_ret_t duk__error_getter_helper(duk_context *ctx, duk_small_int_t output_type) { duk_hthread *thr = (duk_hthread *) ctx; duk_idx_t idx_td; duk_small_int_t i; /* traceback depth fits into 16 bits */ duk_small_int_t t; /* stack type fits into 16 bits */ duk_small_int_t count_func = 0; /* traceback depth ensures fits into 16 bits */ const char *str_tailcall = " tailcall"; 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_UNREF(thr); duk_push_this(ctx); duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TRACEDATA); idx_td = duk_get_top_index(ctx); duk_push_hstring_stridx(ctx, DUK_STRIDX_NEWLINE_4SPACE); duk_push_this(ctx); /* [ ... this tracedata sep this ] */ /* XXX: skip null filename? */ if (duk_check_type(ctx, idx_td, DUK_TYPE_OBJECT)) { /* Current tracedata contains 2 entries per callstack entry. */ for (i = 0; ; i += 2) { duk_int_t pc; duk_int_t line; duk_int_t flags; duk_double_t d; const char *funcname; const char *filename; duk_hobject *h_func; duk_hstring *h_name; 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 = (duk_int_t) DUK_FMOD(d, DUK_DOUBLE_2TO32); flags = (duk_int_t) DUK_FLOOR(d / DUK_DOUBLE_2TO32); t = (duk_small_int_t) duk_get_type(ctx, -2); if (t == DUK_TYPE_OBJECT || t == DUK_TYPE_LIGHTFUNC) { /* * Ecmascript/native function call or lightfunc call */ count_func++; /* [ ... v1(func) v2(pc+flags) ] */ h_func = duk_get_hobject(ctx, -2); /* NULL for lightfunc */ duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); duk_get_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME); #if defined(DUK_USE_PC2LINE) line = duk_hobject_pc2line_query(ctx, -4, (duk_uint_fast32_t) pc); #else line = 0; #endif /* [ ... v1 v2 name filename ] */ /* When looking for .fileName/.lineNumber, blame first * function which has a .fileName. */ if (duk_is_string(ctx, -1)) { if (output_type == DUK__OUTPUT_TYPE_FILENAME) { return 1; } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { duk_push_int(ctx, line); return 1; } } /* XXX: Change 'anon' handling here too, to use empty string for anonymous functions? */ /* XXX: Could be improved by coercing to a readable duk_tval (especially string escaping) */ 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); filename = duk_get_string(ctx, -1); filename = filename ? filename : ""; DUK_ASSERT(funcname != NULL); DUK_ASSERT(filename != NULL); if (h_func == NULL) { duk_push_sprintf(ctx, "at %s light%s%s%s%s%s", (const char *) funcname, (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); } else if (DUK_HOBJECT_HAS_NATFUNC(h_func)) { duk_push_sprintf(ctx, "at %s (%s) native%s%s%s%s%s", (const char *) funcname, (const char *) filename, (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); } else { duk_push_sprintf(ctx, "at %s (%s:%ld)%s%s%s%s%s", (const char *) funcname, (const char *) filename, (long) line, (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), (const char *) ((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) ] */ /* When looking for .fileName/.lineNumber, blame compilation * or C call site unless flagged not to do so. */ 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, "at [anon] (%s:%ld) internal", (const char *) duk_get_string(ctx, -2), (long) pc); duk_replace(ctx, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */ duk_pop(ctx); /* -> [ ... str ] */ } else { /* unknown, ignore */ duk_pop_2(ctx); break; } } if (count_func >= DUK_USE_TRACEBACK_DEPTH) { /* 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 this str1 ... strN ] */ if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) { return 0; } else { /* The 'this' after 'sep' will get ToString() coerced by * duk_join() automatically. We don't want to do that * coercion when providing .fileName or .lineNumber (GH-254). */ duk_join(ctx, duk_get_top(ctx) - (idx_td + 2) /*count, not including sep*/); return 1; } }
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; } }
int duk_bi_function_prototype_to_string(duk_context *ctx) { duk_tval *tv; /* * E5 Section 15.3.4.2 places few requirements on the output of * this function: * * - The result is an implementation dependent representation * of the function; in particular * * - The result must follow the syntax of a FunctionDeclaration. * In particular, the function must have a name (even in the * case of an anonymous function or a function with an empty * name). * * - Note in particular that the output does NOT need to compile * into anything useful. */ /* FIXME: faster internal way to get this */ duk_push_this(ctx); tv = duk_get_tval(ctx, -1); DUK_ASSERT(tv != NULL); if (DUK_TVAL_IS_OBJECT(tv)) { duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv); const char *func_name = "anonymous"; /* FIXME: rework, it would be nice to avoid C formatting functions to * ensure there are no Unicode issues. */ duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); if (!duk_is_undefined(ctx, -1)) { func_name = duk_to_string(ctx, -1); DUK_ASSERT(func_name != NULL); if (func_name[0] == (char) 0) { func_name = "empty"; } } if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(obj)) { /* FIXME: actual source, if available */ duk_push_sprintf(ctx, "function %s() {/* source code */}", func_name); } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(obj)) { duk_push_sprintf(ctx, "function %s() {/* native code */}", func_name); } else if (DUK_HOBJECT_HAS_BOUND(obj)) { duk_push_sprintf(ctx, "function %s() {/* bound */}", func_name); } else { goto type_error; } } else { goto type_error; } return 1; type_error: return DUK_RET_TYPE_ERROR; }
/* XXX: much to improve (code size) */ DUK_INTERNAL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx) { duk_hthread *thr = (duk_hthread *) ctx; duk_hobject *h_pattern; DUK_ASSERT_TOP(ctx, 2); h_pattern = duk_get_hobject(ctx, 0); if (!duk_is_constructor_call(ctx) && h_pattern != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h_pattern) == DUK_HOBJECT_CLASS_REGEXP && duk_is_undefined(ctx, 1)) { /* Called as a function, pattern has [[Class]] "RegExp" and * flags is undefined -> return object as is. */ duk_dup(ctx, 0); return 1; } /* Else functionality is identical for function call and constructor * call. */ if (h_pattern != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h_pattern) == DUK_HOBJECT_CLASS_REGEXP) { if (duk_is_undefined(ctx, 1)) { duk_bool_t flag_g, flag_i, flag_m; duk_get_prop_stridx(ctx, 0, DUK_STRIDX_SOURCE); flag_g = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_GLOBAL, NULL); flag_i = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_IGNORE_CASE, NULL); flag_m = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_MULTILINE, NULL); duk_push_sprintf(ctx, "%s%s%s", (const char *) (flag_g ? "g" : ""), (const char *) (flag_i ? "i" : ""), (const char *) (flag_m ? "m" : "")); /* [ ... pattern flags ] */ } else { return DUK_RET_TYPE_ERROR; } } else { if (duk_is_undefined(ctx, 0)) { duk_push_string(ctx, ""); } else { duk_dup(ctx, 0); duk_to_string(ctx, -1); } if (duk_is_undefined(ctx, 1)) { duk_push_string(ctx, ""); } else { duk_dup(ctx, 1); duk_to_string(ctx, -1); } /* [ ... pattern flags ] */ } DUK_DDD(DUK_DDDPRINT("RegExp constructor/function call, pattern=%!T, flags=%!T", (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1))); /* [ ... pattern flags ] */ duk_regexp_compile(thr); /* [ ... bytecode escaped_source ] */ duk_regexp_create_instance(thr); /* [ ... RegExp ] */ return 1; }
DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx) { duk_hstring *h_bc; duk_small_int_t re_flags; #if 0 /* A little tricky string approach to provide the flags string. * This depends on the specific flag values in duk_regexp.h, * which needs to be asserted for. In practice this doesn't * produce more compact code than the easier approach in use. */ const char *flag_strings = "gim\0gi\0gm\0g\0"; duk_uint8_t flag_offsets[8] = { (duk_uint8_t) 3, /* flags: "" */ (duk_uint8_t) 10, /* flags: "g" */ (duk_uint8_t) 5, /* flags: "i" */ (duk_uint8_t) 4, /* flags: "gi" */ (duk_uint8_t) 2, /* flags: "m" */ (duk_uint8_t) 7, /* flags: "gm" */ (duk_uint8_t) 1, /* flags: "im" */ (duk_uint8_t) 0, /* flags: "gim" */ }; DUK_ASSERT(DUK_RE_FLAG_GLOBAL == 1); DUK_ASSERT(DUK_RE_FLAG_IGNORE_CASE == 2); DUK_ASSERT(DUK_RE_FLAG_MULTILINE == 4); #endif duk__get_this_regexp(ctx); /* [ regexp ] */ duk_get_prop_stridx(ctx, 0, DUK_STRIDX_SOURCE); duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_BYTECODE); h_bc = duk_get_hstring(ctx, -1); DUK_ASSERT(h_bc != NULL); DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(h_bc) >= 1); DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h_bc) >= 1); DUK_ASSERT(DUK_HSTRING_GET_DATA(h_bc)[0] < 0x80); re_flags = (duk_small_int_t) DUK_HSTRING_GET_DATA(h_bc)[0]; /* [ regexp source bytecode ] */ #if 1 /* This is a cleaner approach and also produces smaller code than * the other alternative. Use duk_require_string() for format * safety (although the source property should always exist). */ duk_push_sprintf(ctx, "/%s/%s%s%s", (const char *) duk_require_string(ctx, -2), /* require to be safe */ (re_flags & DUK_RE_FLAG_GLOBAL) ? "g" : "", (re_flags & DUK_RE_FLAG_IGNORE_CASE) ? "i" : "", (re_flags & DUK_RE_FLAG_MULTILINE) ? "m" : ""); #else /* This should not be necessary because no-one should tamper with the * regexp bytecode, but is prudent to avoid potential segfaults if that * were to happen for some reason. */ re_flags &= 0x07; DUK_ASSERT(re_flags >= 0 && re_flags <= 7); /* three flags */ duk_push_sprintf(ctx, "/%s/%s", (const char *) duk_require_string(ctx, -2), (const char *) (flag_strings + flag_offsets[re_flags])); #endif return 1; }