runtime-internal.cc 22.2 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include <memory>

7
#include "src/api/api.h"
8
#include "src/ast/ast-traversal-visitor.h"
9
#include "src/ast/prettyprinter.h"
10
#include "src/builtins/builtins.h"
11
#include "src/common/message-template.h"
12
#include "src/debug/debug.h"
13 14 15
#include "src/execution/arguments-inl.h"
#include "src/execution/frames-inl.h"
#include "src/execution/isolate-inl.h"
16
#include "src/execution/messages.h"
17
#include "src/execution/runtime-profiler.h"
18
#include "src/handles/maybe-handles.h"
19
#include "src/init/bootstrapper.h"
20
#include "src/logging/counters.h"
21
#include "src/numbers/conversions.h"
22
#include "src/objects/feedback-vector-inl.h"
23
#include "src/objects/js-array-inl.h"
24
#include "src/objects/template-objects-inl.h"
25
#include "src/parsing/parse-info.h"
26
#include "src/parsing/parsing.h"
27
#include "src/runtime/runtime-utils.h"
28
#include "src/snapshot/snapshot.h"
29
#include "src/strings/string-builder-inl.h"
30
#include "src/utils/ostreams.h"
31 32 33 34

namespace v8 {
namespace internal {

35 36 37 38 39 40 41 42 43 44 45
RUNTIME_FUNCTION(Runtime_AccessCheck) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
  if (!isolate->MayAccess(handle(isolate->context(), isolate), object)) {
    isolate->ReportFailedAccessCheck(object);
    RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
  }
  return ReadOnlyRoots(isolate).undefined_value();
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59
RUNTIME_FUNCTION(Runtime_FatalProcessOutOfMemoryInAllocateRaw) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
  isolate->heap()->FatalProcessOutOfMemory("CodeStubAssembler::AllocateRaw");
  UNREACHABLE();
}

RUNTIME_FUNCTION(Runtime_FatalProcessOutOfMemoryInvalidArrayLength) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
  isolate->heap()->FatalProcessOutOfMemory("invalid array length");
  UNREACHABLE();
}

60 61
RUNTIME_FUNCTION(Runtime_Throw) {
  HandleScope scope(isolate);
62
  DCHECK_EQ(1, args.length());
63 64 65 66 67
  return isolate->Throw(args[0]);
}

RUNTIME_FUNCTION(Runtime_ReThrow) {
  HandleScope scope(isolate);
68
  DCHECK_EQ(1, args.length());
69 70 71
  return isolate->ReThrow(args[0]);
}

72 73
RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) {
  SealHandleScope shs(isolate);
74
  DCHECK_LE(0, args.length());
75 76 77
  return isolate->StackOverflow();
}

78 79 80 81 82 83 84
RUNTIME_FUNCTION(Runtime_ThrowSymbolAsyncIteratorInvalid) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kSymbolAsyncIteratorInvalid));
}

85 86 87 88 89 90 91 92 93 94 95 96
#define THROW_ERROR(isolate, args, call)                               \
  HandleScope scope(isolate);                                          \
  DCHECK_LE(1, args.length());                                         \
  CONVERT_SMI_ARG_CHECKED(message_id_smi, 0);                          \
                                                                       \
  Handle<Object> undefined = isolate->factory()->undefined_value();    \
  Handle<Object> arg0 = (args.length() > 1) ? args.at(1) : undefined;  \
  Handle<Object> arg1 = (args.length() > 2) ? args.at(2) : undefined;  \
  Handle<Object> arg2 = (args.length() > 3) ? args.at(3) : undefined;  \
                                                                       \
  MessageTemplate message_id = MessageTemplateFromInt(message_id_smi); \
                                                                       \
97 98 99
  THROW_NEW_ERROR_RETURN_FAILURE(isolate, call(message_id, arg0, arg1, arg2));

