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
PositiveNumberOrNull(int value,Isolate * isolate)27 Object* PositiveNumberOrNull(int value, Isolate* isolate) {
28 if (value >= 0) return *isolate->factory()->NewNumberFromInt(value);
29 return ReadOnlyRoots(isolate).null_value();
30 }
31
GetFrameArray(Isolate * isolate,Handle<JSObject> object)32 Handle<FrameArray> GetFrameArray(Isolate* isolate, Handle<JSObject> object) {
33 Handle<Object> frame_array_obj = JSObject::GetDataProperty(
34 object, isolate->factory()->call_site_frame_array_symbol());
35 return Handle<FrameArray>::cast(frame_array_obj);
36 }
37
GetFrameIndex(Isolate * isolate,Handle<JSObject> object)38 int GetFrameIndex(Isolate* isolate, Handle<JSObject> object) {
39 Handle<Object> frame_index_obj = JSObject::GetDataProperty(
40 object, isolate->factory()->call_site_frame_index_symbol());
41 return Smi::ToInt(*frame_index_obj);
42 }
43
44 } // namespace
45
BUILTIN(CallSitePrototypeGetColumnNumber)46 BUILTIN(CallSitePrototypeGetColumnNumber) {
47 HandleScope scope(isolate);
48 CHECK_CALLSITE(recv, "getColumnNumber");
49 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
50 GetFrameIndex(isolate, recv));
51 return PositiveNumberOrNull(it.Frame()->GetColumnNumber(), isolate);
52 }
53
BUILTIN(CallSitePrototypeGetEvalOrigin)54 BUILTIN(CallSitePrototypeGetEvalOrigin) {
55 HandleScope scope(isolate);
56 CHECK_CALLSITE(recv, "getEvalOrigin");
57 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
58 GetFrameIndex(isolate, recv));
59 return *it.Frame()->GetEvalOrigin();
60 }
61
BUILTIN(CallSitePrototypeGetFileName)62 BUILTIN(CallSitePrototypeGetFileName) {
63 HandleScope scope(isolate);
64 CHECK_CALLSITE(recv, "getFileName");
65 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
66 GetFrameIndex(isolate, recv));
67 return *it.Frame()->GetFileName();
68 }
69
BUILTIN(CallSitePrototypeGetFunction)70 BUILTIN(CallSitePrototypeGetFunction) {
71 HandleScope scope(isolate);
72 CHECK_CALLSITE(recv, "getFunction");
73 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
74 GetFrameIndex(isolate, recv));
75
76 StackFrameBase* frame = it.Frame();
77 if (frame->IsStrict()) return ReadOnlyRoots(isolate).undefined_value();
78 return *frame->GetFunction();
79 }
80
BUILTIN(CallSitePrototypeGetFunctionName)81 BUILTIN(CallSitePrototypeGetFunctionName) {
82 HandleScope scope(isolate);
83 CHECK_CALLSITE(recv, "getFunctionName");
84 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
85 GetFrameIndex(isolate, recv));
86 return *it.Frame()->GetFunctionName();
87 }
88
BUILTIN(CallSitePrototypeGetLineNumber)89 BUILTIN(CallSitePrototypeGetLineNumber) {
90 HandleScope scope(isolate);
91 CHECK_CALLSITE(recv, "getLineNumber");
92 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
93 GetFrameIndex(isolate, recv));
94 return PositiveNumberOrNull(it.Frame()->GetLineNumber(), isolate);
95 }
96
BUILTIN(CallSitePrototypeGetMethodName)97 BUILTIN(CallSitePrototypeGetMethodName) {
98 HandleScope scope(isolate);
99 CHECK_CALLSITE(recv, "getMethodName");
100 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
101 GetFrameIndex(isolate, recv));
102 return *it.Frame()->GetMethodName();
103 }
104
BUILTIN(CallSitePrototypeGetPosition)105 BUILTIN(CallSitePrototypeGetPosition) {
106 HandleScope scope(isolate);
107 CHECK_CALLSITE(recv, "getPosition");
108 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
109 GetFrameIndex(isolate, recv));
110 return Smi::FromInt(it.Frame()->GetPosition());
111 }
112
BUILTIN(CallSitePrototypeGetScriptNameOrSourceURL)113 BUILTIN(CallSitePrototypeGetScriptNameOrSourceURL) {
114 HandleScope scope(isolate);
115 CHECK_CALLSITE(recv, "getScriptNameOrSourceUrl");
116 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
117 GetFrameIndex(isolate, recv));
118 return *it.Frame()->GetScriptNameOrSourceUrl();
119 }
120
BUILTIN(CallSitePrototypeGetThis)121 BUILTIN(CallSitePrototypeGetThis) {
122 HandleScope scope(isolate);
123 CHECK_CALLSITE(recv, "getThis");
124 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
125 GetFrameIndex(isolate, recv));
126
127 StackFrameBase* frame = it.Frame();
128 if (frame->IsStrict()) return ReadOnlyRoots(isolate).undefined_value();
129 return *frame->GetReceiver();
130 }
131
BUILTIN(CallSitePrototypeGetTypeName)132 BUILTIN(CallSitePrototypeGetTypeName) {
133 HandleScope scope(isolate);
134 CHECK_CALLSITE(recv, "getTypeName");
135 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
136 GetFrameIndex(isolate, recv));
137 return *it.Frame()->GetTypeName();
138 }
139
BUILTIN(CallSitePrototypeIsConstructor)140 BUILTIN(CallSitePrototypeIsConstructor) {
141 HandleScope scope(isolate);
142 CHECK_CALLSITE(recv, "isConstructor");
143 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
144 GetFrameIndex(isolate, recv));
145 return isolate->heap()->ToBoolean(it.Frame()->IsConstructor());
146 }
147
BUILTIN(CallSitePrototypeIsEval)148 BUILTIN(CallSitePrototypeIsEval) {
149 HandleScope scope(isolate);
150 CHECK_CALLSITE(recv, "isEval");
151 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
152 GetFrameIndex(isolate, recv));
153 return isolate->heap()->ToBoolean(it.Frame()->IsEval());
154 }
155
BUILTIN(CallSitePrototypeIsNative)156 BUILTIN(CallSitePrototypeIsNative) {
157 HandleScope scope(isolate);
158 CHECK_CALLSITE(recv, "isNative");
159 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
160 GetFrameIndex(isolate, recv));
161 return isolate->heap()->ToBoolean(it.Frame()->IsNative());
162 }
163
BUILTIN(CallSitePrototypeIsToplevel)164 BUILTIN(CallSitePrototypeIsToplevel) {
165 HandleScope scope(isolate);
166 CHECK_CALLSITE(recv, "isToplevel");
167 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
168 GetFrameIndex(isolate, recv));
169 return isolate->heap()->ToBoolean(it.Frame()->IsToplevel());
170 }
171
BUILTIN(CallSitePrototypeToString)172 BUILTIN(CallSitePrototypeToString) {
173 HandleScope scope(isolate);
174 CHECK_CALLSITE(recv, "toString");
175 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv),
176 GetFrameIndex(isolate, recv));
177 RETURN_RESULT_OR_FAILURE(isolate, it.Frame()->ToString());
178 }
179
180 #undef CHECK_CALLSITE
181
182 } // namespace internal
183 } // namespace v8
184