static int parse_string_getdate(duk_context *ctx, const char *str) {
	struct tm tm;
	int rc;
	time_t t;

	/* For this to work, DATEMSK must be set, to this is not very
	 * convenient for an embeddable interpreter.
	 */

	DUK_MEMSET(&tm, 0, sizeof(struct tm));
	rc = getdate_r(str, &tm);
	DUK_DDDPRINT("getdate_r() -> %d", rc);

	if (rc == 0) {
		t = mktime(&tm);
		DUK_DDDPRINT("mktime() -> %d", (int) t);
		if (t >= 0) {
			duk_push_number(ctx, (double) t);
			return 1;
		}
	}

	duk_push_nan(ctx);
	return 1;
}
int duk_builtin_date_prototype_get_timezone_offset(duk_context *ctx) {
	/*
	 *  Return (t - LocalTime(t)) in minutes:
	 *
	 *    t - LocalTime(t) = t - (t + LocalTZA + DaylightSavingTA(t))
	 *                     = -(LocalTZA + DaylightSavingTA(t))
	 *
	 *  where DaylightSavingTA() is checked for time 't'.
	 *
	 *  Note that the sign of the result is opposite to common usage,
	 *  e.g. for EE(S)T which normally is +2h or +3h from UTC, this
	 *  function returns -120 or -180.
	 *
	 */

	double d;
	int tzoffset;

	/* Note: DST adjustment is determined using UTC time. */
	d = push_this_and_get_timeval(ctx, 0 /*flags*/);
	DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
	if (DUK_ISNAN(d)) {
		duk_push_nan(ctx);
	} else {
		DUK_ASSERT(DUK_ISFINITE(d));
		tzoffset = GET_LOCAL_TZOFFSET(d);
		duk_push_int(ctx, -tzoffset / 60);
	}
	return 1;
}
static int parse_string(duk_context *ctx, const char *str) {
	/* XXX: there is a small risk here: because the ISO 8601 parser is
	 * very loose, it may end up parsing some datetime values which
	 * would be better parsed with a platform specific parser.
	 */

	DUK_ASSERT(str != NULL);
	DUK_DDDPRINT("parse datetime from string '%s'", str);

	if (parse_string_iso8601_subset(ctx, str) > 0) {
		return 1;
	}

#if defined(DUK_USE_DATE_PRS_STRPTIME)
	if (parse_string_strptime(ctx, str) > 0) {
		return 1;
	}
#elif defined(DUK_USE_DATE_PRS_GETDATE)
	if (parse_string_getdate(ctx, str) > 0) {
		return 1;
	}
#else
	/* No platform-specific parsing, this is not an error. */
#endif

	duk_push_nan(ctx);
	return 1;
}
示例#4
0
/*
 * [0] - dukf_ssl_context_t - pointer
 * [1] - data to write - buffer or string
 */
