fix: correct local variable storage and remove premature local slot allocation in vm

The compiler's storeVariable function was incorrectly emitting CODE_STORE_LOCAL for all variables, including locals that already have their value in the correct stack slot. This change renames storeVariable to defineVariable and adds logic to only emit CODE_STORE_GLOBAL for globals, while for locals it emits CODE_DUP to preserve the value through the subsequent POP. Additionally, the vm's callFunction no longer pre-allocates empty slots for locals, as they are now placed on the stack during compilation. New test files verify duplicate local/parameter detection and correct local variable ordering in block scopes.
This commit is contained in:
Bob Nystrom
2013-11-09 19:38:01 +00:00
parent d43a62e08c
commit 04dda643d0
9 changed files with 64 additions and 16 deletions
+6
View File
@@ -0,0 +1,6 @@
class Foo {
bar {
var a = "value"
var a = "other" // expect error
}
}
+6
View File
@@ -0,0 +1,6 @@
class Foo {
bar(arg,
arg) { // expect error
"body"
}
}
+5
View File
@@ -0,0 +1,5 @@
class Foo {
bar(a) {
var a = "oops" // expect error
}
}
+14
View File
@@ -0,0 +1,14 @@
class Foo {
bar {
var a = "a"
io.write(a) // expect: a
var b = a + " b"
io.write(b) // expect: a b
var c = a + " c"
io.write(c) // expect: a c
var d = b + " d"
io.write(d) // expect: a b d
}
}
Foo.new.bar