@@ -329,9 +329,54 @@ impl From<ControlFrameKind> for ControlFrame {
329
329
}
330
330
}
331
331
332
- impl ControlFrame {
333
- /// Makes the [`ControlFrame`] aware that there is a branch to it.
334
- pub fn branch_to ( & mut self ) {
332
+ /// Trait implemented by control frame types that share a common API.
333
+ pub trait ControlFrameBase {
334
+ /// Returns the branch label of `self`.
335
+ fn label ( & self ) -> LabelRef ;
336
+
337
+ /// Returns `true` if there exists a branch to `self.`
338
+ fn is_branched_to ( & self ) -> bool ;
339
+
340
+ /// Makes `self` aware that there is a branch to it.
341
+ fn branch_to ( & mut self ) ;
342
+
343
+ /// Returns the number of operands required for branching to `self`.
344
+ fn len_branch_params ( & self , engine : & Engine ) -> u16 ;
345
+
346
+ /// Returns a reference to the [`Instruction::ConsumeFuel`] of `self`.
347
+ ///
348
+ /// Returns `None` if fuel metering is disabled.
349
+ fn consume_fuel_instr ( & self ) -> Option < Instr > ;
350
+ }
351
+
352
+ impl ControlFrameBase for ControlFrame {
353
+ fn label ( & self ) -> LabelRef {
354
+ match self {
355
+ ControlFrame :: Block ( frame) => frame. label ( ) ,
356
+ ControlFrame :: Loop ( frame) => frame. label ( ) ,
357
+ ControlFrame :: If ( frame) => frame. label ( ) ,
358
+ ControlFrame :: Else ( frame) => frame. label ( ) ,
359
+ ControlFrame :: Unreachable ( _) => {
360
+ panic ! ( "invalid query for unreachable control frame: `ControlFrame::label`" )
361
+ }
362
+ }
363
+ }
364
+
365
+ fn is_branched_to ( & self ) -> bool {
366
+ match self {
367
+ ControlFrame :: Block ( frame) => frame. is_branched_to ( ) ,
368
+ ControlFrame :: Loop ( frame) => frame. is_branched_to ( ) ,
369
+ ControlFrame :: If ( frame) => frame. is_branched_to ( ) ,
370
+ ControlFrame :: Else ( frame) => frame. is_branched_to ( ) ,
371
+ ControlFrame :: Unreachable ( _) => {
372
+ panic ! (
373
+ "invalid query for unreachable control frame: `ControlFrame::is_branched_to`"
374
+ )
375
+ }
376
+ }
377
+ }
378
+
379
+ fn branch_to ( & mut self ) {
335
380
match self {
336
381
ControlFrame :: Block ( frame) => frame. branch_to ( ) ,
337
382
ControlFrame :: Loop ( frame) => frame. branch_to ( ) ,
@@ -343,8 +388,7 @@ impl ControlFrame {
343
388
}
344
389
}
345
390
346
- /// Returns the number of operands required for branching to the [`ControlFrame`].
347
- pub fn len_branch_params ( & self , engine : & Engine ) -> u16 {
391
+ fn len_branch_params ( & self , engine : & Engine ) -> u16 {
348
392
match self {
349
393
ControlFrame :: Block ( frame) => frame. len_branch_params ( engine) ,
350
394
ControlFrame :: Loop ( frame) => frame. len_branch_params ( engine) ,
@@ -356,10 +400,7 @@ impl ControlFrame {
356
400
}
357
401
}
358
402
359
- /// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`ControlFrame`] if any.
360
- ///
361
- /// Returns `None` if fuel metering is disabled.
362
- pub fn consume_fuel_instr ( & self ) -> Option < Instr > {
403
+ fn consume_fuel_instr ( & self ) -> Option < Instr > {
363
404
match self {
364
405
ControlFrame :: Block ( frame) => frame. consume_fuel_instr ( ) ,
365
406
ControlFrame :: Loop ( frame) => frame. consume_fuel_instr ( ) ,
@@ -397,36 +438,31 @@ impl BlockControlFrame {
397
438
self . ty
398
439
}
399
440
400
- /// Returns the number of operands required for branching to the [`BlockControlFrame`].
401
- pub fn len_branch_params ( & self , engine : & Engine ) -> u16 {
402
- self . ty . len_results ( engine)
403
- }
404
-
405
441
/// Returns the height of the [`BlockControlFrame`].
406
442
pub fn height ( & self ) -> usize {
407
443
self . height . into ( )
408
444
}
445
+ }
446
+
447
+ impl ControlFrameBase for BlockControlFrame {
448
+ fn label ( & self ) -> LabelRef {
449
+ self . label
450
+ }
409
451
410
- /// Returns `true` if there are branches to this [`BlockControlFrame`].
411
- pub fn is_branched_to ( & self ) -> bool {
452
+ fn is_branched_to ( & self ) -> bool {
412
453
self . is_branched_to
413
454
}
414
455
415
- /// Makes the [`BlockControlFrame`] aware that there is a branch to it.
416
- pub fn branch_to ( & mut self ) {
456
+ fn branch_to ( & mut self ) {
417
457
self . is_branched_to = true ;
418
458
}
419
459
420
- /// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`BlockControlFrame`] if any.
421
- ///
422
- /// Returns `None` if fuel metering is disabled.
423
- pub fn consume_fuel_instr ( & self ) -> Option < Instr > {
424
- self . consume_fuel
460
+ fn len_branch_params ( & self , engine : & Engine ) -> u16 {
461
+ self . ty . len_results ( engine)
425
462
}
426
463
427
- /// Returns the branch label of the [`BlockControlFrame`].
428
- pub fn label ( & self ) -> LabelRef {
429
- self . label
464
+ fn consume_fuel_instr ( & self ) -> Option < Instr > {
465
+ self . consume_fuel
430
466
}
431
467
}
432
468
@@ -455,36 +491,31 @@ impl LoopControlFrame {
455
491
self . ty
456
492
}
457
493
458
- /// Returns the number of operands required for branching to the [`LoopControlFrame`].
459
- pub fn len_branch_params ( & self , engine : & Engine ) -> u16 {
460
- self . ty . len_params ( engine)
461
- }
462
-
463
494
/// Returns the height of the [`LoopControlFrame`].
464
495
pub fn height ( & self ) -> usize {
465
496
self . height . into ( )
466
497
}
498
+ }
467
499
468
- /// Returns `true` if there are branches to this [`LoopControlFrame`].
469
- pub fn is_branched_to ( & self ) -> bool {
500
+ impl ControlFrameBase for LoopControlFrame {
501
+ fn label ( & self ) -> LabelRef {
502
+ self . label
503
+ }
504
+
505
+ fn is_branched_to ( & self ) -> bool {
470
506
self . is_branched_to
471
507
}
472
508
473
- /// Makes the [`LoopControlFrame`] aware that there is a branch to it.
474
- pub fn branch_to ( & mut self ) {
509
+ fn branch_to ( & mut self ) {
475
510
self . is_branched_to = true ;
476
511
}
477
512
478
- /// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`LoopControlFrame`] if any.
479
- ///
480
- /// Returns `None` if fuel metering is disabled.
481
- pub fn consume_fuel_instr ( & self ) -> Option < Instr > {
482
- self . consume_fuel
513
+ fn len_branch_params ( & self , engine : & Engine ) -> u16 {
514
+ self . ty . len_params ( engine)
483
515
}
484
516
485
- /// Returns the branch label of the [`LoopControlFrame`].
486
- pub fn label ( & self ) -> LabelRef {
487
- self . label
517
+ fn consume_fuel_instr ( & self ) -> Option < Instr > {
518
+ self . consume_fuel
488
519
}
489
520
}
490
521
@@ -515,38 +546,11 @@ impl IfControlFrame {
515
546
self . ty
516
547
}
517
548
518
- /// Returns the number of operands required for branching to the [`IfControlFrame`].
519
- pub fn len_branch_params ( & self , engine : & Engine ) -> u16 {
520
- self . ty . len_results ( engine)
521
- }
522
-
523
549
/// Returns the height of the [`IfControlFrame`].
524
550
pub fn height ( & self ) -> usize {
525
551
self . height . into ( )
526
552
}
527
553
528
- /// Returns `true` if there are branches to this [`IfControlFrame`].
529
- pub fn is_branched_to ( & self ) -> bool {
530
- self . is_branched_to
531
- }
532
-
533
- /// Makes the [`IfControlFrame`] aware that there is a branch to it.
534
- pub fn branch_to ( & mut self ) {
535
- self . is_branched_to = true ;
536
- }
537
-
538
- /// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`IfControlFrame`] if any.
539
- ///
540
- /// Returns `None` if fuel metering is disabled.
541
- pub fn consume_fuel_instr ( & self ) -> Option < Instr > {
542
- self . consume_fuel
543
- }
544
-
545
- /// Returns the branch label of the [`IfControlFrame`].
546
- pub fn label ( & self ) -> LabelRef {
547
- self . label
548
- }
549
-
550
554
/// Returns the [`IfReachability`] of the [`IfControlFrame`].
551
555
pub fn reachability ( & self ) -> IfReachability {
552
556
self . reachability
@@ -585,6 +589,28 @@ impl IfControlFrame {
585
589
}
586
590
}
587
591
592
+ impl ControlFrameBase for IfControlFrame {
593
+ fn label ( & self ) -> LabelRef {
594
+ self . label
595
+ }
596
+
597
+ fn is_branched_to ( & self ) -> bool {
598
+ self . is_branched_to
599
+ }
600
+
601
+ fn branch_to ( & mut self ) {
602
+ self . is_branched_to = true ;
603
+ }
604
+
605
+ fn len_branch_params ( & self , engine : & Engine ) -> u16 {
606
+ self . ty . len_results ( engine)
607
+ }
608
+
609
+ fn consume_fuel_instr ( & self ) -> Option < Instr > {
610
+ self . consume_fuel
611
+ }
612
+ }
613
+
588
614
/// The reachability of the `if` control flow frame.
589
615
#[ derive( Debug , Copy , Clone ) ]
590
616
pub enum IfReachability {
@@ -678,36 +704,14 @@ impl ElseControlFrame {
678
704
self . ty
679
705
}
680
706
681
- /// Returns the number of operands required for branching to the [`ElseControlFrame`].
682
- pub fn len_branch_params ( & self , engine : & Engine ) -> u16 {
683
- self . ty . len_results ( engine)
684
- }
685
-
686
707
/// Returns the height of the [`ElseControlFrame`].
687
708
pub fn height ( & self ) -> usize {
688
709
self . height . into ( )
689
710
}
690
711
691
- /// Returns `true` if there are branches to this [`ElseControlFrame`].
692
- pub fn is_branched_to ( & self ) -> bool {
693
- self . is_branched_to
694
- }
695
-
696
- /// Makes the [`ElseControlFrame`] aware that there is a branch to it.
697
- pub fn branch_to ( & mut self ) {
698
- self . is_branched_to = true ;
699
- }
700
-
701
- /// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`ElseControlFrame`] if any.
702
- ///
703
- /// Returns `None` if fuel metering is disabled.
704
- pub fn consume_fuel_instr ( & self ) -> Option < Instr > {
705
- self . consume_fuel
706
- }
707
-
708
- /// Returns the branch label of the [`ElseControlFrame`].
709
- pub fn label ( & self ) -> LabelRef {
710
- self . label
712
+ /// Returns the [`ElseReachability`] of the [`ElseReachability`].
713
+ pub fn reachability ( & self ) -> ElseReachability {
714
+ self . reachability
711
715
}
712
716
713
717
/// Returns `true` if the `then` branch is reachable.
@@ -740,6 +744,28 @@ impl ElseControlFrame {
740
744
}
741
745
}
742
746
747
+ impl ControlFrameBase for ElseControlFrame {
748
+ fn label ( & self ) -> LabelRef {
749
+ self . label
750
+ }
751
+
752
+ fn is_branched_to ( & self ) -> bool {
753
+ self . is_branched_to
754
+ }
755
+
756
+ fn branch_to ( & mut self ) {
757
+ self . is_branched_to = true ;
758
+ }
759
+
760
+ fn len_branch_params ( & self , engine : & Engine ) -> u16 {
761
+ self . ty . len_results ( engine)
762
+ }
763
+
764
+ fn consume_fuel_instr ( & self ) -> Option < Instr > {
765
+ self . consume_fuel
766
+ }
767
+ }
768
+
743
769
/// The kind of a Wasm control frame.
744
770
#[ derive( Debug , Copy , Clone ) ]
745
771
pub enum ControlFrameKind {
0 commit comments