/* duk_has_prop(), not an object */ int test_1e(duk_context *ctx) { int rc; duk_set_top(ctx, 0); duk_push_null(ctx); duk_push_string(ctx, "foo"); rc = duk_has_prop(ctx, -2); printf("null.foo -> rc=%d\n", rc); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
static gboolean gum_emit_module (const GumModuleDetails * details, gpointer user_data) { GumDukMatchContext * mc = user_data; GumDukScope * scope = mc->scope; duk_context * ctx = scope->ctx; gboolean proceed = TRUE; duk_push_heapptr (ctx, mc->on_match); duk_push_object (ctx); duk_push_string (ctx, details->name); duk_put_prop_string (ctx, -2, "name"); _gum_duk_push_native_pointer (ctx, GSIZE_TO_POINTER (details->range->base_address), scope->core); duk_put_prop_string (ctx, -2, "base"); duk_push_uint (ctx, details->range->size); duk_put_prop_string (ctx, -2, "size"); duk_push_string (ctx, details->path); duk_put_prop_string (ctx, -2, "path"); if (_gum_duk_scope_call_sync (scope, 1)) { if (duk_is_string (ctx, -1)) proceed = strcmp (duk_require_string (ctx, -1), "stop") != 0; } else { proceed = FALSE; } duk_pop (ctx); return proceed; }
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 main(int argc, char *argv[]) { /**(uint32_t *)0x40023830 |= 0x8; *(uint32_t *)0x40020C00 |= 0x55000000; *(uint32_t *)0x40020C14 = 0x00000000; while(1) { *(uint32_t *)0x40020C14 = 0x00000000; delay(); *(uint32_t *)0x40020C14 = 0x00008000; delay(); } duk_context *ctx = duk_create_heap_default(); duk_eval_string(ctx, "'Hello world!'"); duk_destroy_heap(ctx); */ *(uint32_t *)0x40023830 |= 0x8; *(uint32_t *)0x40020C00 |= 0x55000000; *(uint32_t *)0x40020C14 = 0x00000000; ctx = duk_create_heap_default(); duk_push_global_object(ctx); duk_push_c_function(ctx, LED_Toggle, DUK_VARARGS); duk_put_prop_string(ctx, -2, "LED_Toggle"); duk_push_string(ctx, "blink = function() { LED_Toggle(); };"); duk_safe_call(ctx, safeEval, 1, 1); while (1) { duk_push_global_object(ctx); duk_get_prop_string(ctx, -1, "blink"); duk_push_string(ctx, "0"); duk_safe_call(ctx, safeCall, 1, 0); duk_pop_2(ctx); //for(volatile int i = 0; i < 100000; i++); //delay(); } return 0; }
/* duk_del_prop(), success cases */ static duk_ret_t test_1a(duk_context *ctx) { duk_ret_t rc; prep(ctx); /* existing, configurable */ duk_push_string(ctx, "foo"); rc = duk_del_prop(ctx, 0); printf("delete obj.foo -> rc=%d\n", (int) rc); /* nonexistent */ duk_push_string(ctx, "nonexistent"); rc = duk_del_prop(ctx, 0); printf("delete obj.nonexistent -> rc=%d\n", (int) rc); /* nonexistent */ duk_push_int(ctx, 123); rc = duk_del_prop(ctx, 0); printf("delete obj[123] -> rc=%d\n", (int) rc); /* nonexistent, array */ duk_push_string(ctx, "nonexistent"); rc = duk_del_prop(ctx, 1); printf("delete arr.nonexistent -> rc=%d\n", (int) rc); /* existing, configurable, array */ duk_push_int(ctx, 2); rc = duk_del_prop(ctx, 1); printf("delete arr[2] -> rc=%d\n", (int) rc); duk_json_encode(ctx, 0); printf("final object: %s\n", duk_to_string(ctx, 0)); duk_json_encode(ctx, 1); printf("final array: %s\n", duk_to_string(ctx, 1)); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static void dukzip_push_unzfile(duk_context *ctx, unzFile archive, const char *filename) { /* create object with readable Dukzip archive prototype */ duk_push_object(ctx); duk_get_global_string(ctx, DUKZIP_UNZ_PROTOTYPE); duk_set_prototype(ctx, -2); /* set the archive pointer data */ duk_push_pointer(ctx, archive); duk_put_prop_string(ctx, -2, ZIPHANDLE_PROP); /* set path property */ duk_push_string(ctx, filename); duk_put_prop_string(ctx, -2, ZIPFILENAME_PROP); }
duk_ret_t test_top_1(duk_context *ctx, void *udata) { duk_int_t rc; (void) udata; duk_push_string(ctx, "dummy"); printf("top before: %ld\n", (long) duk_get_top(ctx)); rc = duk_pcompile(ctx, 0); printf("duk_pcompile() rc: %ld\n", (long) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t test_get_string(duk_context *ctx) { duk_idx_t i, n; duk_push_undefined(ctx); duk_push_null(ctx); duk_push_true(ctx); duk_push_false(ctx); duk_push_string(ctx, ""); duk_push_string(ctx, "foo"); duk_push_lstring(ctx, "foo\0bar", 7); duk_push_string(ctx, "\xe1\x88\xb4xyz"); /* 4 chars, first char utf-8 encoded U+1234 */ duk_push_nan(ctx); duk_push_object(ctx); n = duk_get_top(ctx); printf("top: %ld\n", (long) n); for (i = 0; i < n; i++) { printf("index %ld: ", (long) i); dump((const unsigned char *) duk_get_string(ctx, i)); } return 0; }
static int handle_fh(duk_context *ctx, FILE *f, const char *filename) { char *buf = NULL; int len; int got; int rc; int retval = -1; if (fseek(f, 0, SEEK_END) < 0) { goto error; } len = (int) ftell(f); if (fseek(f, 0, SEEK_SET) < 0) { goto error; } buf = (char *) malloc(len); if (!buf) { goto error; } got = fread((void *) buf, (size_t) 1, (size_t) len, f); duk_push_lstring(ctx, buf, got); duk_push_string(ctx, filename); free(buf); buf = NULL; interactive_mode = 0; /* global */ rc = duk_safe_call(ctx, wrapped_compile_execute, 2 /*nargs*/, 1 /*nret*/); if (rc != DUK_EXEC_SUCCESS) { print_pop_error(ctx, stderr); goto error; } else { duk_pop(ctx); retval = 0; } /* fall thru */ cleanup: if (buf) { free(buf); } return retval; error: fprintf(stderr, "error in executing file %s\n", filename); fflush(stderr); goto cleanup; }
static void duknode_push_argv(duk_context *ctx, int argc, const char *argv[]) { duk_get_global_string(ctx, "process"); duk_idx_t arg_array_index; int i; arg_array_index = duk_push_array(ctx); for (i = 0; i < argc; i++) { duk_push_string(ctx, argv[i]); duk_put_prop_index(ctx, arg_array_index, i); } duk_put_prop_string(ctx, -2, "argv"); duk_pop(ctx); }
/* duk_has_prop(), invalid index */ static duk_ret_t test_1b(duk_context *ctx, void *udata) { duk_ret_t rc; (void) udata; prep(ctx); duk_push_string(ctx, "foo"); rc = duk_has_prop(ctx, 234); printf("obj.foo -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* duk_get_prop(), not object coercible */ static duk_ret_t test_1e(duk_context *ctx) { duk_ret_t rc; duk_set_top(ctx, 0); duk_push_null(ctx); duk_push_string(ctx, "foo"); rc = duk_get_prop(ctx, -2); printf("null.foo -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
int w_LocalStorage_prototype_getItem(duk_context *ctx) { const char *key = duk_require_string(ctx, 0); duk_push_this(ctx); /* this */ duk_get_prop_string(ctx, -1, "__MURAL_DATA__"); /* this, __MURAL_DATA__ */ duk_get_prop_string(ctx, -1, key); /* this, __MURAL_DATA__, value */ const char *value = duk_to_string(ctx, -1); /* this, __MURAL_DATA__, string(value) */ duk_pop_3(ctx); duk_push_string(ctx, value); return 1; }
/*----------------------------------------------------------------------------*/ void kbjs_register_built_ins(duk_context *context) { /* STACK: [..., global] */ duk_push_global_object(context); /* STACK: [..., global, "sleep"] */ duk_push_string(context, SLEEP_FUNC_NAME); /* STACK: [..., global, "sleep", function] */ duk_push_c_function(context, kbjs_sleep, (duk_idx_t)1); /* STACK: [..., global] */ duk_put_prop(context, (duk_idx_t)-3); /* STACK: [...] */ duk_pop(context); }
void StyleContext::parseSceneGlobals(const YAML::Node& node, const std::string& key, int seqIndex, duk_idx_t dukObject) { switch(node.Type()) { case YAML::NodeType::Scalar: if (key.size() == 0) { duk_push_string(m_ctx, node.Scalar().c_str()); duk_put_prop_index(m_ctx, dukObject, seqIndex); } else { auto nodeValue = node.Scalar(); if (nodeValue.compare(0, 8, "function") == 0) { duk_push_string(m_ctx, key.c_str()); // push property key duk_push_string(m_ctx, nodeValue.c_str()); // push function string duk_push_string(m_ctx, ""); if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) { // compile function duk_put_prop(m_ctx, -3); // put {key: function()} } else { LOGW("Compile failed in global function: %s\n%s\n---", duk_safe_to_string(m_ctx, -1), nodeValue.c_str()); duk_pop(m_ctx); //pop error duk_pop(m_ctx); //pop key // push property as a string duk_push_string(m_ctx, nodeValue.c_str()); duk_put_prop_string(m_ctx, dukObject, key.c_str()); } } else { duk_push_string(m_ctx, nodeValue.c_str()); duk_put_prop_string(m_ctx, dukObject, key.c_str()); } } break; case YAML::NodeType::Sequence: { auto seqObj = duk_push_array(m_ctx); for (int i = 0; i < node.size(); i++) { parseSceneGlobals(node[i], "", i, seqObj); } duk_put_prop_string(m_ctx, seqObj-1, key.c_str()); break; } case YAML::NodeType::Map: { //duk_push_string(m_ctx, key.c_str()); auto mapObj = duk_push_object(m_ctx); for (auto& mapNode : node) { auto itemKey = mapNode.first.Scalar(); parseSceneGlobals(mapNode.second, itemKey, 0, mapObj); } duk_put_prop_string(m_ctx, mapObj-1, key.c_str()); } default: break; } return; }
DUK_INTERNAL duk_bool_t duk_bi_date_format_parts_strftime(duk_context *ctx, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags) { char buf[DUK__STRFTIME_BUF_SIZE]; struct tm tm; const char *fmt; DUK_UNREF(tzoffset); /* If the platform doesn't support the entire Ecmascript range, we need * to return 0 so that the caller can fall back to the default formatter. * * For now, assume that if time_t is 8 bytes or more, the whole Ecmascript * range is supported. For smaller time_t values (4 bytes in practice), * assumes that the signed 32-bit range is supported. * * XXX: detect this more correctly per platform. The size of time_t is * probably not an accurate guarantee of strftime() supporting or not * supporting a large time range (the full Ecmascript range). */ if (sizeof(time_t) < 8 && (parts[DUK_DATE_IDX_YEAR] < 1970 || parts[DUK_DATE_IDX_YEAR] > 2037)) { /* be paranoid for 32-bit time values (even avoiding negative ones) */ return 0; } DUK_MEMZERO(&tm, sizeof(tm)); tm.tm_sec = parts[DUK_DATE_IDX_SECOND]; tm.tm_min = parts[DUK_DATE_IDX_MINUTE]; tm.tm_hour = parts[DUK_DATE_IDX_HOUR]; tm.tm_mday = parts[DUK_DATE_IDX_DAY]; /* already one-based */ tm.tm_mon = parts[DUK_DATE_IDX_MONTH] - 1; /* one-based -> zero-based */ tm.tm_year = parts[DUK_DATE_IDX_YEAR] - 1900; tm.tm_wday = parts[DUK_DATE_IDX_WEEKDAY]; tm.tm_isdst = 0; DUK_MEMZERO(buf, sizeof(buf)); if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) { fmt = "%c"; } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) { fmt = "%x"; } else { DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME); fmt = "%X"; } (void) strftime(buf, sizeof(buf) - 1, fmt, &tm); DUK_ASSERT(buf[sizeof(buf) - 1] == 0); duk_push_string(ctx, buf); return 1; }
static int Atomic_SplitPath(duk_context* ctx) { String path = duk_require_string(ctx, 0); String pathName; String fileName; String ext; duk_push_object(ctx); SplitPath(path, pathName, fileName, ext); duk_push_string(ctx, pathName.CString()); duk_put_prop_string(ctx, -2, "pathName"); duk_push_string(ctx, fileName.CString()); duk_put_prop_string(ctx, -2, "fileName"); duk_push_string(ctx, ext.CString()); duk_put_prop_string(ctx, -2, "ext"); return 1; }
/* source: non-buffer, target: dynamic buffer */ static duk_ret_t test_3b(duk_context *ctx) { void *q; duk_size_t sz; duk_set_top(ctx, 0); duk_push_string(ctx, "foo"); q = duk_to_dynamic_buffer(ctx, -1, &sz); printf("q is NULL: %d\n", (q == NULL ? 1 : 0)); printf("sz=%lu\n", (unsigned long) sz); dump_buffer(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
void DefineProperty(duk_context* ctx, const char* propertyName, duk_c_function getFunc, duk_c_function setFunc) { duk_push_string(ctx, propertyName); if (setFunc) { duk_push_c_function(ctx, getFunc, 0); duk_push_c_function(ctx, setFunc, 1); duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER); } else { duk_push_c_function(ctx, getFunc, 0); duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_GETTER); } }
static duk_ret_t test_basic(duk_context *ctx, void *udata) { duk_bool_t ret; (void) udata; printf("top: %ld\n", (long) duk_get_top(ctx)); duk_push_string(ctx, "1.2.3"); ret = duk_put_global_string(ctx, "myAppVersion" "\x00" "ignored"); printf("top: %ld\n", (long) duk_get_top(ctx)); printf("ret: %ld\n", (long) ret); duk_eval_string_noresult(ctx, "print(myAppVersion);"); printf("top: %ld\n", (long) duk_get_top(ctx)); duk_push_string(ctx, "nulval"); ret = duk_put_global_lstring(ctx, "nul" "\x00" "keyx", 7); printf("top: %ld\n", (long) duk_get_top(ctx)); printf("ret: %ld\n", (long) ret); duk_eval_string_noresult(ctx, "print(new Function('return this')()['nul\\u0000key']);"); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static int test_6(duk_context *ctx) { int rc; /* property lookup fails: getter throws */ duk_eval_string(ctx, "({ get prop() { throw new RangeError('getter error'); } })"); duk_push_string(ctx, "prop"); duk_push_int(ctx, 10); duk_push_int(ctx, 11); rc = duk_pcall_prop(ctx, 1, 2); printf("rc=%d, result='%s'\n", rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); /* res */ duk_pop(ctx); /* obj */ return 0; }
static int test_5(duk_context *ctx) { int rc; /* property lookup fails: base value does not allow property lookup */ duk_push_undefined(ctx); duk_push_string(ctx, "foo"); duk_push_int(ctx, 10); duk_push_int(ctx, 11); rc = duk_pcall_prop(ctx, 1, 2); printf("rc=%d, result='%s'\n", rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); /* res */ duk_pop(ctx); /* obj */ return 0; }
/* duk_has_prop(), not an object */ static duk_ret_t test_1e(duk_context *ctx, void *udata) { duk_ret_t rc; (void) udata; duk_set_top(ctx, 0); duk_push_null(ctx); duk_push_string(ctx, "foo"); rc = duk_has_prop(ctx, -2); printf("null.foo -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* Some basic tests. */ static int test_1(duk_context *ctx) { int thr_idx; duk_context *new_ctx; duk_push_int(ctx, 123); /* dummy */ thr_idx = duk_push_thread(ctx); printf("duk_is_object(%d) = %d\n", thr_idx, duk_is_object(ctx, thr_idx)); printf("duk_is_thread(%d) = %d\n", thr_idx, duk_is_thread(ctx, thr_idx)); printf("top=%d\n", duk_get_top(ctx)); /* use the thread (context) value stack */ new_ctx = duk_get_context(ctx, thr_idx); duk_push_string(new_ctx, "foo"); duk_push_string(new_ctx, "bar"); duk_push_string(new_ctx, "quux"); duk_concat(new_ctx, 3); printf("concat: %s\n", duk_get_string(new_ctx, -1)); /* make new thread unreachable, so it gets GC'd */ duk_set_top(ctx, 0); printf("done\n"); return 0; }
int test_2(duk_context *ctx) { printf("top=%d\n", duk_get_top(ctx)); /* On a 32-bit platform and 12-byte values there is no zero- * equivalent value: (2**32 / 12) = 0x15555555, but 0x1555555 * 12 * = 0xfffffffc. This is even more harmful than simple wrapping. * Test value 0x15555556 = 0x100000008, wraps to 8. */ duk_push_string(ctx, "foo"); duk_set_top(ctx, 0x15555556); printf("top=%d\n", duk_get_top(ctx)); return 0; }
static int test_1(duk_context *ctx) { int rc; /* basic success case: own property */ duk_eval_string(ctx, "({ name: 'me', foo: function (x,y) { print(this.name); return x+y; } })"); /* idx 1 */ duk_push_string(ctx, "foo"); duk_push_int(ctx, 10); duk_push_int(ctx, 11); rc = duk_pcall_prop(ctx, 1, 2); printf("rc=%d, result='%s'\n", rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); /* res */ duk_pop(ctx); /* obj */ return 0; }
static int test_7(duk_context *ctx) { int rc; /* invalid object index */ duk_eval_string(ctx, "({ foo: 1, bar: 2 })"); duk_push_string(ctx, "foo"); duk_push_int(ctx, 10); duk_push_int(ctx, 11); rc = duk_pcall_prop(ctx, -6, 2); printf("rc=%d, result='%s'\n", rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); /* res */ duk_pop(ctx); /* obj */ return 0; }
static int test_4(duk_context *ctx) { int rc; /* basic error case */ duk_eval_string(ctx, "({ name: 'me', foo: function (x,y) { throw new Error('my error'); } })"); /* idx 1 */ duk_push_string(ctx, "foo"); duk_push_int(ctx, 10); duk_push_int(ctx, 11); rc = duk_pcall_prop(ctx, 1, 2); printf("rc=%d, result='%s'\n", rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); /* res */ duk_pop(ctx); /* obj */ return 0; }
void _gum_duk_push_exception_details (duk_context * ctx, GumExceptionDetails * details, GumDukCore * core, GumDukCpuContext ** cpu_context) { const GumExceptionMemoryDetails * md = &details->memory; gchar * message; message = gum_exception_details_to_string (details); duk_push_error_object (ctx, DUK_ERR_ERROR, "%s", message); g_free (message); duk_push_string (ctx, gum_exception_type_to_string (details->type)); duk_put_prop_string (ctx, -2, "type"); _gum_duk_push_native_pointer (ctx, details->address, core); duk_put_prop_string (ctx, -2, "address"); if (md->operation != GUM_MEMOP_INVALID) { duk_push_object (ctx); duk_push_string (ctx, _gum_duk_memory_operation_to_string (md->operation)); duk_put_prop_string (ctx, -2, "operation"); _gum_duk_push_native_pointer (ctx, md->address, core); duk_put_prop_string (ctx, -2, "address"); duk_put_prop_string (ctx, -2, "memory"); } *cpu_context = _gum_duk_push_cpu_context (ctx, &details->context, GUM_CPU_CONTEXT_READWRITE, core); duk_put_prop_string (ctx, -2, "context"); _gum_duk_push_native_pointer (ctx, details->native_context, core); duk_put_prop_string (ctx, -2, "nativeContext"); }
static int test_8(duk_context *ctx) { int rc; /* invalid arg count, causes 'key' to be identified with the object in the stack */ duk_eval_string(ctx, "({ foo: function () { print('foo called'); } })"); duk_push_string(ctx, "foo"); duk_push_int(ctx, 10); duk_push_int(ctx, 11); rc = duk_pcall_prop(ctx, 1, 3); printf("rc=%d, result='%s'\n", rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); /* res */ duk_pop(ctx); /* obj */ return 0; }