Пример #1
0
static duk_ret_t test_basic(duk_context *ctx) {
	void *ptr;
	duk_size_t sz;
	int i;

	duk_set_top(ctx, 0);
	duk_push_fixed_buffer(ctx, 1024);
	duk_push_fixed_buffer(ctx, 0);
	duk_push_dynamic_buffer(ctx, 1024);
	duk_push_dynamic_buffer(ctx, 0);

	for (i = 0; i < 4; i++) {
		sz = (duk_size_t) 0xdeadbeefUL;
		ptr = duk_require_buffer(ctx, i, &sz);
		printf("buffer: ptr-is-NULL=%d, sz=%ld\n",
		       (sz == 0 ? -1 : (ptr == NULL ? 1 : 0)), (long) sz);

		/* NULL pointer */
		sz = (duk_size_t) 0xdeadbeefUL;
		ptr = duk_require_buffer(ctx, i, NULL);
		printf("buffer\n");
	}

	return 0;
}
Пример #2
0
/* Same test, using shortcut functions. */
int test_1b(duk_context *ctx) {
	void *buf;

	duk_set_top(ctx, 0);

	printf("fixed size, 0 bytes (no guarantee whether ptr NULL or non-NULL)\n");
	buf = duk_push_fixed_buffer(ctx, 0);
	rw_test((unsigned char *) buf, 0);

	printf("fixed size, 1024 bytes\n");
	buf = duk_push_fixed_buffer(ctx, 1024);
	printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
	rw_test((unsigned char *) buf, 1024);

	printf("dynamic size, 0 bytes (no guarantee whether ptr NULL or non-NULL)\n");
	buf = duk_push_dynamic_buffer(ctx, 0);
	rw_test((unsigned char *) buf, 0);

	printf("dynamic size, 1024 bytes\n");
	buf = duk_push_dynamic_buffer(ctx, 1024);
	printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
	rw_test((unsigned char *) buf, 1024);

	printf("final top: %d\n", duk_get_top(ctx));
	return 0;
}
Пример #3
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, "");
	duk_push_string(ctx, "foo");
	duk_push_int(ctx, 123);
	duk_push_object(ctx);
	duk_push_fixed_buffer(ctx, 0);
	duk_push_fixed_buffer(ctx, 1024);
	duk_push_dynamic_buffer(ctx, 0);
	duk_push_dynamic_buffer(ctx, 2048);

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

		len = (duk_size_t) 0xdeadbeef;
		buf = duk_get_buffer(ctx, i, &len);
		if (len == 0) {
			/* avoid printing 'buf' if len is zero, as it is not predictable */
			printf("index %ld: length 0\n", (long) i);
		} else {
			printf("index %ld: length %lu, ptr-is-NULL %d\n",
			       (long) i, (unsigned long) len, (buf == NULL ? 1 : 0));
		}
	}
}
static duk_ret_t test_1(duk_context *ctx) {
	void *ptr;
	duk_size_t sz;
	int i;

	duk_set_top(ctx, 0);
	duk_push_fixed_buffer(ctx, 1024);
	duk_push_fixed_buffer(ctx, 0);
	duk_push_dynamic_buffer(ctx, 1024);
	duk_push_dynamic_buffer(ctx, 0);
	duk_eval_string(ctx, "(function () { return new Uint32Array(16).subarray(3, 6); })()");

	for (i = 0; i < 5; i++) {
		sz = (duk_size_t) 0xdeadbeefUL;
		ptr = duk_require_buffer_data(ctx, i, &sz);
		printf("buffer: ptr-is-NULL=%d, sz=%ld\n",
		       (sz == 0 ? -1 : (ptr == NULL ? 1 : 0)), (long) sz);

		/* NULL pointer */
		sz = (duk_size_t) 0xdeadbeefUL;
		ptr = duk_require_buffer_data(ctx, i, NULL);
		printf("buffer\n");
	}

	return 0;
}
Пример #5
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;
}
Пример #6
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;
}
Пример #7
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;
}
Пример #8
0
/*
 * Serializes various data types into a buffer. Leaves the buffer on the top of the stack.
 */
