static duk_ret_t test_4(duk_context *ctx, void *udata) {
	(void) udata;

	duk_set_top(ctx, 0);
	duk_require_null(ctx, DUK_INVALID_INDEX);
	printf("require DUK_INVALID_INDEX OK\n");
	return 0;
}
static duk_ret_t test_3(duk_context *ctx, void *udata) {
	(void) udata;

	duk_set_top(ctx, 0);
	duk_require_null(ctx, 0);
	printf("require 0 OK\n");
	return 0;
}
Beispiel #3
0
static duk_ret_t test_1(duk_context *ctx) {
	duk_set_top(ctx, 0);

	duk_error(ctx, DUK_ERR_RANGE_ERROR, "range error: %d", 123);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Beispiel #4
0
static duk_ret_t test_3(duk_context *ctx, void *udata) {
	(void) udata;

	duk_set_top(ctx, 0);
	(void) duk_to_buffer(ctx, DUK_INVALID_INDEX, NULL);
	printf("index DUK_INVALID_INDEX OK\n");
	return 0;
}
static duk_ret_t test_2(duk_context *ctx, void *udata) {
	(void) udata;

	duk_set_top(ctx, 0);
	duk_to_undefined(ctx, 3);
	printf("index 3 OK\n");
	return 0;
}
static duk_ret_t test_1(duk_context *ctx) {
	duk_set_top(ctx, 0);
	duk_push_string(ctx, "foo");
	duk_push_string(ctx, "");
	dump_string(duk_require_string(ctx, 0));
	dump_string(duk_require_string(ctx, 1));
	return 0;
}
Beispiel #7
0
static duk_ret_t test_2(duk_context *ctx, void *udata) {
	(void) udata;

	duk_set_top(ctx, 0);
	(void) duk_to_buffer(ctx, 3, NULL);
	printf("index 3 OK\n");
	return 0;
}
static duk_ret_t test_2(duk_context *ctx, void *udata) {
	(void) udata;

	duk_set_top(ctx, 0);
	duk_push_undefined(ctx);
	duk_require_null(ctx, 0);
	return 0;
}
static duk_ret_t test_3(duk_context *ctx, void *udata) {
	(void) udata;

	duk_set_top(ctx, 0);
	duk_to_undefined(ctx, DUK_INVALID_INDEX);
	printf("index DUK_INVALID_INDEX OK\n");
	return 0;
}
static duk_ret_t test_1(duk_context *ctx) {
	duk_set_top(ctx, 0);
	duk_push_pointer(ctx, (void *) 0xdeadbeef);
	duk_push_pointer(ctx, (void *) NULL);
	printf("pointer: %p\n", duk_require_pointer(ctx, 0));
	printf("pointer: %p\n", duk_require_pointer(ctx, 1));
	return 0;
}
DUK_INTERNAL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx) {
	duk_hthread *thr = (duk_hthread *) ctx;
	duk_hstring *h_str;

	DUK_UNREF(thr);

	/* Vararg function: must be careful to check/require arguments.
	 * The JSON helpers accept invalid indices and treat them like
	 * non-existent optional parameters.
	 */

	h_str = duk_require_hstring(ctx, 0);
	duk_require_valid_index(ctx, 1);

	if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
		duk_set_top(ctx, 2);
		duk_hex_encode(ctx, 1);
		DUK_ASSERT_TOP(ctx, 2);
	} else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
		duk_set_top(ctx, 2);
		duk_base64_encode(ctx, 1);
		DUK_ASSERT_TOP(ctx, 2);
#ifdef DUK_USE_JX
	} else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
		duk_bi_json_stringify_helper(ctx,
		                             1 /*idx_value*/,
		                             2 /*idx_replacer*/,
		                             3 /*idx_space*/,
		                             DUK_JSON_FLAG_EXT_CUSTOM |
		                             DUK_JSON_FLAG_ASCII_ONLY |
		                             DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
#endif
#ifdef DUK_USE_JC
	} else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
		duk_bi_json_stringify_helper(ctx,
		                             1 /*idx_value*/,
		                             2 /*idx_replacer*/,
		                             3 /*idx_space*/,
		                             DUK_JSON_FLAG_EXT_COMPATIBLE |
		                             DUK_JSON_FLAG_ASCII_ONLY /*flags*/);