RUNTIME_FUNCTION(Runtime_ThrowRangeError) {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  if (FLAG_correctness_fuzzer_suppressions) {
    DCHECK_LE(1, args.length());
    CONVERT_SMI_ARG_CHECKED(message_id_smi, 0);

    // If the result of a BigInt computation is truncated to 64 bit, Turbofan
    // can sometimes truncate intermediate results already, which can prevent
    // those from exceeding the maximum length, effectively preventing a
    // RangeError from being thrown. As this is a performance optimization, this
    // behavior is accepted. To prevent the correctness fuzzer from detecting
    // this difference, we crash the program.
    if (MessageTemplateFromInt(message_id_smi) ==
        MessageTemplate::kBigIntTooBig) {
      FATAL("Aborting on invalid BigInt length");
    }
  }

116 117
  THROW_ERROR(isolate, args, NewRangeError);
}
118

119 120
RUNTIME_FUNCTION(Runtime_ThrowTypeError) {
  THROW_ERROR(isolate, args, NewTypeError);
121 122
}

123 124 125 126 127 128 129
RUNTIME_FUNCTION(Runtime_ThrowTypeErrorIfStrict) {
  if (GetShouldThrow(isolate, Nothing<ShouldThrow>()) ==
      ShouldThrow::kDontThrow)
    return ReadOnlyRoots(isolate).undefined_value();
  THROW_ERROR(isolate, args, NewTypeError);
}

130 131
#undef THROW_ERROR

132 133 134 135
namespace {

const char* ElementsKindToType(ElementsKind fixed_elements_kind) {
  switch (fixed_elements_kind) {
136 137
#define ELEMENTS_KIND_CASE(Type, type, TYPE, ctype) \
  case TYPE##_ELEMENTS:                             \
138 139 140 141 142 143 144 145 146 147 148 149
    return #Type "Array";

    TYPED_ARRAYS(ELEMENTS_KIND_CASE)
#undef ELEMENTS_KIND_CASE

    default:
      UNREACHABLE();
  }
}

}  // namespace

150 151 152 153 154 155 156 157
RUNTIME_FUNCTION(Runtime_ThrowInvalidTypedArrayAlignment) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Map, map, 0);
  CONVERT_ARG_HANDLE_CHECKED(String, problem_string, 1);

  ElementsKind kind = map->elements_kind();

158 159
  Handle<String> type =
      isolate->factory()->NewStringFromAsciiChecked(ElementsKindToType(kind));
160

161 162 163
  ExternalArrayType external_type;
  size_t size;
  Factory::TypeAndSizeForElementsKind(kind, &external_type, &size);
164 165 166 167 168 169 170 171
  Handle<Object> element_size =
      handle(Smi::FromInt(static_cast<int>(size)), isolate);

  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewRangeError(MessageTemplate::kInvalidTypedArrayAlignment,
                             problem_string, type, element_size));
}

172
RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) {
173
  SealHandleScope shs(isolate);
174
  DCHECK_EQ(0, args.length());
175
  return isolate->UnwindAndFindHandler();
176 177
}

178 179
RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
  SealHandleScope shs(isolate);
180
  DCHECK_EQ(0, args.length());
181 182 183 184 185
  return isolate->PromoteScheduledException();
}

RUNTIME_FUNCTION(Runtime_ThrowReferenceError) {
  HandleScope scope(isolate);
186
  DCHECK_EQ(1, args.length());
187 188
  CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
  THROW_NEW_ERROR_RETURN_FAILURE(
189
      isolate, NewReferenceError(MessageTemplate::kNotDefined, name));
190 191
}

192 193 194 195 196 197 198 199 200
RUNTIME_FUNCTION(Runtime_ThrowAccessedUninitializedVariable) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate,
      NewReferenceError(MessageTemplate::kAccessedUninitializedVariable, name));
}

201 202 203 204 205 206 207 208 209
RUNTIME_FUNCTION(Runtime_NewError) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_INT32_ARG_CHECKED(template_index, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
  MessageTemplate message_template = MessageTemplateFromInt(template_index);
  return *isolate->factory()->NewError(message_template, arg0);
}

