-
Notifications
You must be signed in to change notification settings - Fork 1
/
builtin_number.go
303 lines (262 loc) · 8.55 KB
/
builtin_number.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package sobek
import (
"math"
"sync"
"github.com/grafana/sobek/ftoa"
)
func (r *Runtime) toNumber(v Value) Value {
switch t := v.(type) {
case valueFloat, valueInt:
return v
case *Object:
switch t := t.self.(type) {
case *primitiveValueObject:
return r.toNumber(t.pValue)
case *objectGoReflect:
if t.class == classNumber && t.valueOf != nil {
return t.valueOf()
}
}
if t == r.global.NumberPrototype {
return _positiveZero
}
}
panic(r.NewTypeError("Value is not a number: %s", v))
}
func (r *Runtime) numberproto_valueOf(call FunctionCall) Value {
return r.toNumber(call.This)
}
func (r *Runtime) numberproto_toString(call FunctionCall) Value {
var numVal Value
switch t := call.This.(type) {
case valueFloat, valueInt:
numVal = t
case *Object:
switch t := t.self.(type) {
case *primitiveValueObject:
numVal = r.toNumber(t.pValue)
case *objectGoReflect:
if t.class == classNumber {
if t.toString != nil {
return t.toString()
}
if t.valueOf != nil {
numVal = t.valueOf()
}
}
}
if t == r.global.NumberPrototype {
return asciiString("0")
}
}
if numVal == nil {
panic(r.NewTypeError("Value is not a number"))
}
var radix int
if arg := call.Argument(0); arg != _undefined {
radix = int(arg.ToInteger())
} else {
radix = 10
}
if radix < 2 || radix > 36 {
panic(r.newError(r.getRangeError(), "toString() radix argument must be between 2 and 36"))
}
num := numVal.ToFloat()
if math.IsNaN(num) {
return stringNaN
}
if math.IsInf(num, 1) {
return stringInfinity
}
if math.IsInf(num, -1) {
return stringNegInfinity
}
if radix == 10 {
return asciiString(fToStr(num, ftoa.ModeStandard, 0))
}
return asciiString(ftoa.FToBaseStr(num, radix))
}
func (r *Runtime) numberproto_toFixed(call FunctionCall) Value {
num := r.toNumber(call.This).ToFloat()
prec := call.Argument(0).ToInteger()
if prec < 0 || prec > 100 {
panic(r.newError(r.getRangeError(), "toFixed() precision must be between 0 and 100"))
}
if math.IsNaN(num) {
return stringNaN
}
return asciiString(fToStr(num, ftoa.ModeFixed, int(prec)))
}
func (r *Runtime) numberproto_toExponential(call FunctionCall) Value {
num := r.toNumber(call.This).ToFloat()
precVal := call.Argument(0)
var prec int64
if precVal == _undefined {
return asciiString(fToStr(num, ftoa.ModeStandardExponential, 0))
} else {
prec = precVal.ToInteger()
}
if math.IsNaN(num) {
return stringNaN
}
if math.IsInf(num, 1) {
return stringInfinity
}
if math.IsInf(num, -1) {
return stringNegInfinity
}
if prec < 0 || prec > 100 {
panic(r.newError(r.getRangeError(), "toExponential() precision must be between 0 and 100"))
}
return asciiString(fToStr(num, ftoa.ModeExponential, int(prec+1)))
}
func (r *Runtime) numberproto_toPrecision(call FunctionCall) Value {
numVal := r.toNumber(call.This)
precVal := call.Argument(0)
if precVal == _undefined {
return numVal.toString()
}
num := numVal.ToFloat()
prec := precVal.ToInteger()
if math.IsNaN(num) {
return stringNaN
}
if math.IsInf(num, 1) {
return stringInfinity
}
if math.IsInf(num, -1) {
return stringNegInfinity
}
if prec < 1 || prec > 100 {
panic(r.newError(r.getRangeError(), "toPrecision() precision must be between 1 and 100"))
}
return asciiString(fToStr(num, ftoa.ModePrecision, int(prec)))
}
func (r *Runtime) number_isFinite(call FunctionCall) Value {
switch arg := call.Argument(0).(type) {
case valueInt:
return valueTrue
case valueFloat:
f := float64(arg)
return r.toBoolean(!math.IsInf(f, 0) && !math.IsNaN(f))
default:
return valueFalse
}
}
func (r *Runtime) number_isInteger(call FunctionCall) Value {
switch arg := call.Argument(0).(type) {
case valueInt:
return valueTrue
case valueFloat:
f := float64(arg)
return r.toBoolean(!math.IsNaN(f) && !math.IsInf(f, 0) && math.Floor(f) == f)
default:
return valueFalse
}
}
func (r *Runtime) number_isNaN(call FunctionCall) Value {
if f, ok := call.Argument(0).(valueFloat); ok && math.IsNaN(float64(f)) {
return valueTrue
}
return valueFalse
}
func (r *Runtime) number_isSafeInteger(call FunctionCall) Value {
arg := call.Argument(0)
if i, ok := arg.(valueInt); ok && i >= -(maxInt-1) && i <= maxInt-1 {
return valueTrue
}
if arg == _negativeZero {
return valueTrue
}
return valueFalse
}
func createNumberProtoTemplate() *objectTemplate {
t := newObjectTemplate()
t.protoFactory = func(r *Runtime) *Object {
return r.global.ObjectPrototype
}
t.putStr("constructor", func(r *Runtime) Value { return valueProp(r.getNumber(), true, false, true) })
t.putStr("toExponential", func(r *Runtime) Value { return r.methodProp(r.numberproto_toExponential, "toExponential", 1) })
t.putStr("toFixed", func(r *Runtime) Value { return r.methodProp(r.numberproto_toFixed, "toFixed", 1) })
t.putStr("toLocaleString", func(r *Runtime) Value { return r.methodProp(r.numberproto_toString, "toLocaleString", 0) })
t.putStr("toPrecision", func(r *Runtime) Value { return r.methodProp(r.numberproto_toPrecision, "toPrecision", 1) })
t.putStr("toString", func(r *Runtime) Value { return r.methodProp(r.numberproto_toString, "toString", 1) })
t.putStr("valueOf", func(r *Runtime) Value { return r.methodProp(r.numberproto_valueOf, "valueOf", 0) })
return t
}
var numberProtoTemplate *objectTemplate
var numberProtoTemplateOnce sync.Once
func getNumberProtoTemplate() *objectTemplate {
numberProtoTemplateOnce.Do(func() {
numberProtoTemplate = createNumberProtoTemplate()
})
return numberProtoTemplate
}
func (r *Runtime) getNumberPrototype() *Object {
ret := r.global.NumberPrototype
if ret == nil {
ret = &Object{runtime: r}
r.global.NumberPrototype = ret
o := r.newTemplatedObject(getNumberProtoTemplate(), ret)
o.class = classNumber
}
return ret
}
func (r *Runtime) getParseFloat() *Object {
ret := r.global.parseFloat
if ret == nil {
ret = r.newNativeFunc(r.builtin_parseFloat, "parseFloat", 1)
r.global.parseFloat = ret
}
return ret
}
func (r *Runtime) getParseInt() *Object {
ret := r.global.parseInt
if ret == nil {
ret = r.newNativeFunc(r.builtin_parseInt, "parseInt", 2)
r.global.parseInt = ret
}
return ret
}
func createNumberTemplate() *objectTemplate {
t := newObjectTemplate()
t.protoFactory = func(r *Runtime) *Object {
return r.getFunctionPrototype()
}
t.putStr("length", func(r *Runtime) Value { return valueProp(intToValue(1), false, false, true) })
t.putStr("name", func(r *Runtime) Value { return valueProp(asciiString("Number"), false, false, true) })
t.putStr("prototype", func(r *Runtime) Value { return valueProp(r.getNumberPrototype(), false, false, false) })
t.putStr("EPSILON", func(r *Runtime) Value { return valueProp(_epsilon, false, false, false) })
t.putStr("isFinite", func(r *Runtime) Value { return r.methodProp(r.number_isFinite, "isFinite", 1) })
t.putStr("isInteger", func(r *Runtime) Value { return r.methodProp(r.number_isInteger, "isInteger", 1) })
t.putStr("isNaN", func(r *Runtime) Value { return r.methodProp(r.number_isNaN, "isNaN", 1) })
t.putStr("isSafeInteger", func(r *Runtime) Value { return r.methodProp(r.number_isSafeInteger, "isSafeInteger", 1) })
t.putStr("MAX_SAFE_INTEGER", func(r *Runtime) Value { return valueProp(valueInt(maxInt-1), false, false, false) })
t.putStr("MIN_SAFE_INTEGER", func(r *Runtime) Value { return valueProp(valueInt(-(maxInt - 1)), false, false, false) })
t.putStr("MIN_VALUE", func(r *Runtime) Value { return valueProp(valueFloat(math.SmallestNonzeroFloat64), false, false, false) })
t.putStr("MAX_VALUE", func(r *Runtime) Value { return valueProp(valueFloat(math.MaxFloat64), false, false, false) })
t.putStr("NaN", func(r *Runtime) Value { return valueProp(_NaN, false, false, false) })
t.putStr("NEGATIVE_INFINITY", func(r *Runtime) Value { return valueProp(_negativeInf, false, false, false) })
t.putStr("parseFloat", func(r *Runtime) Value { return valueProp(r.getParseFloat(), true, false, true) })
t.putStr("parseInt", func(r *Runtime) Value { return valueProp(r.getParseInt(), true, false, true) })
t.putStr("POSITIVE_INFINITY", func(r *Runtime) Value { return valueProp(_positiveInf, false, false, false) })
return t
}
var numberTemplate *objectTemplate
var numberTemplateOnce sync.Once
func getNumberTemplate() *objectTemplate {
numberTemplateOnce.Do(func() {
numberTemplate = createNumberTemplate()
})
return numberTemplate
}
func (r *Runtime) getNumber() *Object {
ret := r.global.Number
if ret == nil {
ret = &Object{runtime: r}
r.global.Number = ret
r.newTemplatedFuncObject(getNumberTemplate(), ret, r.builtin_Number,
r.wrapNativeConstruct(r.builtin_newNumber, ret, r.getNumberPrototype()))
}
return ret
}