|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Alexander Akait @alexander-akait |
| 4 | +*/ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +const RuntimeGlobals = require("../RuntimeGlobals"); |
| 9 | +const Template = require("../Template"); |
| 10 | +const HelperRuntimeModule = require("./HelperRuntimeModule"); |
| 11 | + |
| 12 | +/** @import Compilation from "../Compilation" */ |
| 13 | + |
| 14 | +class SpecNamespaceObjectRuntimeModule extends HelperRuntimeModule { |
| 15 | + constructor() { |
| 16 | + super("spec namespace object"); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Generates runtime code for this runtime module. |
| 21 | + * @returns {string | null} runtime code |
| 22 | + */ |
| 23 | + generate() { |
| 24 | + const compilation = /** @type {Compilation} */ (this.compilation); |
| 25 | + const { runtimeTemplate } = compilation; |
| 26 | + const cst = runtimeTemplate.renderConst(); |
| 27 | + const fn = RuntimeGlobals.specNamespaceObject; |
| 28 | + return Template.asString([ |
| 29 | + `${fn} = ${runtimeTemplate.basicFunction("exports", [ |
| 30 | + `${cst} cached = ${fn}.c.get(exports);`, |
| 31 | + "if (cached) return cached;", |
| 32 | + // The exports object keeps the live bindings; the sealed target |
| 33 | + // carries only their names, so the traps stay invariant-safe. |
| 34 | + `${cst} names = Object.getOwnPropertyNames(exports).filter(${runtimeTemplate.expressionFunction( |
| 35 | + "name !== '__esModule'", |
| 36 | + "name" |
| 37 | + )}).sort();`, |
| 38 | + `${cst} target = { __proto__: null };`, |
| 39 | + "for (var i = 0; i < names.length; i++) Object.defineProperty(target, names[i], { value: undefined, writable: true, enumerable: true, configurable: false });", |
| 40 | + "Object.defineProperty(target, Symbol.toStringTag, { value: 'Module' });", |
| 41 | + "Object.preventExtensions(target);", |
| 42 | + `${cst} own = ${runtimeTemplate.expressionFunction( |
| 43 | + "typeof name === 'string' && Object.prototype.hasOwnProperty.call(target, name)", |
| 44 | + "name" |
| 45 | + )};`, |
| 46 | + `${cst} ns = new Proxy(target, {`, |
| 47 | + Template.indent([ |
| 48 | + "__proto__: null,", |
| 49 | + "getPrototypeOf: function() { return null; },", |
| 50 | + "setPrototypeOf: function(_, proto) { return proto === null; },", |
| 51 | + "isExtensible: function() { return false; },", |
| 52 | + "preventExtensions: function() { return true; },", |
| 53 | + "get: function(_, name, receiver) { return own(name) ? exports[name] : Reflect.get(target, name, receiver); },", |
| 54 | + "has: function(_, name) { return own(name) || Reflect.has(target, name); },", |
| 55 | + "ownKeys: function() { return Reflect.ownKeys(target); },", |
| 56 | + "getOwnPropertyDescriptor: function(_, name) { return own(name) ? { value: exports[name], writable: true, enumerable: true, configurable: false } : Reflect.getOwnPropertyDescriptor(target, name); },", |
| 57 | + "set: function() { return false; },", |
| 58 | + // A namespace may only be redefined to the descriptor it |
| 59 | + // already reports, so anything else is rejected. |
| 60 | + "defineProperty: function(_, name, descriptor) {", |
| 61 | + Template.indent([ |
| 62 | + // Symbol keys are ordinary, so redefining Symbol.toStringTag |
| 63 | + // to the descriptor it already has must still succeed. |
| 64 | + "if (typeof name === 'symbol') return Reflect.defineProperty(target, name, descriptor);", |
| 65 | + "if (!own(name) || 'get' in descriptor || 'set' in descriptor) return false;", |
| 66 | + "if (descriptor.configurable === true || descriptor.enumerable === false || descriptor.writable === false) return false;", |
| 67 | + "return !('value' in descriptor) || Object.is(descriptor.value, exports[name]);" |
| 68 | + ]), |
| 69 | + "},", |
| 70 | + "deleteProperty: function(_, name) { return !own(name) && !Reflect.has(target, name); }" |
| 71 | + ]), |
| 72 | + "});", |
| 73 | + `${fn}.c.set(exports, ns);`, |
| 74 | + "return ns;" |
| 75 | + ])};`, |
| 76 | + // The same exports object must always yield the same namespace. |
| 77 | + `${fn}.c = new WeakMap();` |
| 78 | + ]); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +module.exports = SpecNamespaceObjectRuntimeModule; |
0 commit comments