aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/obscuren/otto/type_reference.go
blob: 6c4f37278bf203d426867179e43e2da2c57d117d (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package otto

import (
    "github.com/robertkrimen/otto/ast"
)

type _reference interface {
    GetBase() interface{}      // GetBase
    GetName() string           // GetReferencedName
    IsStrict() bool            // IsStrictReference
    IsUnresolvable() bool      // IsUnresolvableReference
    IsPropertyReference() bool // IsPropertyReference
    GetValue() Value           // GetValue
    PutValue(Value) bool       // PutValue
    Delete() bool
}

// Reference

type _referenceDefault struct {
    name   string
    strict bool
}

func (self _referenceDefault) GetName() string {
    return self.name
}

func (self _referenceDefault) IsStrict() bool {
    return self.strict
}

// PropertyReference

type _propertyReference struct {
    _referenceDefault
    Base *_object
}

func newPropertyReference(base *_object, name string, strict bool) *_propertyReference {
    return &_propertyReference{
        Base: base,
        _referenceDefault: _referenceDefault{
            name:   name,
            strict: strict,
        },
    }
}

func (self *_propertyReference) GetBase() interface{} {
    return self.Base
}

func (self *_propertyReference) IsUnresolvable() bool {
    return self.Base == nil
}

func (self *_propertyReference) IsPropertyReference() bool {
    return true
}

func (self *_propertyReference) GetValue() Value {
    if self.Base == nil {
        panic(newReferenceError("notDefined", self.name))
    }
    return self.Base.get(self.name)
}

func (self *_propertyReference) PutValue(value Value) bool {
    if self.Base == nil {
        return false
    }
    self.Base.put(self.name, value, self.IsStrict())
    return true
}

func (self *_propertyReference) Delete() bool {
    if self.Base == nil {
        // TODO Throw an error if strict
        return true
    }
    return self.Base.delete(self.name, self.IsStrict())
}

// ArgumentReference

func newArgumentReference(base *_object, name string, strict bool) *_propertyReference {
    if base == nil {
        panic(hereBeDragons())
    }
    return newPropertyReference(base, name, strict)
}

type _environmentReference struct {
    _referenceDefault
    Base _environment
    node ast.Node
}

func newEnvironmentReference(base _environment, name string, strict bool, node ast.Node) *_environmentReference {
    return &_environmentReference{
        Base: base,
        _referenceDefault: _referenceDefault{
            name:   name,
            strict: strict,
        },
        node: node,
    }
}

func (self *_environmentReference) GetBase() interface{} {
    return self.Base
}

func (self *_environmentReference) IsUnresolvable() bool {
    return self.Base == nil // The base (an environment) will never be nil
}

func (self *_environmentReference) IsPropertyReference() bool {
    return false
}

func (self *_environmentReference) GetValue() Value {
    if self.Base == nil {
        // This should never be reached, but just in case
    }
    return self.Base.GetValue(self.name, self.IsStrict())
}

func (self *_environmentReference) PutValue(value Value) bool {
    if self.Base == nil {
        // This should never be reached, but just in case
        return false
    }
    self.Base.SetValue(self.name, value, self.IsStrict())
    return true
}

func (self *_environmentReference) Delete() bool {
    if self.Base == nil {
        // This should never be reached, but just in case
        return false
    }
    return self.Base.DeleteBinding(self.name)
}

// getIdentifierReference

func getIdentifierReference(environment _environment, name string, strict bool) _reference {
    if environment == nil {
        return newPropertyReference(nil, name, strict)
    }
    if environment.HasBinding(name) {
        return environment.newReference(name, strict)
    }
    return getIdentifierReference(environment.Outer(), name, strict)
}