static uint8_t* SerializeToBuffer(duk_context* ctx, duk_idx_t idx, duk_size_t* sz)
{
    uint8_t* ptr;

    switch (duk_get_type(ctx, idx)) {
    case DUK_TYPE_BUFFER:
        duk_dup(ctx, idx);
        ptr = duk_get_buffer(ctx, -1, sz);
        break;

    case DUK_TYPE_BOOLEAN:
        ptr = duk_push_fixed_buffer(ctx, 1);
        ptr[0] = duk_get_boolean(ctx, idx);
        *sz = 1;
        break;

    case DUK_TYPE_NUMBER:
        ptr = duk_push_fixed_buffer(ctx, 1);
        ptr[0] = duk_get_int(ctx, idx);
        *sz = 1;
        break;

    case DUK_TYPE_STRING:
        duk_dup(ctx, idx);
        ptr = duk_to_fixed_buffer(ctx, -1, sz);
        break;

    case DUK_TYPE_OBJECT:
        if (duk_is_array(ctx, idx)) {
            duk_idx_t i;
            duk_idx_t len = duk_get_length(ctx, idx);
            ptr = duk_push_fixed_buffer(ctx, len);
            for (i = 0; i < len; ++i) {
                duk_get_prop_index(ctx, idx, i);
                ptr[i] = duk_require_uint(ctx, -1);
                duk_pop(ctx);
            }
            *sz = len;
        } else {
            duk_error(ctx, DUK_ERR_TYPE_ERROR, "Can only serialize arrays of numbers");
        }
        break;

    default:
        duk_error(ctx, DUK_ERR_TYPE_ERROR, "Cannot serialize");
        break;
    }
    return ptr;
}
Пример #9
0
int test_2d(duk_context *ctx) {
	duk_set_top(ctx, 0);
	duk_push_fixed_buffer(ctx, 1024);
	duk_to_object(ctx, 0);
	printf("index 0 OK\n");
	return 0;
}
Пример #10
0
    static Box * PrivatePush( duk_context * ctx, const size_t class_index, const size_t object_size, finalizer_t finalizer )
    {
        duk_push_global_object( ctx );
        duk_get_prop_string( ctx, -1, "Proxy" );
        duk_remove( ctx, -2 );

        duk_push_object( ctx );

        size_t require_size = sizeof( Box ) + object_size;

        Box * box = reinterpret_cast<Box*>( duk_push_fixed_buffer( ctx, require_size ) );
        box->ClassIndex = class_index;
        box->Finalizer = finalizer;

        duk_put_prop_string( ctx, -2, "\xFF" "Box" );

        duk_push_c_function( ctx, &internal::ClassFinalizer, 1 );
        duk_set_finalizer( ctx, -2 );

        duk_push_heap_stash( ctx );
        duk_get_prop_string( ctx, -1, "InstanceHandler" );
        duk_remove( ctx, -2 );
        duk_new( ctx, 2 );

        return box;
    }
