From f86cce7977f0431f5d5923a9c0dd7e266052a066 Mon Sep 17 00:00:00 2001 From: prod2 <95874442+prod2@users.noreply.github.com> Date: Wed, 9 Feb 2022 07:06:40 +0100 Subject: [PATCH] use stack instead of frames to find current closure --- src/ndspkg/vm.nim | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ndspkg/vm.nim b/src/ndspkg/vm.nim index f5f83ed..3631d9a 100644 --- a/src/ndspkg/vm.nim +++ b/src/ndspkg/vm.nim @@ -32,7 +32,7 @@ type Frame = object stackBottom: int # the absolute index of where 0 inside the frame is returnIp: ptr uint8 - closure: Closure[NdValue] +# closure: Closure[NdValue] InterpretResult* = enum irOK, irRuntimeError @@ -72,6 +72,7 @@ proc run*(chunk: Chunk): InterpretResult = chunk.constants[ip.readDU8()] template frameBottom: int = frames.peek().stackBottom + template cclosure: Closure[NdValue] = stack.getIndex(frameBottom()).asClosure() template call(funct: NdValue, argcount: int, error: untyped) = if funct.isFunct(): @@ -79,7 +80,8 @@ proc run*(chunk: Chunk): InterpretResult = frames.add(Frame(stackBottom: stack.high - argcount, returnIp: ip)) ip = funct.asFunct() # jump to the entry point elif funct.isClosure(): - frames.add(Frame(stackBottom: stack.high - argcount, returnIp: ip, closure: funct.asClosure())) + frames.add(Frame(stackBottom: stack.high - argcount, returnIp: ip)) + #frames.add(Frame(stackBottom: stack.high - argcount, returnIp: ip, closure: funct.asClosure())) ip = funct.asClosure().getIp() elif funct.isNative(): var args: seq[NdValue] = newSeq[NdValue](argcount) @@ -159,9 +161,9 @@ proc run*(chunk: Chunk): InterpretResult = when debugClosures: msg = " Closures: [ " - for i in 0 .. frames.high(): - if frames[i].closure != nil: - msg &= debugStr(frames[i].closure) & " " + #for i in 0 .. frames.high(): + #if frames[i].closure != nil: + # msg &= debugStr(frames[i].closure) & " " msg &= "]" echo msg @@ -352,7 +354,7 @@ proc run*(chunk: Chunk): InterpretResult = break of opGetUpvalue: let slot = ip.readDU8() - let val = frames.peek().closure.get(slot).read() + let val = cclosure.get(slot).read() when debugClosures: echo &"CLOSURES - getupvalue got {val} from slot {slot}" stack.push(val) @@ -360,7 +362,7 @@ proc run*(chunk: Chunk): InterpretResult = let slot = ip.readDU8() when debugClosures: echo &"CLOSURES - setupvalue is setting {$stack.peek} to slot {slot}, number of slots: {frames.peek().closure.upvalueCount}" - frames.peek().closure.get(slot).write(stack.peek()) + cclosure.get(slot).write(stack.peek()) of opCloseUpvalue: let slot = ip.readDU8() stack[slot + frameBottom].addr.closeUpvalues() @@ -378,7 +380,7 @@ proc run*(chunk: Chunk): InterpretResult = echo &"CLOSURES - opClosure: local upvalue {loc[]} from local slot {slot} to slot {i}" closure.set(i, loc.captureUpvalue()) else: - let val = frames.peek().closure.get(slot) + let val = cclosure.get(slot) when debugClosures: echo &"CLOSURES - opClosure: non local upvalue {val.location[]} from slot {slot} to slot {i}" closure.set(i, val)