Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include "testing/gtest/include/gtest/gtest.h"
6 :
7 : #include "include/v8.h"
8 : #include "src/api-inl.h"
9 : #include "src/handles.h"
10 : #include "src/objects-inl.h"
11 : #include "test/unittests/test-utils.h"
12 :
13 : namespace v8 {
14 : namespace remote_object_unittest {
15 :
16 : typedef TestWithIsolate RemoteObjectTest;
17 :
18 : namespace {
19 :
20 0 : bool AccessCheck(Local<Context> accessing_context,
21 : Local<Object> accessed_object, Local<Value> data) {
22 0 : return false;
23 : }
24 :
25 0 : void NamedGetter(Local<Name> property,
26 0 : const PropertyCallbackInfo<Value>& info) {}
27 :
28 0 : void Constructor(const FunctionCallbackInfo<Value>& info) {
29 0 : ASSERT_TRUE(info.IsConstructCall());
30 : }
31 :
32 : } // namespace
33 :
34 15443 : TEST_F(RemoteObjectTest, CreationContextOfRemoteContext) {
35 1 : Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate());
36 1 : global_template->SetAccessCheckCallbackAndHandler(
37 : AccessCheck, NamedPropertyHandlerConfiguration(NamedGetter),
38 1 : IndexedPropertyHandlerConfiguration());
39 :
40 : Local<Object> remote_context =
41 2 : Context::NewRemoteContext(isolate(), global_template).ToLocalChecked();
42 2 : EXPECT_TRUE(remote_context->CreationContext().IsEmpty());
43 1 : }
44 :
45 15443 : TEST_F(RemoteObjectTest, CreationContextOfRemoteObject) {
46 : Local<FunctionTemplate> constructor_template =
47 1 : FunctionTemplate::New(isolate(), Constructor);
48 2 : constructor_template->InstanceTemplate()->SetAccessCheckCallbackAndHandler(
49 : AccessCheck, NamedPropertyHandlerConfiguration(NamedGetter),
50 1 : IndexedPropertyHandlerConfiguration());
51 :
52 : Local<Object> remote_object =
53 1 : constructor_template->NewRemoteInstance().ToLocalChecked();
54 2 : EXPECT_TRUE(remote_object->CreationContext().IsEmpty());
55 1 : }
56 :
57 15443 : TEST_F(RemoteObjectTest, RemoteContextInstanceChecks) {
58 : Local<FunctionTemplate> parent_template =
59 1 : FunctionTemplate::New(isolate(), Constructor);
60 :
61 : Local<FunctionTemplate> constructor_template =
62 1 : FunctionTemplate::New(isolate(), Constructor);
63 2 : constructor_template->InstanceTemplate()->SetAccessCheckCallbackAndHandler(
64 : AccessCheck, NamedPropertyHandlerConfiguration(NamedGetter),
65 1 : IndexedPropertyHandlerConfiguration());
66 1 : constructor_template->Inherit(parent_template);
67 :
68 : Local<Object> remote_context =
69 1 : Context::NewRemoteContext(isolate(),
70 2 : constructor_template->InstanceTemplate())
71 : .ToLocalChecked();
72 2 : EXPECT_TRUE(parent_template->HasInstance(remote_context));
73 2 : EXPECT_TRUE(constructor_template->HasInstance(remote_context));
74 1 : }
75 :
76 15443 : TEST_F(RemoteObjectTest, TypeOfRemoteContext) {
77 1 : Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate());
78 1 : global_template->SetAccessCheckCallbackAndHandler(
79 : AccessCheck, NamedPropertyHandlerConfiguration(NamedGetter),
80 1 : IndexedPropertyHandlerConfiguration());
81 :
82 : Local<Object> remote_context =
83 2 : Context::NewRemoteContext(isolate(), global_template).ToLocalChecked();
84 3 : String::Utf8Value result(isolate(), remote_context->TypeOf(isolate()));
85 1 : EXPECT_STREQ("object", *result);
86 1 : }
87 :
88 15443 : TEST_F(RemoteObjectTest, TypeOfRemoteObject) {
89 : Local<FunctionTemplate> constructor_template =
90 1 : FunctionTemplate::New(isolate(), Constructor);
91 2 : constructor_template->InstanceTemplate()->SetAccessCheckCallbackAndHandler(
92 : AccessCheck, NamedPropertyHandlerConfiguration(NamedGetter),
93 1 : IndexedPropertyHandlerConfiguration());
94 :
95 : Local<Object> remote_object =
96 1 : constructor_template->NewRemoteInstance().ToLocalChecked();
97 3 : String::Utf8Value result(isolate(), remote_object->TypeOf(isolate()));
98 1 : EXPECT_STREQ("object", *result);
99 1 : }
100 :
101 15443 : TEST_F(RemoteObjectTest, ClassOf) {
102 : Local<FunctionTemplate> constructor_template =
103 1 : FunctionTemplate::New(isolate(), Constructor);
104 2 : constructor_template->InstanceTemplate()->SetAccessCheckCallbackAndHandler(
105 : AccessCheck, NamedPropertyHandlerConfiguration(NamedGetter),
106 1 : IndexedPropertyHandlerConfiguration());
107 : constructor_template->SetClassName(
108 1 : String::NewFromUtf8(isolate(), "test_class", NewStringType::kNormal)
109 1 : .ToLocalChecked());
110 :
111 : Local<Object> remote_object =
112 1 : constructor_template->NewRemoteInstance().ToLocalChecked();
113 : Local<String> class_name = Utils::ToLocal(
114 2 : i::handle(Utils::OpenHandle(*remote_object)->class_name(), i_isolate()));
115 2 : String::Utf8Value result(isolate(), class_name);
116 1 : EXPECT_STREQ("test_class", *result);
117 1 : }
118 :
119 : } // namespace remote_object_unittest
120 9264 : } // namespace v8
|