OLD | NEW |
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 Loading... |
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|, and the property is |
| 42 // accessed from |caller_context|. |
| 43 Local<Context> receiver_context = Context::New(isolate()); |
| 44 Local<Context> caller_context = Context::New(isolate()); |
| 45 |
| 46 Local<FunctionTemplate> function_template = FunctionTemplate::New(isolate()); |
| 47 Local<Signature> signature = Signature::New(isolate(), function_template); |
| 48 Local<String> property_key = |
| 49 String::NewFromUtf8(isolate(), "property", NewStringType::kNormal) |
| 50 .ToLocalChecked(); |
| 51 Local<FunctionTemplate> get_or_set = FunctionTemplate::New( |
| 52 isolate(), |
| 53 [](const FunctionCallbackInfo<Value>& info) { |
| 54 Local<Context> receiver_context = *reinterpret_cast<Local<Context>*>( |
| 55 info.Data().As<External>()->Value()); |
| 56 EXPECT_EQ(receiver_context, info.GetIsolate()->GetCurrentContext()); |
| 57 }, |
| 58 External::New(isolate(), &receiver_context), signature); |
| 59 function_template->PrototypeTemplate()->SetAccessorProperty( |
| 60 property_key, get_or_set, get_or_set); |
| 61 |
| 62 // |object| is created in |receiver_context|, and |prototype| is created |
| 63 // in |prototype_context|. And then, object.__proto__ = prototype. |
| 64 Local<Function> interface = |
| 65 function_template->GetFunction(receiver_context).ToLocalChecked(); |
| 66 Local<Object> object = |
| 67 interface->NewInstance(receiver_context).ToLocalChecked(); |
| 68 EXPECT_EQ(receiver_context, object->CreationContext()); |
| 69 |
| 70 object->Get(caller_context, property_key).ToLocalChecked(); |
| 71 object->Set(caller_context, property_key, Null(isolate())).ToChecked(); |
| 72 |
| 73 // Test with a compiled version. |
| 74 Local<String> object_key = |
| 75 String::NewFromUtf8(isolate(), "object", NewStringType::kNormal) |
| 76 .ToLocalChecked(); |
| 77 caller_context->Global()->Set(caller_context, object_key, object).ToChecked(); |
| 78 const char script[] = |
| 79 "function f() { return object.property; } " |
| 80 "f(); f(); " |
| 81 "%OptimizeFunctionOnNextCall(f); " |
| 82 "f();"; |
| 83 Context::Scope scope(caller_context); |
| 84 internal::FLAG_allow_natives_syntax = true; |
| 85 Script::Compile( |
| 86 caller_context, |
| 87 String::NewFromUtf8(isolate(), script, v8::NewStringType::kNormal) |
| 88 .ToLocalChecked()) |
| 89 .ToLocalChecked() |
| 90 ->Run(caller_context) |
| 91 .ToLocalChecked(); |
| 92 } |
| 93 |
36 } // namespace | 94 } // namespace |
37 } // namespace v8 | 95 } // namespace v8 |
OLD | NEW |