forked from robertkrimen/otto
/
type_string.go
87 lines (73 loc) · 1.72 KB
/
type_string.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
package otto
import (
"strconv"
"unicode/utf16"
)
func (runtime *_runtime) newStringObject(value Value) *_object {
self := runtime.newPrimitiveObject("String", toValue(toString(value)))
self.stash = newStringStash(toString(value), self.stash)
return self
}
type _stringStash struct {
value string
value16 []uint16
_stash
}
func newStringStash(value string, stash _stash) *_stringStash {
self := &_stringStash{
value: value,
value16: utf16.Encode([]rune(value)),
_stash: stash,
}
return self
}
func (self *_stringStash) test(name string) bool {
// .length
if name == "length" {
return true
}
// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 && index < int64(len(self.value16)) {
return true
}
return self._stash.test(name)
}
func (self *_stringStash) get(name string) Value {
// .length
if name == "length" {
return toValue(len(self.value16))
}
// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 && index < int64(len(self.value16)) {
return toValue(string(self.value16[index]))
}
return self._stash.get(name)
}
func (self *_stringStash) property(name string) *_property {
// .length
if name == "length" {
return &_property{
toValue(len(self.value16)),
0, // -Write -Enumerate -Configure
}
}
// .0, .1, .2, ...
index := stringToArrayIndex(name)
if index >= 0 && index < int64(len(self.value16)) {
return &_property{
toValue(string(self.value16[index])),
0, // -Write -Enumerate -Configure
}
}
return self._stash.property(name)
}
func (self *_stringStash) enumerate(each func(string)) {
// .0, .1, .2, ...
for index, _ := range self.value16 {
name := strconv.FormatInt(int64(index), 10)
each(name)
}
self._stash.enumerate(each)
}