1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

support Number.isNaN

This commit is contained in:
Conrad Pankoff 2019-12-17 17:34:20 +11:00
parent 9fa7c0c0f8
commit 37f8e9a246
4 changed files with 50 additions and 0 deletions

View File

@ -88,6 +88,13 @@ func builtinNumber_toPrecision(call FunctionCall) Value {
return toValue_string(strconv.FormatFloat(call.This.float64(), 'g', int(precision), 64)) return toValue_string(strconv.FormatFloat(call.This.float64(), 'g', int(precision), 64))
} }
func builtinNumber_isNaN(call FunctionCall) Value {
if len(call.ArgumentList) < 1 {
return toValue_bool(false)
}
return toValue_bool(call.Argument(0).IsNaN())
}
func builtinNumber_toLocaleString(call FunctionCall) Value { func builtinNumber_toLocaleString(call FunctionCall) Value {
return builtinNumber_toString(call) return builtinNumber_toString(call)
} }

View File

@ -134,3 +134,14 @@ func TestGlobal_unescape(t *testing.T) {
`, "abc,==,abc=%+32,世界") `, "abc,==,abc=%+32,世界")
}) })
} }
func TestNumber_isNaN(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`Number.isNaN(1)`, false)
test(`Number.isNaN(null)`, false)
test(`Number.isNaN()`, false)
test(`Number.isNaN(Number.NaN)`, true)
test(`Number.isNaN(0+undefined)`, true)
})
}

View File

@ -2627,6 +2627,29 @@ func _newContext(runtime *_runtime) {
call: builtinNumber_toLocaleString, call: builtinNumber_toLocaleString,
}, },
} }
isNaN_function := &_object{
runtime: runtime,
class: "Function",
objectClass: _classObject,
prototype: runtime.global.FunctionPrototype,
extensible: true,
property: map[string]_property{
"length": _property{
mode: 0,
value: Value{
kind: valueNumber,
value: 1,
},
},
},
propertyOrder: []string{
"length",
},
value: _nativeFunctionObject{
name: "isNaN",
call: builtinNumber_isNaN,
},
}
runtime.global.NumberPrototype = &_object{ runtime.global.NumberPrototype = &_object{
runtime: runtime, runtime: runtime,
class: "Number", class: "Number",
@ -2713,6 +2736,13 @@ func _newContext(runtime *_runtime) {
value: runtime.global.NumberPrototype, value: runtime.global.NumberPrototype,
}, },
}, },
"isNaN": _property{
mode: 0101,
value: Value{
kind: valueObject,
value: isNaN_function,
},
},
"MAX_VALUE": _property{ "MAX_VALUE": _property{
mode: 0, mode: 0,
value: Value{ value: Value{
@ -2752,6 +2782,7 @@ func _newContext(runtime *_runtime) {
propertyOrder: []string{ propertyOrder: []string{
"length", "length",
"prototype", "prototype",
"isNaN",
"MAX_VALUE", "MAX_VALUE",
"MIN_VALUE", "MIN_VALUE",
"NaN", "NaN",

View File

@ -372,6 +372,7 @@ sub newContext {
1, 1,
$self->functionDeclare( $self->functionDeclare(
$class, $class,
"isNaN", 1,
), ),
$self->numberConstantDeclare( $self->numberConstantDeclare(
"MAX_VALUE", "math.MaxFloat64", "MAX_VALUE", "math.MaxFloat64",