forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_number.go
111 lines (97 loc) · 3.05 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
package otto
import (
"math"
"strconv"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
)
// Number
func numberValueFromNumberArgumentList(argumentList []Value) Value {
if len(argumentList) > 0 {
return argumentList[0].numberValue()
}
return intValue(0)
}
func builtinNumber(call FunctionCall) Value {
return numberValueFromNumberArgumentList(call.ArgumentList)
}
func builtinNewNumber(obj *object, argumentList []Value) Value {
return objectValue(obj.runtime.newNumber(numberValueFromNumberArgumentList(argumentList)))
}
func builtinNumberToString(call FunctionCall) Value {
// Will throw a TypeError if ThisObject is not a Number
value := call.thisClassObject(classNumberName).primitiveValue()
radix := 10
radixArgument := call.Argument(0)
if radixArgument.IsDefined() {
integer := toIntegerFloat(radixArgument)
if integer < 2 || integer > 36 {
panic(call.runtime.panicRangeError("toString() radix must be between 2 and 36"))
}
radix = int(integer)
}
if radix == 10 {
return stringValue(value.string())
}
return stringValue(numberToStringRadix(value, radix))
}
func builtinNumberValueOf(call FunctionCall) Value {
return call.thisClassObject(classNumberName).primitiveValue()
}
func builtinNumberToFixed(call FunctionCall) Value {
precision := toIntegerFloat(call.Argument(0))
if 20 < precision || 0 > precision {
panic(call.runtime.panicRangeError("toFixed() precision must be between 0 and 20"))
}
if call.This.IsNaN() {
return stringValue("NaN")
}
if value := call.This.float64(); math.Abs(value) >= 1e21 {
return stringValue(floatToString(value, 64))
}
return stringValue(strconv.FormatFloat(call.This.float64(), 'f', int(precision), 64))
}
func builtinNumberToExponential(call FunctionCall) Value {
if call.This.IsNaN() {
return stringValue("NaN")
}
precision := float64(-1)
if value := call.Argument(0); value.IsDefined() {
precision = toIntegerFloat(value)
if 0 > precision {
panic(call.runtime.panicRangeError("toString() radix must be between 2 and 36"))
}
}
return stringValue(strconv.FormatFloat(call.This.float64(), 'e', int(precision), 64))
}
func builtinNumberToPrecision(call FunctionCall) Value {
if call.This.IsNaN() {
return stringValue("NaN")
}
value := call.Argument(0)
if value.IsUndefined() {
return stringValue(call.This.string())
}
precision := toIntegerFloat(value)
if 1 > precision {
panic(call.runtime.panicRangeError("toPrecision() precision must be greater than 1"))
}
return stringValue(strconv.FormatFloat(call.This.float64(), 'g', int(precision), 64))
}
func builtinNumberIsNaN(call FunctionCall) Value {
if len(call.ArgumentList) < 1 {
return boolValue(false)
}
return boolValue(call.Argument(0).IsNaN())
}
func builtinNumberToLocaleString(call FunctionCall) Value {
value := call.thisClassObject(classNumberName).primitiveValue()
locale := call.Argument(0)
lang := defaultLanguage
if locale.IsDefined() {
lang = language.MustParse(locale.string())
}
p := message.NewPrinter(lang)
return stringValue(p.Sprintf("%v", number.Decimal(value.value)))
}