简体   繁体   中英

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? If yes, are these modules packaged as js files. Are the core modules that are referenced within our code converted to c/c++ code first and then executed? 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 ). So how does node link a method in javascript with the one defined in the c/c++ file?

here is the extract from the _tls_common.js file that makes use of the "loadPKCS12" method:

 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?". Let's take them one by one.

How the core modules work?

The core modules are packaged with NodeJS binary. And, while they are packaged with the binary, they are not converted to c++ code before packaging. 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. The actual loading of the internal module obviously happens in c++ code .

How does NodeJS let c++ code get referenced in 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. 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.

Your example for use of c++ function in JS ( loadPKCS12 ), however, is more special case of internal c++ functionality of NodeJS. loadPKCS12 is called on a object of SecureContext imported from crypto c++ module. 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. At the last line in node_crypto.cc file, initializer for internal module crypto is registered. 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 . Eventually these exported functionalities from C++ world are imported and used in JS-World using internalBinding .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM