From 3e8c502550bc87762753ecb03ebaea460fb5e852 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 2 Jun 2020 10:52:46 +0200 Subject: [PATCH] op: return value StackOp from Push and make Pop use a value receiver To match Record, we'd like Push to return a value. To do that and support the one-line defer op.Push(ops).Pop() Pop needs to use a value receiver as well. Drop the active field and make it so. The field was only a sanity check, a check which is already done by Ops.stackStack, albeit with a less specific panic. Signed-off-by: Elias Naur --- op/op.go | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/op/op.go b/op/op.go index 6a230e3b..acc93db2 100644 --- a/op/op.go +++ b/op/op.go @@ -100,7 +100,6 @@ type Ops struct { type StackOp struct { id stackID macroID int - active bool ops *Ops } @@ -158,34 +157,23 @@ func (c CallOp) Add(o *Ops) { } // Push (save) the current operations state. -func Push(o *Ops) *StackOp { - var s StackOp - s.push(o) - return &s -} - -func (s *StackOp) push(o *Ops) { - if s.active { - panic("unbalanced push") +func Push(o *Ops) StackOp { + s := StackOp{ + ops: o, + id: o.stackStack.push(), + macroID: o.macroStack.currentID, } - s.active = true - s.ops = o - s.id = o.stackStack.push() - s.macroID = o.macroStack.currentID data := o.Write(opconst.TypePushLen) data[0] = byte(opconst.TypePush) + return s } // Pop (restore) a previously Pushed operations state. -func (s *StackOp) Pop() { - if !s.active { - panic("unbalanced pop") - } +func (s StackOp) Pop() { if s.ops.macroStack.currentID != s.macroID { panic("pop in a different macro than push") } s.ops.stackStack.pop(s.id) - s.active = false data := s.ops.Write(opconst.TypePopLen) data[0] = byte(opconst.TypePop) }