From ffd22e81983056d09c064c59343a0e488236272d Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 4 Oct 2019 13:37:27 -0400 Subject: module: use v8 synthetic modules PR-URL: https://github.com/nodejs/node/pull/29846 Reviewed-By: Gus Caplan Reviewed-By: Minwoo Jung --- lib/internal/bootstrap/loaders.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'lib/internal/bootstrap/loaders.js') diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index dbc21a9697..0cad5209c4 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -150,8 +150,7 @@ function NativeModule(id) { this.filename = `${id}.js`; this.id = id; this.exports = {}; - this.reflect = undefined; - this.esmFacade = undefined; + this.module = undefined; this.exportKeys = undefined; this.loaded = false; this.loading = false; @@ -240,16 +239,18 @@ NativeModule.prototype.getURL = function() { }; NativeModule.prototype.getESMFacade = function() { - if (this.esmFacade) return this.esmFacade; - const createDynamicModule = nativeModuleRequire( - 'internal/modules/esm/create_dynamic_module'); + if (this.module) return this.module; + const { ModuleWrap } = internalBinding('module_wrap'); const url = this.getURL(); - return this.esmFacade = createDynamicModule( - [], [...this.exportKeys, 'default'], url, (reflect) => { - this.reflect = reflect; - this.syncExports(); - reflect.exports.default.set(this.exports); - }); + const nativeModule = this; + this.module = new ModuleWrap(function() { + nativeModule.syncExports(); + this.setExport('default', nativeModule.exports); + }, [...this.exportKeys, 'default'], url); + // Ensure immediate sync execution to capture exports now + this.module.instantiate(); + this.module.evaluate(-1, false); + return this.module; }; // Provide named exports for all builtin libraries so that the libraries @@ -258,13 +259,12 @@ NativeModule.prototype.getESMFacade = function() { // called so that APMs and other behavior are supported. NativeModule.prototype.syncExports = function() { const names = this.exportKeys; - if (this.reflect) { + if (this.module) { for (let i = 0; i < names.length; i++) { const exportName = names[i]; if (exportName === 'default') continue; - this.reflect.exports[exportName].set( - getOwn(this.exports, exportName, this.exports) - ); + this.module.setExport(exportName, + getOwn(this.exports, exportName, this.exports)); } } }; -- cgit v1.2.3