node: register modules from DSO constructors

Built-in modules should be automatically registered, replacing the
static module list.  Add-on modules should also be automatically
registered via DSO constructors.  This improves flexibility in adding
built-in modules and is also a prerequisite to pure-C addon modules.
archived-io.js-v0.10
Keith M Wesolowski 2013-12-17 00:00:44 +00:00 committed by Timothy J Fontaine
parent f4c8020d10
commit 76b98462e5
25 changed files with 162 additions and 239 deletions

View File

@ -91,7 +91,6 @@
'src/node_buffer.cc',
'src/node_constants.cc',
'src/node_contextify.cc',
'src/node_extensions.cc',
'src/node_file.cc',
'src/node_http_parser.cc',
'src/node_javascript.cc',
@ -123,7 +122,6 @@
'src/node_buffer.h',
'src/node_constants.h',
'src/node_contextify.h',
'src/node_extensions.h',
'src/node_file.h',
'src/node_http_parser.h',
'src/node_internals.h',

View File

@ -1177,4 +1177,4 @@ static void Initialize(Handle<Object> target,
} // namespace cares_wrap
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_cares_wrap, node::cares_wrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(cares_wrap, node::cares_wrap::Initialize)

View File

@ -193,4 +193,4 @@ void FSEventWrap::Close(const FunctionCallbackInfo<Value>& args) {
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_fs_event_wrap, node::FSEventWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs_event_wrap, node::FSEventWrap::Initialize)

View File

