aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/obscuren/otto/environment.go
blob: 891a3c9af5a118710a7b687ef599568ee2675284 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package otto

import (
    "fmt"
)

// _environment

type _environment interface {
    HasBinding(string) bool

    CreateMutableBinding(string, bool)
    SetMutableBinding(string, Value, bool)
    // SetMutableBinding with Lazy CreateMutableBinding(..., true)
    SetValue(string, Value, bool)

    GetBindingValue(string, bool) Value
    GetValue(string, bool) Value // GetBindingValue
    DeleteBinding(string) bool
    ImplicitThisValue() *_object

    Outer() _environment

    newReference(string, bool) _reference
    clone(clone *_clone) _environment
    runtimeOf() *_runtime
}

// _functionEnvironment

type _functionEnvironment struct {
    _declarativeEnvironment
    arguments           *_object
    indexOfArgumentName map[string]string
}

func (runtime *_runtime) newFunctionEnvironment(outer _environment) *_functionEnvironment {
    return &_functionEnvironment{
        _declarativeEnvironment: _declarativeEnvironment{
            runtime:  runtime,
            outer:    outer,
            property: map[string]_declarativeProperty{},
        },
    }
}

func (self0 _functionEnvironment) clone(clone *_clone) _environment {
    return &_functionEnvironment{
        *(self0._declarativeEnvironment.clone(clone).(*_declarativeEnvironment)),
        clone.object(self0.arguments),
        self0.indexOfArgumentName,
    }
}

func (self _functionEnvironment) runtimeOf() *_runtime {
    return self._declarativeEnvironment.runtimeOf()
}

// _objectEnvironment

type _objectEnvironment struct {
    runtime     *_runtime
    outer       _environment
    Object      *_object
    ProvideThis bool
}

func (self *_objectEnvironment) runtimeOf() *_runtime {
    return self.runtime
}

func (runtime *_runtime) newObjectEnvironment(object *_object, outer _environment) *_objectEnvironment {
    if object == nil {
        object = runtime.newBaseObject()
        object.class = "environment"
    }
    return &_objectEnvironment{
        runtime: runtime,
        outer:   outer,
        Object:  object,
    }
}

func (self0 *_objectEnvironment) clone(clone *_clone) _environment {
    self1, exists := clone.objectEnvironment(self0)
    if exists {
        return self1
    }
    *self1 = _objectEnvironment{
        clone.runtime,
        clone.environment(self0.outer),
        clone.object(self0.Object),
        self0.ProvideThis,
    }
    return self1
}

func (self *_objectEnvironment) HasBinding(name string) bool {
    return self.Object.hasProperty(name)
}

func (self *_objectEnvironment) CreateMutableBinding(name string, deletable bool) {
    if self.Object.hasProperty(name) {
        panic(hereBeDragons())
    }
    mode := _propertyMode(0111)
    if !deletable {
        mode = _propertyMode(0110)
    }
    // TODO False?
    self.Object.defineProperty(name, UndefinedValue(), mode, false)
}

func (self *_objectEnvironment) SetMutableBinding(name string, value Value, strict bool) {
    self.Object.put(name, value, strict)
}

func (self *_objectEnvironment) SetValue(name string, value Value, throw bool) {
    if !self.HasBinding(name) {
        self.CreateMutableBinding(name, true) // Configurable by default
    }
    self.SetMutableBinding(name, value, throw)
}

func (self *_objectEnvironment) GetBindingValue(name string, strict bool) Value {
    if self.Object.hasProperty(name) {
        return self.Object.get(name)
    }
    if strict {
        panic(newReferenceError("Not Defined", name))
    }
    return UndefinedValue()
}

func (self *_objectEnvironment) GetValue(name string, throw bool) Value {
    return self.GetBindingValue(name, throw)
}

func (self *_objectEnvironment) DeleteBinding(name string) bool {
    return self.Object.delete(name, false)
}

func (self *_objectEnvironment) ImplicitThisValue() *_object {
    if self.ProvideThis {
        return self.Object
    }
    return nil
}

func (self *_objectEnvironment) Outer() _environment {
    return self.outer
}

func (self *_objectEnvironment) newReference(name string, strict bool) _reference {
    return newPropertyReference(self.Object, name, strict)
}

// _declarativeEnvironment

func (runtime *_runtime) newDeclarativeEnvironment(outer _environment) *_declarativeEnvironment {
    return &_declarativeEnvironment{
        runtime:  runtime,
        outer:    outer,
        property: map[string]_declarativeProperty{},
    }
}

func (self0 *_declarativeEnvironment) clone(clone *_clone) _environment {
    self1, exists := clone.declarativeEnvironment(self0)
    if exists {
        return self1
    }
    property := make(map[string]_declarativeProperty, len(self0.property))
    for index, value := range self0.property {
        property[index] = clone.declarativeProperty(value)
    }
    *self1 = _declarativeEnvironment{
        clone.runtime,
        clone.environment(self0.outer),
        property,
    }
    return self1
}

type _declarativeProperty struct {
    value     Value
    mutable   bool
    deletable bool
    readable  bool
}

type _declarativeEnvironment struct {
    runtime  *_runtime
    outer    _environment
    property map[string]_declarativeProperty
}

func (self *_declarativeEnvironment) HasBinding(name string) bool {
    _, exists := self.property[name]
    return exists
}

func (self *_declarativeEnvironment) runtimeOf() *_runtime {
    return self.runtime
}

func (self *_declarativeEnvironment) CreateMutableBinding(name string, deletable bool) {
    _, exists := self.property[name]
    if exists {
        panic(fmt.Errorf("CreateMutableBinding: %s: already exists", name))
    }
    self.property[name] = _declarativeProperty{
        value:     UndefinedValue(),
        mutable:   true,
        deletable: deletable,
        readable:  false,
    }
}

func (self *_declarativeEnvironment) SetMutableBinding(name string, value Value, strict bool) {
    property, exists := self.property[name]
    if !exists {
        panic(fmt.Errorf("SetMutableBinding: %s: missing", name))
    }
    if property.mutable {
        property.value = value
        self.property[name] = property
    } else {
        typeErrorResult(strict)
    }
}

func (self *_declarativeEnvironment) SetValue(name string, value Value, throw bool) {
    if !self.HasBinding(name) {
        self.CreateMutableBinding(name, false) // NOT deletable by default
    }
    self.SetMutableBinding(name, value, throw)
}

func (self *_declarativeEnvironment) GetBindingValue(name string, strict bool) Value {
    property, exists := self.property[name]
    if !exists {
        panic(fmt.Errorf("GetBindingValue: %s: missing", name))
    }
    if !property.mutable && !property.readable {
        if strict {
            panic(newTypeError())
        }
        return UndefinedValue()
    }
    return property.value
}

func (self *_declarativeEnvironment) GetValue(name string, throw bool) Value {
    return self.GetBindingValue(name, throw)
}

func (self *_declarativeEnvironment) DeleteBinding(name string) bool {
    property, exists := self.property[name]
    if !exists {
        return true
    }
    if !property.deletable {
        return false
    }
    delete(self.property, name)
    return true
}

func (self *_declarativeEnvironment) ImplicitThisValue() *_object {
    return nil
}

func (self *_declarativeEnvironment) Outer() _environment {
    return self.outer
}

func (self *_declarativeEnvironment) newReference(name string, strict bool) _reference {
    return newEnvironmentReference(self, name, strict, nil)
}