static duk_ret_t test_typedarray_set_1(duk_context *ctx, void *udata) { int i, dst, src; unsigned char *data; (void) udata; data = (unsigned char *) duk_push_dynamic_buffer(ctx, 10); /* index 0 */ for (i = 0; i < 10; i++) { data[i] = 0x40 + i; } /* Some combinations are not set() compatible and cause a RangeError * (source longer than target). But this set should exercise byte copy, * fast copy, and slow copy code paths. */ setup_typedarray(ctx, 0, "Uint8Array"); /* index 1, length 10 */ setup_typedarray_slice(ctx, 0, "Uint8Array", 2, 5); /* index 2, length 5 */ setup_typedarray(ctx, 0, "Uint16Array"); /* index 3, length 5 */ setup_typedarray_slice(ctx, 0, "Uint16Array", 2, 3); /* index 4, length 3 */ duk_eval_string(ctx, "[ 1, 2, 3, 4, 5 ]"); /* index 5, length 5 */ for (i = 10; i >= 0; i--) { duk_resize_buffer(ctx, 0, i); /* Various copy combinations. Resulting buffer is omitted; * when both dst and src are typedarrays they share the same * underlying buffer. so the results are a bit confusing (and * not important to memory safety). */ for (dst = 1; dst <= 4; dst++) { for (src = 1; src <= 5; 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" " try { dst.set(src); } catch (e) { print(e.name); }\n" "})\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_json_serialize_1(duk_context *ctx, void *udata) { unsigned char *data; int i; duk_uarridx_t arridx = 0; (void) udata; data = (unsigned char *) duk_push_dynamic_buffer(ctx, 20); for (i = 0; i < 20; i++) { data[i] = 0x40 + i; } duk_push_array(ctx); /* index 1 */ setup_duktape_buffer(ctx, 0); duk_put_prop_index(ctx, 1, arridx++); setup_nodejs_buffer(ctx, 0); duk_put_prop_index(ctx, 1, arridx++); setup_nodejs_buffer_slice(ctx, 0, 3, 5); duk_put_prop_index(ctx, 1, arridx++); setup_arraybuffer(ctx, 0); duk_put_prop_index(ctx, 1, arridx++); setup_typedarray(ctx, 0, "Uint8Array"); duk_put_prop_index(ctx, 1, arridx++); setup_typedarray_slice(ctx, 0, "Uint8Array", 2, 6); duk_put_prop_index(ctx, 1, arridx++); setup_typedarray(ctx, 0, "Uint32Array"); duk_put_prop_index(ctx, 1, arridx++); setup_typedarray_slice(ctx, 0, "Uint32Array", 4, 1); duk_put_prop_index(ctx, 1, arridx++); /* Serialize with a full backing buffer first. */ for (i = 20; i >= 0; i--) { printf("resize to %d\n", i); duk_resize_buffer(ctx, 0, i); duk_dup(ctx, 1); duk_json_encode(ctx, -1); printf("%s\n", duk_to_string(ctx, -1)); duk_pop(ctx); duk_eval_string(ctx, "(function (v) { print(Duktape.enc('jx', v)); })"); duk_dup(ctx, 1); duk_call(ctx, 1); duk_pop(ctx); } printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* Float64Array, "partial slice" */ static duk_ret_t test_float64array_indexed_1b(duk_context *ctx) { duk_push_dynamic_buffer(ctx, 1200); setup_typedarray_slice(ctx, -1, "Float64Array", 240, 100); shared_read_write_index(ctx, 240 + 95 * 8); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* Uint32Array, "partial slice" */ static duk_ret_t test_uint32array_indexed_1b(duk_context *ctx) { duk_push_dynamic_buffer(ctx, 600); setup_typedarray_slice(ctx, -1, "Uint32Array", 120, 100); shared_read_write_index(ctx, 120 + 95 * 4); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* Uint16Array, "partial slice" */ static duk_ret_t test_uint16array_indexed_1b(duk_context *ctx, void *udata) { (void) udata; duk_push_dynamic_buffer(ctx, 300); setup_typedarray_slice(ctx, -1, "Uint16Array", 60, 100); shared_read_write_index(ctx, 60 + 95 * 2); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }