forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
226 lines (192 loc) · 5.99 KB
/
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
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
package goja
import (
"io"
"strconv"
"unicode/utf16"
"unicode/utf8"
)
var (
stringTrue valueString = asciiString("true")
stringFalse valueString = asciiString("false")
stringNull valueString = asciiString("null")
stringUndefined valueString = asciiString("undefined")
stringObjectC valueString = asciiString("object")
stringFunction valueString = asciiString("function")
stringBoolean valueString = asciiString("boolean")
stringString valueString = asciiString("string")
stringNumber valueString = asciiString("number")
stringNaN valueString = asciiString("NaN")
stringInfinity = asciiString("Infinity")
stringPlusInfinity = asciiString("+Infinity")
stringNegInfinity = asciiString("-Infinity")
stringEmpty valueString = asciiString("")
string__proto__ valueString = asciiString("__proto__")
stringError valueString = asciiString("Error")
stringTypeError valueString = asciiString("TypeError")
stringReferenceError valueString = asciiString("ReferenceError")
stringSyntaxError valueString = asciiString("SyntaxError")
stringRangeError valueString = asciiString("RangeError")
stringEvalError valueString = asciiString("EvalError")
stringURIError valueString = asciiString("URIError")
stringGoError valueString = asciiString("GoError")
stringObjectNull valueString = asciiString("[object Null]")
stringObjectObject valueString = asciiString("[object Object]")
stringObjectUndefined valueString = asciiString("[object Undefined]")
stringGlobalObject valueString = asciiString("Global Object")
stringInvalidDate valueString = asciiString("Invalid Date")
)
type valueString interface {
Value
charAt(int64) rune
length() int64
concat(valueString) valueString
substring(start, end int64) valueString
compareTo(valueString) int
reader(start int) io.RuneReader
index(valueString, int64) int64
lastIndex(valueString, int64) int64
toLower() valueString
toUpper() valueString
toTrimmedUTF8() string
}
type stringObject struct {
baseObject
value valueString
length int64
lengthProp valueProperty
}
func newUnicodeString(s string) valueString {
return unicodeString(utf16.Encode([]rune(s)))
}
func newStringValue(s string) valueString {
for _, chr := range s {
if chr >= utf8.RuneSelf {
return newUnicodeString(s)
}
}
return asciiString(s)
}
func (s *stringObject) init() {
s.baseObject.init()
s.setLength()
}
func (s *stringObject) setLength() {
if s.value != nil {
s.length = s.value.length()
}
s.lengthProp.value = intToValue(s.length)
s._put("length", &s.lengthProp)
}
func (s *stringObject) get(n Value) Value {
if idx := toIdx(n); idx >= 0 && idx < s.length {
return s.getIdx(idx)
}
return s.baseObject.get(n)
}
func (s *stringObject) getStr(name string) Value {
if i := strToIdx(name); i >= 0 && i < s.length {
return s.getIdx(i)
}
return s.baseObject.getStr(name)
}
func (s *stringObject) getPropStr(name string) Value {
if i := strToIdx(name); i >= 0 && i < s.length {
return s.getIdx(i)
}
return s.baseObject.getPropStr(name)
}
func (s *stringObject) getProp(n Value) Value {
if i := toIdx(n); i >= 0 && i < s.length {
return s.getIdx(i)
}
return s.baseObject.getProp(n)
}
func (s *stringObject) getOwnProp(name string) Value {
if i := strToIdx(name); i >= 0 && i < s.length {
val := s.getIdx(i)
return &valueProperty{
value: val,
enumerable: true,
}
}
return s.baseObject.getOwnProp(name)
}
func (s *stringObject) getIdx(idx int64) Value {
return s.value.substring(idx, idx+1)
}
func (s *stringObject) put(n Value, val Value, throw bool) {
if i := toIdx(n); i >= 0 && i < s.length {
s.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%d' of a String", i)
return
}
s.baseObject.put(n, val, throw)
}
func (s *stringObject) putStr(name string, val Value, throw bool) {
if i := strToIdx(name); i >= 0 && i < s.length {
s.val.runtime.typeErrorResult(throw, "Cannot assign to read only property '%d' of a String", i)
return
}
s.baseObject.putStr(name, val, throw)
}
func (s *stringObject) defineOwnProperty(n Value, descr propertyDescr, throw bool) bool {
if i := toIdx(n); i >= 0 && i < s.length {
s.val.runtime.typeErrorResult(throw, "Cannot redefine property: %d", i)
return false
}
return s.baseObject.defineOwnProperty(n, descr, throw)
}
type stringPropIter struct {
str valueString // separate, because obj can be the singleton
obj *stringObject
idx, length int64
recursive bool
}
func (i *stringPropIter) next() (propIterItem, iterNextFunc) {
if i.idx < i.length {
name := strconv.FormatInt(i.idx, 10)
i.idx++
return propIterItem{name: name, enumerable: _ENUM_TRUE}, i.next
}
return i.obj.baseObject._enumerate(i.recursive)()
}
func (s *stringObject) _enumerate(recursive bool) iterNextFunc {
return (&stringPropIter{
str: s.value,
obj: s,
length: s.length,
recursive: recursive,
}).next
}
func (s *stringObject) enumerate(all, recursive bool) iterNextFunc {
return (&propFilterIter{
wrapped: s._enumerate(recursive),
all: all,
seen: make(map[string]bool),
}).next
}
func (s *stringObject) deleteStr(name string, throw bool) bool {
if i := strToIdx(name); i >= 0 && i < s.length {
s.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of a String", i)
return false
}
return s.baseObject.deleteStr(name, throw)
}
func (s *stringObject) delete(n Value, throw bool) bool {
if i := toIdx(n); i >= 0 && i < s.length {
s.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of a String", i)
return false
}
return s.baseObject.delete(n, throw)
}
func (s *stringObject) hasOwnProperty(n Value) bool {
if i := toIdx(n); i >= 0 && i < s.length {
return true
}
return s.baseObject.hasOwnProperty(n)
}
func (s *stringObject) hasOwnPropertyStr(name string) bool {
if i := strToIdx(name); i >= 0 && i < s.length {
return true
}
return s.baseObject.hasOwnPropertyStr(name)
}