v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
builtins-callsite.cc
Go to the documentation of this file.
1// Copyright 2016 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
7#include "src/heap/heap-inl.h" // For ToBoolean.
11
12namespace v8 {
13namespace internal {
14
15#define CHECK_CALLSITE(frame, method) \
16 CHECK_RECEIVER(JSObject, receiver, method); \
17 LookupIterator it(isolate, receiver, \
18 isolate->factory()->call_site_info_symbol(), \
19 LookupIterator::OWN_SKIP_INTERCEPTOR); \
20 if (it.state() != LookupIterator::DATA) { \
21 THROW_NEW_ERROR_RETURN_FAILURE( \
22 isolate, \
23 NewTypeError(MessageTemplate::kCallSiteMethod, \
24 isolate->factory()->NewStringFromAsciiChecked(method))); \
25 } \
26 auto frame = Cast<CallSiteInfo>(it.GetDataValue())
27
28namespace {
29
30Tagged<Object> PositiveNumberOrNull(int value, Isolate* isolate) {
31 if (value > 0) return *isolate->factory()->NewNumberFromInt(value);
32 return ReadOnlyRoots(isolate).null_value();
33}
34
35bool NativeContextIsForShadowRealm(Tagged<NativeContext> native_context) {
36 return native_context->scope_info()->scope_type() == SHADOW_REALM_SCOPE;
37}
38
39} // namespace
40
41BUILTIN(CallSitePrototypeGetColumnNumber) {
42 HandleScope scope(isolate);
43 CHECK_CALLSITE(frame, "getColumnNumber");
44 return PositiveNumberOrNull(CallSiteInfo::GetColumnNumber(frame), isolate);
45}
46
47BUILTIN(CallSitePrototypeGetEnclosingColumnNumber) {
48 HandleScope scope(isolate);
49 CHECK_CALLSITE(frame, "getEnclosingColumnNumber");
50 return PositiveNumberOrNull(CallSiteInfo::GetEnclosingColumnNumber(frame),
51 isolate);
52}
53
54BUILTIN(CallSitePrototypeGetEnclosingLineNumber) {
55 HandleScope scope(isolate);
56 CHECK_CALLSITE(frame, "getEnclosingLineNumber");
57 return PositiveNumberOrNull(CallSiteInfo::GetEnclosingLineNumber(frame),
58 isolate);
59}
60
61BUILTIN(CallSitePrototypeGetEvalOrigin) {
62 HandleScope scope(isolate);
63 CHECK_CALLSITE(frame, "getEvalOrigin");
64 return *CallSiteInfo::GetEvalOrigin(frame);
65}
66
67BUILTIN(CallSitePrototypeGetFileName) {
68 HandleScope scope(isolate);
69 CHECK_CALLSITE(frame, "getFileName");
70 return frame->GetScriptName();
71}
72
73BUILTIN(CallSitePrototypeGetFunction) {
74 static const char method_name[] = "getFunction";
75 HandleScope scope(isolate);
76 CHECK_CALLSITE(frame, method_name);
77 // ShadowRealms have a boundary: references to outside objects must not exist
78 // in the ShadowRealm, and references to ShadowRealm objects must not exist
79 // outside the ShadowRealm.
80 if (NativeContextIsForShadowRealm(isolate->raw_native_context()) ||
81 (IsJSFunction(frame->function()) &&
82 NativeContextIsForShadowRealm(
83 Cast<JSFunction>(frame->function())->native_context()))) {
85 isolate,
86 NewTypeError(
87 MessageTemplate::kCallSiteMethodUnsupportedInShadowRealm,
88 isolate->factory()->NewStringFromAsciiChecked(method_name)));
89 }
90 if (frame->IsStrict() ||
91 (IsJSFunction(frame->function()) &&
92 Cast<JSFunction>(frame->function())->shared()->is_toplevel())) {
93 return ReadOnlyRoots(isolate).undefined_value();
94 }
96 return frame->function();
97}
98
99BUILTIN(CallSitePrototypeGetFunctionName) {
100 HandleScope scope(isolate);
101 CHECK_CALLSITE(frame, "getFunctionName");
102 return *CallSiteInfo::GetFunctionName(frame);
103}
104
105BUILTIN(CallSitePrototypeGetLineNumber) {
106 HandleScope scope(isolate);
107 CHECK_CALLSITE(frame, "getLineNumber");
108 return PositiveNumberOrNull(CallSiteInfo::GetLineNumber(frame), isolate);
109}
110
111BUILTIN(CallSitePrototypeGetMethodName) {
112 HandleScope scope(isolate);
113 CHECK_CALLSITE(frame, "getMethodName");
114 return *CallSiteInfo::GetMethodName(frame);
115}
116
117BUILTIN(CallSitePrototypeGetPosition) {
118 HandleScope scope(isolate);
119 CHECK_CALLSITE(frame, "getPosition");
121}
122
123BUILTIN(CallSitePrototypeGetPromiseIndex) {
124 HandleScope scope(isolate);
125 CHECK_CALLSITE(frame, "getPromiseIndex");
126 if (!frame->IsPromiseAll() && !frame->IsPromiseAny() &&
127 !frame->IsPromiseAllSettled()) {
128 return ReadOnlyRoots(isolate).null_value();
129 }
131}
132
133BUILTIN(CallSitePrototypeGetScriptHash) {
134 HandleScope scope(isolate);
135 CHECK_CALLSITE(frame, "getScriptHash");
136 return *CallSiteInfo::GetScriptHash(frame);
137}
138
139BUILTIN(CallSitePrototypeGetScriptNameOrSourceURL) {
140 HandleScope scope(isolate);
141 CHECK_CALLSITE(frame, "getScriptNameOrSourceUrl");
142 return frame->GetScriptNameOrSourceURL();
143}
144
145BUILTIN(CallSitePrototypeGetThis) {
146 static const char method_name[] = "getThis";
147 HandleScope scope(isolate);
148 CHECK_CALLSITE(frame, method_name);
149 // ShadowRealms have a boundary: references to outside objects must not exist
150 // in the ShadowRealm, and references to ShadowRealm objects must not exist
151 // outside the ShadowRealm.
152 if (NativeContextIsForShadowRealm(isolate->raw_native_context()) ||
153 (IsJSFunction(frame->function()) &&
154 NativeContextIsForShadowRealm(
155 Cast<JSFunction>(frame->function())->native_context()))) {
157 isolate,
158 NewTypeError(
159 MessageTemplate::kCallSiteMethodUnsupportedInShadowRealm,
160 isolate->factory()->NewStringFromAsciiChecked(method_name)));
161 }
162 if (frame->IsStrict()) return ReadOnlyRoots(isolate).undefined_value();
164#if V8_ENABLE_WEBASSEMBLY
165 if (frame->IsAsmJsWasm()) {
166 return frame->GetWasmInstance()
167 ->trusted_data(isolate)
168 ->native_context()
169 ->global_proxy();
170 }
171#endif // V8_ENABLE_WEBASSEMBLY
172 return frame->receiver_or_instance();
173}
174
175BUILTIN(CallSitePrototypeGetTypeName) {
176 HandleScope scope(isolate);
177 CHECK_CALLSITE(frame, "getTypeName");
178 return *CallSiteInfo::GetTypeName(frame);
179}
180
181BUILTIN(CallSitePrototypeIsAsync) {
182 HandleScope scope(isolate);
183 CHECK_CALLSITE(frame, "isAsync");
184 return isolate->heap()->ToBoolean(frame->IsAsync());
185}
186
187BUILTIN(CallSitePrototypeIsConstructor) {
188 HandleScope scope(isolate);
189 CHECK_CALLSITE(frame, "isConstructor");
190 return isolate->heap()->ToBoolean(frame->IsConstructor());
191}
192
193BUILTIN(CallSitePrototypeIsEval) {
194 HandleScope scope(isolate);
195 CHECK_CALLSITE(frame, "isEval");
196 return isolate->heap()->ToBoolean(frame->IsEval());
197}
198
199BUILTIN(CallSitePrototypeIsNative) {
200 HandleScope scope(isolate);
201 CHECK_CALLSITE(frame, "isNative");
202 return isolate->heap()->ToBoolean(frame->IsNative());
203}
204
205BUILTIN(CallSitePrototypeIsPromiseAll) {
206 HandleScope scope(isolate);
207 CHECK_CALLSITE(frame, "isPromiseAll");
208 return isolate->heap()->ToBoolean(frame->IsPromiseAll());
209}
210
211BUILTIN(CallSitePrototypeIsToplevel) {
212 HandleScope scope(isolate);
213 CHECK_CALLSITE(frame, "isToplevel");
214 return isolate->heap()->ToBoolean(frame->IsToplevel());
215}
216
217BUILTIN(CallSitePrototypeToString) {
218 HandleScope scope(isolate);
219 CHECK_CALLSITE(frame, "toString");
220 RETURN_RESULT_OR_FAILURE(isolate, SerializeCallSiteInfo(isolate, frame));
221}
222
223#undef CHECK_CALLSITE
224
225} // namespace internal
226} // namespace v8
#define CHECK_CALLSITE(frame, method)
#define BUILTIN(name)
@ kCallSiteAPIGetFunctionSloppyCall
Definition v8-isolate.h:545
@ kCallSiteAPIGetThisSloppyCall
Definition v8-isolate.h:546
static DirectHandle< String > GetScriptHash(DirectHandle< CallSiteInfo > info)
static int GetEnclosingColumnNumber(DirectHandle< CallSiteInfo > info)
static V8_EXPORT_PRIVATE int GetLineNumber(DirectHandle< CallSiteInfo > info)
static Handle< PrimitiveHeapObject > GetEvalOrigin(DirectHandle< CallSiteInfo > info)
static V8_EXPORT_PRIVATE int GetColumnNumber(DirectHandle< CallSiteInfo > info)
static int GetEnclosingLineNumber(DirectHandle< CallSiteInfo > info)
static DirectHandle< Object > GetTypeName(DirectHandle< CallSiteInfo > info)
static int GetSourcePosition(DirectHandle< CallSiteInfo > info)
static V8_EXPORT_PRIVATE DirectHandle< PrimitiveHeapObject > GetFunctionName(DirectHandle< CallSiteInfo > info)
static DirectHandle< Object > GetMethodName(DirectHandle< CallSiteInfo > info)
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call)
Definition isolate.h:294
#define RETURN_RESULT_OR_FAILURE(isolate, call)
Definition isolate.h:264
@ SHADOW_REALM_SCOPE
Definition globals.h:1903
void SerializeCallSiteInfo(Isolate *isolate, DirectHandle< CallSiteInfo > frame, IncrementalStringBuilder *builder)
!IsContextMap !IsContextMap Tagged< NativeContext >
Definition map-inl.h:877
!IsContextMap !IsContextMap native_context
Definition map-inl.h:877
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150