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;
}
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;
}
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;
}
Exemple #4
0
/* Shared helper to implement Object.getPrototypeOf and the ES6
 * Object.prototype.__proto__ getter.
 *
 * https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-object.prototype.__proto__
 */
duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx) {
	duk_hobject *h;

	/* magic: 0=getter call, 1=Object.getPrototypeOf */
	if (duk_get_current_magic(ctx) == 0) {
		duk_push_this_coercible_to_object(ctx);
		duk_insert(ctx, 0);
	}

	h = duk_require_hobject(ctx, 0);
	DUK_ASSERT(h != NULL);

	/* XXX: should the API call handle this directly, i.e. attempt
	 * to duk_push_hobject(ctx, null) would push a null instead?
	 * (On the other hand 'undefined' would be just as logical, but
	 * not wanted here.)
	 */

	if (h->prototype) {
		duk_push_hobject(ctx, h->prototype);
	} else {
		duk_push_null(ctx);
	}
	return 1;
}
Exemple #5
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);
	}
}
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, "foo");
	duk_push_int(ctx, 123);
	duk_push_object(ctx);
	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++) {
		rc = duk_safe_call(ctx, safe_helper, (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;
}
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));
	}
}
Exemple #8
0
static void ctx_push_ruby_object(struct state *state, VALUE obj)
{
  duk_context *ctx = state->ctx;
  duk_idx_t arr_idx;
  VALUE str;

  switch (TYPE(obj)) {
    case T_FIXNUM:
      duk_push_int(ctx, NUM2INT(obj));
      return;

    case T_FLOAT:
      duk_push_number(ctx, NUM2DBL(obj));
      return;

    case T_SYMBOL:
#ifdef HAVE_RB_SYM2STR
      obj = rb_sym2str(obj);
#else
      obj = rb_id2str(SYM2ID(obj));
#endif
      // Intentional fall-through:

    case T_STRING:
      str = encode_cesu8(state, obj);
      duk_push_lstring(ctx, RSTRING_PTR(str), RSTRING_LEN(str));
      return;

    case T_TRUE:
      duk_push_true(ctx);
      return;

    case T_FALSE:
      duk_push_false(ctx);
      return;

    case T_NIL:
      duk_push_null(ctx);
      return;

    case T_ARRAY:
      arr_idx = duk_push_array(ctx);
      for (int idx = 0; idx < RARRAY_LEN(obj); idx++) {
        ctx_push_ruby_object(state, rb_ary_entry(obj, idx));
        duk_put_prop_index(ctx, arr_idx, idx);
      }
      return;

    case T_HASH:
      duk_push_object(ctx);
      rb_hash_foreach(obj, ctx_push_hash_element, (VALUE)state);
      return;

    default:
      // Cannot convert
      break;
  }

  clean_raise(ctx, rb_eTypeError, "cannot convert %s", rb_obj_classname(obj));
}
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;
}
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));
		}
	}
}
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;
}
Exemple #12
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;
}
Exemple #13
0
void como_push_worker_value (duk_context *ctx, comoQueue *queue){
    switch(queue->type){
        case DUK_TYPE_UNDEFINED :
            duk_push_undefined(ctx);
            break;

        case DUK_TYPE_NULL :
                duk_push_null(ctx);
                break;

        case DUK_TYPE_OBJECT :
            duk_push_string(ctx, queue->data);
            duk_json_decode(ctx, -1);
            break;

        case DUK_TYPE_POINTER :
            duk_push_pointer(ctx, queue->data);
            break;

        case DUK_TYPE_STRING :
            duk_push_string(ctx, queue->data);
            break;
        
        default :
            assert(0 && "Duk Type Error");
    }
}
static duk_ret_t test_nargs_minus1(duk_context *ctx, void *udata) {
	duk_int_t rc;

	(void) udata;

	duk_push_null(ctx);
	duk_push_null(ctx);
	duk_push_null(ctx);
	printf("top before: %ld\n", (long) duk_get_top(ctx));

	rc = duk_safe_call(ctx, dummy, NULL, -1 /*nargs, negative*/, 0 /*nrets*/);
	printf("duk_safe_call rc: %ld\n", (long) rc);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Exemple #15
0
int test_2b(duk_context *ctx) {
	duk_set_top(ctx, 0);
	duk_push_null(ctx);
	duk_to_object(ctx, 0);
	printf("index 0 OK\n");
	return 0;
}
Exemple #16
0
JNIEXPORT jobject JNICALL Java_com_furture_react_DuktapeEngine_nativeCallJs
  (JNIEnv *env, jobject thisObject, jlong ptr, jstring jsTarget, jstring jsMethod, jobjectArray args){
	duk_context *ctx  = convert_to_context(ptr);
	jboolean iscopy = JNI_FALSE;
    const char* targetName = ((*env)->GetStringUTFChars(env, jsTarget, &iscopy));
    DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJs call on %s", targetName);
	duk_push_global_object(ctx);
    if(duk_get_prop_string(ctx, -1, targetName)){
    	    const char* methodName = ((*env)->GetStringUTFChars(env, jsMethod, &iscopy));
	    duk_push_string(ctx, methodName);
	    jsize length = duk_push_java_object_array(ctx, env, args);
	    DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJs call  Function %d", length);
	    if(duk_pcall_prop(ctx, -2 - length, length) != DUK_EXEC_SUCCESS){
	     	LOGE("ScriptEngine","ScriptEngine CallJS %s.%s() method %s", targetName, methodName, duk_js_error_to_string(ctx, -1));
	        duk_pop(ctx);
	        duk_push_null(ctx);
	    }
 	    (*env)->ReleaseStringUTFChars(env, jsTarget, targetName);
	    (*env)->ReleaseStringUTFChars(env, jsMethod, methodName);
	    jobject  value =  duk_to_java_object(ctx, env, -1);
	    duk_pop_2(ctx);
	    return value;
    }
    (*env)->ReleaseStringUTFChars(env, jsTarget, targetName);
    duk_pop_2(ctx);
    return NULL;

}
Exemple #17
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;
}
Exemple #18
0
void duk_push_java_object(duk_context *ctx, JNIEnv *env, jobject object){
	if(object == NULL){
		duk_push_null(ctx);
		return;
	}
	if((*env)->IsInstanceOf(env, object, js_ref_class)){
		jint  ref = (*env)->CallIntMethod(env, object, js_ref_get_ref_method);
		duk_push_js_ref(ctx, ref);
		return;
	}
	if((*env)->IsInstanceOf(env, object, java_number_class)){
		jdouble num = (*env)->CallDoubleMethod(env, object, java_number_get_double_value_method);
		duk_push_global_object(ctx);
		duk_get_prop_string(ctx, -1, "Number");
		duk_push_number(ctx, num);
		duk_new(ctx, 1);
		duk_mark_jsobject_to_java_object(ctx, -1, env, object);
		duk_remove(ctx, -2);
        return;
	}
	if((*env)->IsInstanceOf(env, object, java_boolean_class)){
		jboolean value = (*env)->CallBooleanMethod(env, object, java_boolean_get_boolean_value_method);
		if(value){
			duk_push_true(ctx);
		}else{
			duk_push_false(ctx);
		}
		return;
	}
	duk_push_object(ctx);  //empty target
	duk_mark_jsobject_to_java_object(ctx, -1, env, object);
}
Exemple #19
0
static int java_object_to_string(duk_context *ctx) {
	int n = duk_get_top(ctx);
	DEBUG_LOG("ScriptEngine",  "java_object_to_string enter %d", n);
	duk_push_this(ctx);
	if(duk_get_prop_string(ctx, -1, JAVA_OBJECT_MARK)){
		DEBUG_LOG("ScriptEngine",  "java_object_to_string enter java object ");
		jobject  ref = duk_to_pointer(ctx, -1);
		if(ref == NULL){
			 duk_push_null(ctx);
			 return 1;
		}
		JNIEnv*  env =  get_java_jni_env();
		DEBUG_LOG("ScriptEngine",  "java_object_to_string enter java end");
		jclass   refClass = (*env)->GetObjectClass(env, ref);
		jmethodID method = (*env)->GetMethodID(env, refClass, "toString", "()Ljava/lang/String;");
		jstring  value =  (*env)->CallObjectMethod(env, ref, method);
		jboolean iscopy = JNI_FALSE;
		const char* src =  ((*env)->GetStringUTFChars(env, value, &iscopy));
		DEBUG_LOG("ScriptEngine",  "java_object_to_string src %s", src );
        duk_push_string(ctx, src);
        (*env)->ReleaseStringUTFChars(env, value, src);
		(*env)->DeleteLocalRef(env, value);
		(*env)->DeleteLocalRef(env, refClass);
		DEBUG_LOG("ScriptEngine",  "java_object_to_string success");
		return 1;
	}else{
		DEBUG_LOG("ScriptEngine",  "java_object_to_string error");
	}
	return 0;
}
Exemple #20
0
/* Shared helper to implement Object.getPrototypeOf and the ES6
 * Object.prototype.__proto__ getter.
 *
 * https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-object.prototype.__proto__
 */
