OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/builtins/builtins.h" | 5 #include "src/builtins/builtins.h" |
6 #include "src/builtins/builtins-utils.h" | 6 #include "src/builtins/builtins-utils.h" |
7 | 7 |
8 #include "src/string-builder.h" | 8 #include "src/string-builder.h" |
9 #include "src/wasm/wasm-module.h" | 9 #include "src/wasm/wasm-module.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 #define CHECK_CALLSITE(recv, method) \ | 14 #define CHECK_CALLSITE(recv, method) \ |
15 CHECK_RECEIVER(JSObject, recv, method); \ | 15 CHECK_RECEIVER(JSObject, recv, method); \ |
16 if (!JSReceiver::HasOwnProperty( \ | 16 if (!JSReceiver::HasOwnProperty( \ |
17 recv, isolate->factory()->call_site_position_symbol()) \ | 17 recv, isolate->factory()->call_site_frame_array_symbol()) \ |
18 .FromMaybe(false)) { \ | 18 .FromMaybe(false)) { \ |
19 THROW_NEW_ERROR_RETURN_FAILURE( \ | 19 THROW_NEW_ERROR_RETURN_FAILURE( \ |
20 isolate, \ | 20 isolate, \ |
21 NewTypeError(MessageTemplate::kCallSiteMethod, \ | 21 NewTypeError(MessageTemplate::kCallSiteMethod, \ |
22 isolate->factory()->NewStringFromAsciiChecked(method))); \ | 22 isolate->factory()->NewStringFromAsciiChecked(method))); \ |
23 } | 23 } |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 Object* PositiveNumberOrNull(int value, Isolate* isolate) { | 27 Object* PositiveNumberOrNull(int value, Isolate* isolate) { |
28 if (value >= 0) return *isolate->factory()->NewNumberFromInt(value); | 28 if (value >= 0) return *isolate->factory()->NewNumberFromInt(value); |
29 return isolate->heap()->null_value(); | 29 return isolate->heap()->null_value(); |
30 } | 30 } |
31 | 31 |
| 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 |
| 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::cast(*frame_index_obj)->value(); |
| 42 } |
| 43 |
32 } // namespace | 44 } // namespace |
33 | 45 |
34 BUILTIN(CallSitePrototypeGetColumnNumber) { | 46 BUILTIN(CallSitePrototypeGetColumnNumber) { |
35 HandleScope scope(isolate); | 47 HandleScope scope(isolate); |
36 CHECK_CALLSITE(recv, "getColumnNumber"); | 48 CHECK_CALLSITE(recv, "getColumnNumber"); |
37 | 49 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
38 CallSite call_site(isolate, recv); | 50 GetFrameIndex(isolate, recv)); |
39 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 51 return PositiveNumberOrNull(it.Frame()->GetColumnNumber(), isolate); |
40 return PositiveNumberOrNull(call_site.GetColumnNumber(), isolate); | |
41 } | 52 } |
42 | 53 |
43 BUILTIN(CallSitePrototypeGetEvalOrigin) { | 54 BUILTIN(CallSitePrototypeGetEvalOrigin) { |
44 HandleScope scope(isolate); | 55 HandleScope scope(isolate); |
45 CHECK_CALLSITE(recv, "getEvalOrigin"); | 56 CHECK_CALLSITE(recv, "getEvalOrigin"); |
46 | 57 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
47 CallSite call_site(isolate, recv); | 58 GetFrameIndex(isolate, recv)); |
48 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 59 return *it.Frame()->GetEvalOrigin(); |
49 return *call_site.GetEvalOrigin(); | |
50 } | 60 } |
51 | 61 |
52 BUILTIN(CallSitePrototypeGetFileName) { | 62 BUILTIN(CallSitePrototypeGetFileName) { |
53 HandleScope scope(isolate); | 63 HandleScope scope(isolate); |
54 CHECK_CALLSITE(recv, "getFileName"); | 64 CHECK_CALLSITE(recv, "getFileName"); |
55 | 65 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
56 CallSite call_site(isolate, recv); | 66 GetFrameIndex(isolate, recv)); |
57 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 67 return *it.Frame()->GetFileName(); |
58 return *call_site.GetFileName(); | |
59 } | 68 } |
60 | 69 |
61 namespace { | |
62 | |
63 bool CallSiteIsStrict(Isolate* isolate, Handle<JSObject> receiver) { | |
64 Handle<Object> strict; | |
65 Handle<Symbol> symbol = isolate->factory()->call_site_strict_symbol(); | |
66 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, strict, | |
67 JSObject::GetProperty(receiver, symbol)); | |
68 return strict->BooleanValue(); | |
69 } | |
70 | |
71 } // namespace | |
72 | |
73 BUILTIN(CallSitePrototypeGetFunction) { | 70 BUILTIN(CallSitePrototypeGetFunction) { |
74 HandleScope scope(isolate); | 71 HandleScope scope(isolate); |
75 CHECK_CALLSITE(recv, "getFunction"); | 72 CHECK_CALLSITE(recv, "getFunction"); |
| 73 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
| 74 GetFrameIndex(isolate, recv)); |
76 | 75 |
77 if (CallSiteIsStrict(isolate, recv)) | 76 StackFrameBase* frame = it.Frame(); |
78 return *isolate->factory()->undefined_value(); | 77 if (frame->IsStrict()) return isolate->heap()->undefined_value(); |
79 | 78 return *frame->GetFunction(); |
80 Handle<Symbol> symbol = isolate->factory()->call_site_function_symbol(); | |
81 RETURN_RESULT_OR_FAILURE(isolate, JSObject::GetProperty(recv, symbol)); | |
82 } | 79 } |
83 | 80 |
84 BUILTIN(CallSitePrototypeGetFunctionName) { | 81 BUILTIN(CallSitePrototypeGetFunctionName) { |
85 HandleScope scope(isolate); | 82 HandleScope scope(isolate); |
86 CHECK_CALLSITE(recv, "getFunctionName"); | 83 CHECK_CALLSITE(recv, "getFunctionName"); |
87 | 84 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
88 CallSite call_site(isolate, recv); | 85 GetFrameIndex(isolate, recv)); |
89 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 86 return *it.Frame()->GetFunctionName(); |
90 return *call_site.GetFunctionName(); | |
91 } | 87 } |
92 | 88 |
93 BUILTIN(CallSitePrototypeGetLineNumber) { | 89 BUILTIN(CallSitePrototypeGetLineNumber) { |
94 HandleScope scope(isolate); | 90 HandleScope scope(isolate); |
95 CHECK_CALLSITE(recv, "getLineNumber"); | 91 CHECK_CALLSITE(recv, "getLineNumber"); |
96 | 92 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
97 CallSite call_site(isolate, recv); | 93 GetFrameIndex(isolate, recv)); |
98 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 94 return PositiveNumberOrNull(it.Frame()->GetLineNumber(), isolate); |
99 | |
100 int line_number = call_site.IsWasm() ? call_site.wasm_func_index() | |
101 : call_site.GetLineNumber(); | |
102 return PositiveNumberOrNull(line_number, isolate); | |
103 } | 95 } |
104 | 96 |
105 BUILTIN(CallSitePrototypeGetMethodName) { | 97 BUILTIN(CallSitePrototypeGetMethodName) { |
106 HandleScope scope(isolate); | 98 HandleScope scope(isolate); |
107 CHECK_CALLSITE(recv, "getMethodName"); | 99 CHECK_CALLSITE(recv, "getMethodName"); |
108 | 100 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
109 CallSite call_site(isolate, recv); | 101 GetFrameIndex(isolate, recv)); |
110 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 102 return *it.Frame()->GetMethodName(); |
111 return *call_site.GetMethodName(); | |
112 } | 103 } |
113 | 104 |
114 BUILTIN(CallSitePrototypeGetPosition) { | 105 BUILTIN(CallSitePrototypeGetPosition) { |
115 HandleScope scope(isolate); | 106 HandleScope scope(isolate); |
116 CHECK_CALLSITE(recv, "getPosition"); | 107 CHECK_CALLSITE(recv, "getPosition"); |
117 | 108 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
118 Handle<Symbol> symbol = isolate->factory()->call_site_position_symbol(); | 109 GetFrameIndex(isolate, recv)); |
119 RETURN_RESULT_OR_FAILURE(isolate, JSObject::GetProperty(recv, symbol)); | 110 return Smi::FromInt(it.Frame()->GetPosition()); |
120 } | 111 } |
121 | 112 |
122 BUILTIN(CallSitePrototypeGetScriptNameOrSourceURL) { | 113 BUILTIN(CallSitePrototypeGetScriptNameOrSourceURL) { |
123 HandleScope scope(isolate); | 114 HandleScope scope(isolate); |
124 CHECK_CALLSITE(recv, "getScriptNameOrSourceUrl"); | 115 CHECK_CALLSITE(recv, "getScriptNameOrSourceUrl"); |
125 | 116 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
126 CallSite call_site(isolate, recv); | 117 GetFrameIndex(isolate, recv)); |
127 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 118 return *it.Frame()->GetScriptNameOrSourceUrl(); |
128 return *call_site.GetScriptNameOrSourceUrl(); | |
129 } | 119 } |
130 | 120 |
131 BUILTIN(CallSitePrototypeGetThis) { | 121 BUILTIN(CallSitePrototypeGetThis) { |
132 HandleScope scope(isolate); | 122 HandleScope scope(isolate); |
133 CHECK_CALLSITE(recv, "getThis"); | 123 CHECK_CALLSITE(recv, "getThis"); |
| 124 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
| 125 GetFrameIndex(isolate, recv)); |
134 | 126 |
135 if (CallSiteIsStrict(isolate, recv)) | 127 StackFrameBase* frame = it.Frame(); |
136 return *isolate->factory()->undefined_value(); | 128 if (frame->IsStrict()) return isolate->heap()->undefined_value(); |
137 | 129 return *frame->GetReceiver(); |
138 Handle<Object> receiver; | |
139 Handle<Symbol> symbol = isolate->factory()->call_site_receiver_symbol(); | |
140 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver, | |
141 JSObject::GetProperty(recv, symbol)); | |
142 | |
143 if (*receiver == isolate->heap()->call_site_constructor_symbol()) | |
144 return *isolate->factory()->undefined_value(); | |
145 | |
146 return *receiver; | |
147 } | 130 } |
148 | 131 |
149 BUILTIN(CallSitePrototypeGetTypeName) { | 132 BUILTIN(CallSitePrototypeGetTypeName) { |
150 HandleScope scope(isolate); | 133 HandleScope scope(isolate); |
151 CHECK_CALLSITE(recv, "getTypeName"); | 134 CHECK_CALLSITE(recv, "getTypeName"); |
152 | 135 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
153 CallSite call_site(isolate, recv); | 136 GetFrameIndex(isolate, recv)); |
154 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 137 return *it.Frame()->GetTypeName(); |
155 return *call_site.GetTypeName(); | |
156 } | 138 } |
157 | 139 |
158 BUILTIN(CallSitePrototypeIsConstructor) { | 140 BUILTIN(CallSitePrototypeIsConstructor) { |
159 HandleScope scope(isolate); | 141 HandleScope scope(isolate); |
160 CHECK_CALLSITE(recv, "isConstructor"); | 142 CHECK_CALLSITE(recv, "isConstructor"); |
161 | 143 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
162 CallSite call_site(isolate, recv); | 144 GetFrameIndex(isolate, recv)); |
163 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 145 return isolate->heap()->ToBoolean(it.Frame()->IsConstructor()); |
164 return isolate->heap()->ToBoolean(call_site.IsConstructor()); | |
165 } | 146 } |
166 | 147 |
167 BUILTIN(CallSitePrototypeIsEval) { | 148 BUILTIN(CallSitePrototypeIsEval) { |
168 HandleScope scope(isolate); | 149 HandleScope scope(isolate); |
169 CHECK_CALLSITE(recv, "isEval"); | 150 CHECK_CALLSITE(recv, "isEval"); |
170 | 151 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
171 CallSite call_site(isolate, recv); | 152 GetFrameIndex(isolate, recv)); |
172 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 153 return isolate->heap()->ToBoolean(it.Frame()->IsEval()); |
173 return isolate->heap()->ToBoolean(call_site.IsEval()); | |
174 } | 154 } |
175 | 155 |
176 BUILTIN(CallSitePrototypeIsNative) { | 156 BUILTIN(CallSitePrototypeIsNative) { |
177 HandleScope scope(isolate); | 157 HandleScope scope(isolate); |
178 CHECK_CALLSITE(recv, "isNative"); | 158 CHECK_CALLSITE(recv, "isNative"); |
179 | 159 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
180 CallSite call_site(isolate, recv); | 160 GetFrameIndex(isolate, recv)); |
181 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 161 return isolate->heap()->ToBoolean(it.Frame()->IsNative()); |
182 return isolate->heap()->ToBoolean(call_site.IsNative()); | |
183 } | 162 } |
184 | 163 |
185 BUILTIN(CallSitePrototypeIsToplevel) { | 164 BUILTIN(CallSitePrototypeIsToplevel) { |
186 HandleScope scope(isolate); | 165 HandleScope scope(isolate); |
187 CHECK_CALLSITE(recv, "isToplevel"); | 166 CHECK_CALLSITE(recv, "isToplevel"); |
188 | 167 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
189 CallSite call_site(isolate, recv); | 168 GetFrameIndex(isolate, recv)); |
190 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); | 169 return isolate->heap()->ToBoolean(it.Frame()->IsToplevel()); |
191 return isolate->heap()->ToBoolean(call_site.IsToplevel()); | |
192 } | 170 } |
193 | 171 |
194 BUILTIN(CallSitePrototypeToString) { | 172 BUILTIN(CallSitePrototypeToString) { |
195 HandleScope scope(isolate); | 173 HandleScope scope(isolate); |
196 CHECK_CALLSITE(recv, "toString"); | 174 CHECK_CALLSITE(recv, "toString"); |
197 RETURN_RESULT_OR_FAILURE(isolate, CallSiteUtils::ToString(isolate, recv)); | 175 FrameArrayIterator it(isolate, GetFrameArray(isolate, recv), |
| 176 GetFrameIndex(isolate, recv)); |
| 177 RETURN_RESULT_OR_FAILURE(isolate, it.Frame()->ToString()); |
198 } | 178 } |
199 | 179 |
200 #undef CHECK_CALLSITE | 180 #undef CHECK_CALLSITE |
201 | 181 |
202 } // namespace internal | 182 } // namespace internal |
203 } // namespace v8 | 183 } // namespace v8 |
OLD | NEW |