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/objects-inl.h"
9 : #include "src/objects/frame-array-inl.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 : #define CHECK_CALLSITE(recv, method) \
15 : CHECK_RECEIVER(JSObject, recv, method); \
16 : if (!JSReceiver::HasOwnProperty( \
17 : recv, isolate->factory()->call_site_frame_array_symbol()) \
18 : .FromMaybe(false)) { \
19 : THROW_NEW_ERROR_RETURN_FAILURE( \
20 : isolate, \
21 : NewTypeError(MessageTemplate::kCallSiteMethod, \
22 : isolate->factory()->NewStringFromAsciiChecked(method))); \
23 : }
24 :
25 : namespace {
26 :
27 62362 : Object PositiveNumberOrNull(int value, Isolate* isolate) {
28 124670 : if (value >= 0) return *isolate->factory()->NewNumberFromInt(value);
29 54 : return ReadOnlyRoots(isolate).null_value();
30 : }
31 :
32 303451 : Handle<FrameArray> GetFrameArray(Isolate* isolate, Handle<JSObject> object) {
33 : Handle<Object> frame_array_obj = JSObject::GetDataProperty(
34 303451 : object, isolate->factory()->call_site_frame_array_symbol());
35 303451 : return Handle<FrameArray>::cast(frame_array_obj);
36 : }
37 :
38 303451 : int GetFrameIndex(Isolate* isolate, Handle<JSObject> object) {
39 : Handle<Object> frame_index_obj = JSObject::GetDataProperty(
40 303451 : object, isolate->factory()->call_site_frame_index_symbol());
41 303451 : return Smi::ToInt(*frame_index_obj);
42 : }
43 :
44 : } // namespace
45 :
46 123968 : BUILTIN(CallSitePrototypeGetColumnNumber) {
47 : HandleScope scope(isolate);
48 123968 : CHECK_CALLSITE(recv, "getColumnNumber");
49 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
50 30992 : GetFrameIndex(isolate, recv));
51 30992 : return PositiveNumberOrNull(it.Frame()->GetColumnNumber(), isolate);
52 : }
53 :
54 660 : BUILTIN(CallSitePrototypeGetEvalOrigin) {
55 : HandleScope scope(isolate);
56 660 : CHECK_CALLSITE(recv, "getEvalOrigin");
57 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
58 165 : GetFrameIndex(isolate, recv));
59 330 : return *it.Frame()->GetEvalOrigin();
60 : }
61 :
62 215368 : BUILTIN(CallSitePrototypeGetFileName) {
63 : HandleScope scope(isolate);
64 215368 : CHECK_CALLSITE(recv, "getFileName");
65 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
66 53842 : GetFrameIndex(isolate, recv));
67 107684 : return *it.Frame()->GetFileName();
68 : }
69 :
70 1980 : BUILTIN(CallSitePrototypeGetFunction) {
71 : HandleScope scope(isolate);
72 1980 : CHECK_CALLSITE(recv, "getFunction");
73 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
74 495 : GetFrameIndex(isolate, recv));
75 :
76 495 : StackFrameBase* frame = it.Frame();
77 594 : if (frame->IsStrict()) return ReadOnlyRoots(isolate).undefined_value();
78 792 : return *frame->GetFunction();
79 : }
80 :
81 126060 : BUILTIN(CallSitePrototypeGetFunctionName) {
82 : HandleScope scope(isolate);
83 126060 : CHECK_CALLSITE(recv, "getFunctionName");
84 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
85 31515 : GetFrameIndex(isolate, recv));
86 63030 : return *it.Frame()->GetFunctionName();
87 : }
88 :
89 125372 : BUILTIN(CallSitePrototypeGetLineNumber) {
90 : HandleScope scope(isolate);
91 125372 : CHECK_CALLSITE(recv, "getLineNumber");
92 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
93 31343 : GetFrameIndex(isolate, recv));
94 31343 : return PositiveNumberOrNull(it.Frame()->GetLineNumber(), isolate);
95 : }
96 :
97 468 : BUILTIN(CallSitePrototypeGetMethodName) {
98 : HandleScope scope(isolate);
99 531 : CHECK_CALLSITE(recv, "getMethodName");
100 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
101 90 : GetFrameIndex(isolate, recv));
102 180 : return *it.Frame()->GetMethodName();
103 : }
104 :
105 900 : BUILTIN(CallSitePrototypeGetPosition) {
106 : HandleScope scope(isolate);
107 900 : CHECK_CALLSITE(recv, "getPosition");
108 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
109 225 : GetFrameIndex(isolate, recv));
110 450 : return Smi::FromInt(it.Frame()->GetPosition());
111 : }
112 :
113 108 : BUILTIN(CallSitePrototypeGetPromiseIndex) {
114 : HandleScope scope(isolate);
115 108 : CHECK_CALLSITE(recv, "getPromiseIndex");
116 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
117 27 : GetFrameIndex(isolate, recv));
118 27 : return PositiveNumberOrNull(it.Frame()->GetPromiseIndex(), isolate);
119 : }
120 :
121 0 : BUILTIN(CallSitePrototypeGetScriptNameOrSourceURL) {
122 : HandleScope scope(isolate);
123 0 : CHECK_CALLSITE(recv, "getScriptNameOrSourceUrl");
124 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
125 0 : GetFrameIndex(isolate, recv));
126 0 : return *it.Frame()->GetScriptNameOrSourceUrl();
127 : }
128 :
129 2052 : BUILTIN(CallSitePrototypeGetThis) {
130 : HandleScope scope(isolate);
131 2052 : CHECK_CALLSITE(recv, "getThis");
132 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
133 513 : GetFrameIndex(isolate, recv));
134 :
135 513 : StackFrameBase* frame = it.Frame();
136 585 : if (frame->IsStrict()) return ReadOnlyRoots(isolate).undefined_value();
137 882 : return *frame->GetReceiver();
138 : }
139 :
140 1728 : BUILTIN(CallSitePrototypeGetTypeName) {
141 : HandleScope scope(isolate);
142 1728 : CHECK_CALLSITE(recv, "getTypeName");
143 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
144 432 : GetFrameIndex(isolate, recv));
145 864 : return *it.Frame()->GetTypeName();
146 : }
147 :
148 180 : BUILTIN(CallSitePrototypeIsAsync) {
149 : HandleScope scope(isolate);
150 180 : CHECK_CALLSITE(recv, "isAsync");
151 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
152 45 : GetFrameIndex(isolate, recv));
153 45 : return isolate->heap()->ToBoolean(it.Frame()->IsAsync());
154 : }
155 :
156 122352 : BUILTIN(CallSitePrototypeIsConstructor) {
157 : HandleScope scope(isolate);
158 122352 : CHECK_CALLSITE(recv, "isConstructor");
159 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
160 30588 : GetFrameIndex(isolate, recv));
161 30588 : return isolate->heap()->ToBoolean(it.Frame()->IsConstructor());
162 : }
163 :
164 244808 : BUILTIN(CallSitePrototypeIsEval) {
165 : HandleScope scope(isolate);
166 244808 : CHECK_CALLSITE(recv, "isEval");
167 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
168 61202 : GetFrameIndex(isolate, recv));
169 61202 : return isolate->heap()->ToBoolean(it.Frame()->IsEval());
170 : }
171 :
172 122316 : BUILTIN(CallSitePrototypeIsNative) {
173 : HandleScope scope(isolate);
174 122316 : CHECK_CALLSITE(recv, "isNative");
175 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
176 30579 : GetFrameIndex(isolate, recv));
177 30579 : return isolate->heap()->ToBoolean(it.Frame()->IsNative());
178 : }
179 :
180 72 : BUILTIN(CallSitePrototypeIsPromiseAll) {
181 : HandleScope scope(isolate);
182 72 : CHECK_CALLSITE(recv, "isPromiseAll");
183 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
184 18 : GetFrameIndex(isolate, recv));
185 18 : return isolate->heap()->ToBoolean(it.Frame()->IsPromiseAll());
186 : }
187 :
188 122316 : BUILTIN(CallSitePrototypeIsToplevel) {
189 : HandleScope scope(isolate);
190 122316 : CHECK_CALLSITE(recv, "isToplevel");
191 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
192 30579 : GetFrameIndex(isolate, recv));
193 30579 : return isolate->heap()->ToBoolean(it.Frame()->IsToplevel());
194 : }
195 :
196 3204 : BUILTIN(CallSitePrototypeToString) {
197 : HandleScope scope(isolate);
198 3204 : CHECK_CALLSITE(recv, "toString");
199 : FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
200 801 : GetFrameIndex(isolate, recv));
201 1602 : RETURN_RESULT_OR_FAILURE(isolate, it.Frame()->ToString());
202 : }
203 :
204 : #undef CHECK_CALLSITE
205 :
206 : } // namespace internal
207 183867 : } // namespace v8
|