aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/robertkrimen/otto/cmpl_evaluate.go
blob: 6741bf394553d30679fa8d38f72cd646f3a659d9 (plain) (blame)
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
94
95
96
package otto

import (
    "strconv"
)

func (self *_runtime) cmpl_evaluate_nodeProgram(node *_nodeProgram, eval bool) Value {
    if !eval {
        self.enterGlobalScope()
        defer func() {
            self.leaveScope()
        }()
    }
    self.cmpl_functionDeclaration(node.functionList)
    self.cmpl_variableDeclaration(node.varList)
    self.scope.frame.file = node.file
    return self.cmpl_evaluate_nodeStatementList(node.body)
}

func (self *_runtime) cmpl_call_nodeFunction(function *_object, stash *_fnStash, node *_nodeFunctionLiteral, this Value, 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
        self.scope.lexical.setValue(name, value, false)
    }

    if !argumentsFound {
        arguments := self.newArgumentsObject(indexOfParameterName, stash, len(argumentList))
        arguments.defineProperty("callee", toValue_object(function), 0101, false)
        stash.arguments = arguments
        // strict = false
        self.scope.lexical.setValue("arguments", toValue_object(arguments), false)
        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)
    if result.kind == valueResult {
        return result
    }

    return Value{}
}

func (self *_runtime) cmpl_functionDeclaration(list []*_nodeFunctionLiteral) {
    executionContext := self.scope
    eval := executionContext.eval
    stash := executionContext.variable

    for _, function := range list {
        name := function.name
        value := self.cmpl_evaluate_nodeExpression(function)
        if !stash.hasBinding(name) {
            stash.createBinding(name, eval == true, value)
        } else {
            // TODO 10.5.5.e
            stash.setBinding(name, value, false) // TODO strict
        }
    }
}

func (self *_runtime) cmpl_variableDeclaration(list []string) {
    executionContext := self.scope
    eval := executionContext.eval
    stash := executionContext.variable

    for _, name := range list {
        if !stash.hasBinding(name) {
            stash.createBinding(name, eval == true, Value{}) // TODO strict?
        }
    }
}