vm-shim.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. /* eslint-disable no-new-func */
  3. const acorn = require("acorn");
  4. const findGlobals = require("acorn-globals");
  5. const escodegen = require("escodegen");
  6. const jsGlobals = require("./browser/js-globals.json");
  7. // We can't use the default browserify vm shim because it doesn't work in a web worker.
  8. // "eval" is skipped because it's set to a function that calls `runInContext`:
  9. const jsGlobalEntriesToInstall = Object.entries(jsGlobals).filter(([name]) => name !== "eval" && name in global);
  10. exports.createContext = function (sandbox) {
  11. // TODO: This should probably use a symbol
  12. Object.defineProperty(sandbox, "__isVMShimContext", {
  13. value: true,
  14. writable: true,
  15. configurable: true,
  16. enumerable: false
  17. });
  18. for (const [globalName, globalPropDesc] of jsGlobalEntriesToInstall) {
  19. const propDesc = { ...globalPropDesc, value: global[globalName] };
  20. Object.defineProperty(sandbox, globalName, propDesc);
  21. }
  22. Object.defineProperty(sandbox, "eval", {
  23. value(code) {
  24. return exports.runInContext(code, sandbox);
  25. },
  26. writable: true,
  27. configurable: true,
  28. enumerable: false
  29. });
  30. };
  31. exports.isContext = function (sandbox) {
  32. return sandbox.__isVMShimContext;
  33. };
  34. exports.runInContext = function (code, contextifiedSandbox, options) {
  35. if (code === "this") {
  36. // Special case for during window creation.
  37. return contextifiedSandbox;
  38. }
  39. if (options === undefined) {
  40. options = {};
  41. }
  42. const comments = [];
  43. const tokens = [];
  44. const ast = acorn.parse(code, {
  45. allowReturnOutsideFunction: true,
  46. ranges: true,
  47. // collect comments in Esprima's format
  48. onComment: comments,
  49. // collect token ranges
  50. onToken: tokens
  51. });
  52. // make sure we keep comments
  53. escodegen.attachComments(ast, comments, tokens);
  54. const globals = findGlobals(ast);
  55. for (let i = 0; i < globals.length; ++i) {
  56. if (globals[i].name === "window" || globals[i].name === "this") {
  57. continue;
  58. }
  59. const { nodes } = globals[i];
  60. for (let j = 0; j < nodes.length; ++j) {
  61. const { type, name } = nodes[j];
  62. nodes[j].type = "MemberExpression";
  63. nodes[j].property = { name, type };
  64. nodes[j].computed = false;
  65. nodes[j].object = {
  66. name: "window",
  67. type: "Identifier"
  68. };
  69. }
  70. }
  71. const lastNode = ast.body[ast.body.length - 1];
  72. if (lastNode.type === "ExpressionStatement") {
  73. lastNode.type = "ReturnStatement";
  74. lastNode.argument = lastNode.expression;
  75. delete lastNode.expression;
  76. }
  77. const rewrittenCode = escodegen.generate(ast, { comment: true });
  78. const suffix = options.filename !== undefined ? "\n//# sourceURL=" + options.filename : "";
  79. return Function("window", rewrittenCode + suffix).bind(contextifiedSandbox)(contextifiedSandbox);
  80. };
  81. exports.Script = class VMShimScript {
  82. constructor(code, options) {
  83. this._code = code;
  84. this._options = options;
  85. }
  86. runInContext(sandbox, options) {
  87. return exports.runInContext(this._code, sandbox, { ...this._options, ...options });
  88. }
  89. };