/* * Copyright (C) 2021-2022 Kristóf Marussy * * This file is part of Sophie. * * Sophie is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * * SPDX-License-Identifier: AGPL-3.0-only */ /** * Simulates a function defined in native code, i.e., one with * `[native code]` in its `toString`. * * @param name The name of the function. * @param f The function to transform. * @return The transformed function. */ export function simulateNativeFunction( name: string, f: (this: null, ...args: P) => T, ): (...args: P) => T { // Bound functions say `[native code]`, but unfortunately they omit the function name: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString#description // The type of `f` contains type variables, so we need some magic type casting. const boundFunc = f.bind(null as ThisParameterType); Object.defineProperty(boundFunc, 'name', { configurable: true, enumerable: false, value: name, writable: false, }); return boundFunc; } /** * Simulates a native class available on `globalThis`. * * @param name The name of the class. * @param constructor The constructor function. Must already be a constructor (a named `function`). * @param properties The properties to define on the prototype. */ export function simulateNativeClass( name: string, constructor: () => void, properties: PropertyDescriptorMap, ) { Object.defineProperties(constructor.prototype, { [Symbol.toStringTag]: { configurable: true, enumerable: false, value: name, writable: false, }, ...properties, }); const simulatedConstructor = simulateNativeFunction(name, constructor); Object.defineProperty(globalThis, name, { configurable: true, enumerable: true, value: simulatedConstructor, writable: true, }); return simulatedConstructor; } /** * Defines a property on the prototype of an object. * * Only use this with singleton objects, e.g., `window.navigator`. * * @param o The object to modify. Must be a singleton. * @param property The key of the property being defined or modified. * @param attributes The descriptor of the property being defined or modified. */ export function defineProtoProperty( o: object, property: PropertyKey, attributes: PropertyDescriptor, ): void { Object.defineProperty(Object.getPrototypeOf(o), property, attributes); } /** * Deletes a property from the prototype of an object. * * Only use this with singleton objects, e.g., `window.navigator`. * * @param o The object to modify. Must be a singleton. * @param property The key of the property being deleted. */ export function deleteProtoProperty(o: object, property: PropertyKey): void { Reflect.deleteProperty(Object.getPrototypeOf(o), property); }