runtime-internal.cc 20.6 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
#include "src/runtime/runtime-utils.h"
6

7 8
#include <memory>

9
#include "src/api.h"
10
#include "src/arguments.h"
11
#include "src/ast/prettyprinter.h"
12
#include "src/bootstrapper.h"
13
#include "src/builtins/builtins.h"
14
#include "src/conversions.h"
15
#include "src/debug/debug.h"
16
#include "src/frames-inl.h"
17
#include "src/isolate-inl.h"
18
#include "src/messages.h"
19
#include "src/parsing/parse-info.h"
20
#include "src/parsing/parsing.h"
21
#include "src/snapshot/snapshot.h"
22 23 24 25 26 27

namespace v8 {
namespace internal {

RUNTIME_FUNCTION(Runtime_CheckIsBootstrapping) {
  SealHandleScope shs(isolate);
28
  DCHECK_EQ(0, args.length());
29
  CHECK(isolate->bootstrapper()->IsActive());
30 31 32
  return isolate->heap()->undefined_value();
}

33 34 35 36 37 38
RUNTIME_FUNCTION(Runtime_IsScriptWrapper) {
  SealHandleScope shs(isolate);
  DCHECK_EQ(1, args.length());
  return isolate->heap()->ToBoolean(args[0]->IsScriptWrapper());
}

39
RUNTIME_FUNCTION(Runtime_ExportFromRuntime) {
40
  HandleScope scope(isolate);
41
  DCHECK_EQ(1, args.length());
42
  CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0);
43
  CHECK(isolate->bootstrapper()->IsActive());
44
  JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10,
45 46 47
                                "ExportFromRuntime");
  Bootstrapper::ExportFromRuntime(isolate, container);
  JSObject::MigrateSlowToFast(container, 0, "ExportFromRuntime");
48 49 50
  return *container;
}

51
RUNTIME_FUNCTION(Runtime_InstallToContext) {
52
  HandleScope scope(isolate);
53
  DCHECK_EQ(1, args.length());
54
  CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0);
55 56
  CHECK(array->HasFastElements());
  CHECK(isolate->bootstrapper()->IsActive());
57 58
  Handle<Context> native_context = isolate->native_context();
  Handle<FixedArray> fixed_array(FixedArray::cast(array->elements()));
jgruber's avatar
jgruber committed
59
  int length = Smi::ToInt(array->length());
60
  for (int i = 0; i < length; i += 2) {
61
    CHECK(fixed_array->get(i)->IsString());
62
    Handle<String> name(String::cast(fixed_array->get(i)));
63
    CHECK(fixed_array->get(i + 1)->IsJSObject());
64 65 66 67 68
    Handle<JSObject> object(JSObject::cast(fixed_array->get(i + 1)));
    int index = Context::ImportedFieldIndexForName(name);
    if (index == Context::kNotFound) {
      index = Context::IntrinsicIndexForName(name);
    }
69
    CHECK_NE(index, Context::kNotFound);
70 71
    native_context->set(index, *object);
  }
72 73 74
  return isolate->heap()->undefined_value();
}

75 76
RUNTIME_FUNCTION(Runtime_Throw) {
  HandleScope scope(isolate);
77
  DCHECK_EQ(1, args.length());
78 79 80 81 82
  return isolate->Throw(args[0]);
}

RUNTIME_FUNCTION(Runtime_ReThrow) {
  HandleScope scope(isolate);
83
  DCHECK_EQ(1, args.length());
84 85 86
  return isolate->ReThrow(args[0]);
}

87 88
RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) {
  SealHandleScope shs(isolate);
89
  DCHECK_LE(0, args.length());
90 91 92
  return isolate->StackOverflow();
}

93 94 95 96 97 98 99
RUNTIME_FUNCTION(Runtime_ThrowSymbolAsyncIteratorInvalid) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kSymbolAsyncIteratorInvalid));
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#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::Template message_id =                              \
      static_cast<MessageTemplate::Template>(message_id_smi);         \
                                                                      \
  THROW_NEW_ERROR_RETURN_FAILURE(isolate, call(message_id, arg0, arg1, arg2));

RUNTIME_FUNCTION(Runtime_ThrowRangeError) {
  THROW_ERROR(isolate, args, NewRangeError);
}
118

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

123 124
#undef THROW_ERROR

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
namespace {

const char* ElementsKindToType(ElementsKind fixed_elements_kind) {
  switch (fixed_elements_kind) {
#define ELEMENTS_KIND_CASE(Type, type, TYPE, ctype, size) \
  case TYPE##_ELEMENTS:                                   \
    return #Type "Array";

    TYPED_ARRAYS(ELEMENTS_KIND_CASE)
#undef ELEMENTS_KIND_CASE

    default:
      UNREACHABLE();
  }
}

}  // namespace

