@@ -55,8 +55,8 @@ export class WKExecutionContext implements js.ExecutionContextDelegate {
55
55
56
56
async evaluate ( context : js . ExecutionContext , returnByValue : boolean , pageFunction : Function | string , ...args : any [ ] ) : Promise < any > {
57
57
try {
58
- let response = await this . _evaluateRemoteObject ( context , pageFunction , args ) ;
59
- if ( response . result . type === 'object' && response . result . className === 'Promise' ) {
58
+ let response = await this . _evaluateRemoteObject ( context , pageFunction , args , returnByValue ) ;
59
+ if ( response . result . objectId && response . result . className === 'Promise' ) {
60
60
response = await Promise . race ( [
61
61
this . _executionContextDestroyedPromise . then ( ( ) => contextDestroyedResult ) ,
62
62
this . _session . send ( 'Runtime.awaitPromise' , {
@@ -79,14 +79,15 @@ export class WKExecutionContext implements js.ExecutionContextDelegate {
79
79
}
80
80
}
81
81
82
- private async _evaluateRemoteObject ( context : js . ExecutionContext , pageFunction : Function | string , args : any [ ] ) : Promise < any > {
82
+ private async _evaluateRemoteObject ( context : js . ExecutionContext , pageFunction : Function | string , args : any [ ] , returnByValue : boolean ) : Promise < Protocol . Runtime . callFunctionOnReturnValue > {
83
83
if ( helper . isString ( pageFunction ) ) {
84
- const contextId = this . _contextId ;
85
- const expression : string = pageFunction ;
86
- return await this . _session . send ( 'Runtime.evaluate' , {
87
- expression : js . ensureSourceUrl ( expression ) ,
88
- contextId,
89
- returnByValue : false ,
84
+ const utilityScript = await context . utilityScript ( ) ;
85
+ const functionDeclaration = `function (returnByValue, pageFunction) { return this.evaluate(returnByValue, pageFunction); }${ js . generateSourceUrl ( ) } ` ;
86
+ return await this . _session . send ( 'Runtime.callFunctionOn' , {
87
+ functionDeclaration,
88
+ objectId : utilityScript . _remoteObject . objectId ! ,
89
+ arguments : [ { value : returnByValue } , { value : pageFunction } ] ,
90
+ returnByValue : false , // We need to return real Promise if that is a promise.
90
91
emulateUserGesture : true
91
92
} ) ;
92
93
}
@@ -110,22 +111,22 @@ export class WKExecutionContext implements js.ExecutionContextDelegate {
110
111
111
112
try {
112
113
const utilityScript = await context . utilityScript ( ) ;
113
- const callParams = this . _serializeFunctionAndArguments ( functionText , values , handles ) ;
114
+ const callParams = this . _serializeFunctionAndArguments ( functionText , values , handles , returnByValue ) ;
114
115
return await this . _session . send ( 'Runtime.callFunctionOn' , {
115
116
functionDeclaration : callParams . functionText ,
116
117
objectId : utilityScript . _remoteObject . objectId ! ,
117
- arguments : [ ... callParams . callArguments ] ,
118
- returnByValue : false ,
118
+ arguments : callParams . callArguments ,
119
+ returnByValue : false , // We need to return real Promise if that is a promise.
119
120
emulateUserGesture : true
120
121
} ) ;
121
122
} finally {
122
123
dispose ( ) ;
123
124
}
124
125
}
125
126
126
- private _serializeFunctionAndArguments ( originalText : string , values : any [ ] , handles : MaybeCallArgument [ ] ) : { functionText : string , callArguments : Protocol . Runtime . CallArgument [ ] } {
127
+ private _serializeFunctionAndArguments ( originalText : string , values : any [ ] , handles : MaybeCallArgument [ ] , returnByValue : boolean ) : { functionText : string , callArguments : Protocol . Runtime . CallArgument [ ] } {
127
128
const callArguments : Protocol . Runtime . CallArgument [ ] = values . map ( value => ( { value } ) ) ;
128
- let functionText = `function (functionText, ...args) { return this.evaluate( functionText, ...args); }${ js . generateSourceUrl ( ) } ` ;
129
+ let functionText = `function (returnByValue, functionText, ...args) { return this.callFunction(returnByValue, functionText, ...args); }${ js . generateSourceUrl ( ) } ` ;
129
130
if ( handles . some ( handle => 'unserializable' in handle ) ) {
130
131
const paramStrings = [ ] ;
131
132
for ( let i = 0 ; i < callArguments . length ; i ++ )
@@ -138,11 +139,11 @@ export class WKExecutionContext implements js.ExecutionContextDelegate {
138
139
callArguments . push ( handle ) ;
139
140
}
140
141
}
141
- functionText = `function (functionText, ...a) { return this.evaluate( functionText, ${ paramStrings . join ( ',' ) } ); }${ js . generateSourceUrl ( ) } ` ;
142
+ functionText = `function (returnByValue, functionText, ...a) { return this.callFunction(returnByValue, functionText, ${ paramStrings . join ( ',' ) } ); }${ js . generateSourceUrl ( ) } ` ;
142
143
} else {
143
144
callArguments . push ( ...( handles as Protocol . Runtime . CallArgument [ ] ) ) ;
144
145
}
145
- return { functionText, callArguments : [ { value : originalText } , ...callArguments ] } ;
146
+ return { functionText, callArguments : [ { value : returnByValue } , { value : originalText } , ...callArguments ] } ;
146
147
147
148
function unserializableToString ( arg : any ) {
148
149
if ( Object . is ( arg , - 0 ) )
0 commit comments