Skip to content

Commit

Permalink
fix: ajusat lib
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Sep 4, 2023
1 parent c1d5e9c commit 503f6df
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 73 deletions.
5 changes: 2 additions & 3 deletions src/lib/constructor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ describe("(constructor)", () => {
const config: Exotic.emulator.options = constants.CONFIG;
const emulator = new Emulator(config);
const data: Record<string, any> = map.emulators.get(emulator) || {};
const dataProps = ["options", "refs", "links", "counter", "proxySet"];
const { options, refs, links, counter } = data;
const dataProps = ["options", "links", "counter", "proxySet"];
const { options, links, counter } = data;

expect(map.emulators.has(emulator)).toBe(true);
expect(Object.keys(data)).toEqual(dataProps);
expect(options).toEqual(config);
expect(Object.keys(refs).length).toBe(0);
expect(Object.keys(links).length).toBe(0);
expect(Error.stackTraceLimit).toBe(config.stackTraceLimit);
expect(counter).toBe(0);
Expand Down
1 change: 0 additions & 1 deletion src/lib/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default function constructor(

const data: Exotic.emulator.data = {
options: config,
refs: Object.create(null),
links: Object.create(null),
counter: 0,
proxySet: new Set(),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/getters/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Exotic from "../../types/Exotic.js";
import { map } from "../../utils/index.js";

export default function links(scope: Exotic.Emulator): Exotic.key[] {
const { refs }: Exotic.emulator.data = map.emulators.get(scope);
return Object.keys(refs);
const { links }: Exotic.emulator.data = map.emulators.get(scope);
return Object.keys(links);
}
26 changes: 6 additions & 20 deletions src/lib/methods/include.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import Exotic from "../../types/Exotic.js";
import {
map,
traps,
createProxy,
decode,
encode,
findProxy,
} from "../../utils/index.js";
import { map, traps, createProxy, decode, encode } from "../../utils/index.js";

export default function include(
scope: Exotic.Emulator,
Expand All @@ -15,30 +8,24 @@ export default function include(
target?: any,
): any {
const decodedOrigin: Exotic.proxy.origin = decode(scope, origin);
const decodedTarget = decode(scope, target);
const proxyAsTarget = findProxy(decodedTarget);
const newTarget = proxyAsTarget ? proxyAsTarget : target;
const { links } = map.emulators.get(scope);
const { action, proxy, key, value, that, args } = decodedOrigin;
const originData = map.proxies.get(proxy);

if (action === "link") {
// creates proxy by reference
return createProxy(
scope,
decodedOrigin,
links[key] === undefined ? target : links[key],
);
return createProxy(scope, decodedOrigin, links[key] || target);
}

if (action === "exec") {
const decodedTarget = decode(scope, target);
const program = new Function("$", `return (${decodedTarget})($)`);
return createProxy(scope, decodedOrigin, program(scope));
}

if (!action) {
// creates proxy by target
return createProxy(scope, decodedOrigin, newTarget);
return createProxy(scope, decodedOrigin, target);
}

if (!originData) {
Expand All @@ -65,9 +52,8 @@ export default function include(
}

if (proxyFromTrap) {
const localEncodedProxy = encode(proxyFromTrap);
if (localEncodedProxy !== encodedProxy) {
links[encodedProxy] = localEncodedProxy;
if (encode(proxyFromTrap) !== encodedProxy) {
links[encodedProxy] = proxyFromTrap;
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/types/Exotic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ declare namespace Exotic {

interface data {
options: options;
links: Record<key, key>;
refs: Record<key, Proxy>;
links: Record<key, Proxy>;
counter: number;
proxySet: Set<Proxy>;
}
Expand All @@ -60,11 +59,11 @@ declare namespace Exotic {

interface data {
revoke(): void;
id: number;
id: string;
revoked: boolean;
mock: Mock;
scope: Emulator;
sandbox: Record<key, any>;
sandbox: Record<key, Proxy>;
target?: any;
origin?: origin;
key?: key;
Expand Down
66 changes: 38 additions & 28 deletions src/utils/createProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,54 @@ import Exotic from "../types/Exotic.js";
import map from "./map.js";
import findProxy from "./findProxy.js";
import isTraceable from "./isTraceable.js";
import isPayload from "./isPayload.js";
import mockPrototype from "./mockPrototype.js";
import traps from "./traps/index.js";
import encode from "./encode.js";
import findProxyById from "./findProxyById.js";

const createProxy = (
scope: Exotic.Emulator,
origin: Exotic.proxy.origin = {},
target?: any,
): Exotic.Proxy => {
// find proxy by id

if (isPayload(target)) {
const proxyFromPayload = findProxyById(scope, target);
if (proxyFromPayload) {
return proxyFromPayload;
}
}

const data: Exotic.emulator.data = map.emulators.get(scope);
const { refs, options, proxySet } = data;
const { links, options, proxySet } = data;
const error = options.traceErrors ? new Error() : undefined;

const link = origin.action === "link" ? origin.key : undefined;
const validLink = link !== undefined;
const usableProxy = findProxy(target);
const encodedOrigin = encode(origin);
const encodedTarget = encode(target);

// find proxy by link reference

if (validLink) {
const proxyRef = links[link];
if (proxyRef) {
scope.emit(
"proxy",
encode(proxyRef),
encodedOrigin,
encodedTarget,
error,
);
return proxyRef;
}
}

// find a proxy that already exists

if (usableProxy) {
// proxy already exists
if (origin.action) {
data.counter++;
}
Expand All @@ -34,26 +63,7 @@ const createProxy = (
return usableProxy;
}

const refKey = origin.action === "link" ? origin.key : undefined;
const validRefKey = refKey !== undefined;
const reference = validRefKey ? refKey : undefined;

if (validRefKey) {
// proxy reference exists
const proxyRef = refs[reference];
if (proxyRef) {
scope.emit(
"proxy",
encode(proxyRef),
encodedOrigin,
encodedTarget,
error,
);
return proxyRef;
}
}

// create new proxy
// create a new proxy

const mock = Object.setPrototypeOf(
function () {},
Expand All @@ -62,7 +72,7 @@ const createProxy = (

let { proxy, revoke } = Proxy.revocable<Exotic.Proxy>(mock, traps);

const id = ++data.counter;
const id = `${++data.counter}`;
const sandbox = Object.create(null);

const revokeFunction = () => {
Expand All @@ -71,12 +81,12 @@ const createProxy = (
revoke = null;
};

if (validRefKey) {
if (validLink) {
// create unique reference
refs[reference] = proxy;
links[link] = proxy;
}

// proxy information
// add information about this proxy
const proxyData: Exotic.proxy.data = {
id,
mock,
Expand All @@ -85,7 +95,7 @@ const createProxy = (
revoke: revokeFunction,
scope,
sandbox,
key: reference,
key: link,
revoked: false,
};

Expand Down
7 changes: 5 additions & 2 deletions src/utils/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ export default function decode(
}

if (payload) {
const link = links[value] || value;
return findProxyById(scope, parseInt(link.substring(1)));
const link = links[value];
if (link) {
return link;
}
return findProxyById(scope, value);
}

if (traceable) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/findProxyById.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import findProxyById from "./findProxyById.js";
describe("(function) findProxyById", () => {
it("Gets the proxy's id number", () => {
const $ = new Emulator();
const expectedId = 1;
const expectedId = "1";
const proxy = $.use();
expect(findProxyById($, expectedId)).toBe(proxy);
});

it("Gets the proxy's id number within the same instance", () => {
const $ = new Emulator();
const $e = new Emulator();
const expectedId = 1;
const expectedId = "1";
const proxy = $.use();
const proxyOnAnotherInstance = $e.use();

Expand Down
10 changes: 6 additions & 4 deletions src/utils/findProxyById.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Exotic from "../types/Exotic.js";
import map from "./map.js";
import proxyIterator from "./proxyIterator.js";
import isPayload from "./isPayload.js";

export default function findProxyById(
scope: Exotic.Emulator,
id: number,
): void | Exotic.Proxy {
id: string,
): Exotic.Proxy | void {
const uid = isPayload(id) ? id.substring(1) : id;
for (const proxy of proxyIterator(scope)) {
const { id: proxyId } = map.proxies.get(proxy);
if (id === proxyId) {
const proxyData = map.proxies.get(proxy);
if (uid === proxyData.id) {
return proxy;
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/utils/revokeProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ const revokeProxy = (value: Exotic.traceable): boolean => {
}

const data = map.emulators.get(scope);
const { refs, links, proxySet } = data;
const { links, proxySet } = data;
const validKey = key !== undefined;

if (validKey) {
// key is binded to proxy
// delete key from refs object
// the key is linked to a proxy
// delete this key from the links
delete links[key];
// delete key from proxy
proxyData.key = undefined;
}

const decodedOrigin = decode(scope, origin) as Exotic.proxy.origin;
Expand All @@ -49,7 +47,6 @@ const revokeProxy = (value: Exotic.traceable): boolean => {
map.mocks.delete(mock);
map.targets.delete(target);
proxySet.delete(proxy);
delete refs[key];
delete links[encode(proxy)];

// !! keep in proxies map
Expand All @@ -72,7 +69,6 @@ const revokeProxy = (value: Exotic.traceable): boolean => {
// clean internal state
Object.assign(data, {
links: Object.create(null),
refs: Object.create(null),
counter: 0,
} as Exotic.emulator.data);
}
Expand Down

0 comments on commit 503f6df

Please sign in to comment.