Fix local variable declarations in the REPL.

A statement like:

  for (i in 1..2)

When run in the REPL declares a local variable ("i"), but not inside
a function or method body. This hit a corner case in the compiler
where it didn't have the correct slot indexes set up.

That corner case is because sometimes when you compile a chunk, local
slot zero is pre-allocated -- either to refer to "this" or to hold the
closure for a function so that it doesn't get GCed while running. But
if you're compiling top-level code, that slot isn't allocated. But top
level code for the REPL *should* be, because that gets invoked like a
function.

To simplify things, *every* compiled chunk now pre-allocates slot zero.
That way, there are fewer cases to keep in mind.

Also fixed an issue where a GC during an import could collected the
imported module body's closure.

Fix #456.
This commit is contained in:
Bob Nystrom
2018-04-27 08:20:49 -07:00
parent 64f8bf7ae3
commit c5ce6fac46
9 changed files with 66 additions and 73 deletions

View File

@ -1,6 +1,6 @@
{
var a0 = "value"
var a1 = a0
// Slot zero is always taken to hold the closure or receiver.
var a1 = "value"
var a2 = a1
var a3 = a2
var a4 = a3

View File

@ -1,10 +1,10 @@
// Can have more than 256 local variables in a local scope, as long as they
// Can have more than 255 local variables in a local scope, as long as they
// aren't all in scope at the same time.
{
{
var a0 = "value a"
var a1 = a0
// Slot zero is always taken to hold the closure or receiver.
var a1 = "value a"
var a2 = a1
var a3 = a2
var a4 = a3
@ -263,8 +263,8 @@
}
{
var b0 = "value b"
var b1 = b0
// Slot zero is always taken to hold the closure or receiver.
var b1 = "value b"
var b2 = b1
var b3 = b2
var b4 = b3

View File

@ -1,6 +1,6 @@
{
var a0 = "value"
var a1 = a0
// Slot zero is always taken to hold the closure or receiver.
var a1 = "value"
var a2 = a1
var a3 = a2
var a4 = a3

View File

@ -1,6 +1,6 @@
{
var a0 = "value"
var a1 = a0
// Slot zero is always taken to hold the closure or receiver.
var a1 = "value"
var a2 = a1
var a3 = a2
var a4 = a3