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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
0 [void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
1 trong Node. js], cũng có sẵn trực tuyếnvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
2void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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 tinvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
4void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ướcvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
6 vàvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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 addonvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
8 sau sử dụngvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
9void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
20. Để làm như vậy, hãy tạo một tệp có tênvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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 jsvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
2void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
22 được đóng gói và phân phối với Node. js như một phần củavoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ếpvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
22 có thể cài đặt nó bằng lệnh . Xemvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ảngvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
21 đã được tạo, hãy sử dụngvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ệpvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
30 [trên nền tảng Unix] hoặc tệpvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
31void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
33 để tạo tệpvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
20 đã biên dịch. Điều này sẽ được đưa vào thư mụcvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
35void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
24 để cài đặt Nút. js addon, npm sử dụng phiên bảnvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ộtvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
20 đã xây dựngvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
20], các addon có thể sử dụng gói liên kết để tải mô-đun đã biên dịchvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ẫuvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
22 tương tự nhưvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
3void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
23 thích hợp [e. g.void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
24] vàvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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 ývoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
-
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ẵnvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
-
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
22 bằng cách sử dụng cờvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ộcvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
Đ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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
6void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
Phần mở rộng tên tệp của nhị phân addon đã biên dịch là
20 [trái ngược vớimodule.exports.hello = [] => 'world';
21 hoặcmodule.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ệpmodule.exports.hello = [] => 'world';
20 và khởi tạo các tệp đó dưới dạng liên kết độngmodule.exports.hello = [] => 'world';
Khi gọi, phần mở rộng
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ệpmodule.exports.hello = [] => 'world';
27 trong cùng thư mục với tệp nhị phânmodule.exports.hello = [] => 'world';
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
20,void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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 AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
01 để biết cách sử dụng nóvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
4 bằng nội dung sau. Tất cả các hướng dẫn khác vẫn giữ nguyênvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
21 sauvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
2void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
05, chỉ cần thêm tên tệp bổ sung vào mảngvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
06void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
2module.exports.hello = [] => 'world';
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
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ụngvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
22void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
0void AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
đố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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
09 nhận toàn bộ đối tượngvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
20 làm đối số thứ hai. Điều này cho phép addon ghi đè hoàn toànvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
21 bằng một chức năng duy nhất thay vì thêm chức năng dưới dạngvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
Để 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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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
, andLocal 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 toDeleteInstance[]
. 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 tov8::FunctionTemplate::New[]
orv8::Function::New[]
which creates the native-backed JavaScript functions. The third parameter ofv8::FunctionTemplate::New[]
orv8::Function::New[]
accepts thev8::External
and makes it available in the native callback using thev8::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 theAddEnvironmentCleanupHook[]
function:
23 lặp lại chuỗi được chuyển đếnvoid AddEnvironmentCleanupHook[v8::Isolate* isolate, void [*fun][void* arg], void* arg];
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 [seenode.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 isInitialize
and the addon module name isaddon
.When building addons with
node-gyp
, using the macroNODE_GYP_MODULE_NAME
as the first parameter ofNODE_MODULE[]
will ensure that the name of the final binary will be passed toNODE_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 viarequire[]
. 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. UnlikeNODE_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,