17
17
18
18
import { CRSession } from './crConnection' ;
19
19
import { helper } from '../helper' ;
20
- import { valueFromRemoteObject , getExceptionMessage , releaseObject } from './crProtocolHelper' ;
20
+ import { getExceptionMessage , releaseObject } from './crProtocolHelper' ;
21
21
import { Protocol } from './protocol' ;
22
22
import * as js from '../javascript' ;
23
23
import * as debugSupport from '../debug/debugSupport' ;
24
+ import { RemoteObject , parseEvaluationResultValue } from '../remoteObject' ;
24
25
25
26
export class CRExecutionContext implements js . ExecutionContextDelegate {
26
27
_client : CRSession ;
@@ -31,7 +32,7 @@ export class CRExecutionContext implements js.ExecutionContextDelegate {
31
32
this . _contextId = contextPayload . id ;
32
33
}
33
34
34
- async rawEvaluate ( expression : string ) : Promise < js . RemoteObject > {
35
+ async rawEvaluate ( expression : string ) : Promise < RemoteObject > {
35
36
const { exceptionDetails, result : remoteObject } = await this . _client . send ( 'Runtime.evaluate' , {
36
37
expression : debugSupport . ensureSourceUrl ( expression ) ,
37
38
contextId : this . _contextId ,
@@ -51,29 +52,7 @@ export class CRExecutionContext implements js.ExecutionContextDelegate {
51
52
52
53
if ( typeof pageFunction !== 'function' )
53
54
throw new Error ( `Expected to get |string| or |function| as the first argument, but got "${ pageFunction } " instead.` ) ;
54
-
55
- const { functionText, values, handles, dispose } = await js . prepareFunctionCall < Protocol . Runtime . CallArgument > ( pageFunction , context , args , ( value : any ) => {
56
- if ( typeof value === 'bigint' ) // eslint-disable-line valid-typeof
57
- return { handle : { unserializableValue : `${ value . toString ( ) } n` } } ;
58
- if ( Object . is ( value , - 0 ) )
59
- return { handle : { unserializableValue : '-0' } } ;
60
- if ( Object . is ( value , Infinity ) )
61
- return { handle : { unserializableValue : 'Infinity' } } ;
62
- if ( Object . is ( value , - Infinity ) )
63
- return { handle : { unserializableValue : '-Infinity' } } ;
64
- if ( Object . is ( value , NaN ) )
65
- return { handle : { unserializableValue : 'NaN' } } ;
66
- if ( value && ( value instanceof js . JSHandle ) ) {
67
- const remoteObject = toRemoteObject ( value ) ;
68
- if ( remoteObject . unserializableValue )
69
- return { handle : { unserializableValue : remoteObject . unserializableValue } } ;
70
- if ( ! remoteObject . objectId )
71
- return { handle : { value : remoteObject . value } } ;
72
- return { handle : { objectId : remoteObject . objectId } } ;
73
- }
74
- return { value } ;
75
- } ) ;
76
-
55
+ const { functionText, values, handles, dispose } = await js . prepareFunctionCall ( pageFunction , context , args ) ;
77
56
return this . _callOnUtilityScript ( context ,
78
57
'callFunction' , [
79
58
{ value : functionText } ,
@@ -87,7 +66,7 @@ export class CRExecutionContext implements js.ExecutionContextDelegate {
87
66
const utilityScript = await context . utilityScript ( ) ;
88
67
const { exceptionDetails, result : remoteObject } = await this . _client . send ( 'Runtime.callFunctionOn' , {
89
68
functionDeclaration : `function (...args) { return this.${ method } (...args) }` + debugSupport . generateSourceUrl ( ) ,
90
- objectId : utilityScript . _remoteObject . objectId ,
69
+ objectId : utilityScript . _objectId ,
91
70
arguments : [
92
71
{ value : returnByValue } ,
93
72
...args
@@ -98,14 +77,14 @@ export class CRExecutionContext implements js.ExecutionContextDelegate {
98
77
} ) . catch ( rewriteError ) ;
99
78
if ( exceptionDetails )
100
79
throw new Error ( 'Evaluation failed: ' + getExceptionMessage ( exceptionDetails ) ) ;
101
- return returnByValue ? valueFromRemoteObject ( remoteObject ) : context . createHandle ( remoteObject ) ;
80
+ return returnByValue ? parseEvaluationResultValue ( remoteObject . value ) : context . createHandle ( remoteObject ) ;
102
81
} finally {
103
82
dispose ( ) ;
104
83
}
105
84
}
106
85
107
86
async getProperties ( handle : js . JSHandle ) : Promise < Map < string , js . JSHandle > > {
108
- const objectId = toRemoteObject ( handle ) . objectId ;
87
+ const objectId = handle . _objectId ;
109
88
if ( ! objectId )
110
89
return new Map ( ) ;
111
90
const response = await this . _client . send ( 'Runtime.getProperties' , {
@@ -114,43 +93,28 @@ export class CRExecutionContext implements js.ExecutionContextDelegate {
114
93
} ) ;
115
94
const result = new Map ( ) ;
116
95
for ( const property of response . result ) {
117
- if ( ! property . enumerable )
96
+ if ( ! property . enumerable || ! property . value )
118
97
continue ;
119
98
result . set ( property . name , handle . _context . createHandle ( property . value ) ) ;
120
99
}
121
100
return result ;
122
101
}
123
102
124
103
async releaseHandle ( handle : js . JSHandle ) : Promise < void > {
125
- await releaseObject ( this . _client , toRemoteObject ( handle ) ) ;
104
+ if ( ! handle . _objectId )
105
+ return ;
106
+ await releaseObject ( this . _client , handle . _objectId ) ;
126
107
}
127
108
128
109
async handleJSONValue < T > ( handle : js . JSHandle < T > ) : Promise < T > {
129
- const remoteObject = toRemoteObject ( handle ) ;
130
- if ( remoteObject . objectId ) {
131
- const response = await this . _client . send ( 'Runtime.callFunctionOn' , {
132
- functionDeclaration : 'function() { return this; }' + debugSupport . generateSourceUrl ( ) ,
133
- objectId : remoteObject . objectId ,
134
- returnByValue : true ,
135
- awaitPromise : true ,
136
- } ) ;
137
- return valueFromRemoteObject ( response . result ) ;
110
+ if ( handle . _objectId ) {
111
+ return this . _callOnUtilityScript ( handle . _context ,
112
+ `jsonValue` , [
113
+ { objectId : handle . _objectId } ,
114
+ ] , true , ( ) => { } ) ;
138
115
}
139
- return valueFromRemoteObject ( remoteObject ) ;
116
+ return handle . _value ;
140
117
}
141
-
142
- handleToString ( handle : js . JSHandle , includeType : boolean ) : string {
143
- const object = toRemoteObject ( handle ) ;
144
- if ( object . objectId ) {
145
- const type = object . subtype || object . type ;
146
- return 'JSHandle@' + type ;
147
- }
148
- return ( includeType ? 'JSHandle:' : '' ) + valueFromRemoteObject ( object ) ;
149
- }
150
- }
151
-
152
- function toRemoteObject ( handle : js . JSHandle ) : Protocol . Runtime . RemoteObject {
153
- return handle . _remoteObject as Protocol . Runtime . RemoteObject ;
154
118
}
155
119
156
120
function rewriteError ( error : Error ) : Protocol . Runtime . evaluateReturnValue {
0 commit comments