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/heap/heap-write-barrier-inl.h" 9#include "src/objects/objects-inl.h" 10#include "src/objects/oddball.h" 11#include "src/objects/shared-function-info.h" 12#include "src/objects/templates.h" 13 14// Has to be the last include (doesn't have include guards): 15#include "src/objects/object-macros.h" 16 17namespace v8 { 18namespace internal { 19 20#include "torque-generated/src/objects/templates-tq-inl.inc" 21 22TQ_OBJECT_CONSTRUCTORS_IMPL(TemplateInfo) 23TQ_OBJECT_CONSTRUCTORS_IMPL(FunctionTemplateInfo) 24TQ_OBJECT_CONSTRUCTORS_IMPL(ObjectTemplateInfo) 25TQ_OBJECT_CONSTRUCTORS_IMPL(FunctionTemplateRareData) 26 27NEVER_READ_ONLY_SPACE_IMPL(TemplateInfo) 28 29BOOL_ACCESSORS(FunctionTemplateInfo, flag, undetectable, 30 UndetectableBit::kShift) 31BOOL_ACCESSORS(FunctionTemplateInfo, flag, needs_access_check, 32 NeedsAccessCheckBit::kShift) 33BOOL_ACCESSORS(FunctionTemplateInfo, flag, read_only_prototype, 34 ReadOnlyPrototypeBit::kShift) 35BOOL_ACCESSORS(FunctionTemplateInfo, flag, remove_prototype, 36 RemovePrototypeBit::kShift) 37BOOL_ACCESSORS(FunctionTemplateInfo, flag, accept_any_receiver, 38 AcceptAnyReceiverBit::kShift) 39BOOL_ACCESSORS(FunctionTemplateInfo, flag, published, PublishedBit::kShift) 40 41BIT_FIELD_ACCESSORS( 42 FunctionTemplateInfo, flag, allowed_receiver_instance_type_range_start, 43 FunctionTemplateInfo::AllowedReceiverInstanceTypeRangeStartBits) 44BIT_FIELD_ACCESSORS( 45 FunctionTemplateInfo, flag, allowed_receiver_instance_type_range_end, 46 FunctionTemplateInfo::AllowedReceiverInstanceTypeRangeEndBits) 47 48// static 49FunctionTemplateRareData FunctionTemplateInfo::EnsureFunctionTemplateRareData( 50 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info) { 51 HeapObject extra = function_template_info->rare_data(isolate, kAcquireLoad); 52 if (extra.IsUndefined(isolate)) { 53 return AllocateFunctionTemplateRareData(isolate, function_template_info); 54 } else { 55 return FunctionTemplateRareData::cast(extra); 56 } 57} 58 59#define RARE_ACCESSORS(Name, CamelName, Type, Default) \ 60 DEF_GETTER(FunctionTemplateInfo, Get##CamelName, Type) { \ 61 HeapObject extra = rare_data(cage_base, kAcquireLoad); \ 62 HeapObject undefined = GetReadOnlyRoots(cage_base).undefined_value(); \ 63 return extra == undefined ? Default \ 64 : FunctionTemplateRareData::cast(extra).Name(); \ 65 } \ 66 inline void FunctionTemplateInfo::Set##CamelName( \ 67 Isolate* isolate, Handle<FunctionTemplateInfo> function_template_info, \ 68 Handle<Type> Name) { \ 69 FunctionTemplateRareData rare_data = \ 70 EnsureFunctionTemplateRareData(isolate, function_template_info); \ 71 rare_data.set_##Name(*Name); \ 72 } 73 74RARE_ACCESSORS(prototype_template, PrototypeTemplate, HeapObject, undefined) 75RARE_ACCESSORS(prototype_provider_template, PrototypeProviderTemplate, 76 HeapObject, undefined) 77RARE_ACCESSORS(parent_template, ParentTemplate, HeapObject, undefined) 78RARE_ACCESSORS(named_property_handler, NamedPropertyHandler, HeapObject, 79 undefined) 80RARE_ACCESSORS(indexed_property_handler, IndexedPropertyHandler, HeapObject, 81 undefined) 82RARE_ACCESSORS(instance_template, InstanceTemplate, HeapObject, undefined) 83RARE_ACCESSORS(instance_call_handler, InstanceCallHandler, HeapObject, 84 undefined) 85RARE_ACCESSORS(access_check_info, AccessCheckInfo, HeapObject, undefined) 86RARE_ACCESSORS(c_function_overloads, CFunctionOverloads, FixedArray, 87 GetReadOnlyRoots(cage_base).empty_fixed_array()) 88#undef RARE_ACCESSORS 89 90int FunctionTemplateInfo::InstanceType() const { 91 int type = instance_type(); 92 DCHECK(type == kNoJSApiObjectType || 93 (type >= Internals::kFirstJSApiObjectType && 94 type <= Internals::kLastJSApiObjectType)); 95 return type; 96} 97 98void FunctionTemplateInfo::SetInstanceType(int instance_type) { 99 if (instance_type == 0) { 100 set_instance_type(kNoJSApiObjectType); 101 } else { 102 DCHECK_GT(instance_type, 0); 103 DCHECK_LT(Internals::kFirstJSApiObjectType + instance_type, 104 Internals::kLastJSApiObjectType); 105 set_instance_type(Internals::kFirstJSApiObjectType + instance_type); 106 } 107} 108 109bool TemplateInfo::should_cache() const { 110 return serial_number() != kDoNotCache; 111} 112bool TemplateInfo::is_cached() const { return serial_number() > kUncached; } 113 114bool FunctionTemplateInfo::instantiated() { 115 return shared_function_info().IsSharedFunctionInfo(); 116} 117 118inline bool FunctionTemplateInfo::BreakAtEntry() { 119 Object maybe_shared = shared_function_info(); 120 if (maybe_shared.IsSharedFunctionInfo()) { 121 SharedFunctionInfo shared = SharedFunctionInfo::cast(maybe_shared); 122 return shared.BreakAtEntry(); 123 } 124 return false; 125} 126 127FunctionTemplateInfo FunctionTemplateInfo::GetParent(Isolate* isolate) { 128 Object parent = GetParentTemplate(); 129 return parent.IsUndefined(isolate) ? FunctionTemplateInfo() 130 : FunctionTemplateInfo::cast(parent); 131} 132 133ObjectTemplateInfo ObjectTemplateInfo::GetParent(Isolate* isolate) { 134 Object maybe_ctor = constructor(); 135 if (maybe_ctor.IsUndefined(isolate)) return ObjectTemplateInfo(); 136 FunctionTemplateInfo constructor = FunctionTemplateInfo::cast(maybe_ctor); 137 while (true) { 138 constructor = constructor.GetParent(isolate); 139 if (constructor.is_null()) return ObjectTemplateInfo(); 140 Object maybe_obj = constructor.GetInstanceTemplate(); 141 if (!maybe_obj.IsUndefined(isolate)) { 142 return ObjectTemplateInfo::cast(maybe_obj); 143 } 144 } 145 return ObjectTemplateInfo(); 146} 147 148int ObjectTemplateInfo::embedder_field_count() const { 149 return EmbedderFieldCountBits::decode(data()); 150} 151 152void ObjectTemplateInfo::set_embedder_field_count(int count) { 153 DCHECK_LE(count, JSObject::kMaxEmbedderFields); 154 return set_data(EmbedderFieldCountBits::update(data(), count)); 155} 156 157bool ObjectTemplateInfo::immutable_proto() const { 158 return IsImmutablePrototypeBit::decode(data()); 159} 160 161void ObjectTemplateInfo::set_immutable_proto(bool immutable) { 162 return set_data(IsImmutablePrototypeBit::update(data(), immutable)); 163} 164 165bool ObjectTemplateInfo::code_like() const { 166 return IsCodeKindBit::decode(data()); 167} 168 169void ObjectTemplateInfo::set_code_like(bool is_code_like) { 170 return set_data(IsCodeKindBit::update(data(), is_code_like)); 171} 172 173bool FunctionTemplateInfo::IsTemplateFor(JSObject object) { 174 return IsTemplateFor(object.map()); 175} 176 177} // namespace internal 178} // namespace v8 179 180#include "src/objects/object-macros-undef.h" 181 182#endif // V8_OBJECTS_TEMPLATES_INL_H_ 183