Skip to content

Commit a3fdc4b

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

File tree

47 files changed

+478
-494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+478
-494
lines changed

src/compiler/factory/emitHelpers.ts

+107-22
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,26 @@ 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) {
375-
args = [receiver, state];
376-
}
377-
else if(kind && !f) {
374+
if (!f) {
378375
args = [receiver, state, factory.createStringLiteral(kind)];
379376
}
380-
else if(!kind && f) {
381-
args = [receiver, state, factory.createVoidZero(), f];
382-
}
383377
else {
384-
args = [receiver, state, factory.createStringLiteral(kind!), f!];
378+
args = [receiver, state, factory.createStringLiteral(kind), f];
385379
}
386380
return factory.createCallExpression(getUnscopedHelperName("__classPrivateFieldGet"), /*typeArguments*/ undefined, args);
387381
}
388382

389-
function createClassPrivateFieldSetHelper(receiver: Expression, state: Identifier, value: Expression, kind?: "a" | "m" | undefined, f?: Identifier | undefined) {
383+
function createClassPrivateFieldSetHelper(receiver: Expression, state: Identifier, value: Expression, kind: PrivateIdentifierKind, f: Identifier | undefined) {
390384
context.requestEmitHelper(classPrivateFieldSetHelper);
391385
let args;
392-
if(!kind && !f) {
393-
args = [receiver, state, value];
394-
}
395-
else if(kind && !f) {
386+
if (!f) {
396387
args = [receiver, state, value, factory.createStringLiteral(kind)];
397388
}
398-
else if(!kind && f) {
399-
args = [receiver, state, value, factory.createVoidZero(), f];
400-
}
401389
else {
402-
args = [receiver, state, value, factory.createStringLiteral(kind!), f!];
390+
args = [receiver, state, value, factory.createStringLiteral(kind), f];
403391
}
404392
return factory.createCallExpression(getUnscopedHelperName("__classPrivateFieldSet"), /*typeArguments*/ undefined, args);
405393
}
@@ -830,7 +818,6 @@ namespace ts {
830818
};`
831819
};
832820

833-
// emit output for the __export helper function
834821
export const exportStarHelper: UnscopedEmitHelper = {
835822
name: "typescript:export-star",
836823
importName: "__exportStar",
@@ -843,7 +830,57 @@ namespace ts {
843830
};`
844831
};
845832

846-
// Class fields helpers
833+
/**
834+
* Parameters:
835+
* @param receiver — The object on which the private member will be set.
836+
* @param state — One of the following:
837+
* - A WeakMap used to store a private instance field.
838+
* - A WeakSet used as an instance brand for private instance methods and accessors.
839+
* - A function value that should be the undecorated class constructor used to brand check private static fields, methods, and accessors.
840+
* @param value — The value to set.
841+
* @param kind — (optional pre TS 4.3, required for TS 4.3+) One of the following values:
842+
* - undefined — Indicates a private instance field (pre TS 4.3).
843+
* - "f" — Indicates a private field (instance or static).
844+
* - "m" — Indicates a private method (instance or static).
845+
* - "a" — Indicates a private accessor (instance or static).
846+
* @param f — (optional pre TS 4.3) Depends on the arguments for state and kind:
847+
* - If kind is "m", this should be the function corresponding to the static or instance method.
848+
* - If kind is "a", this should be the function corresponding to the setter method, or undefined if the setter was not defined.
849+
* - 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.
850+
* Usage:
851+
* This helper will only ever be used by the compiler in the following ways:
852+
*
853+
* Writing to a private instance field (pre TS 4.3):
854+
* __classPrivateFieldSet(<any>, <WeakMap>, <any>)
855+
*
856+
* Writing to a private instance field (TS 4.3+):
857+
* __classPrivateFieldSet(<any>, <WeakMap>, <any>, "f")
858+
*
859+
* Writing to a private instance set accessor (when defined, TS 4.3+):
860+
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "a", <function>)
861+
*
862+
* Writing to a private instance set accessor (when not defined, TS 4.3+):
863+
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "a", void 0)
864+
* NOTE: This always results in a runtime error.
865+
*
866+
* Writing to a private instance method (TS 4.3+):
867+
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "m", <function>)
868+
* NOTE: This always results in a runtime error.
869+
*
870+
* Writing to a private static field (TS 4.3+):
871+
* __classPrivateFieldSet(<any>, <constructor>, <any>, "f", <{ value: any }>)
872+
*
873+
* Writing to a private static set accessor (when defined, TS 4.3+):
874+
* __classPrivateFieldSet(<any>, <constructor>, <any>, "a", <function>)
875+
*
876+
* Writing to a private static set accessor (when not defined, TS 4.3+):
877+
* __classPrivateFieldSet(<any>, <constructor>, <any>, "a", void 0)
878+
* NOTE: This always results in a runtime error.
879+
*
880+
* Writing to a private static method (TS 4.3+):
881+
* __classPrivateFieldSet(<any>, <constructor>, <any>, "m", <function>)
882+
* NOTE: This always results in a runtime error.
883+
*/
847884
export const classPrivateFieldGetHelper: UnscopedEmitHelper = {
848885
name: "typescript:classPrivateFieldGet",
849886
importName: "__classPrivateFieldGet",
@@ -856,6 +893,54 @@ namespace ts {
856893
};`
857894
};
858895

896+
/**
897+
* Parameters:
898+
* @param receiver — The object from which the private member will be read.
899+
* @param state — One of the following:
900+
* - A WeakMap used to read a private instance field.
901+
* - A WeakSet used as an instance brand for private instance methods and accessors.
902+
* - A function value that should be the undecorated class constructor used to brand check private static fields, methods, and accessors.
903+
* @param kind — (optional pre TS 4.3, required for TS 4.3+) One of the following values:
904+
* - undefined — Indicates a private instance field (pre TS 4.3).
905+
* - "f" — Indicates a private field (instance or static).
906+
* - "m" — Indicates a private method (instance or static).
907+
* - "a" — Indicates a private accessor (instance or static).
908+
* @param f — (optional pre TS 4.3) Depends on the arguments for state and kind:
909+
* - If kind is "m", this should be the function corresponding to the static or instance method.
910+
* - If kind is "a", this should be the function corresponding to the getter method, or undefined if the getter was not defined.
911+
* - 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.
912+
* Usage:
913+
* This helper will only ever be used by the compiler in the following ways:
914+
*
915+
* Reading from a private instance field (pre TS 4.3):
916+
* __classPrivateFieldGet(<any>, <WeakMap>)
917+
*
918+
* Reading from a private instance field (TS 4.3+):
919+
* __classPrivateFieldGet(<any>, <WeakMap>, "f")
920+
*
921+
* Reading from a private instance get accessor (when defined, TS 4.3+):
922+
* __classPrivateFieldGet(<any>, <WeakSet>, "a", <function>)
923+
*
924+
* Reading from a private instance get accessor (when not defined, TS 4.3+):
925+
* __classPrivateFieldGet(<any>, <WeakSet>, "a", void 0)
926+
* NOTE: This always results in a runtime error.
927+
*
928+
* Reading from a private instance method (TS 4.3+):
929+
* __classPrivateFieldGet(<any>, <WeakSet>, "m", <function>)
930+
*
931+
* Reading from a private static field (TS 4.3+):
932+
* __classPrivateFieldGet(<any>, <constructor>, "f", <{ value: any }>)
933+
*
934+
* Reading from a private static get accessor (when defined, TS 4.3+):
935+
* __classPrivateFieldGet(<any>, <constructor>, "a", <function>)
936+
*
937+
* Reading from a private static get accessor (when not defined, TS 4.3+):
938+
* __classPrivateFieldGet(<any>, <constructor>, "a", void 0)
939+
* NOTE: This always results in a runtime error.
940+
*
941+
* Reading from a private static method (TS 4.3+):
942+
* __classPrivateFieldGet(<any>, <constructor>, "m", <function>)
943+
*/
859944
export const classPrivateFieldSetHelper: UnscopedEmitHelper = {
860945
name: "typescript:classPrivateFieldSet",
861946
importName: "__classPrivateFieldSet",

0 commit comments

Comments
 (0)