Skip to content

Commit

Permalink
back to the eval
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Feb 5, 2016
1 parent 1994220 commit 30b0f8b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 106 deletions.
8 changes: 0 additions & 8 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,13 @@ hookConstructor(function(constructor) {
// global behaviour flags
this.warnings = false;
this.defaultJSExtensions = false;
this.globalEvaluationScope = true;
this.pluginFirst = false;

// by default load ".json" files as json
// leading * meta doesn't need normalization
// NB add this in next breaking release
// this.meta['*.json'] = { format: 'json' };

// Default settings for globalEvaluationScope:
// Disabled for WebWorker, Chrome Extensions and jsdom
if (isWorker
|| isBrowser && window.chrome && window.chrome.extension
|| isBrowser && navigator.userAgent.match(/^Node\.js/))
this.globalEvaluationScope = false;

// support the empty module, as a concept
this.set('@empty', this.newModule({}));

Expand Down
131 changes: 33 additions & 98 deletions lib/global-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,6 @@ var __exec;

(function() {

// System clobbering protection (mostly for Traceur)
var curSystem;
var callCounter = 0;
var curLoad;
function preExec(loader, load) {
if (callCounter++ == 0)
curSystem = __global.System;
__global.System = __global.SystemJS = loader;
curLoad = load;
}
function postExec() {
if (--callCounter == 0)
__global.System = __global.SystemJS = curSystem;
curLoad = undefined;
}

// 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;
};
});

var hasBtoa = typeof btoa != 'undefined';

// used to support leading #!/usr/bin/env in scripts as supported in Node
Expand All @@ -40,7 +12,7 @@ var __exec;
function getSource(load, sourceMapOffset) {
var lastLineIndex = load.source.lastIndexOf('\n');

// wrap ES formats with a System closure for System global encapsulation
// wrap all formats with a System closure for System global encapsulation
var wrap = load.metadata.format == 'esm' || load.metadata.format == 'register' || load.metadata.bundle;

var sourceMap = load.metadata.sourceMap;
Expand All @@ -54,87 +26,50 @@ var __exec;

sourceMap = JSON.stringify(sourceMap);

return (wrap ? '(function(System) {' : '') + (load.metadata.format == 'cjs' ? load.source.replace(hashBangRegEx, '') : load.source) + (wrap ? '\n})(System);' : '')
return (wrap ? '(function(System, SystemJS) {' : '') + (load.metadata.format == 'cjs' ? load.source.replace(hashBangRegEx, '') : 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 && hasBtoa && '\n//# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(sourceMap))) || '');
}

function evalExec(load) {
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;
__exec = function(load) {
if (load.metadata.integrity)
throw new TypeError('Subresource integrity checking is not supported in Web Workers or Chrome Extensions.');
throw new TypeError('Subresource integrity checking is only supported on modules with scriptLoad:true metadata or through the SystemCSP build.');
try {
preExec(this, load);
new Function(getSource(load, true)).call(__global);
postExec();
if (callCounter++ == 0)
curSystem = __global.System;
__global.System = __global.SystemJS = this;
curLoad = load;
(0, eval)(getSource(load, true));
if (--callCounter == 0)
__global.System = __global.SystemJS = curSystem;
curLoad = undefined;
}
catch(e) {
postExec();
if (--callCounter == 0)
__global.System = __global.SystemJS = curSystem;
curLoad = undefined;
throw addToError(e, 'Evaluating ' + load.address);
}
}

// use script injection eval to get identical global script behaviour
if (typeof document != 'undefined' && document.getElementsByTagName) {
var head;

var scripts = document.getElementsByTagName('script');
$__curScript = scripts[scripts.length - 1];
__exec = function(load) {
if (!this.globalEvaluationScope)
return evalExec.call(this, 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);
}
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;
};
}

// global scoped eval for node
else if (typeof require != 'undefined') {
var vmModule = 'vm';
var vm = require(vmModule);
__exec = function vmExec(load) {
if (!this.globalEvaluationScope)
return evalExec.call(this, load);

if (load.metadata.integrity)
throw new TypeError('Subresource integrity checking is unavailable in Node.');
try {
preExec(this, load);
vm.runInThisContext(getSource(load, false));
postExec();
}
catch(e) {
postExec();
throw addToError(e.toString(), 'Evaluating ' + load.address);
}
};
}
else {
__exec = evalExec;
}
};

})();

0 comments on commit 30b0f8b

Please sign in to comment.