forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall-optimization.cc
More file actions
150 lines (134 loc) · 5.17 KB
/
Copy pathcall-optimization.cc
File metadata and controls
150 lines (134 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/ic/call-optimization.h"
#include <optional>
#include "src/objects/objects-inl.h"
namespace v8 {
namespace internal {
template <class IsolateT>
CallOptimization::CallOptimization(IsolateT* isolate, Handle<Object> function) {
if (IsJSFunction(*function)) {
Initialize(isolate, Cast<JSFunction>(function));
} else if (IsFunctionTemplateInfo(*function)) {
Initialize(isolate, Cast<FunctionTemplateInfo>(function));
}
}
// Instantiations.
template CallOptimization::CallOptimization(Isolate* isolate,
Handle<Object> function);
template CallOptimization::CallOptimization(LocalIsolate* isolate,
Handle<Object> function);
std::optional<Tagged<NativeContext>> CallOptimization::GetAccessorContext(
Tagged<Map> holder_map) const {
if (is_constant_call()) {
return constant_function_->native_context();
}
Tagged<Object> maybe_native_context =
holder_map->map()->native_context_or_null();
if (IsNull(maybe_native_context)) {
// The holder is a remote object which doesn't have a creation context.
return {};
}
DCHECK(IsNativeContext(maybe_native_context));
return Cast<NativeContext>(maybe_native_context);
}
bool CallOptimization::IsCrossContextLazyAccessorPair(
Tagged<NativeContext> native_context, Tagged<Map> holder_map) const {
DCHECK(IsNativeContext(native_context));
if (is_constant_call()) return false;
std::optional<Tagged<NativeContext>> maybe_context =
GetAccessorContext(holder_map);
if (!maybe_context.has_value()) {
// The holder is a remote object which doesn't have a creation context.
return true;
}
return native_context != maybe_context.value();
}
template <class IsolateT>
Handle<JSObject> CallOptimization::LookupHolderOfExpectedType(
IsolateT* isolate, DirectHandle<Map> object_map,
HolderLookup* holder_lookup) const {
DCHECK(is_simple_api_call());
if (!IsJSObjectMap(*object_map)) {
*holder_lookup = kHolderNotFound;
return Handle<JSObject>::null();
}
if (expected_receiver_type_.is_null() ||
expected_receiver_type_->IsTemplateFor(*object_map)) {
*holder_lookup = kHolderIsReceiver;
return Handle<JSObject>::null();
}
if (IsJSGlobalProxyMap(*object_map) && !IsNull(object_map->prototype())) {
Tagged<JSObject> raw_prototype = Cast<JSObject>(object_map->prototype());
Handle<JSObject> prototype(raw_prototype, isolate);
object_map = direct_handle(prototype->map(), isolate);
if (expected_receiver_type_->IsTemplateFor(*object_map)) {
*holder_lookup = kHolderFound;
return prototype;
}
}
*holder_lookup = kHolderNotFound;
return Handle<JSObject>::null();
}
// Instantiations.
template Handle<JSObject> CallOptimization::LookupHolderOfExpectedType(
Isolate* isolate, DirectHandle<Map> object_map,
HolderLookup* holder_lookup) const;
template Handle<JSObject> CallOptimization::LookupHolderOfExpectedType(
LocalIsolate* isolate, DirectHandle<Map> object_map,
HolderLookup* holder_lookup) const;
bool CallOptimization::IsCompatibleReceiverMap(
DirectHandle<JSObject> api_holder, Handle<JSObject> holder,
HolderLookup holder_lookup) const {
DCHECK(is_simple_api_call());
switch (holder_lookup) {
case kHolderNotFound:
return false;
case kHolderIsReceiver:
return true;
case kHolderFound:
if (api_holder.is_identical_to(holder)) return true;
// Check if holder is in prototype chain of api_holder.
{
Tagged<JSObject> object = *api_holder;
while (true) {
Tagged<Object> prototype = object->map()->prototype();
if (!IsJSObject(prototype)) return false;
if (prototype == *holder) return true;
object = Cast<JSObject>(prototype);
}
}
}
UNREACHABLE();
}
template <class IsolateT>
void CallOptimization::Initialize(
IsolateT* isolate, Handle<FunctionTemplateInfo> function_template_info) {
if (!function_template_info->has_callback(isolate)) return;
api_call_info_ = function_template_info;
Tagged<HeapObject> signature = function_template_info->signature();
if (!IsUndefined(signature, isolate)) {
expected_receiver_type_ =
handle(Cast<FunctionTemplateInfo>(signature), isolate);
}
is_simple_api_call_ = true;
accept_any_receiver_ = function_template_info->accept_any_receiver();
}
template <class IsolateT>
void CallOptimization::Initialize(IsolateT* isolate,
Handle<JSFunction> function) {
if (function.is_null() || !function->is_compiled(isolate)) return;
constant_function_ = function;
AnalyzePossibleApiFunction(isolate, function);
}
template <class IsolateT>
void CallOptimization::AnalyzePossibleApiFunction(
IsolateT* isolate, DirectHandle<JSFunction> function) {
if (!function->shared()->IsApiFunction()) return;
Handle<FunctionTemplateInfo> function_template_info(
function->shared()->api_func_data(), isolate);
Initialize(isolate, function_template_info);
}
} // namespace internal
} // namespace v8