Debian Codegraph @

js/src/vm/Stack-inl.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef vm_Stack_inl_h
8 #define vm_Stack_inl_h
9
10 #include "vm/Stack.h"
11
12 #include "mozilla/PodOperations.h"
13
14 #include "jit/BaselineFrame.h"
15 #include "jit/RematerializedFrame.h"
16 #include "js/friend/StackLimits.h" // js::ReportOverRecursed
17 #include "vm/EnvironmentObject.h"
18 #include "vm/Interpreter.h"
19 #include "vm/JSContext.h"
20 #include "vm/JSScript.h"
21
22 #include "jit/BaselineFrame-inl.h"
23 #include "jit/RematerializedFrame-inl.h" // js::jit::RematerializedFrame::unsetIsDebuggee
24 #include "vm/JSScript-inl.h"
25 #include "vm/NativeObject-inl.h"
26
27 namespace js {
28
29 inline HandleObject InterpreterFrame::environmentChain() const {
30 return HandleObject::fromMarkedLocation(&envChain_);
31 }
32
33 inline GlobalObject& InterpreterFrame::global() const {
34 return script()->global();
35 }
36
37 inline ExtensibleLexicalEnvironmentObject&
38 InterpreterFrame::extensibleLexicalEnvironment() const {
39 return NearestEnclosingExtensibleLexicalEnvironment(environmentChain());
40 }
41
42 inline void InterpreterFrame::initCallFrame(InterpreterFrame* prev,
43 jsbytecode* prevpc, Value* prevsp,
44 JSFunction& callee,
45 JSScript* script, Value* argv,
46 uint32_t nactual,
47 MaybeConstruct constructing) {
48 MOZ_ASSERT(callee.baseScript() == script);
49
50 /* Initialize stack frame members. */
51 flags_ = 0;
52 if (constructing) {
53 flags_ |= CONSTRUCTING;
54 }
55 argv_ = argv;
56 script_ = script;
57 nactual_ = nactual;
58 envChain_ = callee.environment();
59 prev_ = prev;
60 prevpc_ = prevpc;
61 prevsp_ = prevsp;
62
63 if (script->isDebuggee()) {
64 setIsDebuggee();
65 }
66
67 initLocals();
68 }
69
70 inline void InterpreterFrame::initLocals() {
71 SetValueRangeToUndefined(slots(), script()->nfixed());
72 }
73
74 inline Value& InterpreterFrame::unaliasedLocal(uint32_t i) {
75 MOZ_ASSERT(i < script()->nfixed());
76 return slots()[i];
77 }
78
79 inline Value& InterpreterFrame::unaliasedFormal(
80 unsigned i, MaybeCheckAliasing checkAliasing) {
81 MOZ_ASSERT(i < numFormalArgs());
82 MOZ_ASSERT_IF(checkAliasing, !script()->argsObjAliasesFormals());
83 MOZ_ASSERT_IF(checkAliasing, !script()->formalIsAliased(i));
84 return argv()[i];
85 }
86
87 inline Value& InterpreterFrame::unaliasedActual(
88 unsigned i, MaybeCheckAliasing checkAliasing) {
89 MOZ_ASSERT(i < numActualArgs());
90 MOZ_ASSERT_IF(checkAliasing, !script()->argsObjAliasesFormals());
91 MOZ_ASSERT_IF(checkAliasing && i < numFormalArgs(),
92 !script()->formalIsAliased(i));
93 return argv()[i];
94 }
95
96 template <class Op>
97 inline void InterpreterFrame::unaliasedForEachActual(Op op) {
98 // Don't assert !script()->funHasAnyAliasedFormal() since this function is
99 // called from ArgumentsObject::createUnexpected() which can access aliased
100 // slots.
101
102 const Value* argsEnd = argv() + numActualArgs();
103 for (const Value* p = argv(); p < argsEnd; ++p) {
104 op(*p);
105 }
106 }
107
108 inline ArgumentsObject& InterpreterFrame::argsObj() const {
109 MOZ_ASSERT(script()->needsArgsObj());
110 MOZ_ASSERT(flags_ & HAS_ARGS_OBJ);
111 return *argsObj_;
112 }
113
114 inline void InterpreterFrame::initArgsObj(ArgumentsObject& argsobj) {
115 MOZ_ASSERT(script()->needsArgsObj());
116 flags_ |= HAS_ARGS_OBJ;
117 argsObj_ = &argsobj;
118 }
119
120 inline EnvironmentObject& InterpreterFrame::aliasedEnvironment(
121 EnvironmentCoordinate ec) const {
122 JSObject* env = &environmentChain()->as<EnvironmentObject>();
123 for (unsigned i = ec.hops(); i; i--) {
124 env = &env->as<EnvironmentObject>().enclosingEnvironment();
125 }
126 return env->as<EnvironmentObject>();
127 }
128
129 inline EnvironmentObject& InterpreterFrame::aliasedEnvironmentMaybeDebug(
130 EnvironmentCoordinate ec) const {
131 JSObject* env = environmentChain();
132 for (unsigned i = ec.hops(); i; i--) {
133 if (env->is<EnvironmentObject>()) {
134 env = &env->as<EnvironmentObject>().enclosingEnvironment();
135 } else {
136 MOZ_ASSERT(env->is<DebugEnvironmentProxy>());
137 env = &env->as<DebugEnvironmentProxy>().enclosingEnvironment();
138 }
139 }
140 return env->is<EnvironmentObject>()
141 ? env->as<EnvironmentObject>()
142 : env->as<DebugEnvironmentProxy>().environment();
143 }
144
145 template <typename SpecificEnvironment>
146 inline void InterpreterFrame::pushOnEnvironmentChain(SpecificEnvironment& env) {
147 MOZ_ASSERT(*environmentChain() == env.enclosingEnvironment());
148 envChain_ = &env;
149 if (IsFrameInitialEnvironment(this, env)) {
150 flags_ |= HAS_INITIAL_ENV;
151 }
152 }
153
154 template <typename SpecificEnvironment>
155 inline void InterpreterFrame::popOffEnvironmentChain() {
156 MOZ_ASSERT(envChain_->is<SpecificEnvironment>());
157 envChain_ = &envChain_->as<SpecificEnvironment>().enclosingEnvironment();
158 }
159
160 inline void InterpreterFrame::replaceInnermostEnvironment(
161 BlockLexicalEnvironmentObject& env) {
162 MOZ_ASSERT(
163 env.enclosingEnvironment() ==
164 envChain_->as<BlockLexicalEnvironmentObject>().enclosingEnvironment());
165 envChain_ = &env;
166 }
167
168 bool InterpreterFrame::hasInitialEnvironment() const {
169 MOZ_ASSERT(script()->initialEnvironmentShape());
170 return flags_ & HAS_INITIAL_ENV;
171 }
172
173 inline CallObject& InterpreterFrame::callObj() const {
174 MOZ_ASSERT(callee().needsCallObject());
175
176 JSObject* pobj = environmentChain();
177 while (MOZ_UNLIKELY(!pobj->is<CallObject>())) {
178 pobj = pobj->enclosingEnvironment();
179 }
180 return pobj->as<CallObject>();
181 }
182
183 inline void InterpreterFrame::unsetIsDebuggee() {
184 MOZ_ASSERT(!script()->isDebuggee());
185 flags_ &= ~DEBUGGEE;
186 }
187
188 inline bool InterpreterFrame::saveGeneratorSlots(JSContext* cx, unsigned nslots,
189 ArrayObject* dest) const {
190 return dest->initDenseElementsFromRange(cx, slots(), slots() + nslots);
191 }
192
193 inline void InterpreterFrame::restoreGeneratorSlots(ArrayObject* src) {
194 MOZ_ASSERT(script()->nfixed() <= src->length());
195 MOZ_ASSERT(src->length() <= script()->nslots());
196 MOZ_ASSERT(src->getDenseInitializedLength() == src->length());
197 const Value* srcElements = src->getDenseElements();
198 mozilla::PodCopy(slots(), srcElements, src->length());
199 }
200
201 /*****************************************************************************/
202
203 inline void InterpreterStack::purge(JSRuntime* rt) {
204 rt->gc.queueUnusedLifoBlocksForFree(&allocator_);
205 }
206
207 uint8_t* InterpreterStack::allocateFrame(JSContext* cx, size_t size) {
208 size_t maxFrames;
209 if (cx->realm()->principals() == cx->runtime()->trustedPrincipals()) {
210 maxFrames = MAX_FRAMES_TRUSTED;
211 } else {
212 maxFrames = MAX_FRAMES;
213 }
214
215 if (MOZ_UNLIKELY(frameCount_ >= maxFrames)) {
216 ReportOverRecursed(cx);
217 return nullptr;
218 }
219
220 uint8_t* buffer = reinterpret_cast<uint8_t*>(allocator_.alloc(size));
221 if (!buffer) {
222 ReportOutOfMemory(cx);
223 return nullptr;
224 }
225
226 frameCount_++;
227 return buffer;
228 }
229
230 MOZ_ALWAYS_INLINE InterpreterFrame* InterpreterStack::getCallFrame(
231 JSContext* cx, const CallArgs& args, HandleScript script,
232 MaybeConstruct constructing, Value** pargv) {
233 JSFunction* fun = &args.callee().as<JSFunction>();
234
235 MOZ_ASSERT(fun->baseScript() == script);
236 unsigned nformal = fun->nargs();
237 unsigned nvals = script->nslots();
238
239 if (args.length() >= nformal) {
240 *pargv = args.array();
241 uint8_t* buffer =
242 allocateFrame(cx, sizeof(InterpreterFrame) + nvals * sizeof(Value));
243 return reinterpret_cast<InterpreterFrame*>(buffer);
244 }
245
246 // Pad any missing arguments with |undefined|.
247 MOZ_ASSERT(args.length() < nformal);
248
249 unsigned nfunctionState = 2 + constructing; // callee, |this|, |new.target|
250
251 nvals += nformal + nfunctionState;
252 uint8_t* buffer =
253 allocateFrame(cx, sizeof(InterpreterFrame) + nvals * sizeof(Value));
254 if (!buffer) {
255 return nullptr;
256 }
257
258 Value* argv = reinterpret_cast<Value*>(buffer);
259 unsigned nmissing = nformal - args.length();
260
261 mozilla::PodCopy(argv, args.base(), 2 + args.length());
262 SetValueRangeToUndefined(argv + 2 + args.length(), nmissing);
263
264 if (constructing) {
265 argv[2 + nformal] = args.newTarget();
266 }
267
268 *pargv = argv + 2;
269 return reinterpret_cast<InterpreterFrame*>(argv + nfunctionState + nformal);
270 }
271
272 MOZ_ALWAYS_INLINE bool InterpreterStack::pushInlineFrame(
273 JSContext* cx, InterpreterRegs& regs, const CallArgs& args,
274 HandleScript script, MaybeConstruct constructing) {
275 RootedFunction callee(cx, &args.callee().as<JSFunction>());
276 MOZ_ASSERT(regs.sp == args.end());
277 MOZ_ASSERT(callee->baseScript() == script);
278
279 InterpreterFrame* prev = regs.fp();
280 jsbytecode* prevpc = regs.pc;
281 Value* prevsp = regs.sp;
282 MOZ_ASSERT(prev);
283
284 LifoAlloc::Mark mark = allocator_.mark();
285
286 Value* argv;
287 InterpreterFrame* fp = getCallFrame(cx, args, script, constructing, &argv);
288 if (!fp) {
289 return false;
290 }
291
292 fp->mark_ = mark;
293
294 /* Initialize frame, locals, regs. */
295 fp->initCallFrame(prev, prevpc, prevsp, *callee, script, argv, args.length(),
296 constructing);
297
298 regs.prepareToRun(*fp, script);
299 return true;
300 }
301
302 MOZ_ALWAYS_INLINE bool InterpreterStack::resumeGeneratorCallFrame(
303 JSContext* cx, InterpreterRegs& regs, HandleFunction callee,
304 HandleObject envChain) {
305 MOZ_ASSERT(callee->isGenerator() || callee->isAsync());
306 RootedScript script(cx, callee->nonLazyScript());
307 InterpreterFrame* prev = regs.fp();
308 jsbytecode* prevpc = regs.pc;
309 Value* prevsp = regs.sp;
310 MOZ_ASSERT(prev);
311
312 LifoAlloc::Mark mark = allocator_.mark();
313
314 // (Async) generators and async functions are not constructors.
315 MOZ_ASSERT(!callee->isConstructor());
316
317 // Include callee, |this|, and maybe |new.target|
318 unsigned nformal = callee->nargs();
319 unsigned nvals = 2 + nformal + script->nslots();
320
321 uint8_t* buffer =
322 allocateFrame(cx, sizeof(InterpreterFrame) + nvals * sizeof(Value));
323 if (!buffer) {
324 return false;
325 }
326
327 Value* argv = reinterpret_cast<Value*>(buffer) + 2;
328 argv[-2] = ObjectValue(*callee);
329 argv[-1] = UndefinedValue();
330 SetValueRangeToUndefined(argv, nformal);
331
332 InterpreterFrame* fp = reinterpret_cast<InterpreterFrame*>(argv + nformal);
333 fp->mark_ = mark;
334 fp->initCallFrame(prev, prevpc, prevsp, *callee, script, argv, 0,
335 NO_CONSTRUCT);
336 fp->resumeGeneratorFrame(envChain);
337
338 regs.prepareToRun(*fp, script);
339 return true;
340 }
341
342 MOZ_ALWAYS_INLINE void InterpreterStack::popInlineFrame(InterpreterRegs& regs) {
343 InterpreterFrame* fp = regs.fp();
344 regs.popInlineFrame();
345 regs.sp[-1] = fp->returnValue();
346 releaseFrame(fp);
347 MOZ_ASSERT(regs.fp());
348 }
349
350 inline HandleValue AbstractFramePtr::returnValue() const {
351 if (isInterpreterFrame()) {
352 return asInterpreterFrame()->returnValue();
353 }
354 if (isWasmDebugFrame()) {
355 return asWasmDebugFrame()->returnValue();
356 }
357 return asBaselineFrame()->returnValue();
358 }
359
360 inline void AbstractFramePtr::setReturnValue(const Value& rval) const {
361 if (isInterpreterFrame()) {
362 asInterpreterFrame()->setReturnValue(rval);
363 return;
364 }
365 if (isBaselineFrame()) {
366 asBaselineFrame()->setReturnValue(rval);
367 return;
368 }
369 if (isWasmDebugFrame()) {
370 // TODO handle wasm function return value
371 // The function is called from Debugger::slowPathOnLeaveFrame --
372 // ignoring value for wasm.
373 return;
374 }
375 asRematerializedFrame()->setReturnValue(rval);
376 }
377
378 inline JSObject* AbstractFramePtr::environmentChain() const {
379 if (isInterpreterFrame()) {
380 return asInterpreterFrame()->environmentChain();
381 }
382 if (isBaselineFrame()) {
383 return asBaselineFrame()->environmentChain();
384 }
385 if (isWasmDebugFrame()) {
386 return &global()->lexicalEnvironment();
387 }
388 return asRematerializedFrame()->environmentChain();
389 }
390
391 template <typename SpecificEnvironment>
392 inline void AbstractFramePtr::pushOnEnvironmentChain(SpecificEnvironment& env) {
393 if (isInterpreterFrame()) {
394 asInterpreterFrame()->pushOnEnvironmentChain(env);
395 return;
396 }
397 if (isBaselineFrame()) {
398 asBaselineFrame()->pushOnEnvironmentChain(env);
399 return;
400 }
401 asRematerializedFrame()->pushOnEnvironmentChain(env);
402 }
403
404 template <typename SpecificEnvironment>
405 inline void AbstractFramePtr::popOffEnvironmentChain() {
406 if (isInterpreterFrame()) {
407 asInterpreterFrame()->popOffEnvironmentChain<SpecificEnvironment>();
408 return;
409 }
410 if (isBaselineFrame()) {
411 asBaselineFrame()->popOffEnvironmentChain<SpecificEnvironment>();
412 return;
413 }
414 asRematerializedFrame()->popOffEnvironmentChain<SpecificEnvironment>();
415 }
416
417 inline CallObject& AbstractFramePtr::callObj() const {
418 if (isInterpreterFrame()) {
419 return asInterpreterFrame()->callObj();
420 }
421 if (isBaselineFrame()) {
422 return asBaselineFrame()->callObj();
423 }
424 return asRematerializedFrame()->callObj();
425 }
426
427 inline bool AbstractFramePtr::initFunctionEnvironmentObjects(JSContext* cx) {
428 return js::InitFunctionEnvironmentObjects(cx, *this);
429 }
430
431 inline bool AbstractFramePtr::pushVarEnvironment(JSContext* cx,
432 Handle<Scope*> scope) {
433 return js::PushVarEnvironmentObject(cx, scope, *this);
434 }
435
436 inline JS::Realm* AbstractFramePtr::realm() const {
437 return environmentChain()->nonCCWRealm();
438 }
439
440 inline unsigned AbstractFramePtr::numActualArgs() const {
441 if (isInterpreterFrame()) {
442 return asInterpreterFrame()->numActualArgs();
443 }
444 if (isBaselineFrame()) {
445 return asBaselineFrame()->numActualArgs();
446 }
447 return asRematerializedFrame()->numActualArgs();
448 }
449
450 inline unsigned AbstractFramePtr::numFormalArgs() const {
451 if (isInterpreterFrame()) {
452 return asInterpreterFrame()->numFormalArgs();
453 }
454 if (isBaselineFrame()) {
455 return asBaselineFrame()->numFormalArgs();
456 }
457 return asRematerializedFrame()->numFormalArgs();
458 }
459
460 inline Value& AbstractFramePtr::unaliasedLocal(uint32_t i) {
461 if (isInterpreterFrame()) {
462 return asInterpreterFrame()->unaliasedLocal(i);
463 }
464 if (isBaselineFrame()) {
465 return asBaselineFrame()->unaliasedLocal(i);
466 }
467 return asRematerializedFrame()->unaliasedLocal(i);
468 }
469
470 inline Value& AbstractFramePtr::unaliasedFormal(
471 unsigned i, MaybeCheckAliasing checkAliasing) {
472 if (isInterpreterFrame()) {
473 return asInterpreterFrame()->unaliasedFormal(i, checkAliasing);
474 }
475 if (isBaselineFrame()) {
476 return asBaselineFrame()->unaliasedFormal(i, checkAliasing);
477 }
478 return asRematerializedFrame()->unaliasedFormal(i, checkAliasing);
479 }
480
481 inline Value& AbstractFramePtr::unaliasedActual(
482 unsigned i, MaybeCheckAliasing checkAliasing) {
483 if (isInterpreterFrame()) {
484 return asInterpreterFrame()->unaliasedActual(i, checkAliasing);
485 }
486 if (isBaselineFrame()) {
487 return asBaselineFrame()->unaliasedActual(i, checkAliasing);
488 }
489 return asRematerializedFrame()->unaliasedActual(i, checkAliasing);
490 }
491
492 inline bool AbstractFramePtr::hasInitialEnvironment() const {
493 if (isInterpreterFrame()) {
494 return asInterpreterFrame()->hasInitialEnvironment();
495 }
496 if (isBaselineFrame()) {
497 return asBaselineFrame()->hasInitialEnvironment();
498 }
499 return asRematerializedFrame()->hasInitialEnvironment();
500 }
501
502 inline bool AbstractFramePtr::isGlobalFrame() const {
503 if (isInterpreterFrame()) {
504 return asInterpreterFrame()->isGlobalFrame();
505 }
506 if (isBaselineFrame()) {
507 return asBaselineFrame()->isGlobalFrame();
508 }
509 if (isWasmDebugFrame()) {
510 return false;
511 }
512 return asRematerializedFrame()->isGlobalFrame();
513 }
514
515 inline bool AbstractFramePtr::isModuleFrame() const {
516 if (isInterpreterFrame()) {
517 return asInterpreterFrame()->isModuleFrame();
518 }
519 if (isBaselineFrame()) {
520 return asBaselineFrame()->isModuleFrame();
521 }
522 if (isWasmDebugFrame()) {
523 return false;
524 }
525 return asRematerializedFrame()->isModuleFrame();
526 }
527
528 inline bool AbstractFramePtr::isEvalFrame() const {
529 if (isInterpreterFrame()) {
530 return asInterpreterFrame()->isEvalFrame();
531 }
532 if (isBaselineFrame()) {
533 return asBaselineFrame()->isEvalFrame();
534 }
535 if (isWasmDebugFrame()) {
536 return false;
537 }
538 MOZ_ASSERT(isRematerializedFrame());
539 return false;
540 }
541
542 inline bool AbstractFramePtr::isDebuggerEvalFrame() const {
543 if (isInterpreterFrame()) {
544 return asInterpreterFrame()->isDebuggerEvalFrame();
545 }
546 if (isBaselineFrame()) {
547 return asBaselineFrame()->isDebuggerEvalFrame();
548 }
549 MOZ_ASSERT(isRematerializedFrame());
550 return false;
551 }
552
553 inline bool AbstractFramePtr::isDebuggee() const {
554 if (isInterpreterFrame()) {
555 return asInterpreterFrame()->isDebuggee();
556 }
557 if (isBaselineFrame()) {
558 return asBaselineFrame()->isDebuggee();
559 }
560 if (isWasmDebugFrame()) {
561 return asWasmDebugFrame()->isDebuggee();
562 }
563 return asRematerializedFrame()->isDebuggee();
564 }
565
566 inline void AbstractFramePtr::setIsDebuggee() {
567 if (isInterpreterFrame()) {
568 asInterpreterFrame()->setIsDebuggee();
569 } else if (isBaselineFrame()) {
570 asBaselineFrame()->setIsDebuggee();
571 } else if (isWasmDebugFrame()) {
572 asWasmDebugFrame()->setIsDebuggee();
573 } else {
574 asRematerializedFrame()->setIsDebuggee();
575 }
576 }
577
578 inline void AbstractFramePtr::unsetIsDebuggee() {
579 if (isInterpreterFrame()) {
580 asInterpreterFrame()->unsetIsDebuggee();
581 } else if (isBaselineFrame()) {
582 asBaselineFrame()->unsetIsDebuggee();
583 } else if (isWasmDebugFrame()) {
584 asWasmDebugFrame()->unsetIsDebuggee();
585 } else {
586 asRematerializedFrame()->unsetIsDebuggee();
587 }
588 }
589
590 inline bool AbstractFramePtr::isConstructing() const {
591 if (isInterpreterFrame()) {
592 return asInterpreterFrame()->isConstructing();
593 }
594 if (isBaselineFrame()) {
595 return asBaselineFrame()->isConstructing();
596 }
597 if (isRematerializedFrame()) {
598 return asRematerializedFrame()->isConstructing();
599 }
600 MOZ_CRASH("Unexpected frame");
601 }
602
603 inline bool AbstractFramePtr::hasCachedSavedFrame() const {
604 if (isInterpreterFrame()) {
605 return asInterpreterFrame()->hasCachedSavedFrame();
606 }
607 if (isBaselineFrame()) {
608 return asBaselineFrame()->framePrefix()->hasCachedSavedFrame();
609 }
610 if (isWasmDebugFrame()) {
611 return asWasmDebugFrame()->hasCachedSavedFrame();
612 }
613 return asRematerializedFrame()->hasCachedSavedFrame();
614 }
615
616 inline bool AbstractFramePtr::hasArgs() const { return isFunctionFrame(); }
617
618 inline bool AbstractFramePtr::hasScript() const { return !isWasmDebugFrame(); }
619
620 inline JSScript* AbstractFramePtr::script() const {
621 if (isInterpreterFrame()) {
622 return asInterpreterFrame()->script();
623 }
624 if (isBaselineFrame()) {
625 return asBaselineFrame()->script();
626 }
627 return asRematerializedFrame()->script();
628 }
629
630 inline wasm::Instance* AbstractFramePtr::wasmInstance() const {
631 return asWasmDebugFrame()->instance();
632 }
633
634 inline GlobalObject* AbstractFramePtr::global() const {
635 if (isWasmDebugFrame()) {
636 return asWasmDebugFrame()->global();
637 }
638 return &script()->global();
639 }
640
641 inline bool AbstractFramePtr::hasGlobal(const GlobalObject* global) const {
642 if (isWasmDebugFrame()) {
643 return asWasmDebugFrame()->hasGlobal(global);
644 }
645 return script()->hasGlobal(global);
646 }
647
648 inline JSFunction* AbstractFramePtr::callee() const {
649 if (isInterpreterFrame()) {
650 return &asInterpreterFrame()->callee();
651 }
652 if (isBaselineFrame()) {
653 return asBaselineFrame()->callee();
654 }
655 return asRematerializedFrame()->callee();
656 }
657
658 inline Value AbstractFramePtr::calleev() const {
659 if (isInterpreterFrame()) {
660 return asInterpreterFrame()->calleev();
661 }
662 if (isBaselineFrame()) {
663 return asBaselineFrame()->calleev();
664 }
665 return asRematerializedFrame()->calleev();
666 }
667
668 inline bool AbstractFramePtr::isFunctionFrame() const {
669 if (isInterpreterFrame()) {
670 return asInterpreterFrame()->isFunctionFrame();
671 }
672 if (isBaselineFrame()) {
673 return asBaselineFrame()->isFunctionFrame();
674 }
675 if (isWasmDebugFrame()) {
676 return false;
677 }
678 return asRematerializedFrame()->isFunctionFrame();
679 }
680
681 inline bool AbstractFramePtr::isGeneratorFrame() const {
682 if (!isFunctionFrame() && !isModuleFrame()) {
683 return false;
684 }
685 JSScript* s = script();
686 return s->isGenerator() || s->isAsync();
687 }
688
689 inline bool AbstractFramePtr::saveGeneratorSlots(JSContext* cx, unsigned nslots,
690 ArrayObject* dest) const {
691 MOZ_ASSERT(isGeneratorFrame());
692 if (isInterpreterFrame()) {
693 return asInterpreterFrame()->saveGeneratorSlots(cx, nslots, dest);
694 }
695 MOZ_ASSERT(isBaselineFrame(), "unexpected generator frame in Ion");
696 return asBaselineFrame()->saveGeneratorSlots(cx, nslots, dest);
697 }
698
699 inline Value* AbstractFramePtr::argv() const {
700 if (isInterpreterFrame()) {
701 return asInterpreterFrame()->argv();
702 }
703 if (isBaselineFrame()) {
704 return asBaselineFrame()->argv();
705 }
706 return asRematerializedFrame()->argv();
707 }
708
709 inline bool AbstractFramePtr::hasArgsObj() const {
710 if (isInterpreterFrame()) {
711 return asInterpreterFrame()->hasArgsObj();
712 }
713 if (isBaselineFrame()) {
714 return asBaselineFrame()->hasArgsObj();
715 }
716 return asRematerializedFrame()->hasArgsObj();
717 }
718
719 inline ArgumentsObject& AbstractFramePtr::argsObj() const {
720 if (isInterpreterFrame()) {
721 return asInterpreterFrame()->argsObj();
722 }
723 if (isBaselineFrame()) {
724 return asBaselineFrame()->argsObj();
725 }
726 return asRematerializedFrame()->argsObj();
727 }
728
729 inline void AbstractFramePtr::initArgsObj(ArgumentsObject& argsobj) const {
730 if (isInterpreterFrame()) {
731 asInterpreterFrame()->initArgsObj(argsobj);
732 return;
733 }
734 asBaselineFrame()->initArgsObj(argsobj);
735 }
736
737 inline bool AbstractFramePtr::prevUpToDate() const {
738 if (isInterpreterFrame()) {
739 return asInterpreterFrame()->prevUpToDate();
740 }
741 if (isBaselineFrame()) {
742 return asBaselineFrame()->prevUpToDate();
743 }
744 if (isWasmDebugFrame()) {
745 return asWasmDebugFrame()->prevUpToDate();
746 }
747 return asRematerializedFrame()->prevUpToDate();
748 }
749
750 inline void AbstractFramePtr::setPrevUpToDate() const {
751 if (isInterpreterFrame()) {
752 asInterpreterFrame()->setPrevUpToDate();
753 return;
754 }
755 if (isBaselineFrame()) {
756 asBaselineFrame()->setPrevUpToDate();
757 return;
758 }
759 if (isWasmDebugFrame()) {
760 asWasmDebugFrame()->setPrevUpToDate();
761 return;
762 }
763 asRematerializedFrame()->setPrevUpToDate();
764 }
765
766 inline void AbstractFramePtr::unsetPrevUpToDate() const {
767 if (isInterpreterFrame()) {
768 asInterpreterFrame()->unsetPrevUpToDate();
769 return;
770 }
771 if (isBaselineFrame()) {
772 asBaselineFrame()->unsetPrevUpToDate();
773 return;
774 }
775 if (isWasmDebugFrame()) {
776 asWasmDebugFrame()->unsetPrevUpToDate();
777 return;
778 }
779 asRematerializedFrame()->unsetPrevUpToDate();
780 }
781
782 inline Value& AbstractFramePtr::thisArgument() const {
783 if (isInterpreterFrame()) {
784 return asInterpreterFrame()->thisArgument();
785 }
786 if (isBaselineFrame()) {
787 return asBaselineFrame()->thisArgument();
788 }
789 return asRematerializedFrame()->thisArgument();
790 }
791
792 inline bool AbstractFramePtr::debuggerNeedsCheckPrimitiveReturn() const {
793 if (isWasmDebugFrame()) {
794 return false;
795 }
796 return script()->isDerivedClassConstructor();
797 }
798
799 InterpreterActivation::InterpreterActivation(RunState& state, JSContext* cx,
800 InterpreterFrame* entryFrame)
801 : Activation(cx, Interpreter),
802 entryFrame_(entryFrame),
803 opMask_(0)
804 #ifdef DEBUG
805 ,
806 oldFrameCount_(cx->interpreterStack().frameCount_)
807 #endif
808 {
809 regs_.prepareToRun(*entryFrame, state.script());
810 MOZ_ASSERT(regs_.pc == state.script()->code());
811 }
812
813 InterpreterActivation::~InterpreterActivation() {
814 // Pop all inline frames.
815 while (regs_.fp() != entryFrame_) {
816 popInlineFrame(regs_.fp());
817 }
818
819 MOZ_ASSERT(oldFrameCount_ == cx_->interpreterStack().frameCount_);
820 MOZ_ASSERT_IF(oldFrameCount_ == 0,
821 cx_->interpreterStack().allocator_.used() == 0);
822
823 if (entryFrame_) {
824 cx_->interpreterStack().releaseFrame(entryFrame_);
825 }
826 }
827
828 inline bool InterpreterActivation::pushInlineFrame(
829 const CallArgs& args, HandleScript script, MaybeConstruct constructing) {
830 if (!cx_->interpreterStack().pushInlineFrame(cx_, regs_, args, script,
831 constructing)) {
832 return false;
833 }
834 MOZ_ASSERT(regs_.fp()->script()->compartment() == compartment());
835 return true;
836 }
837
838 inline void InterpreterActivation::popInlineFrame(InterpreterFrame* frame) {
839 (void)frame; // Quell compiler warning.
840 MOZ_ASSERT(regs_.fp() == frame);
841 MOZ_ASSERT(regs_.fp() != entryFrame_);
842
843 cx_->interpreterStack().popInlineFrame(regs_);
844 }
845
846 inline bool InterpreterActivation::resumeGeneratorFrame(HandleFunction callee,
847 HandleObject envChain) {
848 InterpreterStack& stack = cx_->interpreterStack();
849 if (!stack.resumeGeneratorCallFrame(cx_, regs_, callee, envChain)) {
850 return false;
851 }
852
853 MOZ_ASSERT(regs_.fp()->script()->compartment() == compartment_);
854 return true;
855 }
856
857 } /* namespace js */
858
859 #endif /* vm_Stack_inl_h */