var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "method") throw new TypeError("Private method is not writable");
if (kind === "accessor" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "accessor" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
Parameters:
receiver
— The object on which the private member will be set (existing).state
— One of the following (existing):- A
WeakMap
used to store a private instance field (existing). - A
WeakSet
used as an instance brand for private instance methods and accessors. - A
function
value that should be the undecorated class constructor used to brand check private static fields, methods, and accessors.
- A
value
— The value to set (existing case).kind
— (optional pre TS 4.3, required for TS 4.3+) One of the following values:undefined
— Indicates a private instance field (pre TS 4.3)."field"
— Indicates a private field (instance or static)."method"
— Indicates a private method (instance or static)."accessor"
— Indicates a private accessor (instance or static).
f
— (optional pre TS 4.3) Depends on the arguments forstate
andkind
:- If
kind
is"method"
, this should be thefunction
corresponding to the static or instance method. - If
kind
is"accessor"
, this should be thefunction
corresponding to the setter method, orundefined
if the setter was not defined. - If
kind
is"field"
andstate
is afunction
, this should be an object holding the value of a static field, orundefined
if the static field declaration has not yet been evaluated.
- If
Usage:
This helper will only ever be used by the compiler in the following ways:
-
Writing to a private instance field (pre TS 4.3):
__classPrivateFieldSet(<any>, <WeakMap>, <any>)
-
Writing to a private instance field (TS 4.3+):
__classPrivateFieldSet(<any>, <WeakMap>, <any>, "field")
-
Writing to a private instance set accessor (when defined, TS 4.3+):
__classPrivateFieldSet(<any>, <WeakSet>, <any>, "accessor", <function>)
-
Writing to a private instance set accessor (when not defined, TS 4.3+):
__classPrivateFieldSet(<any>, <WeakSet>, <any>, "accessor", void 0)
- NOTE: This always results in a runtime error.
-
Writing to a private instance method (TS 4.3+):
__classPrivateFieldSet(<any>, <WeakSet>, <any>, "method", <function>)
- NOTE: This always results in a runtime error.
-
Writing to a private static field (TS 4.3+):
__classPrivateFieldSet(<any>, <constructor>, <any>, "field", <{ value: any }>)
-
Writing to a private static set accessor (when defined, TS 4.3+):
__classPrivateFieldSet(<any>, <constructor>, <any>, "accessor", <function>)
-
Writing to a private static set accessor (when not defined, TS 4.3+):
__classPrivateFieldSet(<any>, <constructor>, <any>, "accessor", void 0)
- NOTE: This always results in a runtime error.
-
Writing to a private static method (TS 4.3+):
__classPrivateFieldSet(<any>, <constructor>, <any>, "method", <function>)
- NOTE: This always results in a runtime error.
Algorithm:
-
if (kind === "method") throw new TypeError("Private method is not writable");
NOTE: This matches the order for Private State-related Errors Thrown by V8.
Private methods (instance or static) are not writable.
-
if (kind === "accessor" && !f) throw new TypeError("Private accessor was defined without a setter");
NOTE: This matches the order for Private State-related Errors Thrown by V8.
A private accessor (instance or static) without a setter throws when an attempt is made to set a value.
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
We will break this down into each branch:
- If
typeof state === "function"
, then:- NOTE: This invocation is for a
static
member, asstate
will be the constructor function. - If
receiver !== state
, then- NOTE: The
receiver
is not the class and thus fails the brand check. - NOTE: This matches the order for Private State-related Errors Thrown by V8.
- Throw a
TypeError
.
- NOTE: The
- If
!f
, then- NOTE:
kind
cannot beundefined
here, asundefined
is only provided when pre TS 4.3 emit encounters this updated helper, which never supported static members. TS 4.3+ will always emit thekind
. - NOTE:
kind
cannot be"method"
here as that was removed as a constituent in Step 1. - NOTE:
kind
cannot be"accessor"
here, as an"accessor"
with a missingf
was tested in Step 2. - NOTE:
kind
can only be"field"
here, given the above. A"field"
with a missing storage object has not yet had its declaration evaluated. - NOTE: This matches the order for Private State-related Errors Thrown by V8.
- Throw a
TypeError
.
- NOTE:
- NOTE: This invocation is for a
- Else,
- NOTE: This invocation is for an instance member, as
state
will be either aWeakMap
(for private fields), or aWeakSet
(for private methods or accessors). - NOTE: Both
WeakMap
andWeakSet
have ahas
method that can be used to test for presence. - If
!state.has(receiver)
, then- NOTE: The
receiver
failed the brand check. - NOTE: This matches the order for Private State-related Errors Thrown by V8.
- Throw a
TypeError
.
- NOTE: The
- NOTE: This invocation is for an instance member, as
- If
-
return (kind === "accessor" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
We will break this down into each branch:
- NOTE: The comma expression here will expression on the left of the
,
, but result in the value on the right. - NOTE:
kind
cannot be"method"
here, as that was tested in Step 1. - If
kind === "accessor"
, then- NOTE:
f
will be defined here, as an"accessor"
with a missingf
was tested in Step 2. - Evaluate the setter using
f.call(receiver, value)
.
- NOTE:
- Else, if
f
is truthy, then- NOTE: Per Step 1, we know that
kind
cannot be"method"
, sof
cannot be a method function. - NOTE: Per Step 4.i, we know that
kind
cannot be"accessor"
, sof
cannot be an accessor function. - NOTE:
f
is not provided for instance fields before or after TS 4.3, so its presence can only indicate a static private field. - NOTE: A static private field with a missing
f
was tested in Step 3.i.c, thereforef
must be the static private field descriptor. - Set
f.value = value
.
- NOTE: Per Step 1, we know that
- Else,
- NOTE:
kind
must either be"field"
(in TS 4.3+), orundefined
(pre TS 4.3). Either way, this must be an instance field per Step 4.iii - NOTE: As this is an instance field,
state
will be aWeakMap
as the compiler will only provide afunction
for static members and aWeakSet
for methods or accessors. - Store the field value using
state.set(receiver, value)
- NOTE:
- Return
value
.
- NOTE: The comma expression here will expression on the left of the
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "accessor" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "method" ? f : kind === "accessor" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
Parameters:
receiver
— The object from which the private member will be read (existing).state
— One of the following (existing):- A
WeakMap
used to read a private instance field (existing). - A
WeakSet
used as an instance brand for private instance methods and accessors. - A
function
value that should be the undecorated class constructor used to brand check private static fields, methods, and accessors.
- A
kind
— (optional pre TS 4.3, required for TS 4.3+) One of the following values:undefined
— Indicates a private instance field (pre TS 4.3)."field"
— Indicates a private field (instance or static)."method"
— Indicates a private method (instance or static)."accessor"
— Indicates a private accessor (instance or static).
f
— (optional pre TS 4.3) Depends on the arguments forstate
andkind
:- If
kind
is"method"
, this should be thefunction
corresponding to the static or instance method. - If
kind
is"accessor"
, this should be thefunction
corresponding to the getter method, orundefined
if the getter was not defined. - If
kind
is"field"
andstate
is afunction
, this should be an object holding the value of a static field, orundefined
if the static field declaration has not yet been evaluated.
- If
Usage:
This helper will only ever be used by the compiler in the following ways:
-
Reading from a private instance field (pre TS 4.3):
__classPrivateFieldGet(<any>, <WeakMap>)
-
Reading from a private instance field (TS 4.3+):
__classPrivateFieldGet(<any>, <WeakMap>, "field")
-
Reading from a private instance get accessor (when defined, TS 4.3+):
__classPrivateFieldGet(<any>, <WeakSet>, "accessor", <function>)
-
Reading from a private instance get accessor (when not defined, TS 4.3+):
__classPrivateFieldGet(<any>, <WeakSet>, "accessor", void 0)
- NOTE: This always results in a runtime error.
-
Reading from a private instance method (TS 4.3+):
__classPrivateFieldGet(<any>, <WeakSet>, "method", <function>)
-
Reading from a private static field (TS 4.3+):
__classPrivateFieldGet(<any>, <constructor>, "field", <{ value: any }>)
-
Reading from a private static get accessor (when defined, TS 4.3+):
__classPrivateFieldGet(<any>, <constructor>, "accessor", <function>)
-
Reading from a private static get accessor (when not defined, TS 4.3+):
__classPrivateFieldGet(<any>, <constructor>, "accessor", void 0)
- NOTE: This always results in a runtime error.
-
Reading from a private static method (TS 4.3+):
__classPrivateFieldGet(<any>, <constructor>, "method", <function>)
Algorithm:
-
if (kind === "accessor" && !f) throw new TypeError("Private accessor was defined without a getter");
A private accessor (instance or static) without a getter throws when an attempt is made to read from the accessor.
NOTE: This matches the order for Private State-related Errors Thrown by V8.
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
We will break this down into each branch:
- If
typeof state === "function"
, then:- NOTE: This invocation is for a
static
member, asstate
will be the constructor function. - If
receiver !== state
, then- NOTE: The
receiver
is not the class, and thus fails the brand check. - NOTE: This matches the order for Private State-related Errors Thrown by V8.
- Throw a
TypeError
.
- NOTE: The
- If
!f
, then- NOTE:
kind
cannot beundefined
here, asundefined
is only provided when pre TS 4.3 emit encounters this updated helper, which never supported static members. TS 4.3+ will always emit thekind
. - NOTE:
kind
cannot be"accessor"
here, as an"accessor"
with a missingf
was tested in Step 1. - NOTE: When
kind
is"method"
,f
will always be provided. - NOTE:
kind
can only be"field"
here, given the above. A"field"
with a missing storage object has not yet had its declaration evaluated. - NOTE: This matches the order for Private State-related Errors Thrown by V8.
- Throw a
TypeError
.
- NOTE:
- NOTE: This invocation is for a
- Else,
- NOTE: This invocation is for an instance member, as
state
will be either aWeakMap
(for private fields), or aWeakSet
(for private methods or accessors). - NOTE: Both
WeakMap
andWeakSet
have ahas
method that can be used to test for presence. - If
!state.has(receiver)
, then- NOTE: This means the
receiver
failed the brand check. - NOTE: This matches the order for Private State-related Errors Thrown by V8.
- Throw a
TypeError
.
- NOTE: This means the
- NOTE: This invocation is for an instance member, as
- If
-
return kind === "method" ? f : kind === "accessor" ? f.call(receiver) : f ? f.value : state.get(receiver);
We will break this down into each branch:
- If
kind === "method"
, then- NOTE:
f
will always be provided whenkind
is"method"
. - Return
f
.
- NOTE:
- If
kind === "accessor"
, then- NOTE:
f
will be defined here, as an"accessor"
with a missingf
was tested in Step 2. - Return
f.call(receiver)
.
- NOTE:
- Else, if
f
is truthy, then- NOTE: Per Step 3.i, we know that
kind
cannot be"method"
, sof
cannot be a method function. - NOTE: Per Step 3.ii, we know that
kind
cannot be"accessor"
, sof
cannot be an accessor function. - NOTE:
f
is not provided for instance fields before or after TS 4.3, so its presence can only indicate a static private field. - NOTE: A static private field with a missing
f
was tested in Step 2.i.c, thereforef
must be the static private field descriptor. - Return
f.value
.
- NOTE: Per Step 3.i, we know that
- Else,
- NOTE:
kind
must either be"field"
(in TS 4.3+), orundefined
(pre TS 4.3). Either way, this must be an instance field per Steps 3.i through 3.iii. - NOTE: As this is an instance field,
state
will be aWeakMap
as the compiler will only provide afunction
for static members and aWeakSet
for methods or accessors. - Return
state.get(receiver)
.
- NOTE:
- If
V8 thows a TypeError
with one of the following messages in various cases of private state access:
NOTE: Each case below is in the order that it would be tested. For example, a violation of case (1) will throw before a violation of case (4).
Private method '<name>' is not writable
- when attempting to write to a private method (static or instance)'<name>' was defined without a setter
- when attempting to set a private accessor that has no setter (static or instance)'<name>' was defined without a getter
- when attempting to read a private accessor that has no getter (static or instance)Object must be an instance of class <name>
- when calling a private method on the wrong instanceCannot read private member <name> from an object whose class did not declare it
- when reading a private field or accessor from the wrong instance, or when reading a private static field whose declaration has not yet been evaluated. Also used when calling a private static method on the wrong object.Cannot write private member <name> to an object whose class did not declare it
- when writing a private field or accessor to the wrong instance, or when writing a private static field whose declaration has not yet been evaluated.
I believe (4) is sufficiently covered by (5), and is therefore unnecessary. This is motivated by the fact that (5) is used as the error message for the static method case.
void 0
in trailing parameters can be omitted, since they default toundefined
.