rum-goggles/v1/vendor/github.com/robertkrimen/otto/cmpl_evaluate.go

94 lines
2.4 KiB
Go
Raw Normal View History

package otto
import (
"strconv"
)
2024-04-04 14:46:14 +00:00
func (rt *runtime) cmplEvaluateNodeProgram(node *nodeProgram, eval bool) Value {
if !eval {
2024-04-04 14:46:14 +00:00
rt.enterGlobalScope()
defer rt.leaveScope()
}
2024-04-04 14:46:14 +00:00
rt.cmplFunctionDeclaration(node.functionList)
rt.cmplVariableDeclaration(node.varList)
rt.scope.frame.file = node.file
return rt.cmplEvaluateNodeStatementList(node.body)
}
2024-04-04 14:46:14 +00:00
func (rt *runtime) cmplCallNodeFunction(function *object, stash *fnStash, node *nodeFunctionLiteral, argumentList []Value) Value {
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
}
value := Value{}
if index < len(argumentList) {
value = argumentList[index]
indexOfParameterName[index] = name
}
// strict = false
2024-04-04 14:46:14 +00:00
rt.scope.lexical.setValue(name, value, false)
}
if !argumentsFound {
2024-04-04 14:46:14 +00:00
arguments := rt.newArgumentsObject(indexOfParameterName, stash, len(argumentList))
arguments.defineProperty("callee", objectValue(function), 0o101, false)
stash.arguments = arguments
// strict = false
2024-04-04 14:46:14 +00:00
rt.scope.lexical.setValue("arguments", objectValue(arguments), false)
for index := range argumentList {
if index < len(node.parameterList) {
continue
}
indexAsString := strconv.FormatInt(int64(index), 10)
2024-04-04 14:46:14 +00:00
arguments.defineProperty(indexAsString, argumentList[index], 0o111, false)
}
}
2024-04-04 14:46:14 +00:00
rt.cmplFunctionDeclaration(node.functionList)
rt.cmplVariableDeclaration(node.varList)
2024-04-04 14:46:14 +00:00
result := rt.cmplEvaluateNodeStatement(node.body)
if result.kind == valueResult {
return result
}
return Value{}
}
2024-04-04 14:46:14 +00:00
func (rt *runtime) cmplFunctionDeclaration(list []*nodeFunctionLiteral) {
executionContext := rt.scope
eval := executionContext.eval
stash := executionContext.variable
for _, function := range list {
name := function.name
2024-04-04 14:46:14 +00:00
value := rt.cmplEvaluateNodeExpression(function)
if !stash.hasBinding(name) {
2024-04-04 14:46:14 +00:00
stash.createBinding(name, eval, value)
} else {
// TODO 10.5.5.e
stash.setBinding(name, value, false) // TODO strict
}
}
}
2024-04-04 14:46:14 +00:00
func (rt *runtime) cmplVariableDeclaration(list []string) {
executionContext := rt.scope
eval := executionContext.eval
stash := executionContext.variable
for _, name := range list {
if !stash.hasBinding(name) {
2024-04-04 14:46:14 +00:00
stash.createBinding(name, eval, Value{}) // TODO strict?
}
}
}