Skip to content

Commit

Permalink
Fix BigInt parsing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ankur22 committed Dec 12, 2023
1 parent 9dec54d commit 9520f70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 11 additions & 0 deletions common/remote_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math"
"regexp"
"strconv"
"strings"

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions tests/remote_obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ 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",
// },
{
// Commented out until fix applied
name: "bigint", eval: `BigInt("2")`, want: 2,
},
{
// Commented out until fix applied
name: "unwrapped_bigint", eval: "3n", want: 3,
},
{
name: "float", eval: "3.14", want: 3.14,
},
Expand Down

0 comments on commit 9520f70

Please sign in to comment.