-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: add experimental support for addon modules
- Loading branch information
1 parent
c39875a
commit 16777e3
Showing
20 changed files
with
312 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
const { | ||
ArrayPrototypeMap, | ||
ArrayPrototypePush, | ||
Boolean, | ||
FunctionPrototypeCall, | ||
JSONParse, | ||
ObjectKeys, | ||
|
@@ -52,6 +51,7 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => { | |
}); | ||
const { emitExperimentalWarning, kEmptyObject, setOwnProperty, isWindows } = require('internal/util'); | ||
const { | ||
ERR_INVALID_RETURN_PROPERTY_VALUE, | ||
ERR_UNKNOWN_BUILTIN_MODULE, | ||
} = require('internal/errors').codes; | ||
const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache'); | ||
|
@@ -229,6 +229,47 @@ function createCJSModuleWrap(url, source, isMain, format, loadCJS = loadCJSModul | |
}, module); | ||
} | ||
|
||
/** | ||
* Creates a ModuleWrap object for a CommonJS module without source texts. | ||
* @param {string} url - The URL of the module. | ||
* @param {boolean} isMain - Whether the module is the main module. | ||
* @returns {ModuleWrap} The ModuleWrap object for the CommonJS module. | ||
*/ | ||
function createCJSNoSourceModuleWrap(url, isMain) { | ||
debug(`Translating CJSModule without source ${url}`); | ||
|
||
const filename = urlToFilename(url); | ||
|
||
const module = cjsEmplaceModuleCacheEntry(filename); | ||
cjsCache.set(url, module); | ||
|
||
if (isMain) { | ||
setOwnProperty(process, 'mainModule', module); | ||
} | ||
|
||
// Addon export names are not known until the addon is loaded. | ||
const exportNames = ['default', 'module.exports']; | ||
return new ModuleWrap(url, undefined, exportNames, function evaluationCallback() { | ||
debug(`Loading CJSModule ${url}`); | ||
|
||
if (!module.loaded) { | ||
wrapModuleLoad(filename, null, isMain); | ||
} | ||
|
||
/** @type {import('./loader').ModuleExports} */ | ||
let exports; | ||
if (module[kModuleExport] !== undefined) { | ||
exports = module[kModuleExport]; | ||
module[kModuleExport] = undefined; | ||
} else { | ||
({ exports } = module); | ||
} | ||
|
||
this.setExport('default', exports); | ||
this.setExport('module.exports', exports); | ||
}, module); | ||
} | ||
|
||
translators.set('commonjs-sync', function requireCommonJS(url, source, isMain) { | ||
initCJSParseSync(); | ||
|
||
|
@@ -280,26 +321,39 @@ translators.set('commonjs', function commonjsStrategy(url, source, isMain) { | |
return createCJSModuleWrap(url, source, isMain, 'commonjs', cjsLoader); | ||
}); | ||
|
||
/** | ||
* Get or create an entry in the CJS module cache for the given filename. | ||
* @param {string} filename CJS module filename | ||
* @returns {CJSModule} the cached CJS module entry | ||
*/ | ||
function cjsEmplaceModuleCacheEntry(filename, exportNames) { | ||
// TODO: Do we want to keep hitting the user mutable CJS loader here? | ||
let cjsMod = CJSModule._cache[filename]; | ||
if (cjsMod) { | ||
return cjsMod; | ||
} | ||
|
||
cjsMod = new CJSModule(filename); | ||
cjsMod.filename = filename; | ||
cjsMod.paths = CJSModule._nodeModulePaths(cjsMod.path); | ||
cjsMod[kIsCachedByESMLoader] = true; | ||
CJSModule._cache[filename] = cjsMod; | ||
|
||
return cjsMod; | ||
} | ||
|
||
/** | ||
* Pre-parses a CommonJS module's exports and re-exports. | ||
* @param {string} filename - The filename of the module. | ||
* @param {string} [source] - The source code of the module. | ||
* @param {boolean} isMain - Whether it is pre-parsing for the entry point. | ||
Check warning on line 349 in lib/internal/modules/esm/translators.js GitHub Actions / lint-js-and-md
|
||
* @param {string} format | ||
*/ | ||
function cjsPreparseModuleExports(filename, source, isMain, format) { | ||
let module = CJSModule._cache[filename]; | ||
if (module && module[kModuleExportNames] !== undefined) { | ||
function cjsPreparseModuleExports(filename, source) { | ||
const module = cjsEmplaceModuleCacheEntry(filename); | ||
if (module[kModuleExportNames] !== undefined) { | ||
return { module, exportNames: module[kModuleExportNames] }; | ||
} | ||
const loaded = Boolean(module); | ||
if (!loaded) { | ||
module = new CJSModule(filename); | ||
module.filename = filename; | ||
module.paths = CJSModule._nodeModulePaths(module.path); | ||
module[kIsCachedByESMLoader] = true; | ||
CJSModule._cache[filename] = module; | ||
} | ||
|
||
if (source === undefined) { | ||
({ source } = loadSourceForCJSWithHooks(module, filename, format)); | ||
|
@@ -462,6 +516,25 @@ translators.set('wasm', async function(url, source) { | |
}).module; | ||
}); | ||
|
||
// Strategy for loading a addon | ||
translators.set('addon', function translateAddon(url, source, isMain) { | ||
emitExperimentalWarning('Importing addons'); | ||
|
||
// The addon must be loaded from file system with dlopen. Assert | ||
// the source is null. | ||
if (source !== null) { | ||
throw new ERR_INVALID_RETURN_PROPERTY_VALUE( | ||
'null', | ||
'load', | ||
'source', | ||
source); | ||
} | ||
|
||
debug(`Translating addon ${url}`); | ||
|
||
return createCJSNoSourceModuleWrap(url, isMain); | ||
}); | ||
|
||
// Strategy for loading a commonjs TypeScript module | ||
translators.set('commonjs-typescript', function(url, source) { | ||
emitExperimentalWarning('Type Stripping'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Flags: --experimental-addon-modules | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('node:assert'); | ||
|
||
/** | ||
* Test that the export condition `node-addons` can be used | ||
* with `*.node` files with the ESM loader. | ||
*/ | ||
|
||
import(`esm-package/${common.buildType}`) | ||
.then((mod) => { | ||
assert.strictEqual(mod.default.hello(), 'world'); | ||
}) | ||
.then(common.mustCall()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Flags: --experimental-addon-modules | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('node:assert'); | ||
|
||
/** | ||
* Test that the export condition `node-addons` can be used | ||
* with `*.node` files with the CJS loader. | ||
*/ | ||
|
||
require(`esm-package/${common.buildType}`) | ||
.then((mod) => { | ||
assert.strictEqual(mod.hello(), 'world'); | ||
}) | ||
.then(common.mustCall()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <node.h> | ||
#include <uv.h> | ||
#include <v8.h> | ||
|
||
static void Method(const v8::FunctionCallbackInfo<v8::Value>& args) { | ||
v8::Isolate* isolate = args.GetIsolate(); | ||
args.GetReturnValue().Set( | ||
v8::String::NewFromUtf8(isolate, "hello world").ToLocalChecked()); | ||
} | ||
|
||
static void InitModule(v8::Local<v8::Object> exports, | ||
v8::Local<v8::Value> module, | ||
v8::Local<v8::Context> context) { | ||
NODE_SET_METHOD(exports, "default", Method); | ||
} | ||
|
||
NODE_MODULE_CONTEXT_AWARE(Binding, InitModule) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <node.h> | ||
#include <uv.h> | ||
#include <v8.h> | ||
|
||
static void InitModule(v8::Local<v8::Object> exports, | ||
v8::Local<v8::Value> module_val, | ||
v8::Local<v8::Context> context) { | ||
v8::Isolate* isolate = context->GetIsolate(); | ||
v8::Local<v8::Object> module = module_val.As<v8::Object>(); | ||
module | ||
->Set(context, | ||
v8::String::NewFromUtf8(isolate, "exports").ToLocalChecked(), | ||
v8::String::NewFromUtf8(isolate, "hello world").ToLocalChecked()) | ||
.FromJust(); | ||
} | ||
|
||
NODE_MODULE_CONTEXT_AWARE(Binding, InitModule) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <node.h> | ||
#include <uv.h> | ||
#include <v8.h> | ||
|
||
static void Method(const v8::FunctionCallbackInfo<v8::Value>& args) { | ||
v8::Isolate* isolate = args.GetIsolate(); | ||
args.GetReturnValue().Set( | ||
v8::String::NewFromUtf8(isolate, "world").ToLocalChecked()); | ||
} | ||
|
||
static void InitModule(v8::Local<v8::Object> exports, | ||
v8::Local<v8::Value> module, | ||
v8::Local<v8::Context> context) { | ||
NODE_SET_METHOD(exports, "hello", Method); | ||
} | ||
|
||
NODE_MODULE_CONTEXT_AWARE(Binding, InitModule) |
Oops, something went wrong.