1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-26 20:28:49 +08:00

Add Number.toString

This commit is contained in:
Robert Krimen
2013-02-05 08:41:10 -08:00
parent 809a0a0ba6
commit d297d3c508
6 changed files with 46 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package otto
import (
"math"
"strconv"
time_ "time"
)
@@ -275,6 +276,18 @@ func newContext() *_runtime {
"Number", builtinNumber,
builtinNewNumber,
self.Global.NumberPrototype,
"toString", func(call FunctionCall) Value {
// Will throw a TypeError if ThisObject is not a Number
value := call.thisClassObject("Number").primitiveValue()
radix := 10
if len(call.ArgumentList) > 0 {
radix = int(toInteger(call.Argument(0)))
if radix < 2 || radix > 36 {
panic(newRangeError("RangeError: toString() radix must be between 2 and 36"))
}
}
return toValue(strconv.FormatInt(toInteger(value), radix))
},
"valueOf", func(call FunctionCall) Value {
return *call.thisClassObject("Number").primitive
},