wallet-core/thirdparty/systemjs/lib/global-eval.js

143 lines
4.3 KiB
JavaScript
Raw Normal View History

// we define a __exec for globally-scoped execution
// used by module format implementations
var __exec;
(function() {
var hasBuffer = typeof Buffer != 'undefined';
try {
if (hasBuffer && new Buffer('a').toString('base64') != 'YQ==')
hasBuffer = false;
}
catch(e) {
hasBuffer = false;
}
var sourceMapPrefix = '\n//# sourceMappingURL=data:application/json;base64,';
function inlineSourceMap(sourceMapString) {
if (hasBuffer)
return sourceMapPrefix + new Buffer(sourceMapString).toString('base64');
else if (typeof btoa != 'undefined')
return sourceMapPrefix + btoa(unescape(encodeURIComponent(sourceMapString)));
else
return '';
}
function getSource(load, wrap) {
var lastLineIndex = load.source.lastIndexOf('\n');
// wrap ES formats with a System closure for System global encapsulation
if (load.metadata.format == 'global')
wrap = false;
var sourceMap = load.metadata.sourceMap;
if (sourceMap) {
if (typeof sourceMap != 'object')
throw new TypeError('load.metadata.sourceMap must be set to an object.');
sourceMap = JSON.stringify(sourceMap);
}
return (wrap ? '(function(System, SystemJS) {' : '') + load.source + (wrap ? '\n})(System, System);' : '')
// adds the sourceURL comment if not already present
+ (load.source.substr(lastLineIndex, 15) != '\n//# sourceURL='
? '\n//# sourceURL=' + load.address + (sourceMap ? '!transpiled' : '') : '')
// add sourceMappingURL if load.metadata.sourceMap is set
+ (sourceMap && inlineSourceMap(sourceMap) || '');
}
var curLoad;
// System.register, System.registerDynamic, AMD define pipeline
// if currently evalling code here, immediately reduce the registered entry against the load record
hook('pushRegister_', function() {
return function(register) {
if (!curLoad)
return false;
this.reduceRegister_(curLoad, register);
return true;
};
});
// System clobbering protection (mostly for Traceur)
var curSystem;
var callCounter = 0;
function preExec(loader, load) {
curLoad = load;
if (callCounter++ == 0)
curSystem = __global.System;
__global.System = __global.SystemJS = loader;
}
function postExec() {
if (--callCounter == 0)
__global.System = __global.SystemJS = curSystem;
curLoad = undefined;
}
var useVm;
var vm;
__exec = function(load) {
if (!load.source)
return;
if ((load.metadata.integrity || load.metadata.nonce) && supportsScriptExec)
return scriptExec.call(this, load);
try {
preExec(this, load);
curLoad = load;
// global scoped eval for node (avoids require scope leak)
if (!vm && this._nodeRequire) {
vm = this._nodeRequire('vm');
useVm = vm.runInThisContext("typeof System !== 'undefined' && System") === this;
}
if (useVm)
vm.runInThisContext(getSource(load, true), { filename: load.address + (load.metadata.sourceMap ? '!transpiled' : '') });
else
(0, eval)(getSource(load, true));
postExec();
}
catch(e) {
postExec();
throw addToError(e, 'Evaluating ' + load.address);
}
};
var supportsScriptExec = false;
if (isBrowser && typeof document != 'undefined' && document.getElementsByTagName) {
if (!(window.chrome && window.chrome.extension || navigator.userAgent.match(/^Node\.js/)))
supportsScriptExec = true;
}
// script execution via injecting a script tag into the page
// this allows CSP integrity and nonce to be set for CSP environments
var head;
function scriptExec(load) {
if (!head)
head = document.head || document.body || document.documentElement;
var script = document.createElement('script');
script.text = getSource(load, false);
var onerror = window.onerror;
var e;
window.onerror = function(_e) {
e = addToError(_e, 'Evaluating ' + load.address);
if (onerror)
onerror.apply(this, arguments);
}
preExec(this, load);
if (load.metadata.integrity)
script.setAttribute('integrity', load.metadata.integrity);
if (load.metadata.nonce)
script.setAttribute('nonce', load.metadata.nonce);
head.appendChild(script);
head.removeChild(script);
postExec();
window.onerror = onerror;
if (e)
throw e;
}
})();