210 211
RUNTIME_FUNCTION(Runtime_NewTypeError) {
  HandleScope scope(isolate);
212 213
  DCHECK_LE(args.length(), 4);
  DCHECK_GE(args.length(), 1);
214
  CONVERT_INT32_ARG_CHECKED(template_index, 0);
215
  MessageTemplate message_template = MessageTemplateFromInt(template_index);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

  Handle<Object> arg0;
  if (args.length() >= 2) {
    CHECK(args[1].IsObject());
    arg0 = args.at<Object>(1);
  }

  Handle<Object> arg1;
  if (args.length() >= 3) {
    CHECK(args[2].IsObject());
    arg1 = args.at<Object>(2);
  }
  Handle<Object> arg2;
  if (args.length() >= 4) {
    CHECK(args[3].IsObject());
    arg2 = args.at<Object>(3);
  }

  return *isolate->factory()->NewTypeError(message_template, arg0, arg1, arg2);
235 236 237 238
}

RUNTIME_FUNCTION(Runtime_NewReferenceError) {
  HandleScope scope(isolate);
239
  DCHECK_EQ(2, args.length());
240 241
  CONVERT_INT32_ARG_CHECKED(template_index, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
242
  MessageTemplate message_template = MessageTemplateFromInt(template_index);
243 244 245 246 247
  return *isolate->factory()->NewReferenceError(message_template, arg0);
}

RUNTIME_FUNCTION(Runtime_NewSyntaxError) {
  HandleScope scope(isolate);
248
  DCHECK_EQ(2, args.length());
249 250
  CONVERT_INT32_ARG_CHECKED(template_index, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
251
  MessageTemplate message_template = MessageTemplateFromInt(template_index);
252 253 254
  return *isolate->factory()->NewSyntaxError(message_template, arg0);
}

255 256
RUNTIME_FUNCTION(Runtime_ThrowInvalidStringLength) {
  HandleScope scope(isolate);
257
  THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
258 259
}

260 261
RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) {
  HandleScope scope(isolate);
262
  DCHECK_EQ(1, args.length());
263 264 265
  CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate,
266
      NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value));
267 268
}

269 270 271 272 273 274 275
RUNTIME_FUNCTION(Runtime_ThrowThrowMethodMissing) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kThrowMethodMissing));
}

276 277
RUNTIME_FUNCTION(Runtime_ThrowSymbolIteratorInvalid) {
  HandleScope scope(isolate);
278
  DCHECK_EQ(0, args.length());
279 280 281 282
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
}

283 284 285 286 287 288 289 290
RUNTIME_FUNCTION(Runtime_ThrowNotConstructor) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kNotConstructor, object));
}

291 292 293 294 295 296 297 298 299
RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
  Handle<String> type = Object::TypeOf(isolate, object);
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kApplyNonFunction, object, type));
}

300 301
RUNTIME_FUNCTION(Runtime_StackGuard) {
  SealHandleScope shs(isolate);
302
  DCHECK_EQ(0, args.length());
303
  TRACE_EVENT0("v8.execute", "V8.StackGuard");
304 305 306 307 308 309 310 311 312 313

  // First check if this is a real stack overflow.
  StackLimitCheck check(isolate);
  if (check.JsHasOverflowed()) {
    return isolate->StackOverflow();
  }

  return isolate->stack_guard()->HandleInterrupts();
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
RUNTIME_FUNCTION(Runtime_StackGuardWithGap) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(args.length(), 1);
  CONVERT_UINT32_ARG_CHECKED(gap, 0);
  TRACE_EVENT0("v8.execute", "V8.StackGuard");

  // First check if this is a real stack overflow.
  StackLimitCheck check(isolate);
  if (check.JsHasOverflowed(gap)) {
    return isolate->StackOverflow();
  }

  return isolate->stack_guard()->HandleInterrupts();
}

329
RUNTIME_FUNCTION(Runtime_BytecodeBudgetInterruptFromBytecode) {
330 331 332
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
333
  function->raw_feedback_cell().set_interrupt_budget(FLAG_interrupt_budget);
334
  if (!function->has_feedback_vector()) {
335 336
    IsCompiledScope is_compiled_scope(
        function->shared().is_compiled_scope(isolate));
337
    JSFunction::EnsureFeedbackVector(function, &is_compiled_scope);
338 339 340
    // Also initialize the invocation count here. This is only really needed for
    // OSR. When we OSR functions with lazy feedback allocation we want to have
    // a non zero invocation count so we can inline functions.
341
    function->feedback_vector().set_invocation_count(1);
342 343 344 345
    return ReadOnlyRoots(isolate).undefined_value();
  }
  {
    SealHandleScope shs(isolate);
346
    isolate->counters()->runtime_profiler_ticks()->Increment();
347
    isolate->runtime_profiler()->MarkCandidatesForOptimizationFromBytecode();
348
    return ReadOnlyRoots(isolate).undefined_value();
349 350 351
  }
}

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
RUNTIME_FUNCTION(Runtime_BytecodeBudgetInterruptFromCode) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(FeedbackCell, feedback_cell, 0);

  DCHECK(feedback_cell->value().IsFeedbackVector());

  feedback_cell->set_interrupt_budget(FLAG_interrupt_budget);

  SealHandleScope shs(isolate);
  isolate->counters()->runtime_profiler_ticks()->Increment();
  isolate->runtime_profiler()->MarkCandidatesForOptimizationFromCode();
  return ReadOnlyRoots(isolate).undefined_value();
}

367
RUNTIME_FUNCTION(Runtime_AllocateInYoungGeneration) {
368
  HandleScope scope(isolate);
369
  DCHECK_EQ(2, args.length());
370
  CONVERT_SMI_ARG_CHECKED(size, 0);
371 372 373 374
  CONVERT_SMI_ARG_CHECKED(flags, 1);
  bool double_align = AllocateDoubleAlignFlag::decode(flags);
  bool allow_large_object_allocation =
      AllowLargeObjectAllocationFlag::decode(flags);
375
  CHECK(IsAligned(size, kTaggedSize));
376
  CHECK_GT(size, 0);
377 378
  CHECK(FLAG_young_generation_large_objects ||
        size <= kMaxRegularHeapObjectSize);
379 380 381
  if (!allow_large_object_allocation) {
    CHECK(size <= kMaxRegularHeapObjectSize);
  }
382 383 384 385 386

  // TODO(v8:9472): Until double-aligned allocation is fixed for new-space
  // allocations, don't request it.
  double_align = false;

387
  return *isolate->factory()->NewFillerObject(size, double_align,
388 389
                                              AllocationType::kYoung,
                                              AllocationOrigin::kGeneratedCode);
390 391
}

392
RUNTIME_FUNCTION(Runtime_AllocateInOldGeneration) {
393
  HandleScope scope(isolate);
394
  DCHECK_EQ(2, args.length());
395 396
  CONVERT_SMI_ARG_CHECKED(size, 0);
  CONVERT_SMI_ARG_CHECKED(flags, 1);
397 398 399
  bool double_align = AllocateDoubleAlignFlag::decode(flags);
  bool allow_large_object_allocation =
      AllowLargeObjectAllocationFlag::decode(flags);
400
  CHECK(IsAligned(size, kTaggedSize));
401
  CHECK_GT(size, 0);
402 403 404
  if (!allow_large_object_allocation) {
    CHECK(size <= kMaxRegularHeapObjectSize);
  }
405
  return *isolate->factory()->NewFillerObject(size, double_align,
406 407
                                              AllocationType::kOld,
                                              AllocationOrigin::kGeneratedCode);
408 409
}

410 411 412 413 414 415 416 417
RUNTIME_FUNCTION(Runtime_AllocateByteArray) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_SMI_ARG_CHECKED(length, 0);
  DCHECK_LT(0, length);
  return *isolate->factory()->NewByteArray(length);
}

418 419 420 421
RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_SMI_ARG_CHECKED(length, 0);
422
  if (length == 0) return ReadOnlyRoots(isolate).empty_string();
423 424 425 426 427 428 429 430 431 432
  Handle<SeqOneByteString> result;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      isolate, result, isolate->factory()->NewRawOneByteString(length));
  return *result;
}

RUNTIME_FUNCTION(Runtime_AllocateSeqTwoByteString) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_SMI_ARG_CHECKED(length, 0);
433
  if (length == 0) return ReadOnlyRoots(isolate).empty_string();
434 435 436 437 438 439
  Handle<SeqTwoByteString> result;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      isolate, result, isolate->factory()->NewRawTwoByteString(length));
  return *result;
}