DUK_INTERNAL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx) {
	duk_hobject *h;
	duk_hobject *proto;

	/* magic: 0=getter call, 1=Object.getPrototypeOf */
	if (duk_get_current_magic(ctx) == 0) {
		duk_push_this_coercible_to_object(ctx);
		duk_insert(ctx, 0);
	}

	h = duk_require_hobject_or_lfunc(ctx, 0);
	/* h is NULL for lightfunc */

	/* XXX: should the API call handle this directly, i.e. attempt
	 * to duk_push_hobject(ctx, null) would push a null instead?
	 * (On the other hand 'undefined' would be just as logical, but
	 * not wanted here.)
	 */

	if (h == NULL) {
		duk_push_hobject_bidx(ctx, DUK_BIDX_FUNCTION_PROTOTYPE);
	} else {
		proto = DUK_HOBJECT_GET_PROTOTYPE(h);
		if (proto) {
			duk_push_hobject(ctx, proto);
		} else {
			duk_push_null(ctx);
		}
	}
	return 1;
}
static duk_ret_t test_invalid_null(duk_context *ctx, void *udata) {
	(void) udata;

	duk_push_null(ctx);
	duk_push_buffer_object(ctx, -1, 7, 512, DUK_BUFOBJ_UINT32ARRAY);
	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Exemple #22
0
void duv_push_status(duk_context *ctx, int status) {
  if (status < 0) {
    duk_push_error_object(ctx, DUK_ERR_ERROR, "%s: %s", uv_err_name(status), uv_strerror(status));
  }
  else {
    duk_push_null(ctx);
  }
}
static duk_ret_t test_1(duk_context *ctx, void *udata) {
	(void) udata;

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

	printf("%ld\n", (long) duk_is_primitive(ctx, 0));  /* valid */
	printf("%ld\n", (long) duk_is_primitive(ctx, 1));  /* invalid */

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
void PushWeakObject(duk_context* ctx, Object* object)
{
    if (!object)
    {
        duk_push_null(ctx);
        return;
    }

    duk_push_heap_stash(ctx);

    // Check if the wrapper for the object already exists in stash
    // This is required so that comparisons of object references (e.g. against the me property) work properly
    if (duk_has_prop_index(ctx, -1, (size_t)object))
    {
        duk_get_prop_index(ctx, -1, (size_t)object);
        WeakPtr<Object>* oldPtr = GetWeakPtr(ctx, -1);
        if (oldPtr && oldPtr->Get() == object)
        {
            duk_remove(ctx, -2); // Remove stash
            return;
        }
        else
            duk_pop(ctx); // Valid existing wrapper not found
    }

    duk_push_object(ctx);
    WeakPtr<Object>* ptr = new WeakPtr<Object>(object);
    duk_push_pointer(ctx, ptr);
    duk_put_prop_string(ctx, -2, "\xff""weak");
    duk_push_c_function(ctx, WeakPtr_Finalizer, 1);
    duk_set_finalizer(ctx, -2);

    // Set prototype. If not found, use base class prototype (e.g. IComponent)
    duk_get_global_string(ctx, object->GetTypeName().CString());
    if (!duk_is_object(ctx, -1))
    {
        duk_pop(ctx);
        duk_get_global_string(ctx, object->GetTypeInfo()->GetBaseTypeInfo()->GetTypeName().CString());
    }
    duk_get_prop_string(ctx, -1, "prototype");
    duk_set_prototype(ctx, -3);
    duk_pop(ctx);

    // Proxied property access handling for scene, entity & component
    if (object->GetType() == Scene::GetTypeStatic())
        SetupProxy(ctx, SceneProxyFunctions);
    if (object->GetType() == Entity::GetTypeStatic())
        SetupProxy(ctx, EntityProxyFunctions);
    else if (dynamic_cast<IComponent*>(object))
        SetupProxy(ctx, ComponentProxyFunctions);

    // Store to stash
    duk_dup(ctx, -1);
    duk_put_prop_index(ctx, -3, (size_t)object);
    duk_remove(ctx, -2); // Remove stash
}
static duk_ret_t test_1(duk_context *ctx, void *udata) {
	(void) udata;

	duk_eval_string_noresult(ctx,
		"this.myFinalizer = function myfin(o) {\n"
		"    print('finalizer, rescuing object', JSON.stringify(o));\n"
		"    var global = new Function('return this')();\n"
		"    global.rescued = o;\n"
		"}\n"
	);

	/* First call; object gets finalized when call returns and call
	 * handling does a duk_set_top() call.  The object is rescued and
	 * later reused.
	 */
	duk_push_c_function(ctx, my_func, 3 /*nargs*/);
	duk_push_null(ctx);
	duk_eval_string(ctx,
		"(function () {\n"
		"    var res = { name: 'my object' };\n"
		"    Duktape.fin(res, myFinalizer);\n"
		"    return res;\n"
		"})()\n");
	duk_push_null(ctx);
	printf("calling\n");
	duk_call(ctx, 3);
	printf("returned\n");
	duk_pop(ctx);

	/* Object is reused for another call, and rescued again. */
	duk_push_c_function(ctx, my_func, 3 /*nargs*/);
	duk_push_null(ctx);
	duk_eval_string(ctx, "rescued");
	duk_eval_string_noresult(ctx, "rescued = null;");
	duk_push_null(ctx);
	printf("calling\n");
	duk_call(ctx, 3);
	printf("returned\n");
	duk_pop(ctx);

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

	(void) udata;

	duk_push_null(ctx);
	duk_push_null(ctx);
	duk_push_null(ctx);
	printf("top before: %ld\n", (long) duk_get_top(ctx));

	/* Here 'nrets' is too large, beyond the current value stack
	 * reserve.  (This succeeds in Duktape 2.1 and prior because
	 * the implementation did a reserve check.)
	 */
	rc = duk_safe_call(ctx, dummy, NULL, 2 /*nargs*/, 1024 /*nrets, too large*/);
	printf("duk_safe_call rc: %ld\n", (long) rc);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Exemple #28
0
/* duk_del_prop_string(), not object coercible */
static duk_ret_t test_delpropstring_g(duk_context *ctx) {
	duk_ret_t rc;

	duk_set_top(ctx, 0);
	duk_push_null(ctx);
	rc = duk_del_prop_string(ctx, -1, "foo");
	printf("delete null.foo -> rc=%d\n", (int) rc);

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

	duk_set_top(ctx, 0);
	duk_push_null(ctx);

	p = duk_require_lstring(ctx, 0, &sz);
	printf("string: %s (%d)\n", p, (int) sz);
	return 0;
}
Exemple #30
0
int test_2(duk_context *ctx) {
	void *ptr;
	size_t sz;

	duk_set_top(ctx, 0);
	duk_push_null(ctx);
	sz = (size_t) 0xdeadbeef;
	ptr = duk_require_buffer(ctx, 0, &sz);
	printf("buffer ok: %p\n", ptr);  /* ok to print, should not be reached */
	return 0;
}