美文网首页
内置C++模块的执行

内置C++模块的执行

作者: orgcheng | 来源:发表于2020-03-27 15:31 被阅读0次

可以搜索C++内置模块的注册列表变量modlist_internal

// node_binding.cc文件

// 留意这个方法名
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
  Environment* env = Environment::GetCurrent(args);

  CHECK(args[0]->IsString());

  Local<String> module = args[0].As<String>();
  node::Utf8Value module_v(env->isolate(), module);
  Local<Object> exports;

  node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL);
  if (mod != nullptr) {
    // 走这里
    exports = InitModule(env, mod, module);
  } else if (!strcmp(*module_v, "constants")) {
    exports = Object::New(env->isolate());
    CHECK(
        exports->SetPrototype(env->context(), Null(env->isolate())).FromJust());
    DefineConstants(env->isolate(), exports);
  } else if (!strcmp(*module_v, "natives")) {
    exports = native_module::NativeModuleEnv::GetSourceObject(env->context());
    // Legacy feature: process.binding('natives').config contains stringified
    // config.gypi
    CHECK(exports
              ->Set(env->context(),
                    env->config_string(),
                    native_module::NativeModuleEnv::GetConfigString(
                        env->isolate()))
              .FromJust());
  } else {
    return ThrowIfNoSuchModule(env, *module_v);
  }

  args.GetReturnValue().Set(exports);
}

// 执行模块对应的注册函数
static Local<Object> InitModule(Environment* env,
                                node_module* mod,
                                Local<String> module) {
  Local<Object> exports = Object::New(env->isolate());
  // Internal bindings don't have a "module" object, only exports.
  CHECK_NULL(mod->nm_register_func);
  CHECK_NOT_NULL(mod->nm_context_register_func);
  Local<Value> unused = Undefined(env->isolate());
  mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv);
  return exports;
}

看下GetLinkedBinding这个方法什么实际调用,全局搜索下

// node.cc文件名

// TODO chengzhen Step24
MaybeLocal<Value> Environment::BootstrapInternalLoaders() {
  EscapableHandleScope scope(isolate_);

  // Create binding loaders
  std::vector<Local<String>> loaders_params = {
      process_string(),
      FIXED_ONE_BYTE_STRING(isolate_, "getLinkedBinding"),
      FIXED_ONE_BYTE_STRING(isolate_, "getInternalBinding"),
      primordials_string()};
  std::vector<Local<Value>> loaders_args = {
      process_object(),
      NewFunctionTemplate(binding::GetLinkedBinding)
          ->GetFunction(context())
          .ToLocalChecked(),
      NewFunctionTemplate(binding::GetInternalBinding)
          ->GetFunction(context())
          .ToLocalChecked(),
      primordials()};

// TODO chengzhen Step25,执行loader.js文件,然后导出对象
// 调用时传递了四个参数
/* global process, getLinkedBinding, getInternalBinding, primordials */

  // Bootstrap internal loaders
  Local<Value> loader_exports;
  if (!ExecuteBootstrapper(
           this, "internal/bootstrap/loaders", &loaders_params, &loaders_args)
           .ToLocal(&loader_exports)) {
    return MaybeLocal<Value>();
  }

// TODO chengzhen
/**   
 *   env.h中有这样的定义
 *   V(internal_binding_string, "internalBinding")
 *   V(internal_binding_loader, v8::Function)
 * 
 *   V(require_string, "require")       
 *   V(native_module_require, v8::Function)             
 */

  CHECK(loader_exports->IsObject());
  Local<Object> loader_exports_obj = loader_exports.As<Object>();
  Local<Value> internal_binding_loader =
      loader_exports_obj->Get(context(), internal_binding_string())
          .ToLocalChecked();
  CHECK(internal_binding_loader->IsFunction());
  set_internal_binding_loader(internal_binding_loader.As<Function>());
// TODO chengzhen 从loader_exports中取出internalBinding对象,这个对象是函数
// 然后设置到internal_binding_loader_属性中, 用来加载核心C++模块的

  Local<Value> require =
      loader_exports_obj->Get(context(), require_string()).ToLocalChecked();
  CHECK(require->IsFunction());
  set_native_module_require(require.As<Function>());
// TODO chengzhen 从loader_exports中取出require对象,这个对象是函数
// 然后设置到native_module_require_属性中,用来加载核心JS模块的
  return scope.Escape(loader_exports);
}

相关文章

  • 内置C++模块的执行

    可以搜索C++内置模块的注册列表变量modlist_internal 看下GetLinkedBinding这个方法...

  • 内置C++模块的注册

    node.cc类的InitializeNodeWithArgs方法中,调用了binding::RegisterBu...

  • Python获取重定向输入

    通过内置的fileinput模块即可实现,创建文件filein.py: 增加可执行权限: 使用:

  • python想引用多个模块避免重复引用

    今天学习了init.py的用法这个文件是不论你执行那个文件他都会执行比如说我想引用内置os模块和sys模块但是我现...

  • node模块载入机制

    node内模块以及载入顺序为: 内置模块 文件模块 文件目录模块 node_modules模块 内置模块 http...

  • Django自定义过滤器及标签

    自定义的引入:内置函数>>>>>>>>>>>>>>>自定义函数内置模块>>>>>>>>>>>>>>>自定义模块内置...

  • python基础篇07-日期模块

    python内置了多个用于日期和时间进行操作的内置模块:time模块,datetime模块和calendar模块。...

  • 四、node(二)

    node模块 文件模块内置模块第三方模块 内置模块 util util.inherits(Child,Parent...

  • Revit开发途径

    1.内置开发环境 模块:必须包含module_starup(在加载更改时执行),module_shutdown(在...

  • 文档测试

    Python内置的“文档测试”(doctest)模块可以直接提取注释中的代码并执行测试。doctest严格按照Py...

网友评论

      本文标题:内置C++模块的执行

      本文链接:https://www.haomeiwen.com/subject/sgiluhtx.html