-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add get/set prototype handlers and static method for Nexo
- Loading branch information
Showing
7 changed files
with
252 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Nexo from "../Nexo.js"; | ||
import ProxyEvent from "../events/ProxyEvent.js"; | ||
|
||
describe("getPrototypeOf", () => { | ||
it("Emits an event with custom data", () => { | ||
const nexo = new Nexo(); | ||
const proxy = nexo.create(); | ||
const wrapper = Nexo.wrap(proxy); | ||
const listener = jest.fn(); | ||
|
||
nexo.on("proxy.getPrototypeOf", listener); | ||
wrapper.on("proxy.getPrototypeOf", listener); | ||
|
||
const prototype = Reflect.getPrototypeOf(proxy); | ||
|
||
const [event]: [ProxyEvent<{ data: object }>] = listener.mock.lastCall; | ||
|
||
expect(listener).toHaveBeenCalledTimes(2); | ||
expect(prototype).toBeNull(); | ||
expect(event).toBeInstanceOf(ProxyEvent); | ||
expect(event.cancelable).toBe(false); | ||
expect(event.target).toBe(proxy); | ||
expect(event.data).toBe(prototype); | ||
}); | ||
|
||
it("Returns the traceable target prototype", () => { | ||
const nexo = new Nexo(); | ||
const target = []; | ||
const proxy = nexo.create(target); | ||
|
||
const prototype = Reflect.getPrototypeOf(target); | ||
|
||
expect(Reflect.getPrototypeOf(proxy)).toBe(prototype); | ||
expect(Object.getPrototypeOf(proxy)).toBe(prototype); | ||
expect(Nexo.getPrototypeOf(proxy)).toBe(prototype); | ||
}); | ||
|
||
it("Returns null for proxies without a traceable target", () => { | ||
const nexo = new Nexo(); | ||
const proxy = nexo.create(); | ||
|
||
expect(Reflect.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Object.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Nexo.getPrototypeOf(proxy)).toBeNull(); | ||
}); | ||
|
||
it("Returns a custom prototype", () => { | ||
const nexo = new Nexo(); | ||
const proxy = nexo.create(); | ||
|
||
Reflect.setPrototypeOf(proxy, Array.prototype); | ||
|
||
expect(Reflect.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Object.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Nexo.getPrototypeOf(proxy)).toBe(Array.prototype); | ||
}); | ||
|
||
it("Updates the targets current prototype", () => { | ||
const nexo = new Nexo(); | ||
const target = new Map(); | ||
const proxy = nexo.create(target); | ||
|
||
Reflect.setPrototypeOf(proxy, Array.prototype); | ||
|
||
expect(Reflect.getPrototypeOf(proxy)).toBe(Array.prototype); | ||
expect(Object.getPrototypeOf(proxy)).toBe(Array.prototype); | ||
expect(Nexo.getPrototypeOf(proxy)).toBe(Array.prototype); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
import type nx from "../types/Nexo.js"; | ||
import isTraceable from "../utils/isTraceable.js"; | ||
import ProxyEvent from "../events/ProxyEvent.js"; | ||
import map from "../utils/maps.js"; | ||
import Nexo from "../Nexo.js"; | ||
|
||
const getPrototypeOf = (target: nx.traceable): object => { | ||
const proxy = map.tracables.get(target); | ||
const { sandbox } = Nexo.wrap(proxy); | ||
const prototype = Reflect.getPrototypeOf(target); | ||
const proto = sandbox ? Reflect.getPrototypeOf(sandbox) : prototype; | ||
|
||
new ProxyEvent("getPrototypeOf", { target: proxy }); | ||
new ProxyEvent("getPrototypeOf", { target: proxy, data: proto }); | ||
|
||
try { | ||
if (isTraceable(target)) { | ||
return Object.getPrototypeOf(target); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
} catch (error) { | ||
// empty | ||
} | ||
|
||
return null; | ||
return prototype; | ||
}; | ||
|
||
export default getPrototypeOf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import Nexo from "../Nexo.js"; | ||
import ProxyError from "../errors/ProxyError.js"; | ||
import ProxyEvent from "../events/ProxyEvent.js"; | ||
|
||
describe("setPrototypeOf", () => { | ||
it("Emits an event with custom data", () => { | ||
const nexo = new Nexo(); | ||
const proxy = nexo.create(); | ||
const wrapper = Nexo.wrap(proxy); | ||
const listener = jest.fn(); | ||
|
||
nexo.on("proxy.setPrototypeOf", listener); | ||
wrapper.on("proxy.setPrototypeOf", listener); | ||
|
||
Reflect.setPrototypeOf(proxy, Array.prototype); | ||
const [event]: [ProxyEvent<{ prototype: object }>] = listener.mock.lastCall; | ||
|
||
expect(listener).toHaveBeenCalledTimes(2); | ||
expect(event).toBeInstanceOf(ProxyEvent); | ||
expect(event.cancelable).toBe(true); | ||
expect(event.target).toBe(proxy); | ||
expect(event.data.prototype).toBe(Array.prototype); | ||
}); | ||
|
||
it("Can prevent the default event behavior", () => { | ||
const nexo = new Nexo(); | ||
const proxy = nexo.create(); | ||
|
||
nexo.on("proxy.setPrototypeOf", (event: ProxyEvent) => { | ||
event.preventDefault(); | ||
return; | ||
}); | ||
|
||
Reflect.setPrototypeOf(proxy, Array.prototype); | ||
|
||
expect(Reflect.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Object.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Nexo.getPrototypeOf(proxy)).toBeNull(); | ||
}); | ||
|
||
it("Cannot replace the prototype with a non-object", () => { | ||
const nexo = new Nexo(); | ||
const proxy = nexo.create(); | ||
|
||
nexo.on("proxy.setPrototypeOf", (event: ProxyEvent) => { | ||
event.preventDefault(); | ||
return "non-object"; | ||
}); | ||
|
||
const setPrototypeOf = Reflect.setPrototypeOf.bind( | ||
null, | ||
proxy, | ||
Array.prototype, | ||
); | ||
|
||
expect(setPrototypeOf).toThrow(ProxyError); | ||
expect(Reflect.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Object.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Nexo.getPrototypeOf(proxy)).toBeNull(); | ||
}); | ||
|
||
it("Can set a new prototype on a proxy without traceable target", () => { | ||
const nexo = new Nexo(); | ||
const proxy = nexo.create(); | ||
|
||
nexo.on("proxy.setPrototypeOf", (event: ProxyEvent) => { | ||
event.preventDefault(); | ||
return Array.prototype; | ||
}); | ||
|
||
Reflect.setPrototypeOf(proxy, null); | ||
|
||
expect(Reflect.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Object.getPrototypeOf(proxy)).toBeNull(); | ||
expect(Nexo.getPrototypeOf(proxy)).toBe(Array.prototype); | ||
}); | ||
|
||
it("Can set a new prototype on a proxy with traceable target", () => { | ||
const nexo = new Nexo(); | ||
const target = {}; | ||
const proxy = nexo.create(target); | ||
|
||
nexo.on("proxy.setPrototypeOf", (event: ProxyEvent) => { | ||
event.preventDefault(); | ||
return Array.prototype; | ||
}); | ||
|
||
Reflect.setPrototypeOf(proxy, null); | ||
|
||
expect(Reflect.getPrototypeOf(proxy)).toBe(Array.prototype); | ||
expect(Reflect.getPrototypeOf(target)).toBe(Array.prototype); | ||
expect(Object.getPrototypeOf(proxy)).toBe(Array.prototype); | ||
expect(Object.getPrototypeOf(target)).toBe(Array.prototype); | ||
expect(Nexo.getPrototypeOf(proxy)).toBe(Array.prototype); | ||
}); | ||
|
||
it("Throws an error when the traceable target is not extensible", () => { | ||
const nexo = new Nexo(); | ||
const proxy = nexo.create(); | ||
|
||
Reflect.preventExtensions(proxy); | ||
|
||
const setNewPrototype = Reflect.setPrototypeOf.bind( | ||
null, | ||
proxy, | ||
Array.prototype, | ||
); | ||
|
||
const setSamePrototype = Reflect.setPrototypeOf.bind( | ||
null, | ||
proxy, | ||
Nexo.getPrototypeOf(proxy), | ||
); | ||
|
||
expect(setNewPrototype).toThrow(ProxyError); | ||
expect(setSamePrototype).not.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters