runtime-generator.cc 5.07 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/execution/arguments-inl.h"
6
#include "src/heap/factory.h"
7
#include "src/heap/heap-inl.h"
8
#include "src/logging/counters.h"
9
#include "src/objects/js-generator-inl.h"
10
#include "src/objects/objects-inl.h"
11
#include "src/runtime/runtime-utils.h"
12 13 14 15

namespace v8 {
namespace internal {

16 17 18 19 20 21 22 23 24 25 26 27
RUNTIME_FUNCTION(Runtime_AsyncFunctionAwaitCaught) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
}

RUNTIME_FUNCTION(Runtime_AsyncFunctionAwaitUncaught) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
RUNTIME_FUNCTION(Runtime_AsyncFunctionEnter) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
}

RUNTIME_FUNCTION(Runtime_AsyncFunctionReject) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
}

RUNTIME_FUNCTION(Runtime_AsyncFunctionResolve) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
}

46 47
RUNTIME_FUNCTION(Runtime_CreateJSGeneratorObject) {
  HandleScope scope(isolate);
48
  DCHECK_EQ(2, args.length());
49 50
  CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1);
51 52 53
  CHECK_IMPLIES(IsAsyncFunction(function->shared().kind()),
                IsAsyncGeneratorFunction(function->shared().kind()));
  CHECK(IsResumableFunction(function->shared().kind()));
54

55
  // Underlying function needs to have bytecode available.
56 57 58
  DCHECK(function->shared().HasBytecodeArray());
  int size = function->shared().internal_formal_parameter_count() +
             function->shared().GetBytecodeArray().register_count();
59 60
  Handle<FixedArray> parameters_and_registers =
      isolate->factory()->NewFixedArray(size);
61

62 63
  Handle<JSGeneratorObject> generator =
      isolate->factory()->NewJSGeneratorObject(function);
64
  generator->set_function(*function);
65 66
  generator->set_context(isolate->context());
  generator->set_receiver(*receiver);
67
  generator->set_parameters_and_registers(*parameters_and_registers);
68
  generator->set_resume_mode(JSGeneratorObject::ResumeMode::kNext);
69
  generator->set_continuation(JSGeneratorObject::kGeneratorExecuting);
70 71 72
  if (generator->IsJSAsyncGeneratorObject()) {
    Handle<JSAsyncGeneratorObject>::cast(generator)->set_is_awaiting(0);
  }
73 74 75
  return *generator;
}

76
RUNTIME_FUNCTION(Runtime_GeneratorClose) {
77 78 79
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
80 81 82 83
}

RUNTIME_FUNCTION(Runtime_GeneratorGetFunction) {
  HandleScope scope(isolate);
84
  DCHECK_EQ(1, args.length());
85 86 87 88 89
  CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);

  return generator->function();
}

90 91 92 93 94 95 96 97 98 99 100 101
RUNTIME_FUNCTION(Runtime_AsyncGeneratorAwaitCaught) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
}

RUNTIME_FUNCTION(Runtime_AsyncGeneratorAwaitUncaught) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
}

102 103 104
RUNTIME_FUNCTION(Runtime_AsyncGeneratorResolve) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
105 106 107 108
  UNREACHABLE();
}

RUNTIME_FUNCTION(Runtime_AsyncGeneratorReject) {
109 110
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
111 112 113
  UNREACHABLE();
}

114 115 116 117 118 119
RUNTIME_FUNCTION(Runtime_AsyncGeneratorYield) {
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
}

120
RUNTIME_FUNCTION(Runtime_GeneratorGetResumeMode) {
121 122 123
  // Runtime call is implemented in InterpreterIntrinsics and lowered in
  // JSIntrinsicLowering
  UNREACHABLE();
124 125
}

126 127 128 129 130
// Return true if {generator}'s PC has a catch handler. This allows
// catch prediction to happen from the AsyncGeneratorResumeNext stub.
RUNTIME_FUNCTION(Runtime_AsyncGeneratorHasCatchHandlerForPC) {
  DisallowHeapAllocation no_allocation_scope;
  DCHECK_EQ(1, args.length());
131
  CONVERT_ARG_CHECKED(JSAsyncGeneratorObject, generator, 0);
132

133
  int state = generator.continuation();
134 135 136 137 138
  DCHECK_NE(state, JSAsyncGeneratorObject::kGeneratorExecuting);

  // If state is 0 ("suspendedStart"), there is guaranteed to be no catch
  // handler. Otherwise, if state is below 0, the generator is closed and will
  // not reach a catch handler.
139
  if (state < 1) return ReadOnlyRoots(isolate).false_value();
140

141 142 143
  SharedFunctionInfo shared = generator.function().shared();
  DCHECK(shared.HasBytecodeArray());
  HandlerTable handler_table(shared.GetBytecodeArray());
144

145
  int pc = Smi::cast(generator.input_or_debug_pos()).value();
146
  HandlerTable::CatchPrediction catch_prediction = HandlerTable::ASYNC_AWAIT;
147
  handler_table.LookupRange(pc, nullptr, &catch_prediction);
148 149 150
  return isolate->heap()->ToBoolean(catch_prediction == HandlerTable::CAUGHT);
}

151 152
}  // namespace internal
}  // namespace v8