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.
|
|
|
|
*/
|
|
|
|
|
2025-04-29 19:07:06 +00:00
|
|
|
import { parseEvaluationResultValue, serializeAsCallArgument } from '@isomorphic/utilityScriptSerializers';
|
2025-03-25 13:49:28 +00:00
|
|
|
|
2025-04-29 19:07:06 +00:00
|
|
|
// --- This section should match javascript.ts and generated_injected_builtins.js ---
|
|
|
|
|
|
|
|
// This flag is replaced by true/false at runtime in all generated sources.
|
|
|
|
const kUtilityScriptIsUnderTest = false;
|
|
|
|
|
2025-05-21 13:45:50 +00:00
|
|
|
// Keep in sync with eslint.config.mjs
|
2025-04-29 19:07:06 +00:00
|
|
|
export type Builtins = {
|
|
|
|
setTimeout: Window['setTimeout'],
|
|
|
|
clearTimeout: Window['clearTimeout'],
|
|
|
|
setInterval: Window['setInterval'],
|
|
|
|
clearInterval: Window['clearInterval'],
|
|
|
|
requestAnimationFrame: Window['requestAnimationFrame'],
|
|
|
|
cancelAnimationFrame: Window['cancelAnimationFrame'],
|
|
|
|
requestIdleCallback: Window['requestIdleCallback'],
|
2025-05-21 13:45:50 +00:00
|
|
|
cancelIdleCallback: Window['cancelIdleCallback'],
|
2025-04-29 19:07:06 +00:00
|
|
|
performance: Window['performance'],
|
|
|
|
// eslint-disable-next-line no-restricted-globals
|
|
|
|
Intl: typeof window['Intl'],
|
|
|
|
// eslint-disable-next-line no-restricted-globals
|
|
|
|
Date: typeof window['Date'],
|
|
|
|
};
|
|
|
|
|
|
|
|
// --- End of the matching section ---
|
|
|
|
|
2022-03-28 22:10:17 -08:00
|
|
|
export class UtilityScript {
|
2025-04-29 19:07:06 +00:00
|
|
|
// eslint-disable-next-line no-restricted-globals
|
|
|
|
readonly global: typeof globalThis;
|
2025-05-21 13:45:50 +00:00
|
|
|
// Builtins protect injected code from clock emulation.
|
2025-04-29 19:07:06 +00:00
|
|
|
readonly builtins: Builtins;
|
|
|
|
readonly isUnderTest: boolean;
|
2025-04-23 21:53:17 -07:00
|
|
|
|
2025-04-29 19:07:06 +00:00
|
|
|
// eslint-disable-next-line no-restricted-globals
|
|
|
|
constructor(global: typeof globalThis) {
|
|
|
|
this.global = global;
|
|
|
|
this.isUnderTest = kUtilityScriptIsUnderTest;
|
2025-05-21 13:45:50 +00:00
|
|
|
if ((global as any).__pwClock) {
|
|
|
|
this.builtins = (global as any).__pwClock.builtins;
|
|
|
|
} else {
|
|
|
|
this.builtins = {
|
|
|
|
setTimeout: global.setTimeout?.bind(global),
|
|
|
|
clearTimeout: global.clearTimeout?.bind(global),
|
|
|
|
setInterval: global.setInterval?.bind(global),
|
|
|
|
clearInterval: global.clearInterval?.bind(global),
|
|
|
|
requestAnimationFrame: global.requestAnimationFrame?.bind(global),
|
|
|
|
cancelAnimationFrame: global.cancelAnimationFrame?.bind(global),
|
|
|
|
requestIdleCallback: global.requestIdleCallback?.bind(global),
|
|
|
|
cancelIdleCallback: global.cancelIdleCallback?.bind(global),
|
|
|
|
performance: global.performance,
|
|
|
|
Intl: global.Intl,
|
|
|
|
Date: global.Date,
|
|
|
|
} satisfies Builtins;
|
|
|
|
}
|
2025-04-29 19:07:06 +00:00
|
|
|
if (this.isUnderTest)
|
|
|
|
(global as any).builtins = this.builtins;
|
2024-05-31 14:44:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
evaluate(isFunction: boolean | undefined, returnByValue: boolean, expression: string, argCount: number, ...argsAndHandles: any[]) {
|
2020-07-19 19:46:19 -07:00
|
|
|
const args = argsAndHandles.slice(0, argCount);
|
|
|
|
const handles = argsAndHandles.slice(argCount);
|
2023-04-21 19:52:13 +02:00
|
|
|
const parameters = [];
|
|
|
|
for (let i = 0; i < args.length; i++)
|
2025-04-29 19:07:06 +00:00
|
|
|
parameters[i] = parseEvaluationResultValue(args[i], handles);
|
2022-11-29 16:57:11 -08:00
|
|
|
|
2025-05-21 13:45:50 +00:00
|
|
|
let result = this.global.eval(expression);
|
2021-02-03 13:49:25 -08:00
|
|
|
if (isFunction === true) {
|
|
|
|
result = result(...parameters);
|
|
|
|
} else if (isFunction === false) {
|
|
|
|
result = result;
|
|
|
|
} else {
|
|
|
|
// auto detect.
|
|
|
|
if (typeof result === 'function')
|
|
|
|
result = result(...parameters);
|
|
|
|
}
|
2020-05-27 17:19:05 -07:00
|
|
|
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.
|
2025-03-25 13:49:28 +00:00
|
|
|
if (value === undefined)
|
2020-06-26 09:50:57 -07:00
|
|
|
return undefined;
|
2025-04-29 19:07:06 +00:00
|
|
|
return serializeAsCallArgument(value, (value: any) => ({ fallThrough: value }));
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:19:05 -07:00
|
|
|
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,
|
2021-02-24 15:11:34 -08:00
|
|
|
// and not some overridden Promise in the page.
|
2020-06-29 16:25:52 -07:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|