@@ -45,16 +45,15 @@ class ExecutionContext {
4545 * @return {!Promise<(!Object|undefined)> }
4646 */
4747 async evaluate ( pageFunction , ...args ) {
48- const handle = await this . evaluateHandle ( pageFunction , ...args ) ;
49- const result = await handle . jsonValue ( ) . catch ( error => {
48+ try {
49+ return await this . _evaluateInternal ( true /* returnByValue */ , pageFunction , ...args ) ;
50+ } catch ( error ) {
5051 if ( error . message . includes ( 'Object reference chain is too long' ) )
5152 return ;
5253 if ( error . message . includes ( 'Object couldn\'t be returned by value' ) )
5354 return ;
5455 throw error ;
55- } ) ;
56- await handle . dispose ( ) ;
57- return result ;
56+ }
5857 }
5958
6059 /**
@@ -63,6 +62,15 @@ class ExecutionContext {
6362 * @return {!Promise<!JSHandle> }
6463 */
6564 async evaluateHandle ( pageFunction , ...args ) {
65+ return this . _evaluateInternal ( false /* returnByValue */ , pageFunction , ...args ) ;
66+ }
67+
68+ /**
69+ * @param {Function|string } pageFunction
70+ * @param {...* } args
71+ * @return {!Promise<!JSHandle> }
72+ */
73+ async _evaluateInternal ( returnByValue , pageFunction , ...args ) {
6674 const suffix = `//# sourceURL=${ EVALUATION_SCRIPT_URL } ` ;
6775
6876 if ( helper . isString ( pageFunction ) ) {
@@ -72,13 +80,13 @@ class ExecutionContext {
7280 const { exceptionDetails, result : remoteObject } = await this . _client . send ( 'Runtime.evaluate' , {
7381 expression : expressionWithSourceUrl ,
7482 contextId,
75- returnByValue : false ,
83+ returnByValue,
7684 awaitPromise : true ,
7785 userGesture : true
7886 } ) . catch ( rewriteError ) ;
7987 if ( exceptionDetails )
8088 throw new Error ( 'Evaluation failed: ' + helper . getExceptionMessage ( exceptionDetails ) ) ;
81- return createJSHandle ( this , remoteObject ) ;
89+ return returnByValue ? helper . valueFromRemoteObject ( remoteObject ) : createJSHandle ( this , remoteObject ) ;
8290 }
8391
8492 if ( typeof pageFunction !== 'function' )
@@ -107,7 +115,7 @@ class ExecutionContext {
107115 functionDeclaration : functionText + '\n' + suffix + '\n' ,
108116 executionContextId : this . _contextId ,
109117 arguments : args . map ( convertArgument . bind ( this ) ) ,
110- returnByValue : false ,
118+ returnByValue,
111119 awaitPromise : true ,
112120 userGesture : true
113121 } ) ;
@@ -119,7 +127,7 @@ class ExecutionContext {
119127 const { exceptionDetails, result : remoteObject } = await callFunctionOnPromise . catch ( rewriteError ) ;
120128 if ( exceptionDetails )
121129 throw new Error ( 'Evaluation failed: ' + helper . getExceptionMessage ( exceptionDetails ) ) ;
122- return createJSHandle ( this , remoteObject ) ;
130+ return returnByValue ? helper . valueFromRemoteObject ( remoteObject ) : createJSHandle ( this , remoteObject ) ;
123131
124132 /**
125133 * @param {* } arg
0 commit comments