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/objects/templates.h"
9
10 #include "src/heap/heap-write-barrier-inl.h"
11 #include "src/objects/oddball.h"
12 #include "src/objects/shared-function-info-inl.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, do_not_cache, DoNotCacheBit::kShift)
38 BOOL_ACCESSORS(FunctionTemplateInfo, flag, accept_any_receiver,
39 AcceptAnyReceiverBit::kShift)
40
41 RELEASE_ACQUIRE_ACCESSORS(FunctionTemplateInfo, call_code, HeapObject,
42 kCallCodeOffset)
43
44 // static
45 FunctionTemplateRareData FunctionTemplateInfo::EnsureFunctionTemplateRareData(
46 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info) {
47 HeapObject extra = function_template_info->rare_data(isolate);
48 if (extra.IsUndefined(isolate)) {
49 return AllocateFunctionTemplateRareData(isolate, function_template_info);
50 } else {
51 return FunctionTemplateRareData::cast(extra);
52 }
53 }
54
55 #define RARE_ACCESSORS(Name, CamelName, Type, Default) \
56 DEF_GETTER(FunctionTemplateInfo, Get##CamelName, Type) { \
57 HeapObject extra = rare_data(isolate); \
58 HeapObject undefined = GetReadOnlyRoots(isolate).undefined_value(); \
59 return extra == undefined ? Default \
60 : FunctionTemplateRareData::cast(extra).Name(); \
61 } \
62 inline void FunctionTemplateInfo::Set##CamelName( \
63 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info, \
64 Handle<Type> Name) { \
65 FunctionTemplateRareData rare_data = \
66 EnsureFunctionTemplateRareData(isolate, function_template_info); \
67 rare_data.set_##Name(*Name); \
68 }
69
RARE_ACCESSORS(prototype_template,PrototypeTemplate,HeapObject,undefined)70 RARE_ACCESSORS(prototype_template, PrototypeTemplate, HeapObject, undefined)
71 RARE_ACCESSORS(prototype_provider_template, PrototypeProviderTemplate,
72 HeapObject, undefined)
73 RARE_ACCESSORS(parent_template, ParentTemplate, HeapObject, undefined)
74 RARE_ACCESSORS(named_property_handler, NamedPropertyHandler, HeapObject,
75 undefined)
76 RARE_ACCESSORS(indexed_property_handler, IndexedPropertyHandler, HeapObject,
77 undefined)
78 RARE_ACCESSORS(instance_template, InstanceTemplate, HeapObject, undefined)
79 RARE_ACCESSORS(instance_call_handler, InstanceCallHandler, HeapObject,
80 undefined)
81 RARE_ACCESSORS(access_check_info, AccessCheckInfo, HeapObject, undefined)
82 RARE_ACCESSORS(c_function, CFunction, Object, Smi(0))
83 RARE_ACCESSORS(c_signature, CSignature, Object, Smi(0))
84 #undef RARE_ACCESSORS
85
86 bool FunctionTemplateInfo::instantiated() {
87 return shared_function_info().IsSharedFunctionInfo();
88 }
89
BreakAtEntry()90 bool FunctionTemplateInfo::BreakAtEntry() {
91 Object maybe_shared = shared_function_info();
92 if (maybe_shared.IsSharedFunctionInfo()) {
93 SharedFunctionInfo shared = SharedFunctionInfo::cast(maybe_shared);
94 return shared.BreakAtEntry();
95 }
96 return false;
97 }
98
GetParent(Isolate * isolate)99 FunctionTemplateInfo FunctionTemplateInfo::GetParent(Isolate* isolate) {
100 Object parent = GetParentTemplate();
101 return parent.IsUndefined(isolate) ? FunctionTemplateInfo()
102 : FunctionTemplateInfo::cast(parent);
103 }
104
GetParent(Isolate * isolate)105 ObjectTemplateInfo ObjectTemplateInfo::GetParent(Isolate* isolate) {
106 Object maybe_ctor = constructor();
107 if (maybe_ctor.IsUndefined(isolate)) return ObjectTemplateInfo();
108 FunctionTemplateInfo constructor = FunctionTemplateInfo::cast(maybe_ctor);
109 while (true) {
110 constructor = constructor.GetParent(isolate);
111 if (constructor.is_null()) return ObjectTemplateInfo();
112 Object maybe_obj = constructor.GetInstanceTemplate();
113 if (!maybe_obj.IsUndefined(isolate)) {
114 return ObjectTemplateInfo::cast(maybe_obj);
115 }
116 }
117 return ObjectTemplateInfo();
118 }
119
embedder_field_count()120 int ObjectTemplateInfo::embedder_field_count() const {
121 return EmbedderFieldCountBits::decode(data());
122 }
123
set_embedder_field_count(int count)124 void ObjectTemplateInfo::set_embedder_field_count(int count) {
125 DCHECK_LE(count, JSObject::kMaxEmbedderFields);
126 return set_data(EmbedderFieldCountBits::update(data(), count));
127 }
128
immutable_proto()129 bool ObjectTemplateInfo::immutable_proto() const {
130 return IsImmutablePrototypeBit::decode(data());
131 }
132
set_immutable_proto(bool immutable)133 void ObjectTemplateInfo::set_immutable_proto(bool immutable) {
134 return set_data(IsImmutablePrototypeBit::update(data(), immutable));
135 }
136
code_like()137 bool ObjectTemplateInfo::code_like() const {
138 return IsCodeKindBit::decode(data());
139 }
140
set_code_like(bool is_code_like)141 void ObjectTemplateInfo::set_code_like(bool is_code_like) {
142 return set_data(IsCodeKindBit::update(data(), is_code_like));
143 }
144
IsTemplateFor(JSObject object)145 bool FunctionTemplateInfo::IsTemplateFor(JSObject object) {
146 return IsTemplateFor(object.map());
147 }
148
149 } // namespace internal
150 } // namespace v8
151
152 #include "src/objects/object-macros-undef.h"
153
154 #endif // V8_OBJECTS_TEMPLATES_INL_H_
155