-
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
5bdf1c4
commit 829cfc6
Showing
15 changed files
with
258 additions
and
23 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
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 |
---|---|---|
@@ -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) |
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,19 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'sources': [ 'binding.cc' ], | ||
'includes': ['../common.gypi'], | ||
}, | ||
{ | ||
'target_name': 'binding-export-default', | ||
'sources': [ 'binding-export-default.cc' ], | ||
'includes': ['../common.gypi'], | ||
}, | ||
{ | ||
'target_name': 'binding-export-primitive', | ||
'sources': [ 'binding-export-primitive.cc' ], | ||
'includes': ['../common.gypi'], | ||
} | ||
] | ||
} |
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,50 @@ | ||
// eslint-disable-next-line node-core/require-common-first | ||
import { buildType } from '../../common/index.mjs'; | ||
import assert from 'node:assert'; | ||
import { createRequire } from 'node:module'; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
||
export async function run() { | ||
// binding.node | ||
{ | ||
const bindingPath = require.resolve(`./build/${buildType}/binding.node`); | ||
const { default: binding, 'module.exports': exports } = await import(bindingPath); | ||
assert.strictEqual(binding, exports); | ||
assert.strictEqual(binding.hello(), 'world'); | ||
|
||
const bindingRequire = require(bindingPath); | ||
assert.strictEqual(binding, bindingRequire); | ||
assert.strictEqual(binding.hello, bindingRequire.hello); | ||
|
||
// Test multiple loading of the same addon. | ||
// ESM cache can not be removed. | ||
delete require.cache[bindingPath]; | ||
const { default: rebinding } = await import(bindingPath); | ||
assert.strictEqual(rebinding.hello(), 'world'); | ||
assert.strictEqual(binding.hello, rebinding.hello); | ||
} | ||
|
||
// binding-export-default.node | ||
{ | ||
const bindingPath = require.resolve(`./build/${buildType}/binding-export-default.node`); | ||
const ns = await import(bindingPath); | ||
|
||
// As same as ESM-import-CJS, the default export is the value of `module.exports`. | ||
assert.strictEqual(ns.default, ns['module.exports']); | ||
assert.strictEqual(ns.default.default(), 'hello world'); | ||
|
||
const bindingRequire = require(bindingPath); | ||
assert.strictEqual(ns.default, bindingRequire); | ||
} | ||
|
||
// binding-export-primitive.node | ||
{ | ||
const bindingPath = require.resolve(`./build/${buildType}/binding-export-primitive.node`); | ||
const ns = await import(bindingPath); | ||
|
||
// As same as ESM-import-CJS, the default export is the value of `module.exports`. | ||
assert.strictEqual(ns.default, ns['module.exports']); | ||
assert.strictEqual(ns.default, 'hello world'); | ||
} | ||
} |
Oops, something went wrong.