440 441 442 443
RUNTIME_FUNCTION(Runtime_ThrowIteratorError) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
444
  return isolate->Throw(*ErrorUtils::NewIteratorError(isolate, object));
445 446
}

447
RUNTIME_FUNCTION(Runtime_ThrowSpreadArgError) {
448
  HandleScope scope(isolate);
449 450 451 452 453
  DCHECK_EQ(2, args.length());
  CONVERT_SMI_ARG_CHECKED(message_id_smi, 0);
  MessageTemplate message_id = MessageTemplateFromInt(message_id_smi);
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 1);
  return ErrorUtils::ThrowSpreadArgError(isolate, message_id, object);
454 455
}

456 457 458 459
RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
460 461
  return isolate->Throw(
      *ErrorUtils::NewCalledNonCallableError(isolate, object));
462 463
}

464 465 466 467
RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
468 469
  return isolate->Throw(
      *ErrorUtils::NewConstructedNonConstructable(isolate, object));
470 471
}

472 473
RUNTIME_FUNCTION(Runtime_ThrowPatternAssignmentNonCoercible) {
  HandleScope scope(isolate);
474 475 476 477
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
  return ErrorUtils::ThrowLoadFromNullOrUndefined(isolate, object,
                                                  MaybeHandle<Object>());
478 479
}

480
RUNTIME_FUNCTION(Runtime_ThrowConstructorReturnedNonObject) {
481 482
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
483

484
  THROW_NEW_ERROR_RETURN_FAILURE(
485 486
      isolate,
      NewTypeError(MessageTemplate::kDerivedConstructorReturnedNonObject));
487 488
}

489 490 491 492 493
// ES6 section 7.3.17 CreateListFromArrayLike (obj)
RUNTIME_FUNCTION(Runtime_CreateListFromArrayLike) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
494 495
  RETURN_RESULT_OR_FAILURE(isolate, Object::CreateListFromArrayLike(
                                        isolate, object, ElementTypes::kAll));
496 497
}

498 499 500 501 502
RUNTIME_FUNCTION(Runtime_IncrementUseCounter) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_SMI_ARG_CHECKED(counter, 0);
  isolate->CountUsage(static_cast<v8::Isolate::UseCounterFeature>(counter));
503
  return ReadOnlyRoots(isolate).undefined_value();
504 505
}

506 507
RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats) {
  HandleScope scope(isolate);
508 509 510 511 512 513

  // Append any worker thread runtime call stats to the main table before
  // printing.
  isolate->counters()->worker_thread_runtime_call_stats()->AddToMainTable(
      isolate->counters()->runtime_call_stats());

514 515 516 517 518 519 520 521 522 523
  if (args.length() == 0) {
    // Without arguments, the result is returned as a string.
    DCHECK_EQ(0, args.length());
    std::stringstream stats_stream;
    isolate->counters()->runtime_call_stats()->Print(stats_stream);
    Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(
        stats_stream.str().c_str());
    isolate->counters()->runtime_call_stats()->Reset();
    return *result;
  } else {
524
    DCHECK_LE(args.length(), 2);
525
    std::FILE* f;
526
    if (args[0].IsString()) {
527 528
      // With a string argument, the results are appended to that file.
      CONVERT_ARG_HANDLE_CHECKED(String, arg0, 0);
529 530
      DisallowHeapAllocation no_gc;
      String::FlatContent flat = arg0->GetFlatContent(no_gc);
531 532 533 534 535 536 537 538 539 540
      const char* filename =
          reinterpret_cast<const char*>(&(flat.ToOneByteVector()[0]));
      f = std::fopen(filename, "a");
      DCHECK_NOT_NULL(f);
    } else {
      // With an integer argument, the results are written to stdout/stderr.
      CONVERT_SMI_ARG_CHECKED(fd, 0);
      DCHECK(fd == 1 || fd == 2);
      f = fd == 1 ? stdout : stderr;
    }
541 542 543 544 545 546 547
    // The second argument (if any) is a message header to be printed.
    if (args.length() >= 2) {
      CONVERT_ARG_HANDLE_CHECKED(String, arg1, 1);
      arg1->PrintOn(f);
      std::fputc('\n', f);
      std::fflush(f);
    }
548 549 550
    OFStream stats_stream(f);
    isolate->counters()->runtime_call_stats()->Print(stats_stream);
    isolate->counters()->runtime_call_stats()->Reset();
551
    if (args[0].IsString())
552 553 554
      std::fclose(f);
    else
      std::fflush(f);
555
    return ReadOnlyRoots(isolate).undefined_value();
556
  }
557 558
}

