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_EXECUTION_ISOLATE_INL_H_
6#define V8_EXECUTION_ISOLATE_INL_H_
7
8#include "src/execution/isolate.h"
9#include "src/objects/contexts-inl.h"
10#include "src/objects/js-function.h"
11#include "src/objects/objects-inl.h"
12#include "src/objects/oddball.h"
13#include "src/objects/property-cell.h"
14#include "src/objects/regexp-match-info.h"
15#include "src/objects/shared-function-info.h"
16#include "src/objects/source-text-module-inl.h"
17
18namespace v8 {
19namespace internal {
20
21void Isolate::set_context(Context context) {
22  DCHECK(context.is_null() || context.IsContext());
23  thread_local_top()->context_ = context;
24}
25
26Handle<NativeContext> Isolate::native_context() {
27  DCHECK(!context().is_null());
28  return handle(context().native_context(), this);
29}
30
31NativeContext Isolate::raw_native_context() {
32  DCHECK(!context().is_null());
33  return context().native_context();
34}
35
36void Isolate::set_pending_message(Object message_obj) {
37  DCHECK(message_obj.IsTheHole(this) || message_obj.IsJSMessageObject());
38  thread_local_top()->pending_message_ = message_obj;
39}
40
41Object Isolate::pending_message() {
42  return thread_local_top()->pending_message_;
43}
44
45void Isolate::clear_pending_message() {
46  set_pending_message(ReadOnlyRoots(this).the_hole_value());
47}
48
49bool Isolate::has_pending_message() {
50  return !pending_message().IsTheHole(this);
51}
52
53Object Isolate::pending_exception() {
54  CHECK(has_pending_exception());
55  DCHECK(!thread_local_top()->pending_exception_.IsException(this));
56  return thread_local_top()->pending_exception_;
57}
58
59void Isolate::set_pending_exception(Object exception_obj) {
60  DCHECK(!exception_obj.IsException(this));
61  thread_local_top()->pending_exception_ = exception_obj;
62}
63
64void Isolate::clear_pending_exception() {
65  DCHECK(!thread_local_top()->pending_exception_.IsException(this));
66  thread_local_top()->pending_exception_ = ReadOnlyRoots(this).the_hole_value();
67}
68
69bool Isolate::has_pending_exception() {
70  DCHECK(!thread_local_top()->pending_exception_.IsException(this));
71  return !thread_local_top()->pending_exception_.IsTheHole(this);
72}
73
74Object Isolate::scheduled_exception() {
75  DCHECK(has_scheduled_exception());
76  DCHECK(!thread_local_top()->scheduled_exception_.IsException(this));
77  return thread_local_top()->scheduled_exception_;
78}
79
80bool 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
86void Isolate::clear_scheduled_exception() {
87  DCHECK(!thread_local_top()->scheduled_exception_.IsException(this));
88  set_scheduled_exception(ReadOnlyRoots(this).the_hole_value());
89}
90
91void Isolate::set_scheduled_exception(Object exception) {
92  thread_local_top()->scheduled_exception_ = exception;
93}
94
95bool Isolate::is_catchable_by_javascript(Object exception) {
96  return exception != ReadOnlyRoots(heap()).termination_exception();
97}
98
99bool Isolate::is_catchable_by_wasm(Object exception) {
100  if (!is_catchable_by_javascript(exception)) return false;
101  if (!exception.IsJSObject()) return true;
102  // We don't allocate, but the LookupIterator interface expects a handle.
103  DisallowGarbageCollection no_gc;
104  HandleScope handle_scope(this);
105  LookupIterator it(this, handle(JSReceiver::cast(exception), this),
106                    factory()->wasm_uncatchable_symbol(),
107                    LookupIterator::OWN_SKIP_INTERCEPTOR);
108  return !JSReceiver::HasProperty(&it).FromJust();
109}
110
111void Isolate::FireBeforeCallEnteredCallback() {
112  for (auto& callback : before_call_entered_callbacks_) {
113    callback(reinterpret_cast<v8::Isolate*>(this));
114  }
115}
116
117Handle<JSGlobalObject> Isolate::global_object() {
118  return handle(context().global_object(), this);
119}
120
121Handle<JSGlobalProxy> Isolate::global_proxy() {
122  return handle(context().global_proxy(), this);
123}
124
125Isolate::ExceptionScope::ExceptionScope(Isolate* isolate)
126    : isolate_(isolate),
127      pending_exception_(isolate_->pending_exception(), isolate_) {}
128
129Isolate::ExceptionScope::~ExceptionScope() {
130  isolate_->set_pending_exception(*pending_exception_);
131}
132
133bool Isolate::IsAnyInitialArrayPrototype(JSArray array) {
134  DisallowGarbageCollection no_gc;
135  return IsInAnyContext(array, Context::INITIAL_ARRAY_PROTOTYPE_INDEX);
136}
137
138void Isolate::DidFinishModuleAsyncEvaluation(unsigned ordinal) {
139  // To address overflow, the ordinal is reset when the async module with the
140  // largest vended ordinal finishes evaluating. Modules are evaluated in
141  // ascending order of their async_evaluating_ordinal.
142  //
143  // While the specification imposes a global total ordering, the intention is
144  // that for each async module, all its parents are totally ordered by when
145  // they first had their [[AsyncEvaluating]] bit set.
146  //
147  // The module with largest vended ordinal finishes evaluating implies that the
148  // async dependency as well as all other modules in that module's graph
149  // depending on async dependencies are finished evaluating.
150  //
151  // If the async dependency participates in other module graphs (e.g. via
152  // dynamic import, or other <script type=module> tags), those module graphs
153  // must have been evaluated either before or after the async dependency is
154  // settled, as the concrete Evaluate() method on cyclic module records is
155  // neither reentrant nor performs microtask checkpoints during its
156  // evaluation. If before, then all modules that depend on the async
157  // dependencies were given an ordinal that ensure they are relatively ordered,
158  // before the global ordinal was reset. If after, then the async evaluating
159  // ordering does not apply, as the dependency is no longer asynchronous.
160  //
161  // https://tc39.es/ecma262/#sec-moduleevaluation
162  if (ordinal + 1 == next_module_async_evaluating_ordinal_) {
163    next_module_async_evaluating_ordinal_ =
164        SourceTextModule::kFirstAsyncEvaluatingOrdinal;
165  }
166}
167
168#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name)    \
169  Handle<type> Isolate::name() {                            \
170    return Handle<type>(raw_native_context().name(), this); \
171  }                                                         \
172  bool Isolate::is_##name(type value) {                     \
173    return raw_native_context().is_##name(value);           \
174  }
175NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
176#undef NATIVE_CONTEXT_FIELD_ACCESSOR
177
178}  // namespace internal
179}  // namespace v8
180
181#endif  // V8_EXECUTION_ISOLATE_INL_H_
182