/* source: fixed buffer, target: dynamic buffer */
static duk_ret_t test_1b(duk_context *ctx) {
	unsigned char *p;
	void *q, *r;
	duk_size_t sz;

	duk_set_top(ctx, 0);

	p = (unsigned char *) duk_push_fixed_buffer(ctx, 16);
	p[0] = 1;
	p[15] = 2;
	dump_buffer(ctx);

	sz = (duk_size_t) 1234;
	q = duk_to_dynamic_buffer(ctx, -1, &sz);
	printf("q is NULL: %d\n", (q == NULL ? 1 : 0));
	printf("p == q: %d\n", (p == q ? 1 : 0));
	printf("sz=%lu\n", (unsigned long) sz);
	dump_buffer(ctx);

	/* second time should be a no-op */
	r = duk_to_dynamic_buffer(ctx, -1, NULL);
	printf("r is NULL: %d\n", (q == NULL ? 1 : 0));
	printf("q == r: %d\n", (q == r ? 1 : 0));
	dump_buffer(ctx);

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

	/* Test first with nothing on stack index -3. */
	duk_safe_call(ctx, test__null, NULL, 0, 1); printf("%s\n", duk_safe_to_string(ctx, 0));
	duk_pop(ctx);

	duk_set_top(ctx, 0); duk_push_undefined(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_null(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_true(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_false(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_number(ctx, 123.0); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_string(ctx, "foo\x00" "bar"); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_fixed_buffer(ctx, 16); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_pointer(ctx, NULL); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_pointer(ctx, (void *) 0xdeadbeefUL); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_object(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_array(ctx); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_c_function(ctx, dummy_func, 0); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_c_lightfunc(ctx, dummy_func, 0, 0, 0); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_eval_string(ctx, "(function dummy(){})"); test__require_calls(ctx);
	duk_set_top(ctx, 0); duk_push_thread(ctx); test__require_calls(ctx);

	printf("done\n");
	return 0;
}
Пример #13
0
/* It's not an error for the underlying plain buffer to be too small to
 * cover the slice.  This is allowed because it may happen for dynamic
 * and external buffers at run time anyway.  In any case, no memory
 * unsafe behavior happens.
 */
static duk_ret_t test_uncovered(duk_context *ctx, void *udata) {
	(void) udata;

	duk_push_fixed_buffer(ctx, 256);
	duk_push_buffer_object(ctx, -1, 7, 512, DUK_BUFOBJ_UINT32ARRAY);

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

	duk_eval_string(ctx,
		"(function (v) {\n"
		"    for (var i = 0; i < v.length; i++) { v[i] = 123; }\n"
		"    for (var i = 0; i < v.length; i++) { var ignore = v[i]; }\n"
		"})");
	duk_dup(ctx, -2);
	duk_call(ctx, 1);
	duk_pop(ctx);

	duk_pop_n(ctx, 2);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #14
0
static duk_ret_t nsp_texture_constructor(duk_context *ctx) {
    int width = duk_require_int(ctx, 0);
    int height = duk_require_int(ctx, 1);
    if (width < 1 || height < 1) {
	duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "Width and height must be positive");
	duk_throw(ctx);
    }
    bool has_transparency;
    uint16_t transparent_color;
    if ((has_transparency = duk_is_number(ctx, 2))) {
	transparent_color = (uint16_t)duk_get_int(ctx, 2);
    }
    duk_push_this(ctx);
    duk_push_fixed_buffer(ctx, width * height * 2);
    duk_put_prop_string(ctx, -2, "bitmap");
    duk_push_int(ctx, width);
    duk_put_prop_string(ctx, -2, "width");
    duk_push_int(ctx, height);
    duk_put_prop_string(ctx, -2, "height");
    if (has_transparency) {
	duk_push_int(ctx, transparent_color);
    } else {
	duk_push_null(ctx);
    }
    duk_put_prop_string(ctx, -2, "transparentColor");
    return 0;
}
Пример #15
0
static int NativeSpiRead(duk_context* ctx)
{
    int size = duk_require_int(ctx, -1);
    void* ptr = duk_push_fixed_buffer(ctx, size);
    AJS_TargetIO_SpiRead(PinCtxPtr(ctx), size, ptr);
    return 1;
}
Пример #16
0
void test(duk_context *ctx) {
	int i, n;

	duk_push_c_function(ctx, func, 0);

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_true(ctx);
	duk_push_false(ctx);
	duk_push_number(ctx, 123.456);
	duk_push_string(ctx, "foo");
	duk_push_object(ctx);
	duk_push_array(ctx);
	duk_push_fixed_buffer(ctx, 16);
	duk_push_pointer(ctx, (void *) 0xdeadbeef);

	n = duk_get_top(ctx);
	printf("top: %d\n", n);
	for (i = 1; i < n; i++) {
		duk_dup(ctx, 0);
		duk_dup(ctx, i);
		duk_call_method(ctx, 0);  /* [ ... func this ] -> [ ret ] */
		duk_pop(ctx);
	}
}
Пример #17
0
/*
 * Called with a service object on the top of the stack. Returns with a message object on top of
 * the stack replacing the service object.
 */
static void MessageSetup(duk_context* ctx, const char* iface, const char* member, const char* path, uint8_t msgType)
{
    AJ_Status status;
    AJ_MsgHeader hdr;
    AJ_Message msg;
    AJS_MsgInfo* msgInfo;
    const char* dest;
    uint8_t secure;
    size_t dlen;
    duk_idx_t objIdx = duk_push_object(ctx);

    /*
     * Get the destination from the service object
     */
    duk_get_prop_string(ctx, -2, "dest");
    dest = duk_get_lstring(ctx, -1, &dlen);
    duk_pop(ctx);
    /*
     * If this is not a broadcast message make sure the destination peer is still connected
     */
    if (dlen) {
        CheckPeerIsAlive(ctx, dest);
    }
    /*
     * Initialize a message struct so we can lookup the message id. We do this now because it is
     * alot more efficient if the method object we are creating is used multiple times.
     */
    memset(&msg, 0, sizeof(AJ_Message));
    memset(&hdr, 0, sizeof(AJ_MsgHeader));
    msg.hdr = &hdr;
    msg.signature = "*";
    msg.member = member;
    msg.iface = iface;
    msg.objPath = path ? path : AJS_GetStringProp(ctx, -2, "path");
    /*
     * This allows us to use one object table entry for all messages
     */
    AJS_SetObjectPath(msg.objPath);
    hdr.msgType = msgType;
    status = AJ_LookupMessageId(&msg, &secure);
    if (status != AJ_OK) {
        duk_error(ctx, DUK_ERR_REFERENCE_ERROR, "Unknown %s %s", path ? "SIGNAL" : "METHOD", member);
    }
    /*
     * Buffer to caching message information stored in the "info" property on the method object
     */
    msgInfo = duk_push_fixed_buffer(ctx, sizeof(AJS_MsgInfo) + dlen + 1);
    msgInfo->secure = secure;
    msgInfo->session = AJS_GetIntProp(ctx, -2, "session");
    msgInfo->msgId = msg.msgId;
    memcpy(msgInfo->dest, dest, dlen);
    msgInfo->dest[dlen] = 0;
    duk_put_prop_string(ctx, objIdx, "info");

    AJ_ASSERT(duk_get_top_index(ctx) == objIdx);
    /*
     * Remove sessions object and leave the message object on the top of the stack
     */
    duk_remove(ctx, -2);
}
Пример #18
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;
}
Пример #19
0
/* Boundary case. */
static duk_ret_t test_invalid_flags3(duk_context *ctx, void *udata) {
	(void) udata;

	duk_push_fixed_buffer(ctx, 256);
	duk_push_buffer_object(ctx, -1, 7, 512, (duk_uint_t) (DUK_BUFOBJ_FLOAT64ARRAY + 1) /* ERROR: bogus type, right after last defined */);
	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #20
0
/* If 'flags' is given as zero, it will match a DUK_BUFOBJ_DUKTAPEBUFFER.
 * So this test succeeds which is intentional.
 */
static duk_ret_t test_invalid_flags1(duk_context *ctx, void *udata) {
	(void) udata;

	duk_push_fixed_buffer(ctx, 256);
	duk_push_buffer_object(ctx, -1, 7, 512, 0 /* no type given, but matches DUK_BUFOBJ_DUKTAPEBUFFER */);
	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #21
0
static duk_ret_t test_invalid_index2(duk_context *ctx, void *udata) {
	(void) udata;

	duk_push_fixed_buffer(ctx, 256);
	duk_push_buffer_object(ctx, DUK_INVALID_INDEX, 7, 512, DUK_BUFOBJ_UINT32ARRAY);
	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #22
0
static duk_ret_t test_invalid_flags2(duk_context *ctx, void *udata) {
	(void) udata;

	duk_push_fixed_buffer(ctx, 256);
	duk_push_buffer_object(ctx, -1, 7, 512, (duk_uint_t) 0xdeadbeef /* ERROR: bogus type */);
	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #23
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;
}
Пример #24
0
static int Deserializer_Read(duk_context* ctx)
{
    duk_int_t magic = duk_get_current_magic(ctx);

    duk_push_this(ctx);

    // safe cast based on type check above
    Deserializer* deserial = CastToDeserializer(ctx, duk_get_top_index(ctx));

    duk_pop(ctx);

    if (!deserial)
    {
        duk_push_boolean(ctx, 0);
        return 1;
    }

    char* data;
    String str;
    size_t length;

    IO_MAGIC_TYPE v = (IO_MAGIC_TYPE) magic;

    bool success = false;

    switch(v)
    {
        case IO_MAGIC_INT:
            duk_push_number(ctx, (double) deserial->ReadInt());
            return 1;
        case IO_MAGIC_STRING:
            length = deserial->GetSize() - deserial->GetPosition();
            str.Resize(length + 1);
            deserial->Read(&str[0], length);
            str[length] = '\0';
            duk_push_string(ctx, str.CString());
            return 1;
        case IO_MAGIC_ZEROSTRING:
            success = duk_push_string(ctx, deserial->ReadString().CString());
            return 1;
        case IO_MAGIC_BINARY:
            length = deserial->GetSize() - deserial->GetPosition();
            duk_push_fixed_buffer(ctx, length);
            duk_push_buffer_object(ctx, -1, 0, length, DUK_BUFOBJ_UINT8ARRAY);
            duk_replace(ctx, -2);
            data = (char*) duk_require_buffer_data(ctx, 0, &length);
            success = deserial->Read(data, length);
            return 1;
        default:
            break;
    }

    duk_push_undefined(ctx);

    return 1;

}
Пример #25
0
static void pushBuffer(const ut8 *buf, int len) {
	int i;
	duk_push_fixed_buffer (ctx, len);
	for (i=0; i<len; i++) {
		duk_push_number (ctx, buf[i]);
		duk_put_prop_index (ctx, -2, i);
	}
	// buffer is in stack[-1]
}
Пример #26
0
static duk_ret_t test_basic(duk_context *ctx, void *udata) {
	duk_idx_t i, n;
	duk_int_t rc;

	(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_int(ctx, 123);
	duk_push_object(ctx);
	duk_push_fixed_buffer(ctx, 0);
	duk_push_fixed_buffer(ctx, 1024);
	duk_push_dynamic_buffer(ctx, 0);
	duk_push_dynamic_buffer(ctx, 2048);
	duk_eval_string(ctx, "(function () { return new ArrayBuffer(16); })()");
	duk_eval_string(ctx, "(function () { return new Uint32Array(16); })()");
	duk_eval_string(ctx, "(function () { return new DataView(new ArrayBuffer(16)); })()");
	duk_eval_string(ctx, "(function () { return new Uint32Array(16).subarray(3, 6); })()");
	duk_eval_string(ctx, "(function () { return new Buffer('ABCDEFGH'); })()");
	duk_eval_string(ctx, "(function () { return new Buffer('ABCDEFGH').slice(3, 6); })()");

	n = duk_get_top(ctx);
	printf("top: %ld\n", (long) n);
	for (i = 0; i <= n; i++) {
		rc = duk_safe_call(ctx, safe_helper1, (void *) i, 0, 1);
		if (rc != DUK_EXEC_SUCCESS) {
			printf("index %ld: %s\n", (long) i, duk_safe_to_string(ctx, -1));
		}
		duk_pop(ctx);

		rc = duk_safe_call(ctx, safe_helper2, (void *) i, 0, 1);
		if (rc != DUK_EXEC_SUCCESS) {
			printf("index %ld: %s\n", (long) i, duk_safe_to_string(ctx, -1));
		}
		duk_pop(ctx);
	}

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #27
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;
}
Пример #28
0
static duk_ret_t test_basic(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, "");
	duk_push_string(ctx, "foo");
	duk_push_int(ctx, 123);
	duk_push_object(ctx);
	duk_push_fixed_buffer(ctx, 0);
	duk_push_fixed_buffer(ctx, 1024);
	duk_push_dynamic_buffer(ctx, 0);
	duk_push_dynamic_buffer(ctx, 2048);
	duk_eval_string(ctx, "(function () { return new ArrayBuffer(16); })()");
	duk_eval_string(ctx, "(function () { return new Uint32Array(16); })()");
	duk_eval_string(ctx, "(function () { return new DataView(new ArrayBuffer(16)); })()");
	duk_eval_string(ctx, "(function () { return new Uint32Array(16).subarray(3, 6); })()");
	duk_eval_string(ctx, "(function () { return new Buffer('ABCDEFGH'); })()");
	duk_eval_string(ctx, "(function () { return new Buffer('ABCDEFGH').slice(3, 6); })()");

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

		len = (duk_size_t) 0xdeadbeefUL;
		buf = duk_get_buffer_data(ctx, i, &len);
		if (len == 0) {
			/* avoid printing 'buf' if len is zero, as it is not predictable */
			printf("index %ld: length 0\n", (long) i);
		} else {
			printf("index %ld: length %lu, ptr-is-NULL %d\n",
			       (long) i, (unsigned long) len, (buf == NULL ? 1 : 0));
		}
	}

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Пример #29
0
duk_idx_t AJS_UnmarshalMessage(duk_context* ctx, AJ_Message* msg, uint8_t accessor)
{
    duk_idx_t objIndex = duk_push_object(ctx);

    duk_push_string(ctx, msg->sender);
    duk_put_prop_string(ctx, objIndex, "sender");

    if ((msg->hdr->msgType == AJ_MSG_METHOD_CALL) || (msg->hdr->msgType == AJ_MSG_SIGNAL)) {
        duk_push_string(ctx, msg->member);
        duk_put_prop_string(ctx, objIndex, "member");
        duk_push_string(ctx, msg->iface);
        duk_put_prop_string(ctx, objIndex, "iface");
        duk_push_string(ctx, msg->objPath);
        duk_put_prop_string(ctx, objIndex, "path");
        /* If sender == unique name then true, otherwise false */
        duk_push_boolean(ctx, !abs(strcmp(msg->sender, AJS_GetBusAttachment()->uniqueName)));
        duk_put_prop_string(ctx, objIndex, "fromSelf");

        if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) {
            AJS_ReplyInternal* msgReply;
            /*
             * Private read-only information needed for composing the reply
             */
            duk_push_string(ctx, AJS_HIDDEN_PROP("reply"));
            msgReply = duk_push_fixed_buffer(ctx, sizeof(AJS_ReplyInternal));
            msgReply->msgId = msg->msgId;
            msgReply->flags = msg->hdr->flags;
            msgReply->serialNum = msg->hdr->serialNum;
            msgReply->sessionId = msg->sessionId;
            msgReply->accessor = accessor;
            duk_def_prop(ctx, objIndex, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
            /*
             * Register the reply functions
             */
            duk_push_c_lightfunc(ctx, AJS_MethodCallReply, DUK_VARARGS, 0, 0);
            duk_put_prop_string(ctx, objIndex, "reply");
            duk_push_c_lightfunc(ctx, AJS_MethodCallError, DUK_VARARGS, 0, 0);
            duk_put_prop_string(ctx, objIndex, "errorReply");
        }
    } else {
        int isError = (msg->hdr->msgType == AJ_MSG_ERROR);

        AJ_InfoPrintf(("Reply serial %d\n", msg->replySerial));
        duk_push_int(ctx, msg->replySerial);
        duk_put_prop_string(ctx, objIndex, "replySerial");
        if (isError) {
            duk_push_string(ctx, msg->error);
            duk_put_prop_string(ctx, objIndex, "error");
        }
        duk_push_boolean(ctx, isError);
        duk_put_prop_string(ctx, objIndex, "isErrorReply");
    }
    return objIndex;
}
Пример #30
0
void test(duk_context *ctx) {
	duk_idx_t i, n;

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_boolean(ctx, 0);
	duk_push_boolean(ctx, 123);
	duk_push_number(ctx, 234);
	duk_push_string(ctx, "foo");
	duk_push_object(ctx);
	duk_push_array(ctx);
	duk_push_c_function(ctx, my_c_func, DUK_VARARGS);
	duk_push_fixed_buffer(ctx, 1024);
	duk_push_dynamic_buffer(ctx, 1024);
	duk_push_pointer(ctx, (void *) 0xdeadbeefUL);

	n = duk_get_top(ctx);
	for (i = 0; i < n + 1; i++) {  /* end on invalid index on purpose */
		duk_int_t typeval, typemask;

		typeval = duk_get_type(ctx, i);
		typemask = duk_get_type_mask(ctx, i);

		printf("stack[%ld] --> type=%ld mask=0x%08lx ",
		       (long) i, (long) typeval, (long) typemask);

		switch(duk_get_type(ctx, i)) {
		case DUK_TYPE_NONE:       printf("none"); break;
		case DUK_TYPE_UNDEFINED:  printf("undefined"); break;
		case DUK_TYPE_NULL:       printf("null"); break;
		case DUK_TYPE_BOOLEAN:    printf("boolean"); break;
		case DUK_TYPE_NUMBER:     printf("number"); break;
		case DUK_TYPE_STRING:     printf("string"); break;
		case DUK_TYPE_OBJECT:     printf("object"); break;
		case DUK_TYPE_BUFFER:     printf("buffer"); break;
		case DUK_TYPE_POINTER:    printf("pointer"); break;
		default:                  printf("unknown(%d)", (int) duk_get_type(ctx, i)); break;
		}

		printf(" bool=%d num=%lf str=%s buf-is-null=%d ptr=%p",
		       (int) duk_get_boolean(ctx, i),
		       (double) duk_get_number(ctx, i),
		       duk_get_string(ctx, i),
		       (duk_get_buffer(ctx, i, NULL) == NULL ? 1 : 0),
		       duk_get_pointer(ctx, i));

		printf(" isobj=%d isarr=%d isfunc=%d",
		       (int) duk_is_object(ctx, i),
		       (int) duk_is_array(ctx, i),
		       (int) duk_is_function(ctx, i));

		printf("\n");
	}
}