Skip to content

Commit

Permalink
MirrorMembrane part 1. Add sourceGraphKey to MembraneInternalIfc.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Sep 20, 2024
1 parent 0d07dd1 commit e153290
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 23 deletions.
14 changes: 10 additions & 4 deletions _02_code_generation/source/ConvertingHead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function getStatementsForProxyHandlerTrap(
(writer: CodeBlockWriter): void => {
writer.write(`this.#membraneIfc.convertArray<`);
param.typeStructure!.writerFunction(writer);
writer.write(`>(graphKey, argArray)`);
writer.write(`>(this.#thisGraphKey, graphKey, argArray)`);
}
);
continue;
Expand All @@ -214,7 +214,7 @@ function getStatementsForProxyHandlerTrap(
descriptorStatement = buildConstStatement(
nextArgumentName,
param.typeStructure!,
`this.#membraneIfc.convertDescriptor(graphKey, ${param.name})!`
`this.#membraneIfc.convertDescriptor(this.#thisGraphKey, graphKey, ${param.name})!`
);
continue;
}
Expand All @@ -232,7 +232,7 @@ function getStatementsForProxyHandlerTrap(
}] = this.#membraneIfc.convertArray<`
);
tupleType.writerFunction(writer);
writer.write(`>(graphKey, [${sourceConvertNames.join(", ")}]);`);
writer.write(`>(this.#thisGraphKey, graphKey, [${sourceConvertNames.join(", ")}]);`);
});
}

