Skip to content

url: check forEach callback is a function - #10905

Closed
TimothyGu wants to merge 6 commits into
nodejs:masterfrom
TimothyGu:urlsearchparams-foreach
Closed

url: check forEach callback is a function#10905
TimothyGu wants to merge 6 commits into
nodejs:masterfrom
TimothyGu:urlsearchparams-foreach

Conversation

@TimothyGu

Copy link
Copy Markdown
Member

Currently, URLSearchParams' forEach function does not have a check to make sure that the type of the provided callback is a function. However, the Web IDL spec, to which this function should conform, insists that a TypeError be thrown if the callback is not callable.

This PR fulfills that requirement. Also included in this PR are additional tests for certain aspects of forEach that are defined in the spec but not yet tested.


Web IDL definesforEach as the following:

voidforEach(Functioncallback, optionalanythisArg);

callback therefore has type Function, which in turn is defined as a callback function:

callbackFunction=any(any...arguments);

The process to convert an ECMAScript value to an IDL callback function then contains the following:

  1. If the result of calling IsCallable(V) is false and the conversion to an IDL value is not being performed due to V being assigned to an attribute whose type is a nullable callback function that is annotated with [TreatNonObjectAsNull], then throw a TypeError.

And since callback is not declared with [TreatNonObjectAsNull], forEach should throw a TypeError in case callback is not a function.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

url

@nodejs-github-botnodejs-github-bot added dont-land-on-v4.x whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Jan 20, 2017
Comment threadlib/internal/url.js
if (arguments.length < 1) {
throw new TypeError('The `callback` argument needs to be specified');
}
if (typeof callback !== 'function') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make the check on line 817 obsolete?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjihrig, this provides more granular error messages, which I think is a good thing. Do you prefer me to just use the same error message for both cases?

In the browser, they have different error messages as well:

>>newURLSearchParams().forEach()**TypeError: Failedtoexecute'forEach'on'URLSearchParams':
1argumentrequired,butonly0present.>>newURLSearchParams().forEach(2)**TypeError: Failedtoexecute'forEach'on'URLSearchParams':
Thecallbackprovidedasparameter1isnotafunction.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the extraneous check as suggested

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our error messages don't even seem to match up though. I'd still change it, but I won't fight it too much.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

sp.forEach(function() {
assert.strictEqual(this, m);
}, m);
assert.throws(() => sp.forEach(), TypeError);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the TypeError constructor, can you pass a regular expression like /^TypeError: The callback argument must be a function$/

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@TimothyGu

Copy link
Copy Markdown
MemberAuthor

/cc @nodejs/url

Comment threadlib/internal/url.js Outdated
throw new TypeError('The `callback` argument needs to be specified');
}
if (typeof callback !== 'function') {
throw new TypeError('The `callback` argument must be a function');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our standard way of writing this would be TypeError('"callback" argument must be a function')

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@TimothyGu

Copy link
Copy Markdown
MemberAuthor

@TimothyGu

Copy link
Copy Markdown
MemberAuthor

Landed in ed0086f.

@TimothyGu
TimothyGu deleted the urlsearchparams-foreach branch January 24, 2017 00:44
TimothyGu added a commit that referenced this pull request Jan 24, 2017
The Web IDL spec mandates such a check.
Also make error messages consistent with rest of Node.js and add
additional tests for forEach().
PR-URL: #10905
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 25, 2017
The Web IDL spec mandates such a check.
Also make error messages consistent with rest of Node.js and add
additional tests for forEach().
PR-URL: nodejs#10905
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 27, 2017
The Web IDL spec mandates such a check.
Also make error messages consistent with rest of Node.js and add
additional tests for forEach().
PR-URL: nodejs#10905
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
@italoacasasitaloacasas mentioned this pull request Jan 29, 2017
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
The Web IDL spec mandates such a check.
Also make error messages consistent with rest of Node.js and add
additional tests for forEach().
PR-URL: nodejs#10905
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
The Web IDL spec mandates such a check.
Also make error messages consistent with rest of Node.js and add
additional tests for forEach().
PR-URL: nodejs#10905
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

whatwg-urlIssues and PRs related to the WHATWG URL implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@TimothyGu@jasnell@targos@cjihrig@joyeecheung@mscdex@nodejs-github-bot