tc39 / proposal-function-implementation-hiding

JavaScript language proposal: function implementation hiding
https://ci.tc39.es/preview/tc39/ecma262/pull/1739
MIT License
98 stars 7 forks source link

Provide a way to determine native implementations #46

Closed ghost closed 3 years ago

ghost commented 3 years ago

From the current README:

Another unnecessary insight gained by f.toString() is how a function was created. Most dramatically, it can be used to detect whether an implementation is "native" or not, by looking for the pattern of [native code].

This proposal fixes one problem, but causes another: we can no longer determine whether or not the function is built-in or not.

How about, instead of preventing this, providing a "proper" way to do this, that the community would encourage over the hacky Function#toString solution?

Something along the lines of:

isNativeCode(Math.sin); // true
isNativeCode(() => {}); // false
isNativeCode(some_wasm_function); // false

Although, this could be considered to be counter-intuitive to some of the goals that this proposal attempts to fix.

ljharb commented 3 years ago

You already can’t, because of .bind().

ghost commented 3 years ago

You already can’t, because of .bind().

Oh, right, I use .bind too often to have forgotten about it.

But still, providing a solution to the problem wouldn't be bad, would it?

ljharb commented 3 years ago

I suspect some would consider it bad, which is likely why the committee decided against a different string in the square brackets than “native code” - iow, it was intentional that hidden-implementation functions and bound functions and native functions be indistinguishable.

ghost commented 3 years ago

Prior proposals such as is types have been denied by the ES committee, suggesting that an idea such as this issue wouldn't be well-received.