Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

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 15, 2023
1 parent 5ee0522 commit 5561e9e
Show file tree
Hide file tree
Showing 2 changed files with 17 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
14 changes: 6 additions & 8 deletions tests/remote_obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit 5561e9e

Please sign in to comment.