forked from robertkrimen/otto
/
type_reference.go
93 lines (79 loc) · 2 KB
/
type_reference.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
type referencer interface {
invalid() bool // IsUnresolvableReference
getValue() Value // getValue
putValue(Value) string // PutValue
delete() bool
}
// PropertyReference
type propertyReference struct {
name string
strict bool
base *object
runtime *runtime
at at
}
func newPropertyReference(rt *runtime, base *object, name string, strict bool, atv at) *propertyReference {
return &propertyReference{
runtime: rt,
name: name,
strict: strict,
base: base,
at: atv,
}
}
func (pr *propertyReference) invalid() bool {
return pr.base == nil
}
func (pr *propertyReference) getValue() Value {
if pr.base == nil {
panic(pr.runtime.panicReferenceError("'%s' is not defined", pr.name, pr.at))
}
return pr.base.get(pr.name)
}
func (pr *propertyReference) putValue(value Value) string {
if pr.base == nil {
return pr.name
}
pr.base.put(pr.name, value, pr.strict)
return ""
}
func (pr *propertyReference) delete() bool {
if pr.base == nil {
// TODO Throw an error if strict
return true
}
return pr.base.delete(pr.name, pr.strict)
}
type stashReference struct {
name string
strict bool
base stasher
}
func (sr *stashReference) invalid() bool {
return false // The base (an environment) will never be nil
}
func (sr *stashReference) getValue() Value {
return sr.base.getBinding(sr.name, sr.strict)
}
func (sr *stashReference) putValue(value Value) string {
sr.base.setValue(sr.name, value, sr.strict)
return ""
}
func (sr *stashReference) delete() bool {
if sr.base == nil {
// This should never be reached, but just in case
return false
}
return sr.base.deleteBinding(sr.name)
}
// getIdentifierReference.
func getIdentifierReference(rt *runtime, stash stasher, name string, strict bool, atv at) referencer {
if stash == nil {
return newPropertyReference(rt, nil, name, strict, atv)
}
if stash.hasBinding(name) {
return stash.newReference(name, strict, atv)
}
return getIdentifierReference(rt, stash.outer(), name, strict, atv)
}