fix for in loop symbols (#290)
* fix for in loop symbols * fix comments
This commit is contained in:
parent
22e1e35e11
commit
e059953c35
2 changed files with 20 additions and 2 deletions
|
@ -847,8 +847,8 @@ func (c *Compiler) compileForInStmt(stmt *parser.ForInStmt) error {
|
|||
// ... body ...
|
||||
// }
|
||||
//
|
||||
// ":it" is a local variable but will be conflict with other user variables
|
||||
// because character ":" is not allowed.
|
||||
// ":it" is a local variable but it will not conflict with other user variables
|
||||
// because character ":" is not allowed in the variable names.
|
||||
|
||||
// init
|
||||
// :it = iterator(iterable)
|
||||
|
@ -893,6 +893,7 @@ func (c *Compiler) compileForInStmt(stmt *parser.ForInStmt) error {
|
|||
if keySymbol.Scope == ScopeGlobal {
|
||||
c.emit(stmt, parser.OpSetGlobal, keySymbol.Index)
|
||||
} else {
|
||||
keySymbol.LocalAssigned = true
|
||||
c.emit(stmt, parser.OpDefineLocal, keySymbol.Index)
|
||||
}
|
||||
}
|
||||
|
@ -909,6 +910,7 @@ func (c *Compiler) compileForInStmt(stmt *parser.ForInStmt) error {
|
|||
if valueSymbol.Scope == ScopeGlobal {
|
||||
c.emit(stmt, parser.OpSetGlobal, valueSymbol.Index)
|
||||
} else {
|
||||
valueSymbol.LocalAssigned = true
|
||||
c.emit(stmt, parser.OpDefineLocal, valueSymbol.Index)
|
||||
}
|
||||
}
|
||||
|
|
16
vm_test.go
16
vm_test.go
|
@ -1531,7 +1531,23 @@ func TestFunction(t *testing.T) {
|
|||
add2 := newAdder(2);
|
||||
out = add2(5);
|
||||
`, nil, 7)
|
||||
expectRun(t, `
|
||||
m := {a: 1}
|
||||
for k,v in m {
|
||||
func(){
|
||||
out = k
|
||||
}()
|
||||
}
|
||||
`, nil, "a")
|
||||
|
||||
expectRun(t, `
|
||||
m := {a: 1}
|
||||
for k,v in m {
|
||||
func(){
|
||||
out = v
|
||||
}()
|
||||
}
|
||||
`, nil, 1)
|
||||
// function as a argument
|
||||
expectRun(t, `
|
||||
add := func(a, b) { return a + b };
|
||||
|
|
Loading…
Reference in a new issue