143 144 145 146 147 148 149 150
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();

151 152
  Handle<String> type =
      isolate->factory()->NewStringFromAsciiChecked(ElementsKindToType(kind));
153

154 155 156
  ExternalArrayType external_type;
  size_t size;
  Factory::TypeAndSizeForElementsKind(kind, &external_type, &size);
157 158 159 160 161 162 163 164
  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));
}

165
RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) {
166
  SealHandleScope shs(isolate);
167
  DCHECK_EQ(0, args.length());
168
  return isolate->UnwindAndFindHandler();
169 170
}

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

RUNTIME_FUNCTION(Runtime_ThrowReferenceError) {
  HandleScope scope(isolate);
179
  DCHECK_EQ(1, args.length());
180 181
  CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
  THROW_NEW_ERROR_RETURN_FAILURE(
182
      isolate, NewReferenceError(MessageTemplate::kNotDefined, name));
183 184
}

185 186
RUNTIME_FUNCTION(Runtime_NewTypeError) {
  HandleScope scope(isolate);
187
  DCHECK_EQ(2, args.length());
188 189 190 191 192 193 194 195 196
  CONVERT_INT32_ARG_CHECKED(template_index, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
  auto message_template =
      static_cast<MessageTemplate::Template>(template_index);
  return *isolate->factory()->NewTypeError(message_template, arg0);
}

RUNTIME_FUNCTION(Runtime_NewReferenceError) {
  HandleScope scope(isolate);
197
  DCHECK_EQ(2, args.length());
198 199 200 201 202 203 204 205 206
  CONVERT_INT32_ARG_CHECKED(template_index, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
  auto message_template =
      static_cast<MessageTemplate::Template>(template_index);
  return *isolate->factory()->NewReferenceError(message_template, arg0);
}

RUNTIME_FUNCTION(Runtime_NewSyntaxError) {
  HandleScope scope(isolate);
207
  DCHECK_EQ(2, args.length());
208 209 210 211 212 213 214
  CONVERT_INT32_ARG_CHECKED(template_index, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
  auto message_template =
      static_cast<MessageTemplate::Template>(template_index);
  return *isolate->factory()->NewSyntaxError(message_template, arg0);
}

215 216
RUNTIME_FUNCTION(Runtime_ThrowInvalidStringLength) {
  HandleScope scope(isolate);
217
  THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
218 219
}

220 221
RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) {
  HandleScope scope(isolate);
222
  DCHECK_EQ(1, args.length());
223 224 225
  CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate,
226
      NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value));
227 228
}

229 230 231 232 233 234 235
RUNTIME_FUNCTION(Runtime_ThrowThrowMethodMissing) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kThrowMethodMissing));
}

236 237
RUNTIME_FUNCTION(Runtime_ThrowSymbolIteratorInvalid) {
  HandleScope scope(isolate);
238
  DCHECK_EQ(0, args.length());
239 240 241 242
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
}

243 244 245 246 247 248 249 250
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));
}

251 252 253 254 255 256 257 258 259
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));
}

260 261
RUNTIME_FUNCTION(Runtime_StackGuard) {
  SealHandleScope shs(isolate);
262
  DCHECK_EQ(0, args.length());
263 264 265 266 267 268 269 270 271 272 273 274

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

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

RUNTIME_FUNCTION(Runtime_Interrupt) {
  SealHandleScope shs(isolate);
275
  DCHECK_EQ(0, args.length());
276 277 278 279 280
  return isolate->stack_guard()->HandleInterrupts();
}

RUNTIME_FUNCTION(Runtime_AllocateInNewSpace) {
  HandleScope scope(isolate);
281
  DCHECK_EQ(1, args.length());
282
  CONVERT_SMI_ARG_CHECKED(size, 0);
283
  CHECK(IsAligned(size, kPointerSize));
284 285
  CHECK_GT(size, 0);
  CHECK_LE(size, kMaxRegularHeapObjectSize);
286 287 288 289 290
  return *isolate->factory()->NewFillerObject(size, false, NEW_SPACE);
}

RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace) {
  HandleScope scope(isolate);
291
  DCHECK_EQ(2, args.length());
292 293
  CONVERT_SMI_ARG_CHECKED(size, 0);
  CONVERT_SMI_ARG_CHECKED(flags, 1);
294
  CHECK(IsAligned(size, kPointerSize));
295
  CHECK_GT(size, 0);
296 297
  bool double_align = AllocateDoubleAlignFlag::decode(flags);
  AllocationSpace space = AllocateTargetSpace::decode(flags);
298
  CHECK(size <= kMaxRegularHeapObjectSize || space == LO_SPACE);
299 300 301
  return *isolate->factory()->NewFillerObject(size, double_align, space);
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_SMI_ARG_CHECKED(length, 0);
  if (length == 0) return isolate->heap()->empty_string();
  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);
  if (length == 0) return isolate->heap()->empty_string();
  Handle<SeqTwoByteString> result;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      isolate, result, isolate->factory()->NewRawTwoByteString(length));
  return *result;
}

