Ohm-Management - Projektarbeit B-ME
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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. data = static_cast<char*>(buffer->GetContents().Data()) + byte_offset;
  28. }
  29. #else
  30. if (from->IsObject() && !from->IsNull()) {
  31. v8::Local<v8::Object> array = v8::Local<v8::Object>::Cast(from);
  32. MaybeLocal<v8::Value> buffer = Get(array,
  33. New<v8::String>("buffer").ToLocalChecked());
  34. MaybeLocal<v8::Value> byte_length = Get(array,
  35. New<v8::String>("byteLength").ToLocalChecked());
  36. MaybeLocal<v8::Value> byte_offset = Get(array,
  37. New<v8::String>("byteOffset").ToLocalChecked());
  38. if (!buffer.IsEmpty() &&
  39. !byte_length.IsEmpty() && byte_length.ToLocalChecked()->IsUint32() &&
  40. !byte_offset.IsEmpty() && byte_offset.ToLocalChecked()->IsUint32()) {
  41. data = array->GetIndexedPropertiesExternalArrayData();
  42. if(data) {
  43. length = byte_length.ToLocalChecked()->Uint32Value() / sizeof(T);
  44. }
  45. }
  46. }
  47. #endif
  48. #if defined(_MSC_VER) && _MSC_VER >= 1900 || __cplusplus >= 201103L
  49. assert(reinterpret_cast<uintptr_t>(data) % alignof (T) == 0);
  50. #elif defined(_MSC_VER) && _MSC_VER >= 1600 || defined(__GNUC__)
  51. assert(reinterpret_cast<uintptr_t>(data) % __alignof(T) == 0);
  52. #else
  53. assert(reinterpret_cast<uintptr_t>(data) % sizeof (T) == 0);
  54. #endif
  55. length_ = length;
  56. data_ = static_cast<T*>(data);
  57. }
  58. inline size_t length() const { return length_; }
  59. inline T* operator*() { return data_; }
  60. inline const T* operator*() const { return data_; }
  61. private:
  62. NAN_DISALLOW_ASSIGN_COPY_MOVE(TypedArrayContents)
  63. //Disable heap allocation
  64. void *operator new(size_t size);
  65. void operator delete(void *, size_t) {
  66. abort();
  67. }
  68. size_t length_;
  69. T* data_;
  70. };
  71. #endif // NAN_TYPEDARRAY_CONTENTS_H_