Skip to content

Commit eed5e6b

Browse files
Simplified emit for private identifier class elements.
1 parent 5a2f195 commit eed5e6b

File tree

3 files changed

+264
-270
lines changed

3 files changed

+264
-270
lines changed

src/compiler/factory/emitHelpers.ts

+111-14
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ namespace ts {
3232
createImportDefaultHelper(expression: Expression): Expression;
3333
createExportStarHelper(moduleExpression: Expression, exportsExpression?: Expression): Expression;
3434
// Class Fields Helpers
35-
createClassPrivateFieldGetHelper(receiver: Expression, privateField: Identifier, kind?: "a" | "m" | undefined, f?: Identifier | undefined): Expression;
36-
createClassPrivateFieldSetHelper(receiver: Expression, privateField: Identifier, value: Expression, kind?: "a" | "m" | undefined, f?: Identifier | undefined): Expression;
35+
createClassPrivateFieldGetHelper(receiver: Expression, state: Identifier, kind: PrivateIdentifierKind, f: Identifier | undefined): Expression;
36+
createClassPrivateFieldSetHelper(receiver: Expression, state: Identifier, value: Expression, kind: PrivateIdentifierKind, f: Identifier | undefined): Expression;
3737
}
3838

3939
export function createEmitHelperFactory(context: TransformationContext): EmitHelperFactory {
@@ -368,38 +368,38 @@ namespace ts {
368368

369369
// Class Fields Helpers
370370

371-
function createClassPrivateFieldGetHelper(receiver: Expression, state: Identifier, kind?: "a" | "m" | undefined, f?: Identifier | undefined) {
371+
function createClassPrivateFieldGetHelper(receiver: Expression, state: Identifier, kind: PrivateIdentifierKind, f: Identifier | undefined) {
372372
context.requestEmitHelper(classPrivateFieldGetHelper);
373373
let args;
374-
if(!kind && !f) {
374+
if (kind === PrivateIdentifierKind.Field && !f) {
375375
args = [receiver, state];
376376
}
377-
else if(kind && !f) {
377+
else if (kind !== PrivateIdentifierKind.Field && !f) {
378378
args = [receiver, state, factory.createStringLiteral(kind)];
379379
}
380-
else if(!kind && f) {
380+
else if (kind === PrivateIdentifierKind.Field && f) {
381381
args = [receiver, state, factory.createVoidZero(), f];
382382
}
383383
else {
384-
args = [receiver, state, factory.createStringLiteral(kind!), f!];
384+
args = [receiver, state, factory.createStringLiteral(kind), f!];
385385
}
386386
return factory.createCallExpression(getUnscopedHelperName("__classPrivateFieldGet"), /*typeArguments*/ undefined, args);
387387
}
388388

389-
function createClassPrivateFieldSetHelper(receiver: Expression, state: Identifier, value: Expression, kind?: "a" | "m" | undefined, f?: Identifier | undefined) {
389+
function createClassPrivateFieldSetHelper(receiver: Expression, state: Identifier, value: Expression, kind: PrivateIdentifierKind, f: Identifier | undefined) {
390390
context.requestEmitHelper(classPrivateFieldSetHelper);
391391
let args;
392-
if(!kind && !f) {
392+
if (kind === PrivateIdentifierKind.Field && !f) {
393393
args = [receiver, state, value];
394394
}
395-
else if(kind && !f) {
395+
else if (kind !== PrivateIdentifierKind.Field && !f) {
396396
args = [receiver, state, value, factory.createStringLiteral(kind)];
397397
}
398-
else if(!kind && f) {
398+
else if (kind === PrivateIdentifierKind.Field && f) {
399399
args = [receiver, state, value, factory.createVoidZero(), f];
400400
}
401401
else {
402-
args = [receiver, state, value, factory.createStringLiteral(kind!), f!];
402+
args = [receiver, state, value, factory.createStringLiteral(kind), f!];
403403
}
404404
return factory.createCallExpression(getUnscopedHelperName("__classPrivateFieldSet"), /*typeArguments*/ undefined, args);
405405
}
@@ -830,7 +830,6 @@ namespace ts {
830830
};`
831831
};
832832

833-
// emit output for the __export helper function
834833
export const exportStarHelper: UnscopedEmitHelper = {
835834
name: "typescript:export-star",
836835
importName: "__exportStar",
@@ -843,7 +842,57 @@ namespace ts {
843842
};`
844843
};
845844

846-
// Class fields helpers
845+
/**
846+
* Parameters:
847+
* @param receiver — The object on which the private member will be set (existing).
848+
* @param state — One of the following (existing):
849+
* - A WeakMap used to store a private instance field (existing).
850+
* - A WeakSet used as an instance brand for private instance methods and accessors.
851+
* - A function value that should be the undecorated class constructor used to brand check private static fields, methods, and accessors.
852+
* @param value — The value to set (existing case).
853+
* @param kind — (optional pre TS 4.3, required for TS 4.3+) One of the following values:
854+
* - undefined — Indicates a private instance field (pre TS 4.3).
855+
* - "field" — Indicates a private field (instance or static).
856+
* - "method" — Indicates a private method (instance or static).
857+
* - "accessor" — Indicates a private accessor (instance or static).
858+
* @param f — (optional pre TS 4.3) Depends on the arguments for state and kind:
859+
* - If kind is "m", this should be the function corresponding to the static or instance method.
860+
* - If kind is "a", this should be the function corresponding to the setter method, or undefined if the setter was not defined.
861+
* - If kind is "f" and state is a function, this should be an object holding the value of a static field, or undefined if the static field declaration has not yet been evaluated.
862+
* Usage:
863+
* This helper will only ever be used by the compiler in the following ways:
864+
*
865+
* Writing to a private instance field (pre TS 4.3):
866+
* __classPrivateFieldSet(<any>, <WeakMap>, <any>)
867+
*
868+
* Writing to a private instance field (TS 4.3+):
869+
* __classPrivateFieldSet(<any>, <WeakMap>, <any>, "f")
870+
*
871+
* Writing to a private instance set accessor (when defined, TS 4.3+):
872+
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "a", <function>)
873+
*
874+
* Writing to a private instance set accessor (when not defined, TS 4.3+):
875+
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "a", void 0)
876+
* NOTE: This always results in a runtime error.
877+
*
878+
* Writing to a private instance method (TS 4.3+):
879+
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "m", <function>)
880+
* NOTE: This always results in a runtime error.
881+
*
882+
* Writing to a private static field (TS 4.3+):
883+
* __classPrivateFieldSet(<any>, <constructor>, <any>, "f", <{ value: any }>)
884+
*
885+
* Writing to a private static set accessor (when defined, TS 4.3+):
886+
* __classPrivateFieldSet(<any>, <constructor>, <any>, "a", <function>)
887+
*
888+
* Writing to a private static set accessor (when not defined, TS 4.3+):
889+
* __classPrivateFieldSet(<any>, <constructor>, <any>, "a", void 0)
890+
* NOTE: This always results in a runtime error.
891+
*
892+
* Writing to a private static method (TS 4.3+):
893+
* __classPrivateFieldSet(<any>, <constructor>, <any>, "m", <function>)
894+
* NOTE: This always results in a runtime error.
895+
*/
847896
export const classPrivateFieldGetHelper: UnscopedEmitHelper = {
848897
name: "typescript:classPrivateFieldGet",
849898
importName: "__classPrivateFieldGet",
@@ -856,6 +905,54 @@ namespace ts {
856905
};`
857906
};
858907

908+
/**
909+
* Parameters:
910+
* @param receiver — The object from which the private member will be read (existing).
911+
* @param state — One of the following (existing):
912+
* - A WeakMap used to read a private instance field (existing).
913+
* - A WeakSet used as an instance brand for private instance methods and accessors.
914+
* - A function value that should be the undecorated class constructor used to brand check private static fields, methods, and accessors.
915+
* @param kind — (optional pre TS 4.3, required for TS 4.3+) One of the following values:
916+
* - undefined — Indicates a private instance field (pre TS 4.3).
917+
* - "field" — Indicates a private field (instance or static).
918+
* - "method" — Indicates a private method (instance or static).
919+
* - "accessor" — Indicates a private accessor (instance or static).
920+
* @param f — (optional pre TS 4.3) Depends on the arguments for state and kind:
921+
* - If kind is "m", this should be the function corresponding to the static or instance method.
922+
* - If kind is "a", this should be the function corresponding to the getter method, or undefined if the getter was not defined.
923+
* - If kind is "f" and state is a function, this should be an object holding the value of a static field, or undefined if the static field declaration has not yet been evaluated.
924+
* Usage:
925+
* This helper will only ever be used by the compiler in the following ways:
926+
*
927+
* Reading from a private instance field (pre TS 4.3):
928+
* __classPrivateFieldGet(<any>, <WeakMap>)
929+
*
930+
* Reading from a private instance field (TS 4.3+):
931+
* __classPrivateFieldGet(<any>, <WeakMap>, "f")
932+
*
933+
* Reading from a private instance get accessor (when defined, TS 4.3+):
934+
* __classPrivateFieldGet(<any>, <WeakSet>, "a", <function>)
935+
*
936+
* Reading from a private instance get accessor (when not defined, TS 4.3+):
937+
* __classPrivateFieldGet(<any>, <WeakSet>, "a", void 0)
938+
* NOTE: This always results in a runtime error.
939+
*
940+
* Reading from a private instance method (TS 4.3+):
941+
* __classPrivateFieldGet(<any>, <WeakSet>, "m", <function>)
942+
*
943+
* Reading from a private static field (TS 4.3+):
944+
* __classPrivateFieldGet(<any>, <constructor>, "f", <{ value: any }>)
945+
*
946+
* Reading from a private static get accessor (when defined, TS 4.3+):
947+
* __classPrivateFieldGet(<any>, <constructor>, "a", <function>)
948+
*
949+
* Reading from a private static get accessor (when not defined, TS 4.3+):
950+
* __classPrivateFieldGet(<any>, <constructor>, "a", void 0)
951+
* NOTE: This always results in a runtime error.
952+
*
953+
* Reading from a private static method (TS 4.3+):
954+
* __classPrivateFieldGet(<any>, <constructor>, "m", <function>)
955+
*/
859956
export const classPrivateFieldSetHelper: UnscopedEmitHelper = {
860957
name: "typescript:classPrivateFieldSet",
861958
importName: "__classPrivateFieldSet",

0 commit comments

Comments
 (0)