nan_typedarray_contents.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * NAN - Native Abstractions for Node.js
00003  *
00004  * Copyright (c) 2016 NAN contributors
00005  *
00006  * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
00007  ********************************************************************/
00008 
00009 #ifndef NAN_TYPEDARRAY_CONTENTS_H_
00010 #define NAN_TYPEDARRAY_CONTENTS_H_
00011 
00012 template<typename T>
00013 class TypedArrayContents {
00014  public:
00015   inline explicit TypedArrayContents(v8::Local<v8::Value> from) :
00016       length_(0), data_(NULL) {
00017 
00018     size_t length = 0;
00019     void*  data = NULL;
00020 
00021 #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 ||                      \
00022   (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
00023 
00024     if (from->IsArrayBufferView()) {
00025       v8::Local<v8::ArrayBufferView> array =
00026         v8::Local<v8::ArrayBufferView>::Cast(from);
00027 
00028       const size_t    byte_length = array->ByteLength();
00029       const ptrdiff_t byte_offset = array->ByteOffset();
00030       v8::Local<v8::ArrayBuffer> buffer = array->Buffer();
00031 
00032       length = byte_length / sizeof(T);
00033       data   = static_cast<char*>(buffer->GetContents().Data()) + byte_offset;
00034     }
00035 
00036 #else
00037 
00038     if (from->IsObject() && !from->IsNull()) {
00039       v8::Local<v8::Object> array = v8::Local<v8::Object>::Cast(from);
00040 
00041       MaybeLocal<v8::Value> buffer = Get(array,
00042         New<v8::String>("buffer").ToLocalChecked());
00043       MaybeLocal<v8::Value> byte_length = Get(array,
00044         New<v8::String>("byteLength").ToLocalChecked());
00045       MaybeLocal<v8::Value> byte_offset = Get(array,
00046         New<v8::String>("byteOffset").ToLocalChecked());
00047 
00048       if (!buffer.IsEmpty() &&
00049           !byte_length.IsEmpty() && byte_length.ToLocalChecked()->IsUint32() &&
00050           !byte_offset.IsEmpty() && byte_offset.ToLocalChecked()->IsUint32()) {
00051         data = array->GetIndexedPropertiesExternalArrayData();
00052         if(data) {
00053           length = byte_length.ToLocalChecked()->Uint32Value() / sizeof(T);
00054         }
00055       }
00056     }
00057 
00058 #endif
00059 
00060 #if defined(_MSC_VER) && _MSC_VER >= 1900 || __cplusplus >= 201103L
00061     assert(reinterpret_cast<uintptr_t>(data) % alignof (T) == 0);
00062 #elif defined(_MSC_VER) && _MSC_VER >= 1600 || defined(__GNUC__)
00063     assert(reinterpret_cast<uintptr_t>(data) % __alignof(T) == 0);
00064 #else
00065     assert(reinterpret_cast<uintptr_t>(data) % sizeof (T) == 0);
00066 #endif
00067 
00068     length_ = length;
00069     data_   = static_cast<T*>(data);
00070   }
00071 
00072   inline size_t length() const      { return length_; }
00073   inline T* operator*()             { return data_;   }
00074   inline const T* operator*() const { return data_;   }
00075 
00076  private:
00077   NAN_DISALLOW_ASSIGN_COPY_MOVE(TypedArrayContents)
00078 
00079   //Disable heap allocation
00080   void *operator new(size_t size);
00081   void operator delete(void *, size_t);
00082 
00083   size_t  length_;
00084   T*      data_;
00085 };
00086 
00087 #endif  // NAN_TYPEDARRAY_CONTENTS_H_


dji_ronin
Author(s):
autogenerated on Sat Jun 8 2019 20:15:31