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 Frame = object
stackBottom: int # the absolute index of where 0 inside the frame is stackBottom: int # the absolute index of where 0 inside the frame is
returnIp: ptr uint8 returnIp: ptr uint8
closure: Closure[NdValue] # closure: Closure[NdValue]
InterpretResult* = enum InterpretResult* = enum
irOK, irRuntimeError irOK, irRuntimeError
@ -72,6 +72,7 @@ proc run*(chunk: Chunk): InterpretResult =
chunk.constants[ip.readDU8()] chunk.constants[ip.readDU8()]
template frameBottom: int = frames.peek().stackBottom template frameBottom: int = frames.peek().stackBottom
template cclosure: Closure[NdValue] = stack.getIndex(frameBottom()).asClosure()
template call(funct: NdValue, argcount: int, error: untyped) = template call(funct: NdValue, argcount: int, error: untyped) =
if funct.isFunct(): if funct.isFunct():
@ -79,7 +80,8 @@ proc run*(chunk: Chunk): InterpretResult =
frames.add(Frame(stackBottom: stack.high - argcount, returnIp: ip)) frames.add(Frame(stackBottom: stack.high - argcount, returnIp: ip))
ip = funct.asFunct() # jump to the entry point ip = funct.asFunct() # jump to the entry point
elif funct.isClosure(): 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() ip = funct.asClosure().getIp()
elif funct.isNative(): elif funct.isNative():
var args: seq[NdValue] = newSeq[NdValue](argcount) var args: seq[NdValue] = newSeq[NdValue](argcount)
@ -159,9 +161,9 @@ proc run*(chunk: Chunk): InterpretResult =
when debugClosures: when debugClosures:
msg = " Closures: [ " msg = " Closures: [ "
for i in 0 .. frames.high(): #for i in 0 .. frames.high():
if frames[i].closure != nil: #if frames[i].closure != nil:
msg &= debugStr(frames[i].closure) & " " # msg &= debugStr(frames[i].closure) & " "
msg &= "]" msg &= "]"
echo msg echo msg
@ -352,7 +354,7 @@ proc run*(chunk: Chunk): InterpretResult =
break break
of opGetUpvalue: of opGetUpvalue:
let slot = ip.readDU8() let slot = ip.readDU8()
let val = frames.peek().closure.get(slot).read() let val = cclosure.get(slot).read()
when debugClosures: when debugClosures:
echo &"CLOSURES - getupvalue got {val} from slot {slot}" echo &"CLOSURES - getupvalue got {val} from slot {slot}"
stack.push(val) stack.push(val)
@ -360,7 +362,7 @@ proc run*(chunk: Chunk): InterpretResult =
let slot = ip.readDU8() let slot = ip.readDU8()
when debugClosures: when debugClosures:
echo &"CLOSURES - setupvalue is setting {$stack.peek} to slot {slot}, number of slots: {frames.peek().closure.upvalueCount}" 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: of opCloseUpvalue:
let slot = ip.readDU8() let slot = ip.readDU8()
stack[slot + frameBottom].addr.closeUpvalues() 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}" echo &"CLOSURES - opClosure: local upvalue {loc[]} from local slot {slot} to slot {i}"
closure.set(i, loc.captureUpvalue()) closure.set(i, loc.captureUpvalue())
else: else:
let val = frames.peek().closure.get(slot) let val = cclosure.get(slot)
when debugClosures: when debugClosures:
echo &"CLOSURES - opClosure: non local upvalue {val.location[]} from slot {slot} to slot {i}" echo &"CLOSURES - opClosure: non local upvalue {val.location[]} from slot {slot} to slot {i}"
closure.set(i, val) closure.set(i, val)