Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BigInt remote object parsing #1133

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 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 bigIntRegex = regexp.MustCompile("^[0-9]*n$")

type objectOverflowError struct{}

// Error returns the description of the overflow error.
Expand Down Expand Up @@ -164,11 +167,21 @@ func parseExceptionDetails(exc *cdpruntime.ExceptionDetails) string {
// parseRemoteObject is to be used by callers that require the string value
// to be parsed to a Go type.
func parseRemoteObject(obj *cdpruntime.RemoteObject) (any, error) {
if obj.UnserializableValue == "" {
uv := obj.UnserializableValue

if uv == "" {
return parseRemoteObjectValue(obj.Type, obj.Subtype, string(obj.Value), obj.Preview)
}

switch obj.UnserializableValue.String() {
if bigIntRegex.Match([]byte(uv)) {
n, err := strconv.ParseInt(strings.ReplaceAll(uv.String(), "n", ""), 10, 64)
if err != nil {
return nil, BigIntParseError{err}
}
return n, nil
}

switch uv.String() {
case "-0": // To handle +0 divided by negative number
return math.Float64frombits(0 | (1 << 63)), nil
case "NaN":
Expand All @@ -182,7 +195,7 @@ func parseRemoteObject(obj *cdpruntime.RemoteObject) (any, error) {
// We should never get here, as previous switch statement should
// be exhaustive and contain all possible unserializable values.
// See: https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-UnserializableValue
return nil, UnserializableValueError{obj.UnserializableValue}
return nil, UnserializableValueError{uv}
}

func valueFromRemoteObject(ctx context.Context, robj *cdpruntime.RemoteObject) (goja.Value, error) {
Expand Down
84 changes: 84 additions & 0 deletions tests/remote_obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -106,3 +107,86 @@ func TestConsoleLogParse(t *testing.T) {
})
}
}

func TestEvalRemoteObjectParse(t *testing.T) {
t.Parallel()

tests := []struct {
name string
eval string
want any
}{
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
{
name: "number", eval: "1", want: 1,
},
{
name: "string", eval: `"some string"`, want: "some string",
},
{
name: "bool", eval: "true", want: true,
},
{
name: "empty_array", eval: "[]", want: []any{},
},
{
name: "empty_object", eval: "{}", want: goja.Undefined(),
},
{
name: "filled_object", eval: `{return {foo:"bar"};}`, want: map[string]any{"foo": "bar"},
},
{
name: "filled_array", eval: `{return ["foo","bar"];}`, want: []any{0: "foo", 1: "bar"},
},
{
name: "filled_array", eval: `() => true`, want: `function()`,
},
{
name: "empty", eval: "", want: "",
},
{
name: "null", eval: "null", want: "null",
},
{
name: "undefined", eval: "undefined", want: goja.Undefined(),
},
{
name: "bigint", eval: `BigInt("2")`, want: 2,
},
{
name: "unwrapped_bigint", eval: "3n", want: 3,
},
{
name: "float", eval: "3.14", want: 3.14,
},
{
name: "scientific_notation", eval: "123e-5", want: 0.00123,
},
// TODO:
// {
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
// // This test is ignored until https://github.com/grafana/xk6-browser/issues/1132
// // has been resolved.
// name: "partially_parsed",
// eval: "window",
// want: `{"document":"#document","location":"Location","name":"","self":"Window","window":"Window"}`,
// },
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

tb := newTestBrowser(t, withFileServer())
p := tb.NewPage(nil)

var got any
if tt.eval == "" {
got = p.Evaluate(tb.toGojaValue(`() => ""`))
} else {
got = p.Evaluate(tb.toGojaValue(fmt.Sprintf("() => %s", tt.eval)))
}

assert.Equal(t, tb.toGojaValue(tt.want), got)
})
}
}
Loading