Bản soạn sẵn của nodejs

Addons là các đối tượng chia sẻ được liên kết động được viết bằng C ++. Chức năng có thể tải các addon như Node thông thường. mô-đun js. Addons cung cấp giao diện giữa thư viện JavaScript và C/C++

Có ba tùy chọn để triển khai addons. Node-API, nan hoặc sử dụng trực tiếp V8, libuv và Node nội bộ. thư viện js. Trừ khi có nhu cầu truy cập trực tiếp vào chức năng mà Node-API không hiển thị, hãy sử dụng Node-API. Tham khảo addons C/C++ với Node-API để biết thêm thông tin về Node-API

Khi không sử dụng Node-API, việc triển khai các addon rất phức tạp, liên quan đến kiến ​​thức về một số thành phần và API

  • V8. thư viện C++ Nút. js sử dụng để cung cấp triển khai JavaScript. V8 cung cấp các cơ chế tạo đối tượng, gọi hàm, v.v. API của V8 được ghi lại chủ yếu trong tệp tiêu đề

    void Initialize(Local exports);
    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
    

    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

    The module_name must match the filename of the final binary (excluding the .node suffix).

    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

    Context-aware addons#

    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

    • Local exports,
    • Local module, and
    • Local context
    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

      The context-aware addon can be structured to avoid global static data by performing the following steps:

      • Define a class which will hold per-addon-instance data and which has a static member of the form
      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
      • Store the instance of the class in a v8::External, and
      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

      The following example illustrates the implementation of a context-aware addon:

      Worker support#

      History VersionChanges v14.8.0, v12.19.0

      Cleanup hooks may now be asynchronous.

      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

      • Be an Node-API addon, or
      • Be declared as context-aware using NODE_MODULE_INIT() as described above

      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                     void (*fun)(void* arg),
                                     void* arg);
      0 (
      void Initialize(Local exports);
      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
      

      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

      The module_name must match the filename of the final binary (excluding the .node suffix).

      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

      Context-aware addons#

      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

      • Local exports,
      • Local module, and
      • Local context
      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

        The context-aware addon can be structured to avoid global static data by performing the following steps:

        • Define a class which will hold per-addon-instance data and which has a static member of the form
        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
        • Store the instance of the class in a v8::External, and
        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

        The following example illustrates the implementation of a context-aware addon:

        Worker support#

        History VersionChanges v14.8.0, v12.19.0

        Cleanup hooks may now be asynchronous.

        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

        • Be an Node-API addon, or
        • Be declared as context-aware using NODE_MODULE_INIT() as described above

        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                       void (*fun)(void* arg),
                                       void* arg);
        1 trong Node. js), cũng có sẵn trực tuyến

      • libuv. Thư viện C triển khai Node. js, chuỗi worker của nó và tất cả các hành vi không đồng bộ của nền tảng. Nó cũng phục vụ như một thư viện trừu tượng đa nền tảng, cho phép truy cập dễ dàng, giống như POSIX trên tất cả các hệ điều hành chính cho nhiều tác vụ hệ thống phổ biến, chẳng hạn như tương tác với hệ thống tệp, ổ cắm, bộ hẹn giờ và sự kiện hệ thống. libuv cũng cung cấp sự trừu tượng hóa luồng tương tự như luồng POSIX cho các phần bổ trợ không đồng bộ phức tạp hơn cần vượt ra ngoài vòng lặp sự kiện tiêu chuẩn. Các tác giả addon nên tránh chặn vòng lặp sự kiện bằng I/O hoặc các tác vụ tốn nhiều thời gian khác bằng cách giảm tải công việc qua libuv sang các hoạt động hệ thống không chặn, luồng công nhân hoặc sử dụng tùy chỉnh các luồng libuv

      • Nút nội bộ. thư viện js. Nút. js tự xuất các API C++ mà các addon có thể sử dụng, trong đó quan trọng nhất là lớp

        void Initialize(Local exports);
        NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
        

        There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

        The module_name must match the filename of the final binary (excluding the .node suffix).

        In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

        When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

        Context-aware addons#

        There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

        A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

        Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

        The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

        • Local exports,
        • Local module, and
        • Local context
        • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

          The context-aware addon can be structured to avoid global static data by performing the following steps:

          • Define a class which will hold per-addon-instance data and which has a static member of the form
          • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
          • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
          • Store the instance of the class in a v8::External, and
          • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

          This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

          The following example illustrates the implementation of a context-aware addon:

          Worker support#

          History VersionChanges v14.8.0, v12.19.0

          Cleanup hooks may now be asynchronous.

          In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

          • Be an Node-API addon, or
          • Be declared as context-aware using NODE_MODULE_INIT() as described above

          In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

          void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                         void (*fun)(void* arg),
                                         void* arg);
          2

        • Nút. js bao gồm các thư viện được liên kết tĩnh khác bao gồm OpenSSL. Các thư viện khác này nằm trong thư mục

          void Initialize(Local exports);
          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
          

          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

          The module_name must match the filename of the final binary (excluding the .node suffix).

          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

          Context-aware addons#

          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

          • Local exports,
          • Local module, and
          • Local context
          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

            The context-aware addon can be structured to avoid global static data by performing the following steps:

            • Define a class which will hold per-addon-instance data and which has a static member of the form
            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
            • Store the instance of the class in a v8::External, and
            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

            The following example illustrates the implementation of a context-aware addon:

            Worker support#

            History VersionChanges v14.8.0, v12.19.0

            Cleanup hooks may now be asynchronous.

            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

            • Be an Node-API addon, or
            • Be declared as context-aware using NODE_MODULE_INIT() as described above

            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                           void (*fun)(void* arg),
                                           void* arg);
            3 trong Node. cây nguồn js. Chỉ các biểu tượng libuv, OpenSSL, V8 và zlib được Node tái xuất có chủ đích. js và có thể được sử dụng ở nhiều mức độ khác nhau bởi addons. Xem để biết thêm thông tin

          Tất cả các ví dụ sau đều có sẵn để tải xuống và có thể được sử dụng làm điểm bắt đầu cho một addon

          Chào thế giới

          Ví dụ "Xin chào thế giới" này là một addon đơn giản, được viết bằng C++, tương đương với mã JavaScript sau

          module.exports.hello = () => 'world';

          Đầu tiên, tạo tệp

          void Initialize(Local exports);
          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
          

          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

          The module_name must match the filename of the final binary (excluding the .node suffix).

          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

          Context-aware addons#

          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

          • Local exports,
          • Local module, and
          • Local context
          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

            The context-aware addon can be structured to avoid global static data by performing the following steps:

            • Define a class which will hold per-addon-instance data and which has a static member of the form
            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
            • Store the instance of the class in a v8::External, and
            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

            The following example illustrates the implementation of a context-aware addon:

            Worker support#

            History VersionChanges v14.8.0, v12.19.0

            Cleanup hooks may now be asynchronous.

            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

            • Be an Node-API addon, or
            • Be declared as context-aware using NODE_MODULE_INIT() as described above

            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                           void (*fun)(void* arg),
                                           void* arg);
            4

            Tất cả nút. js addon phải xuất hàm khởi tạo theo mẫu

            void Initialize(Local exports);
            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
            

            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

            The module_name must match the filename of the final binary (excluding the .node suffix).

            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

            Context-aware addons#

            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

            • Local exports,
            • Local module, and
            • Local context
            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

              The context-aware addon can be structured to avoid global static data by performing the following steps:

              • Define a class which will hold per-addon-instance data and which has a static member of the form
              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
              • Store the instance of the class in a v8::External, and
              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

              The following example illustrates the implementation of a context-aware addon:

              Worker support#

              History VersionChanges v14.8.0, v12.19.0

              Cleanup hooks may now be asynchronous.

              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

              • Be an Node-API addon, or
              • Be declared as context-aware using NODE_MODULE_INIT() as described above

              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                             void (*fun)(void* arg),
                                             void* arg);

              Chức năng này thêm một hook sẽ chạy trước một Nút nhất định. phiên bản js tắt. Nếu cần, các móc như vậy có thể được gỡ bỏ trước khi chúng được chạy bằng cách sử dụng

              void Initialize(Local exports);
              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
              

              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

              The module_name must match the filename of the final binary (excluding the .node suffix).

              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

              Context-aware addons#

              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

              • Local exports,
              • Local module, and
              • Local context
              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                The context-aware addon can be structured to avoid global static data by performing the following steps:

                • Define a class which will hold per-addon-instance data and which has a static member of the form
                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                • Store the instance of the class in a v8::External, and
                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                The following example illustrates the implementation of a context-aware addon:

                Worker support#

                History VersionChanges v14.8.0, v12.19.0

                Cleanup hooks may now be asynchronous.

                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                • Be an Node-API addon, or
                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                               void (*fun)(void* arg),
                                               void* arg);
                5, có cùng chữ ký. Các cuộc gọi lại được chạy theo thứ tự vào trước ra trước

                Nếu cần thiết, có thêm một cặp quá tải

                void Initialize(Local exports);
                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                

                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                The module_name must match the filename of the final binary (excluding the .node suffix).

                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                Context-aware addons#

                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                • Local exports,
                • Local module, and
                • Local context
                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                  • Store the instance of the class in a v8::External, and
                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                  The following example illustrates the implementation of a context-aware addon:

                  Worker support#

                  History VersionChanges v14.8.0, v12.19.0

                  Cleanup hooks may now be asynchronous.

                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                  • Be an Node-API addon, or
                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                 void (*fun)(void* arg),
                                                 void* arg);
                  6 và
                  void Initialize(Local exports);
                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                  

                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                  The module_name must match the filename of the final binary (excluding the .node suffix).

                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                  Context-aware addons#

                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                  • Local exports,
                  • Local module, and
                  • Local context
                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                    • Store the instance of the class in a v8::External, and
                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                    The following example illustrates the implementation of a context-aware addon:

                    Worker support#

                    History VersionChanges v14.8.0, v12.19.0

                    Cleanup hooks may now be asynchronous.

                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                    • Be an Node-API addon, or
                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                   void (*fun)(void* arg),
                                                   void* arg);
                    5, trong đó móc dọn dẹp mất một . Điều này có thể được sử dụng để tắt các tài nguyên không đồng bộ, chẳng hạn như bất kỳ trình xử lý libuv nào được đăng ký bởi addon

                    void Initialize(Local exports);
                    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                    

                    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                    The module_name must match the filename of the final binary (excluding the .node suffix).

                    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                    Context-aware addons#

                    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                    • Local exports,
                    • Local module, and
                    • Local context
                    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                      The context-aware addon can be structured to avoid global static data by performing the following steps:

                      • Define a class which will hold per-addon-instance data and which has a static member of the form
                      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                      • Store the instance of the class in a v8::External, and
                      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                      The following example illustrates the implementation of a context-aware addon:

                      Worker support#

                      History VersionChanges v14.8.0, v12.19.0

                      Cleanup hooks may now be asynchronous.

                      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                      • Be an Node-API addon, or
                      • Be declared as context-aware using NODE_MODULE_INIT() as described above

                      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                     void (*fun)(void* arg),
                                                     void* arg);
                      8 sau sử dụng
                      void Initialize(Local exports);
                      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                      

                      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                      The module_name must match the filename of the final binary (excluding the .node suffix).

                      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                      Context-aware addons#

                      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                      • Local exports,
                      • Local module, and
                      • Local context
                      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                        The context-aware addon can be structured to avoid global static data by performing the following steps:

                        • Define a class which will hold per-addon-instance data and which has a static member of the form
                        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                        • Store the instance of the class in a v8::External, and
                        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                        The following example illustrates the implementation of a context-aware addon:

                        Worker support#

                        History VersionChanges v14.8.0, v12.19.0

                        Cleanup hooks may now be asynchronous.

                        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                        • Be an Node-API addon, or
                        • Be declared as context-aware using NODE_MODULE_INIT() as described above

                        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                       void (*fun)(void* arg),
                                                       void* arg);
                        9

                        Kiểm tra bằng JavaScript bằng cách chạy

                        Xây dựng

                        Khi mã nguồn đã được viết, nó phải được biên dịch thành tệp nhị phân

                        void Initialize(Local exports);
                        NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                        

                        There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                        The module_name must match the filename of the final binary (excluding the .node suffix).

                        In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                        When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                        Context-aware addons#

                        There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                        A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                        Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                        The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                        • Local exports,
                        • Local module, and
                        • Local context
                        • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                          The context-aware addon can be structured to avoid global static data by performing the following steps:

                          • Define a class which will hold per-addon-instance data and which has a static member of the form
                          • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                          • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                          • Store the instance of the class in a v8::External, and
                          • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                          This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                          The following example illustrates the implementation of a context-aware addon:

                          Worker support#

                          History VersionChanges v14.8.0, v12.19.0

                          Cleanup hooks may now be asynchronous.

                          In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                          • Be an Node-API addon, or
                          • Be declared as context-aware using NODE_MODULE_INIT() as described above

                          In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                          void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                         void (*fun)(void* arg),
                                                         void* arg);
                          20. Để làm như vậy, hãy tạo một tệp có tên
                          void Initialize(Local exports);
                          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                          

                          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                          The module_name must match the filename of the final binary (excluding the .node suffix).

                          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                          Context-aware addons#

                          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                          • Local exports,
                          • Local module, and
                          • Local context
                          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                            The context-aware addon can be structured to avoid global static data by performing the following steps:

                            • Define a class which will hold per-addon-instance data and which has a static member of the form
                            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                            • Store the instance of the class in a v8::External, and
                            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                            The following example illustrates the implementation of a context-aware addon:

                            Worker support#

                            History VersionChanges v14.8.0, v12.19.0

                            Cleanup hooks may now be asynchronous.

                            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                            • Be an Node-API addon, or
                            • Be declared as context-aware using NODE_MODULE_INIT() as described above

                            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                           void (*fun)(void* arg),
                                                           void* arg);
                            21 ở cấp cao nhất của dự án mô tả cấu hình bản dựng của mô-đun . Tệp này được sử dụng bởi nút-gyp, một công cụ được viết riêng để biên dịch Nút. addon js

                            void Initialize(Local exports);
                            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                            

                            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                            The module_name must match the filename of the final binary (excluding the .node suffix).

                            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                            Context-aware addons#

                            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                            • Local exports,
                            • Local module, and
                            • Local context
                            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                              The context-aware addon can be structured to avoid global static data by performing the following steps:

                              • Define a class which will hold per-addon-instance data and which has a static member of the form
                              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                              • Store the instance of the class in a v8::External, and
                              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                              The following example illustrates the implementation of a context-aware addon:

                              Worker support#

                              History VersionChanges v14.8.0, v12.19.0

                              Cleanup hooks may now be asynchronous.

                              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                              • Be an Node-API addon, or
                              • Be declared as context-aware using NODE_MODULE_INIT() as described above

                              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                             void (*fun)(void* arg),
                                                             void* arg);
                              2

                              Một phiên bản của tiện ích

                              void Initialize(Local exports);
                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                              

                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                              The module_name must match the filename of the final binary (excluding the .node suffix).

                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                              Context-aware addons#

                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                              • Local exports,
                              • Local module, and
                              • Local context
                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                • Store the instance of the class in a v8::External, and
                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                The following example illustrates the implementation of a context-aware addon:

                                Worker support#

                                History VersionChanges v14.8.0, v12.19.0

                                Cleanup hooks may now be asynchronous.

                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                • Be an Node-API addon, or
                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                               void (*fun)(void* arg),
                                                               void* arg);
                                22 được đóng gói và phân phối với Node. js như một phần của
                                void Initialize(Local exports);
                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                

                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                Context-aware addons#

                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                • Local exports,
                                • Local module, and
                                • Local context
                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                  • Store the instance of the class in a v8::External, and
                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                  The following example illustrates the implementation of a context-aware addon:

                                  Worker support#

                                  History VersionChanges v14.8.0, v12.19.0

                                  Cleanup hooks may now be asynchronous.

                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                  • Be an Node-API addon, or
                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                 void (*fun)(void* arg),
                                                                 void* arg);
                                  23. Phiên bản này không được cung cấp trực tiếp cho các nhà phát triển sử dụng và chỉ nhằm mục đích hỗ trợ khả năng sử dụng . Các nhà phát triển muốn sử dụng trực tiếp
                                  void Initialize(Local exports);
                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                  

                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                  Context-aware addons#

                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                  • Local exports,
                                  • Local module, and
                                  • Local context
                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                    • Store the instance of the class in a v8::External, and
                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                    The following example illustrates the implementation of a context-aware addon:

                                    Worker support#

                                    History VersionChanges v14.8.0, v12.19.0

                                    Cleanup hooks may now be asynchronous.

                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                    • Be an Node-API addon, or
                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                   void (*fun)(void* arg),
                                                                   void* arg);
                                    22 có thể cài đặt nó bằng lệnh . Xem
                                    void Initialize(Local exports);
                                    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                    

                                    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                    The module_name must match the filename of the final binary (excluding the .node suffix).

                                    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                    Context-aware addons#

                                    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                    • Local exports,
                                    • Local module, and
                                    • Local context
                                    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                      The context-aware addon can be structured to avoid global static data by performing the following steps:

                                      • Define a class which will hold per-addon-instance data and which has a static member of the form
                                      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                      • Store the instance of the class in a v8::External, and
                                      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                      The following example illustrates the implementation of a context-aware addon:

                                      Worker support#

                                      History VersionChanges v14.8.0, v12.19.0

                                      Cleanup hooks may now be asynchronous.

                                      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                      • Be an Node-API addon, or
                                      • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                     void (*fun)(void* arg),
                                                                     void* arg);
                                      22 để biết thêm thông tin, bao gồm các yêu cầu dành riêng cho nền tảng

                                      Khi tệp

                                      void Initialize(Local exports);
                                      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                      

                                      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                      The module_name must match the filename of the final binary (excluding the .node suffix).

                                      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                      Context-aware addons#

                                      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                      • Local exports,
                                      • Local module, and
                                      • Local context
                                      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                        The context-aware addon can be structured to avoid global static data by performing the following steps:

                                        • Define a class which will hold per-addon-instance data and which has a static member of the form
                                        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                        • Store the instance of the class in a v8::External, and
                                        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                        The following example illustrates the implementation of a context-aware addon:

                                        Worker support#

                                        History VersionChanges v14.8.0, v12.19.0

                                        Cleanup hooks may now be asynchronous.

                                        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                        • Be an Node-API addon, or
                                        • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                       void (*fun)(void* arg),
                                                                       void* arg);
                                        21 đã được tạo, hãy sử dụng
                                        void Initialize(Local exports);
                                        NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                        

                                        There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                        The module_name must match the filename of the final binary (excluding the .node suffix).

                                        In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                        When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                        Context-aware addons#

                                        There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                        A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                        Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                        The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                        • Local exports,
                                        • Local module, and
                                        • Local context
                                        • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                          The context-aware addon can be structured to avoid global static data by performing the following steps:

                                          • Define a class which will hold per-addon-instance data and which has a static member of the form
                                          • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                          • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                          • Store the instance of the class in a v8::External, and
                                          • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                          This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                          The following example illustrates the implementation of a context-aware addon:

                                          Worker support#

                                          History VersionChanges v14.8.0, v12.19.0

                                          Cleanup hooks may now be asynchronous.

                                          In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                          • Be an Node-API addon, or
                                          • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                          In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                          void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                         void (*fun)(void* arg),
                                                                         void* arg);
                                          29 để tạo tệp xây dựng dự án phù hợp cho nền tảng hiện tại. Điều này sẽ tạo ra một tệp
                                          void Initialize(Local exports);
                                          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                          

                                          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                          The module_name must match the filename of the final binary (excluding the .node suffix).

                                          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                          Context-aware addons#

                                          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                          • Local exports,
                                          • Local module, and
                                          • Local context
                                          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                            The context-aware addon can be structured to avoid global static data by performing the following steps:

                                            • Define a class which will hold per-addon-instance data and which has a static member of the form
                                            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                            • Store the instance of the class in a v8::External, and
                                            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                            The following example illustrates the implementation of a context-aware addon:

                                            Worker support#

                                            History VersionChanges v14.8.0, v12.19.0

                                            Cleanup hooks may now be asynchronous.

                                            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                            • Be an Node-API addon, or
                                            • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                           void (*fun)(void* arg),
                                                                           void* arg);
                                            30 (trên nền tảng Unix) hoặc tệp
                                            void Initialize(Local exports);
                                            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                            

                                            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                            The module_name must match the filename of the final binary (excluding the .node suffix).

                                            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                            Context-aware addons#

                                            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                            • Local exports,
                                            • Local module, and
                                            • Local context
                                            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                              The context-aware addon can be structured to avoid global static data by performing the following steps:

                                              • Define a class which will hold per-addon-instance data and which has a static member of the form
                                              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                              • Store the instance of the class in a v8::External, and
                                              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                              The following example illustrates the implementation of a context-aware addon:

                                              Worker support#

                                              History VersionChanges v14.8.0, v12.19.0

                                              Cleanup hooks may now be asynchronous.

                                              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                              • Be an Node-API addon, or
                                              • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                             void (*fun)(void* arg),
                                                                             void* arg);
                                              31

                                              Tiếp theo, gọi lệnh

                                              void Initialize(Local exports);
                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                              

                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                              Context-aware addons#

                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                              • Local exports,
                                              • Local module, and
                                              • Local context
                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                • Store the instance of the class in a v8::External, and
                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                The following example illustrates the implementation of a context-aware addon:

                                                Worker support#

                                                History VersionChanges v14.8.0, v12.19.0

                                                Cleanup hooks may now be asynchronous.

                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                • Be an Node-API addon, or
                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                               void (*fun)(void* arg),
                                                                               void* arg);
                                                33 để tạo tệp
                                                void Initialize(Local exports);
                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                

                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                Context-aware addons#

                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                • Local exports,
                                                • Local module, and
                                                • Local context
                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                  • Store the instance of the class in a v8::External, and
                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                  The following example illustrates the implementation of a context-aware addon:

                                                  Worker support#

                                                  History VersionChanges v14.8.0, v12.19.0

                                                  Cleanup hooks may now be asynchronous.

                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                  • Be an Node-API addon, or
                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                 void (*fun)(void* arg),
                                                                                 void* arg);
                                                  20 đã biên dịch. Điều này sẽ được đưa vào thư mục
                                                  void Initialize(Local exports);
                                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                  

                                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                  Context-aware addons#

                                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                  • Local exports,
                                                  • Local module, and
                                                  • Local context
                                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                    • Store the instance of the class in a v8::External, and
                                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                    The following example illustrates the implementation of a context-aware addon:

                                                    Worker support#

                                                    History VersionChanges v14.8.0, v12.19.0

                                                    Cleanup hooks may now be asynchronous.

                                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                    • Be an Node-API addon, or
                                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                   void (*fun)(void* arg),
                                                                                   void* arg);
                                                    35

                                                    Khi sử dụng

                                                    void Initialize(Local exports);
                                                    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                    

                                                    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                    The module_name must match the filename of the final binary (excluding the .node suffix).

                                                    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                    Context-aware addons#

                                                    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                    • Local exports,
                                                    • Local module, and
                                                    • Local context
                                                    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                      The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                      • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                      • Store the instance of the class in a v8::External, and
                                                      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                      The following example illustrates the implementation of a context-aware addon:

                                                      Worker support#

                                                      History VersionChanges v14.8.0, v12.19.0

                                                      Cleanup hooks may now be asynchronous.

                                                      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                      • Be an Node-API addon, or
                                                      • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                     void (*fun)(void* arg),
                                                                                     void* arg);
                                                      24 để cài đặt Nút. js addon, npm sử dụng phiên bản
                                                      void Initialize(Local exports);
                                                      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                      

                                                      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                      The module_name must match the filename of the final binary (excluding the .node suffix).

                                                      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                      Context-aware addons#

                                                      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                      • Local exports,
                                                      • Local module, and
                                                      • Local context
                                                      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                        The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                        • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                        • Store the instance of the class in a v8::External, and
                                                        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                        The following example illustrates the implementation of a context-aware addon:

                                                        Worker support#

                                                        History VersionChanges v14.8.0, v12.19.0

                                                        Cleanup hooks may now be asynchronous.

                                                        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                        • Be an Node-API addon, or
                                                        • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                       void (*fun)(void* arg),
                                                                                       void* arg);
                                                        22 đi kèm của chính nó để thực hiện cùng một nhóm hành động này, tạo ra một

                                                        Sau khi được xây dựng, addon nhị phân có thể được sử dụng từ bên trong Node. js bằng cách trỏ đến mô-đun

                                                        void Initialize(Local exports);
                                                        NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                        

                                                        There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                        The module_name must match the filename of the final binary (excluding the .node suffix).

                                                        In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                        When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                        Context-aware addons#

                                                        There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                        A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                        Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                        The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                        • Local exports,
                                                        • Local module, and
                                                        • Local context
                                                        • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                          The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                          • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                          • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                          • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                          • Store the instance of the class in a v8::External, and
                                                          • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                          This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                          The following example illustrates the implementation of a context-aware addon:

                                                          Worker support#

                                                          History VersionChanges v14.8.0, v12.19.0

                                                          Cleanup hooks may now be asynchronous.

                                                          In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                          • Be an Node-API addon, or
                                                          • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                          In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                          void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                         void (*fun)(void* arg),
                                                                                         void* arg);
                                                          20 đã xây dựng

                                                          Bởi vì đường dẫn chính xác đến tệp nhị phân addon đã biên dịch có thể khác nhau tùy thuộc vào cách nó được biên dịch (i. e. đôi khi nó có thể ở trong

                                                          void Initialize(Local exports);
                                                          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                          

                                                          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                          The module_name must match the filename of the final binary (excluding the .node suffix).

                                                          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                          Context-aware addons#

                                                          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                          • Local exports,
                                                          • Local module, and
                                                          • Local context
                                                          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                            The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                            • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                            • Store the instance of the class in a v8::External, and
                                                            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                            The following example illustrates the implementation of a context-aware addon:

                                                            Worker support#

                                                            History VersionChanges v14.8.0, v12.19.0

                                                            Cleanup hooks may now be asynchronous.

                                                            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                            • Be an Node-API addon, or
                                                            • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                           void (*fun)(void* arg),
                                                                                           void* arg);
                                                            20), các addon có thể sử dụng gói liên kết để tải mô-đun đã biên dịch

                                                            Mặc dù việc triển khai gói

                                                            void Initialize(Local exports);
                                                            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                            

                                                            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                            The module_name must match the filename of the final binary (excluding the .node suffix).

                                                            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                            Context-aware addons#

                                                            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                            • Local exports,
                                                            • Local module, and
                                                            • Local context
                                                            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                              The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                              • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                              • Store the instance of the class in a v8::External, and
                                                              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                              The following example illustrates the implementation of a context-aware addon:

                                                              Worker support#

                                                              History VersionChanges v14.8.0, v12.19.0

                                                              Cleanup hooks may now be asynchronous.

                                                              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                              • Be an Node-API addon, or
                                                              • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                             void (*fun)(void* arg),
                                                                                             void* arg);
                                                              21 tinh vi hơn trong cách định vị các mô-đun bổ trợ, nhưng về cơ bản, nó sử dụng mẫu
                                                              void Initialize(Local exports);
                                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                              

                                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                              Context-aware addons#

                                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                              • Local exports,
                                                              • Local module, and
                                                              • Local context
                                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                • Store the instance of the class in a v8::External, and
                                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                The following example illustrates the implementation of a context-aware addon:

                                                                Worker support#

                                                                History VersionChanges v14.8.0, v12.19.0

                                                                Cleanup hooks may now be asynchronous.

                                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                • Be an Node-API addon, or
                                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                               void (*fun)(void* arg),
                                                                                               void* arg);
                                                                22 tương tự như

                                                                void Initialize(Local exports);
                                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                

                                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                Context-aware addons#

                                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                • Local exports,
                                                                • Local module, and
                                                                • Local context
                                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                  • Store the instance of the class in a v8::External, and
                                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                  The following example illustrates the implementation of a context-aware addon:

                                                                  Worker support#

                                                                  History VersionChanges v14.8.0, v12.19.0

                                                                  Cleanup hooks may now be asynchronous.

                                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                  • Be an Node-API addon, or
                                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                 void (*fun)(void* arg),
                                                                                                 void* arg);
                                                                  3

                                                                  Liên kết với các thư viện đi kèm với Node. js

                                                                  Nút. js sử dụng các thư viện được liên kết tĩnh như V8, libuv và OpenSSL. Tất cả các addon được yêu cầu liên kết với V8 và có thể liên kết với bất kỳ phần phụ thuộc nào khác . Thông thường, điều này đơn giản như bao gồm các câu lệnh

                                                                  void Initialize(Local exports);
                                                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                  

                                                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                  Context-aware addons#

                                                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                  • Local exports,
                                                                  • Local module, and
                                                                  • Local context
                                                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                    • Store the instance of the class in a v8::External, and
                                                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                    The following example illustrates the implementation of a context-aware addon:

                                                                    Worker support#

                                                                    History VersionChanges v14.8.0, v12.19.0

                                                                    Cleanup hooks may now be asynchronous.

                                                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                    • Be an Node-API addon, or
                                                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                   void (*fun)(void* arg),
                                                                                                   void* arg);
                                                                    23 thích hợp (e. g.
                                                                    void Initialize(Local exports);
                                                                    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                    

                                                                    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                    The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                    Context-aware addons#

                                                                    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                    • Local exports,
                                                                    • Local module, and
                                                                    • Local context
                                                                    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                      The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                      • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                      • Store the instance of the class in a v8::External, and
                                                                      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                      The following example illustrates the implementation of a context-aware addon:

                                                                      Worker support#

                                                                      History VersionChanges v14.8.0, v12.19.0

                                                                      Cleanup hooks may now be asynchronous.

                                                                      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                      • Be an Node-API addon, or
                                                                      • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                     void (*fun)(void* arg),
                                                                                                     void* arg);
                                                                      24) và
                                                                      void Initialize(Local exports);
                                                                      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                      

                                                                      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                      The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                      Context-aware addons#

                                                                      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                      • Local exports,
                                                                      • Local module, and
                                                                      • Local context
                                                                      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                        The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                        • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                        • Store the instance of the class in a v8::External, and
                                                                        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                        The following example illustrates the implementation of a context-aware addon:

                                                                        Worker support#

                                                                        History VersionChanges v14.8.0, v12.19.0

                                                                        Cleanup hooks may now be asynchronous.

                                                                        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                        • Be an Node-API addon, or
                                                                        • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                       void (*fun)(void* arg),
                                                                                                       void* arg);
                                                                        22 sẽ tự động định vị các tiêu đề phù hợp. Tuy nhiên, có một số lưu ý cần lưu ý

                                                                        • Khi

                                                                          void Initialize(Local exports);
                                                                          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                          

                                                                          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                          The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                          Context-aware addons#

                                                                          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                          • Local exports,
                                                                          • Local module, and
                                                                          • Local context
                                                                          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                            The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                            • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                            • Store the instance of the class in a v8::External, and
                                                                            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                            The following example illustrates the implementation of a context-aware addon:

                                                                            Worker support#

                                                                            History VersionChanges v14.8.0, v12.19.0

                                                                            Cleanup hooks may now be asynchronous.

                                                                            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                            • Be an Node-API addon, or
                                                                            • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                           void (*fun)(void* arg),
                                                                                                           void* arg);
                                                                            22 chạy, nó sẽ phát hiện phiên bản phát hành cụ thể của Node. js và tải xuống tarball nguồn đầy đủ hoặc chỉ các tiêu đề. Nếu nguồn đầy đủ được tải xuống, các addon sẽ có quyền truy cập đầy đủ vào toàn bộ . phụ thuộc js. Tuy nhiên, nếu chỉ có Node. js được tải xuống, sau đó chỉ các ký hiệu được xuất bởi Node. js sẽ có sẵn

                                                                          • Có thể chạy

                                                                            void Initialize(Local exports);
                                                                            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                            

                                                                            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                            The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                            Context-aware addons#

                                                                            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                            • Local exports,
                                                                            • Local module, and
                                                                            • Local context
                                                                            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                              The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                              • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                              • Store the instance of the class in a v8::External, and
                                                                              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                              The following example illustrates the implementation of a context-aware addon:

                                                                              Worker support#

                                                                              History VersionChanges v14.8.0, v12.19.0

                                                                              Cleanup hooks may now be asynchronous.

                                                                              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                              • Be an Node-API addon, or
                                                                              • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                             void (*fun)(void* arg),
                                                                                                             void* arg);
                                                                              22 bằng cách sử dụng cờ
                                                                              void Initialize(Local exports);
                                                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                              

                                                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                              Context-aware addons#

                                                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                              • Local exports,
                                                                              • Local module, and
                                                                              • Local context
                                                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                • Store the instance of the class in a v8::External, and
                                                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                The following example illustrates the implementation of a context-aware addon:

                                                                                Worker support#

                                                                                History VersionChanges v14.8.0, v12.19.0

                                                                                Cleanup hooks may now be asynchronous.

                                                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                • Be an Node-API addon, or
                                                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                               void (*fun)(void* arg),
                                                                                                               void* arg);
                                                                                28 chỉ vào Nút cục bộ. hình ảnh nguồn js. Sử dụng tùy chọn này, addon sẽ có quyền truy cập vào toàn bộ phụ thuộc

                                                                              Đang tải addon bằng cách sử dụng
                                                                              void Initialize(Local exports);
                                                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                              

                                                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                              Context-aware addons#

                                                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                              • Local exports,
                                                                              • Local module, and
                                                                              • Local context
                                                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                • Store the instance of the class in a v8::External, and
                                                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                The following example illustrates the implementation of a context-aware addon:

                                                                                Worker support#

                                                                                History VersionChanges v14.8.0, v12.19.0

                                                                                Cleanup hooks may now be asynchronous.

                                                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                • Be an Node-API addon, or
                                                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                               void (*fun)(void* arg),
                                                                                                               void* arg);
                                                                                6

                                                                              Phần mở rộng tên tệp của nhị phân addon đã biên dịch là

                                                                              module.exports.hello = () => 'world';
                                                                              20 (trái ngược với
                                                                              module.exports.hello = () => 'world';
                                                                              21 hoặc
                                                                              module.exports.hello = () => 'world';
                                                                              22). Hàm này được viết để tìm kiếm các tệp có phần mở rộng tệp
                                                                              module.exports.hello = () => 'world';
                                                                              20 và khởi tạo các tệp đó dưới dạng liên kết động

                                                                              Khi gọi, phần mở rộng

                                                                              module.exports.hello = () => 'world';
                                                                              20 thường có thể được bỏ qua và Node. js vẫn sẽ tìm và khởi tạo addon. Tuy nhiên, một lưu ý là Node. js trước tiên sẽ cố gắng định vị và tải các mô-đun hoặc tệp JavaScript có cùng tên cơ sở. Chẳng hạn, nếu có tệp
                                                                              module.exports.hello = () => 'world';
                                                                              27 trong cùng thư mục với tệp nhị phân
                                                                              void Initialize(Local exports);
                                                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                              

                                                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                              Context-aware addons#

                                                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                              • Local exports,
                                                                              • Local module, and
                                                                              • Local context
                                                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                • Store the instance of the class in a v8::External, and
                                                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                The following example illustrates the implementation of a context-aware addon:

                                                                                Worker support#

                                                                                History VersionChanges v14.8.0, v12.19.0

                                                                                Cleanup hooks may now be asynchronous.

                                                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                • Be an Node-API addon, or
                                                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                               void (*fun)(void* arg),
                                                                                                               void* arg);
                                                                                20,

                                                                                Tóm tắt bản địa cho Node. js

                                                                                Mỗi ví dụ được minh họa trong tài liệu này trực tiếp sử dụng Nút. js và API V8 để triển khai addon. API V8 có thể và đã thay đổi đáng kể từ bản phát hành V8 này sang bản phát hành V8 tiếp theo (và một bản phát hành Node chính. js phát hành tiếp theo). Với mỗi thay đổi, các addon có thể cần được cập nhật và biên dịch lại để tiếp tục hoạt động. nút. js được thiết kế để giảm thiểu tần suất và tác động của những thay đổi đó nhưng có rất ít . js có thể làm gì để đảm bảo tính ổn định của API V8

                                                                                Trừu tượng bản địa cho nút. js (hoặc

                                                                                void Initialize(Local exports);
                                                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                

                                                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                Context-aware addons#

                                                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                • Local exports,
                                                                                • Local module, and
                                                                                • Local context
                                                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                  • Store the instance of the class in a v8::External, and
                                                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                  The following example illustrates the implementation of a context-aware addon:

                                                                                  Worker support#

                                                                                  History VersionChanges v14.8.0, v12.19.0

                                                                                  Cleanup hooks may now be asynchronous.

                                                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                  • Be an Node-API addon, or
                                                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                 void (*fun)(void* arg),
                                                                                                                 void* arg);
                                                                                  01) cung cấp một bộ công cụ mà các nhà phát triển addon nên sử dụng để duy trì khả năng tương thích giữa quá khứ và . js. Xem các ví dụ về
                                                                                  void Initialize(Local exports);
                                                                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                  

                                                                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                  Context-aware addons#

                                                                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                  • Local exports,
                                                                                  • Local module, and
                                                                                  • Local context
                                                                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                    • Store the instance of the class in a v8::External, and
                                                                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                    The following example illustrates the implementation of a context-aware addon:

                                                                                    Worker support#

                                                                                    History VersionChanges v14.8.0, v12.19.0

                                                                                    Cleanup hooks may now be asynchronous.

                                                                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                    • Be an Node-API addon, or
                                                                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                   void (*fun)(void* arg),
                                                                                                                   void* arg);
                                                                                    01 để biết cách sử dụng nó

                                                                                API nút

                                                                                Node-API là một API để xây dựng các addon gốc. Nó độc lập với thời gian chạy JavaScript bên dưới (e. g. V8) và được duy trì như một phần của Node. js chính nó. API này sẽ là Giao diện nhị phân ứng dụng (ABI) ổn định trên các phiên bản của Nút. js. Nó nhằm mục đích cách ly các addon khỏi những thay đổi trong công cụ JavaScript cơ bản và cho phép các mô-đun . js mà không cần biên dịch lại. Addons được xây dựng/đóng gói với cùng một cách tiếp cận/công cụ được nêu trong tài liệu này (node-gyp, v.v. ). Sự khác biệt duy nhất là bộ API được sử dụng bởi mã gốc. Thay vì sử dụng V8 hoặc Trừu tượng gốc cho Nút. js, các chức năng có sẵn trong Node-API được sử dụng

                                                                                Tạo và duy trì một addon được hưởng lợi từ sự ổn định của ABI do Node-API cung cấp mang theo nó một số điều nhất định

                                                                                Để sử dụng Node-API trong ví dụ "Xin chào thế giới" ở trên, hãy thay thế nội dung của

                                                                                void Initialize(Local exports);
                                                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                

                                                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                Context-aware addons#

                                                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                • Local exports,
                                                                                • Local module, and
                                                                                • Local context
                                                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                  • Store the instance of the class in a v8::External, and
                                                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                  The following example illustrates the implementation of a context-aware addon:

                                                                                  Worker support#

                                                                                  History VersionChanges v14.8.0, v12.19.0

                                                                                  Cleanup hooks may now be asynchronous.

                                                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                  • Be an Node-API addon, or
                                                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                 void (*fun)(void* arg),
                                                                                                                 void* arg);
                                                                                  4 bằng nội dung sau. Tất cả các hướng dẫn khác vẫn giữ nguyên

                                                                                  Các chức năng có sẵn và cách sử dụng chúng được ghi lại trong các addon C/C++ với Node-API

                                                                                ví dụ bổ trợ

                                                                                Sau đây là một số addon ví dụ nhằm giúp các nhà phát triển bắt đầu. Các ví dụ sử dụng API V8. Tham khảo tài liệu tham khảo V8 trực tuyến để được trợ giúp về các cuộc gọi V8 khác nhau và Hướng dẫn Trình nhúng của V8 để biết

                                                                                Mỗi ví dụ này sử dụng tệp

                                                                                void Initialize(Local exports);
                                                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                

                                                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                Context-aware addons#

                                                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                • Local exports,
                                                                                • Local module, and
                                                                                • Local context
                                                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                  • Store the instance of the class in a v8::External, and
                                                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                  The following example illustrates the implementation of a context-aware addon:

                                                                                  Worker support#

                                                                                  History VersionChanges v14.8.0, v12.19.0

                                                                                  Cleanup hooks may now be asynchronous.

                                                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                  • Be an Node-API addon, or
                                                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                 void (*fun)(void* arg),
                                                                                                                 void* arg);
                                                                                  21 sau

                                                                                  void Initialize(Local exports);
                                                                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                  

                                                                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                  Context-aware addons#

                                                                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                  • Local exports,
                                                                                  • Local module, and
                                                                                  • Local context
                                                                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                    • Store the instance of the class in a v8::External, and
                                                                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                    The following example illustrates the implementation of a context-aware addon:

                                                                                    Worker support#

                                                                                    History VersionChanges v14.8.0, v12.19.0

                                                                                    Cleanup hooks may now be asynchronous.

                                                                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                    • Be an Node-API addon, or
                                                                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                   void (*fun)(void* arg),
                                                                                                                   void* arg);
                                                                                    2

                                                                                    Trong trường hợp có nhiều hơn một tệp

                                                                                    void Initialize(Local exports);
                                                                                    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                    

                                                                                    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                    The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                    Context-aware addons#

                                                                                    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                    • Local exports,
                                                                                    • Local module, and
                                                                                    • Local context
                                                                                    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                      The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                      • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                      • Store the instance of the class in a v8::External, and
                                                                                      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                      The following example illustrates the implementation of a context-aware addon:

                                                                                      Worker support#

                                                                                      History VersionChanges v14.8.0, v12.19.0

                                                                                      Cleanup hooks may now be asynchronous.

                                                                                      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                      • Be an Node-API addon, or
                                                                                      • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                     void (*fun)(void* arg),
                                                                                                                     void* arg);
                                                                                      05, chỉ cần thêm tên tệp bổ sung vào mảng
                                                                                      void Initialize(Local exports);
                                                                                      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                      

                                                                                      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                      The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                      Context-aware addons#

                                                                                      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                      • Local exports,
                                                                                      • Local module, and
                                                                                      • Local context
                                                                                      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                        The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                        • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                        • Store the instance of the class in a v8::External, and
                                                                                        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                        The following example illustrates the implementation of a context-aware addon:

                                                                                        Worker support#

                                                                                        History VersionChanges v14.8.0, v12.19.0

                                                                                        Cleanup hooks may now be asynchronous.

                                                                                        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                        • Be an Node-API addon, or
                                                                                        • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                       void (*fun)(void* arg),
                                                                                                                       void* arg);
                                                                                        06

                                                                                        module.exports.hello = () => 'world';
                                                                                        2

                                                                                        Khi tệp

                                                                                        void Initialize(Local exports);
                                                                                        NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                        

                                                                                        There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                        The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                        In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                        When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                        Context-aware addons#

                                                                                        There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                        A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                        Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                        The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                        • Local exports,
                                                                                        • Local module, and
                                                                                        • Local context
                                                                                        • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                          The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                          • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                          • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                          • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                          • Store the instance of the class in a v8::External, and
                                                                                          • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                          This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                          The following example illustrates the implementation of a context-aware addon:

                                                                                          Worker support#

                                                                                          History VersionChanges v14.8.0, v12.19.0

                                                                                          Cleanup hooks may now be asynchronous.

                                                                                          In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                          • Be an Node-API addon, or
                                                                                          • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                          In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                          void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                         void (*fun)(void* arg),
                                                                                                                         void* arg);
                                                                                          21 đã sẵn sàng, các addon mẫu có thể được cấu hình và xây dựng bằng cách sử dụng
                                                                                          void Initialize(Local exports);
                                                                                          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                          

                                                                                          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                          The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                          Context-aware addons#

                                                                                          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                          • Local exports,
                                                                                          • Local module, and
                                                                                          • Local context
                                                                                          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                            The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                            • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                            • Store the instance of the class in a v8::External, and
                                                                                            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                            The following example illustrates the implementation of a context-aware addon:

                                                                                            Worker support#

                                                                                            History VersionChanges v14.8.0, v12.19.0

                                                                                            Cleanup hooks may now be asynchronous.

                                                                                            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                            • Be an Node-API addon, or
                                                                                            • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                           void (*fun)(void* arg),
                                                                                                                           void* arg);
                                                                                            22

                                                                                            void Initialize(Local exports);
                                                                                            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                            

                                                                                            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                            The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                            Context-aware addons#

                                                                                            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                            • Local exports,
                                                                                            • Local module, and
                                                                                            • Local context
                                                                                            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                              The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                              • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                              • Store the instance of the class in a v8::External, and
                                                                                              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                              The following example illustrates the implementation of a context-aware addon:

                                                                                              Worker support#

                                                                                              History VersionChanges v14.8.0, v12.19.0

                                                                                              Cleanup hooks may now be asynchronous.

                                                                                              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                              • Be an Node-API addon, or
                                                                                              • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                             void (*fun)(void* arg),
                                                                                                                             void* arg);
                                                                                              0

                                                                                              đối số chức năng

                                                                                              Các addon thường sẽ hiển thị các đối tượng và chức năng có thể được truy cập từ JavaScript đang chạy trong Node. js. Khi các hàm được gọi từ JavaScript, các đối số đầu vào và giá trị trả về phải được ánh xạ tới và từ C/C++

                                                                                              Ví dụ sau minh họa cách đọc đối số hàm được truyền từ JavaScript và cách trả về kết quả

                                                                                              Sau khi được biên dịch, addon ví dụ có thể được yêu cầu và sử dụng từ bên trong Node. js

                                                                                              gọi lại

                                                                                              Thực tế phổ biến trong các addon là chuyển các hàm JavaScript sang hàm C++ và thực thi chúng từ đó. Ví dụ sau minh họa cách gọi các cuộc gọi lại như vậy

                                                                                              Ví dụ này sử dụng dạng hai đối số của

                                                                                              void Initialize(Local exports);
                                                                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                              

                                                                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                              Context-aware addons#

                                                                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                              • Local exports,
                                                                                              • Local module, and
                                                                                              • Local context
                                                                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                • Store the instance of the class in a v8::External, and
                                                                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                The following example illustrates the implementation of a context-aware addon:

                                                                                                Worker support#

                                                                                                History VersionChanges v14.8.0, v12.19.0

                                                                                                Cleanup hooks may now be asynchronous.

                                                                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                • Be an Node-API addon, or
                                                                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                               void (*fun)(void* arg),
                                                                                                                               void* arg);
                                                                                                09 nhận toàn bộ đối tượng
                                                                                                void Initialize(Local exports);
                                                                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                

                                                                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                Context-aware addons#

                                                                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                • Local exports,
                                                                                                • Local module, and
                                                                                                • Local context
                                                                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                  • Store the instance of the class in a v8::External, and
                                                                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                  The following example illustrates the implementation of a context-aware addon:

                                                                                                  Worker support#

                                                                                                  History VersionChanges v14.8.0, v12.19.0

                                                                                                  Cleanup hooks may now be asynchronous.

                                                                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                  • Be an Node-API addon, or
                                                                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                 void (*fun)(void* arg),
                                                                                                                                 void* arg);
                                                                                                  20 làm đối số thứ hai. Điều này cho phép addon ghi đè hoàn toàn
                                                                                                  void Initialize(Local exports);
                                                                                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                  

                                                                                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                  Context-aware addons#

                                                                                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                  • Local exports,
                                                                                                  • Local module, and
                                                                                                  • Local context
                                                                                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                    • Store the instance of the class in a v8::External, and
                                                                                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                    The following example illustrates the implementation of a context-aware addon:

                                                                                                    Worker support#

                                                                                                    History VersionChanges v14.8.0, v12.19.0

                                                                                                    Cleanup hooks may now be asynchronous.

                                                                                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                    • Be an Node-API addon, or
                                                                                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                   void (*fun)(void* arg),
                                                                                                                                   void* arg);
                                                                                                    21 bằng một chức năng duy nhất thay vì thêm chức năng dưới dạng

                                                                                                    Để kiểm tra nó, hãy chạy JavaScript sau

                                                                                                    Trong ví dụ này, chức năng gọi lại được gọi đồng bộ

                                                                                                    nhà máy đối tượng

                                                                                                    Addons có thể tạo và trả về các đối tượng mới từ bên trong hàm C++ như minh họa trong ví dụ sau. Một đối tượng được tạo và trả về với thuộc tính

                                                                                                    void Initialize(Local exports);
                                                                                                    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                    

                                                                                                    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                    The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                    Context-aware addons#

                                                                                                    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                    • Local exports,
                                                                                                    • Local module, and
                                                                                                    • Local context
                                                                                                    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                      The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                      • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                      • Store the instance of the class in a v8::External, and
                                                                                                      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                      The following example illustrates the implementation of a context-aware addon:

                                                                                                      Worker support#

                                                                                                      History VersionChanges v14.8.0, v12.19.0

                                                                                                      Cleanup hooks may now be asynchronous.

                                                                                                      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                      • Be an Node-API addon, or
                                                                                                      • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                     void (*fun)(void* arg),
                                                                                                                                     void* arg);
                                                                                                      23 lặp lại chuỗi được chuyển đến
                                                                                                      void Initialize(Local exports);
                                                                                                      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                      

                                                                                                      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                      The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                      Context-aware addons#

                                                                                                      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                      • Local exports,
                                                                                                      • Local module, and
                                                                                                      • Local context
                                                                                                      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                        The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                        • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                        • Store the instance of the class in a v8::External, and
                                                                                                        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                        The following example illustrates the implementation of a context-aware addon:

                                                                                                        Worker support#

                                                                                                        History VersionChanges v14.8.0, v12.19.0

                                                                                                        Cleanup hooks may now be asynchronous.

                                                                                                        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                        • Be an Node-API addon, or
                                                                                                        • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                       void (*fun)(void* arg),
                                                                                                                                       void* arg);
                                                                                                        24

                                                                                                        Để kiểm tra nó trong JavaScript

                                                                                                        nhà máy chức năng

                                                                                                        Một tình huống phổ biến khác là tạo các hàm JavaScript bao bọc các hàm C++ và trả lại các hàm đó cho JavaScript

                                                                                                        Để kiểm tra

                                                                                                        Bao bọc các đối tượng C++

                                                                                                        Cũng có thể bọc các đối tượng/lớp C++ theo cách cho phép tạo các thể hiện mới bằng cách sử dụng toán tử JavaScript

                                                                                                        void Initialize(Local exports);
                                                                                                        NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                        

                                                                                                        There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                        The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                        In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                        When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                        Context-aware addons#

                                                                                                        There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                        A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                        Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                        The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                        • Local exports,
                                                                                                        • Local module, and
                                                                                                        • Local context
                                                                                                        • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                          The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                          • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                          • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                          • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                          • Store the instance of the class in a v8::External, and
                                                                                                          • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                          This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                          The following example illustrates the implementation of a context-aware addon:

                                                                                                          Worker support#

                                                                                                          History VersionChanges v14.8.0, v12.19.0

                                                                                                          Cleanup hooks may now be asynchronous.

                                                                                                          In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                          • Be an Node-API addon, or
                                                                                                          • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                          In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                          void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                         void (*fun)(void* arg),
                                                                                                                                         void* arg);
                                                                                                          25

                                                                                                          Sau đó, trong

                                                                                                          void Initialize(Local exports);
                                                                                                          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                          

                                                                                                          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                          The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                          Context-aware addons#

                                                                                                          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                          • Local exports,
                                                                                                          • Local module, and
                                                                                                          • Local context
                                                                                                          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                            The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                            • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                            • Store the instance of the class in a v8::External, and
                                                                                                            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                            The following example illustrates the implementation of a context-aware addon:

                                                                                                            Worker support#

                                                                                                            History VersionChanges v14.8.0, v12.19.0

                                                                                                            Cleanup hooks may now be asynchronous.

                                                                                                            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                            • Be an Node-API addon, or
                                                                                                            • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                           void (*fun)(void* arg),
                                                                                                                                           void* arg);
                                                                                                            26, lớp bao bọc kế thừa từ
                                                                                                            void Initialize(Local exports);
                                                                                                            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                            

                                                                                                            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                            The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                            Context-aware addons#

                                                                                                            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                            • Local exports,
                                                                                                            • Local module, and
                                                                                                            • Local context
                                                                                                            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                              The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                              • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                              • Store the instance of the class in a v8::External, and
                                                                                                              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                              The following example illustrates the implementation of a context-aware addon:

                                                                                                              Worker support#

                                                                                                              History VersionChanges v14.8.0, v12.19.0

                                                                                                              Cleanup hooks may now be asynchronous.

                                                                                                              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                              • Be an Node-API addon, or
                                                                                                              • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                             void (*fun)(void* arg),
                                                                                                                                             void* arg);
                                                                                                              2

                                                                                                              Trong

                                                                                                              void Initialize(Local exports);
                                                                                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                              

                                                                                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                              Context-aware addons#

                                                                                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                              • Local exports,
                                                                                                              • Local module, and
                                                                                                              • Local context
                                                                                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                • Store the instance of the class in a v8::External, and
                                                                                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                The following example illustrates the implementation of a context-aware addon:

                                                                                                                Worker support#

                                                                                                                History VersionChanges v14.8.0, v12.19.0

                                                                                                                Cleanup hooks may now be asynchronous.

                                                                                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                • Be an Node-API addon, or
                                                                                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                               void (*fun)(void* arg),
                                                                                                                                               void* arg);
                                                                                                                28, thực hiện các phương pháp khác nhau sẽ được tiếp xúc. Bên dưới, phương thức
                                                                                                                void Initialize(Local exports);
                                                                                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                

                                                                                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                Context-aware addons#

                                                                                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                • Local exports,
                                                                                                                • Local module, and
                                                                                                                • Local context
                                                                                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                  • Store the instance of the class in a v8::External, and
                                                                                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                  The following example illustrates the implementation of a context-aware addon:

                                                                                                                  Worker support#

                                                                                                                  History VersionChanges v14.8.0, v12.19.0

                                                                                                                  Cleanup hooks may now be asynchronous.

                                                                                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                  • Be an Node-API addon, or
                                                                                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                 void (*fun)(void* arg),
                                                                                                                                                 void* arg);
                                                                                                                  29 được hiển thị bằng cách thêm nó vào hàm tạo của

                                                                                                                  Để xây dựng ví dụ này, tệp

                                                                                                                  void Initialize(Local exports);
                                                                                                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                  

                                                                                                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                  Context-aware addons#

                                                                                                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                  • Local exports,
                                                                                                                  • Local module, and
                                                                                                                  • Local context
                                                                                                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                    • Store the instance of the class in a v8::External, and
                                                                                                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                    The following example illustrates the implementation of a context-aware addon:

                                                                                                                    Worker support#

                                                                                                                    History VersionChanges v14.8.0, v12.19.0

                                                                                                                    Cleanup hooks may now be asynchronous.

                                                                                                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                    • Be an Node-API addon, or
                                                                                                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                   void (*fun)(void* arg),
                                                                                                                                                   void* arg);
                                                                                                                    28 phải được thêm vào tệp
                                                                                                                    void Initialize(Local exports);
                                                                                                                    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                    

                                                                                                                    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                    The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                    Context-aware addons#

                                                                                                                    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                    • Local exports,
                                                                                                                    • Local module, and
                                                                                                                    • Local context
                                                                                                                    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                      The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                      • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                      • Store the instance of the class in a v8::External, and
                                                                                                                      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                      The following example illustrates the implementation of a context-aware addon:

                                                                                                                      Worker support#

                                                                                                                      History VersionChanges v14.8.0, v12.19.0

                                                                                                                      Cleanup hooks may now be asynchronous.

                                                                                                                      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                      • Be an Node-API addon, or
                                                                                                                      • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                     void (*fun)(void* arg),
                                                                                                                                                     void* arg);
                                                                                                                      21

                                                                                                                      void Initialize(Local exports);
                                                                                                                      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                      

                                                                                                                      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                      The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                      Context-aware addons#

                                                                                                                      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                      • Local exports,
                                                                                                                      • Local module, and
                                                                                                                      • Local context
                                                                                                                      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                        The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                        • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                        • Store the instance of the class in a v8::External, and
                                                                                                                        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                        The following example illustrates the implementation of a context-aware addon:

                                                                                                                        Worker support#

                                                                                                                        History VersionChanges v14.8.0, v12.19.0

                                                                                                                        Cleanup hooks may now be asynchronous.

                                                                                                                        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                        • Be an Node-API addon, or
                                                                                                                        • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                       void (*fun)(void* arg),
                                                                                                                                                       void* arg);
                                                                                                                        2

                                                                                                                        Kiểm tra nó với

                                                                                                                        Hàm hủy đối với đối tượng bao bọc sẽ chạy khi đối tượng được thu gom rác. Đối với thử nghiệm hàm hủy, có các cờ dòng lệnh có thể được sử dụng để có thể buộc thu gom rác. Các cờ này được cung cấp bởi công cụ JavaScript V8 bên dưới. Chúng có thể thay đổi hoặc loại bỏ bất cứ lúc nào. Chúng không được ghi lại bởi Node. js hoặc V8 và chúng không bao giờ được sử dụng ngoài thử nghiệm

                                                                                                                        Trong quá trình tắt quy trình hoặc các trình hủy luồng công nhân không được gọi bởi công cụ JS. Do đó, người dùng có trách nhiệm theo dõi các đối tượng này và đảm bảo tiêu hủy đúng cách để tránh rò rỉ tài nguyên

                                                                                                                        Xưởng bọc đồ vật

                                                                                                                        Ngoài ra, có thể sử dụng mẫu xuất xưởng để tránh tạo các thể hiện đối tượng một cách rõ ràng bằng cách sử dụng toán tử JavaScript

                                                                                                                        void Initialize(Local exports);
                                                                                                                        NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                        

                                                                                                                        There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                        The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                        In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                        When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                        Context-aware addons#

                                                                                                                        There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                        A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                        Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                        The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                        • Local exports,
                                                                                                                        • Local module, and
                                                                                                                        • Local context
                                                                                                                        • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                          The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                          • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                          • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                          • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                          • Store the instance of the class in a v8::External, and
                                                                                                                          • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                          This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                          The following example illustrates the implementation of a context-aware addon:

                                                                                                                          Worker support#

                                                                                                                          History VersionChanges v14.8.0, v12.19.0

                                                                                                                          Cleanup hooks may now be asynchronous.

                                                                                                                          In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                          • Be an Node-API addon, or
                                                                                                                          • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                          In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                          void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                         void (*fun)(void* arg),
                                                                                                                                                         void* arg);
                                                                                                                          25

                                                                                                                          Đầu tiên, phương pháp

                                                                                                                          void Initialize(Local exports);
                                                                                                                          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                          

                                                                                                                          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                          The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                          Context-aware addons#

                                                                                                                          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                          • Local exports,
                                                                                                                          • Local module, and
                                                                                                                          • Local context
                                                                                                                          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                            The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                            • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                            • Store the instance of the class in a v8::External, and
                                                                                                                            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                            The following example illustrates the implementation of a context-aware addon:

                                                                                                                            Worker support#

                                                                                                                            History VersionChanges v14.8.0, v12.19.0

                                                                                                                            Cleanup hooks may now be asynchronous.

                                                                                                                            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                            • Be an Node-API addon, or
                                                                                                                            • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                           void (*fun)(void* arg),
                                                                                                                                                           void* arg);
                                                                                                                            24 được triển khai trong
                                                                                                                            void Initialize(Local exports);
                                                                                                                            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                            

                                                                                                                            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                            The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                            Context-aware addons#

                                                                                                                            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                            • Local exports,
                                                                                                                            • Local module, and
                                                                                                                            • Local context
                                                                                                                            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                              The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                              • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                              • Store the instance of the class in a v8::External, and
                                                                                                                              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                              The following example illustrates the implementation of a context-aware addon:

                                                                                                                              Worker support#

                                                                                                                              History VersionChanges v14.8.0, v12.19.0

                                                                                                                              Cleanup hooks may now be asynchronous.

                                                                                                                              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                              • Be an Node-API addon, or
                                                                                                                              • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                             void (*fun)(void* arg),
                                                                                                                                                             void* arg);
                                                                                                                              8

                                                                                                                              Trong

                                                                                                                              void Initialize(Local exports);
                                                                                                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                              

                                                                                                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                              Context-aware addons#

                                                                                                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                              • Local exports,
                                                                                                                              • Local module, and
                                                                                                                              • Local context
                                                                                                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                • Store the instance of the class in a v8::External, and
                                                                                                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                The following example illustrates the implementation of a context-aware addon:

                                                                                                                                Worker support#

                                                                                                                                History VersionChanges v14.8.0, v12.19.0

                                                                                                                                Cleanup hooks may now be asynchronous.

                                                                                                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                • Be an Node-API addon, or
                                                                                                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                               void (*fun)(void* arg),
                                                                                                                                                               void* arg);
                                                                                                                                26, phương thức tĩnh
                                                                                                                                void Initialize(Local exports);
                                                                                                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                

                                                                                                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                Context-aware addons#

                                                                                                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                • Local exports,
                                                                                                                                • Local module, and
                                                                                                                                • Local context
                                                                                                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                  • Store the instance of the class in a v8::External, and
                                                                                                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                  The following example illustrates the implementation of a context-aware addon:

                                                                                                                                  Worker support#

                                                                                                                                  History VersionChanges v14.8.0, v12.19.0

                                                                                                                                  Cleanup hooks may now be asynchronous.

                                                                                                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                  • Be an Node-API addon, or
                                                                                                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                 void (*fun)(void* arg),
                                                                                                                                                                 void* arg);
                                                                                                                                  26 được thêm vào để xử lý việc khởi tạo đối tượng. Phương pháp này thay thế việc sử dụng
                                                                                                                                  void Initialize(Local exports);
                                                                                                                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                  

                                                                                                                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                  Context-aware addons#

                                                                                                                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                  • Local exports,
                                                                                                                                  • Local module, and
                                                                                                                                  • Local context
                                                                                                                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                    • Store the instance of the class in a v8::External, and
                                                                                                                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                    The following example illustrates the implementation of a context-aware addon:

                                                                                                                                    Worker support#

                                                                                                                                    History VersionChanges v14.8.0, v12.19.0

                                                                                                                                    Cleanup hooks may now be asynchronous.

                                                                                                                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                    • Be an Node-API addon, or
                                                                                                                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                   void (*fun)(void* arg),
                                                                                                                                                                   void* arg);
                                                                                                                                    25 trong JavaScript

                                                                                                                                    Việc triển khai trong

                                                                                                                                    void Initialize(Local exports);
                                                                                                                                    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                    

                                                                                                                                    There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                    The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                    In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                    When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                    Context-aware addons#

                                                                                                                                    There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                    A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                    Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                    The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                    • Local exports,
                                                                                                                                    • Local module, and
                                                                                                                                    • Local context
                                                                                                                                    • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                      The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                      • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                      • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                      • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                      • Store the instance of the class in a v8::External, and
                                                                                                                                      • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                      This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                      The following example illustrates the implementation of a context-aware addon:

                                                                                                                                      Worker support#

                                                                                                                                      History VersionChanges v14.8.0, v12.19.0

                                                                                                                                      Cleanup hooks may now be asynchronous.

                                                                                                                                      In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                      • Be an Node-API addon, or
                                                                                                                                      • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                      In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                      void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                     void (*fun)(void* arg),
                                                                                                                                                                     void* arg);
                                                                                                                                      28 tương tự như ví dụ trước

                                                                                                                                      Một lần nữa, để xây dựng ví dụ này, tệp

                                                                                                                                      void Initialize(Local exports);
                                                                                                                                      NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                      

                                                                                                                                      There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                      The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                      In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                      When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                      Context-aware addons#

                                                                                                                                      There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                      A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                      Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                      The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                      • Local exports,
                                                                                                                                      • Local module, and
                                                                                                                                      • Local context
                                                                                                                                      • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                        The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                        • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                        • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                        • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                        • Store the instance of the class in a v8::External, and
                                                                                                                                        • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                        This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                        The following example illustrates the implementation of a context-aware addon:

                                                                                                                                        Worker support#

                                                                                                                                        History VersionChanges v14.8.0, v12.19.0

                                                                                                                                        Cleanup hooks may now be asynchronous.

                                                                                                                                        In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                        • Be an Node-API addon, or
                                                                                                                                        • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                        In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                        void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                       void (*fun)(void* arg),
                                                                                                                                                                       void* arg);
                                                                                                                                        28 phải được thêm vào tệp
                                                                                                                                        void Initialize(Local exports);
                                                                                                                                        NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                        

                                                                                                                                        There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                        The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                        In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                        When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                        Context-aware addons#

                                                                                                                                        There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                        A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                        Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                        The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                        • Local exports,
                                                                                                                                        • Local module, and
                                                                                                                                        • Local context
                                                                                                                                        • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                          The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                          • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                          • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                          • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                          • Store the instance of the class in a v8::External, and
                                                                                                                                          • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                          This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                          The following example illustrates the implementation of a context-aware addon:

                                                                                                                                          Worker support#

                                                                                                                                          History VersionChanges v14.8.0, v12.19.0

                                                                                                                                          Cleanup hooks may now be asynchronous.

                                                                                                                                          In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                          • Be an Node-API addon, or
                                                                                                                                          • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                          In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                          void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                         void (*fun)(void* arg),
                                                                                                                                                                         void* arg);
                                                                                                                                          21

                                                                                                                                          void Initialize(Local exports);
                                                                                                                                          NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                          

                                                                                                                                          There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                          The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                          In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                          When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                          Context-aware addons#

                                                                                                                                          There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                          A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                          Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                          The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                          • Local exports,
                                                                                                                                          • Local module, and
                                                                                                                                          • Local context
                                                                                                                                          • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                            The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                            • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                            • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                            • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                            • Store the instance of the class in a v8::External, and
                                                                                                                                            • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                            This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                            The following example illustrates the implementation of a context-aware addon:

                                                                                                                                            Worker support#

                                                                                                                                            History VersionChanges v14.8.0, v12.19.0

                                                                                                                                            Cleanup hooks may now be asynchronous.

                                                                                                                                            In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                            • Be an Node-API addon, or
                                                                                                                                            • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                            In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                            void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                           void (*fun)(void* arg),
                                                                                                                                                                           void* arg);
                                                                                                                                            2

                                                                                                                                            Kiểm tra nó với

                                                                                                                                            Truyền các vật được bọc xung quanh

                                                                                                                                            Ngoài việc bọc và trả về các đối tượng C++, có thể chuyển các đối tượng được bọc xung quanh bằng cách mở gói chúng bằng nút. chức năng trợ giúp js

                                                                                                                                            void Initialize(Local exports);
                                                                                                                                            NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                            

                                                                                                                                            There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                            The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                            In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                            When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                            Context-aware addons#

                                                                                                                                            There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                            A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                            Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                            The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                            • Local exports,
                                                                                                                                            • Local module, and
                                                                                                                                            • Local context
                                                                                                                                            • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                              The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                              • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                              • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                              • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                              • Store the instance of the class in a v8::External, and
                                                                                                                                              • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                              This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                              The following example illustrates the implementation of a context-aware addon:

                                                                                                                                              Worker support#

                                                                                                                                              History VersionChanges v14.8.0, v12.19.0

                                                                                                                                              Cleanup hooks may now be asynchronous.

                                                                                                                                              In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                              • Be an Node-API addon, or
                                                                                                                                              • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                              In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                              void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                             void (*fun)(void* arg),
                                                                                                                                                                             void* arg);
                                                                                                                                              61. Các ví dụ sau đây cho thấy một hàm
                                                                                                                                              void Initialize(Local exports);
                                                                                                                                              NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                              

                                                                                                                                              There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                              The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                              In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                              When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                              Context-aware addons#

                                                                                                                                              There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                              A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                              Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                              The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                              • Local exports,
                                                                                                                                              • Local module, and
                                                                                                                                              • Local context
                                                                                                                                              • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                                The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                                • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                                • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                                • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                                • Store the instance of the class in a v8::External, and
                                                                                                                                                • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                                This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                                The following example illustrates the implementation of a context-aware addon:

                                                                                                                                                Worker support#

                                                                                                                                                History VersionChanges v14.8.0, v12.19.0

                                                                                                                                                Cleanup hooks may now be asynchronous.

                                                                                                                                                In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                                • Be an Node-API addon, or
                                                                                                                                                • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                                In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                                void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                               void (*fun)(void* arg),
                                                                                                                                                                               void* arg);
                                                                                                                                                62 có thể lấy hai đối tượng
                                                                                                                                                void Initialize(Local exports);
                                                                                                                                                NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                                

                                                                                                                                                There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                                The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                                In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                                When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                                Context-aware addons#

                                                                                                                                                There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                                A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                                Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                                The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                                • Local exports,
                                                                                                                                                • Local module, and
                                                                                                                                                • Local context
                                                                                                                                                • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                                  The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                                  • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                                  • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                                  • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                                  • Store the instance of the class in a v8::External, and
                                                                                                                                                  • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                                  This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                                  The following example illustrates the implementation of a context-aware addon:

                                                                                                                                                  Worker support#

                                                                                                                                                  History VersionChanges v14.8.0, v12.19.0

                                                                                                                                                  Cleanup hooks may now be asynchronous.

                                                                                                                                                  In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                                  • Be an Node-API addon, or
                                                                                                                                                  • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                                  In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                                  void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                                 void (*fun)(void* arg),
                                                                                                                                                                                 void* arg);
                                                                                                                                                  63 làm đối số đầu vào

                                                                                                                                                  Trong

                                                                                                                                                  void Initialize(Local exports);
                                                                                                                                                  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
                                                                                                                                                  

                                                                                                                                                  There is no semi-colon after NODE_MODULE as it's not a function (see node.h).

                                                                                                                                                  The module_name must match the filename of the final binary (excluding the .node suffix).

                                                                                                                                                  In the hello.cc example, then, the initialization function is Initialize and the addon module name is addon.

                                                                                                                                                  When building addons with node-gyp, using the macro NODE_GYP_MODULE_NAME as the first parameter of NODE_MODULE() will ensure that the name of the final binary will be passed to NODE_MODULE().

                                                                                                                                                  Context-aware addons#

                                                                                                                                                  There are environments in which Node.js addons may need to be loaded multiple times in multiple contexts. For example, the Electron runtime runs multiple instances of Node.js in a single process. Each instance will have its own require() cache, and thus each instance will need a native addon to behave correctly when loaded via require(). This means that the addon must support multiple initializations.

                                                                                                                                                  A context-aware addon can be constructed by using the macro NODE_MODULE_INITIALIZER, which expands to the name of a function which Node.js will expect to find when it loads an addon. An addon can thus be initialized as in the following example:

                                                                                                                                                  Another option is to use the macro NODE_MODULE_INIT(), which will also construct a context-aware addon. Unlike NODE_MODULE(), which is used to construct an addon around a given addon initializer function, NODE_MODULE_INIT() serves as the declaration of such an initializer to be followed by a function body.

                                                                                                                                                  The following three variables may be used inside the function body following an invocation of NODE_MODULE_INIT():

                                                                                                                                                  • Local exports,
                                                                                                                                                  • Local module, and
                                                                                                                                                  • Local context
                                                                                                                                                  • The choice to build a context-aware addon carries with it the responsibility of carefully managing global static data. Since the addon may be loaded multiple times, potentially even from different threads, any global static data stored in the addon must be properly protected, and must not contain any persistent references to JavaScript objects. The reason for this is that JavaScript objects are only valid in one context, and will likely cause a crash when accessed from the wrong context or from a different thread than the one on which they were created.

                                                                                                                                                    The context-aware addon can be structured to avoid global static data by performing the following steps:

                                                                                                                                                    • Define a class which will hold per-addon-instance data and which has a static member of the form
                                                                                                                                                    • Heap-allocate an instance of this class in the addon initializer. This can be accomplished using the new keyword.
                                                                                                                                                    • Call node::AddEnvironmentCleanupHook(), passing it the above-created instance and a pointer to DeleteInstance(). This will ensure the instance is deleted when the environment is torn down.
                                                                                                                                                    • Store the instance of the class in a v8::External, and
                                                                                                                                                    • Pass the v8::External to all methods exposed to JavaScript by passing it to v8::FunctionTemplate::New() or v8::Function::New() which creates the native-backed JavaScript functions. The third parameter of v8::FunctionTemplate::New() or v8::Function::New() accepts the v8::External and makes it available in the native callback using the v8::FunctionCallbackInfo::Data() method.

                                                                                                                                                    This will ensure that the per-addon-instance data reaches each binding that can be called from JavaScript. The per-addon-instance data must also be passed into any asynchronous callbacks the addon may create.

                                                                                                                                                    The following example illustrates the implementation of a context-aware addon:

                                                                                                                                                    Worker support#

                                                                                                                                                    History VersionChanges v14.8.0, v12.19.0

                                                                                                                                                    Cleanup hooks may now be asynchronous.

                                                                                                                                                    In order to be loaded from multiple Node.js environments, such as a main thread and a Worker thread, an add-on needs to either:

                                                                                                                                                    • Be an Node-API addon, or
                                                                                                                                                    • Be declared as context-aware using NODE_MODULE_INIT() as described above

                                                                                                                                                    In order to support Worker threads, addons need to clean up any resources they may have allocated when such a thread exists. This can be achieved through the usage of the AddEnvironmentCleanupHook() function:

                                                                                                                                                    void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                                                                                                                                                                                   void (*fun)(void* arg),
                                                                                                                                                                                   void* arg);
                                                                                                                                                    26, một phương thức công khai mới được thêm vào để cho phép truy cập vào các giá trị riêng tư sau khi mở gói đối tượng