Skip to content

Linked native module initialization function receives module name instead of module object #4756

Closed
@kaero

Description

@kaero

Linked and built-in native modules initialization function receive v8::String as second argument instead of v8::Object instance of Module. It makes impossible to export something except precreated exports object.

Dynamic modules initialization:
https://github.com/nodejs/node/blob/v5.4.1/src/node.cc#L2193-L2268

void DLOpen(const FunctionCallbackInfo<Value>& args) {
  // ...
  Local<Object> module = args[0]->ToObject(env->isolate());  // Cast
  // ...
  Local<Object> exports = module->Get(exports_string)->ToObject(env->isolate());

  if (mp->nm_context_register_func != nullptr) {
    mp->nm_context_register_func(exports, module, env->context(), mp->nm_priv);
  } else if (mp->nm_register_func != nullptr) {
    mp->nm_register_func(exports, module, mp->nm_priv);
  } 
  // ...
}

Linked modules initalization:
https://github.com/nodejs/node/blob/v5.4.1/src/node.cc#L2400-L2428

static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
  // ...
  Local<String> module = args[0]->ToString(env->isolate());
  // ...
  Local<Object> exports = Object::New(env->isolate());
  if (mod->nm_context_register_func != nullptr) {
    mod->nm_context_register_func(exports,
                                  module,
                                  env->context(),
                                  mod->nm_priv);
  } else if (mod->nm_register_func != nullptr) {
    mod->nm_register_func(exports, module, mod->nm_priv);
  }
  // ...
  cache->Set(module, exports);
  args.GetReturnValue().Set(exports);
}

nodejs/help#44

\cc @indutny

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++Issues and PRs that require attention from people who are familiar with C++.moduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions