mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-26 19:47:20 +00:00
Change evalSandboxed to use only one context for all modules (#4624)
* Run modules in one new context * Fix polyfill running on require * Add code to check for global assignments * Allow globals in separate context This lets the caller request a separate context which may be polluted. * Jasmine needs to pollute the global * Formatting * Add polyfill and cleanup code * Convert to ES5 * One bug fix * Some formatting * Remove console.log line! * Cleanup * revert css-escape-polyfill.js to hopefully remove it from the PR * Remove JSDOC directives
This commit is contained in:
parent
2bfefe3880
commit
2b95daf59b
50
boot/boot.js
50
boot/boot.js
@ -569,10 +569,32 @@ $tw.utils.getTypeEncoding = function(ext) {
|
||||
return typeInfo ? typeInfo.encoding : "utf8";
|
||||
};
|
||||
|
||||
var polyfill =[
|
||||
"// this polyfills the globalThis variable",
|
||||
"// using the this variable on a getter ",
|
||||
"// inserted into the prototype of globalThis",
|
||||
"(function() {",
|
||||
" if (typeof globalThis === 'object') return;",
|
||||
" // node.green says this is available since 0.10.48",
|
||||
" Object.prototype.__defineGetter__('__temp__', function() {",
|
||||
" return this;",
|
||||
" });",
|
||||
" __temp__.globalThis = __temp__;",
|
||||
" delete Object.prototype.__temp__;",
|
||||
"}());"
|
||||
].join("\n");
|
||||
|
||||
var globalCheck =[
|
||||
" if(Object.keys(globalThis).length){",
|
||||
" console.log(Object.keys(globalThis));",
|
||||
" throw \"Global assignment is not allowed within modules on node.\";",
|
||||
" }"
|
||||
].join('\n');
|
||||
|
||||
/*
|
||||
Run code globally with specified context variables in scope
|
||||
*/
|
||||
$tw.utils.evalGlobal = function(code,context,filename) {
|
||||
$tw.utils.evalGlobal = function(code,context,filename,sandbox,allowGlobals) {
|
||||
var contextCopy = $tw.utils.extend(Object.create(null),context);
|
||||
// Get the context variables as a pair of arrays of names and values
|
||||
var contextNames = [], contextValues = [];
|
||||
@ -581,25 +603,39 @@ $tw.utils.evalGlobal = function(code,context,filename) {
|
||||
contextValues.push(value);
|
||||
});
|
||||
// Add the code prologue and epilogue
|
||||
code = "(function(" + contextNames.join(",") + ") {(function(){\n" + code + "\n;})();\nreturn exports;\n})\n";
|
||||
code = [
|
||||
(!$tw.browser ? polyfill : ""),
|
||||
"(function(" + contextNames.join(",") + ") {",
|
||||
" (function(){\n" + code + "\n;})();",
|
||||
(!$tw.browser && sandbox && !allowGlobals) ? globalCheck : "",
|
||||
" return exports;\n",
|
||||
"})"
|
||||
].join("\n");
|
||||
|
||||
// Compile the code into a function
|
||||
var fn;
|
||||
if($tw.browser) {
|
||||
fn = window["eval"](code + "\n\n//# sourceURL=" + filename);
|
||||
} else {
|
||||
if(sandbox){
|
||||
fn = vm.runInContext(code,sandbox,filename)
|
||||
} else {
|
||||
fn = vm.runInThisContext(code,filename);
|
||||
}
|
||||
}
|
||||
// Call the function and return the exports
|
||||
return fn.apply(null,contextValues);
|
||||
};
|
||||
|
||||
$tw.utils.sandbox = !$tw.browser ? vm.createContext({}) : undefined;
|
||||
/*
|
||||
Run code in a sandbox with only the specified context variables in scope
|
||||
*/
|
||||
$tw.utils.evalSandboxed = $tw.browser ? $tw.utils.evalGlobal : function(code,context,filename) {
|
||||
var sandbox = $tw.utils.extend(Object.create(null),context);
|
||||
vm.runInNewContext(code,sandbox,filename);
|
||||
return sandbox.exports;
|
||||
$tw.utils.evalSandboxed = $tw.browser ? $tw.utils.evalGlobal : function(code,context,filename,allowGlobals) {
|
||||
return $tw.utils.evalGlobal(
|
||||
code,context,filename,
|
||||
allowGlobals ? vm.createContext({}) : $tw.utils.sandbox,
|
||||
allowGlobals
|
||||
);
|
||||
};
|
||||
|
||||
/*
|
||||
|
@ -76,7 +76,7 @@ exports.runTests = function(callback,specFilter) {
|
||||
}
|
||||
return $tw.modules.execute(moduleTitle,title);
|
||||
};
|
||||
var contextExports = $tw.utils.evalSandboxed(code,context,title);
|
||||
var contextExports = $tw.utils.evalSandboxed(code,context,title,true);
|
||||
// jasmine/jasmine.js assigns directly to `module.exports`: check
|
||||
// for it first.
|
||||
return context.module.exports || contextExports;
|
||||
|
Loading…
Reference in New Issue
Block a user