void test(duk_context *ctx) { int ret; printf("top: %d\n", (int) duk_get_top(ctx)); ret = duk_get_global_string(ctx, "encodeURIComponent"); printf("top: %d\n", (int) duk_get_top(ctx)); printf("ret: %d\n", ret); duk_push_string(ctx, "foo bar"); duk_call(ctx, 1); printf("encoded: %s\n", duk_to_string(ctx, -1)); duk_pop(ctx); printf("top: %d\n", (int) duk_get_top(ctx)); ret = duk_get_global_string(ctx, "doesNotExist"); printf("top: %d\n", (int) duk_get_top(ctx)); printf("ret: %d\n", ret); printf("%s\n", duk_to_string(ctx, -1)); duk_pop(ctx); printf("top: %d\n", (int) duk_get_top(ctx)); }
static int sys1_chmod(duk_context *ctx) { const char *path = duk_to_string(ctx, 0); mode_t mode = duk_to_int(ctx, 1); int rc; rc = chmod(path, mode); duk_push_int(ctx, rc); return 1; }
static int es_gumbo_find_by_id(duk_context *ctx) { es_gumbo_node_t *egn = es_get_native_obj(ctx, 0, &es_native_gumbo_node); const char *id = duk_to_string(ctx, 1); GumboNode *r = es_gumbo_find_by_id_r(egn->node, id); if(r == NULL) return 0; push_gumbo_node(ctx, r, egn->output); return 1; }
/* 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; }
static duk_ret_t js_Logger_write(duk_context* ctx) { const char* text = duk_to_string(ctx, 0); logger_t* logger; duk_push_this(ctx); logger = duk_require_sphere_obj(ctx, -1, "Logger"); write_log_line(logger, NULL, text); return 0; }
/* duk_get_prop_string(), DUK_INVALID_INDEX */ int test_2c(duk_context *ctx) { int rc; prep(ctx); rc = duk_get_prop_string(ctx, DUK_INVALID_INDEX, "foo"); printf("obj.foo -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
static duk_ret_t handle_set(duk_context *ctx) { /* 'this' binding: handler * [0]: target * [1]: key * [2]: val * [3]: receiver (proxy) */ const char *key = duk_to_string(ctx, 1); const char *val = duk_to_string(ctx, 2); printf("handle_set: key=%s, val=%s\n", key, val); if (key != NULL && key[0] == '_') { /* Indicate set failure for properties beginning with underscore. */ duk_push_false(ctx); } else { duk_push_true(ctx); } return 1; }
static int es_gumbo_find_by_class(duk_context *ctx) { es_gumbo_node_t *egn = es_get_native_obj(ctx, 0, &es_native_gumbo_node); const char *cls = duk_to_string(ctx, 1); int idx = 0; duk_push_array(ctx); char **classlist = strvec_split(cls, ' '); es_gumbo_find_by_class_r(egn->node, classlist, ctx, &idx, egn->output); strvec_free(classlist); return 1; }
static int r2cmd(duk_context *ctx) { char *ret; int n = duk_get_top (ctx); /* #args */ if (n>0) { const char *s = duk_to_string (ctx, 0); ret = r_core_cmd_str (Gcore, s); duk_push_string (ctx, ret); free (ret); return 1; } return 0; }
/* duk_get_prop_index(), DUK_INVALID_INDEX */ static duk_ret_t test_3c(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_get_prop_index(ctx, DUK_INVALID_INDEX, 123); printf("obj[123] -> 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; }
static duk_ret_t test_1(duk_context *ctx) { duk_idx_t i, n; prep(ctx); duk_remove(ctx, -2); /* -> [ 123 345 ] */ n = duk_get_top(ctx); for (i = 0; i < n; i++) { printf("%ld: %s\n", (long) i, duk_to_string(ctx, i)); } return 0; }
DUK_LOCAL void duk__push_string(duk_context *ctx, duk_bitdecoder_ctx *bd) { duk_small_uint_t n; duk_small_uint_t i; duk_uint8_t *p; n = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRING_LENGTH_BITS); p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, n); for (i = 0; i < n; i++) { *p++ = (duk_uint8_t) duk_bd_decode(bd, DUK__STRING_CHAR_BITS); } duk_to_string(ctx, -1); }
static duk_ret_t test_decode_invalid_char(duk_context *ctx, void *udata) { (void) udata; duk_set_top(ctx, 0); duk_push_string(ctx, "dGVzdCBzdHJ@bmc="); duk_push_int(ctx, 321); /* dummy */ duk_base64_decode(ctx, -2); /* buffer */ printf("base64 decode: %s\n", duk_to_string(ctx, -2)); printf("top after: %ld\n", (long) duk_get_top(ctx)); /* value + dummy */ duk_set_top(ctx, 0); return 0; }
static int sys1_stat(duk_context *ctx) { const char *path = duk_to_string(ctx, 0); struct stat buf; int rc; rc = stat(path, &buf); sys1_push_stat(ctx, &buf); duk_push_int(ctx, rc); duk_put_prop_string(ctx, -2, "rc"); return 1; }
static int sys1_getenv(duk_context *ctx) { const char *name = duk_to_string(ctx, 0); char *val; val = getenv(name); if(val) duk_push_string(ctx, val); else duk_push_undefined(ctx); return 1; }
/* duk_get_prop_string(), invalid index */ static duk_ret_t test_2b(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_get_prop_string(ctx, 234, "foo"); printf("obj.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; }
/* duk_get_prop_index(), invalid index */ int test_3b(duk_context *ctx) { int rc; prep(ctx); rc = duk_get_prop_index(ctx, 234, 123); printf("obj[123] -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
/* duk_get_prop(), success cases */ static duk_ret_t test_getprop_a(duk_context *ctx, void *udata) { duk_ret_t rc; (void) udata; prep(ctx); duk_push_string(ctx, "foo"); rc = duk_get_prop(ctx, 0); printf("obj.foo -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_string(ctx, "nonexistent"); rc = duk_get_prop(ctx, 0); printf("obj.nonexistent -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_int(ctx, 123); rc = duk_get_prop(ctx, 0); printf("obj[123] -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_string(ctx, "nonexistent"); rc = duk_get_prop(ctx, 1); printf("arr.nonexistent -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_int(ctx, 2); rc = duk_get_prop(ctx, 1); printf("arr[2] -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_string(ctx, "length"); rc = duk_get_prop(ctx, 1); printf("arr.length -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_int(ctx, 5); rc = duk_get_prop(ctx, 2); printf("'test_string'[5] -> rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_string(ctx, "length"); rc = duk_get_prop(ctx, 2); 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; }
/* duk_del_prop(), success cases */ static duk_ret_t test_delprop_a_safecall(duk_context *ctx, void *udata) { duk_ret_t rc; (void) udata; 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 int sys1_open(duk_context *ctx) { int fd; const char *path = duk_to_string(ctx, 0); uint32_t flags = duk_to_uint32(ctx, 1); int mode = duk_to_int(ctx, 2); fd = open(path, flags, mode); duk_push_number(ctx, fd); return 1; }
/* duk_del_prop_lstring(), success case */ static duk_ret_t test_delproplstring_a(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_del_prop_lstring(ctx, 0, "nul" "\x00" "keyx", 7); printf("delete obj.nul<NUL>key -> rc=%d\n", (int) rc); duk_json_encode(ctx, 0); printf("%s\n", duk_to_string(ctx, 0)); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t js_Font_getStringWidth(duk_context* ctx) { const char* text = duk_to_string(ctx, 0); font_t* font; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_pop(ctx); duk_push_int(ctx, get_text_width(font, text)); return 1; }
static duk_ret_t js_Logger_beginBlock(duk_context* ctx) { const char* title = duk_to_string(ctx, 0); logger_t* logger; duk_push_this(ctx); logger = duk_require_sphere_obj(ctx, -1, "Logger"); if (!begin_log_block(logger, title)) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "Log:beginBlock(): Failed to create new log block"); return 0; }
/* duk_get_prop(), invalid index */ int test_1b(duk_context *ctx) { int rc; prep(ctx); duk_push_string(ctx, "foo"); rc = duk_get_prop(ctx, 234); printf("obj.foo -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
static void raise_ctx_error(struct state *state) { duk_context *ctx = state->ctx; duk_get_prop_string(ctx, -1, "name"); const char *name = duk_safe_to_string(ctx, -1); duk_get_prop_string(ctx, -2, "message"); const char *message = duk_to_string(ctx, -1); VALUE exc_class = error_name_class(name); VALUE exc = rb_exc_new2(exc_class, message); clean_raise_exc(ctx, exc); }
static int es_gumbo_find_by_tag_name(duk_context *ctx) { es_gumbo_node_t *egn = es_get_native_obj(ctx, 0, &es_native_gumbo_node); const char *tagstr = duk_to_string(ctx, 1); GumboTag tag = gumbo_tag_enum(tagstr); if(tag == GUMBO_TAG_UNKNOWN) duk_error(ctx, DUK_ERR_ERROR, "Unknown tag %s", tagstr); int idx = 0; duk_push_array(ctx); es_gumbo_find_by_tag_name_r(egn->node, tag, ctx, &idx, egn->output); return 1; }
static duk_ret_t js_Font_getStringWidth(duk_context* ctx) { const char* text = duk_to_string(ctx, 0); font_t* font; duk_push_this(ctx); duk_get_prop_string(ctx, -1, "\xFF" "ptr"); font = duk_get_pointer(ctx, -1); duk_pop(ctx); duk_pop(ctx); duk_push_int(ctx, get_text_width(font, text)); return 1; }
static duk_ret_t test_2(duk_context *ctx) { duk_idx_t i, n; prep(ctx); duk_copy(ctx, -3, -3); /* nop */ n = duk_get_top(ctx); for (i = 0; i < n; i++) { printf("%ld: %s\n", (long) i, duk_to_string(ctx, i)); } return 0; }
/* duk_del_prop_string(), success cases */ int test_2a(duk_context *ctx) { int rc; prep(ctx); rc = duk_del_prop_string(ctx, 0, "foo"); printf("delete obj.foo -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 0, "nonexistent"); printf("delete obj.nonexistent -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 0, "123"); printf("delete obj['123'] -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 1, "nonexistent"); printf("delete arr.nonexistent -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 1, "2"); printf("delete arr['2'] -> rc=%d\n", rc); /* non-configurable property, but running in non-strict mode */ rc = duk_del_prop_string(ctx, 1, "length"); printf("delete arr.length -> rc=%d\n", rc); /* non-configurable property, but running in non-strict mode */ rc = duk_del_prop_string(ctx, 2, "5"); printf("delete 'test_string'['5'] -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 2, "length"); printf("delete 'test_string'.length -> rc=%d\n", 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: %d\n", duk_get_top(ctx)); return 0; }
/* duk_get_prop(), success cases */ int test_1a(duk_context *ctx) { int rc; prep(ctx); duk_push_string(ctx, "foo"); rc = duk_get_prop(ctx, 0); printf("obj.foo -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_string(ctx, "nonexistent"); rc = duk_get_prop(ctx, 0); printf("obj.nonexistent -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_int(ctx, 123); rc = duk_get_prop(ctx, 0); printf("obj[123] -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_string(ctx, "nonexistent"); rc = duk_get_prop(ctx, 1); printf("arr.nonexistent -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_int(ctx, 2); rc = duk_get_prop(ctx, 1); printf("arr[2] -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_string(ctx, "length"); rc = duk_get_prop(ctx, 1); printf("arr.length -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_int(ctx, 5); rc = duk_get_prop(ctx, 2); printf("'test_string'[5] -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); duk_push_string(ctx, "length"); rc = duk_get_prop(ctx, 2); printf("'test_string'.length -> rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); duk_pop(ctx); printf("final top: %d\n", duk_get_top(ctx)); return 0; }