New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Add utility to keep unused parameters #4233
Conversation
Many times in our default configuration, we need to create an empty default implementation of a callback. The compiler wants to strip out unused parameters from these functions, but this breaks our runtime-type-checking for functions in configure(). We need a way to keep the full function signature in default config callbacks in compiled mode. This adds a new utility: ConfigUtils.referenceParametersAndReturn(). It references the input parameters so the compiler doesn't remove them from the calling function, and returns whatever value is specified. The utility function is marked with `@noinline`, so the compiler won't tamper with it. Default config callbacks that use this utility will still bear the complete function signature even in compiled mode. The caller should look something like this: const callback = (a, b, c, d) => { return referenceParametersAndReturn( [a, b, c, d], a); // Can be anything, doesn't need to be one of the parameters }; See also shaka-project#4231 (comment)
* @noinline | ||
*/ | ||
static referenceParametersAndReturn(parameters, returnValue) { | ||
return parameters && returnValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't come up in any situation where we actually call this at the moment, but technically this approach won't work correctly if returnValue
is falsey.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It definitely will. true && 0 === 0
, true && undefined === undefined
, etc. It works this way for any truthy value, not just true
. And parameters
(being a non-nullable Array) is always truthy, even if it's an empty array for some reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see, I made a mistake while testing this.
Many times in our default configuration, we need to create an empty default implementation of a callback. The compiler wants to strip out unused parameters from these functions, but this breaks our runtime-type-checking for functions in configure(). We need a way to keep the full function signature in default config callbacks in compiled mode. This adds a new utility: ConfigUtils.referenceParametersAndReturn(). It references the input parameters so the compiler doesn't remove them from the calling function, and returns whatever value is specified. The utility function is marked with `@noinline`, so the compiler won't tamper with it. Default config callbacks that use this utility will still bear the complete function signature even in compiled mode. The caller should look something like this: ```js const callback = (a, b, c, d) => { return referenceParametersAndReturn( [a, b, c, d], a); // Can be anything, doesn't need to be one of the parameters }; ``` See also #4231 (comment)
Many times in our default configuration, we need to create an empty default implementation of a callback. The compiler wants to strip out unused parameters from these functions, but this breaks our runtime-type-checking for functions in configure(). We need a way to keep the full function signature in default config callbacks in compiled mode. This adds a new utility: ConfigUtils.referenceParametersAndReturn(). It references the input parameters so the compiler doesn't remove them from the calling function, and returns whatever value is specified. The utility function is marked with `@noinline`, so the compiler won't tamper with it. Default config callbacks that use this utility will still bear the complete function signature even in compiled mode. The caller should look something like this: ```js const callback = (a, b, c, d) => { return referenceParametersAndReturn( [a, b, c, d], a); // Can be anything, doesn't need to be one of the parameters }; ``` See also #4231 (comment)
Many times in our default configuration, we need to create an empty default implementation of a callback. The compiler wants to strip out unused parameters from these functions, but this breaks our runtime-type-checking for functions in configure(). We need a way to keep the full function signature in default config callbacks in compiled mode. This adds a new utility: ConfigUtils.referenceParametersAndReturn(). It references the input parameters so the compiler doesn't remove them from the calling function, and returns whatever value is specified. The utility function is marked with `@noinline`, so the compiler won't tamper with it. Default config callbacks that use this utility will still bear the complete function signature even in compiled mode. The caller should look something like this: ```js const callback = (a, b, c, d) => { return referenceParametersAndReturn( [a, b, c, d], a); // Can be anything, doesn't need to be one of the parameters }; ``` See also #4231 (comment)
Many times in our default configuration, we need to create an empty
default implementation of a callback. The compiler wants to strip
out unused parameters from these functions, but this breaks our
runtime-type-checking for functions in configure(). We need a way to
keep the full function signature in default config callbacks in
compiled mode.
This adds a new utility: ConfigUtils.referenceParametersAndReturn().
It references the input parameters so the compiler doesn't remove them
from the calling function, and returns whatever value is specified.
The utility function is marked with
@noinline
, so the compiler won'ttamper with it.
Default config callbacks that use this utility will still bear the
complete function signature even in compiled mode.
The caller should look something like this:
See also #4231 (comment)