From 37f8e9a2460cc10ee975f900b11af0fc55ca1406 Mon Sep 17 00:00:00 2001 From: Conrad Pankoff Date: Tue, 17 Dec 2019 17:34:20 +1100 Subject: [PATCH] support Number.isNaN --- builtin_number.go | 7 +++++++ builtin_test.go | 11 +++++++++++ inline.go | 31 +++++++++++++++++++++++++++++++ inline.pl | 1 + 4 files changed, 50 insertions(+) diff --git a/builtin_number.go b/builtin_number.go index f99a42a..9f11ef6 100644 --- a/builtin_number.go +++ b/builtin_number.go @@ -88,6 +88,13 @@ func builtinNumber_toPrecision(call FunctionCall) Value { 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 { return builtinNumber_toString(call) } diff --git a/builtin_test.go b/builtin_test.go index f5be00a..dd76dcd 100644 --- a/builtin_test.go +++ b/builtin_test.go @@ -134,3 +134,14 @@ func TestGlobal_unescape(t *testing.T) { `, "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) + }) +} diff --git a/inline.go b/inline.go index 6e5df83..149ffd8 100644 --- a/inline.go +++ b/inline.go @@ -2627,6 +2627,29 @@ func _newContext(runtime *_runtime) { 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: runtime, class: "Number", @@ -2713,6 +2736,13 @@ func _newContext(runtime *_runtime) { value: runtime.global.NumberPrototype, }, }, + "isNaN": _property{ + mode: 0101, + value: Value{ + kind: valueObject, + value: isNaN_function, + }, + }, "MAX_VALUE": _property{ mode: 0, value: Value{ @@ -2752,6 +2782,7 @@ func _newContext(runtime *_runtime) { propertyOrder: []string{ "length", "prototype", + "isNaN", "MAX_VALUE", "MIN_VALUE", "NaN", diff --git a/inline.pl b/inline.pl index e902904..e21444d 100755 --- a/inline.pl +++ b/inline.pl @@ -372,6 +372,7 @@ sub newContext { 1, $self->functionDeclare( $class, + "isNaN", 1, ), $self->numberConstantDeclare( "MAX_VALUE", "math.MaxFloat64",