From 5561e9ec922a8732d73bf09649c3ebc371020636 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Tue, 12 Dec 2023 10:38:59 +0000 Subject: [PATCH] Fix BigInt parsing For an unknown reason BigInt can't be parsed and is being set as a UnserializableValue. To ensure that we parse it correctly to an int we need to catch this case when dealing with other UnserializableValue. --- common/remote_object.go | 11 +++++++++++ tests/remote_obj_test.go | 14 ++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/common/remote_object.go b/common/remote_object.go index fec6ec2af..ade675032 100644 --- a/common/remote_object.go +++ b/common/remote_object.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "math" + "regexp" "strconv" "strings" @@ -15,6 +16,8 @@ import ( "github.com/dop251/goja" ) +var bigIntR = regexp.MustCompile("^[0-9]*n$") + type objectOverflowError struct{} // Error returns the description of the overflow error. @@ -168,6 +171,14 @@ func parseRemoteObject(obj *cdpruntime.RemoteObject) (any, error) { return parseRemoteObjectValue(obj.Type, obj.Subtype, string(obj.Value), obj.Preview) } + if bigIntR.Match([]byte(obj.UnserializableValue)) { + n, err := strconv.ParseInt(strings.ReplaceAll(obj.UnserializableValue.String(), "n", ""), 10, 64) + if err != nil { + return nil, BigIntParseError{err} + } + return n, nil + } + switch obj.UnserializableValue.String() { case "-0": // To handle +0 divided by negative number return math.Float64frombits(0 | (1 << 63)), nil diff --git a/tests/remote_obj_test.go b/tests/remote_obj_test.go index be5ab9a68..0b6d92319 100644 --- a/tests/remote_obj_test.go +++ b/tests/remote_obj_test.go @@ -149,14 +149,12 @@ func TestEvalRemoteObjectParse(t *testing.T) { { name: "undefined", eval: "undefined", want: goja.Undefined(), }, - // { - // // Commented out until fix applied - // name: "bigint", eval: `BigInt("2")`, want: "2n", - // }, - // { - // // Commented out until fix applied - // name: "unwrapped_bigint", eval: "3n", want: "3n", - // }, + { + name: "bigint", eval: `BigInt("2")`, want: 2, + }, + { + name: "unwrapped_bigint", eval: "3n", want: 3, + }, { name: "float", eval: "3.14", want: 3.14, },