2020-05-20 15:55:33 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-06-11 18:18:33 -07:00
|
|
|
import { serializeAsCallArgument, parseEvaluationResultValue } from '../common/utilityScriptSerializers';
|
2020-05-27 17:19:05 -07:00
|
|
|
|
2020-05-20 15:55:33 -07:00
|
|
|
export default class UtilityScript {
|
2020-05-21 16:00:55 -07:00
|
|
|
evaluate(returnByValue: boolean, expression: string) {
|
|
|
|
const result = global.eval(expression);
|
2020-05-27 17:19:05 -07:00
|
|
|
return returnByValue ? this._promiseAwareJsonValueNoThrow(result) : result;
|
2020-05-21 16:00:55 -07:00
|
|
|
}
|
|
|
|
|
2020-07-19 19:46:19 -07:00
|
|
|
callFunction(returnByValue: boolean, functionText: string, argCount: number, ...argsAndHandles: any[]) {
|
|
|
|
const args = argsAndHandles.slice(0, argCount);
|
|
|
|
const handles = argsAndHandles.slice(argCount);
|
|
|
|
const parameters = args.map(a => parseEvaluationResultValue(a, handles));
|
2020-05-20 15:55:33 -07:00
|
|
|
const func = global.eval('(' + functionText + ')');
|
2020-05-27 17:19:05 -07:00
|
|
|
const result = func(...parameters);
|
|
|
|
return returnByValue ? this._promiseAwareJsonValueNoThrow(result) : result;
|
2020-05-21 16:00:55 -07:00
|
|
|
}
|
|
|
|
|
2020-05-27 17:19:05 -07:00
|
|
|
jsonValue(returnByValue: true, value: any) {
|
2020-06-26 09:50:57 -07:00
|
|
|
// Special handling of undefined to work-around multi-step returnByValue handling in WebKit.
|
|
|
|
if (Object.is(value, undefined))
|
|
|
|
return undefined;
|
2020-05-27 17:19:05 -07:00
|
|
|
return serializeAsCallArgument(value, (value: any) => ({ fallThrough: value }));
|
|
|
|
}
|
|
|
|
|
|
|
|
private _promiseAwareJsonValueNoThrow(value: any) {
|
|
|
|
const safeJson = (value: any) => {
|
|
|
|
try {
|
|
|
|
return this.jsonValue(true, value);
|
|
|
|
} catch (e) {
|
|
|
|
return undefined;
|
2020-05-21 16:00:55 -07:00
|
|
|
}
|
2020-05-27 17:19:05 -07:00
|
|
|
};
|
|
|
|
|
2020-06-29 16:25:52 -07:00
|
|
|
if (value && typeof value === 'object' && typeof value.then === 'function') {
|
|
|
|
return (async () => {
|
|
|
|
// By using async function we ensure that return value is a native Promise,
|
|
|
|
// and not some overriden Promise in the page.
|
|
|
|
// This makes Firefox and WebKit debugging protocols recognize it as a Promise,
|
|
|
|
// properly await and return the value.
|
|
|
|
const promiseValue = await value;
|
|
|
|
return safeJson(promiseValue);
|
|
|
|
})();
|
|
|
|
}
|
2020-05-27 17:19:05 -07:00
|
|
|
return safeJson(value);
|
2020-05-20 15:55:33 -07:00
|
|
|
}
|
|
|
|
}
|