You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nan_typedarray_contents.h 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2018 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. HandleScope scope;
  16. size_t length = 0;
  17. void* data = NULL;
  18. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  19. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  20. if (from->IsArrayBufferView()) {
  21. v8::Local<v8::ArrayBufferView> array =
  22. v8::Local<v8::ArrayBufferView>::Cast(from);
  23. const size_t byte_length = array->ByteLength();
  24. const ptrdiff_t byte_offset = array->ByteOffset();
  25. v8::Local<v8::ArrayBuffer> buffer = array->Buffer();
  26. length = byte_length / sizeof(T);
  27. // Actually it's 7.9 here but this would lead to ABI issues with Node.js 13
  28. // using 7.8 till 13.2.0.
  29. #if (V8_MAJOR_VERSION >= 8)
  30. data = static_cast<char*>(buffer->GetBackingStore()->Data()) + byte_offset;
  31. #else
  32. data = static_cast<char*>(buffer->GetContents().Data()) + byte_offset;
  33. #endif
  34. }
  35. #else
  36. if (from->IsObject() && !from->IsNull()) {
  37. v8::Local<v8::Object> array = v8::Local<v8::Object>::Cast(from);
  38. MaybeLocal<v8::Value> buffer = Get(array,
  39. New<v8::String>("buffer").ToLocalChecked());
  40. MaybeLocal<v8::Value> byte_length = Get(array,
  41. New<v8::String>("byteLength").ToLocalChecked());
  42. MaybeLocal<v8::Value> byte_offset = Get(array,
  43. New<v8::String>("byteOffset").ToLocalChecked());
  44. if (!buffer.IsEmpty() &&
  45. !byte_length.IsEmpty() && byte_length.ToLocalChecked()->IsUint32() &&
  46. !byte_offset.IsEmpty() && byte_offset.ToLocalChecked()->IsUint32()) {
  47. data = array->GetIndexedPropertiesExternalArrayData();
  48. if(data) {
  49. length = byte_length.ToLocalChecked()->Uint32Value() / sizeof(T);
  50. }
  51. }
  52. }
  53. #endif
  54. #if defined(_MSC_VER) && _MSC_VER >= 1900 || __cplusplus >= 201103L
  55. assert(reinterpret_cast<uintptr_t>(data) % alignof (T) == 0);
  56. #elif defined(_MSC_VER) && _MSC_VER >= 1600 || defined(__GNUC__)
  57. assert(reinterpret_cast<uintptr_t>(data) % __alignof(T) == 0);
  58. #else
  59. assert(reinterpret_cast<uintptr_t>(data) % sizeof (T) == 0);
  60. #endif
  61. length_ = length;
  62. data_ = static_cast<T*>(data);
  63. }
  64. inline size_t length() const { return length_; }
  65. inline T* operator*() { return data_; }
  66. inline const T* operator*() const { return data_; }
  67. private:
  68. NAN_DISALLOW_ASSIGN_COPY_MOVE(TypedArrayContents)
  69. //Disable heap allocation
  70. void *operator new(size_t size);
  71. void operator delete(void *, size_t) {
  72. abort();
  73. }
  74. size_t length_;
  75. T* data_;
  76. };
  77. #endif // NAN_TYPEDARRAY_CONTENTS_H_