fix global detection in safari (#1858)

This commit is contained in:
Guy Bedford 2018-10-06 22:08:43 +02:00 committed by GitHub
parent 6953624846
commit 989a04f1c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,20 +7,42 @@
const systemJSPrototype = System.constructor.prototype;
function getLastGlobalProp () {
// alternatively Object.keys(global).pop()
// but this may be faster (pending benchmarks)
// safari unpredictably lists some new globals first or second in object order
let firstGlobalProp, secondGlobalProp, lastGlobalProp;
function getGlobalProp () {
let cnt = 0;
let lastProp;
for (let p in global)
if (global.hasOwnProperty(p))
lastProp = p;
return lastProp;
for (let p in global) {
if (!global.hasOwnProperty(p))
continue;
if (cnt === 0 && p !== firstGlobalProp || cnt === 1 && p !== secondGlobalProp)
return p;
cnt++;
lastProp = p;
}
if (lastProp !== lastGlobalProp)
return lastProp;
}
function noteGlobalProps () {
// alternatively Object.keys(global).pop()
// but this may be faster (pending benchmarks)
firstGlobalProp = secondGlobalProp = undefined;
for (let p in global) {
if (!global.hasOwnProperty(p))
continue;
if (!firstGlobalProp)
firstGlobalProp = p;
else if (!secondGlobalProp)
secondGlobalProp = p;
lastGlobalProp = p;
}
return lastGlobalProp;
}
let lastGlobalProp;
const impt = systemJSPrototype.import;
systemJSPrototype.import = function (id, parentUrl) {
lastGlobalProp = getLastGlobalProp();
noteGlobalProps();
return impt.call(this, id, parentUrl);
};
@ -36,11 +58,10 @@ systemJSPrototype.getRegister = function () {
// when multiple globals, we take the global value to be the last defined new global object property
// for performance, this will not support multi-version / global collisions as previous SystemJS versions did
// note in Edge, deleting and re-adding a global does not change its ordering
const globalProp = getLastGlobalProp();
if (lastGlobalProp === globalProp)
const globalProp = getGlobalProp();
if (!globalProp)
return emptyInstantiation;
lastGlobalProp = globalProp;
let globalExport;
try {
globalExport = global[globalProp];