forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmpl_evaluate.go
93 lines (81 loc) · 2.45 KB
/
cmpl_evaluate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package otto
import (
"strconv"
)
func (rt *runtime) cmplEvaluateNodeProgram(node *nodeProgram, eval bool) Value {
if !eval {
rt.enterGlobalScope()
defer rt.leaveScope()
}
rt.cmplFunctionDeclaration(node.functionList)
rt.cmplVariableDeclaration(node.varList)
rt.scope.frame.file = node.file
return rt.cmplEvaluateNodeStatementList(node.body)
}
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
rt.scope.lexical.setValue(name, value, false)
}
if !argumentsFound {
arguments := rt.newArgumentsObject(indexOfParameterName, stash, len(argumentList))
arguments.defineProperty("callee", objectValue(function), 0o101, false)
stash.arguments = arguments
// strict = false
rt.scope.lexical.setValue("arguments", objectValue(arguments), false)
for index := range argumentList {
if index < len(node.parameterList) {
continue
}
indexAsString := strconv.FormatInt(int64(index), 10)
arguments.defineProperty(indexAsString, argumentList[index], 0o111, false)
}
}
rt.cmplFunctionDeclaration(node.functionList)
rt.cmplVariableDeclaration(node.varList)
result := rt.cmplEvaluateNodeStatement(node.body)
if result.kind == valueResult {
return result
}
return Value{}
}
func (rt *runtime) cmplFunctionDeclaration(list []*nodeFunctionLiteral) {
executionContext := rt.scope
eval := executionContext.eval
stash := executionContext.variable
for _, function := range list {
name := function.name
value := rt.cmplEvaluateNodeExpression(function)
if !stash.hasBinding(name) {
stash.createBinding(name, eval, value)
} else {
// TODO 10.5.5.e
stash.setBinding(name, value, false) // TODO strict
}
}
}
func (rt *runtime) cmplVariableDeclaration(list []string) {
executionContext := rt.scope
eval := executionContext.eval
stash := executionContext.variable
for _, name := range list {
if !stash.hasBinding(name) {
stash.createBinding(name, eval, Value{}) // TODO strict?
}
}
}