mirror of
https://github.com/robertkrimen/otto
synced 2025-10-19 19:55:30 +08:00
* Context setup is now done via _newContext.
* _newContext is a function that resides in inline.go. _newContext is very flat,
resulting in almost no function calls (a 180 from the earlier status quo).
* inline.go is a Go source file that is built by Perl (via inline).
* Lots of crufty functions removed (along with all of their TODO & FIXME).
* In addition, before, the underlying value of _object.value was a pointer to
something. This made for extra work, since the type of _object.value is interface{},
which is already something of a pointer. Now, the underlying value of _object.value
in Function, Date, RegExp, ..., is a struct value.
* type_function.go was streamlined, removing superfluous struct fields and methods.
* There is now less "digging" to get to the actual value of a function, which is important
when makings lots of calls.
Before (without inline):
PASS
BenchmarkNew 2000 1067871 ns/op
ok github.com/robertkrimen/otto 3.336s
PASS
BenchmarkNew 2000 1077644 ns/op
ok github.com/robertkrimen/otto 3.367s
After (with inline):
PASS
BenchmarkNew 10000 364418 ns/op
ok github.com/robertkrimen/otto 4.616s
PASS
BenchmarkNew 10000 307241 ns/op
ok github.com/robertkrimen/otto 4.051s
This (partially) fixes #22
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package otto
|
|
|
|
import (
|
|
"strconv"
|
|
)
|
|
|
|
// Number
|
|
|
|
func numberValueFromNumberArgumentList(argumentList []Value) Value {
|
|
if len(argumentList) > 0 {
|
|
return toValue(toNumber(argumentList[0]))
|
|
}
|
|
return toValue(0)
|
|
}
|
|
|
|
func builtinNumber(call FunctionCall) Value {
|
|
return numberValueFromNumberArgumentList(call.ArgumentList)
|
|
}
|
|
|
|
func builtinNewNumber(self *_object, _ Value, argumentList []Value) Value {
|
|
return toValue(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
|
|
if len(call.ArgumentList) > 0 {
|
|
integer := _toInteger(call.Argument(0))
|
|
if integer < 2 || integer > 36 {
|
|
panic(newRangeError("RangeError: toString() radix must be between 2 and 36"))
|
|
}
|
|
radix = int(integer)
|
|
}
|
|
if radix == 10 {
|
|
return toValue(toString(value))
|
|
}
|
|
return toValue(numberToStringRadix(value, radix))
|
|
}
|
|
|
|
func builtinNumber_valueOf(call FunctionCall) Value {
|
|
return call.thisClassObject("Number").primitiveValue()
|
|
}
|
|
|
|
func builtinNumber_toFixed(call FunctionCall) Value {
|
|
if call.This.IsNaN() {
|
|
return toValue("NaN")
|
|
}
|
|
precision := toIntegerFloat(call.Argument(0))
|
|
if 0 > precision {
|
|
panic(newRangeError("RangeError: toFixed() precision must be greater than 0"))
|
|
}
|
|
return toValue(strconv.FormatFloat(toFloat(call.This), 'f', int(precision), 64))
|
|
}
|
|
|
|
func builtinNumber_toExponential(call FunctionCall) Value {
|
|
if call.This.IsNaN() {
|
|
return toValue("NaN")
|
|
}
|
|
precision := float64(-1)
|
|
if value := call.Argument(0); value.IsDefined() {
|
|
precision = toIntegerFloat(value)
|
|
if 0 > precision {
|
|
panic(newRangeError("RangeError: toExponential() precision must be greater than 0"))
|
|
}
|
|
}
|
|
return toValue(strconv.FormatFloat(toFloat(call.This), 'e', int(precision), 64))
|
|
}
|
|
|
|
func builtinNumber_toPrecision(call FunctionCall) Value {
|
|
if call.This.IsNaN() {
|
|
return toValue("NaN")
|
|
}
|
|
value := call.Argument(0)
|
|
if value.IsUndefined() {
|
|
return toValue(toString(call.This))
|
|
}
|
|
precision := toIntegerFloat(value)
|
|
if 1 > precision {
|
|
panic(newRangeError("RangeError: toPrecision() precision must be greater than 1"))
|
|
}
|
|
return toValue(strconv.FormatFloat(toFloat(call.This), 'g', int(precision), 64))
|
|
}
|