basant307/AI_Governance_Project
048
1const copyProperty = (to, from, property, ignoreNonConfigurable) => {2 // `Function#length` should reflect the parameters of `to` not `from` since we keep its body.3 // `Function#prototype` is non-writable and non-configurable so can never be modified.4 if (property === 'length' || property === 'prototype') {5 return;6 }7 8 // `Function#arguments` and `Function#caller` should not be copied. They were reported to be present in `Reflect.ownKeys` for some devices in React Native (#41), so we explicitly ignore them here.9 if (property === 'arguments' || property === 'caller') {10 return;11 }12 13 const toDescriptor = Object.getOwnPropertyDescriptor(to, property);14 const fromDescriptor = Object.getOwnPropertyDescriptor(from, property);15 16 if (!canCopyProperty(toDescriptor, fromDescriptor) && ignoreNonConfigurable) {17 return;18 }19 20 Object.defineProperty(to, property, fromDescriptor);21};22 23// `Object.defineProperty()` throws if the property exists, is not configurable and either:24// - one its descriptors is changed25// - it is non-writable and its value is changed26const canCopyProperty = function (toDescriptor, fromDescriptor) {27 return toDescriptor === undefined || toDescriptor.configurable || (28 toDescriptor.writable === fromDescriptor.writable29 && toDescriptor.enumerable === fromDescriptor.enumerable30 && toDescriptor.configurable === fromDescriptor.configurable31 && (toDescriptor.writable || toDescriptor.value === fromDescriptor.value)32 );33};34 35const changePrototype = (to, from) => {36 const fromPrototype = Object.getPrototypeOf(from);37 if (fromPrototype === Object.getPrototypeOf(to)) {38 return;39 }40 41 Object.setPrototypeOf(to, fromPrototype);42};43 44const wrappedToString = (withName, fromBody) => `/* Wrapped ${withName}*/\n${fromBody}`;45 46const toStringDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, 'toString');47const toStringName = Object.getOwnPropertyDescriptor(Function.prototype.toString, 'name');48 49// We call `from.toString()` early (not lazily) to ensure `from` can be garbage collected.50// We use `bind()` instead of a closure for the same reason.51// Calling `from.toString()` early also allows caching it in case `to.toString()` is called several times.52const changeToString = (to, from, name) => {53 const withName = name === '' ? '' : `with ${name.trim()}() `;54 const newToString = wrappedToString.bind(null, withName, from.toString());55 // Ensure `to.toString.toString` is non-enumerable and has the same `same`56 Object.defineProperty(newToString, 'name', toStringName);57 const {writable, enumerable, configurable} = toStringDescriptor; // We destructue to avoid a potential `get` descriptor.58 Object.defineProperty(to, 'toString', {value: newToString, writable, enumerable, configurable});59};60 61export default function mimicFunction(to, from, {ignoreNonConfigurable = false} = {}) {62 const {name} = to;63 64 for (const property of Reflect.ownKeys(from)) {65 copyProperty(to, from, property, ignoreNonConfigurable);66 }67 68 changePrototype(to, from);69 changeToString(to, from, name);70 71 return to;72}73 