Debian Codegraph @

js/src/frontend/UsingEmitter.cpp

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "frontend/UsingEmitter.h"
6
7 #include "frontend/BytecodeEmitter.h"
8 #include "frontend/EmitterScope.h"
9 #include "frontend/IfEmitter.h"
10 #include "frontend/TryEmitter.h"
11 #include "frontend/WhileEmitter.h"
12
13 using namespace js;
14 using namespace js::frontend;
15
16 UsingEmitter::UsingEmitter(BytecodeEmitter* bce) : bce_(bce) {}
17
18 bool UsingEmitter::emitTakeDisposeCapability() {
19 if (!bce_->emit1(JSOp::TakeDisposeCapability)) {
20 // [stack] RESOURCES-OR-UNDEF
21 return false;
22 }
23
24 if (!bce_->emit1(JSOp::IsNullOrUndefined)) {
25 // [stack] RESOURCES-OR-UNDEF IS-UNDEF
26 return false;
27 }
28
29 InternalIfEmitter ifUndefined(bce_);
30
31 if (!ifUndefined.emitThenElse()) {
32 // [stack] UNDEFINED
33 return false;
34 }
35
36 if (!bce_->emit1(JSOp::Zero)) {
37 // [stack] UNDEFINED 0
38 return false;
39 }
40
41 if (!ifUndefined.emitElse()) {
42 // [stack] RESOURCES
43 return false;
44 }
45
46 if (!bce_->emit1(JSOp::Dup)) {
47 // [stack] RESOURCES RESOURCES
48 return false;
49 }
50
51 if (!bce_->emitAtomOp(JSOp::GetProp,
52 TaggedParserAtomIndex::WellKnown::length())) {
53 // [stack] RESOURCES COUNT
54 return false;
55 }
56
57 if (!ifUndefined.emitEnd()) {
58 // [stack] RESOURCES COUNT
59 return false;
60 }
61
62 return true;
63 }
64
65 bool UsingEmitter::emitThrowIfException() {
66 // [stack] EXC THROWING
67
68 InternalIfEmitter ifThrow(bce_);
69
70 if (!ifThrow.emitThenElse()) {
71 // [stack] EXC
72 return false;
73 }
74
75 if (!bce_->emit1(JSOp::Throw)) {
76 // [stack]
77 return false;
78 }
79
80 if (!ifThrow.emitElse()) {
81 // [stack] EXC
82 return false;
83 }
84
85 if (!bce_->emit1(JSOp::Pop)) {
86 // [stack]
87 return false;
88 }
89
90 if (!ifThrow.emitEnd()) {
91 // [stack]
92 return false;
93 }
94
95 return true;
96 }
97
98 bool DisposalEmitter::emitResourcePropertyAccess(TaggedParserAtomIndex prop,
99 unsigned resourcesFromTop) {
100 // [stack] # if resourcesFromTop == 1
101 // [stack] RESOURCES INDEX
102 // [stack] # if resourcesFromTop > 1
103 // [stack] RESOURCES INDEX ... (resourcesFromTop - 1 values)
104 MOZ_ASSERT(resourcesFromTop >= 1);
105
106 if (!bce_->emitDupAt(resourcesFromTop, 2)) {
107 // [stack] RESOURCES INDEX ... RESOURCES INDEX
108 return false;
109 }
110
111 if (!bce_->emit1(JSOp::GetElem)) {
112 // [stack] RESOURCES INDEX ... RESOURCE
113 return false;
114 }
115
116 if (!bce_->emitAtomOp(JSOp::GetProp, prop)) {
117 // [stack] RESOURCES INDEX ... VALUE
118 return false;
119 }
120
121 return true;
122 }
123
124 // Explicit Resource Management Proposal
125 // DisposeResources ( disposeCapability, completion )
126 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-disposeresources
127 // Steps 1-2.
128 bool DisposalEmitter::prepareForDisposeCapability() {
129 MOZ_ASSERT(state_ == State::Start);
130
131 // [stack] THROWING EXC
132
133 if (hasAsyncDisposables_) {
134 // Step 1. Let needsAwait be false.
135 if (!bce_->emit1(JSOp::False)) {
136 // [stack] THROWING EXC NEEDS-AWAIT
137 return false;
138 }
139
140 // Step 2. Let hasAwaited be false.
141 if (!bce_->emit1(JSOp::False)) {
142 // [stack] THROWING EXC NEEDS-AWAIT HAS-AWAITED
143 return false;
144 }
145
146 if (!bce_->emitPickN(3)) {
147 // [stack] EXC NEEDS-AWAIT HAS-AWAITED THROWING
148 return false;
149 }
150
151 if (!bce_->emitPickN(3)) {
152 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC
153 return false;
154 }
155 }
156
157 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC
158
159 #ifdef DEBUG
160 state_ = State::DisposeCapability;
161 #endif
162 return true;
163 }
164
165 // Explicit Resource Management Proposal
166 // DisposeResources ( disposeCapability, completion )
167 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-disposeresources
168 // Steps 3-5, 7.
169 //
170 // This implementation of DisposeResources is designed for using and await using
171 // syntax as well as for disposals in DisposableStack and AsyncDisposableStack,
172 // it covers the complete algorithm as defined in the spec for both sync and
173 // async disposals as necessary in bytecode.
174 bool DisposalEmitter::emitEnd(EmitterScope& es) {
175 MOZ_ASSERT(state_ == State::DisposeCapability);
176
177 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC RESOURCES COUNT
178
179 // For the purpose of readbility some values are omitted from
180 // the stack comments and are assumed to be present,
181 // we mention the values in the comments as and when they are being
182 // operated upon.
183
184 // [stack] ... RESOURCES COUNT
185
186 // We do the iteration in reverse order as per spec,
187 // there can be the case when count is 0 and hence index
188 // below becomes -1 but the loop condition will ensure
189 // no code is executed in that case.
190 if (!bce_->emit1(JSOp::Dec)) {
191 // [stack] ... RESOURCES INDEX
192 return false;
193 }
194
195 InternalWhileEmitter wh(bce_);
196
197 // Step 3. For each element resource of
198 // disposeCapability.[[DisposableResourceStack]], in reverse list order, do
199 if (!wh.emitCond()) {
200 // [stack] ... RESOURCES INDEX
201 return false;
202 }
203
204 if (!bce_->emit1(JSOp::Dup)) {
205 // [stack] ... RESOURCES INDEX INDEX
206 return false;
207 }
208
209 if (!bce_->emit1(JSOp::Zero)) {
210 // [stack] ... RESOURCES INDEX INDEX 0
211 return false;
212 }
213
214 if (!bce_->emit1(JSOp::Ge)) {
215 // [stack] ... RESOURCES INDEX BOOL
216 return false;
217 }
218
219 if (!wh.emitBody()) {
220 // [stack] ... RESOURCES INDEX
221 return false;
222 }
223
224 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC RESOURCES INDEX
225
226 if (hasAsyncDisposables_) {
227 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX
228
229 // Step 3.b. Let hint be resource.[[Hint]].
230 // (reordered)
231 // Step 3.d. If hint is sync-dispose and needsAwait is true and hasAwaited
232 // is false, then
233 if (!emitResourcePropertyAccess(TaggedParserAtomIndex::WellKnown::hint())) {
234 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX HINT
235 return false;
236 }
237
238 // [stack] NEEDS-AWAIT HAS-AWAITED ... HINT
239
240 static_assert(uint8_t(UsingHint::Sync) == 0, "Sync hint must be 0");
241 static_assert(uint8_t(UsingHint::Async) == 1, "Async hint must be 1");
242 if (!bce_->emit1(JSOp::Not)) {
243 // [stack] NEEDS-AWAIT HAS-AWAITED ... IS-SYNC
244 return false;
245 }
246
247 if (!bce_->emitDupAt(6, 2)) {
248 // [stack] NEEDS-AWAIT HAS-AWAITED ... IS-SYNC NEEDS-AWAIT HAS-AWAITED
249 return false;
250 }
251
252 // [stack] ... IS-SYNC NEEDS-AWAIT HAS-AWAITED
253
254 if (!bce_->emit1(JSOp::Not)) {
255 // [stack] ... IS-SYNC NEEDS-AWAIT (!HAS-AWAITED)
256 return false;
257 }
258
259 // The use of BitAnd is a simple optimisation to avoid having
260 // jumps if we were to implement this using && operator. The value
261 // IS-SYNC is integer 0 or 1 (see static_assert above) and
262 // NEEDS-AWAIT and HAS-AWAITED are boolean values. thus
263 // the result of the operation is either 0 or 1 which is
264 // truthy value that can be consumed by the IfEmitter.
265 if (!bce_->emit1(JSOp::BitAnd)) {
266 // [stack] ... IS-SYNC (NEEDS-AWAIT & !HAS-AWAITED)
267 return false;
268 }
269
270 if (!bce_->emit1(JSOp::BitAnd)) {
271 // [stack] ... (IS-SYNC & NEEDS-AWAIT & !HAS-AWAITED)
272 return false;
273 }
274
275 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX AWAIT-NEEDED
276
277 InternalIfEmitter ifNeedsSyncDisposeUndefinedAwaited(bce_);
278
279 if (!ifNeedsSyncDisposeUndefinedAwaited.emitThen()) {
280 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX
281 return false;
282 }
283
284 // Step 3.d.i. Perform ! Await(undefined).
285 if (!bce_->emit1(JSOp::Undefined)) {
286 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX UNDEF
287 return false;
288 }
289
290 if (!bce_->emitAwaitInScope(es)) {
291 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX RESOLVED
292 return false;
293 }
294
295 // Step 3.d.ii. Set needsAwait to false.
296 if (!bce_->emitPickN(6)) {
297 // [stack] HAS-AWAITED THROWING EXC RESOURCES INDEX RESOLVED NEEDS-AWAIT
298 return false;
299 }
300
301 if (!bce_->emitPopN(2)) {
302 // [stack] HAS-AWAITED THROWING EXC RESOURCES INDEX
303 return false;
304 }
305
306 if (!bce_->emit1(JSOp::False)) {
307 // [stack] HAS-AWAITED THROWING EXC RESOURCES INDEX NEEDS-AWAIT
308 return false;
309 }
310
311 if (!bce_->emitUnpickN(5)) {
312 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX
313 return false;
314 }
315
316 if (!ifNeedsSyncDisposeUndefinedAwaited.emitEnd()) {
317 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX
318 return false;
319 }
320 }
321
322 // [stack] ... RESOURCES INDEX
323
324 // Step 3.c. Let method be resource.[[DisposeMethod]].
325 // (reordered)
326 // Step 3.e. If method is not undefined, then
327 if (!emitResourcePropertyAccess(TaggedParserAtomIndex::WellKnown::method())) {
328 // [stack] ... RESOURCES INDEX METHOD
329 return false;
330 }
331
332 if (!bce_->emit1(JSOp::IsNullOrUndefined)) {
333 // [stack] ... RESOURCES INDEX METHOD IS-UNDEF
334 return false;
335 }
336
337 InternalIfEmitter ifMethodNotUndefined(bce_);
338
339 if (!ifMethodNotUndefined.emitThenElse(IfEmitter::ConditionKind::Negative)) {
340 // [stack] ... RESOURCES INDEX METHOD
341 return false;
342 }
343
344 if (!bce_->emit1(JSOp::Pop)) {
345 // [stack] ... RESOURCES INDEX
346 return false;
347 }
348
349 TryEmitter tryCall(bce_, TryEmitter::Kind::TryCatch,
350 TryEmitter::ControlKind::NonSyntactic);
351
352 if (!tryCall.emitTry()) {
353 // [stack] ... RESOURCES INDEX
354 return false;
355 }
356
357 // Step 3.c. Let method be resource.[[DisposeMethod]].
358 // (reordered)
359 if (!emitResourcePropertyAccess(TaggedParserAtomIndex::WellKnown::method())) {
360 // [stack] ... RESOURCES INDEX METHOD
361 return false;
362 }
363
364 // Step 3.a. Let value be resource.[[ResourceValue]].
365 // (reordered)
366 if (!emitResourcePropertyAccess(TaggedParserAtomIndex::WellKnown::value(),
367 2)) {
368 // [stack] ... RESOURCES INDEX METHOD VALUE
369 return false;
370 }
371
372 // Step 3.e.i. Let result be Completion(Call(method, value)).
373 if (!bce_->emitCall(JSOp::Call, 0)) {
374 // [stack] ... RESOURCES INDEX RESULT
375 return false;
376 }
377
378 if (hasAsyncDisposables_) {
379 // Step 3.e.ii. If result is a normal completion and hint is async-dispose,
380 // then
381 if (!emitResourcePropertyAccess(TaggedParserAtomIndex::WellKnown::hint(),
382 2)) {
383 // [stack] ... RESOURCES INDEX RESULT HINT
384 return false;
385 }
386
387 // Hint value is either 0 or 1, which can be consumed by the IfEmitter,
388 // see static_assert above.
389 // [stack] ... RESOURCES INDEX RESULT IS-ASYNC
390
391 InternalIfEmitter ifAsyncDispose(bce_);
392
393 if (!ifAsyncDispose.emitThen()) {
394 // [stack] ... RESOURCES INDEX RESULT
395 return false;
396 }
397
398 // [stack] NEEDS-AWAIT THROWING EXC RESOURCES INDEX RESULT HAS-AWAITED
399
400 // Step 3.e.ii.2. Set hasAwaited to true. (reordered)
401 if (!bce_->emitPickN(5)) {
402 // [stack] NEEDS-AWAIT THROWING EXC RESOURCES INDEX RESULT HAS-AWAITED
403 return false;
404 }
405
406 if (!bce_->emit1(JSOp::Pop)) {
407 // [stack] NEEDS-AWAIT THROWING EXC RESOURCES INDEX RESULT
408 return false;
409 }
410
411 if (!bce_->emit1(JSOp::True)) {
412 // [stack] NEEDS-AWAIT THROWING EXC RESOURCES INDEX RESULT HAS-AWAITED
413 return false;
414 }
415
416 if (!bce_->emitUnpickN(5)) {
417 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX RESULT
418 return false;
419 }
420
421 // Step 3.e.ii.1. Set result to Completion(Await(result.[[Value]])).
422 if (!bce_->emitAwaitInScope(es)) {
423 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX RESOLVED
424 return false;
425 }
426
427 if (!ifAsyncDispose.emitEnd()) {
428 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX RESULT
429 return false;
430 }
431 }
432
433 // [stack] ... THROWING EXC RESOURCES INDEX RESULT
434
435 if (!bce_->emit1(JSOp::Pop)) {
436 // [stack] ... THROWING EXC RESOURCES INDEX
437 return false;
438 }
439
440 // Step 3.e.iii. If result is a throw completion, then
441 if (!tryCall.emitCatch()) {
442 // [stack] ... THROWING EXC RESOURCES INDEX EXC2
443 return false;
444 }
445
446 if (!bce_->emitPickN(3)) {
447 // [stack] ... THROWING RESOURCES INDEX EXC2 EXC
448 return false;
449 }
450
451 if (bce_->sc->isSuspendableContext() &&
452 bce_->sc->asSuspendableContext()->isGenerator()) {
453 // [stack] ... THROWING RESOURCES INDEX EXC2 EXC
454
455 // Generator closure is implemented by throwing a magic value
456 // thus when we have a throw completion we must check whether
457 // the pending exception is a generator closing exception and overwrite
458 // it with the normal exception here or else we will end up exposing
459 // the magic value to user program.
460 if (!bce_->emit1(JSOp::IsGenClosing)) {
461 // [stack] ... THROWING RESOURCES INDEX EXC2 EXC GEN-CLOSING
462 return false;
463 }
464
465 if (!bce_->emit1(JSOp::Not)) {
466 // [stack] ... THROWING RESOURCES INDEX EXC2 EXC !GEN-CLOSING
467 return false;
468 }
469
470 if (!bce_->emitPickN(5)) {
471 // [stack] ... RESOURCES INDEX EXC2 EXC (!GEN-CLOSING) THROWING
472 return false;
473 }
474
475 if (!bce_->emit1(JSOp::BitAnd)) {
476 // [stack] ... RESOURCES INDEX EXC2 EXC (!GEN-CLOSING & THROWING)
477 return false;
478 }
479 } else {
480 if (!bce_->emitPickN(4)) {
481 // [stack] ... RESOURCES INDEX EXC2 EXC THROWING
482 return false;
483 }
484 }
485
486 // [stack] NEEDS-AWAIT? HAS-AWAITED? RESOURCES INDEX EXC2 EXC THROWING
487
488 InternalIfEmitter ifException(bce_);
489
490 // Step 3.e.iii.1. If completion is a throw completion, then
491 if (!ifException.emitThenElse()) {
492 // [stack] NEEDS-AWAIT? HAS-AWAITED? RESOURCES INDEX EXC2 EXC
493 return false;
494 }
495
496 // Step 3.e.iii.1.a-f
497 if (!bce_->emit1(JSOp::CreateSuppressedError)) {
498 // [stack] NEEDS-AWAIT? HAS-AWAITED? RESOURCES INDEX SUPPRESSED
499 return false;
500 }
501
502 if (!bce_->emitUnpickN(2)) {
503 // [stack] NEEDS-AWAIT? HAS-AWAITED? SUPPRESSED RESOURCED INDEX
504 return false;
505 }
506
507 if (!bce_->emit1(JSOp::True)) {
508 // [stack] NEEDS-AWAIT? HAS-AWAITED? SUPPRESSED RESOURCED INDEX THROWING
509 return false;
510 }
511
512 if (!bce_->emitUnpickN(3)) {
513 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING SUPPRESSED RESOURCED INDEX
514 return false;
515 }
516
517 // Step 3.e.iii.2. Else,
518 // Step 3.e.iii.2.a. Set completion to result.
519 if (!ifException.emitElse()) {
520 // [stack] NEEDS-AWAIT? HAS-AWAITED? RESOURCES INDEX EXC2 EXC
521 return false;
522 }
523
524 if (!bce_->emit1(JSOp::Pop)) {
525 // [stack] NEEDS-AWAIT? HAS-AWAITED? RESOURCES INDEX EXC2
526 return false;
527 }
528
529 if (!bce_->emitUnpickN(2)) {
530 // [stack] NEEDS-AWAIT? HAS-AWAITED? EXC2 RESOURCES INDEX
531 return false;
532 }
533
534 if (!bce_->emit1(JSOp::True)) {
535 // [stack] NEEDS-AWAIT? HAS-AWAITED? EXC2 RESOURCES INDEX THROWING
536 return false;
537 }
538
539 if (!bce_->emitUnpickN(3)) {
540 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC2 RESOURCES INDEX
541 return false;
542 }
543
544 if (!ifException.emitEnd()) {
545 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC RESOURCES INDEX
546 return false;
547 }
548
549 if (!tryCall.emitEnd()) {
550 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC RESOURCES INDEX
551 return false;
552 }
553
554 // [stack] ... THROWING EXC RESOURCES INDEX
555
556 // Step 3.f. Else,
557 // Step 3.f.i. Assert: hint is async-dispose.
558 // (implicit)
559 if (!ifMethodNotUndefined.emitElse()) {
560 // [stack] ... THROWING EXC RESOURCES INDEX METHOD
561 return false;
562 }
563
564 if (!bce_->emit1(JSOp::Pop)) {
565 // [stack] ... THROWING EXC RESOURCES INDEX
566 return false;
567 }
568
569 if (hasAsyncDisposables_) {
570 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX
571
572 // Step 3.f.ii. Set needsAwait to true.
573 if (!bce_->emitPickN(5)) {
574 // [stack] HAS-AWAITED THROWING EXC RESOURCES INDEX NEEDS-AWAIT
575 return false;
576 }
577
578 if (!bce_->emit1(JSOp::Pop)) {
579 // [stack] HAS-AWAITED THROWING EXC RESOURCES INDEX
580 return false;
581 }
582
583 if (!bce_->emit1(JSOp::True)) {
584 // [stack] HAS-AWAITED THROWING EXC RESOURCES INDEX NEEDS-AWAIT
585 return false;
586 }
587
588 if (!bce_->emitUnpickN(5)) {
589 // [stack] NEEDS-AWAIT HAS-AWAITED THROWING EXC RESOURCES INDEX
590 return false;
591 }
592 }
593
594 if (!ifMethodNotUndefined.emitEnd()) {
595 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC RESOURCES INDEX
596 return false;
597 }
598
599 if (!bce_->emit1(JSOp::Dec)) {
600 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC RESOURCES INDEX
601 return false;
602 }
603
604 if (!wh.emitEnd()) {
605 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC RESOURCES INDEX
606 return false;
607 }
608
609 if (!bce_->emitPopN(2)) {
610 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC
611 return false;
612 }
613
614 if (hasAsyncDisposables_) {
615 // Step 4. If needsAwait is true and hasAwaited is false, then
616 if (!bce_->emitPickN(3)) {
617 // [stack] HAS-AWAITED THROWING EXC NEEDS-AWAIT
618 return false;
619 }
620
621 if (!bce_->emitPickN(3)) {
622 // [stack] THROWING EXC NEEDS-AWAIT HAS-AWAITED
623 return false;
624 }
625
626 if (!bce_->emit1(JSOp::Not)) {
627 // [stack] THROWING EXC NEEDS-AWAIT (!HAS-AWAITED)
628 return false;
629 }
630
631 if (!bce_->emit1(JSOp::BitAnd)) {
632 // [stack] THROWING EXC (NEEDS-AWAIT & !HAS-AWAITED)
633 return false;
634 }
635
636 InternalIfEmitter ifNeedsUndefinedAwait(bce_);
637
638 if (!ifNeedsUndefinedAwait.emitThen()) {
639 // [stack] THROWING EXC
640 return false;
641 }
642
643 if (!bce_->emit1(JSOp::Undefined)) {
644 // [stack] THROWING EXC UNDEF
645 return false;
646 }
647
648 if (!bce_->emitAwaitInScope(es)) {
649 // [stack] THROWING EXC RESOLVED
650 return false;
651 }
652
653 if (!bce_->emit1(JSOp::Pop)) {
654 // [stack] THROWING EXC
655 return false;
656 }
657
658 if (!ifNeedsUndefinedAwait.emitEnd()) {
659 // [stack] THROWING EXC
660 return false;
661 }
662 }
663
664 // Step 7. Return ? completion.
665 if (!bce_->emit1(JSOp::Swap)) {
666 // [stack] EXC THROWING
667 return false;
668 }
669
670 #ifdef DEBUG
671 state_ = State::End;
672 #endif
673 return true;
674 }
675
676 bool UsingEmitter::emitDisposeResourcesForEnvironment(EmitterScope& es) {
677 // [stack] THROWING EXC
678
679 DisposalEmitter de(bce_, hasAwaitUsing_);
680 if (!de.prepareForDisposeCapability()) {
681 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC
682 return false;
683 }
684
685 // Explicit Resource Management Proposal
686 // DisposeResources ( disposeCapability, completion )
687 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-disposeresources
688 //
689 // Step 6. Set disposeCapability.[[DisposableResourceStack]] to a new empty
690 // List.
691 if (!emitTakeDisposeCapability()) {
692 // [stack] NEEDS-AWAIT? HAS-AWAITED? THROWING EXC RESOURCES COUNT
693 return false;
694 }
695
696 if (!de.emitEnd(es)) {
697 // [stack] EXC THROWING
698 return false;
699 }
700
701 return true;
702 }
703
704 bool UsingEmitter::prepareForDisposableScopeBody(BlockKind blockKind) {
705 MOZ_ASSERT(state_ == State::Start);
706
707 // For-of loops are already wrapped in try-catch and have special case
708 // handling for the same.
709 // See ForOfLoopControl::emitEndCodeNeedingIteratorClose.
710 if (blockKind != BlockKind::ForOf) {
711 tryEmitter_.emplace(bce_, TryEmitter::Kind::TryFinally,
712 TryEmitter::ControlKind::Disposal);
713 if (!tryEmitter_->emitTry()) {
714 return false;
715 }
716 }
717
718 #ifdef DEBUG
719 state_ = State::DisposableScopeBody;
720 #endif
721 return true;
722 }
723
724 // Explicit Resource Management Proposal
725 // GetDisposeMethod ( V, hint )
726 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-getdisposemethod
727 // Steps 1.a-1.b.i., 2-3.
728 bool UsingEmitter::emitGetDisposeMethod(UsingHint hint) {
729 // [stack] VAL
730
731 // Step 1. If hint is async-dispose, then
732 if (hint == UsingHint::Async) {
733 // Step 1.a. Let method be ? GetMethod(V, @@asyncDispose).
734 if (!bce_->emit1(JSOp::Dup)) {
735 // [stack] VAL VAL
736 return false;
737 }
738
739 if (!bce_->emit1(JSOp::Dup)) {
740 // [stack] VAL VAL VAL
741 return false;
742 }
743
744 if (!bce_->emit2(JSOp::Symbol, uint8_t(JS::SymbolCode::asyncDispose))) {
745 // [stack] VAL VAL VAL SYMBOL
746 return false;
747 }
748
749 if (!bce_->emit1(JSOp::GetElem)) {
750 // [stack] VAL VAL ASYNC-DISPOSE
751 return false;
752 }
753
754 // Step 1.b. If method is undefined, then
755 // GetMethod returns undefined if the function is null but
756 // since we do not do the conversion here we check for
757 // null or undefined here.
758 if (!bce_->emit1(JSOp::IsNullOrUndefined)) {
759 // [stack] VAL VAL ASYNC-DISPOSE IS-NULL-OR-UNDEF
760 return false;
761 }
762
763 InternalIfEmitter ifAsyncDisposeNullOrUndefined(bce_);
764
765 if (!ifAsyncDisposeNullOrUndefined.emitThenElse()) {
766 // [stack] VAL VAL ASYNC-DISPOSE
767 return false;
768 }
769
770 if (!bce_->emit1(JSOp::Pop)) {
771 // [stack] VAL VAL
772 return false;
773 }
774
775 if (!bce_->emit1(JSOp::Dup)) {
776 // [stack] VAL VAL VAL
777 return false;
778 }
779
780 if (!bce_->emit2(JSOp::Symbol, uint8_t(JS::SymbolCode::dispose))) {
781 // [stack] VAL VAL VAL SYMBOL
782 return false;
783 }
784
785 // Step 1.b.i. Set method to ? GetMethod(V, @@dispose).
786 if (!bce_->emit1(JSOp::GetElem)) {
787 // [stack] VAL VAL DISPOSE
788 return false;
789 }
790
791 if (!bce_->emit1(JSOp::True)) {
792 // [stack] VAL VAL DISPOSE NEEDS-CLOSURE
793 return false;
794 }
795
796 if (!ifAsyncDisposeNullOrUndefined.emitElse()) {
797 // [stack] VAL VAL ASYNC-DISPOSE
798 return false;
799 }
800
801 if (!bce_->emit1(JSOp::False)) {
802 // [stack] VAL VAL ASYNC-DISPOSE NEEDS-CLOSURE
803 return false;
804 }
805
806 if (!ifAsyncDisposeNullOrUndefined.emitEnd()) {
807 // [stack] VAL VAL METHOD NEEDS-CLOSURE
808 return false;
809 }
810
811 } else {
812 MOZ_ASSERT(hint == UsingHint::Sync);
813
814 // Step 2. Else,
815 // Step 2.a. Let method be ? GetMethod(V, @@dispose).
816 if (!bce_->emit1(JSOp::Dup)) {
817 // [stack] VAL VAL
818 return false;
819 }
820
821 if (!bce_->emit1(JSOp::Dup)) {
822 // [stack] VAL VAL VAL
823 return false;
824 }
825
826 if (!bce_->emit2(JSOp::Symbol, uint8_t(JS::SymbolCode::dispose))) {
827 // [stack] VAL VAL VAL SYMBOL
828 return false;
829 }
830
831 if (!bce_->emit1(JSOp::GetElem)) {
832 // [stack] VAL VAL DISPOSE
833 return false;
834 }
835
836 if (!bce_->emit1(JSOp::False)) {
837 // [stack] VAL VAL DISPOSE NEEDS-CLOSURE
838 return false;
839 }
840 }
841
842 if (!bce_->emitDupAt(1)) {
843 // [stack] VAL VAL METHOD NEEDS-CLOSURE METHOD
844 return false;
845 }
846
847 // According to spec GetMethod throws TypeError if the method is not callable
848 // and returns undefined if the value is either undefined or null.
849 // but the caller of this function, emitCreateDisposableResource is
850 // supposed to throw TypeError as well if returned value is undefined,
851 // thus we combine the steps here.
852 if (!bce_->emitCheckIsCallable()) {
853 // [stack] VAL VAL METHOD NEEDS-CLOSURE METHOD IS-CALLABLE
854 return false;
855 }
856
857 InternalIfEmitter ifMethodNotCallable(bce_);
858
859 if (!ifMethodNotCallable.emitThen(IfEmitter::ConditionKind::Negative)) {
860 // [stack] VAL VAL METHOD NEEDS-CLOSURE METHOD
861 return false;
862 }
863
864 if (!bce_->emit2(JSOp::ThrowMsg, uint8_t(ThrowMsgKind::DisposeNotCallable))) {
865 // [stack] VAL VAL METHOD NEEDS-CLOSURE METHOD
866 return false;
867 }
868
869 if (!ifMethodNotCallable.emitEnd()) {
870 // [stack] VAL VAL METHOD NEEDS-CLOSURE METHOD
871 return false;
872 }
873
874 if (!bce_->emit1(JSOp::Pop)) {
875 // [stack] VAL VAL METHOD NEEDS-CLOSURE
876 return false;
877 }
878
879 return true;
880 }
881
882 // Explicit Resource Management Proposal
883 // CreateDisposableResource ( V, hint [ , method ] )
884 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-createdisposableresource
885 bool UsingEmitter::emitCreateDisposableResource(UsingHint hint) {
886 // [stack] VAL
887
888 // Step 1. If method is not present, then (implicit)
889 // Step 1.a. If V is either null or undefined, then
890 if (!bce_->emit1(JSOp::IsNullOrUndefined)) {
891 // [stack] VAL IS-NULL-OR-UNDEF
892 return false;
893 }
894
895 InternalIfEmitter ifNullUndefined(bce_);
896
897 if (!ifNullUndefined.emitThenElse()) {
898 // [stack] VAL
899 return false;
900 }
901
902 // Step 1.a.i. Set V to undefined.
903 if (!bce_->emit1(JSOp::Undefined)) {
904 // [stack] VAL UNDEF
905 return false;
906 }
907
908 // Step 1.a.ii. Set method to undefined.
909 if (!bce_->emit1(JSOp::Undefined)) {
910 // [stack] VAL UNDEF UNDEF
911 return false;
912 }
913
914 if (!bce_->emit1(JSOp::False)) {
915 // [stack] VAL UNDEF UNDEF NEEDS-CLOSURE
916 return false;
917 }
918
919 // Step 1.b. Else,
920 if (!ifNullUndefined.emitElse()) {
921 // [stack] VAL
922 return false;
923 }
924
925 // Step 1.b.i. If V is not an Object, throw a TypeError exception.
926 if (!bce_->emitCheckIsObj(CheckIsObjectKind::Disposable)) {
927 // [stack] VAL
928 return false;
929 }
930
931 // Step 1.b.ii. Set method to ? GetDisposeMethod(V, hint).
932 // Step 1.b.iii. If method is undefined, throw a TypeError exception.
933 if (!emitGetDisposeMethod(hint)) {
934 // [stack] VAL VAL METHOD NEEDS-CLOSURE
935 return false;
936 }
937
938 if (!ifNullUndefined.emitEnd()) {
939 // [stack] VAL VAL METHOD NEEDS-CLOSURE
940 return false;
941 }
942
943 return true;
944 }
945
946 // Explicit Resource Management Proposal
947 // 7.5.4 AddDisposableResource ( disposeCapability, V, hint [ , method ] )
948 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-adddisposableresource
949 // Steps 1, 3-4.
950 bool UsingEmitter::prepareForAssignment(UsingHint hint) {
951 MOZ_ASSERT(state_ == State::DisposableScopeBody);
952 MOZ_ASSERT(bce_->innermostEmitterScope()->hasDisposables());
953
954 if (hint == UsingHint::Async) {
955 setHasAwaitUsing(true);
956 }
957
958 // [stack] VAL
959
960 // Step 1. If method is not present, then (implicit)
961 // Step 1.a. If V is either null or undefined and hint is sync-dispose, return
962 // unused.
963 if (hint == UsingHint::Sync) {
964 if (!bce_->emit1(JSOp::IsNullOrUndefined)) {
965 // [stack] VAL IS-NULL-OR-UNDEF
966 return false;
967 }
968
969 if (!bce_->emit1(JSOp::Not)) {
970 // [stack] VAL !IS-NULL-OR-UNDEF
971 return false;
972 }
973 } else {
974 MOZ_ASSERT(hint == UsingHint::Async);
975 if (!bce_->emit1(JSOp::True)) {
976 // [stack] VAL TRUE
977 return false;
978 }
979 }
980
981 // [stack] VAL SHOULD-CREATE-RESOURCE
982
983 InternalIfEmitter ifCreateResource(bce_);
984
985 if (!ifCreateResource.emitThen()) {
986 // [stack] VAL
987 return false;
988 }
989
990 // Step 1.c. Let resource be ? CreateDisposableResource(V, hint).
991 if (!emitCreateDisposableResource(hint)) {
992 // [stack] VAL VAL METHOD NEEDS-CLOSURE
993 return false;
994 }
995
996 // Step 3. Append resource to disposeCapability.[[DisposableResourceStack]].
997 if (!bce_->emit2(JSOp::AddDisposable, uint8_t(hint))) {
998 // [stack] VAL
999 return false;
1000 }
1001
1002 if (!ifCreateResource.emitEnd()) {
1003 // [stack] VAL
1004 return false;
1005 }
1006
1007 // Step 4. Return unused.
1008 return true;
1009 }
1010
1011 bool ForOfDisposalEmitter::prepareForForOfLoopIteration() {
1012 MOZ_ASSERT(state_ == State::Start);
1013 EmitterScope* es = bce_->innermostEmitterScopeNoCheck();
1014 MOZ_ASSERT(es->hasDisposables());
1015
1016 if (!bce_->emit1(JSOp::False)) {
1017 // [stack] THROWING
1018 return false;
1019 }
1020
1021 if (!bce_->emit1(JSOp::Undefined)) {
1022 // [stack] THROWING UNDEF
1023 return false;
1024 }
1025
1026 if (!emitDisposeResourcesForEnvironment(*es)) {
1027 // [stack] EXC THROWING
1028 return false;
1029 }
1030
1031 if (!emitThrowIfException()) {
1032 // [stack]
1033 return false;
1034 }
1035
1036 #ifdef DEBUG
1037 state_ = State::Iteration;
1038 #endif
1039 return true;
1040 }
1041
1042 bool ForOfDisposalEmitter::emitEnd() {
1043 MOZ_ASSERT(state_ == State::Iteration);
1044 EmitterScope* es = bce_->innermostEmitterScopeNoCheck();
1045 MOZ_ASSERT(es->hasDisposables());
1046
1047 // [stack] EXC STACK
1048
1049 if (!bce_->emit1(JSOp::Swap)) {
1050 // [stack] STACK EXC
1051 return false;
1052 }
1053
1054 if (!bce_->emit1(JSOp::True)) {
1055 // [stack] STACK EXC THROWING
1056 return false;
1057 }
1058
1059 if (!bce_->emit1(JSOp::Swap)) {
1060 // [stack] STACK THROWING EXC
1061 return false;
1062 }
1063
1064 if (!emitDisposeResourcesForEnvironment(*es)) {
1065 // [stack] STACK EXC THROWING
1066 return false;
1067 }
1068
1069 if (!bce_->emit1(JSOp::Pop)) {
1070 // [stack] STACK EXC
1071 return false;
1072 }
1073
1074 if (!bce_->emit1(JSOp::Swap)) {
1075 // [stack] EXC STACK
1076 return false;
1077 }
1078
1079 #ifdef DEBUG
1080 state_ = State::End;
1081 #endif
1082 return true;
1083 }
1084
1085 bool UsingEmitter::emitEnd() {
1086 MOZ_ASSERT(state_ == State::DisposableScopeBody);
1087 EmitterScope* es = bce_->innermostEmitterScopeNoCheck();
1088 MOZ_ASSERT(es->hasDisposables());
1089 MOZ_ASSERT(tryEmitter_.isSome());
1090
1091 if (!tryEmitter_->emitFinally()) {
1092 // [stack] EXC-OR-RESUME STACK THROWING RVAL?
1093 return false;
1094 }
1095
1096 if (!bce_->emitDupAt(tryEmitter_->shouldUpdateRval() ? 1 : 0)) {
1097 // [stack] EXC-OR-RESUME STACK THROWING RVAL? THROWING
1098 return false;
1099 }
1100
1101 InternalIfEmitter ifThrowing(bce_);
1102
1103 if (!ifThrowing.emitThenElse()) {
1104 // [stack] EXC STACK THROWING RVAL?
1105 return false;
1106 }
1107
1108 if (!bce_->emit1(JSOp::True)) {
1109 // [stack] EXC STACK THROWING RVAL? THROWING
1110 return false;
1111 }
1112
1113 if (!bce_->emitDupAt(tryEmitter_->shouldUpdateRval() ? 4 : 3)) {
1114 // [stack] EXC STACK THROWING RVAL? THROWING EXC
1115 return false;
1116 }
1117
1118 if (!ifThrowing.emitElse()) {
1119 // [stack] RESUME STACK THROWING RVAL?
1120 return false;
1121 }
1122
1123 if (!bce_->emit1(JSOp::False)) {
1124 // [stack] RESUME STACK THROWING RVAL? THROWING
1125 return false;
1126 }
1127
1128 if (!bce_->emit1(JSOp::Undefined)) {
1129 // [stack] RESUME STACK THROWING RVAL? THROWING UNDEF
1130 return false;
1131 }
1132
1133 if (!ifThrowing.emitEnd()) {
1134 // [stack] EXC-OR-RESUME STACK THROWING RVAL? THROWING EXC-OR-UNDEF
1135 return false;
1136 }
1137
1138 if (!emitDisposeResourcesForEnvironment(*es)) {
1139 // [stack] EXC-OR-RESUME STACK THROWING RVAL? DISPOSAL-EXC DISPOSAL-THROWING
1140 return false;
1141 }
1142
1143 if (bce_->sc->isSuspendableContext() &&
1144 bce_->sc->asSuspendableContext()->isGenerator()) {
1145 // [stack] ... DISP-EXC DISP-THROWING
1146
1147 if (!bce_->emit1(JSOp::Swap)) {
1148 // [stack] ... DISP-THROWING DISP-EXC
1149 return false;
1150 }
1151
1152 if (!bce_->emit1(JSOp::IsGenClosing)) {
1153 // [stack] ... DISP-THROWING DISP-EXC GEN-CLOSING
1154 return false;
1155 }
1156
1157 if (!bce_->emit1(JSOp::Not)) {
1158 // [stack] ... DISP-THROWING DISP-EXC !GEN-CLOSING
1159 return false;
1160 }
1161
1162 if (!bce_->emitPickN(2)) {
1163 // [stack] ... DISP-EXC !GEN-CLOSING DISP-THROWING
1164 return false;
1165 }
1166
1167 if (!bce_->emit1(JSOp::BitAnd)) {
1168 // [stack] ... DISP-EXC (DISP-THROWING & !GEN-CLOSING)
1169 return false;
1170 }
1171 }
1172
1173 if (!emitThrowIfException()) {
1174 // [stack] EXC-OR-RESUME STACK THROWING RVAL?
1175 return false;
1176 }
1177
1178 if (!tryEmitter_->emitEnd()) {
1179 // [stack]
1180 return false;
1181 }
1182
1183 #ifdef DEBUG
1184 state_ = State::End;
1185 #endif
1186 return true;
1187 }
1188
1189 // Explicit Resource Management Proposal
1190 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset
1191 // Step 9.k.i.
1192 bool NonLocalIteratorCloseUsingEmitter::prepareForIteratorClose(
1193 EmitterScope& es) {
1194 MOZ_ASSERT(state_ == State::Start);
1195 // In this function we prepare for the closure of the iterator but first
1196 // emitting the dispose loop and preseving exceptions on the stack and after
1197 // that emitting a try to wrap the iterator closure code that shall come after
1198 // this.
1199 if (!es.hasDisposables()) {
1200 #ifdef DEBUG
1201 state_ = State::IteratorClose;
1202 #endif
1203 return true;
1204 }
1205
1206 setHasAwaitUsing(es.hasAsyncDisposables());
1207
1208 // [stack] ITER
1209
1210 if (!bce_->emit1(JSOp::False)) {
1211 // [stack] THROWING
1212 return false;
1213 }
1214
1215 if (!bce_->emit1(JSOp::Undefined)) {
1216 // [stack] THROWING UNDEF
1217 return false;
1218 }
1219
1220 if (!emitDisposeResourcesForEnvironment(es)) {
1221 // [stack] ITER EXC-DISPOSE DISPOSE-THROWING
1222 return false;
1223 }
1224
1225 if (!bce_->emitPickN(2)) {
1226 // [stack] EXC-DISPOSE DISPOSE-THROWING ITER
1227 return false;
1228 }
1229
1230 tryClosingIterator_.emplace(bce_, TryEmitter::Kind::TryCatch,
1231 TryEmitter::ControlKind::NonSyntactic);
1232
1233 if (!tryClosingIterator_->emitTry()) {
1234 // [stack] EXC-DISPOSE DISPOSE-THROWING ITER
1235 return false;
1236 }
1237
1238 #ifdef DEBUG
1239 state_ = State::IteratorClose;
1240 #endif
1241 return true;
1242 }
1243
1244 // Explicit Resource Management Proposal
1245 // 7.4.9 IteratorClose ( iteratorRecord, completion )
1246 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-iteratorclose&secAll=true
1247 // Steps 5-6.
1248 //
1249 // 7.4.11 AsyncIteratorClose ( iteratorRecord, completion )
1250 // https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-asynciteratorclose&secAll=true
1251 // Steps 5-6.
1252 bool NonLocalIteratorCloseUsingEmitter::emitEnd() {
1253 // This function handles the steps after the iterator close operation which
1254 // may or may not have thrown. note that prepareForIteratorClose would have
1255 // already wrapped the iterator closure with a try and have preserved any
1256 // exception by the disposal operation on the stack now this function does the
1257 // equivalent of the following pseudocode (consider excDispose and
1258 // disposeThrowing and iter equal to corresponding values left on stack by
1259 // prepareForIteratorClose):
1260 //
1261 //
1262 // let excToThrow, throwing = disposeThrowing;
1263 // try {
1264 // IteratorClose(iter);
1265 // } catch (excIterClose) {
1266 // throwing = true;
1267 // if (disposeThrowing) {
1268 // excToThrow = excDispose;
1269 // } else {
1270 // excToThrow = excIterClose;
1271 // }
1272 // }
1273 // if (throwing) {
1274 // throw excToThrow;
1275 // }
1276 //
1277 MOZ_ASSERT(state_ == State::IteratorClose);
1278
1279 if (!tryClosingIterator_.isSome()) {
1280 #ifdef DEBUG
1281 state_ = State::End;
1282 #endif
1283 return true;
1284 }
1285
1286 // [stack] EXC-DISPOSE DISPOSE-THROWING ITER
1287
1288 if (!tryClosingIterator_->emitCatch()) {
1289 // [stack] EXC-DISPOSE DISPOSE-THROWING ITER EXC-ITER-CLOSE
1290 return false;
1291 }
1292
1293 if (!bce_->emitPickN(2)) {
1294 // [stack] EXC-DISPOSE ITER EXC-ITER-CLOSE DISPOSE-THROWING
1295 return false;
1296 }
1297
1298 InternalIfEmitter ifDisposeWasThrowing(bce_);
1299
1300 if (!ifDisposeWasThrowing.emitThenElse()) {
1301 // [stack] EXC-DISPOSE ITER EXC-ITER-CLOSE
1302 return false;
1303 }
1304
1305 if (!bce_->emit1(JSOp::Pop)) {
1306 // [stack] EXC-DISPOSE ITER
1307 return false;
1308 }
1309
1310 if (!bce_->emit1(JSOp::True)) {
1311 // [stack] EXC-DISPOSE ITER THROWING
1312 return false;
1313 }
1314
1315 // This swap operation is to make the stack state consistent with the
1316 // the non-throwing case.
1317 if (!bce_->emit1(JSOp::Swap)) {
1318 // [stack] EXC-DISPOSE THROWING ITER
1319 return false;
1320 }
1321
1322 if (!ifDisposeWasThrowing.emitElse()) {
1323 // [stack] EXC-DISPOSE ITER EXC-ITER-CLOSE
1324 return false;
1325 }
1326
1327 if (!bce_->emitPickN(2)) {
1328 // [stack] ITER EXC-ITER-CLOSE EXC-DISPOSE
1329 return false;
1330 }
1331
1332 if (!bce_->emit1(JSOp::Pop)) {
1333 // [stack] ITER EXC-ITER-CLOSE
1334 return false;
1335 }
1336
1337 if (!bce_->emit1(JSOp::Swap)) {
1338 // [stack] EXC-ITER-CLOSE ITER
1339 return false;
1340 }
1341
1342 if (!bce_->emit1(JSOp::True)) {
1343 // [stack] EXC-ITER-CLOSE ITER THROWING
1344 return false;
1345 }
1346
1347 if (!bce_->emit1(JSOp::Swap)) {
1348 // [stack] EXC-ITER-CLOSE THROWING ITER
1349 return false;
1350 }
1351
1352 if (!ifDisposeWasThrowing.emitEnd()) {
1353 // [stack] EXC THROWING ITER
1354 return false;
1355 }
1356
1357 if (!tryClosingIterator_->emitEnd()) {
1358 // [stack] EXC THROWING ITER
1359 return false;
1360 }
1361
1362 if (!bce_->emit1(JSOp::Swap)) {
1363 // [stack] EXC ITER THROWING
1364 return false;
1365 }
1366
1367 InternalIfEmitter ifThrowing(bce_);
1368
1369 if (!ifThrowing.emitThenElse()) {
1370 // [stack] EXC ITER
1371 return false;
1372 }
1373
1374 if (!bce_->emit1(JSOp::Swap)) {
1375 // [stack] ITER EXC
1376 return false;
1377 }
1378
1379 if (!bce_->emit1(JSOp::Throw)) {
1380 // [stack] ITER
1381 return false;
1382 }
1383
1384 if (!ifThrowing.emitElse()) {
1385 // [stack] EXC ITER
1386 return false;
1387 }
1388
1389 if (!bce_->emit1(JSOp::Swap)) {
1390 // [stack] ITER EXC
1391 return false;
1392 }
1393
1394 if (!bce_->emit1(JSOp::Pop)) {
1395 // [stack] ITER
1396 return false;
1397 }
1398
1399 if (!ifThrowing.emitEnd()) {
1400 // [stack] ITER
1401 return false;
1402 }
1403
1404 #ifdef DEBUG
1405 state_ = State::End;
1406 #endif
1407 return true;
1408 }