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