Skip to content

Commit

Permalink
Forbid async shared array (#3102)
Browse files Browse the repository at this point in the history
This is mostly to prevent users from using it for now.

Also move isAsyncFunction to js/common in order to reuse it

updates #3014 


Co-authored-by: Ivan Mirić <[email protected]>
  • Loading branch information
mstoykov and Ivan Mirić authored Jun 1, 2023
1 parent b46ca04 commit 23ff170
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
8 changes: 8 additions & 0 deletions js/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ func RunWithPanicCatching(logger logrus.FieldLogger, rt *goja.Runtime, fn func()
func IsNullish(v goja.Value) bool {
return v == nil || goja.IsUndefined(v) || goja.IsNull(v)
}

// IsAsyncFunction checks if the provided value is an AsyncFunction
func IsAsyncFunction(rt *goja.Runtime, val goja.Value) bool {
if IsNullish(val) {
return false
}
return val.ToObject(rt).Get("constructor").ToObject(rt).Get("name").String() == "AsyncFunction"
}
9 changes: 8 additions & 1 deletion js/modules/k6/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func (d *Data) Exports() modules.Exports {
}
}

const asyncFunctionNotSupportedMsg = "SharedArray constructor does not support async functions as second argument"

// sharedArray is a constructor returning a shareable read-only array
// indentified by the name and having their contents be whatever the call returns
func (d *Data) sharedArray(call goja.ConstructorCall) *goja.Object {
Expand All @@ -74,8 +76,13 @@ func (d *Data) sharedArray(call goja.ConstructorCall) *goja.Object {
if name == "" {
common.Throw(rt, errors.New("empty name provided to SharedArray's constructor"))
}
val := call.Argument(1)

if common.IsAsyncFunction(rt, val) {
common.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))
}

fn, ok := goja.AssertFunction(call.Argument(1))
fn, ok := goja.AssertFunction(val)
if !ok {
common.Throw(rt, errors.New("a function is expected as the second argument of SharedArray's constructor"))
}
Expand Down
8 changes: 8 additions & 0 deletions js/modules/k6/data/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func TestSharedArrayConstructorExceptions(t *testing.T) {
code: `var s = new SharedArray("wat3", "astring");`,
err: "a function is expected",
},
"async function": {
code: `var s = new SharedArray("wat3", async function() {});`,
err: "SharedArray constructor does not support async functions as second argument",
},
"async lambda": {
code: `var s = new SharedArray("wat3", async () => {});`,
err: "SharedArray constructor does not support async functions as second argument",
},
}

for name, testCase := range cases {
Expand Down
11 changes: 2 additions & 9 deletions js/modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ func (mi *K6) RandomSeed(seed int64) {
mi.vu.Runtime().SetRandSource(randSource)
}

func isAsyncFunction(rt *goja.Runtime, val goja.Value) bool {
if common.IsNullish(val) {
return false
}
return val.ToObject(rt).Get("constructor").ToObject(rt).Get("name").String() == "AsyncFunction"
}

// Group wraps a function call and executes it within the provided group name.
func (mi *K6) Group(name string, val goja.Value) (goja.Value, error) {
state := mi.vu.State()
Expand All @@ -109,7 +102,7 @@ func (mi *K6) Group(name string, val goja.Value) (goja.Value, error) {
if !ok {
return nil, errors.New("group() requires a callback as a second argument")
}
if isAsyncFunction(mi.vu.Runtime(), val) {
if common.IsAsyncFunction(mi.vu.Runtime(), val) {
return goja.Undefined(), fmt.Errorf(asyncFunctionNotSupportedMsg, "group")
}
g, err := state.Group.Group(name)
Expand Down Expand Up @@ -194,7 +187,7 @@ func (mi *K6) Check(arg0, checks goja.Value, extras ...goja.Value) (bool, error)
tags = tags.With("check", check.Name)
}

if isAsyncFunction(rt, val) {
if common.IsAsyncFunction(rt, val) {
return false, fmt.Errorf(asyncFunctionNotSupportedMsg, "check")
}

Expand Down

0 comments on commit 23ff170

Please sign in to comment.