risor-io / risor

Fast and flexible scripting for Go developers and DevOps.
https://risor.io
Apache License 2.0
610 stars 26 forks source link

Bug: panic inside range #211

Closed the-plash closed 6 months ago

the-plash commented 6 months ago

The following risor code panics when run:

for i := range 1 {
  try(func() {
    print("hello")
    error("kaboom")
  })
}
hello
panic: interface conversion: *object.NilType is not object.Iterator: missing method Entry

Assigning the return value of print(...) to a local variable fixes the problem

for i := range 1 {
  try(func() {
    x := print("hello")
    error("kaboom")
  })
}

hello

The bug seems to be related to the implicit nil return value from print(...) which pollutes the stack. The same behaviour is observed more clearly with the following code:

for i := range 1 {
  try(func() {
    "hello"
    error("kaboom")
  })
}

panic: interface conversion: *object.String is not object.Iterator: missing method Entry

The string "hello" is implicitly returned and is popped from the stack instead of the expected IntIter when resuming. Unfortunately i don't have any more insight on why this may be happening.

Possibly related to #173

myzie commented 6 months ago

Thanks for the thorough bug report @the-plash. I'll track it down.

myzie commented 6 months ago

Hi @the-plash, this should be fixed in v1.5.2. Let me know how it does for you.