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_H_
6#define V8_OBJECTS_TEMPLATES_H_
7
8#include "src/objects/struct.h"
9
10// Has to be the last include (doesn't have include guards):
11#include "src/objects/object-macros.h"
12
13namespace v8 {
14namespace internal {
15
16class TemplateInfo : public Struct {
17 public:
18 NEVER_READ_ONLY_SPACE
19 DECL_ACCESSORS(tag, Object)
20 DECL_ACCESSORS(serial_number, Object)
21 DECL_INT_ACCESSORS(number_of_properties)
22 DECL_ACCESSORS(property_list, Object)
23 DECL_ACCESSORS(property_accessors, Object)
24
25 DECL_VERIFIER(TemplateInfo)
26
27 DECL_CAST(TemplateInfo)
28
29 DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
30 TORQUE_GENERATED_TEMPLATE_INFO_FIELDS)
31
32 static const int kFastTemplateInstantiationsCacheSize = 1 * KB;
33
34 // While we could grow the slow cache until we run out of memory, we put
35 // a limit on it anyway to not crash for embedders that re-create templates
36 // instead of caching them.
37 static const int kSlowTemplateInstantiationsCacheSize = 1 * MB;
38
39 OBJECT_CONSTRUCTORS(TemplateInfo, Struct);
40};
41
42// Contains data members that are rarely set on a FunctionTemplateInfo.
43class FunctionTemplateRareData : public Struct {
44 public:
45 // See DECL_RARE_ACCESSORS in FunctionTemplateInfo.
46 DECL_ACCESSORS(prototype_template, Object)
47 DECL_ACCESSORS(prototype_provider_template, Object)
48 DECL_ACCESSORS(parent_template, Object)
49 DECL_ACCESSORS(named_property_handler, Object)
50 DECL_ACCESSORS(indexed_property_handler, Object)
51 DECL_ACCESSORS(instance_template, Object)
52 DECL_ACCESSORS(instance_call_handler, Object)
53 DECL_ACCESSORS(access_check_info, Object)
54
55 DECL_CAST(FunctionTemplateRareData)
56
57 // Dispatched behavior.
58 DECL_PRINTER(FunctionTemplateRareData)
59 DECL_VERIFIER(FunctionTemplateRareData)
60
61 DEFINE_FIELD_OFFSET_CONSTANTS(
62 HeapObject::kHeaderSize,
63 TORQUE_GENERATED_FUNCTION_TEMPLATE_RARE_DATA_FIELDS)
64
65 OBJECT_CONSTRUCTORS(FunctionTemplateRareData, Struct);
66};
67
68// See the api-exposed FunctionTemplate for more information.
69class FunctionTemplateInfo : public TemplateInfo {
70 public:
71 // Handler invoked when calling an instance of this FunctionTemplateInfo.
72 // Either CallInfoHandler or Undefined.
73 DECL_ACCESSORS(call_code, Object)
74
75 DECL_ACCESSORS(class_name, Object)
76
77 // If the signature is a FunctionTemplateInfo it is used to check whether the
78 // receiver calling the associated JSFunction is a compatible receiver, i.e.
79 // it is an instance of the signature FunctionTemplateInfo or any of the
80 // receiver's prototypes are.
81 DECL_ACCESSORS(signature, Object)
82
83 // If any of the setters below declared by DECL_RARE_ACCESSORS are used then
84 // a FunctionTemplateRareData will be stored here. Until then this contains
85 // undefined.
86 DECL_ACCESSORS(rare_data, HeapObject)
87
88#define DECL_RARE_ACCESSORS(Name, CamelName, Type) \
89 inline Type Get##CamelName(); \
90 static inline void Set##CamelName( \
91 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info, \
92 Handle<Type> Name);
93
94 // ObjectTemplateInfo or Undefined, used for the prototype property of the
95 // resulting JSFunction instance of this FunctionTemplate.
96 DECL_RARE_ACCESSORS(prototype_template, PrototypeTemplate, Object)
97
98 // In the case the prototype_template is Undefined we use the
99 // prototype_provider_template to retrieve the instance prototype. Either
100 // contains an ObjectTemplateInfo or Undefined.
101 DECL_RARE_ACCESSORS(prototype_provider_template, PrototypeProviderTemplate,
102 Object)
103
104 // Used to create prototype chains. The parent_template's prototype is set as
105 // __proto__ of this FunctionTemplate's instance prototype. Is either a
106 // FunctionTemplateInfo or Undefined.
107 DECL_RARE_ACCESSORS(parent_template, ParentTemplate, Object)
108
109 // Returns an InterceptorInfo or Undefined for named properties.
110 DECL_RARE_ACCESSORS(named_property_handler, NamedPropertyHandler, Object)
111 // Returns an InterceptorInfo or Undefined for indexed properties/elements.
112 DECL_RARE_ACCESSORS(indexed_property_handler, IndexedPropertyHandler, Object)
113
114 // An ObjectTemplateInfo that is used when instantiating the JSFunction
115 // associated with this FunctionTemplateInfo. Contains either an
116 // ObjectTemplateInfo or Undefined. A default instance_template is assigned
117 // upon first instantiation if it's Undefined.
118 DECL_RARE_ACCESSORS(instance_template, InstanceTemplate, Object)
119
120 // Either a CallHandlerInfo or Undefined. If an instance_call_handler is
121 // provided the instances created from the associated JSFunction are marked as
122 // callable.
123 DECL_RARE_ACCESSORS(instance_call_handler, InstanceCallHandler, Object)
124
125 DECL_RARE_ACCESSORS(access_check_info, AccessCheckInfo, Object)
126#undef DECL_RARE_ACCESSORS
127
128 DECL_ACCESSORS(shared_function_info, Object)
129
130 // Internal field to store a flag bitfield.
131 DECL_INT_ACCESSORS(flag)
132
133 // "length" property of the final JSFunction.
134 DECL_INT_ACCESSORS(length)
135
136 // Either the_hole or a private symbol. Used to cache the result on
137 // the receiver under the the cached_property_name when this
138 // FunctionTemplateInfo is used as a getter.
139 DECL_ACCESSORS(cached_property_name, Object)
140
141 // Begin flag bits ---------------------
142 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
143 DECL_BOOLEAN_ACCESSORS(undetectable)
144
145 // If set, object instances created by this function
146 // requires access check.
147 DECL_BOOLEAN_ACCESSORS(needs_access_check)
148
149 DECL_BOOLEAN_ACCESSORS(read_only_prototype)
150
151 // If set, do not create a prototype property for the associated
152 // JSFunction. This bit implies that neither the prototype_template nor the
153 // prototype_provoider_template are instantiated.
154 DECL_BOOLEAN_ACCESSORS(remove_prototype)
155
156 // If set, do not attach a serial number to this FunctionTemplate and thus do
157 // not keep an instance boilerplate around.
158 DECL_BOOLEAN_ACCESSORS(do_not_cache)
159
160 // If not set an access may be performed on calling the associated JSFunction.
161 DECL_BOOLEAN_ACCESSORS(accept_any_receiver)
162 // End flag bits ---------------------
163
164 DECL_CAST(FunctionTemplateInfo)
165
166 // Dispatched behavior.
167 DECL_PRINTER(FunctionTemplateInfo)
168 DECL_VERIFIER(FunctionTemplateInfo)
169
170 static const int kInvalidSerialNumber = 0;
171
172 DEFINE_FIELD_OFFSET_CONSTANTS(TemplateInfo::kSize,
173 TORQUE_GENERATED_FUNCTION_TEMPLATE_INFO_FIELDS)
174
175 static Handle<SharedFunctionInfo> GetOrCreateSharedFunctionInfo(
176 Isolate* isolate, Handle<FunctionTemplateInfo> info,
177 MaybeHandle<Name> maybe_name);
178 // Returns parent function template or a null FunctionTemplateInfo.
179 inline FunctionTemplateInfo GetParent(Isolate* isolate);
180 // Returns true if |object| is an instance of this function template.
181 inline bool IsTemplateFor(JSObject object);
182 bool IsTemplateFor(Map map);
183 inline bool instantiated();
184
185 inline bool BreakAtEntry();
186
187 // Helper function for cached accessors.
188 static MaybeHandle<Name> TryGetCachedPropertyName(Isolate* isolate,
189 Handle<Object> getter);
190
191 // Bit position in the flag, from least significant bit position.
192 static const int kHiddenPrototypeBit = 0;
193 static const int kUndetectableBit = 1;
194 static const int kNeedsAccessCheckBit = 2;
195 static const int kReadOnlyPrototypeBit = 3;
196 static const int kRemovePrototypeBit = 4;
197 static const int kDoNotCacheBit = 5;
198 static const int kAcceptAnyReceiver = 6;
199
200 private:
201 static inline FunctionTemplateRareData EnsureFunctionTemplateRareData(
202 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info);
203
204 static FunctionTemplateRareData AllocateFunctionTemplateRareData(
205 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info);
206
207 OBJECT_CONSTRUCTORS(FunctionTemplateInfo, TemplateInfo);
208};
209
210class ObjectTemplateInfo : public TemplateInfo {
211 public:
212 DECL_ACCESSORS(constructor, Object)
213 DECL_ACCESSORS(data, Object)
214 DECL_INT_ACCESSORS(embedder_field_count)
215 DECL_BOOLEAN_ACCESSORS(immutable_proto)
216
217 DECL_CAST(ObjectTemplateInfo)
218
219 // Dispatched behavior.
220 DECL_PRINTER(ObjectTemplateInfo)
221 DECL_VERIFIER(ObjectTemplateInfo)
222
223 // Layout description.
224 DEFINE_FIELD_OFFSET_CONSTANTS(TemplateInfo::kSize,
225 TORQUE_GENERATED_OBJECT_TEMPLATE_INFO_FIELDS)
226
227 // Starting from given object template's constructor walk up the inheritance
228 // chain till a function template that has an instance template is found.
229 inline ObjectTemplateInfo GetParent(Isolate* isolate);
230
231 private:
232 class IsImmutablePrototype : public BitField<bool, 0, 1> {};
233 class EmbedderFieldCount
234 : public BitField<int, IsImmutablePrototype::kNext, 29> {};
235
236 OBJECT_CONSTRUCTORS(ObjectTemplateInfo, TemplateInfo);
237};
238
239} // namespace internal
240} // namespace v8
241
242#include "src/objects/object-macros-undef.h"
243
244#endif // V8_OBJECTS_TEMPLATES_H_
245