martok / palefill

Inject Polyfills for various web technologies into pages requiring them
https://martok.github.io/palefill/
Mozilla Public License 2.0
79 stars 9 forks source link

Use feature checking instead of version checking #23

Closed roytam1 closed 2 years ago

roytam1 commented 2 years ago

If you run palefill on platform code which just changed version but not getting opchain code in (i.e. https://repo.palemoon.org/MoonchildProductions/UXP/commit/062a5085b12f33e8f7c4bb76bd539bd60f3c3686) then palefill doesn't rewrite optchain code (even platform code returns 5.1.0 but it can't parse optchain javascript code)

instead, feature checking will be better-fit:

const isOptionalChainingSupported = () => {
  try {
    eval('const foo = {}; foo?.bar');
  } catch(e) {
    return false;
  }

  return true;
}

const isNullishCoalescingSupported = () => {
  try {
    eval('const foo = null ?? "default string";');
  } catch(e) {
    return false;
  }

  return true;
}

(credits: copied and rewrote from https://stackoverflow.com/a/69935847)

martok commented 2 years ago

What happened to "don't use eval in strict mode"? Is that not a thing anymore? IIRC having that call somewhere in a module completely disabled JIT, that would be pretty bad.


Edit: The Function() constructor is fine, apparently. Also marginally faster since we don't actually need to execute the code anyway.