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

Side by Side Diff: test/unittests/api/v8-object-unittest.cc

Issue 2770003002: Set the current context to the function's context when entering to LAP. (Closed)
Patch Set: . Created 3 years, 8 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 unified diff | Download patch
« src/x64/code-stubs-x64.cc ('K') | « src/x64/code-stubs-x64.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 the V8 project authors. All rights reserved. 1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "include/v8.h" 5 #include "include/v8.h"
6 #include "src/api.h"
7 #include "src/objects-inl.h"
6 #include "test/unittests/test-utils.h" 8 #include "test/unittests/test-utils.h"
7 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
8 10
9 namespace v8 { 11 namespace v8 {
10 namespace { 12 namespace {
11 13
12 using ObjectTest = TestWithContext; 14 using ObjectTest = TestWithContext;
13 15
14 void accessor_name_getter_callback(Local<Name>, 16 void accessor_name_getter_callback(Local<Name>,
15 const PropertyCallbackInfo<Value>&) {} 17 const PropertyCallbackInfo<Value>&) {}
(...skipping 10 matching lines...) Expand all
26 prop_desc.set_configurable(false); 28 prop_desc.set_configurable(false);
27 global->DefineProperty(context(), property_name, prop_desc).ToChecked(); 29 global->DefineProperty(context(), property_name, prop_desc).ToChecked();
28 30
29 Maybe<bool> result = global->SetAccessor(context(), property_name, 31 Maybe<bool> result = global->SetAccessor(context(), property_name,
30 accessor_name_getter_callback); 32 accessor_name_getter_callback);
31 ASSERT_TRUE(result.IsJust()); 33 ASSERT_TRUE(result.IsJust());
32 ASSERT_FALSE(result.FromJust()); 34 ASSERT_FALSE(result.FromJust());
33 ASSERT_FALSE(try_catch.HasCaught()); 35 ASSERT_FALSE(try_catch.HasCaught());
34 } 36 }
35 37
38 using LapContextTest = TestWithIsolate;
39
40 TEST_F(LapContextTest, CurrentContextMustBeFunctionContext) {
41 // The receiver object is created in |receiver_context|, but its prototype
42 // object is created in |prototype_context|, and the property is accessed
43 // from |caller_context|.
44 Local<Context> receiver_context = Context::New(isolate());
45 Local<Context> prototype_context = Context::New(isolate());
46 Local<Context> caller_context = Context::New(isolate());
47
48 Local<FunctionTemplate> function_template = FunctionTemplate::New(isolate());
49 Local<String> property_key =
50 String::NewFromUtf8(isolate(), "property", NewStringType::kNormal)
51 .ToLocalChecked();
52 Local<FunctionTemplate> get_or_set = FunctionTemplate::New(
53 isolate(),
54 [](const FunctionCallbackInfo<Value>& info) {
55 Local<Context> prototype_context = *reinterpret_cast<Local<Context>*>(
56 info.Data().As<External>()->Value());
57 EXPECT_EQ(prototype_context, info.GetIsolate()->GetCurrentContext());
58 },
59 External::New(isolate(), &prototype_context));
60 function_template->PrototypeTemplate()->SetAccessorProperty(
61 property_key, get_or_set, get_or_set);
62
63 // |object| is created in |receiver_context|, and |prototype| is created
64 // in |prototype_context|. And then, object.__proto__ = prototype.
65 Local<Function> interface_for_receiver =
66 function_template->GetFunction(receiver_context).ToLocalChecked();
67 Local<Function> interface_for_prototype =
68 function_template->GetFunction(prototype_context).ToLocalChecked();
69 Local<String> prototype_key =
70 String::NewFromUtf8(isolate(), "prototype", NewStringType::kNormal)
71 .ToLocalChecked();
72 Local<Object> prototype =
73 interface_for_prototype->Get(caller_context, prototype_key)
74 .ToLocalChecked()
75 .As<Object>();
76 Local<Object> object =
77 interface_for_receiver->NewInstance(receiver_context).ToLocalChecked();
78 object->SetPrototype(caller_context, prototype).ToChecked();
79 EXPECT_EQ(receiver_context, object->CreationContext());
80 EXPECT_EQ(prototype_context, prototype->CreationContext());
81
82 object->Get(caller_context, property_key).ToLocalChecked();
83 object->Set(caller_context, property_key, Null(isolate())).ToChecked();
84
85 // Test with a compiled version.
86 Local<String> object_key =
87 String::NewFromUtf8(isolate(), "object", NewStringType::kNormal)
88 .ToLocalChecked();
89 caller_context->Global()->Set(caller_context, object_key, object).ToChecked();
90 const char script[] =
91 "function f() { return object.property; } "
92 "f(); f(); "
93 "%OptimizeFunctionOnNextCall(f); "
94 "f();";
95 Context::Scope scope(caller_context);
96 internal::FLAG_allow_natives_syntax = true;
97 Script::Compile(
98 caller_context,
99 String::NewFromUtf8(isolate(), script, v8::NewStringType::kNormal)
100 .ToLocalChecked())
101 .ToLocalChecked()
102 ->Run(caller_context)
103 .ToLocalChecked();
104 }
105
36 } // namespace 106 } // namespace
37 } // namespace v8 107 } // namespace v8
OLDNEW
« src/x64/code-stubs-x64.cc ('K') | « src/x64/code-stubs-x64.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698