Skip to content

Commit

Permalink
js: Give better error message for async functions and SharedArray
Browse files Browse the repository at this point in the history
This is mostly to prevent users from using it for now.

updates #3014
  • Loading branch information
mstoykov committed May 30, 2023
1 parent 158ea04 commit 53fd247
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion js/modules/k6/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (d *Data) Exports() modules.Exports {
}
}

// TODO potentially try to DRY with this with a similar message in the `k6` js module.
// This likely won't be needed as SharedArray should likely support async function at some point ... somehow.
const asyncFunctionNotSupportedMsg = "SharedArray constructor does not support async functions as second argument, " +
"please see https://k6.io/docs/javascript-api/k6/group/#working-with-async-functions for more info"

// 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 +79,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

0 comments on commit 53fd247

Please sign in to comment.