@@ -22,6 +22,9 @@ Returns a new `Napi::Buffer` object.
22
22
23
23
### New
24
24
25
+ > When `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` is defined, this method is not available.
26
+ > See [External Buffer][] for more information.
27
+
25
28
Wraps the provided external data into a new `Napi::Buffer` object.
26
29
27
30
The `Napi::Buffer` object does not assume ownership for the data and expects it to be
@@ -47,6 +50,9 @@ Returns a new `Napi::Buffer` object.
47
50
48
51
### New
49
52
53
+ > When ` NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED ` is defined, this method is not available.
54
+ > See [ External Buffer] [ ] for more information.
55
+
50
56
Wraps the provided external data into a new ` Napi::Buffer ` object.
51
57
52
58
The ` Napi::Buffer ` object does not assume ownership for the data and expects it
@@ -72,6 +78,9 @@ Returns a new `Napi::Buffer` object.
72
78
73
79
### New
74
80
81
+ > When `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` is defined, this method is not available.
82
+ > See [External Buffer][] for more information.
83
+
75
84
Wraps the provided external data into a new `Napi::Buffer` object.
76
85
77
86
The `Napi::Buffer` object does not assume ownership for the data and expects it to be
@@ -98,6 +107,93 @@ static Napi::Buffer<T> Napi::Buffer::New(napi_env env,
98
107
99
108
Returns a new ` Napi::Buffer ` object.
100
109
110
+ ### NewOrCopy
111
+
112
+ Wraps the provided external data into a new ` Napi::Buffer ` object. When the
113
+ [ external buffer] [ ] is not supported, allocates a new ` Napi::Buffer ` object and
114
+ copies the provided external data into it.
115
+
116
+ The ` Napi::Buffer ` object does not assume ownership for the data and expects it to be
117
+ valid for the lifetime of the object. Since the ` Napi::Buffer ` is subject to garbage
118
+ collection this overload is only suitable for data which is static and never
119
+ needs to be freed.
120
+
121
+ This factory method will not provide the caller with an opportunity to free the
122
+ data when the ` Napi::Buffer ` gets garbage-collected. If you need to free the
123
+ data retained by the ` Napi::Buffer ` object please use other variants of the
124
+ ` Napi::Buffer::New ` factory method that accept ` Napi::Finalizer ` , which is a
125
+ function that will be invoked when the ` Napi::Buffer ` object has been
126
+ destroyed.
127
+
128
+ ``` cpp
129
+ static Napi::Buffer<T> Napi::Buffer::NewOrCopy (napi_env env, T* data, size_t length);
130
+ ```
131
+
132
+ - `[in] env`: The environment in which to create the `Napi::Buffer` object.
133
+ - `[in] data`: The pointer to the external data to expose.
134
+ - `[in] length`: The number of `T` elements in the external data.
135
+
136
+ Returns a new `Napi::Buffer` object.
137
+
138
+ ### NewOrCopy
139
+
140
+ Wraps the provided external data into a new `Napi::Buffer` object. When the
141
+ [external buffer][] is not supported, allocates a new `Napi::Buffer` object and
142
+ copies the provided external data into it and the `finalizeCallback` is invoked
143
+ immediately.
144
+
145
+ The `Napi::Buffer` object does not assume ownership for the data and expects it
146
+ to be valid for the lifetime of the object. The data can only be freed once the
147
+ `finalizeCallback` is invoked to indicate that the `Napi::Buffer` has been released.
148
+
149
+ ```cpp
150
+ template <typename Finalizer>
151
+ static Napi::Buffer<T> Napi::Buffer::NewOrCopy(napi_env env,
152
+ T* data,
153
+ size_t length,
154
+ Finalizer finalizeCallback);
155
+ ```
156
+
157
+ - ` [in] env ` : The environment in which to create the ` Napi::Buffer ` object.
158
+ - ` [in] data ` : The pointer to the external data to expose.
159
+ - ` [in] length ` : The number of ` T ` elements in the external data.
160
+ - ` [in] finalizeCallback ` : The function to be called when the ` Napi::Buffer ` is
161
+ destroyed. It must implement ` operator() ` , accept an Napi::Env, a ` T* ` (which is the
162
+ external data pointer), and return ` void ` .
163
+
164
+ Returns a new ` Napi::Buffer ` object.
165
+
166
+ ### NewOrCopy
167
+
168
+ Wraps the provided external data into a new ` Napi::Buffer ` object. When the
169
+ [ external buffer] [ ] is not supported, allocates a new ` Napi::Buffer ` object and
170
+ copies the provided external data into it and the ` finalizeCallback ` is invoked
171
+ immediately.
172
+
173
+ The ` Napi::Buffer ` object does not assume ownership for the data and expects it to be
174
+ valid for the lifetime of the object. The data can only be freed once the
175
+ ` finalizeCallback ` is invoked to indicate that the ` Napi::Buffer ` has been released.
176
+
177
+ ``` cpp
178
+ template <typename Finalizer, typename Hint>
179
+ static Napi::Buffer<T> Napi::Buffer::NewOrCopy (napi_env env,
180
+ T* data,
181
+ size_t length,
182
+ Finalizer finalizeCallback,
183
+ Hint* finalizeHint);
184
+ ```
185
+
186
+ - `[in] env`: The environment in which to create the `Napi::Buffer` object.
187
+ - `[in] data`: The pointer to the external data to expose.
188
+ - `[in] length`: The number of `T` elements in the external data.
189
+ - `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is
190
+ destroyed. It must implement `operator()`, accept an Napi::Env, a `T*` (which is the
191
+ external data pointer) and `Hint*`, and return `void`.
192
+ - `[in] finalizeHint`: The hint to be passed as the second parameter of the
193
+ finalize callback.
194
+
195
+ Returns a new `Napi::Buffer` object.
196
+
101
197
### Copy
102
198
103
199
Allocates a new `Napi::Buffer` object and copies the provided external data into it.
@@ -148,3 +244,4 @@ size_t Napi::Buffer::Length() const;
148
244
Returns the number of ` T ` elements in the external data.
149
245
150
246
[ `Napi::Uint8Array` ] : ./typed_array_of.md
247
+ [ External Buffer ] : ./external_buffer.md
0 commit comments