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