Пример #1
0
static void
es_fa_read(void *aux)
{
  es_fa_handle_t *fah = aux;
  es_fap_t *ef = fah->fah_ef;
  es_context_t *ec = ef->super.er_ctx;
  duk_context *ctx = ec->ec_duk;

  es_context_begin(ec);

  duk_set_top(ctx, 0);

  duk_push_external_buffer(ctx);
  es_root_register(ctx, -1, fah->fah_readbuf);

  duk_config_buffer(ctx, 0, fah->fah_readbuf, fah->fah_readlen);

  es_push_root(ctx, ef);
  duk_get_prop_string(ctx, -1, "read");
  es_push_native_obj(ctx, &es_native_fah, fah_retain(fah));
  es_push_root(ctx, fah);

  duk_push_buffer_object(ctx, 0, 0, fah->fah_readlen, DUK_BUFOBJ_UINT8ARRAY);
  duk_push_int(ctx, fah->fah_readlen);
  duk_push_number(ctx, fah->fah_fpos);

  int rc = duk_pcall(ctx, 5);
  if(rc) {
    fah_exception(fah, ctx);
    es_dump_err(ctx);
  }

  es_context_end(ec, 0);
  fah_release(fah);
}
Пример #2
0
static duk_int_t load_file(duk_context *ctx, const char *filename, const char *var) {
	size_t len; char *buf = read_file(filename, &len);
	if(!buf) FAIL_LOAD

	duk_push_external_buffer(ctx);
	duk_config_buffer(ctx, -1, buf, len);
	duk_put_global_string(ctx, var);
	return 0;
}
static duk_ret_t test_expand_overlap(duk_context *ctx, void *udata) {
	unsigned char buf[48];
	int offset;
	int i;

	(void) udata;

	for (offset = 0; offset < 16; offset++) {  /* dst offset */
		printf("offset: %d\n", offset);

		for (i = 0; i < 48; i++) {
			buf[i] = (unsigned char) i;
		}

		duk_push_external_buffer(ctx);  /* src */
		duk_config_buffer(ctx, -1, (void *) (buf + 4), 4);  /* |04050607| */

		duk_push_external_buffer(ctx);  /* dst */
		duk_config_buffer(ctx, -1, (void *) (buf + offset), 32);

		duk_eval_string(ctx,
			"(function (plain_src, plain_dst) {\n"
			"    var bsrc = new Uint8Array(plain_src.buffer);\n"
			"    var bdst = new Uint32Array(plain_dst.buffer);\n"
			"    print(Duktape.enc('jx', bsrc));\n"
			"    print(Duktape.enc('jx', bdst));\n"
			"    bdst.set(bsrc, 1);\n"
			"    print(Duktape.enc('jx', bsrc));\n"
			"    print(Duktape.enc('jx', bdst));\n"
			"})");
		duk_dup(ctx, 0);
		duk_dup(ctx, 1);
		duk_call(ctx, 2);
		duk_pop_n(ctx, 3);

		for (i = 0; i < 48; i++) {
			printf("%02x", (unsigned int) buf[i]);
		}
		printf("\n");
	}

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
static duk_ret_t test_basic_overlap(duk_context *ctx, void *udata) {
	unsigned char buf[16];
	int offset;
	int i;

	(void) udata;

	for (offset = 0; offset <= 8; offset++) {
		printf("offset: %d\n", offset);

		/* 001122334455...ff */
		for (i = 0; i < sizeof(buf); i++) {
			buf[i] = (unsigned char) (0x11 * i);
		}

		/* Create two separate external buffer values that point to
		 * the same underlying C array with some overlap.
		 */
		duk_eval_string(ctx,
			"(function (plain1, plain2) {\n"
			"    var b1 = new Uint8Array(plain1.buffer);\n"
			"    var b2 = new Uint8Array(plain2.buffer);\n"
			"    print(Duktape.enc('jx', b1));\n"
			"    print(Duktape.enc('jx', b2));\n"
			"    b1.set(b2, 4);\n"
			"    print(Duktape.enc('jx', b1));\n"
			"    print(Duktape.enc('jx', b2));\n"
			"})");
		duk_push_external_buffer(ctx);
		duk_config_buffer(ctx, -1, (void *) buf, 16);  /* [0,16[ */
		duk_push_external_buffer(ctx);
		duk_config_buffer(ctx, -1, (void *) (buf + offset), 8);
		duk_call(ctx, 2 /*nargs*/);
		duk_pop(ctx);
	}

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #5
0
static duk_ret_t test_basic(duk_context *ctx, void *udata) {
	duk_uint_t test[] = {
		DUK_BUFOBJ_NODEJS_BUFFER,
		DUK_BUFOBJ_ARRAYBUFFER,
		DUK_BUFOBJ_DATAVIEW,
		DUK_BUFOBJ_INT8ARRAY,
		DUK_BUFOBJ_UINT8ARRAY,
		DUK_BUFOBJ_UINT8CLAMPEDARRAY,
		DUK_BUFOBJ_INT16ARRAY,
		DUK_BUFOBJ_UINT16ARRAY,
		DUK_BUFOBJ_INT32ARRAY,
		DUK_BUFOBJ_UINT32ARRAY,
		DUK_BUFOBJ_FLOAT32ARRAY,
		DUK_BUFOBJ_FLOAT64ARRAY,
	};
	int i;
	unsigned char extbuf[256];

	(void) udata;

	for (i = 0; i < sizeof(test) / sizeof(duk_uint_t); i++) {
		switch (i % 3) {
		case 0:
			duk_push_fixed_buffer(ctx, 256);
			break;
		case 1:
			duk_push_dynamic_buffer(ctx, 256);
			break;
		case 2:
			duk_push_external_buffer(ctx);
			duk_config_buffer(ctx, -1, (void *) extbuf, 256);
			break;
		}

		duk_push_undefined(ctx);  /* dummy */

		duk_push_buffer_object(ctx, -2, 128, 32, test[i]);

		duk_eval_string(ctx, "dumpBufferInfo");
		duk_dup(ctx, -2);
		duk_call(ctx, 1);

		/* ... plain undefined bufferobject result */
		duk_pop_n(ctx, 4);
	}

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #6
0
/* The basic test ensures all typed array views get a .buffer property.
 * Test the .buffer reference in more detail: check that property
 * attributes are correct, check that it backs to the same slice, etc.
 */
static duk_ret_t test_view_buffer_prop(duk_context *ctx, void *udata) {
	unsigned char extbuf[256];

	(void) udata;

	duk_push_external_buffer(ctx);
	duk_config_buffer(ctx, -1, (void *) extbuf, 256);
	duk_push_buffer_object(ctx, -1, 16, 22, DUK_BUFOBJ_UINT8ARRAY);

	duk_eval_string(ctx,
		"(function (v) {\n"
		"    var pd;\n"
		"    print(Object.prototype.toString.call(v));\n"
		"    dumpBufferInfo(v);\n"
		"    print(v.length);\n"
		"    print(v.byteOffset);\n"
		"    print(v.byteLength);\n"
		"    print(v.BYTES_PER_ELEMENT);\n"
		"    print(v.buffer);\n"
		"    pd = Object.getOwnPropertyDescriptor(v, 'buffer');\n"
		"    print(pd.value, pd.writable, pd.enumerable, pd.configurable);\n"
		"    print(Object.prototype.toString.call(v.buffer));\n"
		"    dumpBufferInfo(v.buffer);\n"
		"    print(v.buffer.length);\n"  /* some of these are Duktape custom */
		"    print(v.buffer.byteOffset);\n"
		"    print(v.buffer.byteLength);\n"
		"    print(v.buffer.BYTES_PER_ELEMENT);\n"
		"    print(v.buffer.buffer);\n"
		"    v[3] = 123;  /* check that backing buffer and slice matches */\n"
		"    print(v[3], new Uint8Array(v.buffer)[3]);\n"
		"})");
	duk_dup(ctx, -2);
	duk_call(ctx, 1);
	duk_pop_2(ctx);

	printf("extbuf[16 + 3] = %d\n", (int) extbuf[16 + 3]);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #7
0
static int
es_fap_read(fa_handle_t *fh, void *buf, size_t size)
{
  es_fa_handle_t *fah = (es_fa_handle_t *)fh;
  fah->fah_readbuf = buf;
  fah->fah_readlen = size;

  hts_mutex_lock(&es_fa_mutex);
  fah->fah_status = ES_FA_WORKING;

  task_run(es_fa_read, fah_retain(fah));

  while(fah->fah_status == ES_FA_WORKING)
    hts_cond_wait(&es_fa_cond, &es_fa_mutex);

  hts_mutex_unlock(&es_fa_mutex);

  es_fap_t *ef = fah->fah_ef;
  es_context_t *ec = ef->super.er_ctx;
  duk_context *ctx = ec->ec_duk;

  es_context_begin(ec);
  es_push_root(ctx, fah->fah_readbuf);
  duk_config_buffer(ctx, -1, NULL, 0);
  es_root_unregister(ctx, fah->fah_readbuf);
  es_context_end(ec, 0);


  switch(fah->fah_status) {
  case ES_FA_OK:
    fah->fah_fpos += fah->fah_return_value;
    return fah->fah_return_value;

  case ES_FA_CANCELLED:
  case ES_FA_ERROR:
    return -1;
  }
  return 0;
}
/* When converting from an external buffer to a "don't care" buffer,
 * an external buffer is kept as is.
 */
static duk_ret_t test_6c(duk_context *ctx) {
	unsigned char buf[16];
	int i;
	unsigned char *p;
	duk_size_t sz;

	for (i = 0; i < 16; i++) {
		buf[i] = (unsigned char) i;
	}

	duk_push_external_buffer(ctx);
	duk_config_buffer(ctx, -1, (void *) buf, 16);

	p = (unsigned char *) duk_to_buffer(ctx, -1, &sz);
	printf("sz=%ld\n", (long) sz); fflush(stdout);
	p[0] = (unsigned char) 123;
	printf("p[0]=%u, buf[0]=%u\n", (unsigned int) p[0], (unsigned int) buf[0]);

	duk_pop(ctx);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #9
0
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;
}
Пример #10
0
//void duk_config_buffer(duk_context *ctx, duk_idx_t index, void *ptr, duk_size_t len);
void aperl_duk_config_buffer(duk_context *ctx, duk_idx_t index, void *ptr, duk_size_t len) {
	duk_config_buffer(ctx, index, ptr, len);
}