@ -130,6 +130,9 @@ static bool use_debug_agent = false;
static bool debug_wait_connect = false;
static int debug_port = 5858;
static bool v8_is_profiling = false;
static node_module* modpending;
static node_module* modlist_builtin;
static node_module* modlist_addon;
// used by C++ modules as well
bool no_deprecation = false;
@ -1882,6 +1885,29 @@ void Hrtime(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(tuple);
}
extern "C" void node_module_register(void* m) {
struct node_module* mp = reinterpret_cast<struct node_module*>(m);
if (mp->nm_flags & NM_F_BUILTIN) {
mp->nm_link = modlist_builtin;
modlist_builtin = mp;
} else {
assert(modpending == NULL);
modpending = mp;
}
}
struct node_module* get_builtin_module(const char* name) {
struct node_module* mp;
for (mp = modlist_builtin; mp != NULL; mp = mp->nm_link) {
if (strcmp(mp->nm_modname, name) == 0)
break;
}
assert(mp == NULL || (mp->nm_flags & NM_F_BUILTIN) != 0);
return (mp);
}
typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
@ -1894,12 +1920,12 @@ typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
void DLOpen(const FunctionCallbackInfo<Value>& args) {
HandleScope handle_scope(args.GetIsolate());
Environment* env = Environment::GetCurrent(args.GetIsolate());
char symbol[1024], *base, *pos;
struct node_module* mp;
uv_lib_t lib;
int r;
if (args.Length() < 2) {
return ThrowError("process.dlopen takes exactly 2 arguments.");
ThrowError("process.dlopen takes exactly 2 arguments.");
return;
}
Local<Object> module = args[0]->ToObject(); // Cast
@ -1918,68 +1944,43 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
return;
}
String::Utf8Value path(args[1]);
base = *path;
/* Find the shared library filename within the full path. */
#ifdef __POSIX__
pos = strrchr(base, '/');
if (pos != NULL) {
base = pos + 1;
}
#else // Windows
for (;;) {
pos = strpbrk(base, "\\/:");
if (pos == NULL) {
break;
}
base = pos + 1;
}
#endif // __POSIX__
/* Strip the .node extension. */
pos = strrchr(base, '.');
if (pos != NULL) {
*pos = '\0';
}
/* Add the `_module` suffix to the extension name. */
r = snprintf(symbol, sizeof symbol, "%s_module", base);
if (r <= 0 || static_cast<size_t>(r) >= sizeof symbol) {
return ThrowError("Out of memory.");
}
/* Replace dashes with underscores. When loading foo-bar.node,
* look for foo_bar_module, not foo-bar_module.
/*
* Objects containing v14 or later modules will have registered themselves
* on the pending list. Activate all of them now. At present, only one
* module per object is supported.
*/
for (pos = symbol; *pos != '\0'; ++pos) {
if (*pos == '-')
*pos = '_';
}
mp = modpending;
modpending = NULL;
node_module_struct *mod;
if (uv_dlsym(&lib, symbol, reinterpret_cast<void**>(&mod))) {
char errmsg[1024];
snprintf(errmsg, sizeof(errmsg), "Symbol %s not found.", symbol);
return ThrowError(errmsg);
if (mp == NULL) {
ThrowError("Module did not self-register.");
return;
}
if (mod->version != NODE_MODULE_VERSION) {
if (mp->nm_version != NODE_MODULE_VERSION) {
char errmsg[1024];
snprintf(errmsg,
sizeof(errmsg),
"Module version mismatch. Expected %d, got %d.",
NODE_MODULE_VERSION, mod->version);
return ThrowError(errmsg);
NODE_MODULE_VERSION, mp->nm_version);
ThrowError(errmsg);
return;
}
if (mp->nm_flags & NM_F_BUILTIN) {
ThrowError("Built-in module self-registered.");
return;
}
// Execute the C++ module
if (mod->register_context_func != NULL) {
mod->register_context_func(exports, module, env->context());
} else if (mod->register_func != NULL) {
mod->register_func(exports, module);
mp->nm_dso_handle = lib.handle;
mp->nm_link = modlist_addon;
modlist_addon = mp;
if (mp->nm_context_register_func != NULL) {
mp->nm_context_register_func(exports, module, env->context(), mp->nm_priv);
} else if (mp->nm_register_func != NULL) {
mp->nm_register_func(exports, module, mp->nm_priv);
} else {
return ThrowError("Module has no declared entry point.");
ThrowError("Module has no declared entry point.");
return;
}
// Tell coverity that 'handle' should not be freed when we return.
@ -2082,14 +2083,15 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
uint32_t l = modules->Length();
modules->Set(l, OneByteString(node_isolate, buf));
node_module_struct* mod = get_builtin_module(*module_v);
node_module* mod = get_builtin_module(*module_v);
if (mod != NULL) {
exports = Object::New();
// Internal bindings don't have a "module" object, only exports.
assert(mod->register_func == NULL);
assert(mod->register_context_func != NULL);
assert(mod->nm_register_func == NULL);
assert(mod->nm_context_register_func != NULL);
Local<Value> unused = Undefined(env->isolate());
mod->register_context_func(exports, unused, env->context());
mod->nm_context_register_func(exports, unused,
env->context(), mod->nm_priv);
cache->Set(module, exports);
} else if (!strcmp(*module_v, "constants")) {
exports = Object::New();

View File

@ -205,28 +205,32 @@ const char *signo_string(int errorno);
typedef void (*addon_register_func)(
v8::Handle<v8::Object> exports,
v8::Handle<v8::Value> module);
v8::Handle<v8::Value> module,
void* priv);
typedef void (*addon_context_register_func)(
v8::Handle<v8::Object> exports,
v8::Handle<v8::Value> module,
v8::Handle<v8::Context> context);
v8::Handle<v8::Context> context,
void* priv);
struct node_module_struct {
int version;
void *dso_handle;
const char *filename;
node::addon_register_func register_func;
node::addon_context_register_func register_context_func;
const char *modname;
#define NM_F_BUILTIN 0x01
struct node_module {
int nm_version;
unsigned int nm_flags;
void* nm_dso_handle;
const char* nm_filename;
node::addon_register_func nm_register_func;
node::addon_context_register_func nm_context_register_func;
const char* nm_modname;
void* nm_priv;
struct node_module* nm_link;
};
node_module_struct* get_builtin_module(const char *name);
node_module* get_builtin_module(const char *name);
#define NODE_STANDARD_MODULE_STUFF \
NODE_MODULE_VERSION, \
NULL, \
__FILE__
extern "C" NODE_EXTERN void node_module_register(void* mod);
#ifdef _WIN32
# define NODE_MODULE_EXPORT __declspec(dllexport)
@ -234,30 +238,70 @@ node_module_struct* get_builtin_module(const char *name);
# define NODE_MODULE_EXPORT /* empty */
#endif
#define NODE_MODULE(modname, regfunc) \
#if defined(_MSC_VER)
#pragma section(".CRT$XCU", read)
#define NODE_C_CTOR(fn) \
static void __cdecl fn(void); \
__declspec(allocate(".CRT$XCU")) static void (__cdecl*fn ## _)(void) = fn; \
static void __cdecl fn(void)
#else
#define NODE_C_CTOR(fn) \
static void fn(void) __attribute__((constructor)); \
static void fn(void)
#endif
#define NODE_MODULE_X(modstr, regfunc, priv, flags) \
extern "C" { \
NODE_MODULE_EXPORT node::node_module_struct modname ## _module = \
static node::node_module _module = \
{ \
NODE_STANDARD_MODULE_STUFF, \
NODE_MODULE_VERSION, \
flags, \
NULL, \
__FILE__, \
(node::addon_register_func) (regfunc), \
NULL, \
NODE_STRINGIFY(modname) \
modstr, \
priv, \
NULL \
}; \
NODE_C_CTOR(_register) { \
node_module_register(&_module); \
} \
}
#define NODE_MODULE_CONTEXT_AWARE_X(modstr, regfunc, priv, flags) \
extern "C" { \
static node::node_module _module = \
{ \
NODE_MODULE_VERSION, \
flags, \
NULL, \
__FILE__, \
NULL, \
(node::addon_context_register_func) (regfunc), \
modstr, \
priv, \
NULL \
}; \
NODE_C_CTOR(_register) { \
node_module_register(&_module); \
} \
}
#define NODE_MODULE(modname, regfunc) \
NODE_MODULE_X(NODE_STRINGIFY(modname), regfunc, NULL, 0)
#define NODE_MODULE_CONTEXT_AWARE(modname, regfunc) \
extern "C" { \
NODE_MODULE_EXPORT node::node_module_struct modname ## _module = \
{ \
NODE_STANDARD_MODULE_STUFF, \
NULL, \
(regfunc), \
NODE_STRINGIFY(modname) \
}; \
}
NODE_MODULE_CONTEXT_AWARE_X(NODE_STRINGIFY(modname), regfunc, NULL, 0)
#define NODE_MODULE_DECL(modname) \
extern "C" node::node_module_struct modname ## _module;
#define NODE_MODULE_CONTEXT_AWARE_BUILTIN(modname, regfunc) \
NODE_MODULE_CONTEXT_AWARE_X(NODE_STRINGIFY(modname), \
regfunc, NULL, NM_F_BUILTIN)
/*
* For backward compatibility in add-on modules.
*/
#define NODE_MODULE_DECL /* nothing */
/* Called after the event loop exits but before the VM is disposed.
* Callbacks are run in reverse order of registration, i.e. newest first.

View File

@ -665,4 +665,4 @@ void Initialize(Handle<Object> target,
} // namespace Buffer
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_buffer, node::Buffer::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(buffer, node::Buffer::Initialize)

View File

@ -628,4 +628,4 @@ void InitContextify(Handle<Object> target,
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_contextify, node::InitContextify);
NODE_MODULE_CONTEXT_AWARE_BUILTIN(contextify, node::InitContextify);

View File

@ -4210,7 +4210,8 @@ void SetEngine(const FunctionCallbackInfo<Value>& args) {
// FIXME(bnoordhuis) Handle global init correctly.
void InitCrypto(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Handle<Context> context,
void* priv) {
static uv_once_t init_once = UV_ONCE_INIT;
uv_once(&init_once, InitCryptoOnce);
@ -4239,4 +4240,4 @@ void InitCrypto(Handle<Object> target,
} // namespace crypto
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_crypto, node::crypto::InitCrypto)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(crypto, node::crypto::InitCrypto)

View File

@ -1,68 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "node.h"
#include "node_version.h"
#include "node_extensions.h"
#include <string.h>
#include <stdio.h>
#undef NODE_EXT_LIST_START
#undef NODE_EXT_LIST_ITEM
#undef NODE_EXT_LIST_END
#define NODE_EXT_LIST_START
#define NODE_EXT_LIST_ITEM NODE_MODULE_DECL
#define NODE_EXT_LIST_END
NODE_EXT_LIST(NODE_EXT_LIST_START, NODE_EXT_LIST_ITEM, NODE_EXT_LIST_END)
#undef NODE_EXT_LIST_START
#undef NODE_EXT_LIST_ITEM
#undef NODE_EXT_LIST_END
#define NODE_EXT_STRING(x) &x ## _module,
#define NODE_EXT_LIST_START node::node_module_struct *node_module_list[] = {
#define NODE_EXT_LIST_ITEM NODE_EXT_STRING
#define NODE_EXT_LIST_END NULL};
NODE_EXT_LIST(NODE_EXT_LIST_START, NODE_EXT_LIST_ITEM, NODE_EXT_LIST_END)
namespace node {
node_module_struct* get_builtin_module(const char *name) {
char buf[128];
node_module_struct *cur = NULL;
snprintf(buf, sizeof(buf), "node_%s", name);
/* TODO: you could look these up in a hash, but there are only
* a few, and once loaded they are cached. */
for (int i = 0; node_module_list[i] != NULL; i++) {
cur = node_module_list[i];
if (strcmp(cur->modname, buf) == 0) {
return cur;
}
}
return NULL;
}
} // namespace node

View File

@ -1,57 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef SRC_NODE_EXTENSIONS_H_
#define SRC_NODE_EXTENSIONS_H_
#if HAVE_OPENSSL
# define NODE_EXT_LIST_SSL(ITEM) \
ITEM(node_crypto) \
ITEM(node_tls_wrap)
#else
# define NODE_EXT_LIST_SSL(ITEM)
#endif // HAVE_OPENSSL
#define NODE_EXT_LIST(START, ITEM, END) \
START \
ITEM(node_buffer) \
NODE_EXT_LIST_SSL(ITEM) \
ITEM(node_contextify) \
ITEM(node_fs) \
ITEM(node_http_parser) \
ITEM(node_os) \
ITEM(node_smalloc) \
ITEM(node_zlib) \
\
ITEM(node_uv) \
ITEM(node_timer_wrap) \
ITEM(node_tcp_wrap) \
ITEM(node_udp_wrap) \
ITEM(node_pipe_wrap) \
ITEM(node_cares_wrap) \
ITEM(node_tty_wrap) \
ITEM(node_process_wrap) \
ITEM(node_fs_event_wrap) \
ITEM(node_signal_wrap) \
\
END \
#endif // SRC_NODE_EXTENSIONS_H_

View File

@ -1053,7 +1053,8 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
void InitFs(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Handle<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
// Initialize the stats object
@ -1097,4 +1098,4 @@ void InitFs(Handle<Object> target,
} // end namespace node
NODE_MODULE_CONTEXT_AWARE(node_fs, node::InitFs)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs, node::InitFs)

View File

@ -563,7 +563,8 @@ const struct http_parser_settings Parser::settings = {
void InitHttpParser(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Handle<Context> context,
void* priv) {
Local<FunctionTemplate> t = FunctionTemplate::New(Parser::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(FIXED_ONE_BYTE_STRING(node_isolate, "HTTPParser"));
@ -600,4 +601,4 @@ void InitHttpParser(Handle<Object> target,
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_http_parser, node::InitHttpParser)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(http_parser, node::InitHttpParser)

View File

@ -302,4 +302,4 @@ void Initialize(Handle<Object> target,
} // namespace os
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_os, node::os::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(os, node::os::Initialize)

View File

@ -64,6 +64,6 @@
* an API is broken in the C++ side, including in v8 or
* other dependencies.
*/
#define NODE_MODULE_VERSION 13 /* v0.12 */
#define NODE_MODULE_VERSION 14 /* v0.12 */
#endif /* SRC_NODE_VERSION_H_ */

View File

@ -550,7 +550,8 @@ class ZCtx : public AsyncWrap {
void InitZlib(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Handle<Context> context,
void* priv) {
Local<FunctionTemplate> z = FunctionTemplate::New(ZCtx::New);
z->InstanceTemplate()->SetInternalFieldCount(1);
@ -608,4 +609,4 @@ void InitZlib(Handle<Object> target,
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_zlib, node::InitZlib)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(zlib, node::InitZlib)

View File

@ -288,4 +288,4 @@ void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_pipe_wrap, node::PipeWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(pipe_wrap, node::PipeWrap::Initialize)

View File

@ -293,4 +293,4 @@ class ProcessWrap : public HandleWrap {
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_process_wrap, node::ProcessWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(process_wrap, node::ProcessWrap::Initialize)

View File

@ -114,4 +114,4 @@ class SignalWrap : public HandleWrap {
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_signal_wrap, node::SignalWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(signal_wrap, node::SignalWrap::Initialize)

View File

@ -496,4 +496,4 @@ void Initialize(Handle<Object> exports,
} // namespace smalloc
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_smalloc, node::smalloc::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(smalloc, node::smalloc::Initialize)

View File

@ -465,4 +465,4 @@ Local<Object> AddressToJS(Environment* env,
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_tcp_wrap, node::TCPWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(tcp_wrap, node::TCPWrap::Initialize)

View File

@ -157,4 +157,4 @@ class TimerWrap : public HandleWrap {
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_timer_wrap, node::TimerWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)

View File

@ -802,4 +802,4 @@ void TLSCallbacks::Initialize(Handle<Object> target,
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_tls_wrap, node::TLSCallbacks::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(tls_wrap, node::TLSCallbacks::Initialize)

View File

@ -180,4 +180,4 @@ TTYWrap::TTYWrap(Environment* env, Handle<Object> object, int fd, bool readable)
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_tty_wrap, node::TTYWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(tty_wrap, node::TTYWrap::Initialize)

View File

@ -436,4 +436,4 @@ uv_udp_t* UDPWrap::UVHandle() {
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_udp_wrap, node::UDPWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(udp_wrap, node::UDPWrap::Initialize)

View File

@ -62,4 +62,4 @@ void Initialize(Handle<Object> target,
} // namespace uv
} // namespace node
NODE_MODULE_CONTEXT_AWARE(node_uv, node::uv::Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(uv, node::uv::Initialize)