• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 namespace v8 {
15 namespace internal {
16 
17 #include "torque-generated/src/objects/templates-tq.inc"
18 
19 class TemplateInfo : public TorqueGeneratedTemplateInfo<TemplateInfo, Struct> {
20  public:
21   NEVER_READ_ONLY_SPACE
22 
23   static const int kFastTemplateInstantiationsCacheSize = 1 * KB;
24 
25   // While we could grow the slow cache until we run out of memory, we put
26   // a limit on it anyway to not crash for embedders that re-create templates
27   // instead of caching them.
28   static const int kSlowTemplateInstantiationsCacheSize = 1 * MB;
29 
30   TQ_OBJECT_CONSTRUCTORS(TemplateInfo)
31 };
32 
33 // Contains data members that are rarely set on a FunctionTemplateInfo.
34 class FunctionTemplateRareData
35     : public TorqueGeneratedFunctionTemplateRareData<FunctionTemplateRareData,
36                                                      Struct> {
37  public:
38   TQ_OBJECT_CONSTRUCTORS(FunctionTemplateRareData)
39 };
40 
41 // See the api-exposed FunctionTemplate for more information.
42 class FunctionTemplateInfo
43     : public TorqueGeneratedFunctionTemplateInfo<FunctionTemplateInfo,
44                                                  TemplateInfo> {
45  public:
46 #define DECL_RARE_ACCESSORS(Name, CamelName, Type)                           \
47   DECL_GETTER(Get##CamelName, Type)                                          \
48   static inline void Set##CamelName(                                         \
49       Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info, \
50       Handle<Type> Name);
51 
52   // ObjectTemplateInfo or Undefined, used for the prototype property of the
53   // resulting JSFunction instance of this FunctionTemplate.
54   DECL_RARE_ACCESSORS(prototype_template, PrototypeTemplate, HeapObject)
55 
56   // In the case the prototype_template is Undefined we use the
57   // prototype_provider_template to retrieve the instance prototype. Either
58   // contains an FunctionTemplateInfo or Undefined.
59   DECL_RARE_ACCESSORS(prototype_provider_template, PrototypeProviderTemplate,
60                       HeapObject)
61 
62   // Used to create prototype chains. The parent_template's prototype is set as
63   // __proto__ of this FunctionTemplate's instance prototype. Is either a
64   // FunctionTemplateInfo or Undefined.
65   DECL_RARE_ACCESSORS(parent_template, ParentTemplate, HeapObject)
66 
67   // Returns an InterceptorInfo or Undefined for named properties.
68   DECL_RARE_ACCESSORS(named_property_handler, NamedPropertyHandler, HeapObject)
69   // Returns an InterceptorInfo or Undefined for indexed properties/elements.
70   DECL_RARE_ACCESSORS(indexed_property_handler, IndexedPropertyHandler,
71                       HeapObject)
72 
73   // An ObjectTemplateInfo that is used when instantiating the JSFunction
74   // associated with this FunctionTemplateInfo. Contains either an
75   // ObjectTemplateInfo or Undefined. A default instance_template is assigned
76   // upon first instantiation if it's Undefined.
77   DECL_RARE_ACCESSORS(instance_template, InstanceTemplate, HeapObject)
78 
79   // Either a CallHandlerInfo or Undefined. If an instance_call_handler is
80   // provided the instances created from the associated JSFunction are marked as
81   // callable.
82   DECL_RARE_ACCESSORS(instance_call_handler, InstanceCallHandler, HeapObject)
83 
84   DECL_RARE_ACCESSORS(access_check_info, AccessCheckInfo, HeapObject)
85 
86   DECL_RARE_ACCESSORS(c_function, CFunction, Object)
87   DECL_RARE_ACCESSORS(c_signature, CSignature, Object)
88 #undef DECL_RARE_ACCESSORS
89 
90   DECL_RELEASE_ACQUIRE_ACCESSORS(call_code, HeapObject)
91 
92   // Begin flag bits ---------------------
93   DECL_BOOLEAN_ACCESSORS(undetectable)
94 
95   // If set, object instances created by this function
96   // requires access check.
97   DECL_BOOLEAN_ACCESSORS(needs_access_check)
98 
99   DECL_BOOLEAN_ACCESSORS(read_only_prototype)
100 
101   // If set, do not create a prototype property for the associated
102   // JSFunction. This bit implies that neither the prototype_template nor the
103   // prototype_provoider_template are instantiated.
104   DECL_BOOLEAN_ACCESSORS(remove_prototype)
105 
106   // If set, do not attach a serial number to this FunctionTemplate and thus do
107   // not keep an instance boilerplate around.
108   DECL_BOOLEAN_ACCESSORS(do_not_cache)
109 
110   // If not set an access may be performed on calling the associated JSFunction.
111   DECL_BOOLEAN_ACCESSORS(accept_any_receiver)
112   // End flag bits ---------------------
113 
114   // Dispatched behavior.
115   DECL_PRINTER(FunctionTemplateInfo)
116 
117   static const int kInvalidSerialNumber = 0;
118 
119   static Handle<SharedFunctionInfo> GetOrCreateSharedFunctionInfo(
120       Isolate* isolate, Handle<FunctionTemplateInfo> info,
121       MaybeHandle<Name> maybe_name);
122 
GetOrCreateSharedFunctionInfo(LocalIsolate * isolate,Handle<FunctionTemplateInfo> info,Handle<Name> maybe_name)123   static Handle<SharedFunctionInfo> GetOrCreateSharedFunctionInfo(
124       LocalIsolate* isolate, Handle<FunctionTemplateInfo> info,
125       Handle<Name> maybe_name) {
126     // We don't support streaming compilation of scripts with natives, so we
127     // don't need an off-thread implementation of this.
128     UNREACHABLE();
129   }
130 
131   // Returns parent function template or a null FunctionTemplateInfo.
132   inline FunctionTemplateInfo GetParent(Isolate* isolate);
133   // Returns true if |object| is an instance of this function template.
134   inline bool IsTemplateFor(JSObject object);
135   bool IsTemplateFor(Map map);
136   inline bool instantiated();
137 
138   inline bool BreakAtEntry();
139 
140   // Helper function for cached accessors.
141   static MaybeHandle<Name> TryGetCachedPropertyName(Isolate* isolate,
142                                                     Handle<Object> getter);
143 
144   // Bit position in the flag, from least significant bit position.
145   DEFINE_TORQUE_GENERATED_FUNCTION_TEMPLATE_INFO_FLAGS()
146 
147  private:
148   static inline FunctionTemplateRareData EnsureFunctionTemplateRareData(
149       Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info);
150 
151   static FunctionTemplateRareData AllocateFunctionTemplateRareData(
152       Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info);
153 
154   TQ_OBJECT_CONSTRUCTORS(FunctionTemplateInfo)
155 };
156 
157 class ObjectTemplateInfo
158     : public TorqueGeneratedObjectTemplateInfo<ObjectTemplateInfo,
159                                                TemplateInfo> {
160  public:
161   DECL_INT_ACCESSORS(embedder_field_count)
162   DECL_BOOLEAN_ACCESSORS(immutable_proto)
163   DECL_BOOLEAN_ACCESSORS(code_like)
164 
165   // Dispatched behavior.
166   DECL_PRINTER(ObjectTemplateInfo)
167 
168   // Starting from given object template's constructor walk up the inheritance
169   // chain till a function template that has an instance template is found.
170   inline ObjectTemplateInfo GetParent(Isolate* isolate);
171 
172  private:
173   DEFINE_TORQUE_GENERATED_OBJECT_TEMPLATE_INFO_FLAGS()
174 
175   TQ_OBJECT_CONSTRUCTORS(ObjectTemplateInfo)
176 };
177 
178 }  // namespace internal
179 }  // namespace v8
180 
181 #include "src/objects/object-macros-undef.h"
182 
183 #endif  // V8_OBJECTS_TEMPLATES_H_
184