[英]How do the core modules in node.js work? (https://github.com/nodejs/node/blob/master/lib)
Does the node interpreter look for core modules (let's say "fs") within the node binary? 节点解释器是否在节点二进制文件中寻找核心模块(比如说“ fs”)? If yes, are these modules packaged as js files.
如果是,这些模块是否打包为js文件。 Are the core modules that are referenced within our code converted to c/c++ code first and then executed?
我们的代码中引用的核心模块是否先转换为c / c ++代码然后执行? For example, I see a method in the _tls_common.js ( https://github.com/nodejs/node/blob/master/lib/_tls_common.js ) file called "loadPKCS12" and the only place that I see this method being referenced/defined is within the "node_crypto.cc" file ( https://github.com/nodejs/node/blob/master/src/node_crypto.cc ).
例如,我在_tls_common.js( https://github.com/nodejs/node/blob/master/lib/_tls_common.js )文件中看到一个名为“ loadPKCS12”的方法,而我唯一看到此方法的地方引用/定义在“ node_crypto.cc”文件中( https://github.com/nodejs/node/blob/master/src/node_crypto.cc )。 So how does node link a method in javascript with the one defined in the c/c++ file?
那么,节点如何将javascript中的方法与c / c ++文件中定义的方法链接起来?
here is the extract from the _tls_common.js file that makes use of the "loadPKCS12" method: 以下是使用“ loadPKCS12”方法的_tls_common.js文件的摘录:
if (passphrase) {
c.context.loadPKCS12(buf, toBuf(passphrase));
} else {
c.context.loadPKCS12(buf);
}
}
} else {
const buf = toBuf(options.pfx);
const passphrase = options.passphrase;
if (passphrase) {
c.context.loadPKCS12(buf, toBuf(passphrase));
} else {
c.context.loadPKCS12(buf);
There are two different (but seemingly related) questions asked here. 这里有两个不同(但看似相关)的问题。 The first one is: "How the core modules work?".
第一个是:“核心模块如何工作?”。 Second one being "How does NodeJS let c++ code get referenced and executed in JavaScript?".
第二个是“ NodeJS如何让c ++代码在JavaScript中被引用和执行?”。 Let's take them one by one.
让我们一一介绍。
How the core modules work? 核心模块如何工作?
The core modules are packaged with NodeJS binary. 核心模块与NodeJS二进制文件一起打包。 And, while they are packaged with the binary, they are not converted to c++ code before packaging.
而且,尽管它们与二进制文件一起打包,但打包之前不会转换为c ++代码。 The internal modules are loaded into memory during bootstrap of the node process.
内部模块在节点进程的引导过程中被加载到内存中。 When a program executes, lets say
require('fs')
, the require function simply returns the already loaded module from cache. 当程序执行时,可以说
require('fs')
,require函数只是从缓存中返回已经加载的模块。 The actual loading of the internal module obviously happens in c++ code . 内部模块的实际加载显然是在c ++代码中进行的 。
How does NodeJS let c++ code get referenced in JS? NodeJS如何让c ++代码在JS中被引用?
This ability comes partly from V8 engine which exposes the ability to create and manage JS constructs in C++, and partly from NodeJS / LibUV which create a wrapper on top of V8 to provide the execution environment. 此功能部分来自V8引擎,该引擎提供了在C ++中创建和管理JS构造的功能,另一部分来自NodeJS / LibUV,后者在V8之上创建了一个包装器以提供执行环境。 The documentation about such node modules can be accessed here .
可以在此处访问有关此类节点模块的文档。 As the documentation states, these c++ modules can be used in JS file by requiring them, like any other ordinary JS module.
如文档所述,这些c ++模块可以像其他任何普通JS模块一样通过要求在JS文件中使用。
Your example for use of c++ function in JS ( loadPKCS12
), however, is more special case of internal c++ functionality of NodeJS. 但是,您在JS中使用c ++函数的示例(
loadPKCS12
)是loadPKCS12
内部c ++功能的更特殊情况。 loadPKCS12
is called on a object of SecureContext
imported from crypto
c++ module. 从
crypto
c ++模块导入的SecureContext
对象上调用loadPKCS12
。 If you follow the link to SecureContext import in _tls_common.js
above, you will see that the crypto is not loaded using require()
, instead a special (global) method internalBinding
is used to obtain the reference. 如果您
_tls_common.js
上面_tls_common.js
SecureContext导入的链接,您将看到未使用require()
加载加密_tls_common.js
,而是使用特殊的(全局)方法internalBinding
获取引用。 At the last line in node_crypto.cc
file, initializer for internal module crypto
is registered. 在
node_crypto.cc
文件的最后一行 ,注册了内部模块crypto
初始化程序。 Following the chain of initialization, node::crypto::Initialize
calls node::crypto::SecureContext::Initialize
which creates a function template, assigns the appropriate prototype methods and exports it on target
. 按照初始化链,
node::crypto::Initialize
调用node::crypto::SecureContext::Initialize
创建一个功能模板,分配适当的原型方法并将其导出到target
。 Eventually these exported functionalities from C++ world are imported and used in JS-World using internalBinding
. 最终,这些来自C ++ world的导出功能通过
internalBinding
导入并在JS-World中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.