Skip to content

Commit 195ec28

Browse files
committed
Address review comments
1 parent a8f6380 commit 195ec28

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

doc/basic_env.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# BasicEnv
22

3-
The opaque data structure containing the environment in which the request is
4-
being run.
3+
The data structure containing the environment in which the request is being run.
54

65
The `Napi::BasicEnv` object is usually created and passed by the Node.js runtime
76
or node-addon-api infrastructure.
@@ -50,13 +49,13 @@ void SetInstanceData(T* data) const;
5049
- `[template] fini`: A function to call when the instance data is to be deleted.
5150
Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If
5251
not given, the default finalizer will be used, which simply uses the `delete`
53-
operator to destroy `T*` when the addon instance is unloaded.
52+
operator to destroy `T*` when the add-on instance is unloaded.
5453
- `[in] data`: A pointer to data that will be associated with the instance of
55-
the addon for the duration of its lifecycle.
54+
the add-on for the duration of its lifecycle.
5655
5756
Associates a data item stored at `T* data` with the current instance of the
58-
addon. The item will be passed to the function `fini` which gets called when an
59-
instance of the addon is unloaded.
57+
add-on. The item will be passed to the function `fini` which gets called when an
58+
instance of the add-on is unloaded.
6059
6160
### SetInstanceData
6261
@@ -73,16 +72,16 @@ void SetInstanceData(DataType* data, HintType* hint) const;
7372
- `[template] fini`: A function to call when the instance data is to be deleted.
7473
Accepts a function of the form `void CleanupData(Napi::Env env, DataType* data,
7574
HintType* hint)`. If not given, the default finalizer will be used, which simply
76-
uses the `delete` operator to destroy `T*` when the addon instance is unloaded.
75+
uses the `delete` operator to destroy `T*` when the add-on instance is unloaded.
7776
- `[in] data`: A pointer to data that will be associated with the instance of
78-
the addon for the duration of its lifecycle.
77+
the add-on for the duration of its lifecycle.
7978
- `[in] hint`: A pointer to data that will be associated with the instance of
80-
the addon for the duration of its lifecycle and will be passed as a hint to
81-
`fini` when the addon instance is unloaded.
79+
the add-on for the duration of its lifecycle and will be passed as a hint to
80+
`fini` when the add-on instance is unloaded.
8281

8382
Associates a data item stored at `T* data` with the current instance of the
84-
addon. The item will be passed to the function `fini` which gets called when an
85-
instance of the addon is unloaded. This overload accepts an additional hint to
83+
add-on. The item will be passed to the function `fini` which gets called when an
84+
instance of the add-on is unloaded. This overload accepts an additional hint to
8685
be passed to `fini`.
8786

8887
### GetModuleFileName
@@ -91,10 +90,10 @@ be passed to `fini`.
9190
const char* Napi::Env::GetModuleFileName() const;
9291
```
9392

94-
Returns a A URL containing the absolute path of the location from which the
95-
add-on was loaded. For a file on the local file system it will start with
96-
`file://`. The string is null-terminated and owned by env and must thus not be
97-
modified or freed. It is only valid while the add-on is loaded.
93+
Returns a URL containing the absolute path of the location from which the add-on
94+
was loaded. For a file on the local file system it will start with `file://`.
95+
The string is null-terminated and owned by env and must thus not be modified or
96+
freed. It is only valid while the add-on is loaded.
9897

9998
### AddCleanupHook
10099

doc/env.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Env
22

3-
The opaque data structure containing the environment in which the request is
4-
being run.
3+
Class `Napi::Env` inherits from class [`Napi::BasicEnv`][].
4+
5+
The data structure containing the environment in which the request is being run.
56

67
The `Napi::Env` object is usually created and passed by the Node.js runtime or
78
node-addon-api infrastructure.
@@ -82,4 +83,5 @@ The `script` can be any of the following types:
8283
- `const char *`
8384
- `const std::string &`
8485
86+
[`Napi::BasicEnv`]: ./basic_env.md
8587
[Finalization]: ./finalization.md

doc/finalization.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Various node-addon-api methods accept a templated `Finalizer finalizeCallback`
44
parameter. This parameter represents a native callback function that runs in
55
response to a garbage collection event. A finalizer is considered a _basic_
66
finalizer if the callback only utilizes a certain subset of APIs, which may
7-
provide optimizations, improved execution, or other benefits.
7+
provide more efficient memory management, optimizations, improved execution, or
8+
other benefits.
89

910
In general, it is best to use basic finalizers whenever possible (eg. when
1011
access to JavaScript is _not_ needed).
@@ -28,7 +29,7 @@ Use of basic finalizers may allow the engine to perform optimizations when
2829
scheduling or executing the callback. For example, V8 does not allow access to
2930
the engine heap during garbage collection. Restricting finalizers from accessing
3031
the engine heap allows the callback to execute during garbage collection,
31-
providing a chance to free native memory on the current tick.
32+
providing a chance to free native memory eagerly.
3233

3334
In general, APIs that access engine heap are not allowed in basic finalizers.
3435

@@ -46,12 +47,13 @@ Napi::ArrayBuffer::New(
4647
## Scheduling Finalizers
4748
4849
In addition to passing finalizers to `Napi::External`s and other Node-API
49-
constructs, use `Napi::BasicEnv::PostFinalize(Napi::BasicEnv, Finalizer)` to
50-
schedule a callback to run outside of the garbage collector finalization. Since
51-
the associated native memory may already be freed by the basic finalizer, any
52-
additional data may be passed eg. via the finalizer's parameters (`T data*`,
53-
`Hint hint*`) or via lambda capture. This allows for freeing native data in a
54-
basic finalizer, while executing any JavaScript code in an additional finalizer.
50+
constructs, `Napi::BasicEnv::PostFinalize(Napi::BasicEnv, Finalizer)` can be
51+
used to schedule a callback to run outside of the garbage collector
52+
finalization. Since the associated native memory may already be freed by the
53+
basic finalizer, any additional data may be passed eg. via the finalizer's
54+
parameters (`T data*`, `Hint hint*`) or via lambda capture. This allows for
55+
freeing native data in a basic finalizer, while executing any JavaScript code in
56+
an additional finalizer.
5557
5658
### Example
5759

napi.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ namespace NAPI_CPP_CUSTOM_NAMESPACE {
187187
#endif
188188

189189
// Forward declarations
190-
class BasicEnv;
191190
class Env;
192191
class Value;
193192
class Boolean;

0 commit comments

Comments
 (0)