Home Web Front-end JS Tutorial Node explains the process analysis of executing js

Node explains the process analysis of executing js

Apr 03, 2018 pm 02:41 PM
javascript node analyze

The content of this article is to share with you the analysis of the process of node explaining and executing js. Friends who are interested can take a look, and friends in need can also refer to it

Explanation: node is single-threaded. Non-blocking, event-driven (similar to udev events in the kernel, you can refer to the listening-callback mechanism)

Take node-v8.10.0 as the object, mainly src/node_main.cc and src/node.cc document.

  1. Entry
    node-v8.10.0/src/node_main.cc --> 90 int main(int argc, char *argv[])
    Call node::Start( argc, argv);
    node-v8.10.0/src/node.cc --> 4863 int Start(int argc, char** argv)
    a: 4864 atexit([] () { uv_tty_reset_mode( ); });
    # Execute the anonymous function after executing *.js, which is actually executing uv_tty_reset_mode()
    b: 4865 PlatformInit();
    # Execute the inline function PlatformInit(), signal Volume processing function registration
    c: 4866 node::performance::performance_node_start = PERFORMANCE_NOW();
    Encapsulate the uv_hrtime function: src/node_perf_common.h:13:#define PERFORMANCE_NOW() uv_hrtime()
    Export definition :deps/uv/include/uv.h:1457:UV_EXTERN uint64_t uv_hrtime(void);
    Implementation: deps/uv/src/unix/core.c:111:uint64_t uv_hrtime(void)
    uv_hrtime calls uv__hrtime
    Definition: deps/uv/src/unix/internal.h:252:uint64_t uv__hrtime(uv_clocktype_t type);
    Implementation: deps/uv/src/unix/linux-core.c:442:uint64_t uv__hrtime( UV_CLOCKTYPE_T Type) {
    In short: Record the starting time point of the node execution*.js script. Similar, the starting time point of the V8:
    4903 node :: Performance_v8_start = Performance_now;
    # d: 4868 CHECK_GT(argc, 0);
    src/util.h:129:#define CHECK_GT(a, b) CHECK((a) > (b))
    e: 4871 argv = uv_setup_args(argc, argv);
    Definition: deps/uv/include/uv.h:1051:UV_EXTERN char** uv_setup_args(int argc, char** argv);
    Implementation:
    f: 4877 Init(&argc, const_cast(argv), &exec_argc, &exec_argv);
    4542 void Init(int* argc,
    4543 const char** argv,
    4544 int* exec_argc,
    4545 const char*** exec_argv) {
    4617 ProcessArgv(argc, argv, exec_argc, exec_argv);
    4502 ParseArgs(argc, argv, exec _argc, exec_argv, &v8_argc, &v8_argv, is_env);
    4015 Static Void Parseargs (Int* ARGC,
    Analysis parameter
    G: Openssl related configuration
    H: 4895 v8_platform.Initialize
    i: 4902 v8 :: initialize ();
    v8 initialization
    j: 4905 const int exit_code =
    4906 Start(uv_default_loop(), argc, argv, exec_argc, exec_argv);
    k: Exit
        4908   v8_platform.StopTracingAgent ();
    4910 v8_initialized = false;
    4911 V8::Dispose();
    4919 v8_platform.Dispose();
    4921 delete[] exec_argv;
    4922 exec_argv = nullptr;
    4924 return exit_code;
    2. Analyze part j in 1
    a: 4814 inline int Start(uv_loop_t* event_loop,
    4815 int argc, const char* const* argv,
              4816                  int exec_argc, const char* const* exec_argv) {
          b:  4824   Isolate* const isolate = Isolate::New(params);
              4828   isolate->AddMessageListener(OnMessage);
              4829   isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
              4830   isolate->SetAutorunMicrotasks(false);
              4831   isolate->SetFatalErrorHandler(OnFatalError);
                new Isolate对象,并设置相关参数。
          c:  4843   int exit_code;
              4844   {
              4845     Locker locker(isolate);
              4846     Isolate::Scope isolate_scope(isolate);
              4847     HandleScope handle_scope(isolate);
              4848     IsolateData isolate_data(isolate, event_loop, allocator.zero_fill_field());
              4849     exit_code = Start(isolate, &isolate_data, argc, argv, exec_argc, exec_argv);
              4850   }
                准备开始执行的参数,isolate对象。
          d:  4745 inline int Start(Isolate* isolate, IsolateData* isolate_data,
              4746                  int argc, const char* const* argv,
              4747                  int exec_argc, const char* const* exec_argv) {
          e:  环境准备
                4748   HandleScope handle_scope(isolate);
                4749   Local context = Context::New(isolate);
                4750   Context::Scope context_scope(context);
                4751   Environment env(isolate_data, context);
                4754   env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);
                执行代码 src/env.cc:18:void Environment::Start(int argc,
                4771     LoadEnvironment(&env);
                加载env
          f:  在d中的函数里面进行eventloop,没有event的时候,就会退出node

    3.  分析核心部分
            4777   {
            4778     SealHandleScope seal(isolate);
            4779     bool more;
            4780     PERFORMANCE_MARK(&env, LOOP_START);
            4781     do {
            4782       uv_run(env.event_loop(), UV_RUN_DEFAULT);
            4783
            4784       v8_platform.DrainVMTasks();
            4785
            4786       more = uv_loop_alive(env.event_loop());
            4787       if (more)
            4788         continue;
            4789
            4790       EmitBeforeExit(&env);
            4791
            4792       // Emit `beforeExit` if the loop became alive either after emitting
                                                                                                                                                                                                                                                                                                       ‐ ‐‐‐‐‐
    4795 } while (more == true);
    4796 PERFORMANCE_MARK(&env, LOOP_EXIT); // If there is no event processing, exit.
    4797 }
    a: The core function uv_run for processing events
    Declaration: deps/uv/include/uv.h:281:UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
    Implementation: deps /uv/src/unix/core.c:348:int uv_run(uv_loop_t* loop, uv_run_mode mode) {
    b: Determine whether the loop is in the alive state: whether there is a handle, request-signal and the handle is not closed.
    343 int uv_loop_alive(const uv_loop_t* loop) {
    344 return uv__loop_alive(loop);
    345 }
    336 static int uv__loop_alive(const uv_loop_t* loop) {
    337 return uv__has_active_handles(loop ) ||
    338 uv__has_active_reqs(loop) ||
    339 loop->closing_handles != NULL;
    340 }
    c: uv__has_active_handles(loop ):
    deps/uv/src/ uv-common.h:145: #define uv__has_active_handles(loop)                                                                                                                                                                                                                                                                        ):
    129 #define uv__has_active_reqs(loop)                                                                       0)




The above is the detailed content of Node explains the process analysis of executing js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Analysis of the reasons why the secondary directory of DreamWeaver CMS cannot be opened Analysis of the reasons why the secondary directory of DreamWeaver CMS cannot be opened Mar 13, 2024 pm 06:24 PM

Title: Analysis of the reasons and solutions for why the secondary directory of DreamWeaver CMS cannot be opened. Dreamweaver CMS (DedeCMS) is a powerful open source content management system that is widely used in the construction of various websites. However, sometimes during the process of building a website, you may encounter a situation where the secondary directory cannot be opened, which brings trouble to the normal operation of the website. In this article, we will analyze the possible reasons why the secondary directory cannot be opened and provide specific code examples to solve this problem. 1. Possible cause analysis: Pseudo-static rule configuration problem: during use

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Analyze whether Tencent's main programming language is Go Analyze whether Tencent's main programming language is Go Mar 27, 2024 pm 04:21 PM

Title: Is Tencent’s main programming language Go: An in-depth analysis. As China’s leading technology company, Tencent has always attracted much attention in its choice of programming languages. In recent years, some people believe that Tencent mainly adopts Go as its main programming language. This article will conduct an in-depth analysis of whether Tencent's main programming language is Go, and give specific code examples to support this view. 1. Application of Go language in Tencent Go is an open source programming language developed by Google. Its efficiency, concurrency and simplicity are loved by many developers.

Analyze the advantages and disadvantages of static positioning technology Analyze the advantages and disadvantages of static positioning technology Jan 18, 2024 am 11:16 AM

Analysis of the advantages and limitations of static positioning technology With the development of modern technology, positioning technology has become an indispensable part of our lives. As one of them, static positioning technology has its unique advantages and limitations. This article will conduct an in-depth analysis of static positioning technology to better understand its current application status and future development trends. First, let’s take a look at the advantages of static positioning technology. Static positioning technology achieves the determination of position information by observing, measuring and calculating the object to be positioned. Compared with other positioning technologies,

See all articles