简体   繁体   English

core-js / babel-polyfill polyfilled函数显示为[本机代码]

[英]core-js/babel-polyfill polyfilled functions appear as [native code]

Functions that were polyfilled with core-js (for example, babel-polyfill ) appear as native. core-js进行多填充的函数(例如babel-polyfill )显示为本机。

Promise.race.toString() produces: Promise.race.toString()产生:

function race() {
    [native code]
}

So does Object.values.toString() . Object.values.toString()

While they are certainly not browser's native implementations. 虽然它们当然不是浏览器的本机实现。 The example is Babel REPL page. 该示例是Babel REPL页面。

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)? 开发人员如何通过编程方式检查功能不是本机实现,而是一个模仿本机功能的polyfill(例如,当已知某个特定的polyfill可能在应用程序中引起问题时)?

Usually native code regexp helps, but it is certainly not the case. 通常, native code regexp会有所帮助,但事实并非如此。

How can source code be retrieved from such functions? 如何从此类函数中检索源代码? I'm primarily interested in Node.js but cross-platform solution is welcome. 我主要对Node.js感兴趣,但是欢迎跨平台解决方案。

How exactly was this trick done? 这个技巧到底是怎么完成的? I cannot find nothing for native code search in core-js and babel-polyfill source code. 对于core-jsbabel-polyfill源代码中的native 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] . core-js尝试通过将此处的 Function.prototype.toString替换为默认版本,以使其原生实现的方式显示其polyfilled功能,但如果愿意,可通过在fn[SRC]处设置值来允许core-js覆盖字符串值fn[SRC]

You can see farther up in that file here it assigns fn[SRC] , specifically 你可以看到在该文件更远了这里它分配fn[SRC]特别是

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 如果在这种情况下检查TPL ,则它是["function ", "() { [native code] }"] ,因此,当使用.join(String(key))调用时,您最终会得到

function race() { [native code] }

that you see in your output. 您在输出中看到的。

Polyfilled core-js functions has unique property which is defined by the library and stored internally, like Symbol(src)_1.g50a4eqv8s8xgvi . Polyfilled core-js函数具有由库定义并在内部存储的唯一属性 ,例如Symbol(src)_1.g50a4eqv8s8xgvi This allows the library to identify them and forge toString() results (as the other answer thoroughly explains). 这使库可以识别它们并伪造toString()结果(如其他答案所详尽解释的)。

It is possible to detect unique property and cheat core-js into revealing real function body: 可以检测到独特的属性并欺骗core-js以揭示真实的函数体:

function getPolyfillUid(fn) {
    return Object.getOwnPropertyNames(fn).find(prop => /^Symbol\(.+\)_/.test(prop))
}

function toTrueString(fn) {
    const uid = getPolyfillUid(fn);
    let fnString;
    if (uid) {
        const uidDescriptor = Object.getOwnPropertyDescriptor(fn, uid);
        delete fn[uid];
        fnString = fn.toString();
        Object.defineProperty(fn, uid, uidDescriptor);
    } else {
        fnString = fn.toString();
    }

    return fnString;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM