2121* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222* THE SOFTWARE.
2323*
24- * static ArrayBuffer* New(void *ptr = 0, size_t length = 0, bool release = false)
25- * static ArrayBuffer* New(const char *ptr, int length = -1, bool release = false)
26- * static ArrayBuffer* New(const char *ptr, size_t length = 0, bool release = false)
27- * static ArrayBuffer* New(char *ptr, size_t length = 0, bool release = false)
28- * static ArrayBuffer* New(const v8::Local<v8::ArrayBuffer> &arrayBuffer)
29- * static ArrayBuffer* New(const T &content)
30- * static ArrayBuffer* New(const T &content, const char *ptr, size_t length)
31- * static ArrayBuffer* New(const T &content, void *ptr, size_t length = 0)
32- * static ArrayBuffer* New(const T &content, const char *ptr, int length = -1)
33- * static ArrayBuffer* New(const v8::Local<v8::Value> &arg)
24+ * NODEJS_VERSION < 12
25+ * static ArrayBuffer* New(const char *str = 0)
26+ * static ArrayBuffer* New(const char *str, size_t length)
27+ * static ArrayBuffer* New(const std::string &data)
28+ * static ArrayBuffer* New(const v8::Local<v8::Object> &arrayBuffer)
29+ * static ArrayBuffer* New(const v8::Local<v8::Value> &arg)
30+ *
31+ * v8::Local<v8::Object> ToArrayBuffer() const
32+ * v8::Local<v8::String> ToString() const
3433*
35- * static ArrayBuffer* New(v8::Isolate *isolate = 0, void *ptr = 0, size_t length = 0, bool release = false)
36- * static ArrayBuffer* New(v8::Isolate *isolate, const char *ptr, int length = -1, bool release = false)
37- * static ArrayBuffer* New(v8::Isolate *isolate, const char *ptr, size_t length, bool release = false)
38- * static ArrayBuffer* New(v8::Isolate *isolate, char *ptr, size_t length = 0, bool release = false)
39- * static ArrayBuffer* New(v8::Isolate *isolate, const v8::Local<v8::ArrayBuffer> &arrayBuffer)
40- * static ArrayBuffer* New(v8::Isolate *isolate, const T &content)
41- * static ArrayBuffer* New(v8::Isolate *isolate, const T &content, void *ptr, size_t length = 0)
42- * static ArrayBuffer* New(v8::Isolate *isolate, const T &content, const char *ptr, size_t length)
43- * static ArrayBuffer* New(v8::Isolate *isolate, const T &content, const char *ptr, int length = -1)
44- * static ArrayBuffer* New(v8::Isolate *isolate, const v8::Local<v8::Value> &arg)
34+ * const char *ToUtf8() const
35+ * void *Data() const
36+ * size_t Length() const
37+ * size_t ByteLength() const
4538*
46- * ArrayBufferWrapper(v8::Isolate *isolate, const T &content, void *ptr = 0, size_t length = 0)
47- * ArrayBufferWrapper(const T &content, void *ptr = 0, size_t length = 0)
4839*
49- * v8::Local<v8::ArrayBuffer> ToArrayBuffer() const
50- * v8::Local<v8::String> ToString() const
51- * void *Data() const
52- * size_t Length() const
53- * size_t ByteLength() const
54- * const T &Unwrap() const
40+ * NODEJS_VERSION >= 12
41+ * static ArrayBuffer* New(const char *str = 0)
42+ * static ArrayBuffer* New(const char *str, size_t length)
43+ * static ArrayBuffer* New(const std::string &data)
44+ * static ArrayBuffer* New(const v8::Local<v8::ArrayBuffer> &arrayBuffer)
45+ * static ArrayBuffer* New(const v8::Local<v8::Value> &arg)
46+ *
47+ * static ArrayBuffer* New(v8::Isolate *isolate, const char *str = 0)
48+ * static ArrayBuffer* New(v8::Isolate *isolate, const char *str, size_t length)
49+ * static ArrayBuffer* New(v8::Isolate *isolate, const std::string &data)
50+ * static ArrayBuffer* New(v8::Isolate *isolate, const v8::Local<v8::ArrayBuffer> &arrayBuffer)
51+ * static ArrayBuffer* New(v8::Isolate *isolate, const v8::Local<v8::Value> &arg)
52+ *
53+ * v8::Local<v8::ArrayBuffer> ToArrayBuffer(v8::Isolate *isolate = 0) const
54+ * v8::Local<v8::String> ToStringv8::Isolate *isolate = 0() const
55+ *
56+ * const char *ToUtf8() const
57+ * void *Data() const
58+ * size_t Length() const
59+ * size_t ByteLength() const
5560*
5661*/
5762
6772#include < string>
6873
6974namespace node {
70- #if NODE_MINOR_VERSION >= 11
71-
7275 class ArrayBuffer {
7376 public:
7477 inline static ArrayBuffer* New (const char *str = 0 ) {
78+ #if NODE_MINOR_VERSION >= 11
7579 return ArrayBuffer::New (v8::Isolate::GetCurrent (), std::string (str));
80+ #else
81+ return ArrayBuffer::New (std::string (str));
82+ #endif
7683 }
7784
7885 inline static ArrayBuffer* New (const char *str, size_t length) {
86+ #if NODE_MINOR_VERSION >= 11
7987 return ArrayBuffer::New (v8::Isolate::GetCurrent (), str, length);
88+ #else
89+ return ArrayBuffer::New (str, length);
90+ #endif
8091 }
8192
8293 inline static ArrayBuffer* New (const std::string &data) {
94+ #if NODE_MINOR_VERSION >= 11
8395 return ArrayBuffer::New (v8::Isolate::GetCurrent (), data.data (), data.size ());
96+ #else
97+ return ArrayBuffer::New (data.data (), data.size ());
98+ #endif
8499 }
85100
101+ #if NODE_MINOR_VERSION >= 11
86102 inline static ArrayBuffer* New (const v8::Local<v8::ArrayBuffer> &arrayBuffer) {
87103 return ArrayBuffer::New (v8::Isolate::GetCurrent (), arrayBuffer);
88104 }
@@ -107,6 +123,7 @@ namespace node {
107123 ArrayBuffer *buffer = new ArrayBuffer ();
108124 v8::Local<v8::ArrayBuffer> arrayBuffer;
109125
126+ buffer->_data = 0 ;
110127 buffer->_len = length;
111128
112129 if (length) {
@@ -146,13 +163,11 @@ namespace node {
146163 if (ptr.IsEmpty ()) {
147164 v8::Local<v8::Value> uintArray = v8::Uint8Array::New (arrayBuffer, 0 , arrayBuffer->ByteLength ());
148165 return ArrayBuffer::New (isolate, uintArray);
149- }
150- else {
166+ } else {
151167 v8::Local<v8::External> ext = v8::Local<v8::External>::Cast (ptr);
152168 return static_cast <ArrayBuffer*>(ext->Value ());
153169 }
154- }
155- else {
170+ } else {
156171 ArrayBuffer *buffer = new ArrayBuffer ();
157172 v8::ArrayBuffer::Contents content = arrayBuffer->Externalize ();
158173
@@ -210,13 +225,92 @@ namespace node {
210225 }
211226
212227 v8::EscapableHandleScope scope (isolate);
213- v8::Local<v8::String> retval = v8::String::NewFromUtf8 (isolate,
214- ArrayBuffer::ToUtf8 (),
215- v8::String::kNormalString ,
216- ArrayBuffer::Length ());
228+ v8::Local<v8::String> retval = v8::String::NewFromUtf8 (isolate, ArrayBuffer::ToUtf8 (), v8::String::kNormalString , ArrayBuffer::Length ());
217229 return scope.Escape (retval);
218230 }
219231
232+ #else
233+
234+ inline static ArrayBuffer* New (const char *str, size_t length) {
235+ ArrayBuffer *buffer = new ArrayBuffer ();
236+
237+ v8::Local<v8::Object> global = v8::Context::GetCurrent ()->Global ();
238+ v8::Local<v8::Object> constructor = v8::Object::Cast (global->Get (v8::String::New (" ArrayBuffer" )));
239+ v8::Local<v8::Object> arrayBuffer = constructor->CallAsConstructor ();
240+
241+ buffer->_data = 0 ;
242+ buffer->_len = length;
243+
244+ if (length) {
245+ buffer->_data = new char [length + 1 ];
246+ buffer->_data [length] = ' \0 ' ;
247+
248+ for (size_t index = 0 ; index < length; index++) {
249+ buffer->_data [index] = str[index];
250+ }
251+
252+ arrayBuffer->SetIndexedPropertiesToExternalArrayData (buffer->_data , v8::kExternalByteArray , buffer->_len );
253+ }
254+
255+ buffer->_arrayBuffer .Reset (isolate, arrayBuffer);
256+ buffer->_arrayBuffer .SetWeak (buffer, ArrayBuffer::onDispose);
257+ buffer->_arrayBuffer .MarkIndependent ();
258+
259+ arrayBuffer->SetHiddenValue (v8::String::New (" node::ArrayBuffer" ), v8::External::New (buffer));
260+ return buffer;
261+ }
262+
263+ inline static ArrayBuffer* New (const v8::Local<v8::Object> &arrayBuffer) {
264+ if (!arrayBuffer.IsEmpty ()) {
265+ v8::Local<v8::Value> ptr = arrayBuffer->GetHiddenValue (v8::String::NewFromUtf8 (isolate, " node::ArrayBuffer" ));
266+
267+ if (!ptr.IsEmpty ()) {
268+ v8::Local<v8::External> ext = v8::Local<v8::External>::Cast (ptr);
269+ return static_cast <ArrayBuffer*>(ext->Value ());
270+ } else {
271+ if (arrayBuffer->HasIndexedPropertiesInExternalArrayData ()) {
272+ int length = arrayBuffer->GetIndexedPropertiesExternalArrayDataLength ();
273+
274+ if (length > 0 ) {
275+ return ArrayBuffer::New (arrayBuffer->GetIndexedPropertiesExternalArrayData (), static_cast <size_t >(length));
276+ }
277+ }
278+ }
279+ }
280+
281+ return ArrayBuffer::New ();
282+ }
283+
284+ inline static ArrayBuffer* New (const v8::Local<v8::Value> &arg) {
285+ if (!arg.IsEmpty ()) {
286+ if (arg->IsObject ()) {
287+ v8::Local<v8::Object> arrayBuffer = v8::Local<v8::Object>::Cast (arg);
288+ return ArrayBuffer::New (isolate, arrayBuffer);
289+ }
290+
291+ if (arg->IsString ()) {
292+ v8::String::Utf8Value str (arg->ToString ());
293+ return ArrayBuffer::New (isolate, *str, str.length ());
294+ }
295+ }
296+
297+ return ArrayBuffer::New ();
298+ }
299+
300+ inline v8::Local<v8::ArrayBuffer> ToArrayBuffer () const {
301+ v8::HandleScope scope;
302+ v8::Local<v8::Object> arrayBuffer = v8::Local<v8::Object>::New (_arrayBuffer);
303+ return scope.Close (arrayBuffer);
304+ }
305+
306+ inline v8::Local<v8::String> ToString () const {
307+ v8::HandleScope scope;
308+ v8::Local<v8::String> str = v8::String::New (ArrayBuffer::ToUtf8 (), ArrayBuffer::Length ());
309+ return scope.Close (str);
310+ }
311+
312+ #endif
313+
220314 inline const char *ToUtf8 () const {
221315 return _data;
222316 }
@@ -234,41 +328,53 @@ namespace node {
234328 }
235329
236330 static inline void onDispose (const v8::WeakCallbackData<v8::ArrayBuffer, ArrayBuffer> &info) {
331+ #if NODE_MINOR_VERSION >= 11
237332 v8::Isolate *isolate = info.GetIsolate ();
238333 v8::HandleScope scope (isolate);
334+ #else
335+ v8::HandleScope scope;
336+ #endif
239337
240338 ArrayBuffer *wrap = info.GetParameter ();
241339
242340 if (wrap) {
341+ #if NODE_MINOR_VERSION >= 11
243342 v8::Local<v8::ArrayBuffer> arrayBuffer = v8::Local<v8::ArrayBuffer>::New (isolate, wrap->_arrayBuffer );
343+ #else
344+ v8::Local<v8::Object> arrayBuffer = v8::Local<v8::Object>::New (wrap->_arrayBuffer );
345+ #endif
244346 wrap->_arrayBuffer .Reset ();
245347
246348 if (!arrayBuffer.IsEmpty ()) {
349+ #if NODE_MINOR_VERSION >= 11
247350 arrayBuffer->DeleteHiddenValue (v8::String::NewFromUtf8 (isolate, " node::ArrayBuffer" ));
351+ #else
352+ arrayBuffer->DeleteHiddenValue (v8::String::New (" node::ArrayBuffer" ));
353+ #endif
248354 }
249355
250356 delete wrap;
251357 }
252358 }
253359
254- private:
360+ private:
255361 virtual ~ArrayBuffer () {
256362 if (_len) {
257363 delete[] _data;
258364 }
259365 }
260366
261- protected:
367+ protected:
262368 char * _data;
263369 size_t _len;
264- v8::Persistent<v8::ArrayBuffer> _arrayBuffer;
265- };
266370
371+ #if NODE_MINOR_VERSION >= 11
372+ v8::Persistent<v8::ArrayBuffer> _arrayBuffer;
267373#else
268-
269-
374+ v8::Persistent<v8::Object> _arrayBuffer;
270375#endif
271376
377+ };
272378};
273379
274380#endif
0 commit comments