559 560 561 562 563
RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 1);
564 565
  RETURN_RESULT_OR_FAILURE(
      isolate, Object::OrdinaryHasInstance(isolate, callable, object));
566 567
}

568 569 570 571 572 573 574
RUNTIME_FUNCTION(Runtime_Typeof) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
  return *Object::TypeOf(isolate, object);
}

575 576 577 578 579 580 581 582 583
RUNTIME_FUNCTION(Runtime_AllowDynamicFunction) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0);
  Handle<JSObject> global_proxy(target->global_proxy(), isolate);
  return *isolate->factory()->ToBoolean(
      Builtins::AllowDynamicFunction(isolate, target, global_proxy));
}

584
RUNTIME_FUNCTION(Runtime_CreateAsyncFromSyncIterator) {
585 586 587 588 589 590 591 592 593 594
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());

  CONVERT_ARG_HANDLE_CHECKED(Object, sync_iterator, 0);

  if (!sync_iterator->IsJSReceiver()) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
  }

595 596 597
  Handle<Object> next;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      isolate, next,
598 599
      Object::GetProperty(isolate, sync_iterator,
                          isolate->factory()->next_string()));
600

601
  return *isolate->factory()->NewJSAsyncFromSyncIterator(
602
      Handle<JSReceiver>::cast(sync_iterator), next);
603 604
}

605
RUNTIME_FUNCTION(Runtime_GetTemplateObject) {
606
  HandleScope scope(isolate);
607
  DCHECK_EQ(3, args.length());
608
  CONVERT_ARG_HANDLE_CHECKED(TemplateObjectDescription, description, 0);
609 610
  CONVERT_ARG_HANDLE_CHECKED(SharedFunctionInfo, shared_info, 1);
  CONVERT_SMI_ARG_CHECKED(slot_id, 2);
611

612 613
  Handle<NativeContext> native_context(isolate->context().native_context(),
                                       isolate);
614 615
  return *TemplateObjectDescription::GetTemplateObject(
      isolate, native_context, description, shared_info, slot_id);
616 617
}

618
RUNTIME_FUNCTION(Runtime_ReportMessageFromMicrotask) {
619
  // Helper to report messages and continue JS execution. This is intended to
620 621
  // behave similarly to reporting exceptions which reach the top-level, but
  // allow the JS code to continue.
622 623 624
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());

625
  CONVERT_ARG_HANDLE_CHECKED(Object, exception, 0);
626 627

  DCHECK(!isolate->has_pending_exception());
628 629 630 631 632
  isolate->set_pending_exception(*exception);
  MessageLocation* no_location = nullptr;
  Handle<JSMessageObject> message =
      isolate->CreateMessageOrAbort(exception, no_location);
  MessageHandler::ReportMessage(isolate, no_location, message);
633
  isolate->clear_pending_exception();
634
  return ReadOnlyRoots(isolate).undefined_value();
635 636
}

637 638 639 640 641 642 643 644 645
RUNTIME_FUNCTION(Runtime_GetInitializerFunction) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());

  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, constructor, 0);
  Handle<Symbol> key = isolate->factory()->class_fields_symbol();
  Handle<Object> initializer = JSReceiver::GetDataProperty(constructor, key);
  return *initializer;
}
646 647 648 649 650 651 652 653 654 655 656 657 658

RUNTIME_FUNCTION(Runtime_DoubleToStringWithRadix) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_DOUBLE_ARG_CHECKED(number, 0);
  CONVERT_INT32_ARG_CHECKED(radix, 1);

  char* const str = DoubleToRadixCString(number, radix);
  Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(str);
  DeleteArray(str);
  return *result;
}

659 660
}  // namespace internal
}  // namespace v8