Expand Down Expand Up @@ -322,15 +322,20 @@ function insertConversionMembers(

// #graphHeadIfc
const ObjectGraphConversionIfc = new PropertyDeclarationImpl(false, "#graphConversionIfc");
const ThisGraphKey = new PropertyDeclarationImpl(false, "#thisGraphKey");
{
ObjectGraphConversionIfc.isReadonly = true;
ObjectGraphConversionIfc.typeStructure = LiteralTypeStructureImpl.get("ObjectGraphConversionIfc");

ThisGraphKey.isReadonly = true;
ThisGraphKey.typeStructure = UnionStringOrSymbol;

const ctorParam = new ParameterDeclarationImpl("graphConversionIfc");
ctorParam.typeStructure = ObjectGraphConversionIfc.typeStructure;
ctor.parameters.push(ctorParam);

ctor.statements.push(`this.#graphConversionIfc = graphConversionIfc`);
ctor.statements.push(`this.#graphConversionIfc = graphConversionIfc;`);
ctor.statements.push(`this.#thisGraphKey = graphConversionIfc.objectGraphKey;`);
}

// #getCommonConversions(target: object): CommonConversions
Expand Down Expand Up @@ -362,6 +367,7 @@ function insertConversionMembers(
MembraneInternalIfc,
ObjectGraphHandlerIfc,
ObjectGraphConversionIfc,
ThisGraphKey,
);

classDecl.methods.unshift(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function InheritedPropertyTraps(
if (shadowDesc.get === undefined)
return undefined;

const wrappedGet = this.membrane.convertDescriptor(nextGraphKey, shadowDesc)?.get;
const wrappedGet = this.membrane.convertDescriptor(this.thisGraphKey, nextGraphKey, shadowDesc)?.get;
graphAssert(typeof wrappedGet === "function", "must have a wrapped getter", this.membrane, this.thisGraphKey);

return super.apply(
Expand Down Expand Up @@ -162,11 +162,11 @@ export default function InheritedPropertyTraps(
if (setter === undefined)
return false;

const wrappedSet = this.membrane.convertDescriptor(nextGraphKey, ownDesc)?.set;
const wrappedSet = this.membrane.convertDescriptor(this.thisGraphKey, nextGraphKey, ownDesc)?.set;
graphAssert(wrappedSet !== undefined, "must have a wrapped getter", this.membrane, this.thisGraphKey);

const valueArgs = [newValue];
const wrappedValueArgs = this.membrane.convertArray(nextGraphKey, valueArgs);
const wrappedValueArgs = this.membrane.convertArray(this.thisGraphKey, nextGraphKey, valueArgs);

super.apply(
setter, receiver, valueArgs, nextGraphKey, wrappedSet, nextReceiver, wrappedValueArgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class ConvertingHeadProxyHandler
readonly #membraneIfc: MembraneInternalIfc;
readonly #graphHandlerIfc: ObjectGraphHandlerIfc;
readonly #graphConversionIfc: ObjectGraphConversionIfc;
readonly #thisGraphKey: string | symbol;

constructor(
membraneIfc: MembraneInternalIfc,
Expand All @@ -23,6 +24,7 @@ export default class ConvertingHeadProxyHandler
this.#membraneIfc = membraneIfc;
this.#graphHandlerIfc = graphHandlerIfc;
this.#graphConversionIfc = graphConversionIfc;
this.#thisGraphKey = graphConversionIfc.objectGraphKey;
}

#getCommonConversions(target: object): CommonConversions {
Expand All @@ -39,10 +41,13 @@ export default class ConvertingHeadProxyHandler
*/
public apply(shadowTarget: object, thisArg: any, argArray: any[]): any {
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextThisArg] = this.#membraneIfc.convertArray<[any]>(graphKey, [
thisArg,
]);
const [nextThisArg] = this.#membraneIfc.convertArray<[any]>(
this.#thisGraphKey,
graphKey,
[thisArg],
);
const nextArgArray: any[] = this.#membraneIfc.convertArray<any[]>(
this.#thisGraphKey,
graphKey,
argArray,
);
Expand All @@ -69,10 +74,12 @@ export default class ConvertingHeadProxyHandler
): object {
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextNewTarget] = this.#membraneIfc.convertArray<[Function]>(
this.#thisGraphKey,
graphKey,
[newTarget],
);
const nextArgArray: any[] = this.#membraneIfc.convertArray<any[]>(
this.#thisGraphKey,
graphKey,
argArray,
);
Expand All @@ -99,11 +106,16 @@ export default class ConvertingHeadProxyHandler
): boolean {
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextProperty] = this.#membraneIfc.convertArray<[string | symbol]>(
this.#thisGraphKey,
graphKey,
[property],
);
const nextAttributes: PropertyDescriptor =
this.#membraneIfc.convertDescriptor(graphKey, attributes)!;
this.#membraneIfc.convertDescriptor(
this.#thisGraphKey,
graphKey,
attributes,
)!;
return this.#graphHandlerIfc.defineProperty(
shadowTarget,
property,
Expand All @@ -124,6 +136,7 @@ export default class ConvertingHeadProxyHandler
public deleteProperty(shadowTarget: object, p: string | symbol): boolean {
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextP] = this.#membraneIfc.convertArray<[string | symbol]>(
this.#thisGraphKey,
graphKey,
[p],
);
Expand All @@ -146,7 +159,7 @@ export default class ConvertingHeadProxyHandler
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextP, nextReceiver] = this.#membraneIfc.convertArray<
[string | symbol, any]
>(graphKey, [p, receiver]);
>(this.#thisGraphKey, graphKey, [p, receiver]);
return this.#graphHandlerIfc.get(
shadowTarget,
p,
Expand All @@ -169,6 +182,7 @@ export default class ConvertingHeadProxyHandler
): PropertyDescriptor | undefined {
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextP] = this.#membraneIfc.convertArray<[string | symbol]>(
this.#thisGraphKey,
graphKey,
[p],
);
Expand Down Expand Up @@ -202,6 +216,7 @@ export default class ConvertingHeadProxyHandler
public has(shadowTarget: object, p: string | symbol): boolean {
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextP] = this.#membraneIfc.convertArray<[string | symbol]>(
this.#thisGraphKey,
graphKey,
[p],
);
Expand Down Expand Up @@ -265,7 +280,7 @@ export default class ConvertingHeadProxyHandler
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextP, nextNewValue, nextReceiver] = this.#membraneIfc.convertArray<
[string | symbol, any, any]
>(graphKey, [p, newValue, receiver]);
>(this.#thisGraphKey, graphKey, [p, newValue, receiver]);
return this.#graphHandlerIfc.set(
shadowTarget,
p,
Expand All @@ -286,9 +301,11 @@ export default class ConvertingHeadProxyHandler
*/
public setPrototypeOf(shadowTarget: object, v: object | null): boolean {
const { realTarget, graphKey } = this.#getCommonConversions(shadowTarget);
const [nextV] = this.#membraneIfc.convertArray<[object | null]>(graphKey, [
v,
]);
const [nextV] = this.#membraneIfc.convertArray<[object | null]>(
this.#thisGraphKey,
graphKey,
[v],
);
return this.#graphHandlerIfc.setPrototypeOf(
shadowTarget,
v,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
export interface MembraneInternalIfc
{
convertArray<ValueTypes extends unknown[]>(
sourceGraphKey: string | symbol,
targetGraphKey: string | symbol,
values: ValueTypes
) : ValueTypes;

convertDescriptor(
sourceGraphKey: string | symbol,
targetGraphKey: string | symbol,
descriptor: PropertyDescriptor | undefined,
): PropertyDescriptor | undefined;
Expand Down
6 changes: 3 additions & 3 deletions _03_objectgraph_handlers/source/types/ObjectGraphHeadIfc.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/** This interface is for membrane use. */
export interface ObjectGraphHeadIfc extends ObjectGraphValuesIfc {
/** The unique graph key. */
readonly objectGraphKey: string | symbol;

/** Revoke all proxies for a given object graph. */
revokeAllProxiesForGraph(
graphKey: string | symbol
Expand Down Expand Up @@ -31,6 +28,9 @@ export interface ObjectGraphConversionIfc extends ObjectGraphValuesIfc {
}

export interface ObjectGraphValuesIfc {
/** The unique graph key. */
readonly objectGraphKey: string | symbol;

/**
* This method exists to return an array of proxies, not a proxy to an array of values.
*/
Expand Down
2 changes: 2 additions & 0 deletions _03_objectgraph_handlers/spec/ConvertingHeadProxyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import MockMembrane from "./support/MockMembrane.js";
import SpyProxyHandler from "./support/SpyProxyHandler.js";

class LocalHead implements ObjectGraphConversionIfc {
readonly objectGraphKey = "this graph";

readonly #expectedRealTarget: () => void;
targetGraph?: string | symbol

Expand Down
2 changes: 2 additions & 0 deletions _03_objectgraph_handlers/spec/ObjectGraphTailHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe("ObjectGraphTailHandler", () => {
let handler = new PublicHandlerProperties(membrane, "this graph");

const mockGraphValues: ObjectGraphValuesIfc = {
objectGraphKey: "this graph",

getArrayInGraph: function <Elements extends unknown[] = unknown[]>(
valuesInSourceGraph: Elements,
sourceGraphKey: string | symbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ describe("Inherited property traps:", () => {
}

const graphValues: ObjectGraphValuesIfc = {
objectGraphKey: shadowGraphKey,

getArrayInGraph: function<
Elements extends unknown[] = unknown[]
>
Expand Down Expand Up @@ -92,6 +94,7 @@ describe("Inherited property traps:", () => {

const membraneMock: MembraneInternalIfc = {
convertArray: function <ValueTypes extends unknown[]>(
sourceGraphKey: string | symbol,
targetGraphKey: string | symbol,
values: ValueTypes
): ValueTypes
Expand All @@ -106,6 +109,7 @@ describe("Inherited property traps:", () => {
},

convertDescriptor: function (
sourceGraphKey: string | symbol,
targetGraphKey: string | symbol,
descriptor: PropertyDescriptor | undefined
): PropertyDescriptor | undefined
Expand Down
2 changes: 2 additions & 0 deletions _03_objectgraph_handlers/spec/decorators/revokedInFlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ it("Revoked in-flight proxies throw the revocation error", () => {
);

const mockGraphValues: ObjectGraphValuesIfc = {
objectGraphKey: Symbol("this graph"),

getArrayInGraph: function <Elements extends unknown[] = unknown[]>(valuesInSourceGraph: Elements, sourceGraphKey: string | symbol): Elements {
throw fooError;
},
Expand Down
2 changes: 2 additions & 0 deletions _03_objectgraph_handlers/spec/decorators/wrapReturnValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe("Wrapping return values works for the trap", () => {
let handler: ObjectGraphTailHandler;

class MockGraphValues implements ObjectGraphValuesIfc {
objectGraphKey = "this graph";

returnArray?: unknown[];
returnDesc?: PropertyDescriptor | undefined;
returnValue?: unknown;
Expand Down
2 changes: 2 additions & 0 deletions _03_objectgraph_handlers/spec/support/MockMembrane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class MockMembrane implements MembraneInternalIfc
ValueTypes extends unknown[]
>
(
sourceGraphKey: string | symbol,
targetGraphKey: string | symbol,
values: ValueTypes
): ValueTypes
Expand All @@ -16,6 +17,7 @@ export default class MockMembrane implements MembraneInternalIfc
}

convertDescriptor(
sourceGraphKey: string | symbol,
targetGraphKey: string | symbol,
descriptor: PropertyDescriptor
): PropertyDescriptor
Expand Down
Loading

0 comments on commit e153290

Please sign in to comment.