static duk_ret_t js_ssl_write(duk_context *ctx) {
	char errortext[256];
	LOGD(">> js_ssl_write");
	dukf_ssl_context_t *dukf_ssl_context = duk_get_pointer(ctx, -2);
	size_t len;
	uint8_t *buf;

	// If the data is a string, then the string is the data to transmit else it
	// is a buffer and the content of the buffer is the data to transmit,
	if (duk_is_string(ctx, -1)) {
		buf = (void *)duk_get_string(ctx, -1);
		len = strlen((char *)buf);
	} else {
		buf = duk_get_buffer_data(ctx, -1, &len);
		if (len == 0) {
			LOGE("js_ssl_write: The data buffer is zero length.");
			return 0;
		}
	}

	LOGD("About to send data over SSL: %.*s", len, buf);
	int rc = mbedtls_ssl_write(&dukf_ssl_context->ssl, buf, len);
	if (rc < 0) {
		 mbedtls_strerror(rc, errortext, sizeof(errortext));
		 LOGE("error from mbedtls_ssl_write: %d - %x - %s", rc, rc, errortext);
		 duk_push_nan(ctx);
	} else {
		duk_push_int(ctx, rc);
	}
	LOGD("<< js_ssl_write: rc=%d", rc);
	return 1;
} // js_ssl_write
示例#5
0
void test(duk_context *ctx) {
	duk_idx_t i, n;

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_true(ctx);
	duk_push_false(ctx);
	duk_push_string(ctx, "foo");
	duk_push_string(ctx, "123");
	duk_push_number(ctx, -INFINITY);
	duk_push_number(ctx, -123456789.0);
	duk_push_number(ctx, -0.0);
	duk_push_number(ctx, +0.0);
	duk_push_number(ctx, +123456789.0);
	duk_push_number(ctx, +INFINITY);
	duk_push_nan(ctx);
	duk_push_object(ctx);

	n = duk_get_top(ctx);
	printf("top: %ld\n", (long) n);
	for (i = 0; i < n; i++) {
		double d = duk_get_number(ctx, i);
		int c = fpclassify(d);
		printf("index %ld: number %lf, FP_NAN=%d, FP_INFINITE=%d, FP_ZERO=%d, FP_SUBNORMAL=%d, FP_NORMAL=%d, signbit=%d\n",
		       (long) i, d, (c == FP_NAN ? 1 : 0), (c == FP_INFINITE ? 1 : 0), (c == FP_ZERO ? 1 : 0),
		       (c == FP_SUBNORMAL ? 1 : 0), (c == FP_NORMAL ? 1 : 0), (signbit(d) ? 1 : 0));
	}
}
示例#6
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 *) 0xdeadbeef);

	n = duk_get_top(ctx);
	printf("top: %ld\n", (long) n);
	for (i = 0; i < n; i++) {
		duk_to_null(ctx, i);
		printf("index %ld, is-null: %d\n", (long) i, (int) duk_is_null(ctx, i));
	}

	return 0;
}
int test_1(duk_context *ctx) {
    int i, n;

    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 *) 0xdeadbeef);

    n = duk_get_top(ctx);
    printf("top: %d\n", n);
    for (i = 0; i < n; i++) {
        duk_to_undefined(ctx, i);
        printf("index %d, is-undefined: %d\n", i, duk_is_undefined(ctx, i));
    }

    return 0;
}
static duk_ret_t test_string(duk_context *ctx, void *udata) {
	duk_idx_t i, n;

	(void) udata;

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_true(ctx);
	duk_push_false(ctx);
	duk_push_string(ctx, "");
	duk_push_string(ctx, "foo");
	duk_push_lstring(ctx, "foo\0bar", 7);
	duk_push_string(ctx, "\xe1\x88\xb4xyz");  /* 4 chars, first char utf-8 encoded U+1234 */
	duk_push_nan(ctx);
	duk_push_object(ctx);

	n = duk_get_top(ctx);
	printf("top: %ld\n", (long) n);
	for (i = 0; i <= n; i++) {
		printf("index %ld: ", (long) i);
		dump((const unsigned char *) duk_get_string_default(ctx, i, "default"));
	}

	return 0;
}
static duk_ret_t test_lstring(duk_context *ctx, void *udata) {
	duk_idx_t i, n;

	(void) udata;

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_true(ctx);
	duk_push_false(ctx);
	duk_push_string(ctx, "");
	duk_push_string(ctx, "foo");
	duk_push_lstring(ctx, "foo\0bar", 7);
	duk_push_string(ctx, "\xe1\x88\xb4xyz");  /* 4 chars, first char utf-8 encoded U+1234 */
	duk_push_nan(ctx);
	duk_push_object(ctx);

	n = duk_get_top(ctx);
	printf("top: %ld\n", (long) n);
	for (i = 0; i < n; i++) {
		const char *buf;
		size_t len;

		len = (size_t) 0xdeadbeefUL;
		buf = duk_get_lstring_default(ctx, i, &len, "default!", 7);
		printf("index %ld: length %lu: ",
		       (long) i, (unsigned long) len);
		dump((const unsigned char *) buf);

		buf = duk_get_lstring_default(ctx, i, NULL, "default!", 7);
		printf("index %ld: ", (long) i);
		dump((const unsigned char *) buf);
	}

	return 0;
}
示例#10
0
static duk_ret_t test_1(duk_context *ctx) {
	duk_size_t 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: %ld\n", (long) n);
	for (i = 0; i < n; i++) {
		duk_int_t t1, t2;
		void *ptr;
		duk_size_t sz;

		duk_dup(ctx, i);
		t1 = duk_get_type(ctx, -1);
		sz = (duk_size_t) 0xdeadbeef;
		ptr = duk_to_buffer(ctx, -1, &sz);
		t2 = duk_get_type(ctx, -1);
		printf("index %ld, type %ld -> %ld, ptr-is-NULL %d, size %lu\n",
		       (long) i, (long) t1, (long) t2,
		       (sz == 0 ? -1 : (ptr == NULL ? 1 : 0)),
		       (unsigned long) 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;
}
示例#11
0
int test_1(duk_context *ctx) {
	int i, n;

	duk_set_top(ctx, 0);
	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);

	n = duk_get_top(ctx);
	printf("top: %d\n", n);
	for (i = 0; i < n; i++) {
		int t;
		duk_to_object(ctx, i);
		t = duk_get_type(ctx, i);
		duk_to_string(ctx, i);
		printf("index %d, type: %d, string coerced: %s\n", i, t, duk_to_string(ctx, i));
	}

	return 0;
}
示例#12
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, 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 *) 0xdeadbeefUL);

	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;
}
示例#13
0
/*
 * [0] - dukf_ssl_context_t - pointer
 * [1] - data to read - buffer
 */
static duk_ret_t js_ssl_read(duk_context *ctx) {
	char errortext[256];
	LOGD(">> js_ssl_read");
	dukf_ssl_context_t *dukf_ssl_context = duk_get_pointer(ctx, -2);
	size_t len;
	uint8_t *buf = duk_get_buffer_data(ctx, -1, &len);
	int rc = mbedtls_ssl_read(&dukf_ssl_context->ssl, buf, len);
	if (rc < 0) {
		 mbedtls_strerror(rc, errortext, sizeof(errortext));
		 LOGE("error from mbedtls_ssl_read: %d - %x - %s", rc, rc, errortext);
		 duk_push_nan(ctx);
	} else {
		duk_push_int(ctx, rc);
	}
	LOGD("<< js_ssl_read: rc=%d", rc);
	return 1;
} // js_ssl_read
示例#14
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;
}
示例#15
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;
}
示例#16
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_number(ctx, 123.456);
	duk_to_object(ctx, -1);  /* Number(123.456) */
	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 *) 0xdeadbeef);

	n = duk_get_top(ctx);
	printf("top: %ld\n", (long) n);
	for (i = 0; i < n; i++) {
		duk_int_t t1, t2;

		t1 = duk_get_type(ctx, i);
		duk_to_primitive(ctx, i, DUK_HINT_NONE);
		t2 = duk_get_type(ctx, i);

		printf("index %ld, ToString(result): '%s', type: %ld -> %ld\n",
		       (long) i, duk_to_string(ctx, i), (long) t1, (long) t2);
	}

	return 0;
}
示例#17
0
/* Magic: 0=charCodeAt, 1=codePointAt */
DUK_INTERNAL duk_ret_t duk_bi_string_prototype_char_code_at(duk_context *ctx) {
	duk_hthread *thr = (duk_hthread *) ctx;
	duk_int_t pos;
	duk_hstring *h;
	duk_bool_t clamped;
	duk_uint32_t cp;
	duk_int_t magic;

	/* XXX: faster implementation */

	DUK_DDD(DUK_DDDPRINT("arg=%!T", (duk_tval *) duk_get_tval(ctx, 0)));

	h = duk_push_this_coercible_to_string(ctx);
	DUK_ASSERT(h != NULL);

	pos = duk_to_int_clamped_raw(ctx,
	                             0 /*index*/,
	                             0 /*min(incl)*/,
	                             DUK_HSTRING_GET_CHARLEN(h) - 1 /*max(incl)*/,
	                             &clamped /*out_clamped*/);
#if defined(DUK_USE_ES6)
	magic = duk_get_current_magic(ctx);
#else
	DUK_ASSERT(duk_get_current_magic(ctx) == 0);
	magic = 0;
#endif
	if (clamped) {
		/* For out-of-bounds indices .charCodeAt() returns NaN and
		 * .codePointAt() returns undefined.
		 */
		if (magic != 0) {
			return 0;
		}
		duk_push_nan(ctx);
	} else {
		cp = (duk_uint32_t) duk_hstring_char_code_at_raw(thr, h, pos, (duk_bool_t) magic /*surrogate_aware*/);
		duk_push_u32(ctx, cp);
	}
	return 1;
}
示例#18
0
void test(duk_context *ctx) {
	const char *res;

	duk_push_undefined(ctx); PRINTTOP();
	duk_push_null(ctx); PRINTTOP();
	duk_push_true(ctx); PRINTTOP();
	duk_push_false(ctx); PRINTTOP();
	duk_push_boolean(ctx, -1); PRINTTOP();
	duk_push_boolean(ctx, 0); PRINTTOP();
	duk_push_boolean(ctx, 1); PRINTTOP();
	duk_push_number(ctx, 123.4); PRINTTOP();
	duk_push_int(ctx, 234); PRINTTOP();
	duk_push_nan(ctx); PRINTTOP();

	res = duk_push_string(ctx, "foo"); PRINTRESTOP();
	res = duk_push_string(ctx, "foo\0bar\0"); PRINTRESTOP();
	res = duk_push_string(ctx, ""); PRINTRESTOP();    /* pushes empty */
	res = duk_push_string(ctx, NULL); PRINTRESTOP();  /* pushes a NULL */

	res = duk_push_lstring(ctx, "foobar", 4); PRINTRESTOP();
	res = duk_push_lstring(ctx, "foob\0\0", 6); PRINTRESTOP();
	res = duk_push_lstring(ctx, "\0", 1); PRINTRESTOP();  /* pushes 1-byte string (0x00) */
	res = duk_push_lstring(ctx, "\0", 0); PRINTRESTOP();  /* pushes empty */
	res = duk_push_lstring(ctx, NULL, 0); PRINTRESTOP();  /* pushes empty */
	res = duk_push_lstring(ctx, NULL, 10); PRINTRESTOP(); /* pushes empty */

	res = duk_push_sprintf(ctx, "foo"); PRINTRESTOP();
	res = duk_push_sprintf(ctx, "foo %d %s 0x%08lx", 123, "bar", (long) 0x1234cafe); PRINTRESTOP();
	res = duk_push_sprintf(ctx, ""); PRINTRESTOP();
	res = duk_push_sprintf(ctx, NULL); PRINTRESTOP();
	
	res = test_vsprintf_3x_int(ctx, 2, 3, 5); PRINTRESTOP();
	res = test_vsprintf_empty(ctx, 2, 3, 5); PRINTRESTOP();
	res = test_vsprintf_null(ctx, 2, 3, 5); PRINTRESTOP();

	duk_push_pointer(ctx, (void *) 0); PRINTTOP();
	duk_push_pointer(ctx, (void *) 0xdeadbeef); PRINTTOP();
}
示例#19
0
/* Helper for component getter calls: check 'this' binding, get the
 * internal time value, split it into parts (either as UTC time or
 * local time), push a specified component as a return value to the
 * value stack and return 1 (caller can then tailcall us).
 */
static int get_part_helper(duk_context *ctx, int flags_and_idx) {
	double d;
	int parts[NUM_PARTS];
	int idx_part = flags_and_idx >> 12;  /* unpack args */

	DUK_ASSERT(idx_part >= 0 && idx_part < NUM_PARTS);

	d = push_this_and_get_timeval(ctx, flags_and_idx);
	if (DUK_ISNAN(d)) {
		duk_push_nan(ctx);
		return 1;
	}
	DUK_ASSERT(DUK_ISFINITE(d));

	timeval_to_parts(d, parts, NULL, flags_and_idx);

	/* Setter APIs detect special year numbers (0...99) and apply a +1900
	 * only in certain cases.  The legacy getYear() getter applies -1900
	 * unconditionally.
	 */
	duk_push_int(ctx, (flags_and_idx & FLAG_SUB1900) ? parts[idx_part] - 1900 : parts[idx_part]);
	return 1;
}
示例#20
0
static duk_ret_t test_1(duk_context *ctx) {
	duk_int_t i, j, n;
	char *ptr;

	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_number(ctx, -INFINITY);
	duk_push_string(ctx, "");
	duk_push_string(ctx, "foo");
	duk_push_lstring(ctx, "foo\0bar", 7);
	duk_push_object(ctx);
	duk_push_thread(ctx);
	(void) duk_push_fixed_buffer(ctx, 0);
	ptr = (char *) duk_push_fixed_buffer(ctx, 16);
	for (i = 0; i < 16; i++) {
		ptr[i] = (char) ('a' + i);
	}
	(void) duk_push_dynamic_buffer(ctx, 0);
	ptr = (char *) duk_push_dynamic_buffer(ctx, 16);
	for (i = 0; i < 16; i++) {
		ptr[i] = (char) ('a' + i);
	}
	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++) {
		const unsigned char *p;
		duk_size_t sz;

		duk_dup(ctx, i);
		sz = (duk_size_t) 0xdeadbeef;
		p = (const unsigned char *) duk_to_lstring(ctx, -1, &sz);
		printf("index %ld, string: '", (long) i);
		for (j = 0; j < sz; j++) {
			if (p[j] >= 0x20 && p[j] <= 0x7e) {
				printf("%c", (int) p[j]);
			} else {
				printf("\\x%02x", (int) p[j]);
			}
		}
		printf("', length %lu\n", (unsigned long) sz);
		duk_pop(ctx);

		duk_dup(ctx, i);
		sz = (duk_size_t) 0xdeadbeef;
		p = (const unsigned char *) duk_to_lstring(ctx, -1, NULL);
		printf("index %ld, string: '%s'\n", (long) i, (const char *) p);
		duk_pop(ctx);
	}

	return 0;
}
示例#21
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;
}
示例#22
0
void test(duk_context *ctx) {
	int i, n;

	/* 0 */
	duk_push_string(ctx, "foo");

	/* 1 */
	duk_push_string(ctx, "\xe1\x88\xb4xyz");  /* 4 chars, first char utf-8 encoded U+1234 */

	/* 2 */
	duk_push_object(ctx);  /* no length property */

	/* 3 */
	duk_push_object(ctx);  /* length: 123 */
	duk_push_int(ctx, 123);
	duk_put_prop_string(ctx, -2, "length");

	/* 4 */
	duk_push_object(ctx);  /* length: "bar" */
	duk_push_string(ctx, "bar");
	duk_put_prop_string(ctx, -2, "length");

	/* 5 */
	duk_push_object(ctx);  /* length: 123.9, fractional number */
	duk_push_number(ctx, 123.9);
	duk_put_prop_string(ctx, -2, "length");

	/* 6 */
	duk_push_object(ctx);  /* length: negative but within 32-bit range after ToInteger() */
	duk_push_number(ctx, -0.9);
	duk_put_prop_string(ctx, -2, "length");

	/* 7 */
	duk_push_object(ctx);  /* length: negative, outside 32-bit range */
	duk_push_number(ctx, -1);
	duk_put_prop_string(ctx, -2, "length");

	/* 8 */
	duk_push_object(ctx);  /* length: outside 32-bit range but within range after ToInteger() */
	duk_push_number(ctx, 4294967295.9);
	duk_put_prop_string(ctx, -2, "length");

	/* 9 */
	duk_push_object(ctx);  /* length: outside 32-bit range */
	duk_push_number(ctx, 4294967296);
	duk_put_prop_string(ctx, -2, "length");

	/* 10 */
	duk_push_object(ctx);  /* length: nan */
	duk_push_nan(ctx);
	duk_put_prop_string(ctx, -2, "length");

	/* 11 */
	duk_push_object(ctx);  /* length: +Infinity */
	duk_push_number(ctx, INFINITY);
	duk_put_prop_string(ctx, -2, "length");

	/* 12 */
	duk_push_object(ctx);  /* length: -Infinity */
	duk_push_number(ctx, -INFINITY);
	duk_put_prop_string(ctx, -2, "length");

	/* 13 */
	duk_push_fixed_buffer(ctx, 1234);

	/* 14 */
	duk_push_dynamic_buffer(ctx, 2345);

	/* 15 */
	duk_push_undefined(ctx);

	/* 16 */
	duk_push_null(ctx);

	/* 17 */
	duk_push_true(ctx);

	/* 18 */
	duk_push_false(ctx);

	n = duk_get_top(ctx);
	printf("top: %d\n", n);
	for (i = 0; i < n; i++) {
		printf("index %d: length %u\n", i, (unsigned int) duk_get_length(ctx, i));
	}
}
示例#23
0
void test(duk_context *ctx) {
	duk_idx_t i, n;

	/*
	 *  push test values
	 */

	/* 0 */
	duk_push_undefined(ctx);

	/* 1 */
	duk_push_null(ctx);

	/* 2 */
	duk_push_true(ctx);

	/* 3 */
	duk_push_false(ctx);

	/* 4 */
	duk_push_int(ctx, 123);

	/* 5 */
	duk_push_number(ctx, 123.4);

	/* 6 */
	duk_push_nan(ctx);

	/* 7 */
	duk_push_number(ctx, INFINITY);

	/* 8 */
	duk_push_number(ctx, -INFINITY);

	/* 9 */
	duk_push_string(ctx, "");

	/* 10 */
	duk_push_string(ctx, "foo");

	/* 11 */
	duk_push_object(ctx);

	/* 12 */
	duk_push_array(ctx);

	/* 13 */
	duk_push_c_function(ctx, my_c_func, DUK_VARARGS);

	/* 14 */
	duk_push_string(ctx, "(function() { print('hello'); })");
	duk_eval(ctx);

	/* 15 */
	duk_push_string(ctx, "escape.bind(null, 'foo')");
	duk_eval(ctx);

	/* 16 */
	duk_push_thread(ctx);

	/* 17 */
	duk_push_buffer(ctx, 1024, 0 /*dynamic*/);

	/* 18 */
	duk_push_buffer(ctx, 1024, 1 /*dynamic*/);

	/* 19 */
	duk_push_pointer(ctx, (void *) 0xf00);

	/*
	 *  call checkers for each
	 */

	n = duk_get_top(ctx);
	for (i = 0; i < n; i++) {
		printf("%02ld: ", (long) i);
		printf(" und=%d", (int) duk_is_undefined(ctx, i));
		printf(" null=%d", (int) duk_is_null(ctx, i));
		printf(" noru=%d", (int) duk_is_null_or_undefined(ctx, i));
		printf(" bool=%d", (int) duk_is_boolean(ctx, i));
		printf(" num=%d", (int) duk_is_number(ctx, i));
		printf(" nan=%d", (int) duk_is_nan(ctx, i));
		printf(" str=%d", (int) duk_is_string(ctx, i));
		printf(" obj=%d", (int) duk_is_object(ctx, i));
		printf(" arr=%d", (int) duk_is_array(ctx, i));
		printf(" fun=%d", (int) duk_is_function(ctx, i));
		printf(" cfun=%d", (int) duk_is_c_function(ctx, i));
		printf(" efun=%d", (int) duk_is_ecmascript_function(ctx, i));
		printf(" bfun=%d", (int) duk_is_bound_function(ctx, i));
		printf(" call=%d", (int) duk_is_callable(ctx, i));
		printf(" thr=%d", (int) duk_is_thread(ctx, i));
		printf(" buf=%d", (int) duk_is_buffer(ctx, i));
		printf(" dyn=%d", (int) duk_is_dynamic_buffer(ctx, i));
		printf(" fix=%d", (int) duk_is_fixed_buffer(ctx, i));
		printf(" ptr=%d", (int) duk_is_pointer(ctx, i));
		printf(" prim=%d", (int) duk_is_primitive(ctx, i));
		printf(" objcoerc=%d", (int) duk_is_object_coercible(ctx, i));
		printf("\n");
	}
}
示例#24
0
void Context::pushNaN()
{
    duk_push_nan(m_handle);
}
示例#25
0
/* Helper for component setter calls: check 'this' binding, get the
 * internal time value, split it into parts (either as UTC time or
 * local time), modify one or more components as specified, recompute
 * the time value, set it as the internal value.  Finally, push the
 * new time value as a return value to the value stack and return 1
 * (caller can then tailcall us).
 */
static int set_part_helper(duk_context *ctx, int flags_and_maxnargs) {
	double d;
	int parts[NUM_PARTS];
	double dparts[NUM_PARTS];
	int nargs;
	int maxnargs = flags_and_maxnargs >> 12;  /* unpack args */
	int idx_first, idx;
	int i;

	nargs = duk_get_top(ctx);
	d = push_this_and_get_timeval(ctx, flags_and_maxnargs);
	DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));

	if (DUK_ISFINITE(d)) {
		timeval_to_parts(d, parts, dparts, flags_and_maxnargs);
	} else {
		/* NaN timevalue: we need to coerce the arguments, but
		 * the resulting internal timestamp needs to remain NaN.
		 * This works but is not pretty: parts and dparts will
		 * be partially uninitialized, but we only write to it.
		 */
	}

	/*
	 *  Determining which datetime components to overwrite based on
	 *  stack arguments is a bit complicated, but important to factor
	 *  out from setters themselves for compactness.
	 *
	 *  If FLAG_TIMESETTER, maxnargs indicates setter type:
	 *
	 *   1 -> millisecond
	 *   2 -> second, [millisecond]
	 *   3 -> minute, [second], [millisecond]
	 *   4 -> hour, [minute], [second], [millisecond]
	 *
	 *  Else:
	 *
	 *   1 -> date
	 *   2 -> month, [date]
	 *   3 -> year, [month], [date]
	 *
	 *  By comparing nargs and maxnargs (and flags) we know which
	 *  components to override.  We rely on part index ordering.
	 */

	if (flags_and_maxnargs & FLAG_TIMESETTER) {
		DUK_ASSERT(maxnargs >= 1 && maxnargs <= 4);
		idx_first = IDX_MILLISECOND - (maxnargs - 1);
	} else {
		DUK_ASSERT(maxnargs >= 1 && maxnargs <= 3);
		idx_first = IDX_DAY - (maxnargs - 1);
	}
	DUK_ASSERT(idx_first >= 0 && idx_first < NUM_PARTS);

	for (i = 0; i < maxnargs; i++) {
		if (i >= nargs) {
			/* no argument given -> leave components untouched */
			break;
		}
		idx = idx_first + i;
		DUK_ASSERT(idx >= 0 && idx < NUM_PARTS);

		if (idx == IDX_YEAR && (flags_and_maxnargs & FLAG_YEAR_FIXUP)) {
			twodigit_year_fixup(ctx, i);
		}

		dparts[idx] = duk_to_number(ctx, i);

		if (idx == IDX_DAY) {
			/* Day-of-month is one-based in the API, but zero-based
			 * internally, so fix here.  Note that month is zero-based
			 * both in the API and internally.
			 */
			dparts[idx] -= 1.0;
		}
	}

	/* Leaves new timevalue on stack top and returns 1, which is correct
	 * for part setters.
	 */
	if (DUK_ISFINITE(d)) {
		return set_this_timeval_from_dparts(ctx, dparts, flags_and_maxnargs);
	} else {
		/* Internal timevalue is already NaN, so don't touch it. */
		duk_push_nan(ctx);
		return 1;
	}
}
示例#26
0
void test(duk_context *ctx) {
	double d;

	/* INT_MIN/INT_MAX cases don't print concrete numbers because
	 * the limits are platform dependent.  In particular, on platforms
	 * with 64-bit ints we want to allow the full range.
	 */

	printf("positive numbers truncate towards zero\n");
	d = 3.9;
	duk_push_number(ctx, d);
	printf("number: %d\n", duk_get_int(ctx, -1));
	duk_pop(ctx);

	printf("negative numbers truncate towards zero\n");
	d = -3.9;
	duk_push_number(ctx, d);
	printf("number: %d\n", duk_get_int(ctx, -1));
	duk_pop(ctx);

	printf("integer below INT_MIN, coerces to INT_MIN\n");
	d = ((double) INT_MIN) - 1.0;
	duk_push_number(ctx, d);
#if 0
	printf("number: %d\n", duk_get_int(ctx, -1));
#endif
	printf("number is INT_MIN: %d\n", (duk_get_int(ctx, -1) == INT_MIN));
	duk_pop(ctx);

	printf("INT_MIN\n");
	d = (double) INT_MIN;
	duk_push_number(ctx, d);
#if 0
	printf("number: %d\n", duk_get_int(ctx, -1));
#endif
	printf("number is INT_MIN: %d\n", (duk_get_int(ctx, -1) == INT_MIN));
	duk_pop(ctx);

	printf("INT_MAX\n");
	d = (double) INT_MAX;
	duk_push_number(ctx, d);
#if 0
	printf("number: %d\n", duk_get_int(ctx, -1));
#endif
	printf("number is INT_MAX: %d\n", (duk_get_int(ctx, -1) == INT_MAX));
	duk_pop(ctx);

	printf("integer above INT_MAX, coerces to INT_MAX\n");
	d = ((double) INT_MAX) + 1.0;
	duk_push_number(ctx, d);
#if 0
	printf("number: %d\n", duk_get_int(ctx, -1));
#endif
	printf("number is INT_MAX: %d\n", (duk_get_int(ctx, -1) == INT_MAX));
	duk_pop(ctx);

	printf("NaN coerces to zero\n");
	duk_push_nan(ctx);
	printf("number: %d\n", duk_get_int(ctx, -1));
	duk_pop(ctx);

	printf("non-number coerces to zero\n");
	duk_push_string(ctx, "123");
	printf("number: %d\n", duk_get_int(ctx, -1));
	duk_pop(ctx);
}
示例#27
0
int test_1(duk_context *ctx) {
	int i, n;

	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_number(ctx, 123.456);
	duk_push_number(ctx, -123.456);
	duk_push_number(ctx, 123.999);
	duk_push_number(ctx, -123.999);
	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);
	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 *) 0xdeadbeef);

	n = duk_get_top(ctx);
	printf("top: %d\n", n);
	for (i = 0; i < n; i++) {
		double dval_pre;
		double dval_post;
		int ival;

		dval_pre = duk_get_number(ctx, i);  /* number before ToInteger() coercion */
		ival = duk_to_int(ctx, i);
		dval_post = duk_get_number(ctx, i);      /* number after ToInteger() coercion */
		printf("index %d, ", i);
		if (ival == INT_MIN) {
			printf("int: INT_MIN");
		} else if (ival == INT_MAX) {
			printf("int: INT_MAX");
		} else {
			printf("int: %d", ival);
		}
		printf(", number before: %lf, number after: %lf\n", dval_pre, dval_post);
	}

	return 0;
}
static duk_ret_t test_1(duk_context *ctx) {
	duk_idx_t i, n;

	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_int(ctx, -1);
	duk_push_number(ctx, 123.456);
	duk_push_number(ctx, -123.456);
	duk_push_number(ctx, 123.999);

	duk_push_number(ctx, -123.999);
	duk_push_number(ctx, -2147483649.0); /* min int32 - 1 */
	duk_push_number(ctx, -2147483648.0); /* min int32 */
	duk_push_number(ctx, 2147483647.0);  /* max int32 */
	duk_push_number(ctx, 2147483648.0);  /* max int32 + 1 */
	duk_push_number(ctx, 4294967295.0);  /* max uint32 */
	duk_push_number(ctx, 4294967296.0);  /* max uint32 + 1 */
	duk_push_number(ctx, 65535.0);       /* max uint16 */
	duk_push_number(ctx, 65536.0);       /* max uint16 + 1 */
	duk_push_number(ctx, 9999999999.0);

	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);     /* ToNumber('') = 0 */
	duk_push_fixed_buffer(ctx, 1024);  /* ToNumber('\u0000...') = NaN, converts into 0 when doing integer coercion */
	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++) {
		duk_double_t dval_pre;
		duk_double_t dval_post;
		duk_int_t ival;
		duk_uint_t uval;

		duk_dup(ctx, i);
		dval_pre = duk_get_number(ctx, -1);
		ival = duk_to_int32(ctx, -1);
		dval_post = duk_get_number(ctx, -1);
		printf("index %ld, int32: %ld, number before: %lf, number after: %lf\n",
		       (long) i, (long) ival, (double) dval_pre, (double) dval_post);
		duk_pop(ctx);

		duk_dup(ctx, i);
		dval_pre = duk_get_number(ctx, -1);
		uval = duk_to_uint32(ctx, -1);
		dval_post = duk_get_number(ctx, -1);
		printf("index %ld, uint32: %lu, number before: %lf, number after: %lf\n",
		       (long) i, (unsigned long) uval, (double) dval_pre, (double) dval_post);
		duk_pop(ctx);

		duk_dup(ctx, i);
		dval_pre = duk_get_number(ctx, -1);
		uval = duk_to_uint16(ctx, -1);
		dval_post = duk_get_number(ctx, -1);
		printf("index %ld, uint16: %lu, number before: %lf, number after: %lf\n",
		       (long) i, (unsigned long) uval, (double) dval_pre, (double) dval_post);
		duk_pop(ctx);
	}

	return 0;
}
示例#29
0
/*
 * Call a JS method/function.
 * Usage: call token method this ?{arg ?type?}? ...
 * Return value: the result of the method call coerced to string.
 * Side effects: may change the Duktape interpreter heap.
 */
