diff --git a/src/events/handlers/defineProperty.ts b/src/events/handlers/defineProperty.ts index c223ee8..b885e66 100644 --- a/src/events/handlers/defineProperty.ts +++ b/src/events/handlers/defineProperty.ts @@ -2,17 +2,10 @@ import Nexo from "../../types/Nexo.js"; import { isTraceable, map } from "../../utils/index.js"; import ProxyEvent from "../ProxyEvent.js"; -type descriptor = { - enumerable?: boolean; - writable?: boolean; - configurable?: boolean; - value?: unknown; -}; - const defineProperty = ( mock: Nexo.Mock, key: Nexo.objectKey, - descriptor: descriptor, + descriptor: PropertyDescriptor, ): boolean => { const proxy = map.tracables.get(mock); const data = map.proxies.get(proxy); diff --git a/src/events/handlers/getOwnPropertyDescriptor.ts b/src/events/handlers/getOwnPropertyDescriptor.ts new file mode 100644 index 0000000..33bb156 --- /dev/null +++ b/src/events/handlers/getOwnPropertyDescriptor.ts @@ -0,0 +1,35 @@ +import Nexo from "../../types/Nexo.js"; +import { getTarget, map } from "../../utils/index.js"; +import ProxyEvent from "../ProxyEvent.js"; + +const getOwnPropertyDescriptor = ( + mock: Nexo.Mock, + key: Nexo.objectKey, +): PropertyDescriptor => { + const proxy = map.tracables.get(mock); + const data = map.proxies.get(proxy); + + const { sandbox } = data; + const scope = data.scope.deref(); + const value = getTarget(sandbox.get(key), true); + + const event = new ProxyEvent("handler.getOwnPropertyDescriptor", { + proxy, + key, + }); + + scope.emit(event.name, event); + + if (event.defaultPrevented) { + return event.returnValue; + } + + return { + configurable: false, + enumerable: true, + writable: false, + value, + }; +}; + +export default getOwnPropertyDescriptor; diff --git a/src/events/handlers/index.ts b/src/events/handlers/index.ts index c8a2363..2875f38 100644 --- a/src/events/handlers/index.ts +++ b/src/events/handlers/index.ts @@ -2,7 +2,16 @@ import get from "./get.js"; import set from "./set.js"; import defineProperty from "./defineProperty.js"; import deleteProperty from "./deleteProperty.js"; +import getOwnPropertyDescriptor from "./getOwnPropertyDescriptor.js"; import construct from "./construct.js"; import apply from "./apply.js"; -export default { get, set, defineProperty, deleteProperty, construct, apply }; +export default { + get, + set, + defineProperty, + deleteProperty, + getOwnPropertyDescriptor, + construct, + apply, +}; diff --git a/src/types/Nexo.ts b/src/types/Nexo.ts index c018b0b..4964b4a 100644 --- a/src/types/Nexo.ts +++ b/src/types/Nexo.ts @@ -37,6 +37,7 @@ declare namespace Nexo { | "handler.set" | "handler.defineProperty" | "handler.deleteProperty" + | "handler.getOwnPropertyDescriptor" | "handler.apply" | "handler.construct"; @@ -61,6 +62,11 @@ declare namespace Nexo { key: objectKey; } + interface getOwnPropertyDescriptorHandler extends ProxyEvent { + name: "handler.getOwnPropertyDescriptor"; + key: objectKey; + } + interface applyHandler extends ProxyEvent { name: "handler.apply"; args: unknown[];