Skip to content

Commit

Permalink
Fix a bug where module functions would fail on subsequent calls
Browse files Browse the repository at this point in the history
Signed-off-by: Fadion Dashi <[email protected]>
  • Loading branch information
fadion committed Jul 31, 2017
1 parent 2611d7d commit 6fa45f0
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ func (i *Interpreter) runFunction(node *ast.FunctionCall, scope *Scope) DataType
}

function := fn.(*FunctionType)
fnscope := NewScopeFrom(function.Scope)

// Non-variadic function shouldn't be called
// with more arguments than declared.
Expand Down Expand Up @@ -711,7 +712,7 @@ func (i *Interpreter) runFunction(node *ast.FunctionCall, scope *Scope) DataType
}
}

function.Scope.Write(param.Name.Value, value)
fnscope.Write(param.Name.Value, value)
defaultCount++
}
}
Expand Down Expand Up @@ -763,17 +764,17 @@ func (i *Interpreter) runFunction(node *ast.FunctionCall, scope *Scope) DataType
if function.Variadic && index >= countParams {
arguments = append(arguments, value)
} else {
function.Scope.Write(paramname.Value, value)
fnscope.Write(paramname.Value, value)
}
}

// Variadic argument is passed as a single array
// of parameters.
if function.Variadic && len(arguments) > 0 {
function.Scope.Write(function.Parameters[len(function.Parameters)-1].Name.Value, &ArrayType{Elements: arguments})
fnscope.Write(function.Parameters[len(function.Parameters)-1].Name.Value, &ArrayType{Elements: arguments})
}

result := i.unwrapReturnValue(i.Interpret(function.Body, function.Scope))
result := i.unwrapReturnValue(i.Interpret(function.Body, fnscope))
if result == nil {
return nil
}
Expand All @@ -786,10 +787,6 @@ func (i *Interpreter) runFunction(node *ast.FunctionCall, scope *Scope) DataType
}
}

// Reset the scope so inner variables aren't
// carried over to the next call.
function.Scope = NewScopeFrom(scope)

return result
}

Expand Down

0 comments on commit 6fa45f0

Please sign in to comment.