node. JS source code parsing----to implement a node. JS's Difficulties and ideas

Source: Internet
Author: User
Tags first string

Objective:

Recently looking at node. js and looking at it for a while and thinking about implementing a node. JS has now been realized ( mostly imitating others , But to realize it once and basically understand the principle of node. js, here is the pit in this process, and some places to be noticed;

node. js needs a certain C + + basis, it is recommended to read C++primer again, otherwise V8 a lot of expression, pointers, references, templates and the like will not understand;

Code uploaded github Address: Https://github.com/sven36/cNode

Compile: I use the WIN10 environment, the specific compilation please refer to node. JS's compilation instructions: HTTPS://GITHUB.COM/NODEJS/NODE/BLOB/MASTER/BUILDING.MD

where the pit: Python should be 2.6 or 2.7, do not install 3.0 or more, because node. JS has a PY File 3.0 compilation error ; Visual C + + Build tools must be 2015, the official installation link is, Because node. JS is compiled on the Windows platform using the VS2015 v140 platform toolset, the v141 platform toolset with the lower version or vs2017 will be error -based.

Compilation process: After installing Python and Visual C + + build Tools2015, download node. js source node-v6.10.0.tar.gz and use Visual C + + build tools 2015 command line runs the Vcbuild.bat processing file in the extracted directory, and it will start compiling; the. sln file can be used when the build succeeds (vs also needs to be 2015 or 2017, and the Platform tool set of the project also needs to be v140 or compile error );

realize a node. JS's Difficulties and ideas:

V8 Engine:

Because node. JS is actually a C + + program embedded in V8, the first thing to know about the concepts of V8 Isolate,localhandle,scope, which is not expanded here, please refer to this document:

Https://github.com/Chunlin-Li/Chunlin-Li.github.io/blob/master/blogs/javascript/V8_Embedder ' s_guide_chs.md

In my code I also added some comments, in the SRC directory in the node.cpp file, you can refer to;

C + + compilation differs from the usual approach to object-oriented compilation :

In languages such as. NET or Java, you do not have to focus on the declaration order of the methods, such as:

private void A () {

b ();

}

private void B () {

Console.log (1);

}

However, in C + + This is not possible, before invoking B, you must completely declare B; that is, put B before method A ( which is also why the Start method in the Node.cpp file is placed at the bottom );

Why is node. JS's header file wrapped with namespace node:

is to better decouple the various modules of node. js, where namespaces are the same in C + + and internal member names are different, they are automatically merged into the same namespace, which can be understood as append;

More complex macro definitions inside node. JS:

node. js and V8 use a lot of complex macro definitions, which can be laborious if not understood; in C + +, the # #符号是连接字符串的意思 in the macro definition;

For example: the macro definition under the Env-inl.h file:

#define VP (PropertyName, StringValue) V (V8::P rivate, PropertyName, StringValue)
#define VS (PropertyName, StringValue) V (v8::string, PropertyName, StringValue)
#define V (TypeName, PropertyName, stringvalue) \
inline \
V8::local<typename> environment::isolatedata::P ropertyname () const {\
/* Strings is immutable so casting away const-ness this is okay. */      \
Return const_cast<isolatedata*> (This)->propertyname # # _.  Get (Isolate ()); \
}
Per_isolate_private_symbol_properties (VP)
Per_isolate_string_properties (VS)
#undef V
#undef VS
VP #undef

Its final compile form is:

Inline v8::local<v8::string> environment::isolatedata::async_queue_string () const {
Return const_cast<isolatedata*> (This)->async_queue_string_. Get (Isolate ());
}

This place more patience and careful analysis will understand; I wrote the code inside basically this macro definition of the first string I was this handwriting, the rest is in accordance with the original macro definition of the way the declaration, you can refer to;

What to look for when using vs compilation :

The node. JS project file is actually produced using Google's Gyp tool, so the VS Project compilation process is also within the. gyp file under each project.

For more information on Gyp tools, please refer to: http://www.cnblogs.com/nanvann/p/3913880.html#conditions

Of course, there are a lot of specific C + + problems need to rely on their own more thinking, diligent search;

node. JS source code parsing----to implement a node. JS's Difficulties and ideas

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.