nan_typedarray_contents.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2016 NAN contributors
  5. *
  6. * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
  7. ********************************************************************/
  8. #ifndef NAN_TYPEDARRAY_CONTENTS_H_
  9. #define NAN_TYPEDARRAY_CONTENTS_H_
  10. template<typename T>
  11. class TypedArrayContents {
  12. public:
  13. inline explicit TypedArrayContents(v8::Local<v8::Value> from) :
  14. length_(0), data_(NULL) {
  15. size_t length = 0;
  16. void* data = NULL;
  17. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  18. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  19. if (from->IsArrayBufferView()) {
  20. v8::Local<v8::ArrayBufferView> array =
  21. v8::Local<v8::ArrayBufferView>::Cast(from);
  22. const size_t byte_length = array->ByteLength();
  23. const ptrdiff_t byte_offset = array->ByteOffset();
  24. v8::Local<v8::ArrayBuffer> buffer = array->Buffer();
  25. length = byte_length / sizeof(T);
  26. data = static_cast<char*>(buffer->GetContents().Data()) + byte_offset;
  27. }
  28. #else
  29. if (from->IsObject() && !from->IsNull()) {
  30. v8::Local<v8::Object> array = v8::Local<v8::Object>::Cast(from);
  31. MaybeLocal<v8::Value> buffer = Get(array,
  32. New<v8::String>("buffer").ToLocalChecked());
  33. MaybeLocal<v8::Value> byte_length = Get(array,
  34. New<v8::String>("byteLength").ToLocalChecked());
  35. MaybeLocal<v8::Value> byte_offset = Get(array,
  36. New<v8::String>("byteOffset").ToLocalChecked());
  37. if (!buffer.IsEmpty() &&
  38. !byte_length.IsEmpty() && byte_length.ToLocalChecked()->IsUint32() &&
  39. !byte_offset.IsEmpty() && byte_offset.ToLocalChecked()->IsUint32()) {
  40. data = array->GetIndexedPropertiesExternalArrayData();
  41. if(data) {
  42. length = byte_length.ToLocalChecked()->Uint32Value() / sizeof(T);
  43. }
  44. }
  45. }
  46. #endif
  47. #if defined(_MSC_VER) && _MSC_VER >= 1900 || __cplusplus >= 201103L
  48. assert(reinterpret_cast<uintptr_t>(data) % alignof (T) == 0);
  49. #elif defined(_MSC_VER) && _MSC_VER >= 1600 || defined(__GNUC__)
  50. assert(reinterpret_cast<uintptr_t>(data) % __alignof(T) == 0);
  51. #else
  52. assert(reinterpret_cast<uintptr_t>(data) % sizeof (T) == 0);
  53. #endif
  54. length_ = length;
  55. data_ = static_cast<T*>(data);
  56. }
  57. inline size_t length() const { return length_; }
  58. inline T* operator*() { return data_; }
  59. inline const T* operator*() const { return data_; }
  60. private:
  61. NAN_DISALLOW_ASSIGN_COPY_MOVE(TypedArrayContents)
  62. //Disable heap allocation
  63. void *operator new(size_t size);
  64. void operator delete(void *, size_t) {
  65. abort();
  66. }
  67. size_t length_;
  68. T* data_;
  69. };
  70. #endif // NAN_TYPEDARRAY_CONTENTS_H_