Skip to content

Commit bcd7cc6

Browse files
committed
Added IsNumber(), IsBigInt() and IsString() convenience methods. Treat nil *big.Int as 0n.
1 parent 203961f commit bcd7cc6

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

runtime.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,13 @@ Notes on individual types:
16201620
Primitive types (numbers, string, bool) are converted to the corresponding JavaScript primitives. These values
16211621
are goroutine-safe and can be transferred between runtimes.
16221622
1623+
# *big.Int
1624+
1625+
A *big.Int value is converted to a BigInt value. Note, because BigInt is immutable, but *big.Int isn't, the value is
1626+
copied. Export()'ing this value returns a *big.Int which is also a copy.
1627+
1628+
If the pointer value is nil, the resulting BigInt is 0n.
1629+
16231630
# Strings
16241631
16251632
Because of the difference in internal string representation between ECMAScript (which uses UTF-16) and Go (which uses
@@ -1861,7 +1868,11 @@ func (r *Runtime) toValue(i interface{}, origValue reflect.Value) Value {
18611868
case float64:
18621869
return floatToValue(i)
18631870
case *big.Int:
1864-
return (*valueBigInt)(new(big.Int).Set(i))
1871+
v := new(big.Int)
1872+
if i != nil {
1873+
v.Set(i)
1874+
}
1875+
return (*valueBigInt)(v)
18651876
case map[string]interface{}:
18661877
if i == nil {
18671878
return _null
@@ -2536,6 +2547,25 @@ func IsInfinity(v Value) bool {
25362547
return v == _positiveInf || v == _negativeInf
25372548
}
25382549

2550+
func IsNumber(v Value) bool {
2551+
switch v.(type) {
2552+
case valueInt, valueFloat:
2553+
return true
2554+
default:
2555+
return false
2556+
}
2557+
}
2558+
2559+
func IsBigInt(v Value) bool {
2560+
_, ok := v.(*valueBigInt)
2561+
return ok
2562+
}
2563+
2564+
func IsString(v Value) bool {
2565+
_, ok := v.(String)
2566+
return ok
2567+
}
2568+
25392569
// Undefined returns JS undefined value. Note if global 'undefined' property is changed this still returns the original value.
25402570
func Undefined() Value {
25412571
return _undefined

runtime_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"math"
7+
"math/big"
78
"reflect"
89
"runtime"
910
"strconv"
@@ -2845,6 +2846,9 @@ func TestErrorFormatSymbols(t *testing.T) {
28452846
vm := New()
28462847
vm.Set("a", func() (Value, error) { return nil, errors.New("something %s %f") })
28472848
_, err := vm.RunString("a()")
2849+
if err == nil {
2850+
t.Fatal("expected error")
2851+
}
28482852
if !strings.Contains(err.Error(), "something %s %f") {
28492853
t.Fatalf("Wrong value %q", err.Error())
28502854
}
@@ -3107,6 +3111,12 @@ func TestToNumber(t *testing.T) {
31073111
testScriptWithTestLib(SCRIPT, _undefined, t)
31083112
}
31093113

3114+
func TestToValueNilBigInt(t *testing.T) {
3115+
vm := New()
3116+
vm.Set("n", (*big.Int)(nil))
3117+
vm.testScript(`n === 0n`, valueTrue, t)
3118+
}
3119+
31103120
/*
31113121
func TestArrayConcatSparse(t *testing.T) {
31123122
function foo(a,b,c)

0 commit comments

Comments
 (0)