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

Use is-callable for a reliable function test across older engines as well #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ljharb
Copy link

@ljharb ljharb commented May 10, 2015

Fixes #7.

@spion
Copy link
Owner

spion commented May 10, 2015

Thanks for the PR. I did fix the instanceof slipup. If I'm not mistaken, bluebird, when and q all use that exact same test to test for thenables. If its good enough for them, its good enough (and consistent) for blue-tape.

@ljharb
Copy link
Author

ljharb commented Jun 24, 2015

As the language gets more Realm support (which already works via the vm module and iframes) this will become more of a problem. They'll all have to fix it eventually ¯_(ツ)_/¯

@spion
Copy link
Owner

spion commented Jun 25, 2015

Are you sure this applies to the current code, which now relies on typeof rather than instanceof? With the typeof change the only remaining examples are on the fringe of JS edge cases afaik - or am I missing something?

@ljharb
Copy link
Author

ljharb commented Jun 25, 2015

Ah yes, sorry, you're right. If you're using typeof, you're only going to run into an issue with false positives with older engine regexes, and false negatives with some native objects that don't report "function".

In the future, the spec requires that class constructors (which aren't callable) be rejected, and if possible, is-callable will be updated to handle that - whereas typeof won't :-)

@tomek-he-him
Copy link
Contributor

👍 the future is now: https://nodejs.org/en/docs/es6/

@spion
Copy link
Owner

spion commented Nov 27, 2015

It looks to me like native promises don't check for class constructors yet, and do infact try to invoke them (tested with node v5.1.0)

var o = {then: class { constructor() { } } }
Promise.resolve(o).catch(e => console.log(e))

Output

Promise { <pending> }
> [TypeError: Class constructors cannot be invoked without 'new']

Will keep this PR open for when things change though.

@ljharb
Copy link
Author

ljharb commented Nov 27, 2015

@spion it's impossible to check for class constructors in a reliable cross-engine way. How would you check for function () { throw new TypeError("Class constructors cannot be invoked without 'new'"); }?

Trying to invoke it synchronously is in fact the correct native behavior, per spec, as I understand it. However, it should reject the promise, and I don't believe node's v8 has caught up to that fix just yet.

@spion
Copy link
Owner

spion commented Nov 27, 2015

Hmm. I thought thats what you meant by

In the future, the spec requires that class constructors (which aren't callable) be rejected, and if possible, is-callable will be updated to handle that - whereas typeof won't :-)

The promise was rejected though (with the error thrown by the class constructor being called without new)

@ljharb
Copy link
Author

ljharb commented Nov 27, 2015

Originally that is what I intended, but in the meantime, sadly, Firefox shipped "class" without the ability to determine if a function is from a "class" or not. The spec does require that "class"es toString to the original class syntax, but I haven't bothered to write that code yet since some versions of Firefox will never have the ability to determine it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

instanceof Function isn't reliable cross-realm
3 participants