This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Trick minimized source and static feature probes, take II #15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Continue the work begun in #14, addressing the problems that only surfaced when I tried to use the new version in atom/atom as part of atom/atom#18611.
The problem
Recent versions of React, which we snapshot as a dependency of the GitHub package, are distributed on npm as minimized source. Unfortunately, a common pattern within minimizers is to alias a global function as a short name to shave off a few characters at each call site:
Because
requestAnimationFrameis not defined while the snapshot is being produced,Ais bound toundefinedand any calls fail.React's Scheduler also uses feature probing to fall back to more primitive implementations when certain global functions are unavailable:
This means that the React scheduler will be using a less performant fallback implementation, even though
requestAnimationFrameand friends are available at runtime!The fix
To handle both of these situations, I'm introducing a set of trampolines for all global functions that are defined in a full Electron renderer process, but not within the mksnapshot environment.
First of all, I needed to find the full list of global functions that should be there in a valid browser window. I ran the following in the dev console of an
atom --safewindow:Next, I needed to discover which of these global functions are still defined under mksnapshot. To do that I first needed to be able to run arbitrary source from a snapshot, so I wrote @smashwilson/run-in-snapshot. Once that was working, I pointed it at a script in this repo that I wrote that attempted to
eval()each name from the list and collected lists of those that passed and failed; this is available astest/samples/list-globals.js.I added the global names we need to shim to
src/blueprint.jsas an Array, and added logic that would define placeholder functions for each. The placeholder functions use a variable in scope calledglobalFunctionTrampolineto determine if the deferred global function has been set or not, and either call it appropriately or fail.Finally, I've modified
setGlobals()to populate the trampolines with the real functions.My first attempt at doing this broke Atom because I'd accidentally been installing the global function shims on
window, rather than in a self-contained function scope within the blueprint script. As a result, the real global functions were trampled by the shims and were unable to be restored properly. I gave the IIFE its own scope to prevent this from happening.Remaining work