Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | #include "src/runtime/runtime-utils.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6 | |
| 7 | #include "src/arguments.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/ast/prettyprinter.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9 | #include "src/bootstrapper.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | #include "src/conversions.h" |
| 11 | #include "src/debug/debug.h" |
| 12 | #include "src/frames-inl.h" |
| 13 | #include "src/isolate-inl.h" |
| 14 | #include "src/messages.h" |
| 15 | #include "src/parsing/parser.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16 | |
| 17 | namespace v8 { |
| 18 | namespace internal { |
| 19 | |
| 20 | RUNTIME_FUNCTION(Runtime_CheckIsBootstrapping) { |
| 21 | SealHandleScope shs(isolate); |
| 22 | DCHECK(args.length() == 0); |
| 23 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 24 | return isolate->heap()->undefined_value(); |
| 25 | } |
| 26 | |
| 27 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | RUNTIME_FUNCTION(Runtime_ExportFromRuntime) { |
| 29 | HandleScope scope(isolate); |
| 30 | DCHECK(args.length() == 1); |
| 31 | CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0); |
| 32 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 33 | JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10, |
| 34 | "ExportFromRuntime"); |
| 35 | Bootstrapper::ExportFromRuntime(isolate, container); |
| 36 | JSObject::MigrateSlowToFast(container, 0, "ExportFromRuntime"); |
| 37 | return *container; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | RUNTIME_FUNCTION(Runtime_ExportExperimentalFromRuntime) { |
| 42 | HandleScope scope(isolate); |
| 43 | DCHECK(args.length() == 1); |
| 44 | CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0); |
| 45 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 46 | JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10, |
| 47 | "ExportExperimentalFromRuntime"); |
| 48 | Bootstrapper::ExportExperimentalFromRuntime(isolate, container); |
| 49 | JSObject::MigrateSlowToFast(container, 0, "ExportExperimentalFromRuntime"); |
| 50 | return *container; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | RUNTIME_FUNCTION(Runtime_InstallToContext) { |
| 55 | HandleScope scope(isolate); |
| 56 | DCHECK(args.length() == 1); |
| 57 | CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0); |
| 58 | RUNTIME_ASSERT(array->HasFastElements()); |
| 59 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 60 | Handle<Context> native_context = isolate->native_context(); |
| 61 | Handle<FixedArray> fixed_array(FixedArray::cast(array->elements())); |
| 62 | int length = Smi::cast(array->length())->value(); |
| 63 | for (int i = 0; i < length; i += 2) { |
| 64 | RUNTIME_ASSERT(fixed_array->get(i)->IsString()); |
| 65 | Handle<String> name(String::cast(fixed_array->get(i))); |
| 66 | RUNTIME_ASSERT(fixed_array->get(i + 1)->IsJSObject()); |
| 67 | Handle<JSObject> object(JSObject::cast(fixed_array->get(i + 1))); |
| 68 | int index = Context::ImportedFieldIndexForName(name); |
| 69 | if (index == Context::kNotFound) { |
| 70 | index = Context::IntrinsicIndexForName(name); |
| 71 | } |
| 72 | RUNTIME_ASSERT(index != Context::kNotFound); |
| 73 | native_context->set(index, *object); |
| 74 | } |
| 75 | return isolate->heap()->undefined_value(); |
| 76 | } |
| 77 | |
| 78 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 79 | RUNTIME_FUNCTION(Runtime_Throw) { |
| 80 | HandleScope scope(isolate); |
| 81 | DCHECK(args.length() == 1); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 82 | return isolate->Throw(args[0]); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | RUNTIME_FUNCTION(Runtime_ReThrow) { |
| 87 | HandleScope scope(isolate); |
| 88 | DCHECK(args.length() == 1); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 89 | return isolate->ReThrow(args[0]); |
| 90 | } |
| 91 | |
| 92 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 93 | RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) { |
| 94 | SealHandleScope shs(isolate); |
| 95 | DCHECK_LE(0, args.length()); |
| 96 | return isolate->StackOverflow(); |
| 97 | } |
| 98 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 99 | RUNTIME_FUNCTION(Runtime_ThrowWasmError) { |
| 100 | HandleScope scope(isolate); |
| 101 | DCHECK_EQ(2, args.length()); |
| 102 | CONVERT_SMI_ARG_CHECKED(message_id, 0); |
| 103 | CONVERT_SMI_ARG_CHECKED(byte_offset, 1); |
| 104 | Handle<Object> error_obj = isolate->factory()->NewError( |
| 105 | static_cast<MessageTemplate::Template>(message_id)); |
| 106 | |
| 107 | // For wasm traps, the byte offset (a.k.a source position) can not be |
| 108 | // determined from relocation info, since the explicit checks for traps |
| 109 | // converge in one singe block which calls this runtime function. |
| 110 | // We hence pass the byte offset explicitely, and patch it into the top-most |
| 111 | // frame (a wasm frame) on the collected stack trace. |
| 112 | // TODO(wasm): This implementation is temporary, see bug #5007: |
| 113 | // https://bugs.chromium.org/p/v8/issues/detail?id=5007 |
| 114 | Handle<JSObject> error = Handle<JSObject>::cast(error_obj); |
| 115 | Handle<Object> stack_trace_obj = JSReceiver::GetDataProperty( |
| 116 | error, isolate->factory()->stack_trace_symbol()); |
| 117 | // Patch the stack trace (array of <receiver, function, code, position>). |
| 118 | if (stack_trace_obj->IsJSArray()) { |
| 119 | Handle<FixedArray> stack_elements( |
| 120 | FixedArray::cast(JSArray::cast(*stack_trace_obj)->elements())); |
| 121 | DCHECK_EQ(1, stack_elements->length() % 4); |
| 122 | DCHECK(Code::cast(stack_elements->get(3))->kind() == Code::WASM_FUNCTION); |
| 123 | DCHECK(stack_elements->get(4)->IsSmi() && |
| 124 | Smi::cast(stack_elements->get(4))->value() >= 0); |
| 125 | stack_elements->set(4, Smi::FromInt(-1 - byte_offset)); |
| 126 | } |
| 127 | Handle<Object> detailed_stack_trace_obj = JSReceiver::GetDataProperty( |
| 128 | error, isolate->factory()->detailed_stack_trace_symbol()); |
| 129 | // Patch the detailed stack trace (array of JSObjects with various |
| 130 | // properties). |
| 131 | if (detailed_stack_trace_obj->IsJSArray()) { |
| 132 | Handle<FixedArray> stack_elements( |
| 133 | FixedArray::cast(JSArray::cast(*detailed_stack_trace_obj)->elements())); |
| 134 | DCHECK_GE(stack_elements->length(), 1); |
| 135 | Handle<JSObject> top_frame(JSObject::cast(stack_elements->get(0))); |
| 136 | Handle<String> wasm_offset_key = |
| 137 | isolate->factory()->InternalizeOneByteString( |
| 138 | STATIC_CHAR_VECTOR("column")); |
| 139 | LookupIterator it(top_frame, wasm_offset_key, top_frame, |
| 140 | LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 141 | if (it.IsFound()) { |
| 142 | DCHECK(JSReceiver::GetDataProperty(&it)->IsSmi()); |
| 143 | Maybe<bool> data_set = JSReceiver::SetDataProperty( |
| 144 | &it, handle(Smi::FromInt(byte_offset), isolate)); |
| 145 | DCHECK(data_set.IsJust() && data_set.FromJust() == true); |
| 146 | USE(data_set); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return isolate->Throw(*error_obj); |
| 151 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 152 | |
| 153 | RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) { |
| 154 | SealHandleScope shs(isolate); |
| 155 | DCHECK(args.length() == 0); |
| 156 | return isolate->UnwindAndFindHandler(); |
| 157 | } |
| 158 | |
| 159 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 160 | RUNTIME_FUNCTION(Runtime_PromoteScheduledException) { |
| 161 | SealHandleScope shs(isolate); |
| 162 | DCHECK(args.length() == 0); |
| 163 | return isolate->PromoteScheduledException(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | RUNTIME_FUNCTION(Runtime_ThrowReferenceError) { |
| 168 | HandleScope scope(isolate); |
| 169 | DCHECK(args.length() == 1); |
| 170 | CONVERT_ARG_HANDLE_CHECKED(Object, name, 0); |
| 171 | THROW_NEW_ERROR_RETURN_FAILURE( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 172 | isolate, NewReferenceError(MessageTemplate::kNotDefined, name)); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | RUNTIME_FUNCTION(Runtime_NewTypeError) { |
| 177 | HandleScope scope(isolate); |
| 178 | DCHECK(args.length() == 2); |
| 179 | CONVERT_INT32_ARG_CHECKED(template_index, 0); |
| 180 | CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1); |
| 181 | auto message_template = |
| 182 | static_cast<MessageTemplate::Template>(template_index); |
| 183 | return *isolate->factory()->NewTypeError(message_template, arg0); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | RUNTIME_FUNCTION(Runtime_NewReferenceError) { |
| 188 | HandleScope scope(isolate); |
| 189 | DCHECK(args.length() == 2); |
| 190 | CONVERT_INT32_ARG_CHECKED(template_index, 0); |
| 191 | CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1); |
| 192 | auto message_template = |
| 193 | static_cast<MessageTemplate::Template>(template_index); |
| 194 | return *isolate->factory()->NewReferenceError(message_template, arg0); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | RUNTIME_FUNCTION(Runtime_NewSyntaxError) { |
| 199 | HandleScope scope(isolate); |
| 200 | DCHECK(args.length() == 2); |
| 201 | CONVERT_INT32_ARG_CHECKED(template_index, 0); |
| 202 | CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1); |
| 203 | auto message_template = |
| 204 | static_cast<MessageTemplate::Template>(template_index); |
| 205 | return *isolate->factory()->NewSyntaxError(message_template, arg0); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | RUNTIME_FUNCTION(Runtime_ThrowIllegalInvocation) { |
| 210 | HandleScope scope(isolate); |
| 211 | DCHECK(args.length() == 0); |
| 212 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 213 | isolate, NewTypeError(MessageTemplate::kIllegalInvocation)); |
| 214 | } |
| 215 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 216 | RUNTIME_FUNCTION(Runtime_ThrowIncompatibleMethodReceiver) { |
| 217 | HandleScope scope(isolate); |
| 218 | DCHECK_EQ(2, args.length()); |
| 219 | CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 0); |
| 220 | CONVERT_ARG_HANDLE_CHECKED(Object, arg1, 1); |
| 221 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 222 | isolate, |
| 223 | NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, arg0, arg1)); |
| 224 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 225 | |
| 226 | RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) { |
| 227 | HandleScope scope(isolate); |
| 228 | DCHECK(args.length() == 1); |
| 229 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 0); |
| 230 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 231 | isolate, |
| 232 | NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value)); |
| 233 | } |
| 234 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 235 | RUNTIME_FUNCTION(Runtime_ThrowGeneratorRunning) { |
| 236 | HandleScope scope(isolate); |
| 237 | DCHECK_EQ(0, args.length()); |
| 238 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 239 | isolate, NewTypeError(MessageTemplate::kGeneratorRunning)); |
| 240 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 241 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 242 | RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) { |
| 243 | HandleScope scope(isolate); |
| 244 | DCHECK_EQ(1, args.length()); |
| 245 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 246 | Handle<String> type = Object::TypeOf(isolate, object); |
| 247 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 248 | isolate, NewTypeError(MessageTemplate::kApplyNonFunction, object, type)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
| 252 | RUNTIME_FUNCTION(Runtime_PromiseRejectEvent) { |
| 253 | DCHECK(args.length() == 3); |
| 254 | HandleScope scope(isolate); |
| 255 | CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0); |
| 256 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); |
| 257 | CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2); |
| 258 | if (debug_event) isolate->debug()->OnPromiseReject(promise, value); |
| 259 | Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol(); |
| 260 | // Do not report if we actually have a handler. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 261 | if (JSReceiver::GetDataProperty(promise, key)->IsUndefined()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 262 | isolate->ReportPromiseReject(promise, value, |
| 263 | v8::kPromiseRejectWithNoHandler); |
| 264 | } |
| 265 | return isolate->heap()->undefined_value(); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | RUNTIME_FUNCTION(Runtime_PromiseRevokeReject) { |
| 270 | DCHECK(args.length() == 1); |
| 271 | HandleScope scope(isolate); |
| 272 | CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0); |
| 273 | Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol(); |
| 274 | // At this point, no revocation has been issued before |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 275 | RUNTIME_ASSERT(JSReceiver::GetDataProperty(promise, key)->IsUndefined()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 276 | isolate->ReportPromiseReject(promise, Handle<Object>(), |
| 277 | v8::kPromiseHandlerAddedAfterReject); |
| 278 | return isolate->heap()->undefined_value(); |
| 279 | } |
| 280 | |
| 281 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 282 | RUNTIME_FUNCTION(Runtime_StackGuard) { |
| 283 | SealHandleScope shs(isolate); |
| 284 | DCHECK(args.length() == 0); |
| 285 | |
| 286 | // First check if this is a real stack overflow. |
| 287 | StackLimitCheck check(isolate); |
| 288 | if (check.JsHasOverflowed()) { |
| 289 | return isolate->StackOverflow(); |
| 290 | } |
| 291 | |
| 292 | return isolate->stack_guard()->HandleInterrupts(); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | RUNTIME_FUNCTION(Runtime_Interrupt) { |
| 297 | SealHandleScope shs(isolate); |
| 298 | DCHECK(args.length() == 0); |
| 299 | return isolate->stack_guard()->HandleInterrupts(); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | RUNTIME_FUNCTION(Runtime_AllocateInNewSpace) { |
| 304 | HandleScope scope(isolate); |
| 305 | DCHECK(args.length() == 1); |
| 306 | CONVERT_SMI_ARG_CHECKED(size, 0); |
| 307 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 308 | RUNTIME_ASSERT(size > 0); |
| 309 | RUNTIME_ASSERT(size <= Page::kMaxRegularHeapObjectSize); |
| 310 | return *isolate->factory()->NewFillerObject(size, false, NEW_SPACE); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace) { |
| 315 | HandleScope scope(isolate); |
| 316 | DCHECK(args.length() == 2); |
| 317 | CONVERT_SMI_ARG_CHECKED(size, 0); |
| 318 | CONVERT_SMI_ARG_CHECKED(flags, 1); |
| 319 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 320 | RUNTIME_ASSERT(size > 0); |
| 321 | RUNTIME_ASSERT(size <= Page::kMaxRegularHeapObjectSize); |
| 322 | bool double_align = AllocateDoubleAlignFlag::decode(flags); |
| 323 | AllocationSpace space = AllocateTargetSpace::decode(flags); |
| 324 | return *isolate->factory()->NewFillerObject(size, double_align, space); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | // Collect the raw data for a stack trace. Returns an array of 4 |
| 329 | // element segments each containing a receiver, function, code and |
| 330 | // native code offset. |
| 331 | RUNTIME_FUNCTION(Runtime_CollectStackTrace) { |
| 332 | HandleScope scope(isolate); |
| 333 | DCHECK(args.length() == 2); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 334 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, error_object, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 335 | CONVERT_ARG_HANDLE_CHECKED(Object, caller, 1); |
| 336 | |
| 337 | if (!isolate->bootstrapper()->IsActive()) { |
| 338 | // Optionally capture a more detailed stack trace for the message. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 339 | RETURN_FAILURE_ON_EXCEPTION( |
| 340 | isolate, isolate->CaptureAndSetDetailedStackTrace(error_object)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 341 | // Capture a simple stack trace for the stack property. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 342 | RETURN_FAILURE_ON_EXCEPTION( |
| 343 | isolate, isolate->CaptureAndSetSimpleStackTrace(error_object, caller)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 344 | } |
| 345 | return isolate->heap()->undefined_value(); |
| 346 | } |
| 347 | |
| 348 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 349 | RUNTIME_FUNCTION(Runtime_MessageGetStartPosition) { |
| 350 | SealHandleScope shs(isolate); |
| 351 | DCHECK(args.length() == 1); |
| 352 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
| 353 | return Smi::FromInt(message->start_position()); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | RUNTIME_FUNCTION(Runtime_MessageGetScript) { |
| 358 | SealHandleScope shs(isolate); |
| 359 | DCHECK(args.length() == 1); |
| 360 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
| 361 | return message->script(); |
| 362 | } |
| 363 | |
| 364 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 365 | RUNTIME_FUNCTION(Runtime_FormatMessageString) { |
| 366 | HandleScope scope(isolate); |
| 367 | DCHECK(args.length() == 4); |
| 368 | CONVERT_INT32_ARG_CHECKED(template_index, 0); |
| 369 | CONVERT_ARG_HANDLE_CHECKED(String, arg0, 1); |
| 370 | CONVERT_ARG_HANDLE_CHECKED(String, arg1, 2); |
| 371 | CONVERT_ARG_HANDLE_CHECKED(String, arg2, 3); |
| 372 | Handle<String> result; |
| 373 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 374 | isolate, result, |
| 375 | MessageTemplate::FormatMessage(template_index, arg0, arg1, arg2)); |
| 376 | isolate->native_context()->IncrementErrorsThrown(); |
| 377 | return *result; |
| 378 | } |
| 379 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 380 | #define CALLSITE_GET(NAME, RETURN) \ |
| 381 | RUNTIME_FUNCTION(Runtime_CallSite##NAME##RT) { \ |
| 382 | HandleScope scope(isolate); \ |
| 383 | DCHECK(args.length() == 1); \ |
| 384 | CONVERT_ARG_HANDLE_CHECKED(JSObject, call_site_obj, 0); \ |
| 385 | Handle<String> result; \ |
| 386 | CallSite call_site(isolate, call_site_obj); \ |
| 387 | RUNTIME_ASSERT(call_site.IsJavaScript() || call_site.IsWasm()); \ |
| 388 | return RETURN(call_site.NAME(), isolate); \ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static inline Object* ReturnDereferencedHandle(Handle<Object> obj, |
| 392 | Isolate* isolate) { |
| 393 | return *obj; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | static inline Object* ReturnPositiveNumberOrNull(int value, Isolate* isolate) { |
| 398 | if (value >= 0) return *isolate->factory()->NewNumberFromInt(value); |
| 399 | return isolate->heap()->null_value(); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static inline Object* ReturnBoolean(bool value, Isolate* isolate) { |
| 404 | return isolate->heap()->ToBoolean(value); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | CALLSITE_GET(GetFileName, ReturnDereferencedHandle) |
| 409 | CALLSITE_GET(GetFunctionName, ReturnDereferencedHandle) |
| 410 | CALLSITE_GET(GetScriptNameOrSourceUrl, ReturnDereferencedHandle) |
| 411 | CALLSITE_GET(GetMethodName, ReturnDereferencedHandle) |
| 412 | CALLSITE_GET(GetLineNumber, ReturnPositiveNumberOrNull) |
| 413 | CALLSITE_GET(GetColumnNumber, ReturnPositiveNumberOrNull) |
| 414 | CALLSITE_GET(IsNative, ReturnBoolean) |
| 415 | CALLSITE_GET(IsToplevel, ReturnBoolean) |
| 416 | CALLSITE_GET(IsEval, ReturnBoolean) |
| 417 | CALLSITE_GET(IsConstructor, ReturnBoolean) |
| 418 | |
| 419 | #undef CALLSITE_GET |
| 420 | |
| 421 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 422 | RUNTIME_FUNCTION(Runtime_IS_VAR) { |
| 423 | UNREACHABLE(); // implemented as macro in the parser |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 428 | namespace { |
| 429 | |
| 430 | bool ComputeLocation(Isolate* isolate, MessageLocation* target) { |
| 431 | JavaScriptFrameIterator it(isolate); |
| 432 | if (!it.done()) { |
| 433 | JavaScriptFrame* frame = it.frame(); |
| 434 | JSFunction* fun = frame->function(); |
| 435 | Object* script = fun->shared()->script(); |
| 436 | if (script->IsScript() && |
| 437 | !(Script::cast(script)->source()->IsUndefined())) { |
| 438 | Handle<Script> casted_script(Script::cast(script)); |
| 439 | // Compute the location from the function and the relocation info of the |
| 440 | // baseline code. For optimized code this will use the deoptimization |
| 441 | // information to get canonical location information. |
| 442 | List<FrameSummary> frames(FLAG_max_inlining_levels + 1); |
| 443 | it.frame()->Summarize(&frames); |
| 444 | FrameSummary& summary = frames.last(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 445 | int pos = summary.abstract_code()->SourcePosition(summary.code_offset()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 446 | *target = MessageLocation(casted_script, pos, pos + 1, handle(fun)); |
| 447 | return true; |
| 448 | } |
| 449 | } |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | Handle<String> RenderCallSite(Isolate* isolate, Handle<Object> object) { |
| 455 | MessageLocation location; |
| 456 | if (ComputeLocation(isolate, &location)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 457 | Zone zone(isolate->allocator()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 458 | base::SmartPointer<ParseInfo> info( |
| 459 | location.function()->shared()->is_function() |
| 460 | ? new ParseInfo(&zone, location.function()) |
| 461 | : new ParseInfo(&zone, location.script())); |
| 462 | if (Parser::ParseStatic(info.get())) { |
| 463 | CallPrinter printer(isolate, location.function()->shared()->IsBuiltin()); |
| 464 | const char* string = printer.Print(info->literal(), location.start_pos()); |
| 465 | if (strlen(string) > 0) { |
| 466 | return isolate->factory()->NewStringFromAsciiChecked(string); |
| 467 | } |
| 468 | } else { |
| 469 | isolate->clear_pending_exception(); |
| 470 | } |
| 471 | } |
| 472 | return Object::TypeOf(isolate, object); |
| 473 | } |
| 474 | |
| 475 | } // namespace |
| 476 | |
| 477 | |
| 478 | RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 479 | HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 480 | DCHECK_EQ(1, args.length()); |
| 481 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 482 | Handle<String> callsite = RenderCallSite(isolate, object); |
| 483 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 484 | isolate, NewTypeError(MessageTemplate::kCalledNonCallable, callsite)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 485 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 486 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 487 | RUNTIME_FUNCTION(Runtime_ThrowCalledOnNullOrUndefined) { |
| 488 | HandleScope scope(isolate); |
| 489 | DCHECK_EQ(1, args.length()); |
| 490 | CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
| 491 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 492 | isolate, NewTypeError(MessageTemplate::kCalledOnNullOrUndefined, name)); |
| 493 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 494 | |
| 495 | RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable) { |
| 496 | HandleScope scope(isolate); |
| 497 | DCHECK_EQ(1, args.length()); |
| 498 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 499 | Handle<String> callsite = RenderCallSite(isolate, object); |
| 500 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 501 | isolate, NewTypeError(MessageTemplate::kNotConstructor, callsite)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 502 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 503 | |
| 504 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 505 | RUNTIME_FUNCTION(Runtime_ThrowDerivedConstructorReturnedNonObject) { |
| 506 | HandleScope scope(isolate); |
| 507 | DCHECK_EQ(0, args.length()); |
| 508 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 509 | isolate, NewTypeError(MessageTemplate::kDerivedConstructorReturn)); |
| 510 | } |
| 511 | |
| 512 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 513 | // ES6 section 7.3.17 CreateListFromArrayLike (obj) |
| 514 | RUNTIME_FUNCTION(Runtime_CreateListFromArrayLike) { |
| 515 | HandleScope scope(isolate); |
| 516 | DCHECK_EQ(1, args.length()); |
| 517 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 518 | Handle<FixedArray> result; |
| 519 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 520 | isolate, result, |
| 521 | Object::CreateListFromArrayLike(isolate, object, ElementTypes::kAll)); |
| 522 | return *result; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | RUNTIME_FUNCTION(Runtime_IncrementUseCounter) { |
| 527 | HandleScope scope(isolate); |
| 528 | DCHECK_EQ(1, args.length()); |
| 529 | CONVERT_SMI_ARG_CHECKED(counter, 0); |
| 530 | isolate->CountUsage(static_cast<v8::Isolate::UseCounterFeature>(counter)); |
| 531 | return isolate->heap()->undefined_value(); |
| 532 | } |
| 533 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 534 | RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats) { |
| 535 | HandleScope scope(isolate); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 536 | if (args.length() == 0) { |
| 537 | // Without arguments, the result is returned as a string. |
| 538 | DCHECK_EQ(0, args.length()); |
| 539 | std::stringstream stats_stream; |
| 540 | isolate->counters()->runtime_call_stats()->Print(stats_stream); |
| 541 | Handle<String> result = isolate->factory()->NewStringFromAsciiChecked( |
| 542 | stats_stream.str().c_str()); |
| 543 | isolate->counters()->runtime_call_stats()->Reset(); |
| 544 | return *result; |
| 545 | } else { |
| 546 | DCHECK_LE(args.length(), 2); |
| 547 | std::FILE* f; |
| 548 | if (args[0]->IsString()) { |
| 549 | // With a string argument, the results are appended to that file. |
| 550 | CONVERT_ARG_HANDLE_CHECKED(String, arg0, 0); |
| 551 | String::FlatContent flat = arg0->GetFlatContent(); |
| 552 | const char* filename = |
| 553 | reinterpret_cast<const char*>(&(flat.ToOneByteVector()[0])); |
| 554 | f = std::fopen(filename, "a"); |
| 555 | DCHECK_NOT_NULL(f); |
| 556 | } else { |
| 557 | // With an integer argument, the results are written to stdout/stderr. |
| 558 | CONVERT_SMI_ARG_CHECKED(fd, 0); |
| 559 | DCHECK(fd == 1 || fd == 2); |
| 560 | f = fd == 1 ? stdout : stderr; |
| 561 | } |
| 562 | // The second argument (if any) is a message header to be printed. |
| 563 | if (args.length() >= 2) { |
| 564 | CONVERT_ARG_HANDLE_CHECKED(String, arg1, 1); |
| 565 | arg1->PrintOn(f); |
| 566 | std::fputc('\n', f); |
| 567 | std::fflush(f); |
| 568 | } |
| 569 | OFStream stats_stream(f); |
| 570 | isolate->counters()->runtime_call_stats()->Print(stats_stream); |
| 571 | isolate->counters()->runtime_call_stats()->Reset(); |
| 572 | if (args[0]->IsString()) |
| 573 | std::fclose(f); |
| 574 | else |
| 575 | std::fflush(f); |
| 576 | return isolate->heap()->undefined_value(); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | RUNTIME_FUNCTION(Runtime_EnqueueMicrotask) { |
| 581 | HandleScope scope(isolate); |
| 582 | DCHECK(args.length() == 1); |
| 583 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, microtask, 0); |
| 584 | isolate->EnqueueMicrotask(microtask); |
| 585 | return isolate->heap()->undefined_value(); |
| 586 | } |
| 587 | |
| 588 | RUNTIME_FUNCTION(Runtime_RunMicrotasks) { |
| 589 | HandleScope scope(isolate); |
| 590 | DCHECK(args.length() == 0); |
| 591 | isolate->RunMicrotasks(); |
| 592 | return isolate->heap()->undefined_value(); |
| 593 | } |
| 594 | |
| 595 | RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) { |
| 596 | HandleScope scope(isolate); |
| 597 | DCHECK_EQ(2, args.length()); |
| 598 | CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0); |
| 599 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 1); |
| 600 | Handle<Object> result; |
| 601 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 602 | isolate, result, Object::OrdinaryHasInstance(isolate, callable, object)); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 603 | return *result; |
| 604 | } |
| 605 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 606 | } // namespace internal |
| 607 | } // namespace v8 |