Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Unified Diff: test/cctest/test-api.cc

Issue 1642223003: [api] Make ObjectTemplate::SetNativeDataProperty() work even if the ObjectTemplate does not have a … (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/builtins.cc ('K') | « src/x87/builtins-x87.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 9400b0fd169dc3a5264dd54ce5d7e03b0efc7976..fdf6c130f073a7870cec084c941a83707a1770ec 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -1902,16 +1902,8 @@ THREADED_TEST(ObjectTemplate) {
templ1->NewInstance(env.local()).ToLocalChecked();
CHECK(class_name->StrictEquals(instance1->GetConstructorName()));
CHECK(env->Global()->Set(env.local(), v8_str("p"), instance1).FromJust());
- CHECK(v8_compile("(p.x == 10)")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
- CHECK(v8_compile("(p.y == 13)")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
+ CHECK(CompileRun("(p.x == 10)")->BooleanValue(env.local()).FromJust());
+ CHECK(CompileRun("(p.y == 13)")->BooleanValue(env.local()).FromJust());
Local<v8::FunctionTemplate> fun2 = v8::FunctionTemplate::New(isolate);
fun2->PrototypeTemplate()->Set(isolate, "nirk", v8_num(123));
Local<ObjectTemplate> templ2 = fun2->InstanceTemplate();
@@ -1920,28 +1912,190 @@ THREADED_TEST(ObjectTemplate) {
Local<v8::Object> instance2 =
templ2->NewInstance(env.local()).ToLocalChecked();
CHECK(env->Global()->Set(env.local(), v8_str("q"), instance2).FromJust());
- CHECK(v8_compile("(q.nirk == 123)")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
- CHECK(v8_compile("(q.a == 12)")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
- CHECK(v8_compile("(q.b.x == 10)")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
- CHECK(v8_compile("(q.b.y == 13)")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
+ CHECK(CompileRun("(q.nirk == 123)")->BooleanValue(env.local()).FromJust());
+ CHECK(CompileRun("(q.a == 12)")->BooleanValue(env.local()).FromJust());
+ CHECK(CompileRun("(q.b.x == 10)")->BooleanValue(env.local()).FromJust());
+ CHECK(CompileRun("(q.b.y == 13)")->BooleanValue(env.local()).FromJust());
+}
+
+THREADED_TEST(IntegerValue) {
+ LocalContext env;
+ v8::Isolate* isolate = CcTest::isolate();
+ v8::HandleScope scope(isolate);
+
+ CHECK_EQ(0, CompileRun("undefined")->IntegerValue(env.local()).FromJust());
+}
+
+static void GetNirk(Local<String> name,
+ const v8::PropertyCallbackInfo<v8::Value>& info) {
+ ApiTestFuzzer::Fuzz();
+ info.GetReturnValue().Set(v8_num(900));
+}
+
+static void GetRino(Local<String> name,
+ const v8::PropertyCallbackInfo<v8::Value>& info) {
+ ApiTestFuzzer::Fuzz();
+ info.GetReturnValue().Set(v8_num(560));
+}
+
+enum ObjectInstantiationMode {
+ // Create object using ObjectTemplate::NewInstance.
+ ObjectTemplate_NewInstance,
+ // Create object using FunctionTemplate::NewInstance on constructor.
+ Constructor_GetFunction_NewInstance,
+ // Create object using new operator on constructor.
+ Constructor_GetFunction_New
+};
+
+// Test object instance creation using a function template with an instance
+// template inherited from another function template with accessors and data
+// properties in prototype template.
+static void TestObjectTemplateInheritedWithPrototype(
+ ObjectInstantiationMode mode) {
+ LocalContext env;
+ v8::Isolate* isolate = CcTest::isolate();
+ v8::HandleScope scope(isolate);
+
+ Local<v8::FunctionTemplate> fun_A = v8::FunctionTemplate::New(isolate);
+ fun_A->SetClassName(v8_str("A"));
+ v8::Local<v8::ObjectTemplate> prototype_templ = fun_A->PrototypeTemplate();
+ prototype_templ->Set(isolate, "a", v8_num(113));
+ prototype_templ->SetNativeDataProperty(v8_str("nirk"), GetNirk);
+ prototype_templ->Set(isolate, "b", v8_num(153));
+
+ Local<v8::FunctionTemplate> fun_B = v8::FunctionTemplate::New(isolate);
+ v8::Local<v8::String> class_name = v8_str("B");
+ fun_B->SetClassName(class_name);
+ fun_B->Inherit(fun_A);
+ prototype_templ = fun_B->PrototypeTemplate();
+ prototype_templ->Set(isolate, "c", v8_num(713));
+ prototype_templ->SetNativeDataProperty(v8_str("rino"), GetRino);
+ prototype_templ->Set(isolate, "d", v8_num(753));
+
+ Local<ObjectTemplate> templ = fun_B->InstanceTemplate();
+ templ->Set(isolate, "x", v8_num(10));
+ templ->Set(isolate, "y", v8_num(13));
+
+ // Perform several iterations to trigger creation from cached boilerplate.
+ for (int i = 0; i < 3; i++) {
+ Local<v8::Object> instance;
+ switch (mode) {
+ case ObjectTemplate_NewInstance:
+ instance = templ->NewInstance(env.local()).ToLocalChecked();
+ break;
+
+ case Constructor_GetFunction_NewInstance: {
+ Local<v8::Function> function_B =
+ fun_B->GetFunction(env.local()).ToLocalChecked();
+ instance = function_B->NewInstance(env.local()).ToLocalChecked();
+ break;
+ }
+ case Constructor_GetFunction_New: {
+ Local<v8::Function> function_B =
+ fun_B->GetFunction(env.local()).ToLocalChecked();
+ if (i == 0) {
+ CHECK(env->Global()
+ ->Set(env.local(), class_name, function_B)
+ .FromJust());
+ }
+ instance =
+ CompileRun("new B()")->ToObject(env.local()).ToLocalChecked();
+ break;
+ }
+ default:
+ UNREACHABLE();
+ }
+
+ CHECK(class_name->StrictEquals(instance->GetConstructorName()));
+ CHECK(env->Global()->Set(env.local(), v8_str("o"), instance).FromJust());
+
+ CHECK_EQ(10, CompileRun("o.x")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(13, CompileRun("o.y")->IntegerValue(env.local()).FromJust());
+
+ CHECK_EQ(113, CompileRun("o.a")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(900, CompileRun("o.nirk")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(153, CompileRun("o.b")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(713, CompileRun("o.c")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(560, CompileRun("o.rino")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(753, CompileRun("o.d")->IntegerValue(env.local()).FromJust());
+ }
+}
+
+THREADED_TEST(TestObjectTemplateInheritedWithAccessorsInPrototype1) {
+ TestObjectTemplateInheritedWithPrototype(ObjectTemplate_NewInstance);
+}
+
+THREADED_TEST(TestObjectTemplateInheritedWithAccessorsInPrototype2) {
+ TestObjectTemplateInheritedWithPrototype(Constructor_GetFunction_NewInstance);
+}
+
+THREADED_TEST(TestObjectTemplateInheritedWithAccessorsInPrototype3) {
+ TestObjectTemplateInheritedWithPrototype(Constructor_GetFunction_New);
+}
+
+// Test object instance creation using a function template without an instance
+// template inherited from another function template.
+static void TestObjectTemplateInheritedWithoutInstanceTemplate(
+ ObjectInstantiationMode mode) {
+ LocalContext env;
+ v8::Isolate* isolate = CcTest::isolate();
+ v8::HandleScope scope(isolate);
+
+ Local<v8::FunctionTemplate> fun_A = v8::FunctionTemplate::New(isolate);
+ fun_A->SetClassName(v8_str("A"));
+
+ Local<ObjectTemplate> templ_A = fun_A->InstanceTemplate();
+ templ_A->SetNativeDataProperty(v8_str("nirk"), GetNirk);
+ templ_A->SetNativeDataProperty(v8_str("rino"), GetRino);
+
+ Local<v8::FunctionTemplate> fun_B = v8::FunctionTemplate::New(isolate);
+ v8::Local<v8::String> class_name = v8_str("B");
+ fun_B->SetClassName(class_name);
+ fun_B->Inherit(fun_A);
+
+ // Perform several iterations to trigger creation from cached boilerplate.
+ for (int i = 0; i < 3; i++) {
+ Local<v8::Object> instance;
+ switch (mode) {
+ case Constructor_GetFunction_NewInstance: {
+ Local<v8::Function> function_B =
+ fun_B->GetFunction(env.local()).ToLocalChecked();
+ instance = function_B->NewInstance(env.local()).ToLocalChecked();
+ break;
+ }
+ case Constructor_GetFunction_New: {
+ Local<v8::Function> function_B =
+ fun_B->GetFunction(env.local()).ToLocalChecked();
+ if (i == 0) {
+ CHECK(env->Global()
+ ->Set(env.local(), class_name, function_B)
+ .FromJust());
+ }
+ instance =
+ CompileRun("new B()")->ToObject(env.local()).ToLocalChecked();
+ break;
+ }
+ default:
+ UNREACHABLE();
+ }
+
+ CHECK(class_name->StrictEquals(instance->GetConstructorName()));
+ CHECK(env->Global()->Set(env.local(), v8_str("o"), instance).FromJust());
+
+ CHECK_EQ(900, CompileRun("o.nirk")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(560, CompileRun("o.rino")->IntegerValue(env.local()).FromJust());
+ }
+}
+
+THREADED_TEST(TestObjectTemplateInheritedWithPrototype1) {
+ TestObjectTemplateInheritedWithoutInstanceTemplate(
+ Constructor_GetFunction_NewInstance);
}
+THREADED_TEST(TestObjectTemplateInheritedWithPrototype2) {
+ TestObjectTemplateInheritedWithoutInstanceTemplate(
+ Constructor_GetFunction_New);
+}
static void GetFlabby(const v8::FunctionCallbackInfo<v8::Value>& args) {
ApiTestFuzzer::Fuzz();
@@ -2020,31 +2174,12 @@ THREADED_TEST(DescriptorInheritance) {
->NewInstance(env.local())
.ToLocalChecked())
.FromJust());
- CHECK_EQ(17.2, v8_compile("obj.flabby()")
- ->Run(env.local())
- .ToLocalChecked()
- ->NumberValue(env.local())
- .FromJust());
- CHECK(v8_compile("'flabby' in obj")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
- CHECK_EQ(15.2, v8_compile("obj.knurd")
- ->Run(env.local())
- .ToLocalChecked()
- ->NumberValue(env.local())
- .FromJust());
- CHECK(v8_compile("'knurd' in obj")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
- CHECK_EQ(20.1, v8_compile("obj.v1")
- ->Run(env.local())
- .ToLocalChecked()
- ->NumberValue(env.local())
- .FromJust());
+ CHECK_EQ(17.2,
+ CompileRun("obj.flabby()")->NumberValue(env.local()).FromJust());
+ CHECK(CompileRun("'flabby' in obj")->BooleanValue(env.local()).FromJust());
+ CHECK_EQ(15.2, CompileRun("obj.knurd")->NumberValue(env.local()).FromJust());
+ CHECK(CompileRun("'knurd' in obj")->BooleanValue(env.local()).FromJust());
+ CHECK_EQ(20.1, CompileRun("obj.v1")->NumberValue(env.local()).FromJust());
CHECK(env->Global()
->Set(env.local(), v8_str("obj2"), base2->GetFunction(env.local())
@@ -2052,36 +2187,104 @@ THREADED_TEST(DescriptorInheritance) {
->NewInstance(env.local())
.ToLocalChecked())
.FromJust());
- CHECK_EQ(17.2, v8_compile("obj2.flabby()")
- ->Run(env.local())
- .ToLocalChecked()
- ->NumberValue(env.local())
- .FromJust());
- CHECK(v8_compile("'flabby' in obj2")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
- CHECK_EQ(15.2, v8_compile("obj2.knurd")
- ->Run(env.local())
- .ToLocalChecked()
- ->NumberValue(env.local())
- .FromJust());
- CHECK(v8_compile("'knurd' in obj2")
- ->Run(env.local())
- .ToLocalChecked()
- ->BooleanValue(env.local())
- .FromJust());
- CHECK_EQ(10.1, v8_compile("obj2.v2")
- ->Run(env.local())
- .ToLocalChecked()
- ->NumberValue(env.local())
- .FromJust());
+ CHECK_EQ(17.2,
+ CompileRun("obj2.flabby()")->NumberValue(env.local()).FromJust());
+ CHECK(CompileRun("'flabby' in obj2")->BooleanValue(env.local()).FromJust());
+ CHECK_EQ(15.2, CompileRun("obj2.knurd")->NumberValue(env.local()).FromJust());
+ CHECK(CompileRun("'knurd' in obj2")->BooleanValue(env.local()).FromJust());
+ CHECK_EQ(10.1, CompileRun("obj2.v2")->NumberValue(env.local()).FromJust());
// base1 and base2 cannot cross reference to each's prototype
- CHECK(v8_compile("obj.v2")->Run(env.local()).ToLocalChecked()->IsUndefined());
- CHECK(
- v8_compile("obj2.v1")->Run(env.local()).ToLocalChecked()->IsUndefined());
+ CHECK(CompileRun("obj.v2")->IsUndefined());
+ CHECK(CompileRun("obj2.v1")->IsUndefined());
+}
+
+THREADED_TEST(DescriptorInheritance2) {
+ LocalContext env;
+ v8::Isolate* isolate = CcTest::isolate();
+ v8::HandleScope scope(isolate);
+ v8::Local<v8::FunctionTemplate> fun_A = v8::FunctionTemplate::New(isolate);
+ fun_A->SetClassName(v8_str("A"));
+ fun_A->InstanceTemplate()->SetNativeDataProperty(v8_str("knurd1"), GetKnurd);
+ fun_A->InstanceTemplate()->SetNativeDataProperty(v8_str("nirk1"), GetNirk);
+ fun_A->InstanceTemplate()->SetNativeDataProperty(v8_str("rino1"), GetRino);
+
+ v8::Local<v8::FunctionTemplate> fun_B = v8::FunctionTemplate::New(isolate);
+ fun_B->SetClassName(v8_str("B"));
+ fun_B->Inherit(fun_A);
+
+ v8::Local<v8::FunctionTemplate> fun_C = v8::FunctionTemplate::New(isolate);
+ fun_C->SetClassName(v8_str("C"));
+ fun_C->Inherit(fun_B);
+ fun_C->InstanceTemplate()->SetNativeDataProperty(v8_str("knurd2"), GetKnurd);
+ fun_C->InstanceTemplate()->SetNativeDataProperty(v8_str("nirk2"), GetNirk);
+ fun_C->InstanceTemplate()->SetNativeDataProperty(v8_str("rino2"), GetRino);
+
+ v8::Local<v8::FunctionTemplate> fun_D = v8::FunctionTemplate::New(isolate);
+ fun_D->SetClassName(v8_str("D"));
+ fun_D->Inherit(fun_C);
+
+ v8::Local<v8::FunctionTemplate> fun_E = v8::FunctionTemplate::New(isolate);
+ fun_E->SetClassName(v8_str("E"));
+ fun_E->Inherit(fun_D);
+ fun_E->InstanceTemplate()->SetNativeDataProperty(v8_str("knurd3"), GetKnurd);
+ fun_E->InstanceTemplate()->SetNativeDataProperty(v8_str("nirk3"), GetNirk);
+ fun_E->InstanceTemplate()->SetNativeDataProperty(v8_str("rino3"), GetRino);
+
+ v8::Local<v8::FunctionTemplate> fun_F = v8::FunctionTemplate::New(isolate);
+ fun_F->SetClassName(v8_str("F"));
+ fun_F->Inherit(fun_E);
+ v8::Local<v8::ObjectTemplate> templ = fun_F->InstanceTemplate();
+ const int kDataPropertiesNumber = 100;
+ for (int i = 0; i < kDataPropertiesNumber; i++) {
+ v8::Local<v8::Value> val = v8_num(i);
+ v8::Local<v8::String> val_str = val->ToString(env.local()).ToLocalChecked();
+ v8::Local<v8::String> name = String::Concat(v8_str("p"), val_str);
+
+ templ->Set(name, val);
+ templ->Set(val_str, val);
+ }
+
+ CHECK(env->Global()
+ ->Set(env.local(), v8_str("F"),
+ fun_F->GetFunction(env.local()).ToLocalChecked())
+ .FromJust());
+
+ v8::Local<v8::Script> script = v8_compile("o = new F()");
+
+ for (int i = 0; i < 100; i++) {
+ v8::HandleScope scope(isolate);
+ script->Run(env.local()).ToLocalChecked();
+ }
+ v8::Local<v8::Value> result = script->Run(env.local()).ToLocalChecked();
+
+ CHECK_EQ(15.2, CompileRun("o.knurd1")->NumberValue(env.local()).FromJust());
+ CHECK_EQ(15.2, CompileRun("o.knurd2")->NumberValue(env.local()).FromJust());
+ CHECK_EQ(15.2, CompileRun("o.knurd3")->NumberValue(env.local()).FromJust());
+
+ CHECK_EQ(900, CompileRun("o.nirk1")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(900, CompileRun("o.nirk2")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(900, CompileRun("o.nirk3")->IntegerValue(env.local()).FromJust());
+
+ CHECK_EQ(560, CompileRun("o.rino1")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(560, CompileRun("o.rino2")->IntegerValue(env.local()).FromJust());
+ CHECK_EQ(560, CompileRun("o.rino3")->IntegerValue(env.local()).FromJust());
+
+ v8::Local<v8::Object> object = result->ToObject(env.local()).ToLocalChecked();
+ for (int i = 0; i < kDataPropertiesNumber; i++) {
+ v8::Local<v8::Value> val = v8_num(i);
+ v8::Local<v8::String> val_str = val->ToString(env.local()).ToLocalChecked();
+ v8::Local<v8::String> name = String::Concat(v8_str("p"), val_str);
+
+ CHECK_EQ(i, object->Get(env.local(), name)
+ .ToLocalChecked()
+ ->IntegerValue(env.local())
+ .FromJust());
+ CHECK_EQ(i, object->Get(env.local(), val)
+ .ToLocalChecked()
+ ->IntegerValue(env.local())
+ .FromJust());
+ }
}
@@ -22148,7 +22351,7 @@ THREADED_TEST(FunctionNew) {
->get_api_func_data()
->serial_number()),
i_isolate);
- auto cache = i_isolate->function_cache();
+ auto cache = i_isolate->template_instantiations_cache();
CHECK(cache->Lookup(serial_number)->IsTheHole());
// Verify that each Function::New creates a new function instance
Local<Object> data2 = v8::Object::New(isolate);
« src/builtins.cc ('K') | « src/x87/builtins-x87.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698