static int
CallMethod_Cmd(ClientData cdata, Tcl_Interp *interp, int objc,
        Tcl_Obj *const objv[])
{
    int i;
    int list_length;
    int tableIndex;
    int int_value;
    double double_value;
    duk_context *ctx;
    duk_int_t duk_result;
    Tcl_Obj *value;
    Tcl_Obj *type;

    static const char *types[] = {
        "boolean",
        "nan",
        "null",
        "number",
        "string",
        "undefined",
        (char *)NULL
    };
    enum types {
        TYPE_BOOLEAN,
        TYPE_NAN,
        TYPE_NULL,
        TYPE_NUMBER,
        TYPE_STRING,
        TYPE_UNDEFINED
    };

    if (objc < 4) {
        Tcl_WrongNumArgs(interp, 1, objv, USAGE_CALL_METHOD);
        return TCL_ERROR;
    }

    ctx = parse_id(cdata, interp, objv[1], 0);
    if (ctx == NULL) {
        return TCL_ERROR;
    }

    /* Eval the function name and "this" to put them on the stack. */
    for (i = 2; i < 4; i++)
    {
        duk_result = duk_peval_string(ctx, Tcl_GetString(objv[i]));
        if (duk_result != 0) {
            Tcl_SetObjResult(interp,
                    Tcl_NewStringObj(
                        duk_safe_to_string(ctx, -1), -1));
            duk_pop(ctx);
            return TCL_ERROR;
        }
    }

    /* Push the arguments. */
    for (i = 4; i < objc; i++) {
        if (Tcl_ListObjIndex(interp, objv[i], 0, &value) != TCL_OK) {
            return TCL_ERROR;
        }

        if (Tcl_ListObjLength(interp, objv[i], &list_length) != TCL_OK) {
            return TCL_ERROR;
        }

        if (list_length == 2) {
            if (Tcl_ListObjIndex(interp, objv[i], 1, &type) != TCL_OK) {
                return TCL_ERROR;
            }

            if (Tcl_GetIndexFromObj(interp, type, types, "type", 0,
                    &tableIndex) != TCL_OK) {
                return TCL_ERROR;
            }
        } else if (list_length == 1) {
            tableIndex = TYPE_STRING;
        } else {
            Tcl_SetObjResult(interp, Tcl_NewStringObj(ERROR_ARG_LENGTH, -1));
            return TCL_ERROR;
        }

        switch ((enum types) tableIndex) {
            case TYPE_BOOLEAN:
                if (Tcl_GetIntFromObj(interp, value, &int_value) != TCL_OK) {
                    return TCL_ERROR;
                }
                duk_push_boolean(ctx, int_value);
                break;
            case TYPE_NAN:
                duk_push_nan(ctx);
                break;
            case TYPE_NULL:
                duk_push_null(ctx);
                break;
            case TYPE_NUMBER:
                if (Tcl_GetDoubleFromObj(interp, value,
                        &double_value) != TCL_OK) {
                    return TCL_ERROR;
                }
                duk_push_number(ctx, double_value);
                break;
            case TYPE_UNDEFINED:
                duk_push_undefined(ctx);
                break;
            case TYPE_STRING:
            default:
                duk_push_string(ctx, Tcl_GetString(value));
                break;
        }
    }
    duk_result = duk_pcall_method(ctx, objc - 4);

    Tcl_SetObjResult(interp, Tcl_NewStringObj(duk_safe_to_string(ctx, -1), -1));
    duk_pop(ctx);

    if (duk_result == 0) {
        return TCL_OK;
    } else {
        return TCL_ERROR;
    }
}
//void duk_push_nan(duk_context *ctx);
void aperl_duk_push_nan(duk_context *ctx) {
	duk_push_nan(ctx);
}