324 325 326 327
RUNTIME_FUNCTION(Runtime_IS_VAR) {
  UNREACHABLE();  // implemented as macro in the parser
}

328 329
namespace {

330 331 332
bool ComputeLocation(Isolate* isolate, MessageLocation* target) {
  JavaScriptFrameIterator it(isolate);
  if (!it.done()) {
333 334 335
    // Compute the location from the function and the relocation info of the
    // baseline code. For optimized code this will use the deoptimization
    // information to get canonical location information.
336
    std::vector<FrameSummary> frames;
337
    it.frame()->Summarize(&frames);
338
    auto& summary = frames.back().AsJavaScript();
339 340
    Handle<SharedFunctionInfo> shared(summary.function()->shared());
    Handle<Object> script(shared->script(), isolate);
341
    int pos = summary.abstract_code()->SourcePosition(summary.code_offset());
342
    if (script->IsScript() &&
343 344
        !(Handle<Script>::cast(script)->source()->IsUndefined(isolate))) {
      Handle<Script> casted_script = Handle<Script>::cast(script);
345
      *target = MessageLocation(casted_script, pos, pos + 1, shared);
346 347 348 349 350 351
      return true;
    }
  }
  return false;
}

352
Handle<String> RenderCallSite(Isolate* isolate, Handle<Object> object,
353
                              CallPrinter::ErrorHint* hint) {
354
  MessageLocation location;
355
  if (ComputeLocation(isolate, &location)) {
356
    ParseInfo info(location.shared());
357
    if (parsing::ParseAny(&info, location.shared(), isolate)) {
358
      info.ast_value_factory()->Internalize(isolate);
359
      CallPrinter printer(isolate, location.shared()->IsUserJavaScript());
360
      Handle<String> str = printer.Print(info.literal(), location.start_pos());
361
      *hint = printer.GetErrorHint();
362
      if (str->length() > 0) return str;
363 364 365 366 367 368 369
    } else {
      isolate->clear_pending_exception();
    }
  }
  return Object::TypeOf(isolate, object);
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383
MessageTemplate::Template UpdateErrorTemplate(
    CallPrinter::ErrorHint hint, MessageTemplate::Template default_id) {
  switch (hint) {
    case CallPrinter::ErrorHint::kNormalIterator:
      return MessageTemplate::kNotIterable;

    case CallPrinter::ErrorHint::kCallAndNormalIterator:
      return MessageTemplate::kNotCallableOrIterable;

    case CallPrinter::ErrorHint::kAsyncIterator:
      return MessageTemplate::kNotAsyncIterable;

    case CallPrinter::ErrorHint::kCallAndAsyncIterator:
      return MessageTemplate::kNotCallableOrAsyncIterable;
384

385 386
    case CallPrinter::ErrorHint::kNone:
      return default_id;
387
  }
388
  return default_id;
389 390
}

391 392
}  // namespace

393 394
MaybeHandle<Object> Runtime::ThrowIteratorError(Isolate* isolate,
                                                Handle<Object> object) {
395
  CallPrinter::ErrorHint hint = CallPrinter::kNone;
396 397 398 399 400 401 402 403 404
  Handle<String> callsite = RenderCallSite(isolate, object, &hint);
  MessageTemplate::Template id = MessageTemplate::kNonObjectPropertyLoad;

  if (hint == CallPrinter::kNone) {
    Handle<Symbol> iterator_symbol = isolate->factory()->iterator_symbol();
    THROW_NEW_ERROR(isolate, NewTypeError(id, iterator_symbol, callsite),
                    Object);
  }

405
  id = UpdateErrorTemplate(hint, id);
406 407 408
  THROW_NEW_ERROR(isolate, NewTypeError(id, callsite), Object);
}

409 410 411 412
RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
413
  CallPrinter::ErrorHint hint = CallPrinter::kNone;
414
  Handle<String> callsite = RenderCallSite(isolate, object, &hint);
415
  MessageTemplate::Template id = MessageTemplate::kCalledNonCallable;
416
  id = UpdateErrorTemplate(hint, id);
417
  THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(id, callsite));
418 419
}

420 421 422 423
RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
424
  CallPrinter::ErrorHint hint = CallPrinter::kNone;
425
  Handle<String> callsite = RenderCallSite(isolate, object, &hint);
426 427
  MessageTemplate::Template id = MessageTemplate::kNotConstructor;
  THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(id, callsite));
428 429
}

430
RUNTIME_FUNCTION(Runtime_ThrowConstructorReturnedNonObject) {
431 432
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
433 434 435 436 437 438
  if (FLAG_harmony_restrict_constructor_return) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate,
        NewTypeError(MessageTemplate::kClassConstructorReturnedNonObject));
  }

