示例#1
0
void test_object()
{
	v8pp::context context;
	v8::Isolate* isolate = context.isolate();

	v8::HandleScope scope(isolate);

	v8::Local<v8::Object> obj = v8::Object::New(isolate);

	v8pp::set_option(isolate, obj, "a", 10);
	v8pp::set_option(isolate, obj, "b", true);
	v8pp::set_option(isolate, obj, "sub", v8::Object::New(isolate));
	v8pp::set_option(isolate, obj, "sub.x", "zzz");
	v8pp::set_const(isolate, obj, "pi", 3.1415926);

	int a;
	check("get obj.a", v8pp::get_option(isolate, obj, "a", a));
	check_eq("obj.a", a, 10);

	bool b;
	check("get obj.b", v8pp::get_option(isolate, obj, "b", b));
	check_eq("obj.b", b, true);

	std::string x;
	check("get obj.sub.x", v8pp::get_option(isolate, obj, "sub.x", x));
	check_eq("obj.sub.x", x, "zzz");

	double pi;
	check("get obj.pi", v8pp::get_option(isolate, obj, "pi", pi));
	check("obj.pi", abs(pi - 3.1415926) < 10e-6);
}
示例#2
0
void test_call_v8()
{
	v8pp::context context;

	v8::Isolate* isolate = context.isolate();
	v8::HandleScope scope(isolate);
	v8::Handle<v8::Function> fun = v8::Function::New(isolate, v8_arg_count);

	check_eq("no args", v8pp::call_v8(isolate, fun, fun)->Int32Value(), 0);
	check_eq("1 arg", v8pp::call_v8(isolate, fun, fun, 1)->Int32Value(), 1);
	check_eq("2 args", v8pp::call_v8(isolate, fun, fun, true, 2.2)->Int32Value(), 2);
	check_eq("3 args", v8pp::call_v8(isolate, fun, fun, 1, true, "abc")->Int32Value(), 3);
}
示例#3
0
void test_function()
{
	v8pp::context context;
	v8::Isolate* isolate = context.isolate();
	v8::HandleScope scope(isolate);

	context.set("f", v8pp::wrap_function(isolate, "f", &f));
	check_eq("f", run_script<int>(context, "f(1)"), 1);

	context.set("g", v8pp::wrap_function(isolate, "g", &g));
	check_eq("g", run_script<std::string>(context, "g('abc')"), "abc");

	context.set("h", v8pp::wrap_function(isolate, "h", &h));
	check_eq("h", run_script<int>(context, "h(1, 2)"), 3);
}
示例#4
0
void test_context()
{
	v8pp::context context;

	v8::HandleScope scope(context.isolate());
	int const r = context.run_script("42")->Int32Value();
	check_eq("run_script", r, 42);
}
示例#5
0
文件: test_json.cpp 项目: pmed/v8pp
void test_json()
{
	v8pp::context context;
	v8::Isolate* isolate = context.isolate();
	v8::HandleScope scope(isolate);

	v8::Local<v8::Value> v;
	std::string str;

	str = v8pp::json_str(isolate, v);
	v = v8pp::json_parse(isolate, str);
	check("empty string", str.empty());
	check("empty parse", v.IsEmpty());

	v = v8::Integer::New(isolate, 42);

	str = v8pp::json_str(isolate, v);
	v = v8pp::json_parse(isolate, "42");
	check_eq("int string", str, "42");
	check_eq("int parse", v->Int32Value(isolate->GetCurrentContext()).FromJust(), 42);

	v8::Local<v8::Object> obj = v8::Object::New(isolate);
	v8pp::set_option(isolate, obj, "x", 1);
	v8pp::set_option(isolate, obj, "y", 2.2);
	v8pp::set_option(isolate, obj, "z", "abc");

	str = v8pp::json_str(isolate, obj);
	v = v8pp::json_parse(isolate, str);
	check_eq("object string", str, R"({"x":1,"y":2.2,"z":"abc"})");
	check_eq("object parse", v8pp::json_str(isolate, v), str);

	v = v8pp::json_object(isolate, v.As<v8::Object>());
	check_eq("json object", v8pp::json_str(isolate, v), str);

	v8::Local<v8::Array> arr = v8::Array::New(isolate, 1);
	arr->Set(isolate->GetCurrentContext(), 0, obj).FromJust();

	str = v8pp::json_str(isolate, arr);
	v = v8pp::json_parse(isolate, str);
	check_eq("array string", str, R"([{"x":1,"y":2.2,"z":"abc"}])");
	check_eq("array parse", v8pp::json_str(isolate, v), str);

	v = v8pp::json_parse(isolate, "blah-blah");
	check("parse error", v->IsNativeError());
}
示例#6
0
文件: test_object.cpp 项目: pmed/v8pp
void test_object()
{
	v8pp::context context;
	v8::Isolate* isolate = context.isolate();

	v8::HandleScope scope(isolate);

	v8::Local<v8::Object> obj = v8::Object::New(isolate);

	v8pp::set_option(isolate, obj, "a", 10);
	v8pp::set_option(isolate, obj, "b", true);
	v8pp::set_option(isolate, obj, "sub", v8::Object::New(isolate));
	v8pp::set_option(isolate, obj, "sub.x", "zzz");
	v8pp::set_const(isolate, obj, "pi", 3.1415926);

	int a;
	check("get obj.a", v8pp::get_option(isolate, obj, "a", a));
	check_eq("obj.a", a, 10);

	bool b;
	check("get obj.b", v8pp::get_option(isolate, obj, "b", b));
	check_eq("obj.b", b, true);

	std::string x;
	check("get obj.sub.x", v8pp::get_option(isolate, obj, "sub.x", x));
	check_eq("obj.sub.x", x, "zzz");

	double pi;
	check("get obj.pi", v8pp::get_option(isolate, obj, "pi", pi));
	check("obj.pi", fabs(pi - 3.1415926) < 10e-6);

	v8::Local<v8::Object> sub;
	check("get obj.sub", v8pp::get_option(isolate, obj, "sub", sub)
		&& sub->IsObject());

	context.run_script("test = { test : function() { return 1; } }");
	obj = isolate->GetCurrentContext()->Global();
	v8::Local<v8::Function> fun;
	check("test.test", v8pp::get_option(isolate, obj, "test.test", fun)
		&& fun->IsFunction());
}
示例#7
0
void test_module()
{
	v8pp::context context;

	v8::HandleScope scope(context.isolate());

	v8pp::module module(context.isolate());
	v8pp::module consts(context.isolate());

	consts
		.set_const("bool", true)
		.set_const("char", 'Z')
		.set_const("int", 100)
		.set_const("str", "str")
		.set_const("num", 99.9)
		;

	module
		.set("consts", consts)
		.set("var", var)
		.set("fun", &fun)
		.set("empty", v8::Null(context.isolate()))
		.set("rprop", v8pp::property(get_x))
		.set("wprop", v8pp::property(get_x, set_x))
		;
	context.set("module", module);

	check_eq("module.consts.bool", run_script<bool>(context, "module.consts.bool"), true);
	check_eq("module.consts.char", run_script<char>(context, "module.consts.char"), 'Z');
	check_eq("module.consts.int", run_script<char>(context, "module.consts.int"), 100);
	check_eq("module.consts.str", run_script<std::string>(context, "module.consts.str"), "str");

	check_eq("module.var", run_script<std::string>(context, "module.var = 'test'; module.var"), "test");
	check_eq("var", var, "test");

	check_eq("module.fun", run_script<int>(context, "module.fun(100)"), 101);

	check_eq("module.rprop", run_script<int>(context, "module.rprop"), 2);
	check_eq("module.wrop", run_script<int>(context, "++module.wprop"), 3);
	check_eq("x", x, 2);
}
示例#8
0
void test_class()
{
	v8pp::context context;
	v8::Isolate* isolate = context.isolate();
	v8::HandleScope scope(isolate);

	v8pp::class_<X> X_class(isolate);
	X_class
		.ctor()
		.set_const("konst", 99)
		.set("var", &X::var)
		.set("rprop", v8pp::property(&X::get))
		.set("wprop", v8pp::property(&X::get, &X::set))
		.set("fun", &X::fun)
		.set("static_fun", &X::static_fun)
	;

	v8pp::class_<Y> Y_class(context.isolate());
	Y_class
		.inherit<X>()
		.ctor<int>()
		;

	context
		.set("X", X_class)
		.set("Y", Y_class);

	check_eq("X object", run_script<int>(context, "x = new X(); x.konst + x.var"), 100);
	check_eq("X::rprop", run_script<int>(context, "x = new X(); x.rprop"), 1);
	check_eq("X::wprop", run_script<int>(context, "x = new X(); ++x.wprop"), 2);
	check_eq("X::fun(1)", run_script<int>(context, "x = new X(); x.fun(1)"), 2);
	check_eq("X::static_fun(1)", run_script<int>(context, "X.static_fun(3)"), 3);

	check_eq("Y object", run_script<int>(context, "y = new Y(-100); y.konst + y.var"), -1);
}
示例#9
0
void test_class()
{
	v8pp::context context;
	v8::Isolate* isolate = context.isolate();
	v8::HandleScope scope(isolate);

	v8pp::class_<X> X_class(isolate);
	X_class
		.ctor()
		.set_const("konst", 99)
		.set("var", &X::var)
		.set("rprop", v8pp::property(&X::get))
		.set("wprop", v8pp::property(&X::get, &X::set))
		.set("fun", &X::fun)
		.set("static_fun", &X::static_fun)
	;

	v8pp::class_<Y> Y_class(context.isolate());
	Y_class
		.inherit<X>()
		.ctor<int>()
		;

	context
		.set("X", X_class)
		.set("Y", Y_class)
		.set("runGC", v8pp::wrap_function(isolate, "", &run_GC));

	check_eq("X object", run_script<int>(context, "x = new X(); x.konst + x.var"), 100);
	check_eq("X::rprop", run_script<int>(context, "x = new X(); x.rprop"), 1);
	check_eq("X::wprop", run_script<int>(context, "x = new X(); ++x.wprop"), 2);
	check_eq("X::fun(1)", run_script<int>(context, "x = new X(); x.fun(1)"), 2);
	check_eq("X::static_fun(1)", run_script<int>(context, "X.static_fun(3)"), 3);

	check_eq("Y object", run_script<int>(context, "y = new Y(-100); y.konst + y.var"), -1);
	v8pp::class_<Y>::reference_external(context.isolate(), new Y(-1));
	run_script<int>(context, "for (i = 0; i < 10; ++i) new Y(i); i");
	check_eq("Y count", Y::instance_count, 10 + 2); // 10, y, and reference_external above
	run_script<int>(context, "y = null; runGC(3); 0");
	check_eq("Y count after GC", Y::instance_count, 1); // 1 reference_external
}
示例#10
0
int main()
{
    slong iter, bits;
    flint_rand_t state;

    flint_printf("properties....");
    fflush(stdout);
    flint_randinit(state);
    for (bits = 5; bits <= 30; bits += 5)
    {

        for (iter = 0; iter < 50; iter++)
        {
            dirichlet_group_t G;
            dirichlet_char_t chi, psi;
            ulong q, iter2;

            q = 2 + n_randint(state, 1 << bits);

            dirichlet_group_init(G, q);
            dirichlet_char_init(chi, G);
            dirichlet_char_init(psi, G);

            /* check number char properties */
            for (iter2 = 0; iter2 < 100; iter2++)
            {
                ulong m, n;
                ulong p1, p2, pairing, cm, cn, q2, q3;
                dirichlet_group_t G2, G3;
                dirichlet_char_t chi2, chi3;

                if (iter2 == 50)
                    dirichlet_group_dlog_precompute(G, 5);

                /* one random character */
                do
                    m = n_randint(state, q);
                while (n_gcd(q, m) > 1);

                dirichlet_char_log(chi, G, m);

                p1 = dirichlet_order_ui(G, m);
                p2 = dirichlet_order_char(G, chi);
                check_eq(p1, p2, q, m, "order m", "order chi");

                p1 = dirichlet_conductor_ui(G, m);
                p2 = dirichlet_conductor_char(G, chi);
                check_eq(p1, p2, q, m, "conductor m", "conductor chi");

                p1 = dirichlet_parity_ui(G, m);
                p2 = dirichlet_parity_char(G, chi);
                check_eq(p1, p2, q, m, "parity m", "parity chi");

                p1 = dirichlet_char_is_real(G, chi);
                p2 = (dirichlet_order_char(G, chi) <= 2);
                check_eq(p1, p2, q, m, "is_real", "(order <= 2)");

                /* check index */
                p1 = dirichlet_index_char(G, chi);
                dirichlet_char_index(psi, G, p1);

                if (!dirichlet_char_eq_deep(G, chi, psi))
                {
                    flint_printf("FAIL: index\n\n");
                    flint_printf("q = %wu\n\n", q);
                    flint_printf("m = %wu\n\n", m);
                    flint_printf("chi = "); dirichlet_char_print(G, chi);
                    flint_printf("\n\nindex(chi) = %wu\n\n", p1);
                    flint_printf("psi(index) = %wu\n\n", psi->n);
                    flint_printf("psi = "); dirichlet_char_print(G, psi);
                    flint_printf("\n\n");
                    abort();
                }

                /* lift to higher modulus */
                q2 = q * (1 + n_randint(state, 100));

                dirichlet_group_init(G2, q2);
                dirichlet_char_init(chi2, G2);
                dirichlet_char_lift(chi2, G2, chi, G);

                p1 = dirichlet_conductor_char(G, chi);
                p2 = dirichlet_conductor_char(G2, chi2);
                check_eq(p1, p2, q, m, "conductor chi", "conductor lift");

                p1 = dirichlet_order_char(G, chi);
                p2 = dirichlet_order_char(G2, chi2);
                check_eq(p1, p2, q, m, "order chi", "order lift");

                /* and lower */

                dirichlet_char_lower(psi, G, chi2, G2);
                if (!dirichlet_char_eq_deep(G, chi, psi))
                {
                    flint_printf("FAIL: lift and lower back\n\n");
                    flint_printf("q = %wu\n\nchi = ", q);
                    dirichlet_char_print(G, chi);
                    flint_printf("\n\nq2 = %wu\n\nchi2 = ", q2);
                    dirichlet_char_print(G2, chi2);
                    flint_printf("\n\nq = %wu\n\npsi = ", q);
                    dirichlet_char_print(G, psi);
                    flint_printf("\n\n");
                    abort();
                }

                q3 = dirichlet_conductor_char(G, chi) * random_divisor(state, G);
                q3 = n_gcd(q, q3);

                dirichlet_group_init(G3, q3);
                dirichlet_char_init(chi3, G3);
                dirichlet_char_lower(chi3, G3, chi2, G2);

                p1 = dirichlet_conductor_char(G, chi);
                p2 = dirichlet_conductor_char(G3, chi3);
                check_eq(p1, p2, q, m, "conductor chi", "conductor lower");

                p1 = dirichlet_order_char(G, chi);
                p2 = dirichlet_order_char(G3, chi3);
                check_eq(p1, p2, q, m, "order chi", "order lower");

                dirichlet_char_clear(chi3);
                dirichlet_group_clear(G3);
                dirichlet_char_clear(chi2);
                dirichlet_group_clear(G2);

                /* another random character */
                do
                    n = n_randint(state, q);
                while (n_gcd(q, n) > 1);

                dirichlet_char_log(psi, G, n);
                pairing = dirichlet_pairing(G, m, n);
                cn = dirichlet_chi(G, chi, n);
                cm = dirichlet_chi(G, psi, m);

                if (pairing != cn || pairing != cm)
                {
                    flint_printf("FAIL: pairing\n\n");
                    flint_printf("q = %wu\n\n", q);
                    flint_printf("m = %wu\n\n", m);
                    flint_printf("n = %wu\n\n", n);
                    flint_printf("chi(m,n) = %wu\n\n", pairing);
                    flint_printf("chi(m)(n) = %wu\n\n", cn);
                    flint_printf("chi(n)(m) = %wu\n\n", cm);
                    abort();
                }

            }

            dirichlet_group_dlog_clear(G);

            dirichlet_char_clear(chi);
            dirichlet_char_clear(psi);
            dirichlet_group_clear(G);
        }
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
示例#11
0
void test_conv(v8::Isolate* isolate, T value)
{
	v8::Local<v8::Value> v8_value = v8pp::to_v8(isolate, value);
	auto const value2 = v8pp::from_v8<T>(isolate, v8_value);
	check_eq(typeid(T).name(), value2, value);
}