summaryrefslogtreecommitdiffstats
path: root/js/src/vm/Activation-inl.h
blob: 1ee1439beb81e4f385b415998ef87d885f90e07f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef vm_Activation_inl_h
#define vm_Activation_inl_h

#include "vm/Activation.h"

#include "mozilla/Assertions.h"  // MOZ_ASSERT{,_IF}, MOZ_CRASH
#include "mozilla/Likely.h"      // MOZ_UNLIKELY
#include "mozilla/Maybe.h"       // mozilla::Maybe

#include "jit/CalleeToken.h"   // js::jit::CalleeToken
#include "js/Debug.h"          // JS::dbg::AutoEntryMonitor
#include "vm/FrameIter.h"      // js::FrameIter
#include "vm/JitActivation.h"  // js::jit::JitActivation
#include "vm/JSContext.h"      // JSContext
#include "vm/Stack.h"          // js::AbstractFramePtr

namespace js {

inline ActivationEntryMonitor::ActivationEntryMonitor(JSContext* cx)
    : cx_(cx), entryMonitor_(cx->entryMonitor) {
  cx->entryMonitor = nullptr;
}

inline ActivationEntryMonitor::ActivationEntryMonitor(
    JSContext* cx, InterpreterFrame* entryFrame)
    : ActivationEntryMonitor(cx) {
  if (MOZ_UNLIKELY(entryMonitor_)) {
    init(cx, entryFrame);
  }
}

inline ActivationEntryMonitor::ActivationEntryMonitor(
    JSContext* cx, jit::CalleeToken entryToken)
    : ActivationEntryMonitor(cx) {
  if (MOZ_UNLIKELY(entryMonitor_)) {
    init(cx, entryToken);
  }
}

inline ActivationEntryMonitor::~ActivationEntryMonitor() {
  if (entryMonitor_) {
    entryMonitor_->Exit(cx_);
  }

  cx_->entryMonitor = entryMonitor_;
}

inline Activation::Activation(JSContext* cx, Kind kind)
    : cx_(cx),
      compartment_(cx->compartment()),
      prev_(cx->activation_),
      prevProfiling_(prev_ ? prev_->mostRecentProfiling() : nullptr),
      hideScriptedCallerCount_(0),
      frameCache_(cx),
      asyncStack_(cx, cx->asyncStackForNewActivations()),
      asyncCause_(cx->asyncCauseForNewActivations),
      asyncCallIsExplicit_(cx->asyncCallIsExplicit),
      kind_(kind) {
  cx->asyncStackForNewActivations() = nullptr;
  cx->asyncCauseForNewActivations = nullptr;
  cx->asyncCallIsExplicit = false;
  cx->activation_ = this;
}

inline Activation::~Activation() {
  MOZ_ASSERT_IF(isProfiling(), this != cx_->profilingActivation_);
  MOZ_ASSERT(cx_->activation_ == this);
  MOZ_ASSERT(hideScriptedCallerCount_ == 0);
  cx_->activation_ = prev_;
  cx_->asyncCauseForNewActivations = asyncCause_;
  cx_->asyncStackForNewActivations() = asyncStack_;
  cx_->asyncCallIsExplicit = asyncCallIsExplicit_;
}

inline bool Activation::isProfiling() const {
  if (isInterpreter()) {
    return asInterpreter()->isProfiling();
  }

  MOZ_ASSERT(isJit());
  return asJit()->isProfiling();
}

inline Activation* Activation::mostRecentProfiling() {
  if (isProfiling()) {
    return this;
  }
  return prevProfiling_;
}

inline LiveSavedFrameCache* Activation::getLiveSavedFrameCache(JSContext* cx) {
  if (!frameCache_.get().initialized() && !frameCache_.get().init(cx)) {
    return nullptr;
  }
  return frameCache_.address();
}

/* static */ inline mozilla::Maybe<LiveSavedFrameCache::FramePtr>
LiveSavedFrameCache::FramePtr::create(const FrameIter& iter) {
  if (iter.done()) {
    return mozilla::Nothing();
  }

  if (iter.isPhysicalJitFrame()) {
    return mozilla::Some(FramePtr(iter.physicalJitFrame()));
  }

  if (!iter.hasUsableAbstractFramePtr()) {
    return mozilla::Nothing();
  }

  auto afp = iter.abstractFramePtr();

  if (afp.isInterpreterFrame()) {
    return mozilla::Some(FramePtr(afp.asInterpreterFrame()));
  }
  if (afp.isWasmDebugFrame()) {
    return mozilla::Some(FramePtr(afp.asWasmDebugFrame()));
  }
  if (afp.isRematerializedFrame()) {
    return mozilla::Some(FramePtr(afp.asRematerializedFrame()));
  }

  MOZ_CRASH("unexpected frame type");
}

struct LiveSavedFrameCache::FramePtr::HasCachedMatcher {
  template <typename Frame>
  bool operator()(Frame* f) const {
    return f->hasCachedSavedFrame();
  }
};

inline bool LiveSavedFrameCache::FramePtr::hasCachedSavedFrame() const {
  return ptr.match(HasCachedMatcher());
}

struct LiveSavedFrameCache::FramePtr::SetHasCachedMatcher {
  template <typename Frame>
  void operator()(Frame* f) {
    f->setHasCachedSavedFrame();
  }
};

inline void LiveSavedFrameCache::FramePtr::setHasCachedSavedFrame() {
  ptr.match(SetHasCachedMatcher());
}

struct LiveSavedFrameCache::FramePtr::ClearHasCachedMatcher {
  template <typename Frame>
  void operator()(Frame* f) {
    f->clearHasCachedSavedFrame();
  }
};

inline void LiveSavedFrameCache::FramePtr::clearHasCachedSavedFrame() {
  ptr.match(ClearHasCachedMatcher());
}

inline bool Activation::hasWasmExitFP() const {
  return isJit() && asJit()->hasWasmExitFP();
}

}  // namespace js

#endif  // vm_Activation_inl_h