1 // Copyright 2018 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 #ifndef V8_OBJECTS_TEMPLATES_INL_H_
6 #define V8_OBJECTS_TEMPLATES_INL_H_
7
8 #include "src/heap/heap-write-barrier-inl.h"
9 #include "src/objects/objects-inl.h"
10 #include "src/objects/oddball.h"
11 #include "src/objects/shared-function-info.h"
12 #include "src/objects/templates.h"
13
14 // Has to be the last include (doesn't have include guards):
15 #include "src/objects/object-macros.h"
16
17 namespace v8 {
18 namespace internal {
19
20 #include "torque-generated/src/objects/templates-tq-inl.inc"
21
22 TQ_OBJECT_CONSTRUCTORS_IMPL(TemplateInfo)
TQ_OBJECT_CONSTRUCTORS_IMPL(FunctionTemplateInfo)23 TQ_OBJECT_CONSTRUCTORS_IMPL(FunctionTemplateInfo)
24 TQ_OBJECT_CONSTRUCTORS_IMPL(ObjectTemplateInfo)
25 TQ_OBJECT_CONSTRUCTORS_IMPL(FunctionTemplateRareData)
26
27 NEVER_READ_ONLY_SPACE_IMPL(TemplateInfo)
28
29 BOOL_ACCESSORS(FunctionTemplateInfo, flag, undetectable,
30 UndetectableBit::kShift)
31 BOOL_ACCESSORS(FunctionTemplateInfo, flag, needs_access_check,
32 NeedsAccessCheckBit::kShift)
33 BOOL_ACCESSORS(FunctionTemplateInfo, flag, read_only_prototype,
34 ReadOnlyPrototypeBit::kShift)
35 BOOL_ACCESSORS(FunctionTemplateInfo, flag, remove_prototype,
36 RemovePrototypeBit::kShift)
37 BOOL_ACCESSORS(FunctionTemplateInfo, flag, accept_any_receiver,
38 AcceptAnyReceiverBit::kShift)
39 BOOL_ACCESSORS(FunctionTemplateInfo, flag, published, PublishedBit::kShift)
40
41 BIT_FIELD_ACCESSORS(
42 FunctionTemplateInfo, flag, allowed_receiver_instance_type_range_start,
43 FunctionTemplateInfo::AllowedReceiverInstanceTypeRangeStartBits)
44 BIT_FIELD_ACCESSORS(
45 FunctionTemplateInfo, flag, allowed_receiver_instance_type_range_end,
46 FunctionTemplateInfo::AllowedReceiverInstanceTypeRangeEndBits)
47
48 // static
49 FunctionTemplateRareData FunctionTemplateInfo::EnsureFunctionTemplateRareData(
50 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info) {
51 HeapObject extra = function_template_info->rare_data(isolate, kAcquireLoad);
52 if (extra.IsUndefined(isolate)) {
53 return AllocateFunctionTemplateRareData(isolate, function_template_info);
54 } else {
55 return FunctionTemplateRareData::cast(extra);
56 }
57 }
58
59 #define RARE_ACCESSORS(Name, CamelName, Type, Default) \
60 DEF_GETTER(FunctionTemplateInfo, Get##CamelName, Type) { \
61 HeapObject extra = rare_data(cage_base, kAcquireLoad); \
62 HeapObject undefined = GetReadOnlyRoots(cage_base).undefined_value(); \
63 return extra == undefined ? Default \
64 : FunctionTemplateRareData::cast(extra).Name(); \
65 } \
66 inline void FunctionTemplateInfo::Set##CamelName( \
67 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info, \
68 Handle<Type> Name) { \
69 FunctionTemplateRareData rare_data = \
70 EnsureFunctionTemplateRareData(isolate, function_template_info); \
71 rare_data.set_##Name(*Name); \
72 }
73
RARE_ACCESSORS(prototype_template,PrototypeTemplate,HeapObject,undefined)74 RARE_ACCESSORS(prototype_template, PrototypeTemplate, HeapObject, undefined)
75 RARE_ACCESSORS(prototype_provider_template, PrototypeProviderTemplate,
76 HeapObject, undefined)
77 RARE_ACCESSORS(parent_template, ParentTemplate, HeapObject, undefined)
78 RARE_ACCESSORS(named_property_handler, NamedPropertyHandler, HeapObject,
79 undefined)
80 RARE_ACCESSORS(indexed_property_handler, IndexedPropertyHandler, HeapObject,
81 undefined)
82 RARE_ACCESSORS(instance_template, InstanceTemplate, HeapObject, undefined)
83 RARE_ACCESSORS(instance_call_handler, InstanceCallHandler, HeapObject,
84 undefined)
85 RARE_ACCESSORS(access_check_info, AccessCheckInfo, HeapObject, undefined)
86 RARE_ACCESSORS(c_function_overloads, CFunctionOverloads, FixedArray,
87 GetReadOnlyRoots(cage_base).empty_fixed_array())
88 #undef RARE_ACCESSORS
89
90 int FunctionTemplateInfo::InstanceType() const {
91 int type = instance_type();
92 DCHECK(type == kNoJSApiObjectType ||
93 (type >= Internals::kFirstJSApiObjectType &&
94 type <= Internals::kLastJSApiObjectType));
95 return type;
96 }
97
SetInstanceType(int instance_type)98 void FunctionTemplateInfo::SetInstanceType(int instance_type) {
99 if (instance_type == 0) {
100 set_instance_type(kNoJSApiObjectType);
101 } else {
102 DCHECK_GT(instance_type, 0);
103 DCHECK_LT(Internals::kFirstJSApiObjectType + instance_type,
104 Internals::kLastJSApiObjectType);
105 set_instance_type(Internals::kFirstJSApiObjectType + instance_type);
106 }
107 }
108
should_cache()109 bool TemplateInfo::should_cache() const {
110 return serial_number() != kDoNotCache;
111 }
is_cached()112 bool TemplateInfo::is_cached() const { return serial_number() > kUncached; }
113
instantiated()114 bool FunctionTemplateInfo::instantiated() {
115 return shared_function_info().IsSharedFunctionInfo();
116 }
117
BreakAtEntry()118 inline bool FunctionTemplateInfo::BreakAtEntry() {
119 Object maybe_shared = shared_function_info();
120 if (maybe_shared.IsSharedFunctionInfo()) {
121 SharedFunctionInfo shared = SharedFunctionInfo::cast(maybe_shared);
122 return shared.BreakAtEntry();
123 }
124 return false;
125 }
126
GetParent(Isolate * isolate)127 FunctionTemplateInfo FunctionTemplateInfo::GetParent(Isolate* isolate) {
128 Object parent = GetParentTemplate();
129 return parent.IsUndefined(isolate) ? FunctionTemplateInfo()
130 : FunctionTemplateInfo::cast(parent);
131 }
132
GetParent(Isolate * isolate)133 ObjectTemplateInfo ObjectTemplateInfo::GetParent(Isolate* isolate) {
134 Object maybe_ctor = constructor();
135 if (maybe_ctor.IsUndefined(isolate)) return ObjectTemplateInfo();
136 FunctionTemplateInfo constructor = FunctionTemplateInfo::cast(maybe_ctor);
137 while (true) {
138 constructor = constructor.GetParent(isolate);
139 if (constructor.is_null()) return ObjectTemplateInfo();
140 Object maybe_obj = constructor.GetInstanceTemplate();
141 if (!maybe_obj.IsUndefined(isolate)) {
142 return ObjectTemplateInfo::cast(maybe_obj);
143 }
144 }
145 return ObjectTemplateInfo();
146 }
147
embedder_field_count()148 int ObjectTemplateInfo::embedder_field_count() const {
149 return EmbedderFieldCountBits::decode(data());
150 }
151
set_embedder_field_count(int count)152 void ObjectTemplateInfo::set_embedder_field_count(int count) {
153 DCHECK_LE(count, JSObject::kMaxEmbedderFields);
154 return set_data(EmbedderFieldCountBits::update(data(), count));
155 }
156
immutable_proto()157 bool ObjectTemplateInfo::immutable_proto() const {
158 return IsImmutablePrototypeBit::decode(data());
159 }
160
set_immutable_proto(bool immutable)161 void ObjectTemplateInfo::set_immutable_proto(bool immutable) {
162 return set_data(IsImmutablePrototypeBit::update(data(), immutable));
163 }
164
code_like()165 bool ObjectTemplateInfo::code_like() const {
166 return IsCodeKindBit::decode(data());
167 }
168
set_code_like(bool is_code_like)169 void ObjectTemplateInfo::set_code_like(bool is_code_like) {
170 return set_data(IsCodeKindBit::update(data(), is_code_like));
171 }
172
IsTemplateFor(JSObject object)173 bool FunctionTemplateInfo::IsTemplateFor(JSObject object) {
174 return IsTemplateFor(object.map());
175 }
176
177 } // namespace internal
178 } // namespace v8
179
180 #include "src/objects/object-macros-undef.h"
181
182 #endif // V8_OBJECTS_TEMPLATES_INL_H_
183