xref: /third_party/node/deps/v8/src/objects/templates.h (revision 1cb0ef41)
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#include "torque-generated/bit-fields.h"
10
11// Has to be the last include (doesn't have include guards):
12#include "src/objects/object-macros.h"
13
14namespace v8 {
15
16class CFunctionInfo;
17class StructBodyDescriptor;
18
19namespace internal {
20
21#include "torque-generated/src/objects/templates-tq.inc"
22
23class TemplateInfo : public TorqueGeneratedTemplateInfo<TemplateInfo, Struct> {
24 public:
25  NEVER_READ_ONLY_SPACE
26
27  static const int kFastTemplateInstantiationsCacheSize = 1 * KB;
28
29  // While we could grow the slow cache until we run out of memory, we put
30  // a limit on it anyway to not crash for embedders that re-create templates
31  // instead of caching them.
32  static const int kSlowTemplateInstantiationsCacheSize = 1 * MB;
33
34  // If the serial number is set to kDoNotCache, then we should never cache this
35  // TemplateInfo.
36  static const int kDoNotCache = -1;
37  // If the serial number is set to kUncached, it means that this TemplateInfo
38  // has not been cached yet but it can be.
39  static const int kUncached = -2;
40
41  inline bool should_cache() const;
42  inline bool is_cached() const;
43
44  using BodyDescriptor = StructBodyDescriptor;
45
46  TQ_OBJECT_CONSTRUCTORS(TemplateInfo)
47};
48
49// Contains data members that are rarely set on a FunctionTemplateInfo.
50class FunctionTemplateRareData
51    : public TorqueGeneratedFunctionTemplateRareData<FunctionTemplateRareData,
52                                                     Struct> {
53 public:
54  DECL_VERIFIER(FunctionTemplateRareData)
55
56  using BodyDescriptor = StructBodyDescriptor;
57
58  TQ_OBJECT_CONSTRUCTORS(FunctionTemplateRareData)
59};
60
61// See the api-exposed FunctionTemplate for more information.
62class FunctionTemplateInfo
63    : public TorqueGeneratedFunctionTemplateInfo<FunctionTemplateInfo,
64                                                 TemplateInfo> {
65 public:
66#define DECL_RARE_ACCESSORS(Name, CamelName, Type)                           \
67  DECL_GETTER(Get##CamelName, Type)                                          \
68  static inline void Set##CamelName(                                         \
69      Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info, \
70      Handle<Type> Name);
71
72  // ObjectTemplateInfo or Undefined, used for the prototype property of the
73  // resulting JSFunction instance of this FunctionTemplate.
74  DECL_RARE_ACCESSORS(prototype_template, PrototypeTemplate, HeapObject)
75
76  // In the case the prototype_template is Undefined we use the
77  // prototype_provider_template to retrieve the instance prototype. Either
78  // contains an FunctionTemplateInfo or Undefined.
79  DECL_RARE_ACCESSORS(prototype_provider_template, PrototypeProviderTemplate,
80                      HeapObject)
81
82  // Used to create prototype chains. The parent_template's prototype is set as
83  // __proto__ of this FunctionTemplate's instance prototype. Is either a
84  // FunctionTemplateInfo or Undefined.
85  DECL_RARE_ACCESSORS(parent_template, ParentTemplate, HeapObject)
86
87  // Returns an InterceptorInfo or Undefined for named properties.
88  DECL_RARE_ACCESSORS(named_property_handler, NamedPropertyHandler, HeapObject)
89  // Returns an InterceptorInfo or Undefined for indexed properties/elements.
90  DECL_RARE_ACCESSORS(indexed_property_handler, IndexedPropertyHandler,
91                      HeapObject)
92
93  // An ObjectTemplateInfo that is used when instantiating the JSFunction
94  // associated with this FunctionTemplateInfo. Contains either an
95  // ObjectTemplateInfo or Undefined. A default instance_template is assigned
96  // upon first instantiation if it's Undefined.
97  DECL_RARE_ACCESSORS(instance_template, InstanceTemplate, HeapObject)
98
99  // Either a CallHandlerInfo or Undefined. If an instance_call_handler is
100  // provided the instances created from the associated JSFunction are marked as
101  // callable.
102  DECL_RARE_ACCESSORS(instance_call_handler, InstanceCallHandler, HeapObject)
103
104  DECL_RARE_ACCESSORS(access_check_info, AccessCheckInfo, HeapObject)
105
106  DECL_RARE_ACCESSORS(c_function_overloads, CFunctionOverloads, FixedArray)
107#undef DECL_RARE_ACCESSORS
108
109  // Begin flag bits ---------------------
110  DECL_BOOLEAN_ACCESSORS(undetectable)
111
112  // If set, object instances created by this function
113  // requires access check.
114  DECL_BOOLEAN_ACCESSORS(needs_access_check)
115
116  DECL_BOOLEAN_ACCESSORS(read_only_prototype)
117
118  // If set, do not create a prototype property for the associated
119  // JSFunction. This bit implies that neither the prototype_template nor the
120  // prototype_provoider_template are instantiated.
121  DECL_BOOLEAN_ACCESSORS(remove_prototype)
122
123  // If not set an access may be performed on calling the associated JSFunction.
124  DECL_BOOLEAN_ACCESSORS(accept_any_receiver)
125
126  // This flag is used to check that the FunctionTemplateInfo instance is not
127  // changed after it became visible to TurboFan (either set in a
128  // SharedFunctionInfo or an accessor), because TF relies on immutability to
129  // safely read concurrently.
130  DECL_BOOLEAN_ACCESSORS(published)
131
132  // This specifies the permissable range of instance type of objects that can
133  // be allowed to be used as receivers with the given template.
134  DECL_INT16_ACCESSORS(allowed_receiver_instance_type_range_start)
135  DECL_INT16_ACCESSORS(allowed_receiver_instance_type_range_end)
136  // End flag bits ---------------------
137
138  // Dispatched behavior.
139  DECL_PRINTER(FunctionTemplateInfo)
140
141  inline int InstanceType() const;
142  inline void SetInstanceType(int instance_type);
143
144  static Handle<SharedFunctionInfo> GetOrCreateSharedFunctionInfo(
145      Isolate* isolate, Handle<FunctionTemplateInfo> info,
146      MaybeHandle<Name> maybe_name);
147
148  static Handle<SharedFunctionInfo> GetOrCreateSharedFunctionInfo(
149      LocalIsolate* isolate, Handle<FunctionTemplateInfo> info,
150      Handle<Name> maybe_name) {
151    // We don't support streaming compilation of scripts with natives, so we
152    // don't need an off-thread implementation of this.
153    UNREACHABLE();
154  }
155
156  // Returns parent function template or a null FunctionTemplateInfo.
157  inline FunctionTemplateInfo GetParent(Isolate* isolate);
158  // Returns true if |object| is an instance of this function template.
159  inline bool IsTemplateFor(JSObject object);
160  bool IsTemplateFor(Map map) const;
161  // Returns true if |object| is an API object and is constructed by this
162  // particular function template (skips walking up the chain of inheriting
163  // functions that is done by IsTemplateFor).
164  bool IsLeafTemplateForApiObject(Object object) const;
165  inline bool instantiated();
166
167  bool BreakAtEntry();
168  bool HasInstanceType();
169
170  // Helper function for cached accessors.
171  static base::Optional<Name> TryGetCachedPropertyName(Isolate* isolate,
172                                                       Object getter);
173  // Fast API overloads.
174  int GetCFunctionsCount() const;
175  Address GetCFunction(int index) const;
176  const CFunctionInfo* GetCSignature(int index) const;
177
178  // CFunction data for a set of overloads is stored into a FixedArray, as
179  // [address_0, signature_0, ... address_n-1, signature_n-1].
180  static const int kFunctionOverloadEntrySize = 2;
181
182  // Bit position in the flag, from least significant bit position.
183  DEFINE_TORQUE_GENERATED_FUNCTION_TEMPLATE_INFO_FLAGS()
184
185  using BodyDescriptor = StructBodyDescriptor;
186
187 private:
188  static constexpr int kNoJSApiObjectType = 0;
189  static inline FunctionTemplateRareData EnsureFunctionTemplateRareData(
190      Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info);
191
192  static FunctionTemplateRareData AllocateFunctionTemplateRareData(
193      Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info);
194
195  TQ_OBJECT_CONSTRUCTORS(FunctionTemplateInfo)
196};
197
198class ObjectTemplateInfo
199    : public TorqueGeneratedObjectTemplateInfo<ObjectTemplateInfo,
200                                               TemplateInfo> {
201 public:
202  DECL_INT_ACCESSORS(embedder_field_count)
203  DECL_BOOLEAN_ACCESSORS(immutable_proto)
204  DECL_BOOLEAN_ACCESSORS(code_like)
205
206  // Dispatched behavior.
207  DECL_PRINTER(ObjectTemplateInfo)
208
209  // Starting from given object template's constructor walk up the inheritance
210  // chain till a function template that has an instance template is found.
211  inline ObjectTemplateInfo GetParent(Isolate* isolate);
212
213  using BodyDescriptor = StructBodyDescriptor;
214
215 private:
216  DEFINE_TORQUE_GENERATED_OBJECT_TEMPLATE_INFO_FLAGS()
217
218  TQ_OBJECT_CONSTRUCTORS(ObjectTemplateInfo)
219};
220
221}  // namespace internal
222}  // namespace v8
223
224#include "src/objects/object-macros-undef.h"
225
226#endif  // V8_OBJECTS_TEMPLATES_H_
227