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

Function.isCallable() / Function.isConstructor() example matrix #10

Open
aedart opened this issue Jan 29, 2024 · 9 comments
Open

Function.isCallable() / Function.isConstructor() example matrix #10

aedart opened this issue Jan 29, 2024 · 9 comments

Comments

@aedart
Copy link

aedart commented Jan 29, 2024

Hi

Would it be possible to provide an example matrix with various argument types and the expected result of isCallable() and isConstructor()?

Currently, I'm looking at Core-JS' implementation of this proposal and I'm wondering how built-in classes, e.g. Array, Proxy...etc, are expected to behave when tested via these methods.

@caitp
Copy link
Owner

caitp commented Jan 29, 2024

It will take some work to put together a complete matrix, I don't know when I'll have the time to complete this. But my vision overall is:

Refers to isCallable() specifically:

  • Most values with a [[Call]] internal method would return true
  • Class constructors would return false
  • A bound function would produce a value consistent with the target of the bound function
  • Returns true for Proxy objects which have an "apply" trap, or whose target has a [[Call]] method that does not statically throw (e.g, returns false for class constructors or builtins which emulate that behaviour)
  • Anything without a [[Call]] internal method, would return false (this is the case for most values, such as numbers, strings, most objects)

Similar logic would apply to isConstructor. If I get some time, and someone is interested in championing this in tc39, I'm happy to draft a matrix to help out

@aedart
Copy link
Author

aedart commented Jan 30, 2024

The reason why I'm asking about this, is to determine whether the implementation by Core-JS actually satisfies your vision, or if it is lacking. I'm particularly concerned with the about the "isClassConstructor" part in Core-JS (not intended as criticism of the author).

I guess what I'm trying to ask for, is if this proposal can be polyfilled. If so, perhaps that could be a place to start and continue to ask tc39 champions for help (sorry, I'm not much help here because I'm not skilled enough).

In any case, I do hope that this proposal gets through. JavaScript developers could really benefit from this type of "simple" functionality.

@caitp
Copy link
Owner

caitp commented Jan 30, 2024

I believe this would have false negatives, in the case where a class constructor is bound. OTOH, a bound class constructor should fail the toString test, and the property descriptor test

I may be wrong about that, ill take a look not on mobile in a bit to be sure. Probably does not have false positives, though

@caitp
Copy link
Owner

caitp commented Jan 30, 2024

Yeah,

class C {
  constructor() {
    console.log("C construct");
  }
}

let ctor = C.prototype.constructor.bind(null);

ctor(); // throws TypeError as it should
new ctor(); // returns instance of the class, invokes ctor
`${ctor}` // "function() {\n    [native code]\n}" 

(https://jsfiddle.net/utm79h2e/)

@zloirock
Copy link

Yep, AFAIK we have no way to detect bound classes. So it's marked sham: true.

@caitp
Copy link
Owner

caitp commented Jan 30, 2024

yeah, it's not fully polyfillable, but it gets pretty close.

@zloirock
Copy link

BTW I'm planning to remove proposals that have not progressed for a long time from core-js@4. Do you have any plans to advance it?

@caitp
Copy link
Owner

caitp commented Jan 30, 2024

It needs a champion to really get it anywhere, I can talk to my colleagues about raising it in committee, but it's not a super high priority atm

@aedart
Copy link
Author

aedart commented Jan 31, 2024

Thanks for your input. I really hope that this proposal will get through.

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

No branches or pull requests

3 participants