mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.6 KiB
94 lines
2.6 KiB
10 years ago
|
package otto
|
||
|
|
||
|
import (
|
||
|
"math"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
// Number
|
||
|
|
||
|
func numberValueFromNumberArgumentList(argumentList []Value) Value {
|
||
|
if len(argumentList) > 0 {
|
||
10 years ago
|
return argumentList[0].numberValue()
|
||
10 years ago
|
}
|
||
|
return toValue_int(0)
|
||
|
}
|
||
|
|
||
|
func builtinNumber(call FunctionCall) Value {
|
||
|
return numberValueFromNumberArgumentList(call.ArgumentList)
|
||
|
}
|
||
|
|
||
10 years ago
|
func builtinNewNumber(self *_object, argumentList []Value) Value {
|
||
10 years ago
|
return toValue_object(self.runtime.newNumber(numberValueFromNumberArgumentList(argumentList)))
|
||
|
}
|
||
|
|
||
|
func builtinNumber_toString(call FunctionCall) Value {
|
||
|
// Will throw a TypeError if ThisObject is not a Number
|
||
|
value := call.thisClassObject("Number").primitiveValue()
|
||
|
radix := 10
|
||
|
radixArgument := call.Argument(0)
|
||
|
if radixArgument.IsDefined() {
|
||
|
integer := toIntegerFloat(radixArgument)
|
||
|
if integer < 2 || integer > 36 {
|
||
8 years ago
|
panic(call.runtime.panicRangeError("toString() radix must be between 2 and 36"))
|
||
10 years ago
|
}
|
||
|
radix = int(integer)
|
||
|
}
|
||
|
if radix == 10 {
|
||
10 years ago
|
return toValue_string(value.string())
|
||
10 years ago
|
}
|
||
|
return toValue_string(numberToStringRadix(value, radix))
|
||
|
}
|
||
|
|
||
|
func builtinNumber_valueOf(call FunctionCall) Value {
|
||
|
return call.thisClassObject("Number").primitiveValue()
|
||
|
}
|
||
|
|
||
|
func builtinNumber_toFixed(call FunctionCall) Value {
|
||
|
precision := toIntegerFloat(call.Argument(0))
|
||
|
if 20 < precision || 0 > precision {
|
||
10 years ago
|
panic(call.runtime.panicRangeError("toFixed() precision must be between 0 and 20"))
|
||
10 years ago
|
}
|
||
|
if call.This.IsNaN() {
|
||
|
return toValue_string("NaN")
|
||
|
}
|
||
10 years ago
|
value := call.This.float64()
|
||
10 years ago
|
if math.Abs(value) >= 1e21 {
|
||
|
return toValue_string(floatToString(value, 64))
|
||
|
}
|
||
10 years ago
|
return toValue_string(strconv.FormatFloat(call.This.float64(), 'f', int(precision), 64))
|
||
10 years ago
|
}
|
||
|
|
||
|
func builtinNumber_toExponential(call FunctionCall) Value {
|
||
|
if call.This.IsNaN() {
|
||
|
return toValue_string("NaN")
|
||
|
}
|
||
|
precision := float64(-1)
|
||
|
if value := call.Argument(0); value.IsDefined() {
|
||
|
precision = toIntegerFloat(value)
|
||
|
if 0 > precision {
|
||
8 years ago
|
panic(call.runtime.panicRangeError("toString() radix must be between 2 and 36"))
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
return toValue_string(strconv.FormatFloat(call.This.float64(), 'e', int(precision), 64))
|
||
10 years ago
|
}
|
||
|
|
||
|
func builtinNumber_toPrecision(call FunctionCall) Value {
|
||
|
if call.This.IsNaN() {
|
||
|
return toValue_string("NaN")
|
||
|
}
|
||
|
value := call.Argument(0)
|
||
|
if value.IsUndefined() {
|
||
10 years ago
|
return toValue_string(call.This.string())
|
||
10 years ago
|
}
|
||
|
precision := toIntegerFloat(value)
|
||
|
if 1 > precision {
|
||
8 years ago
|
panic(call.runtime.panicRangeError("toPrecision() precision must be greater than 1"))
|
||
10 years ago
|
}
|
||
10 years ago
|
return toValue_string(strconv.FormatFloat(call.This.float64(), 'g', int(precision), 64))
|
||
10 years ago
|
}
|
||
|
|
||
|
func builtinNumber_toLocaleString(call FunctionCall) Value {
|
||
|
return builtinNumber_toString(call)
|
||
|
}
|