@@ -55,28 +55,35 @@ export class CRExecutionContext implements js.ExecutionContextDelegate {
55
55
if ( typeof pageFunction !== 'function' )
56
56
throw new Error ( `Expected to get |string| or |function| as the first argument, but got "${ pageFunction } " instead.` ) ;
57
57
58
- let functionText = pageFunction . toString ( ) ;
59
- try {
60
- new Function ( '(' + functionText + ')' ) ;
61
- } catch ( e1 ) {
62
- // This means we might have a function shorthand. Try another
63
- // time prefixing 'function '.
64
- if ( functionText . startsWith ( 'async ' ) )
65
- functionText = 'async function ' + functionText . substring ( 'async ' . length ) ;
66
- else
67
- functionText = 'function ' + functionText ;
68
- try {
69
- new Function ( '(' + functionText + ')' ) ;
70
- } catch ( e2 ) {
71
- // We tried hard to serialize, but there's a weird beast here.
72
- throw new Error ( 'Passed function is not well-serializable!' ) ;
58
+ const { functionText, values, handles } = js . prepareFunctionCall < Protocol . Runtime . CallArgument > ( pageFunction , context , args , ( value : any ) => {
59
+ if ( typeof value === 'bigint' ) // eslint-disable-line valid-typeof
60
+ return { handle : { unserializableValue : `${ value . toString ( ) } n` } } ;
61
+ if ( Object . is ( value , - 0 ) )
62
+ return { handle : { unserializableValue : '-0' } } ;
63
+ if ( Object . is ( value , Infinity ) )
64
+ return { handle : { unserializableValue : 'Infinity' } } ;
65
+ if ( Object . is ( value , - Infinity ) )
66
+ return { handle : { unserializableValue : '-Infinity' } } ;
67
+ if ( Object . is ( value , NaN ) )
68
+ return { handle : { unserializableValue : 'NaN' } } ;
69
+ if ( value && ( value instanceof js . JSHandle ) ) {
70
+ const remoteObject = toRemoteObject ( value ) ;
71
+ if ( remoteObject . unserializableValue )
72
+ return { handle : { unserializableValue : remoteObject . unserializableValue } } ;
73
+ if ( ! remoteObject . objectId )
74
+ return { value : remoteObject . value } ;
75
+ return { handle : { objectId : remoteObject . objectId } } ;
73
76
}
74
- }
77
+ return { value } ;
78
+ } ) ;
75
79
76
80
const { exceptionDetails, result : remoteObject } = await this . _client . send ( 'Runtime.callFunctionOn' , {
77
81
functionDeclaration : functionText + '\n' + suffix + '\n' ,
78
82
executionContextId : this . _contextId ,
79
- arguments : args . map ( convertArgument . bind ( this ) ) ,
83
+ arguments : [
84
+ ...values . map ( value => ( { value } ) ) ,
85
+ ...handles ,
86
+ ] ,
80
87
returnByValue,
81
88
awaitPromise : true ,
82
89
userGesture : true
@@ -85,33 +92,6 @@ export class CRExecutionContext implements js.ExecutionContextDelegate {
85
92
throw new Error ( 'Evaluation failed: ' + getExceptionMessage ( exceptionDetails ) ) ;
86
93
return returnByValue ? valueFromRemoteObject ( remoteObject ) : context . _createHandle ( remoteObject ) ;
87
94
88
- function convertArgument ( arg : any ) : any {
89
- if ( typeof arg === 'bigint' ) // eslint-disable-line valid-typeof
90
- return { unserializableValue : `${ arg . toString ( ) } n` } ;
91
- if ( Object . is ( arg , - 0 ) )
92
- return { unserializableValue : '-0' } ;
93
- if ( Object . is ( arg , Infinity ) )
94
- return { unserializableValue : 'Infinity' } ;
95
- if ( Object . is ( arg , - Infinity ) )
96
- return { unserializableValue : '-Infinity' } ;
97
- if ( Object . is ( arg , NaN ) )
98
- return { unserializableValue : 'NaN' } ;
99
- const objectHandle = arg && ( arg instanceof js . JSHandle ) ? arg : null ;
100
- if ( objectHandle ) {
101
- if ( objectHandle . _context !== context )
102
- throw new Error ( 'JSHandles can be evaluated only in the context they were created!' ) ;
103
- if ( objectHandle . _disposed )
104
- throw new Error ( 'JSHandle is disposed!' ) ;
105
- const remoteObject = toRemoteObject ( objectHandle ) ;
106
- if ( remoteObject . unserializableValue )
107
- return { unserializableValue : remoteObject . unserializableValue } ;
108
- if ( ! remoteObject . objectId )
109
- return { value : remoteObject . value } ;
110
- return { objectId : remoteObject . objectId } ;
111
- }
112
- return { value : arg } ;
113
- }
114
-
115
95
function rewriteError ( error : Error ) : Protocol . Runtime . evaluateReturnValue {
116
96
if ( error . message . includes ( 'Object reference chain is too long' ) )
117
97
return { result : { type : 'undefined' } } ;
0 commit comments