439
  THROW_NEW_ERROR_RETURN_FAILURE(
440 441
      isolate,
      NewTypeError(MessageTemplate::kDerivedConstructorReturnedNonObject));
442 443
}

444 445 446 447 448
// 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);
449 450
  RETURN_RESULT_OR_FAILURE(isolate, Object::CreateListFromArrayLike(
                                        isolate, object, ElementTypes::kAll));
451 452
}

453 454 455 456 457 458 459 460
RUNTIME_FUNCTION(Runtime_DeserializeLazy) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);

  DCHECK(FLAG_lazy_deserialization);

  Handle<SharedFunctionInfo> shared(function->shared(), isolate);
461 462

#ifdef DEBUG
463
  int builtin_id = shared->builtin_id();
464 465 466 467 468 469 470 471 472 473
  // At this point, the builtins table should definitely have DeserializeLazy
  // set at the position of the target builtin.
  CHECK_EQ(Builtins::kDeserializeLazy,
           isolate->builtins()->builtin(builtin_id)->builtin_index());
  // The DeserializeLazy builtin tail-calls the deserialized builtin. This only
  // works with JS-linkage.
  CHECK(Builtins::IsLazy(builtin_id));
  CHECK_EQ(Builtins::TFJ, Builtins::KindOf(builtin_id));
#endif  // DEBUG

474
  Code* code = Snapshot::EnsureBuiltinIsDeserialized(isolate, shared);
475

476 477
  function->set_code(code);
  return code;
478 479
}

480 481 482 483 484 485 486 487
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));
  return isolate->heap()->undefined_value();
}

488 489 490 491 492 493 494 495 496
RUNTIME_FUNCTION(
    Runtime_IncrementUseCounterConstructorReturnNonUndefinedPrimitive) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
  isolate->CountUsage(
      v8::Isolate::UseCounterFeature::kConstructorNonUndefinedPrimitiveReturn);
  return isolate->heap()->undefined_value();
}

497 498
RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats) {
  HandleScope scope(isolate);
499 500 501 502 503 504 505 506 507 508
  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 {
509
    DCHECK_LE(args.length(), 2);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    std::FILE* f;
    if (args[0]->IsString()) {
      // With a string argument, the results are appended to that file.
      CONVERT_ARG_HANDLE_CHECKED(String, arg0, 0);
      String::FlatContent flat = arg0->GetFlatContent();
      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;
    }
525 526 527 528 529 530 531
    // 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);
    }
532 533 534 535 536 537 538 539 540
    OFStream stats_stream(f);
    isolate->counters()->runtime_call_stats()->Print(stats_stream);
    isolate->counters()->runtime_call_stats()->Reset();
    if (args[0]->IsString())
      std::fclose(f);
    else
      std::fflush(f);
    return isolate->heap()->undefined_value();
  }
541 542
}

543 544 545 546 547
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);
548 549
  RETURN_RESULT_OR_FAILURE(
      isolate, Object::OrdinaryHasInstance(isolate, callable, object));
550 551
}

552 553 554 555 556 557 558
RUNTIME_FUNCTION(Runtime_Typeof) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
  return *Object::TypeOf(isolate, object);
}

559 560 561 562 563 564 565 566 567
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));
}

568
RUNTIME_FUNCTION(Runtime_CreateAsyncFromSyncIterator) {
569 570 571 572 573 574 575 576 577 578
  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));
  }

579 580 581 582 583
  Handle<Object> next;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      isolate, next,
      Object::GetProperty(sync_iterator, isolate->factory()->next_string()));

584
  return *isolate->factory()->NewJSAsyncFromSyncIterator(
585
      Handle<JSReceiver>::cast(sync_iterator), next);
586 587
}

588
RUNTIME_FUNCTION(Runtime_CreateTemplateObject) {
589 590 591 592
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(TemplateObjectDescription, description, 0);

593
  return *TemplateObjectDescription::CreateTemplateObject(description);
594 595
}

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
RUNTIME_FUNCTION(Runtime_ReportMessage) {
  // Helper to report messages and continue JS execution. This is intended to
  // behave similarly to reporting exceptions which reach the top-level in
  // Execution.cc, but allow the JS code to continue. This is useful for
  // implementing algorithms such as RunMicrotasks in JS.
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());

  CONVERT_ARG_HANDLE_CHECKED(Object, message_obj, 0);

  DCHECK(!isolate->has_pending_exception());
  isolate->set_pending_exception(*message_obj);
  isolate->ReportPendingMessagesFromJavaScript();
  isolate->clear_pending_exception();
  return isolate->heap()->undefined_value();
}

613 614
}  // namespace internal
}  // namespace v8