OLD | NEW |
(Empty) | |
| 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.h" |
| 6 #include "src/builtins/builtins-utils.h" |
| 7 |
| 8 #include "src/string-builder.h" |
| 9 #include "src/wasm/wasm-module.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 |
| 14 #define SET_CALLSITE_PROPERTY(target, key, value) \ |
| 15 RETURN_FAILURE_ON_EXCEPTION( \ |
| 16 isolate, JSObject::SetOwnPropertyIgnoreAttributes( \ |
| 17 target, isolate->factory()->key(), value, DONT_ENUM)) |
| 18 |
| 19 #define CHECK_CALLSITE(recv, method) \ |
| 20 CHECK_RECEIVER(JSObject, recv, method); \ |
| 21 if (!JSReceiver::HasOwnProperty( \ |
| 22 recv, isolate->factory()->call_site_position_symbol()) \ |
| 23 .FromMaybe(false)) { \ |
| 24 THROW_NEW_ERROR_RETURN_FAILURE( \ |
| 25 isolate, \ |
| 26 NewTypeError(MessageTemplate::kCallSiteMethod, \ |
| 27 isolate->factory()->NewStringFromAsciiChecked(method))); \ |
| 28 } |
| 29 |
| 30 namespace { |
| 31 |
| 32 Object* PositiveNumberOrNull(int value, Isolate* isolate) { |
| 33 if (value >= 0) return *isolate->factory()->NewNumberFromInt(value); |
| 34 return isolate->heap()->null_value(); |
| 35 } |
| 36 |
| 37 bool CallSiteIsStrict(Isolate* isolate, Handle<JSObject> receiver) { |
| 38 Handle<Object> strict; |
| 39 Handle<Symbol> symbol = isolate->factory()->call_site_strict_symbol(); |
| 40 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, strict, |
| 41 JSObject::GetProperty(receiver, symbol)); |
| 42 return strict->BooleanValue(); |
| 43 } |
| 44 |
| 45 Object* EvalFromFunctionName(Isolate* isolate, Handle<Script> script) { |
| 46 if (script->eval_from_shared()->IsUndefined(isolate)) |
| 47 return *isolate->factory()->undefined_value(); |
| 48 |
| 49 Handle<SharedFunctionInfo> shared( |
| 50 SharedFunctionInfo::cast(script->eval_from_shared())); |
| 51 // Find the name of the function calling eval. |
| 52 if (shared->name()->BooleanValue()) { |
| 53 return shared->name(); |
| 54 } |
| 55 |
| 56 return shared->inferred_name(); |
| 57 } |
| 58 |
| 59 Object* EvalFromScript(Isolate* isolate, Handle<Script> script) { |
| 60 if (script->eval_from_shared()->IsUndefined(isolate)) |
| 61 return *isolate->factory()->undefined_value(); |
| 62 |
| 63 Handle<SharedFunctionInfo> eval_from_shared( |
| 64 SharedFunctionInfo::cast(script->eval_from_shared())); |
| 65 return eval_from_shared->script()->IsScript() |
| 66 ? eval_from_shared->script() |
| 67 : *isolate->factory()->undefined_value(); |
| 68 } |
| 69 |
| 70 MaybeHandle<String> FormatEvalOrigin(Isolate* isolate, Handle<Script> script) { |
| 71 Handle<Object> sourceURL = Script::GetNameOrSourceURL(script); |
| 72 if (!sourceURL->IsUndefined(isolate)) { |
| 73 DCHECK(sourceURL->IsString()); |
| 74 return Handle<String>::cast(sourceURL); |
| 75 } |
| 76 |
| 77 IncrementalStringBuilder builder(isolate); |
| 78 builder.AppendCString("eval at "); |
| 79 |
| 80 Handle<Object> eval_from_function_name = |
| 81 handle(EvalFromFunctionName(isolate, script), isolate); |
| 82 if (eval_from_function_name->BooleanValue()) { |
| 83 Handle<String> str; |
| 84 ASSIGN_RETURN_ON_EXCEPTION( |
| 85 isolate, str, Object::ToString(isolate, eval_from_function_name), |
| 86 String); |
| 87 builder.AppendString(str); |
| 88 } else { |
| 89 builder.AppendCString("<anonymous>"); |
| 90 } |
| 91 |
| 92 Handle<Object> eval_from_script_obj = |
| 93 handle(EvalFromScript(isolate, script), isolate); |
| 94 if (eval_from_script_obj->IsScript()) { |
| 95 Handle<Script> eval_from_script = |
| 96 Handle<Script>::cast(eval_from_script_obj); |
| 97 builder.AppendCString(" ("); |
| 98 if (eval_from_script->compilation_type() == Script::COMPILATION_TYPE_EVAL) { |
| 99 // Eval script originated from another eval. |
| 100 Handle<String> str; |
| 101 ASSIGN_RETURN_ON_EXCEPTION( |
| 102 isolate, str, FormatEvalOrigin(isolate, eval_from_script), String); |
| 103 builder.AppendString(str); |
| 104 } else { |
| 105 DCHECK(eval_from_script->compilation_type() != |
| 106 Script::COMPILATION_TYPE_EVAL); |
| 107 // eval script originated from "real" source. |
| 108 Handle<Object> name_obj = handle(eval_from_script->name(), isolate); |
| 109 if (eval_from_script->name()->IsString()) { |
| 110 builder.AppendString(Handle<String>::cast(name_obj)); |
| 111 |
| 112 Script::PositionInfo info; |
| 113 if (eval_from_script->GetPositionInfo(script->GetEvalPosition(), &info, |
| 114 Script::NO_OFFSET)) { |
| 115 builder.AppendCString(":"); |
| 116 |
| 117 Handle<String> str = isolate->factory()->NumberToString( |
| 118 handle(Smi::FromInt(info.line + 1), isolate)); |
| 119 builder.AppendString(str); |
| 120 |
| 121 builder.AppendCString(":"); |
| 122 |
| 123 str = isolate->factory()->NumberToString( |
| 124 handle(Smi::FromInt(info.column + 1), isolate)); |
| 125 builder.AppendString(str); |
| 126 } |
| 127 } else { |
| 128 DCHECK(!eval_from_script->name()->IsString()); |
| 129 builder.AppendCString("unknown source"); |
| 130 } |
| 131 } |
| 132 builder.AppendCString(")"); |
| 133 } |
| 134 |
| 135 Handle<String> result; |
| 136 ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String); |
| 137 return result; |
| 138 } |
| 139 |
| 140 } // namespace |
| 141 |
| 142 BUILTIN(CallSiteConstructor) { |
| 143 HandleScope scope(isolate); |
| 144 Handle<JSFunction> target = args.target<JSFunction>(); |
| 145 Handle<HeapObject> new_target = args.new_target(); |
| 146 Handle<Object> receiver = args.atOrUndefined(isolate, 1); |
| 147 Handle<Object> fun = args.atOrUndefined(isolate, 2); |
| 148 Handle<Object> pos = args.atOrUndefined(isolate, 3); |
| 149 Handle<Object> strict_mode = args.atOrUndefined(isolate, 4); |
| 150 |
| 151 // Create the JS object. |
| 152 |
| 153 Handle<JSReceiver> new_target_recv = |
| 154 new_target->IsJSReceiver() ? Handle<JSReceiver>::cast(new_target) |
| 155 : Handle<JSReceiver>::cast(target); |
| 156 |
| 157 Handle<JSObject> obj; |
| 158 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, obj, |
| 159 JSObject::New(target, new_target_recv)); |
| 160 |
| 161 // For wasm frames, receiver is the wasm object and fun is the function index |
| 162 // instead of an actual function. |
| 163 const bool is_wasm_object = |
| 164 receiver->IsJSObject() && wasm::IsWasmObject(JSObject::cast(*receiver)); |
| 165 if (!fun->IsJSFunction() && !is_wasm_object) { |
| 166 THROW_NEW_ERROR_RETURN_FAILURE( |
| 167 isolate, NewTypeError(MessageTemplate::kCallSiteExpectsFunction, |
| 168 Object::TypeOf(isolate, receiver), |
| 169 Object::TypeOf(isolate, fun))); |
| 170 } |
| 171 |
| 172 if (is_wasm_object) { |
| 173 DCHECK(!fun->IsJSFunction()); |
| 174 SET_CALLSITE_PROPERTY(obj, call_site_wasm_obj_symbol, receiver); |
| 175 |
| 176 Handle<Object> fun_index; |
| 177 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, fun_index, |
| 178 Object::ToUint32(isolate, fun)); |
| 179 SET_CALLSITE_PROPERTY(obj, call_site_wasm_func_index_symbol, fun); |
| 180 } else { |
| 181 DCHECK(fun->IsJSFunction()); |
| 182 SET_CALLSITE_PROPERTY(obj, call_site_receiver_symbol, receiver); |
| 183 SET_CALLSITE_PROPERTY(obj, call_site_function_symbol, fun); |
| 184 } |
| 185 |
| 186 Handle<Object> pos_int32; |
| 187 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, pos_int32, |
| 188 Object::ToInt32(isolate, pos)); |
| 189 SET_CALLSITE_PROPERTY(obj, call_site_position_symbol, pos_int32); |
| 190 SET_CALLSITE_PROPERTY( |
| 191 obj, call_site_strict_symbol, |
| 192 isolate->factory()->ToBoolean(strict_mode->BooleanValue())); |
| 193 |
| 194 return *obj; |
| 195 } |
| 196 |
| 197 BUILTIN(CallSitePrototypeGetColumnNumber) { |
| 198 HandleScope scope(isolate); |
| 199 CHECK_CALLSITE(recv, "getColumnNumber"); |
| 200 |
| 201 CallSite call_site(isolate, recv); |
| 202 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 203 return PositiveNumberOrNull(call_site.GetColumnNumber(), isolate); |
| 204 } |
| 205 |
| 206 BUILTIN(CallSitePrototypeGetEvalOrigin) { |
| 207 HandleScope scope(isolate); |
| 208 CHECK_CALLSITE(recv, "getEvalOrigin"); |
| 209 |
| 210 // Retrieve the function's script object. |
| 211 |
| 212 Handle<Object> function_obj; |
| 213 Handle<Symbol> symbol = isolate->factory()->call_site_function_symbol(); |
| 214 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, function_obj, |
| 215 JSObject::GetProperty(recv, symbol)); |
| 216 |
| 217 DCHECK(function_obj->IsJSFunction()); |
| 218 Handle<JSFunction> function = Handle<JSFunction>::cast(function_obj); |
| 219 Handle<Object> script = handle(function->shared()->script(), isolate); |
| 220 |
| 221 RETURN_RESULT_OR_FAILURE( |
| 222 isolate, FormatEvalOrigin(isolate, Handle<Script>::cast(script))); |
| 223 } |
| 224 |
| 225 BUILTIN(CallSitePrototypeGetFileName) { |
| 226 HandleScope scope(isolate); |
| 227 CHECK_CALLSITE(recv, "getFileName"); |
| 228 |
| 229 CallSite call_site(isolate, recv); |
| 230 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 231 return *call_site.GetFileName(); |
| 232 } |
| 233 |
| 234 BUILTIN(CallSitePrototypeGetFunction) { |
| 235 HandleScope scope(isolate); |
| 236 CHECK_CALLSITE(recv, "getFunction"); |
| 237 |
| 238 if (CallSiteIsStrict(isolate, recv)) |
| 239 return *isolate->factory()->undefined_value(); |
| 240 |
| 241 Handle<Symbol> symbol = isolate->factory()->call_site_function_symbol(); |
| 242 RETURN_RESULT_OR_FAILURE(isolate, JSObject::GetProperty(recv, symbol)); |
| 243 } |
| 244 |
| 245 BUILTIN(CallSitePrototypeGetFunctionName) { |
| 246 HandleScope scope(isolate); |
| 247 CHECK_CALLSITE(recv, "getFunctionName"); |
| 248 |
| 249 CallSite call_site(isolate, recv); |
| 250 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 251 return *call_site.GetFunctionName(); |
| 252 } |
| 253 |
| 254 BUILTIN(CallSitePrototypeGetLineNumber) { |
| 255 HandleScope scope(isolate); |
| 256 CHECK_CALLSITE(recv, "getLineNumber"); |
| 257 |
| 258 CallSite call_site(isolate, recv); |
| 259 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 260 |
| 261 int line_number = call_site.IsWasm() ? call_site.wasm_func_index() |
| 262 : call_site.GetLineNumber(); |
| 263 return PositiveNumberOrNull(line_number, isolate); |
| 264 } |
| 265 |
| 266 BUILTIN(CallSitePrototypeGetMethodName) { |
| 267 HandleScope scope(isolate); |
| 268 CHECK_CALLSITE(recv, "getMethodName"); |
| 269 |
| 270 CallSite call_site(isolate, recv); |
| 271 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 272 return *call_site.GetMethodName(); |
| 273 } |
| 274 |
| 275 BUILTIN(CallSitePrototypeGetPosition) { |
| 276 HandleScope scope(isolate); |
| 277 CHECK_CALLSITE(recv, "getPosition"); |
| 278 |
| 279 Handle<Symbol> symbol = isolate->factory()->call_site_position_symbol(); |
| 280 RETURN_RESULT_OR_FAILURE(isolate, JSObject::GetProperty(recv, symbol)); |
| 281 } |
| 282 |
| 283 BUILTIN(CallSitePrototypeGetScriptNameOrSourceURL) { |
| 284 HandleScope scope(isolate); |
| 285 CHECK_CALLSITE(recv, "getScriptNameOrSourceUrl"); |
| 286 |
| 287 CallSite call_site(isolate, recv); |
| 288 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 289 return *call_site.GetScriptNameOrSourceUrl(); |
| 290 } |
| 291 |
| 292 BUILTIN(CallSitePrototypeGetThis) { |
| 293 HandleScope scope(isolate); |
| 294 CHECK_CALLSITE(recv, "getThis"); |
| 295 |
| 296 if (CallSiteIsStrict(isolate, recv)) |
| 297 return *isolate->factory()->undefined_value(); |
| 298 |
| 299 Handle<Object> receiver; |
| 300 Handle<Symbol> symbol = isolate->factory()->call_site_receiver_symbol(); |
| 301 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver, |
| 302 JSObject::GetProperty(recv, symbol)); |
| 303 |
| 304 if (*receiver == isolate->heap()->call_site_constructor_symbol()) |
| 305 return *isolate->factory()->undefined_value(); |
| 306 |
| 307 return *receiver; |
| 308 } |
| 309 |
| 310 BUILTIN(CallSitePrototypeGetTypeName) { |
| 311 HandleScope scope(isolate); |
| 312 CHECK_CALLSITE(recv, "getTypeName"); |
| 313 |
| 314 Handle<Object> receiver; |
| 315 Handle<Symbol> symbol = isolate->factory()->call_site_receiver_symbol(); |
| 316 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver, |
| 317 JSObject::GetProperty(recv, symbol)); |
| 318 |
| 319 // TODO(jgruber): Check for strict/constructor here as above. |
| 320 |
| 321 if (receiver->IsNull(isolate) || receiver->IsUndefined(isolate)) |
| 322 return *isolate->factory()->null_value(); |
| 323 |
| 324 if (receiver->IsJSProxy()) return *isolate->factory()->Proxy_string(); |
| 325 |
| 326 Handle<JSReceiver> receiver_object = |
| 327 Object::ToObject(isolate, receiver).ToHandleChecked(); |
| 328 return *JSReceiver::GetConstructorName(receiver_object); |
| 329 } |
| 330 |
| 331 BUILTIN(CallSitePrototypeIsConstructor) { |
| 332 HandleScope scope(isolate); |
| 333 CHECK_CALLSITE(recv, "isConstructor"); |
| 334 |
| 335 CallSite call_site(isolate, recv); |
| 336 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 337 return isolate->heap()->ToBoolean(call_site.IsConstructor()); |
| 338 } |
| 339 |
| 340 BUILTIN(CallSitePrototypeIsEval) { |
| 341 HandleScope scope(isolate); |
| 342 CHECK_CALLSITE(recv, "isEval"); |
| 343 |
| 344 CallSite call_site(isolate, recv); |
| 345 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 346 return isolate->heap()->ToBoolean(call_site.IsEval()); |
| 347 } |
| 348 |
| 349 BUILTIN(CallSitePrototypeIsNative) { |
| 350 HandleScope scope(isolate); |
| 351 CHECK_CALLSITE(recv, "isNative"); |
| 352 |
| 353 CallSite call_site(isolate, recv); |
| 354 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 355 return isolate->heap()->ToBoolean(call_site.IsNative()); |
| 356 } |
| 357 |
| 358 BUILTIN(CallSitePrototypeIsToplevel) { |
| 359 HandleScope scope(isolate); |
| 360 CHECK_CALLSITE(recv, "isToplevel"); |
| 361 |
| 362 CallSite call_site(isolate, recv); |
| 363 CHECK(call_site.IsJavaScript() || call_site.IsWasm()); |
| 364 return isolate->heap()->ToBoolean(call_site.IsToplevel()); |
| 365 } |
| 366 |
| 367 BUILTIN(CallSitePrototypeToString) { |
| 368 HandleScope scope(isolate); |
| 369 // TODO(jgruber) |
| 370 return *isolate->factory()->undefined_value(); |
| 371 } |
| 372 |
| 373 #undef CHECK_CALLSITE |
| 374 #undef SET_CALLSITE_PROPERTY |
| 375 |
| 376 } // namespace internal |
| 377 } // namespace v8 |
OLD | NEW |