Skip to content
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

Merged
merged 1 commit into from May 17, 2022

Conversation

joeyparrish
Copy link
Member

@joeyparrish joeyparrish commented May 17, 2022

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 #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:

  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;
Copy link
Collaborator

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.

Copy link
Member Author

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.

Copy link
Collaborator

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.

@joeyparrish joeyparrish merged commit 4ae3a08 into shaka-project:main May 17, 2022
@joeyparrish joeyparrish deleted the config-ref-util branch May 17, 2022 18:19
joeyparrish added a commit that referenced this pull request May 17, 2022
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)
joeyparrish added a commit that referenced this pull request May 17, 2022
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)
joeyparrish added a commit that referenced this pull request May 17, 2022
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)
@github-actions github-actions bot added the status: archived Archived and locked; will not be updated label Jul 25, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: archived Archived and locked; will not be updated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants