MCPcopy Index your code
hub / github.com/ClearURLs/Addon / wrapObject

Function wrapObject

browser-polyfill.js:909–994  ·  view source on GitHub ↗
(target, wrappers = {}, metadata = {})

Source from the content-addressed store, hash-verified

907 * @returns {Proxy<object>}
908 */
909 const wrapObject = (target, wrappers = {}, metadata = {}) => {
910 let cache = Object.create(null);
911 let handlers = {
912 has(proxyTarget, prop) {
913 return prop in target || prop in cache;
914 },
915 get(proxyTarget, prop, receiver) {
916 if (prop in cache) {
917 return cache[prop];
918 }
919 if (!(prop in target)) {
920 return undefined;
921 }
922 let value = target[prop];
923 if (typeof value === "function") {
924 // This is a method on the underlying object. Check if we need to do
925 // any wrapping.
926
927 if (typeof wrappers[prop] === "function") {
928 // We have a special-case wrapper for this method.
929 value = wrapMethod(target, target[prop], wrappers[prop]);
930 } else if (hasOwnProperty(metadata, prop)) {
931 // This is an async method that we have metadata for. Create a
932 // Promise wrapper for it.
933 let wrapper = wrapAsyncFunction(prop, metadata[prop]);
934 value = wrapMethod(target, target[prop], wrapper);
935 } else {
936 // This is a method that we don't know or care about. Return the
937 // original method, bound to the underlying object.
938 value = value.bind(target);
939 }
940 } else if (typeof value === "object" && value !== null && (hasOwnProperty(wrappers, prop) || hasOwnProperty(metadata, prop))) {
941 // This is an object that we need to do some wrapping for the children
942 // of. Create a sub-object wrapper for it with the appropriate child
943 // metadata.
944 value = wrapObject(value, wrappers[prop], metadata[prop]);
945 } else if (hasOwnProperty(metadata, "*")) {
946 // Wrap all properties in * namespace.
947 value = wrapObject(value, wrappers[prop], metadata["*"]);
948 } else {
949 // We don't need to do any wrapping for this property,
950 // so just forward all access to the underlying object.
951 Object.defineProperty(cache, prop, {
952 configurable: true,
953 enumerable: true,
954 get() {
955 return target[prop];
956 },
957 set(value) {
958 target[prop] = value;
959 }
960 });
961 return value;
962 }
963 cache[prop] = value;
964 return value;
965 },
966 set(proxyTarget, prop, value, receiver) {

Callers 2

getFunction · 0.85
wrapAPIsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected