use stack instead of frames to find current closure

This commit is contained in:
prod2 2022-02-09 07:06:40 +01:00
parent 7e5dff3263
commit f86cce7977
1 changed files with 10 additions and 8 deletions

View File

@ -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)