0

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);

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

does js2c.py convert all the "required" modules in our js code to its c/c++ equivalent? What about the code that we write on top of the node runtime environment? Does js2c.py take care of translating it as well?
js2c.py doesn't change the Logic in JS program to equivalent Logic in C++ program. It outputs the string representation of JS code stored in character array. which could potentially be compiled with another c++ program. For eg, if the JS code was var foo = 'bar' js2c.py will output something like: char prog[] = { 'v', 'a', 'r', ' ', '=', ' ', '\', 'b', 'a', 'r' };. This is still only a string representation of JS code in c/c++ syntax. This will still have to be evaluated and executed by a JS engine. User's JavaScript code does not go through any such transformation before execution.
Ah...!!! What about npm modules referenced in our code? Do they get translated the same way as well?
And what about an application source code? Does JavaScript source code get converted to C-style char arrays as well? Thanks for answering my questions btw :)
js2c.py does not come into picture during normal execution of any NodeJS program. AAMOF, in regular execution of JS files using NodeJS, python is not needed at all (js2c.py is a python script). Its is only used at compile time by c++ module developers who need to keep JS snippets in c++ code which can be evaluated as JavaScript by v8 in runtime. The script itself plays no role at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.