Line data Source code
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 :
5 : #include "src/builtins/builtins-utils-inl.h"
6 : #include "src/builtins/builtins.h"
7 : #include "src/counters.h"
8 : #include "src/heap/heap-inl.h" // For ToBoolean.
9 : #include "src/objects-inl.h"
10 : #include "src/objects/frame-array-inl.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : #define CHECK_CALLSITE(recv, method) \
16 : CHECK_RECEIVER(JSObject, recv, method); \
17 : if (!JSReceiver::HasOwnProperty( \
18 : recv, isolate->factory()->call_site_frame_array_symbol()) \
19 : .FromMaybe(false)) { \
20 : THROW_NEW_ERROR_RETURN_FAILURE( \
21 : isolate, \
22 : NewTypeError(MessageTemplate::kCallSiteMethod, \
23 : isolate->factory()->NewStringFromAsciiChecked(method))); \
24 : }
25 :
26 : namespace {
27 :
28 63210 : Object PositiveNumberOrNull(int value, Isolate* isolate) {
29 126366 : if (value >= 0) return *isolate->factory()->NewNumberFromInt(value);
30 54 : return ReadOnlyRoots(isolate).null_value();
31 : }
32 :
33 : Handle<FrameArray> GetFrameArray(Isolate* isolate, Handle<JSObject> object) {
34 : Handle<Object> frame_array_obj = JSObject::GetDataProperty(
35 308085 : object, isolate->factory()->call_site_frame_array_symbol());
36 : return Handle<FrameArray>::cast(frame_array_obj);
37 : }
38 :
39 : int GetFrameIndex(Isolate* isolate, Handle<JSObject> object) {
40 : Handle<Object> frame_index_obj = JSObject::GetDataProperty(
41 308085 : object, isolate->factory()->call_site_frame_index_symbol());
42 : return Smi::ToInt(*frame_index_obj);
43 : }
44 :
45 : } // namespace
46 :
47 157135 : BUILTIN(CallSitePrototypeGetColumnNumber) {
48 : HandleScope scope(isolate);
49 94281 : CHECK_CALLSITE(recv, "getColumnNumber");
50 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
51 31427 : GetFrameIndex(isolate, recv));
52 31427 : return PositiveNumberOrNull(it.Frame()->GetColumnNumber(), isolate);
53 : }
54 :
55 825 : BUILTIN(CallSitePrototypeGetEvalOrigin) {
56 : HandleScope scope(isolate);
57 495 : CHECK_CALLSITE(recv, "getEvalOrigin");
58 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
59 165 : GetFrameIndex(isolate, recv));
60 330 : return *it.Frame()->GetEvalOrigin();
61 : }
62 :
63 274020 : BUILTIN(CallSitePrototypeGetFileName) {
64 : HandleScope scope(isolate);
65 164412 : CHECK_CALLSITE(recv, "getFileName");
66 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
67 54804 : GetFrameIndex(isolate, recv));
68 109608 : return *it.Frame()->GetFileName();
69 : }
70 :
71 2475 : BUILTIN(CallSitePrototypeGetFunction) {
72 : HandleScope scope(isolate);
73 1485 : CHECK_CALLSITE(recv, "getFunction");
74 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
75 495 : GetFrameIndex(isolate, recv));
76 :
77 495 : StackFrameBase* frame = it.Frame();
78 594 : if (frame->IsStrict()) return ReadOnlyRoots(isolate).undefined_value();
79 792 : return *frame->GetFunction();
80 : }
81 :
82 159675 : BUILTIN(CallSitePrototypeGetFunctionName) {
83 : HandleScope scope(isolate);
84 95805 : CHECK_CALLSITE(recv, "getFunctionName");
85 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
86 31935 : GetFrameIndex(isolate, recv));
87 63870 : return *it.Frame()->GetFunctionName();
88 : }
89 :
90 158780 : BUILTIN(CallSitePrototypeGetLineNumber) {
91 : HandleScope scope(isolate);
92 95268 : CHECK_CALLSITE(recv, "getLineNumber");
93 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
94 31756 : GetFrameIndex(isolate, recv));
95 31756 : return PositiveNumberOrNull(it.Frame()->GetLineNumber(), isolate);
96 : }
97 :
98 585 : BUILTIN(CallSitePrototypeGetMethodName) {
99 : HandleScope scope(isolate);
100 414 : CHECK_CALLSITE(recv, "getMethodName");
101 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
102 90 : GetFrameIndex(isolate, recv));
103 180 : return *it.Frame()->GetMethodName();
104 : }
105 :
106 1000 : BUILTIN(CallSitePrototypeGetPosition) {
107 : HandleScope scope(isolate);
108 600 : CHECK_CALLSITE(recv, "getPosition");
109 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
110 200 : GetFrameIndex(isolate, recv));
111 400 : return Smi::FromInt(it.Frame()->GetPosition());
112 : }
113 :
114 135 : BUILTIN(CallSitePrototypeGetPromiseIndex) {
115 : HandleScope scope(isolate);
116 81 : CHECK_CALLSITE(recv, "getPromiseIndex");
117 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
118 27 : GetFrameIndex(isolate, recv));
119 27 : return PositiveNumberOrNull(it.Frame()->GetPromiseIndex(), isolate);
120 : }
121 :
122 0 : BUILTIN(CallSitePrototypeGetScriptNameOrSourceURL) {
123 : HandleScope scope(isolate);
124 0 : CHECK_CALLSITE(recv, "getScriptNameOrSourceUrl");
125 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
126 0 : GetFrameIndex(isolate, recv));
127 0 : return *it.Frame()->GetScriptNameOrSourceUrl();
128 : }
129 :
130 2355 : BUILTIN(CallSitePrototypeGetThis) {
131 : HandleScope scope(isolate);
132 1413 : CHECK_CALLSITE(recv, "getThis");
133 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
134 471 : GetFrameIndex(isolate, recv));
135 :
136 471 : StackFrameBase* frame = it.Frame();
137 543 : if (frame->IsStrict()) return ReadOnlyRoots(isolate).undefined_value();
138 798 : return *frame->GetReceiver();
139 : }
140 :
141 2660 : BUILTIN(CallSitePrototypeGetTypeName) {
142 : HandleScope scope(isolate);
143 1596 : CHECK_CALLSITE(recv, "getTypeName");
144 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
145 532 : GetFrameIndex(isolate, recv));
146 1064 : return *it.Frame()->GetTypeName();
147 : }
148 :
149 225 : BUILTIN(CallSitePrototypeIsAsync) {
150 : HandleScope scope(isolate);
151 135 : CHECK_CALLSITE(recv, "isAsync");
152 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
153 45 : GetFrameIndex(isolate, recv));
154 90 : return isolate->heap()->ToBoolean(it.Frame()->IsAsync());
155 : }
156 :
157 155400 : BUILTIN(CallSitePrototypeIsConstructor) {
158 : HandleScope scope(isolate);
159 93240 : CHECK_CALLSITE(recv, "isConstructor");
160 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
161 31080 : GetFrameIndex(isolate, recv));
162 62160 : return isolate->heap()->ToBoolean(it.Frame()->IsConstructor());
163 : }
164 :
165 310875 : BUILTIN(CallSitePrototypeIsEval) {
166 : HandleScope scope(isolate);
167 186525 : CHECK_CALLSITE(recv, "isEval");
168 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
169 62175 : GetFrameIndex(isolate, recv));
170 124350 : return isolate->heap()->ToBoolean(it.Frame()->IsEval());
171 : }
172 :
173 155355 : BUILTIN(CallSitePrototypeIsNative) {
174 : HandleScope scope(isolate);
175 93213 : CHECK_CALLSITE(recv, "isNative");
176 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
177 31071 : GetFrameIndex(isolate, recv));
178 62142 : return isolate->heap()->ToBoolean(it.Frame()->IsNative());
179 : }
180 :
181 90 : BUILTIN(CallSitePrototypeIsPromiseAll) {
182 : HandleScope scope(isolate);
183 54 : CHECK_CALLSITE(recv, "isPromiseAll");
184 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
185 18 : GetFrameIndex(isolate, recv));
186 36 : return isolate->heap()->ToBoolean(it.Frame()->IsPromiseAll());
187 : }
188 :
189 155355 : BUILTIN(CallSitePrototypeIsToplevel) {
190 : HandleScope scope(isolate);
191 93213 : CHECK_CALLSITE(recv, "isToplevel");
192 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
193 31071 : GetFrameIndex(isolate, recv));
194 62142 : return isolate->heap()->ToBoolean(it.Frame()->IsToplevel());
195 : }
196 :
197 3615 : BUILTIN(CallSitePrototypeToString) {
198 : HandleScope scope(isolate);
199 2169 : CHECK_CALLSITE(recv, "toString");
200 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
201 723 : GetFrameIndex(isolate, recv));
202 1446 : RETURN_RESULT_OR_FAILURE(isolate, it.Frame()->ToString());
203 : }
204 :
205 : #undef CHECK_CALLSITE
206 :
207 : } // namespace internal
208 120216 : } // namespace v8
|