static int es_sqlite_query(duk_context *ctx) { int argc = duk_get_top(ctx); if(argc < 2) return DUK_RET_TYPE_ERROR; es_sqlite_t *es = es_resource_get(ctx, 0, &es_resource_sqlite); const char *query = duk_safe_to_string(ctx, 1); if(es->es_stmt) { sqlite3_finalize(es->es_stmt); es->es_stmt = NULL; } int rc = db_prepare(es->es_db, &es->es_stmt, query); if(rc != SQLITE_OK) { if(es->es_transaction && rc == SQLITE_LOCKED) duk_error(ctx, ST_ERROR_SQLITE_BASE | rc , "Deadlock"); duk_error(ctx, ST_ERROR_SQLITE_BASE | rc, "Sqlite error 0x%x -- %s", rc, sqlite3_errmsg(es->es_db)); } sqlite3_stmt *stmt = es->es_stmt; for(int i = 2; i < argc; i++) { int sqlite_arg = i - 1; if(duk_is_null_or_undefined(ctx, i)) { sqlite3_bind_null(stmt, sqlite_arg); } else if(duk_is_number(ctx, i)) { sqlite3_bind_double(stmt, sqlite_arg, duk_get_number(ctx, i)); } else if(duk_is_boolean(ctx, i)) { sqlite3_bind_int(stmt, sqlite_arg, duk_get_boolean(ctx, i)); } else { sqlite3_bind_text(stmt, sqlite_arg, duk_safe_to_string(ctx, i), -1, SQLITE_STATIC); } } es_sqlite_stmt_step(ctx, es); return 0; }
/* reads a file from path and returns it as string */ int js_read_file(duk_context *ctx) { /* return error if no filename is given */ if (duk_get_top(ctx) == 0) { printf("'read' expects one argument: 0 given\n"); return DUK_RET_TYPE_ERROR; } /* also needs to be a string */ if (!duk_is_string(ctx, 0)) { printf("'read' expects argument 1 of type string\n"); return DUK_RET_TYPE_ERROR; } /* get the first argument */ const char *filename = duk_get_string(ctx, 0); /* save file contents in this buffer */ char *buffer = 0; /* how big is the file? */ long length; /* try opening the file */ FILE *fp = fopen(filename, "r"); if (!fp) { printf("Failed to open file '%s'\n", filename); return DUK_RET_TYPE_ERROR; } /* get the files length... */ fseek(fp, 0, SEEK_END); length = ftell(fp); fseek(fp, 0, SEEK_SET); /* ...allocate memory for its content... */ buffer = malloc(length); /* ...and get its content if possible */ if (buffer) fread(buffer, 1, length, fp); /* close the file again */ fclose(fp); /* finally, push the string on the js stack */ duk_push_string(ctx, buffer); free(buffer); return 1; }
static duk_ret_t test_1(duk_context *ctx, void *udata) { duk_idx_t i, n; (void) udata; 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_string(ctx, ""); duk_push_string(ctx, "foo"); duk_push_string(ctx, "123"); duk_push_string(ctx, "123.456"); duk_push_string(ctx, "123.456e3"); duk_push_string(ctx, " -123.456e+3 "); duk_push_string(ctx, "NaN"); duk_push_string(ctx, "-Infinity"); duk_push_string(ctx, "+Infinity"); duk_push_string(ctx, "Infinity"); duk_push_string(ctx, "Infinityx"); duk_push_string(ctx, "xInfinity"); duk_push_string(ctx, " Infinity "); duk_push_object(ctx); duk_push_thread(ctx); duk_push_fixed_buffer(ctx, 0); /* coerces like string: ToNumber('') = 0 */ duk_push_fixed_buffer(ctx, 1024); /* coerces like string: ToNumber('\u0000\u0000...') = NaN */ duk_push_dynamic_buffer(ctx, 0); duk_push_dynamic_buffer(ctx, 1024); 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++) { printf("index %ld, number: %lf\n", (long) i, (double) duk_to_number(ctx, i)); } return 0; }
/* same test but with one element checks, once per loop */ static duk_ret_t check_2(duk_context *ctx, void *udata) { int i; duk_ret_t rc; (void) udata; for (i = 0; i < 1000; i++) { rc = duk_check_stack_top(ctx, i + 1); if (rc == 0) { printf("duk_check_stack failed\n"); } duk_push_int(ctx, 123); } printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t test_c_pcall_error2(duk_context *ctx) { duk_idx_t i; duk_int_t rc; duk_require_stack(ctx, STACK_REQUIRE + 1); duk_push_c_function(ctx, my_func_error2, 0); rc = duk_pcall(ctx, 0 /*nargs*/); printf("duk_pcall: rc=%ld, value: %s\n", (long) rc, duk_safe_to_string(ctx, -1)); for (i = 0; i < STACK_REQUIRE; i++) { duk_push_int(ctx, 123); } printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t test_ecma_pcall_error(duk_context *ctx) { duk_idx_t i; duk_int_t rc; duk_require_stack(ctx, STACK_REQUIRE + 1); duk_eval_string(ctx, "(function () { print('hello from ecma func'); throw Error('ecma thrown'); })"); rc = duk_pcall(ctx, 0 /*nargs*/); printf("duk_pcall: rc=%ld, value: %s\n", (long) rc, duk_safe_to_string(ctx, -1)); for (i = 0; i < STACK_REQUIRE; i++) { duk_push_int(ctx, 123); } printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
duk_ret_t JsExe::LinkArgs(duk_context* duktapeContext) { try { int args = duk_get_top(duktapeContext); duk_push_this(duktapeContext); auto obj = JavaScriptHelper::CppObject<JsExe>(duktapeContext); if (!args) duk_push_string(duktapeContext, obj->linker.Args().c_str()); else obj->linker.Args(JavaScriptHelper::AsString(duktapeContext, args)); return 1; } catch (std::exception& e) { JavaScriptHelper::Throw(duktapeContext, e.what()); } }
duk_ret_t JsExe::WarningDisable(duk_context* duktapeContext) { try { int args = duk_get_top(duktapeContext); duk_push_this(duktapeContext); auto obj = JavaScriptHelper::CppObject<JsExe>(duktapeContext); if (!args) JavaScriptHelper::PushArray(duktapeContext, obj->compiler.WarningDisable()); else obj->compiler.WarningDisable(JavaScriptHelper::AsIntVector(duktapeContext, args)); return 1; } catch (std::exception& e) { JavaScriptHelper::Throw(duktapeContext, e.what()); } }
static duk_ret_t test_1(duk_context *ctx, void *udata) { (void) udata; duk_set_top(ctx, 0); remove_handlers(ctx); duk_eval_string(ctx, "Duktape.errThrow = function (err) { err.name = 'ForcedName'; return err; }"); duk_pop(ctx); /* Throw with duk_throw(). */ duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "range error: %d", 123); duk_throw(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t js_OutlinedCircle(duk_context* ctx) { bool antialiased = false; color_t color; int n_args; int x, y, radius; n_args = duk_get_top(ctx); x = duk_to_int(ctx, 0); y = duk_to_int(ctx, 1); radius = duk_to_int(ctx, 2); color = duk_require_sphere_color(ctx, 3); if (n_args >= 5) antialiased = duk_require_boolean(ctx, 4); if (!is_skipped_frame()) al_draw_circle(x, y, radius, nativecolor(color), 1); return 0; }
duk_ret_t JavaMethod::invoke(duk_context* ctx, JNIEnv* env, jobject javaThis) const { if (duk_get_top(ctx) != m_argumentLoaders.size()) { // Wrong number of arguments given - throw an error. duk_error(ctx, DUK_ERR_API_ERROR, "wrong number of arguments"); // unreachable - duk_error never returns. return DUK_RET_API_ERROR; } std::vector<jvalue> args(m_argumentLoaders.size()); // Load the arguments off the stack and convert to Java types. // Note we're going backwards since the last argument is at the top of the stack. for (ssize_t i = m_argumentLoaders.size() - 1; i >= 0; --i) { args[i] = m_argumentLoaders[i](ctx, env); } return m_methodBody(ctx, env, javaThis, args.data()); }
/* Basic test. */ static duk_ret_t test_1(duk_context *ctx) { int i; duk_eval_string(ctx, "new RangeError('test error')"); /* 0 */ duk_eval_string(ctx, "Error"); /* 1 */ duk_eval_string(ctx, "RangeError"); /* 2 */ duk_eval_string(ctx, "TypeError"); /* 3 */ duk_eval_string(ctx, "Object"); /* 4 */ duk_eval_string(ctx, "Function"); /* 5 */ for (i = 1; i <= 5; i++) { printf("0 instanceof %d: %d\n", i, (int) duk_instanceof(ctx, 0, i)); } printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t Line_Intersects_Selector(duk_context* ctx) { int numArgs = duk_get_top(ctx); if (numArgs == 3 && GetValueObject<OBB>(ctx, 0, OBB_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_Intersects_OBB_float_float(ctx); if (numArgs == 3 && GetValueObject<AABB>(ctx, 0, AABB_ID) && duk_is_number(ctx, 1) && duk_is_number(ctx, 2)) return Line_Intersects_AABB_float_float(ctx); if (numArgs == 1 && GetValueObject<Capsule>(ctx, 0, Capsule_ID)) return Line_Intersects_Capsule(ctx); if (numArgs == 1 && GetValueObject<Frustum>(ctx, 0, Frustum_ID)) return Line_Intersects_Frustum(ctx); if (numArgs == 1 && GetValueObject<AABB>(ctx, 0, AABB_ID)) return Line_Intersects_AABB(ctx); if (numArgs == 1 && GetValueObject<OBB>(ctx, 0, OBB_ID)) return Line_Intersects_OBB(ctx); duk_error(ctx, DUK_ERR_ERROR, "Could not select function overload"); }
static duk_ret_t test_1(duk_context *ctx) { duk_set_top(ctx, 0); duk_push_c_function(ctx, my_adder, 3 /*nargs*/); duk_push_int(ctx, 10); duk_push_int(ctx, 11); duk_push_int(ctx, 12); duk_push_int(ctx, 13); /* clipped */ duk_push_int(ctx, 14); /* clipped */ duk_call(ctx, 5); printf("result=%s\n", duk_to_string(ctx, -1)); duk_pop(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
int test_2(duk_context *ctx) { duk_set_top(ctx, 0); duk_push_c_function(ctx, my_adder, 3 /*nargs*/); duk_push_int(ctx, 10); duk_push_int(ctx, 11); duk_push_string(ctx, "foo"); /* causes error */ duk_push_int(ctx, 13); /* clipped */ duk_push_int(ctx, 14); /* clipped */ duk_call(ctx, 5); printf("result=%s\n", duk_to_string(ctx, -1)); duk_pop(ctx); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
void CallDisconnectSignal(duk_context* ctx, void* signal) { int numArgs = duk_get_top(ctx); duk_push_number(ctx, (size_t)signal); duk_insert(ctx, 0); duk_push_global_object(ctx); duk_get_prop_string(ctx, -1, "_DisconnectSignal"); duk_remove(ctx, -2); // Global object duk_insert(ctx, 0); duk_pcall(ctx, numArgs + 1); if (duk_get_boolean(ctx, -1)) // Last receiver disconnected { HashMap<void*, SharedPtr<SignalReceiver> >& signalReceivers = JavaScriptInstance::InstanceFromContext(ctx)->SignalReceivers(); signalReceivers.Erase(signal); } duk_pop(ctx); // Result }
/* duk_get_prop_string(), success cases */ static duk_ret_t test_getpropstring_a(duk_context *ctx, void *udata) { duk_ret_t rc; (void) udata; prep(ctx); rc = duk_get_prop_string(ctx, 0, "foo"); printf("obj.foo -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); rc = duk_get_prop_string(ctx, 0, "foo" "\x00" "bar"); /* embedded NUL terminates key */ printf("obj.foo -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); rc = duk_get_prop_string(ctx, 0, "nonexistent"); printf("obj.nonexistent -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); rc = duk_get_prop_string(ctx, 0, "123"); printf("obj['123'] -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); rc = duk_get_prop_string(ctx, 1, "nonexistent"); printf("arr.nonexistent -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); rc = duk_get_prop_string(ctx, 1, "2"); printf("arr['2'] -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); rc = duk_get_prop_string(ctx, 1, "length"); printf("arr.length -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); rc = duk_get_prop_string(ctx, 2, "5"); printf("'test_string'['5'] -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); rc = duk_get_prop_string(ctx, 2, "length"); printf("'test_string'.length -> 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 duk_builtin_date_constructor(duk_context *ctx) { int nargs = duk_get_top(ctx); int is_cons = duk_is_constructor_call(ctx); double dparts[NUM_PARTS]; double d; DUK_DDDPRINT("Date constructor, nargs=%d, is_cons=%d", nargs, is_cons); duk_push_object_helper(ctx, DUK_HOBJECT_FLAG_EXTENSIBLE | DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATE), DUK_BIDX_DATE_PROTOTYPE); /* Unlike most built-ins, the internal [[PrimitiveValue]] of a Date * is mutable. */ if (nargs == 0 || !is_cons) { d = timeclip(GET_NOW_TIMEVAL(ctx)); duk_push_number(ctx, d); duk_def_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W); if (!is_cons) { /* called as a normal function: return new Date().toString() */ duk_to_string(ctx, -1); } return 1; } else if (nargs == 1) { duk_to_primitive(ctx, 0, DUK_HINT_NONE); if (duk_is_string(ctx, 0)) { parse_string(ctx, duk_to_string(ctx, 0)); duk_replace(ctx, 0); /* may be NaN */ } d = timeclip(duk_to_number(ctx, 0)); duk_push_number(ctx, d); duk_def_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W); return 1; } set_parts_from_args(ctx, dparts, nargs); /* Parts are in local time, convert when setting. */ set_this_timeval_from_dparts(ctx, dparts, FLAG_LOCALTIME /*flags*/); /* -> [ ... this timeval ] */ duk_pop(ctx); /* -> [ ... this ] */ return 1; }
static duk_ret_t test_suspend_resume_throw_basic(duk_context *ctx, void *udata) { duk_context *other; duk_thread_state st; (void) duk_push_thread(ctx); other = duk_get_context(ctx, -1); duk_suspend(ctx, &st); duk_eval_string(other, "print('other thread executing');"); duk_resume(ctx, &st); (void) duk_range_error(ctx, "aiee"); duk_pop(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* Underlying buffer doesn't cover logical slice. */ static duk_ret_t test_uncovered_buffer(duk_context *ctx) { unsigned char *p; duk_size_t sz; duk_push_dynamic_buffer(ctx, 64); /* 16x4 elements */ duk_eval_string(ctx, "(function (plain_buffer) {\n" " var b = new ArrayBuffer(plain_buffer);\n" " return new Uint32Array(b).subarray(1,5);\n" "})"); duk_dup(ctx, 0); duk_call(ctx, 1); /* -> [ plain_buffer Uint32Array ] */ /* Initially OK. */ sz = (duk_size_t) 0xdeadbeefUL; p = duk_get_buffer_data(ctx, -1, &sz); if (p) { printf("p is not NULL, sz=%ld\n", (long) sz); } else { printf("p is NULL\n"); } /* Resize; slice still covered. */ duk_resize_buffer(ctx, 0, 20); /* 5x4 = 20, subarray is [1*4, 5*4[ */ sz = (duk_size_t) 0xdeadbeefUL; p = duk_get_buffer_data(ctx, -1, &sz); if (p) { printf("p is not NULL, sz=%ld\n", (long) sz); } else { printf("p is NULL\n"); } /* Resize; no longer covered. */ duk_resize_buffer(ctx, 0, 19); sz = (duk_size_t) 0xdeadbeefUL; p = duk_get_buffer_data(ctx, -1, &sz); if (p) { printf("p is not NULL, sz=%ld\n", (long) sz); } else { printf("p is NULL\n"); } printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
duk_ret_t JsLibrarian::Files(duk_context* duktapeContext) { try { int args = duk_get_top(duktapeContext); duk_push_this(duktapeContext); auto obj = JavaScriptHelper::CppObject<JsLibrarian>(duktapeContext); if (!args) JavaScriptHelper::PushArray(duktapeContext, obj->librarian.Files()); else obj->librarian.Files(JavaScriptHelper::AsStringVector(duktapeContext, args)); return 1; } catch (std::exception& e) { JavaScriptHelper::Throw(duktapeContext, e.what()); } }
static int test_nodejs_buffer_copy_1(duk_context *ctx) { int i, dst, src; unsigned char *data; data = (unsigned char *) duk_push_dynamic_buffer(ctx, 10); /* index 0 */ for (i = 0; i < 10; i++) { data[i] = 0x40 + i; } setup_nodejs_buffer(ctx, 0); setup_nodejs_buffer_slice(ctx, 0, 1, 4); setup_nodejs_buffer_slice(ctx, 0, 3, 7); setup_nodejs_buffer_slice(ctx, 0, 6, 9); for (i = 10; i >= 0; i--) { duk_resize_buffer(ctx, 0, i); /* The resulting buffers are not printed here; they're not very * intuitive because both the source and the destination share * the same underlying buffer. */ for (dst = 1; dst <= 4; dst++) { for (src = 1; src <= 4; src++) { printf("%d %d %d\n", i, dst, src); duk_eval_string(ctx, "(function (dst, src) {\n" " for (var i = 0; i < dst.length; i++) { dst[i] = 0x11; }\n" " for (var i = 0; i < src.length; i++) { src[i] = 0x22; }\n" " src.copy(dst);\n" " for (var i = 0; i < dst.length; i++) { dst[i] = 0x11; }\n" " for (var i = 0; i < src.length; i++) { src[i] = 0x22; }\n" " src.copy(dst, 4, 1);\n" "})"); duk_dup(ctx, dst); duk_dup(ctx, src); duk_call(ctx, 2); duk_pop(ctx); } } } printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t test_1(duk_context *ctx, void *udata) { duk_idx_t i, n; (void) udata; 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, 0); duk_push_int(ctx, 1); duk_push_nan(ctx); duk_push_number(ctx, INFINITY); duk_push_string(ctx, ""); duk_push_string(ctx, "foo"); duk_push_object(ctx); duk_push_thread(ctx); duk_push_fixed_buffer(ctx, 0); duk_push_fixed_buffer(ctx, 1024); duk_push_dynamic_buffer(ctx, 0); duk_push_dynamic_buffer(ctx, 1024); duk_push_pointer(ctx, (void *) NULL); duk_push_pointer(ctx, (void *) 0xdeadbeefUL); n = duk_get_top(ctx); printf("top: %ld\n", (long) n); for (i = 0; i < n; i++) { void *ptr; duk_int_t t1, t2; t1 = duk_get_type(ctx, i); ptr = duk_to_pointer(ctx, i); t2 = duk_get_type(ctx, i); printf("index %ld, ptr-is-NULL: %d, type: %ld -> %ld\n", (long) i, (ptr == NULL ? 1 : 0), (long) t1, (long) t2); if (t1 == DUK_TYPE_POINTER) { /* check that pointer is retained as is (can safely print) */ printf("pointer: %p\n", ptr); } } return 0; }
int duk_builtin_date_constructor_utc(duk_context *ctx) { int nargs = duk_get_top(ctx); double dparts[NUM_PARTS]; double d; /* Behavior for nargs < 2 is implementation dependent: currently we'll * set a NaN time value (matching V8 behavior) in this case. */ if (nargs < 2) { duk_push_nan(ctx); } else { set_parts_from_args(ctx, dparts, nargs); d = get_timeval_from_dparts(dparts, 0 /*flags*/); duk_push_number(ctx, d); } return 1; }
void test(duk_context *ctx) { duk_ret_t rc; duk_eval_string(ctx, "this.testFunc = eval.bind(this, 'print(\\'hello from eval\\')');"); duk_pop(ctx); duk_push_global_object(ctx); duk_get_prop_string(ctx, -1, "testFunc"); rc = duk_pcall(ctx, 0); printf("rc=%d\n", (int) rc); if (rc != 0) { /* unexpected error */ printf("error=%s\n", duk_safe_to_string(ctx, -1)); } duk_pop_2(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); }
duk_ret_t JsLibrarian::Output(duk_context* duktapeContext) { try { int args = duk_get_top(duktapeContext); duk_push_this(duktapeContext); auto obj = JavaScriptHelper::CppObject<JsLibrarian>(duktapeContext); if (!args) duk_push_string(duktapeContext, obj->librarian.Output().c_str()); else if (args == 1) obj->librarian.Output(duk_require_string(duktapeContext, 0)); else JavaScriptHelper::Throw(duktapeContext, "One argument for Librarian::Output() expected"); return 1; } catch (std::exception& e) { JavaScriptHelper::Throw(duktapeContext, e.what()); } }
duk_ret_t JsLibrarian::DependencyCheck(duk_context* duktapeContext) { try { int args = duk_get_top(duktapeContext); duk_push_this(duktapeContext); auto obj = JavaScriptHelper::CppObject<JsLibrarian>(duktapeContext); if (!args) duk_push_boolean(duktapeContext, obj->librarian.DependencyCheck()); else if (args == 1) obj->librarian.DependencyCheck(duk_require_boolean(duktapeContext, 0) != 0); else JavaScriptHelper::Throw(duktapeContext, "One argument for Librarian::DependencyCheck() expected"); return 1; } catch (std::exception& e) { JavaScriptHelper::Throw(duktapeContext, e.what()); } }
duk_ret_t JsLibrarian::Create(duk_context* duktapeContext) { try { int args = duk_get_top(duktapeContext); duk_push_this(duktapeContext); auto obj = JavaScriptHelper::CppObject<JsLibrarian>(duktapeContext); if (args != 0) JavaScriptHelper::Throw(duktapeContext, "No arguments for Librarian::Create() expected"); obj->librarian.Create(); return 1; } catch (std::exception& e) { JavaScriptHelper::Throw(duktapeContext, e.what()); } }
static duk_ret_t test_putprop_shorthand_a(duk_context *ctx) { duk_eval_string(ctx, "({ foo: 123 })"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, -2, "bar" "\x00" "quux"); duk_push_uint(ctx, 234); duk_put_prop_index(ctx, -2, 2001); duk_push_uint(ctx, 345); duk_put_prop_lstring(ctx, -2, "nul" "\x00" "keyx", 7); duk_json_encode(ctx, -1); printf("%s\n", duk_to_string(ctx, -1)); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t test_api_example(duk_context *ctx, void *udata) { void *ptr; (void) udata; duk_eval_string(ctx, "({ foo: 'bar' })"); ptr = duk_get_heapptr(ctx, -1); duk_put_global_string(ctx, "ref"); duk_set_top(ctx, 0); duk_push_heapptr(ctx, ptr); duk_get_prop_string(ctx, -1, "foo"); printf("obj.foo: %s\n", duk_safe_to_string(ctx, -1)); /* prints 'bar' */ printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }