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(page.evaluate): better function serialization #3480

Merged
merged 7 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/ExecutionContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,23 @@ class ExecutionContext {
if (typeof pageFunction !== 'function')
throw new Error('The following is not a function: ' + pageFunction);

let functionText = pageFunction.toString();
try {
eval(functionText);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd be worried about unintended side effects here. Consider: function functionText(){return 5}.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch; switched to new Function instead

} catch (e1) {
// This means we have a function shorthand. Try another
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// This means we have a function shorthand. Try another
// This means we might have a function shorthand. Try another

// time prefixing 'function '.
functionText = 'function ' + functionText;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about async static generators?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

async done; the rest is too exotic for now!

try {
eval(functionText);
} catch (e2) {
// We tried hard to serialize, but there's a weird beast here.
throw new Error('Passed function is not well-serializable!');
}
}

const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.callFunctionOn', {
functionDeclaration: pageFunction.toString() + '\n' + suffix + '\n',
functionDeclaration: functionText + '\n' + suffix + '\n',
executionContextId: this._contextId,
arguments: args.map(convertArgument.bind(this)),
returnByValue: false,
Expand Down
7 changes: 7 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ module.exports.addTests = function({testRunner, expect, headless}) {
const result = await page.evaluate(() => 7 * 3);
expect(result).toBe(21);
});
it('should work with function shorthands', async({page, server}) => {
const a = {
sum(a, b) { return a + b; }
};
const result = await page.evaluate(a.sum, 1, 2);
expect(result).toBe(3);
});
it('should throw when evaluation triggers reload', async({page, server}) => {
let error = null;
await page.evaluate(() => {
Expand Down