2015-02-16 13:28:33 +00:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
func (self *_runtime) cmpl_evaluate_nodeProgram(node *_nodeProgram, eval bool) Value {
|
|
|
|
if !eval {
|
|
|
|
self.enterGlobalScope()
|
|
|
|
defer func() {
|
|
|
|
self.leaveScope()
|
|
|
|
}()
|
|
|
|
}
|
2015-02-16 13:28:33 +00:00
|
|
|
self.cmpl_functionDeclaration(node.functionList)
|
|
|
|
self.cmpl_variableDeclaration(node.varList)
|
2015-03-20 12:22:01 +00:00
|
|
|
self.scope.frame.file = node.file
|
2015-02-16 13:28:33 +00:00
|
|
|
return self.cmpl_evaluate_nodeStatementList(node.body)
|
|
|
|
}
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
func (self *_runtime) cmpl_call_nodeFunction(function *_object, stash *_fnStash, node *_nodeFunctionLiteral, this Value, argumentList []Value) Value {
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
indexOfParameterName := make([]string, len(argumentList))
|
|
|
|
// function(abc, def, ghi)
|
|
|
|
// indexOfParameterName[0] = "abc"
|
|
|
|
// indexOfParameterName[1] = "def"
|
|
|
|
// indexOfParameterName[2] = "ghi"
|
|
|
|
// ...
|
|
|
|
|
|
|
|
argumentsFound := false
|
|
|
|
for index, name := range node.parameterList {
|
|
|
|
if name == "arguments" {
|
|
|
|
argumentsFound = true
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
value := Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
if index < len(argumentList) {
|
|
|
|
value = argumentList[index]
|
|
|
|
indexOfParameterName[index] = name
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
// strict = false
|
|
|
|
self.scope.lexical.setValue(name, value, false)
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !argumentsFound {
|
2015-03-20 12:22:01 +00:00
|
|
|
arguments := self.newArgumentsObject(indexOfParameterName, stash, len(argumentList))
|
2015-02-16 13:28:33 +00:00
|
|
|
arguments.defineProperty("callee", toValue_object(function), 0101, false)
|
2015-03-20 12:22:01 +00:00
|
|
|
stash.arguments = arguments
|
|
|
|
// strict = false
|
|
|
|
self.scope.lexical.setValue("arguments", toValue_object(arguments), false)
|
2015-02-16 13:28:33 +00:00
|
|
|
for index, _ := range argumentList {
|
|
|
|
if index < len(node.parameterList) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
indexAsString := strconv.FormatInt(int64(index), 10)
|
|
|
|
arguments.defineProperty(indexAsString, argumentList[index], 0111, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cmpl_functionDeclaration(node.functionList)
|
|
|
|
self.cmpl_variableDeclaration(node.varList)
|
|
|
|
|
|
|
|
result := self.cmpl_evaluate_nodeStatement(node.body)
|
2015-03-20 12:22:01 +00:00
|
|
|
if result.kind == valueResult {
|
2015-02-16 13:28:33 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
return Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_functionDeclaration(list []*_nodeFunctionLiteral) {
|
2015-03-20 12:22:01 +00:00
|
|
|
executionContext := self.scope
|
2015-02-16 13:28:33 +00:00
|
|
|
eval := executionContext.eval
|
2015-03-20 12:22:01 +00:00
|
|
|
stash := executionContext.variable
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
for _, function := range list {
|
|
|
|
name := function.name
|
|
|
|
value := self.cmpl_evaluate_nodeExpression(function)
|
2015-03-20 12:22:01 +00:00
|
|
|
if !stash.hasBinding(name) {
|
|
|
|
stash.createBinding(name, eval == true, value)
|
|
|
|
} else {
|
|
|
|
// TODO 10.5.5.e
|
|
|
|
stash.setBinding(name, value, false) // TODO strict
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_variableDeclaration(list []string) {
|
2015-03-20 12:22:01 +00:00
|
|
|
executionContext := self.scope
|
2015-02-16 13:28:33 +00:00
|
|
|
eval := executionContext.eval
|
2015-03-20 12:22:01 +00:00
|
|
|
stash := executionContext.variable
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
for _, name := range list {
|
2015-03-20 12:22:01 +00:00
|
|
|
if !stash.hasBinding(name) {
|
|
|
|
stash.createBinding(name, eval == true, Value{}) // TODO strict?
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|