📄 test-api.cc
字号:
CHECK(try_catch.HasCaught()); String::AsciiValue exception_value(try_catch.Exception()); CHECK_EQ(*exception_value, "panama!");}bool message_received;static void check_message(v8::Handle<v8::Message> message, v8::Handle<Value> data) { CHECK_EQ(5.76, data->NumberValue()); CHECK_EQ(6.75, message->GetSourceData()->NumberValue()); message_received = true;}THREADED_TEST(MessageHandlerData) { message_received = false; v8::HandleScope scope; CHECK(!message_received); v8::V8::AddMessageListener(check_message, v8_num(5.76)); LocalContext context; v8::ScriptOrigin origin = v8::ScriptOrigin(v8_str("6.75")); Script::Compile(v8_str("throw 'error'"), &origin)->Run(); CHECK(message_received); // clear out the message listener v8::V8::RemoveMessageListeners(check_message);}THREADED_TEST(GetSetProperty) { v8::HandleScope scope; LocalContext context; context->Global()->Set(v8_str("foo"), v8_num(14)); context->Global()->Set(v8_str("12"), v8_num(92)); context->Global()->Set(v8::Integer::New(16), v8_num(32)); context->Global()->Set(v8_num(13), v8_num(56)); Local<Value> foo = Script::Compile(v8_str("this.foo"))->Run(); CHECK_EQ(14, foo->Int32Value()); Local<Value> twelve = Script::Compile(v8_str("this[12]"))->Run(); CHECK_EQ(92, twelve->Int32Value()); Local<Value> sixteen = Script::Compile(v8_str("this[16]"))->Run(); CHECK_EQ(32, sixteen->Int32Value()); Local<Value> thirteen = Script::Compile(v8_str("this[13]"))->Run(); CHECK_EQ(56, thirteen->Int32Value()); CHECK_EQ(92, context->Global()->Get(v8::Integer::New(12))->Int32Value()); CHECK_EQ(92, context->Global()->Get(v8_str("12"))->Int32Value()); CHECK_EQ(92, context->Global()->Get(v8_num(12))->Int32Value()); CHECK_EQ(32, context->Global()->Get(v8::Integer::New(16))->Int32Value()); CHECK_EQ(32, context->Global()->Get(v8_str("16"))->Int32Value()); CHECK_EQ(32, context->Global()->Get(v8_num(16))->Int32Value()); CHECK_EQ(56, context->Global()->Get(v8::Integer::New(13))->Int32Value()); CHECK_EQ(56, context->Global()->Get(v8_str("13"))->Int32Value()); CHECK_EQ(56, context->Global()->Get(v8_num(13))->Int32Value());}THREADED_TEST(PropertyAttributes) { v8::HandleScope scope; LocalContext context; // read-only Local<String> prop = v8_str("read_only"); context->Global()->Set(prop, v8_num(7), v8::ReadOnly); CHECK_EQ(7, context->Global()->Get(prop)->Int32Value()); Script::Compile(v8_str("read_only = 9"))->Run(); CHECK_EQ(7, context->Global()->Get(prop)->Int32Value()); context->Global()->Set(prop, v8_num(10)); CHECK_EQ(7, context->Global()->Get(prop)->Int32Value()); // dont-delete prop = v8_str("dont_delete"); context->Global()->Set(prop, v8_num(13), v8::DontDelete); CHECK_EQ(13, context->Global()->Get(prop)->Int32Value()); Script::Compile(v8_str("delete dont_delete"))->Run(); CHECK_EQ(13, context->Global()->Get(prop)->Int32Value());}THREADED_TEST(Array) { v8::HandleScope scope; LocalContext context; Local<v8::Array> array = v8::Array::New(); CHECK_EQ(0, array->Length()); CHECK(array->Get(v8::Integer::New(0))->IsUndefined()); CHECK(!array->Has(0)); CHECK(array->Get(v8::Integer::New(100))->IsUndefined()); CHECK(!array->Has(100)); array->Set(v8::Integer::New(2), v8_num(7)); CHECK_EQ(3, array->Length()); CHECK(!array->Has(0)); CHECK(!array->Has(1)); CHECK(array->Has(2)); CHECK_EQ(7, array->Get(v8::Integer::New(2))->Int32Value()); Local<Value> obj = Script::Compile(v8_str("[1, 2, 3]"))->Run(); Local<v8::Array> arr = Local<v8::Array>::Cast(obj); CHECK_EQ(3, arr->Length()); CHECK_EQ(1, arr->Get(v8::Integer::New(0))->Int32Value()); CHECK_EQ(2, arr->Get(v8::Integer::New(1))->Int32Value()); CHECK_EQ(3, arr->Get(v8::Integer::New(2))->Int32Value());}v8::Handle<Value> HandleF(const v8::Arguments& args) { v8::HandleScope scope; ApiTestFuzzer::Fuzz(); Local<v8::Array> result = v8::Array::New(args.Length()); for (int i = 0; i < args.Length(); i++) result->Set(v8::Integer::New(i), args[i]); return scope.Close(result);}THREADED_TEST(Vector) { v8::HandleScope scope; Local<ObjectTemplate> global = ObjectTemplate::New(); global->Set(v8_str("f"), v8::FunctionTemplate::New(HandleF)); LocalContext context(0, global); const char* fun = "f()"; Local<v8::Array> a0 = Local<v8::Array>::Cast(Script::Compile(String::New(fun))->Run()); CHECK_EQ(0, a0->Length()); const char* fun2 = "f(11)"; Local<v8::Array> a1 = Local<v8::Array>::Cast(Script::Compile(String::New(fun2))->Run()); CHECK_EQ(1, a1->Length()); CHECK_EQ(11, a1->Get(v8::Integer::New(0))->Int32Value()); const char* fun3 = "f(12, 13)"; Local<v8::Array> a2 = Local<v8::Array>::Cast(Script::Compile(String::New(fun3))->Run()); CHECK_EQ(2, a2->Length()); CHECK_EQ(12, a2->Get(v8::Integer::New(0))->Int32Value()); CHECK_EQ(13, a2->Get(v8::Integer::New(1))->Int32Value()); const char* fun4 = "f(14, 15, 16)"; Local<v8::Array> a3 = Local<v8::Array>::Cast(Script::Compile(String::New(fun4))->Run()); CHECK_EQ(3, a3->Length()); CHECK_EQ(14, a3->Get(v8::Integer::New(0))->Int32Value()); CHECK_EQ(15, a3->Get(v8::Integer::New(1))->Int32Value()); CHECK_EQ(16, a3->Get(v8::Integer::New(2))->Int32Value()); const char* fun5 = "f(17, 18, 19, 20)"; Local<v8::Array> a4 = Local<v8::Array>::Cast(Script::Compile(String::New(fun5))->Run()); CHECK_EQ(4, a4->Length()); CHECK_EQ(17, a4->Get(v8::Integer::New(0))->Int32Value()); CHECK_EQ(18, a4->Get(v8::Integer::New(1))->Int32Value()); CHECK_EQ(19, a4->Get(v8::Integer::New(2))->Int32Value()); CHECK_EQ(20, a4->Get(v8::Integer::New(3))->Int32Value());}THREADED_TEST(FunctionCall) { v8::HandleScope scope; LocalContext context; CompileRun( "function Foo() {" " var result = [];" " for (var i = 0; i < arguments.length; i++) {" " result.push(arguments[i]);" " }" " return result;" "}"); Local<Function> Foo = Local<Function>::Cast(context->Global()->Get(v8_str("Foo"))); v8::Handle<Value>* args0 = NULL; Local<v8::Array> a0 = Local<v8::Array>::Cast(Foo->Call(Foo, 0, args0)); CHECK_EQ(0, a0->Length()); v8::Handle<Value> args1[] = { v8_num(1.1) }; Local<v8::Array> a1 = Local<v8::Array>::Cast(Foo->Call(Foo, 1, args1)); CHECK_EQ(1, a1->Length()); CHECK_EQ(1.1, a1->Get(v8::Integer::New(0))->NumberValue()); v8::Handle<Value> args2[] = { v8_num(2.2), v8_num(3.3) }; Local<v8::Array> a2 = Local<v8::Array>::Cast(Foo->Call(Foo, 2, args2)); CHECK_EQ(2, a2->Length()); CHECK_EQ(2.2, a2->Get(v8::Integer::New(0))->NumberValue()); CHECK_EQ(3.3, a2->Get(v8::Integer::New(1))->NumberValue()); v8::Handle<Value> args3[] = { v8_num(4.4), v8_num(5.5), v8_num(6.6) }; Local<v8::Array> a3 = Local<v8::Array>::Cast(Foo->Call(Foo, 3, args3)); CHECK_EQ(3, a3->Length()); CHECK_EQ(4.4, a3->Get(v8::Integer::New(0))->NumberValue()); CHECK_EQ(5.5, a3->Get(v8::Integer::New(1))->NumberValue()); CHECK_EQ(6.6, a3->Get(v8::Integer::New(2))->NumberValue()); v8::Handle<Value> args4[] = { v8_num(7.7), v8_num(8.8), v8_num(9.9), v8_num(10.11) }; Local<v8::Array> a4 = Local<v8::Array>::Cast(Foo->Call(Foo, 4, args4)); CHECK_EQ(4, a4->Length()); CHECK_EQ(7.7, a4->Get(v8::Integer::New(0))->NumberValue()); CHECK_EQ(8.8, a4->Get(v8::Integer::New(1))->NumberValue()); CHECK_EQ(9.9, a4->Get(v8::Integer::New(2))->NumberValue()); CHECK_EQ(10.11, a4->Get(v8::Integer::New(3))->NumberValue());}static const char* js_code_causing_out_of_memory = "var a = new Array(); while(true) a.push(a);";// These tests run for a long time and prevent us from running tests// that come after them so they cannot run in parallel.TEST(OutOfMemory) { // It's not possible to read a snapshot into a heap with different dimensions. if (v8::internal::Snapshot::IsEnabled()) return; // Set heap limits. static const int K = 1024; v8::ResourceConstraints constraints; constraints.set_max_young_space_size(256 * K); constraints.set_max_old_space_size(4 * K * K); v8::SetResourceConstraints(&constraints); // Execute a script that causes out of memory. v8::HandleScope scope; LocalContext context; v8::V8::IgnoreOutOfMemoryException(); Local<Script> script = Script::Compile(String::New(js_code_causing_out_of_memory)); Local<Value> result = script->Run(); // Check for out of memory state. CHECK(result.IsEmpty()); CHECK(context->HasOutOfMemoryException());}v8::Handle<Value> ProvokeOutOfMemory(const v8::Arguments& args) { ApiTestFuzzer::Fuzz(); v8::HandleScope scope; LocalContext context; Local<Script> script = Script::Compile(String::New(js_code_causing_out_of_memory)); Local<Value> result = script->Run(); // Check for out of memory state. CHECK(result.IsEmpty()); CHECK(context->HasOutOfMemoryException()); return result;}TEST(OutOfMemoryNested) { // It's not possible to read a snapshot into a heap with different dimensions. if (v8::internal::Snapshot::IsEnabled()) return; // Set heap limits. static const int K = 1024; v8::ResourceConstraints constraints; constraints.set_max_young_space_size(256 * K); constraints.set_max_old_space_size(4 * K * K); v8::SetResourceConstraints(&constraints); v8::HandleScope scope; Local<ObjectTemplate> templ = ObjectTemplate::New(); templ->Set(v8_str("ProvokeOutOfMemory"), v8::FunctionTemplate::New(ProvokeOutOfMemory)); LocalContext context(0, templ); v8::V8::IgnoreOutOfMemoryException(); Local<Value> result = CompileRun( "var thrown = false;" "try {" " ProvokeOutOfMemory();" "} catch (e) {" " thrown = true;" "}"); // Check for out of memory state. CHECK(result.IsEmpty()); CHECK(context->HasOutOfMemoryException());}TEST(HugeConsStringOutOfMemory) { // It's not possible to read a snapshot into a heap with different dimensions. if (v8::internal::Snapshot::IsEnabled()) return; v8::HandleScope scope; LocalContext context; // Set heap limits. static const int K = 1024; v8::ResourceConstraints constraints; constraints.set_max_young_space_size(256 * K); constraints.set_max_old_space_size(2 * K * K); v8::SetResourceConstraints(&constraints); // Execute a script that causes out of memory. v8::V8::IgnoreOutOfMemoryException(); // Build huge string. This should fail with out of memory exception. Local<Value> result = CompileRun( "var str = Array.prototype.join.call({length: 513}, \"A\").toUpperCase();" "for (var i = 0; i < 21; i++) { str = str + str; }"); // Check for out of memory state. CHECK(result.IsEmpty()); CHECK(context->HasOutOfMemoryException());}THREADED_TEST(ConstructCall) { v8::HandleScope scope; LocalContext context; CompileRun( "function Foo() {" " var result = [];" " for (var i = 0; i < arguments.length; i++) {" " result.push(arguments[i]);" " }" " return result;" "}"); Local<Function> Foo = Local<Function>::Cast(context->Global()->Get(v8_str("Foo"))); v8::Handle<Value>* args0 = NULL; Local<v8::Array> a0 = Local<v8::Array>::Cast(Foo->NewInstance(0, args0)); CHECK_EQ(0, a0->Length()); v8::Handle<Value> args1[] = { v8_num(1.1) }; Local<v8::Array> a1 = Local<v8::Array>::Cast(Foo->NewInstance(1, args1)); CHECK_EQ(1, a1->Length()); CHECK_EQ(1.1, a1->Get(v8::Integer::New(0))->NumberValue()); v8::Handle<Value> args2[] = { v8_num(2.2), v8_num(3.3) }; Local<v8::Array> a2 = Local<v8::Array>::Cast(Foo->NewInstance(2, args2)); CHECK_EQ(2, a2->Length()); CHECK_EQ(2.2, a2->Get(v8::Integer::New(0))->NumberValue()); CHECK_EQ(3.3, a2->Get(v8::Integer::New(1))->NumberValue()); v8::Handle<Value> args3[] = { v8_num(4.4), v8_num(5.5), v8_num(6.6) }; Local<v8::Array> a3 = Local<v8::Array>::Cast(Foo->NewInstance(3, args3)); CHECK_EQ(3, a3->Length()); CHECK_EQ(4.4, a3->Get(v8::Integer::New(0))->NumberValue()); CHECK_EQ(5.5, a3->Get(v8::Integer::New(1))->NumberValue()); CHECK_EQ(6.6, a3->Get(v8::Integer::New(2))->NumberValue()); v8::Handle<Value> args4[] = { v8_num(7.7), v8_num(8.8), v8_num(9.9), v8_num(10.11) }; Local<v8::Array> a4 = Local<v8::Array>::Cast(Foo->NewInstance(4, args4)); CHECK_EQ(4, a4->Length()); CHECK_EQ(7.7, a4->Get(v8::Integer::New(0))->NumberValue()); CHECK_EQ(8.8, a4->Get(v8::Integer::New(1))->NumberValue()); CHECK_EQ(9.9, a4->Get(v8::Integer::New(2))->NumberValue()); CHECK_EQ(10.11, a4->Get(v8::Integer::New(3))->NumberValue());}static void CheckUncle(v8::TryCatch* try_catch) { CHECK(try_catch->HasCaught()); String::AsciiValue str_value(try_catch->Exception()); CHECK_EQ(*str_value, "uncle?"); try_catch->Reset();}THREADED_TEST(ConversionException) { v8::HandleScope scope; LocalContext env; CompileRun( "function TestClass() { };" "TestClass.prototype.toString = function () { throw 'uncle?'; };" "var obj = new TestClass();"); Local<Value> obj = env->Global()->Get(v8_str("obj")); v8::TryCatch try_catch; Local<Value> to_string_result = obj->ToString(); CHECK(to_string_result.IsEmpty()); CheckUncle(&try_catch); Local<Value> to_number_result = obj->ToNumber(); CHECK(to_number_result.IsEmpty()); CheckUncle(&try_catch); Local<Value> to_integer_result = obj->ToInteger(); CHECK(to_integer_result.IsEmpty()); CheckUncle(&try_catch); Local<Value> to_uint32_result = obj->ToUint32(); CHECK(to_uint32_result.IsEmpty()); CheckUncle(&try_catch); Local<Value> to_int32_result = obj->ToInt32(); CHECK(to_int32_result.IsEmpty()); CheckUncle(&try_catch); Local<Value> to_object_result = v8::Undefined()->ToObject(); CHECK(to_object_result.IsEmpty()); CHECK(try_catch.HasCaught()); try_catch.Reset();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -