isolate-inl.h 5.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2015 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.

#ifndef V8_ISOLATE_INL_H_
#define V8_ISOLATE_INL_H_

#include "src/isolate.h"
#include "src/objects-inl.h"

namespace v8 {
namespace internal {


void Isolate::set_context(Context* context) {
16
  DCHECK(context == nullptr || context->IsContext());
17 18 19
  thread_local_top_.context_ = context;
}

20 21 22 23 24
Handle<Context> Isolate::native_context() {
  return handle(context()->native_context(), this);
}

Context* Isolate::raw_native_context() { return context()->native_context(); }
25 26 27

Object* Isolate::pending_exception() {
  DCHECK(has_pending_exception());
28
  DCHECK(!thread_local_top_.pending_exception_->IsException(this));
29 30 31 32 33
  return thread_local_top_.pending_exception_;
}


void Isolate::set_pending_exception(Object* exception_obj) {
34
  DCHECK(!exception_obj->IsException(this));
35 36 37 38 39
  thread_local_top_.pending_exception_ = exception_obj;
}


void Isolate::clear_pending_exception() {
40
  DCHECK(!thread_local_top_.pending_exception_->IsException(this));
41 42 43 44 45
  thread_local_top_.pending_exception_ = heap_.the_hole_value();
}


bool Isolate::has_pending_exception() {
46
  DCHECK(!thread_local_top_.pending_exception_->IsException(this));
47
  return !thread_local_top_.pending_exception_->IsTheHole(this);
48 49
}

50
Object* Isolate::get_wasm_caught_exception() {
51 52 53
  return thread_local_top_.wasm_caught_exception_;
}

54 55
void Isolate::set_wasm_caught_exception(Object* exception) {
  thread_local_top_.wasm_caught_exception_ = exception;
56 57 58 59 60
}

void Isolate::clear_wasm_caught_exception() {
  thread_local_top_.wasm_caught_exception_ = nullptr;
}
61 62 63 64 65 66 67 68

void Isolate::clear_pending_message() {
  thread_local_top_.pending_message_obj_ = heap_.the_hole_value();
}


Object* Isolate::scheduled_exception() {
  DCHECK(has_scheduled_exception());
69
  DCHECK(!thread_local_top_.scheduled_exception_->IsException(this));
70 71 72 73 74
  return thread_local_top_.scheduled_exception_;
}


bool Isolate::has_scheduled_exception() {
75
  DCHECK(!thread_local_top_.scheduled_exception_->IsException(this));
76 77 78 79 80
  return thread_local_top_.scheduled_exception_ != heap_.the_hole_value();
}


void Isolate::clear_scheduled_exception() {
81
  DCHECK(!thread_local_top_.scheduled_exception_->IsException(this));
82 83 84 85 86 87 88
  thread_local_top_.scheduled_exception_ = heap_.the_hole_value();
}

bool Isolate::is_catchable_by_javascript(Object* exception) {
  return exception != heap()->termination_exception();
}

89
void Isolate::FireBeforeCallEnteredCallback() {
90 91 92 93 94 95
  for (auto& callback : before_call_entered_callbacks_) {
    callback(reinterpret_cast<v8::Isolate*>(this));
  }
}

void Isolate::FireMicrotasksCompletedCallback() {
96 97 98
  std::vector<MicrotasksCompletedCallback> callbacks(
      microtasks_completed_callbacks_);
  for (auto& callback : callbacks) {
99
    callback(reinterpret_cast<v8::Isolate*>(this));
100 101
  }
}
102

103
Handle<JSGlobalObject> Isolate::global_object() {
104 105 106 107 108
  return handle(context()->global_object(), this);
}

Handle<JSObject> Isolate::global_proxy() {
  return handle(context()->global_proxy(), this);
109 110 111 112 113 114 115 116 117 118 119 120
}


Isolate::ExceptionScope::ExceptionScope(Isolate* isolate)
    : isolate_(isolate),
      pending_exception_(isolate_->pending_exception(), isolate_) {}


Isolate::ExceptionScope::~ExceptionScope() {
  isolate_->set_pending_exception(*pending_exception_);
}

121 122 123 124 125 126
#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name)     \
  Handle<type> Isolate::name() {                             \
    return Handle<type>(raw_native_context()->name(), this); \
  }                                                          \
  bool Isolate::is_##name(type* value) {                     \
    return raw_native_context()->is_##name(value);           \
127 128 129 130
  }
NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
#undef NATIVE_CONTEXT_FIELD_ACCESSOR

131 132 133 134 135
bool Isolate::IsArrayConstructorIntact() {
  Cell* array_constructor_cell = heap()->array_constructor_protector();
  return array_constructor_cell->value() == Smi::FromInt(kProtectorValid);
}

136
bool Isolate::IsSpeciesLookupChainIntact() {
137 138 139 140 141 142 143 144 145 146 147 148
  // Note: It would be nice to have debug checks to make sure that the
  // species protector is accurate, but this would be hard to do for most of
  // what the protector stands for:
  // - You'd need to traverse the heap to check that no Array instance has
  //   a constructor property
  // - To check that Array[Symbol.species] == Array, JS code has to execute,
  //   but JS cannot be invoked in callstack overflow situations
  // All that could be checked reliably is that
  // Array.prototype.constructor == Array. Given that limitation, no check is
  // done here. In place, there are mjsunit tests harmony/array-species* which
  // ensure that behavior is correct in various invalid protector cases.

149
  PropertyCell* species_cell = heap()->species_protector();
150
  return species_cell->value()->IsSmi() &&
jgruber's avatar
jgruber committed
151
         Smi::ToInt(species_cell->value()) == kProtectorValid;
152
}
153

154
bool Isolate::IsStringLengthOverflowIntact() {
155
  Cell* string_length_cell = heap()->string_length_protector();
156
  return string_length_cell->value() == Smi::FromInt(kProtectorValid);
157 158
}

159
bool Isolate::IsArrayBufferNeuteringIntact() {
160 161
  PropertyCell* buffer_neutering = heap()->array_buffer_neutering_protector();
  return buffer_neutering->value() == Smi::FromInt(kProtectorValid);
162 163
}

164
bool Isolate::IsArrayIteratorLookupChainIntact() {
165
  PropertyCell* array_iterator_cell = heap()->array_iterator_protector();
166
  return array_iterator_cell->value() == Smi::FromInt(kProtectorValid);
167 168
}

169 170 171 172
}  // namespace internal
}  // namespace v8

#endif  // V8_ISOLATE_INL_H_