Functions that were polyfilled with core-js
(for example, babel-polyfill
) appear as native.
Promise.race.toString()
produces:
function race() {
[native code]
}
So does Object.values.toString()
.
While they are certainly not browser's native implementations. The example is Babel REPL page.
How can a developer programmatically check that a function is not native implementation but a polyfill that mimics native function (for instance, when it is known that a particular polyfill may cause problems in the application)?
Usually native code
regexp helps, but it is certainly not the case.
How can source code be retrieved from such functions? I'm primarily interested in Node.js but cross-platform solution is welcome.
How exactly was this trick done? I cannot find nothing for native code
search in core-js
and babel-polyfill
source code.
core-js
attempts to make its polyfilled functionality appear as natively implemented by replacing Function.prototype.toString
here with a version that falls back on the default, but allows core-js to override the string value if it wants by setting a value at fn[SRC]
.
You can see farther up in that file here it assigns fn[SRC]
, specifically
if(isFunction)has(val, SRC) || hide(val, SRC, O[key] ? '' + O[key] : TPL.join(String(key)));
If you inspect TPL
in this case it is ["function ", "() { [native code] }"]
, so when called with .join(String(key))
you end up with the
function race() { [native code] }
that you see in your output.