#endif
	} else {
		return DUK_RET_TYPE_ERROR;
	}
	return 1;
}
Beispiel #12
0
static duk_ret_t test_2(duk_context *ctx) {
	duk_set_top(ctx, 0);

	duk_error(ctx, 1234567, "arbitrary error code");

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Beispiel #13
0
static duk_ret_t test_encode(duk_context *ctx) {
	duk_set_top(ctx, 0);
	duk_push_string(ctx, "foo");
	duk_push_int(ctx, 123);  /* dummy */
	printf("hex encode: %s\n", duk_hex_encode(ctx, -2));
	printf("top after: %ld\n", (long) duk_get_top(ctx));  /* value + dummy */
	return 0;
}
Beispiel #14
0
static void prep(duk_context *ctx) {
	duk_set_top(ctx, 0);
	print_stack(ctx);
	duk_push_string(ctx, "foo");
	duk_push_string(ctx, "bar");
	duk_push_string(ctx, "quux");
	print_stack(ctx);
}
Beispiel #15
0
int test_1(duk_context *ctx) {
	int i, n;
	char *buf;

	duk_set_top(ctx, 0);

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_true(ctx);
	duk_push_false(ctx);
	duk_push_nan(ctx);
	duk_push_number(ctx, -INFINITY);
	duk_push_number(ctx, +INFINITY);
	duk_push_number(ctx, -0.0);
	duk_push_number(ctx, +0.0);
	duk_push_int(ctx, 123);

	duk_push_string(ctx, "foo");
	duk_push_lstring(ctx, "foo\0bar", 7);  /* internal NULs are kept */
	duk_push_object(ctx);
	buf = (char *) duk_push_fixed_buffer(ctx, 0);
	buf = (char *) duk_push_fixed_buffer(ctx, 16);
	for (i = 0; i < 16; i++) {
		buf[i] = i;
	}
	buf = (char *) duk_push_dynamic_buffer(ctx, 0);
	buf = (char *) duk_push_dynamic_buffer(ctx, 16);
	for (i = 0; i < 16; i++) {
		buf[i] = i;
	}
	duk_push_pointer(ctx, (void *) NULL);
	duk_push_pointer(ctx, (void *) 0xdeadbeef);

	n = duk_get_top(ctx);
	printf("top: %d\n", n);
	for (i = 0; i < n; i++) {
		int t1, t2;
		void *ptr;
		size_t sz;

		duk_dup(ctx, i);
		t1 = duk_get_type(ctx, -1);
		sz = (size_t) 0xdeadbeef;
		ptr = duk_to_buffer(ctx, -1, &sz);
		t2 = duk_get_type(ctx, -1);
		printf("index %d, type %d -> %d, ptr-is-NULL %d, size %u\n",
		       i, t1, t2, (sz == 0 ? -1 : (ptr == NULL ? 1 : 0)), (unsigned int) sz);
		dump_buffer(ctx);
		duk_pop(ctx);

		/* just check that this doesn't break */
		duk_dup(ctx, i);
		ptr = duk_to_buffer(ctx, -1, NULL);
		duk_pop(ctx);
	}

	return 0;
}
Beispiel #16
0
static duk_ret_t test_3(duk_context *ctx) {
	duk_set_top(ctx, 0);

	/* error code replaces message automatically now if message is NULL */
	duk_error(ctx, DUK_ERR_TYPE_ERROR, NULL);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Beispiel #17
0
static duk_ret_t test_3(duk_context *ctx) {
	const char *p;
	duk_size_t sz;

	duk_set_top(ctx, 0);
	p = duk_to_lstring(ctx, DUK_INVALID_INDEX, &sz);
	printf("index DUK_INVALID_INDEX: %p, %ld\n", (void *) p, (unsigned long) sz);  /* ok to print, not shown in expected case */
	return 0;
}
Beispiel #18
0
static duk_ret_t test_1(duk_context *ctx) {
	printf("test_1\n");
	prep(ctx);

	/* extend with undefined */
	duk_set_top(ctx, 5);
	print_stack(ctx);

	/* chop, using a negative top value */
	duk_set_top(ctx, -3);   /* = duk_set_top(ctx, 2) */
	print_stack(ctx);

	/* to empty */
	duk_set_top(ctx, 0);
	print_stack(ctx);

	return 0;
}
Beispiel #19
0
/* invalid index */
static duk_ret_t test_3(duk_context *ctx) {
	duk_set_top(ctx, 0);

	duk_push_string(ctx, "foobar");
	duk_substring(ctx, -2, 2, 4);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Beispiel #20
0
/* non-string -> error */
static duk_ret_t test_2(duk_context *ctx) {
	duk_set_top(ctx, 0);

	duk_push_int(ctx, 123456);
	duk_substring(ctx, -1, 2, 4);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Beispiel #21
0
int test_2(duk_context *ctx) {
	const char *p;
	size_t sz;

	duk_set_top(ctx, 0);
	p = duk_to_lstring(ctx, 3, &sz);
	printf("index 3: %p, %d\n", (void *) p, (int) sz);  /* ok to print, not shown in expected case */
	return 0;
}
Beispiel #22
0
static duk_ret_t test_2(duk_context *ctx) {
	/* non-object input */
	duk_set_top(ctx, 0);
	duk_push_int(ctx, 123);
	duk_to_defaultvalue(ctx, 0, DUK_HINT_NONE);
	printf("non-object value OK\n");
	return 0;

}
Beispiel #23
0
int test_4(duk_context *ctx) {
	void *ptr;
	size_t sz;

	duk_set_top(ctx, 0);
	sz = (size_t) 0xdeadbeef;
	ptr = duk_require_buffer(ctx, DUK_INVALID_INDEX, &sz);
	printf("buffer ok: %p\n", ptr);  /* ok to print, should not be reached */
	return 0;
}
Beispiel #24
0
static duk_ret_t test_3a(duk_context *ctx, void *udata) {
	(void) udata;

	duk_set_top(ctx, 0);

	duk_swap_top(ctx, 0);  /* empty stack */

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
static duk_ret_t test_3(duk_context *ctx) {
	void *ptr;
	duk_size_t sz;

	duk_set_top(ctx, 0);
	sz = (duk_size_t) 0xdeadbeefUL;
	ptr = duk_require_buffer_data(ctx, 0, &sz);
	printf("buffer ok: %p\n", ptr);  /* ok to print, should not be reached */
	return 0;
}
Beispiel #26
0
int test_4(duk_context *ctx) {
	duk_set_top(ctx, 0);

	duk_push_int(ctx, 123);
	duk_trim(ctx, DUK_INVALID_INDEX);
	printf("trimmed DUK_INVALID_INDEX, should not happen\n");

	printf("final top: %d\n", duk_get_top(ctx));
	return 0;
}
Beispiel #27
0
int test_3(duk_context *ctx) {
	duk_set_top(ctx, 0);

	duk_push_int(ctx, 123);
	duk_trim(ctx, 4);
	printf("trimmed invalid index, should not happen\n");

	printf("final top: %d\n", duk_get_top(ctx));
	return 0;
}
Beispiel #28
0
int test_2(duk_context *ctx) {
	duk_set_top(ctx, 0);

	duk_push_int(ctx, 123);
	duk_trim(ctx, -1);
	printf("trimmed non-string, should not happen\n");

	printf("final top: %d\n", duk_get_top(ctx));
	return 0;
}
Beispiel #29
0
int test_2(duk_context *ctx) {
	duk_context *new_ctx;

	/* non-thread value */
	duk_set_top(ctx, 0);
	duk_push_int(ctx, 123);
	new_ctx = duk_require_context(ctx, -1);
	printf("new_ctx is NULL: %d\n", (new_ctx == NULL ? 1 : 0));
	return 0;
}
Beispiel #30
0
int test_4(duk_context *ctx) {
	duk_context *new_ctx;

	/* invalid index */
	duk_set_top(ctx, 0);
	duk_push_int(ctx, 123);
	new_ctx = duk_require_context(ctx, DUK_INVALID_INDEX);
	printf("new_ctx is NULL: %d\n", (new_ctx == NULL ? 1 : 0));
	return 0;
}