mirror of https://github.com/nodejs/node.git
lib: add validateThis function to validate this context
This commit is contained in:
parent
7335473dda
commit
6afc36d548
|
@ -17,6 +17,7 @@ const {
|
|||
String,
|
||||
StringPrototypeToUpperCase,
|
||||
StringPrototypeTrim,
|
||||
SymbolToStringTag,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
|
@ -24,7 +25,6 @@ const {
|
|||
ERR_INVALID_ARG_TYPE: { HideStackFramesError: ERR_INVALID_ARG_TYPE },
|
||||
ERR_INVALID_ARG_VALUE: { HideStackFramesError: ERR_INVALID_ARG_VALUE },
|
||||
ERR_INVALID_THIS: { HideStackFramesError: ERR_INVALID_THIS },
|
||||
ERR_MISSING_ARGS: { HideStackFramesError: ERR_MISSING_ARGS },
|
||||
ERR_OUT_OF_RANGE: { HideStackFramesError: ERR_OUT_OF_RANGE },
|
||||
ERR_SOCKET_BAD_PORT: { HideStackFramesError: ERR_SOCKET_BAD_PORT },
|
||||
ERR_UNKNOWN_SIGNAL: { HideStackFramesError: ERR_UNKNOWN_SIGNAL },
|
||||
|
@ -502,7 +502,6 @@ function validateUnion(value, name, union) {
|
|||
|
||||
/**
|
||||
* Validates that the `this` context is of the expected class type.
|
||||
*
|
||||
* @param {*} value - The `this` value to validate.
|
||||
* @param {string} className - The expected class name.
|
||||
* @throws {TypeError} Throws if `value` is not an object, is `null`, or does not match the expected class.
|
||||
|
@ -513,7 +512,7 @@ const validateThis = hideStackFrames((value, className) => {
|
|||
if (typeof value !== 'object' || value === null) {
|
||||
throw new ERR_INVALID_THIS(className);
|
||||
}
|
||||
const actualClassName = value[Symbol.toStringTag] || value.constructor?.name;
|
||||
const actualClassName = value[SymbolToStringTag] || value.constructor?.name;
|
||||
if (actualClassName !== className) {
|
||||
throw new ERR_INVALID_THIS(className);
|
||||
}
|
||||
|
@ -620,7 +619,6 @@ module.exports = {
|
|||
validateUndefined,
|
||||
validateUnion,
|
||||
validateThis,
|
||||
validateMissingArgs,
|
||||
validateAbortSignal,
|
||||
validateLinkHeaderValue,
|
||||
validateInternalField,
|
||||
|
|
Loading…
Reference in New Issue