1 // Copyright 2015 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 
5 #ifndef V8_ISOLATE_INL_H_
6 #define V8_ISOLATE_INL_H_
7 
8 #include "src/isolate.h"
9 #include "src/objects-inl.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 
set_context(Context * context)15 void Isolate::set_context(Context* context) {
16   DCHECK(context == nullptr || context->IsContext());
17   thread_local_top_.context_ = context;
18 }
19 
native_context()20 Handle<Context> Isolate::native_context() {
21   return handle(context()->native_context(), this);
22 }
23 
raw_native_context()24 Context* Isolate::raw_native_context() { return context()->native_context(); }
25 
pending_exception()26 Object* Isolate::pending_exception() {
27   DCHECK(has_pending_exception());
28   DCHECK(!thread_local_top_.pending_exception_->IsException(this));
29   return thread_local_top_.pending_exception_;
30 }
31 
32 
set_pending_exception(Object * exception_obj)33 void Isolate::set_pending_exception(Object* exception_obj) {
34   DCHECK(!exception_obj->IsException(this));
35   thread_local_top_.pending_exception_ = exception_obj;
36 }
37 
38 
clear_pending_exception()39 void Isolate::clear_pending_exception() {
40   DCHECK(!thread_local_top_.pending_exception_->IsException(this));
41   thread_local_top_.pending_exception_ = heap_.the_hole_value();
42 }
43 
44 
has_pending_exception()45 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 
get_wasm_caught_exception()50 Object* Isolate::get_wasm_caught_exception() {
51   return thread_local_top_.wasm_caught_exception_;
52 }
53 
set_wasm_caught_exception(Object * exception)54 void Isolate::set_wasm_caught_exception(Object* exception) {
55   thread_local_top_.wasm_caught_exception_ = exception;
56 }
57 
clear_wasm_caught_exception()58 void Isolate::clear_wasm_caught_exception() {
59   thread_local_top_.wasm_caught_exception_ = nullptr;
60 }
61 
clear_pending_message()62 void Isolate::clear_pending_message() {
63   thread_local_top_.pending_message_obj_ = heap_.the_hole_value();
64 }
65 
66 
scheduled_exception()67 Object* Isolate::scheduled_exception() {
68   DCHECK(has_scheduled_exception());
69   DCHECK(!thread_local_top_.scheduled_exception_->IsException(this));
70   return thread_local_top_.scheduled_exception_;
71 }
72 
73 
has_scheduled_exception()74 bool Isolate::has_scheduled_exception() {
75   DCHECK(!thread_local_top_.scheduled_exception_->IsException(this));
76   return thread_local_top_.scheduled_exception_ != heap_.the_hole_value();
77 }
78 
79 
clear_scheduled_exception()80 void Isolate::clear_scheduled_exception() {
81   DCHECK(!thread_local_top_.scheduled_exception_->IsException(this));
82   thread_local_top_.scheduled_exception_ = heap_.the_hole_value();
83 }
84 
is_catchable_by_javascript(Object * exception)85 bool Isolate::is_catchable_by_javascript(Object* exception) {
86   return exception != heap()->termination_exception();
87 }
88 
FireBeforeCallEnteredCallback()89 void Isolate::FireBeforeCallEnteredCallback() {
90   for (auto& callback : before_call_entered_callbacks_) {
91     callback(reinterpret_cast<v8::Isolate*>(this));
92   }
93 }
94 
FireMicrotasksCompletedCallback()95 void Isolate::FireMicrotasksCompletedCallback() {
96   std::vector<MicrotasksCompletedCallback> callbacks(
97       microtasks_completed_callbacks_);
98   for (auto& callback : callbacks) {
99     callback(reinterpret_cast<v8::Isolate*>(this));
100   }
101 }
102 
global_object()103 Handle<JSGlobalObject> Isolate::global_object() {
104   return handle(context()->global_object(), this);
105 }
106 
global_proxy()107 Handle<JSObject> Isolate::global_proxy() {
108   return handle(context()->global_proxy(), this);
109 }
110 
111 
ExceptionScope(Isolate * isolate)112 Isolate::ExceptionScope::ExceptionScope(Isolate* isolate)
113     : isolate_(isolate),
114       pending_exception_(isolate_->pending_exception(), isolate_) {}
115 
116 
~ExceptionScope()117 Isolate::ExceptionScope::~ExceptionScope() {
118   isolate_->set_pending_exception(*pending_exception_);
119 }
120 
121 #define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name)     \
122   Handle<type> Isolate::name() {                             \
123     return Handle<type>(raw_native_context()->name(), this); \
124   }                                                          \
125   bool Isolate::is_##name(type* value) {                     \
126     return raw_native_context()->is_##name(value);           \
127   }
NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)128 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
129 #undef NATIVE_CONTEXT_FIELD_ACCESSOR
130 
131 bool Isolate::IsArrayConstructorIntact() {
132   Cell* array_constructor_cell = heap()->array_constructor_protector();
133   return array_constructor_cell->value() == Smi::FromInt(kProtectorValid);
134 }
135 
IsArraySpeciesLookupChainIntact()136 bool Isolate::IsArraySpeciesLookupChainIntact() {
137   // Note: It would be nice to have debug checks to make sure that the
138   // species protector is accurate, but this would be hard to do for most of
139   // what the protector stands for:
140   // - You'd need to traverse the heap to check that no Array instance has
141   //   a constructor property
142   // - To check that Array[Symbol.species] == Array, JS code has to execute,
143   //   but JS cannot be invoked in callstack overflow situations
144   // All that could be checked reliably is that
145   // Array.prototype.constructor == Array. Given that limitation, no check is
146   // done here. In place, there are mjsunit tests harmony/array-species* which
147   // ensure that behavior is correct in various invalid protector cases.
148 
149   PropertyCell* species_cell = heap()->array_species_protector();
150   return species_cell->value()->IsSmi() &&
151          Smi::ToInt(species_cell->value()) == kProtectorValid;
152 }
153 
IsTypedArraySpeciesLookupChainIntact()154 bool Isolate::IsTypedArraySpeciesLookupChainIntact() {
155   PropertyCell* species_cell = heap()->typed_array_species_protector();
156   return species_cell->value()->IsSmi() &&
157          Smi::ToInt(species_cell->value()) == kProtectorValid;
158 }
159 
IsPromiseSpeciesLookupChainIntact()160 bool Isolate::IsPromiseSpeciesLookupChainIntact() {
161   PropertyCell* species_cell = heap()->promise_species_protector();
162   return species_cell->value()->IsSmi() &&
163          Smi::ToInt(species_cell->value()) == kProtectorValid;
164 }
165 
IsStringLengthOverflowIntact()166 bool Isolate::IsStringLengthOverflowIntact() {
167   Cell* string_length_cell = heap()->string_length_protector();
168   return string_length_cell->value() == Smi::FromInt(kProtectorValid);
169 }
170 
IsArrayBufferNeuteringIntact()171 bool Isolate::IsArrayBufferNeuteringIntact() {
172   PropertyCell* buffer_neutering = heap()->array_buffer_neutering_protector();
173   return buffer_neutering->value() == Smi::FromInt(kProtectorValid);
174 }
175 
IsArrayIteratorLookupChainIntact()176 bool Isolate::IsArrayIteratorLookupChainIntact() {
177   PropertyCell* array_iterator_cell = heap()->array_iterator_protector();
178   return array_iterator_cell->value() == Smi::FromInt(kProtectorValid);
179 }
180 
181 }  // namespace internal
182 }  // namespace v8
183 
184 #endif  // V8_ISOLATE_INL_H_
185