I know that internalBindings is used in nodejs to help javascript code introduce the c++ module, but in the end v8 will compile javascript into ast. At this time, ast contains both the content of the javascript language and the content referenced by c++.
When executing ast, I wonder if it is parsed When reaching the content referenced by c++, first run the c++ code and then return the result, so that JavaScript can be fully integrated into the c++ code. Is this how v8 runs js code?
2
Answers
You are mistaken about the nature of native bindings. There is no JS code referencing C++ code, and there is no AST that contains code content in both languages. Only JavaScript code is parsed into the AST.
When calling from JS into C++, it is a JS object that references a C++ object and calls its methods. The internal bindings of nodejs are set up by C++ code, creating JS object that represent native resources and JS function objects that call a C++ function.
Like Bergi mentioned the nature of native biding is different and it happens at binary "level" in what we call an ABI (Application Binary Interface). Basically it is the runtime that knows how to deal with objects written in both languages because they respect the same binary structure and interface.
NodeJS allows you to write C++ addons but it typically provides a Node-API interface in C which can be used with a C++ wrapper that provides a C++ object model and exception handling semantics with low overhead.