diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:50:11 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:50:11 +0200 |
commit | 9af485a584e47fd503ed5c62b9f6482574715f1e (patch) | |
tree | 687abbcff412bd4ce5c51d51de030a06e3963cf5 /lib/global-eval.js |
Squashed 'thirdparty/systemjs/' content from commit 5ed69b6
git-subtree-dir: thirdparty/systemjs
git-subtree-split: 5ed69b6344e8fc1cd43bf758350b2236f57e1499
Diffstat (limited to 'lib/global-eval.js')
-rw-r--r-- | lib/global-eval.js | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/lib/global-eval.js b/lib/global-eval.js new file mode 100644 index 000000000..f27efc222 --- /dev/null +++ b/lib/global-eval.js @@ -0,0 +1,142 @@ +// 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; + } + +})(); |