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

Refactor ExecutionContext.eval() and related code #293

Merged
merged 4 commits into from
Apr 19, 2022

Conversation

imiric
Copy link
Contributor

@imiric imiric commented Apr 13, 2022

This was done as part of the work for #291, but I split it off into a separate PR to make reviewing easier. See the commits for details.

Ivan Mirić added 3 commits April 13, 2022 11:26
Some exceptions were previously not exposed to the script (e.g. promise
rejection) so this makes sure the exception message is properly exposed.
This was causing data races if the runtime was also being used by the
event loop goroutine, since goja is not thread-safe. It also moves
towards removing the dependency on goja from internal methods, which is
what we're aiming for. A good rule of thumb to follow from now on is to
never use goja on unexported methods, and only on those that are
directly exposed to JS.

Part of #271
@imiric imiric requested a review from olegbespalov April 13, 2022 16:35
@olegbespalov
Copy link
Contributor

@imiric, the PR looks good, but it's just a refactoring, right? I mean, this isn't a fix for the #291?

@imiric
Copy link
Contributor Author

imiric commented Apr 18, 2022

@olegbespalov Correct, it doesn't fix the issue, but it avoids a couple of issues found while working on the fix, and does some general improvements. See the commit messages for details.

I hope to create the fix PR later today.

@imiric imiric mentioned this pull request Apr 18, 2022
@imiric imiric merged commit aa02b40 into main Apr 19, 2022
@imiric imiric deleted the refactor/291-execution-context branch April 19, 2022 09:05
@imiric imiric requested a review from inancgumus May 6, 2022 08:47
Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great to see that we're making progress towards #271 👏

I have some findings.

Comment on lines +82 to +86
expErr := fmt.Errorf("%w", errors.New(tc.errMsg))
require.IsType(t, expErr, got)
gotErr, ok := got.(error)
require.True(t, ok)
assert.Contains(t, gotErr.Error(), expErr.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious whether there was a reason for wrapping the error.

gotErr, ok := got.(error)
require.True(t, ok)
expErr := errors.New(tc.errMsg)
assert.Contains(t, gotErr.Error(), expErr.Error())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to wrap it for IsType() to pass. But while working on #300 that was failing as the error wasn't wrapped, so instead of handling both cases, I just removed it altogether to simplify. The type check is not important anyway.

Comment on lines +39 to +50
t.Run("ok/func", func(t *testing.T) {
t.Parallel()

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

got := p.Evaluate(tb.rt.ToValue("() => document.location.toString()"))

require.IsType(t, tb.rt.ToValue(""), got)
gotVal, _ := got.(goja.Value)
assert.Equal(t, "about:blank", gotVal.Export())
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this test can check that Evaluate can execute a proper function, it doesn't test whether Evaluate handles arguments correctly.

For example, the test can't detect the following:

func (f *Frame) evaluate(
	apiCtx context.Context,
	world executionWorld,
	opts evalOptions, pageFunc goja.Value, args ...goja.Value,
) (interface{}, error) {
	...     
	for _, a := range args {
		// evalArgs = append(evalArgs, a.Export())
		_ = a
	}
	...

An improved version of the test can be as follows (or as another test):

		...
		got := p.Evaluate(
			tb.rt.ToValue("(v) => { window['v'] = v; return window['v']; }"),
			tb.rt.ToValue("hey"),
		)

		require.IsType(t, tb.rt.ToValue("hey"), got)
		gotVal, _ := got.(goja.Value)
		assert.Equal(t, "hey", gotVal.Export())
		...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, good point. I changed it in 72a7ff4.

@imiric imiric mentioned this pull request May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants