array_buffer.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef GIN_ARRAY_BUFFER_H_
  5. #define GIN_ARRAY_BUFFER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "gin/converter.h"
  11. #include "gin/gin_export.h"
  12. #include "v8/include/v8-array-buffer.h"
  13. #include "v8/include/v8-forward.h"
  14. namespace gin {
  15. class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
  16. public:
  17. void* Allocate(size_t length) override;
  18. void* AllocateUninitialized(size_t length) override;
  19. void Free(void* data, size_t length) override;
  20. GIN_EXPORT static ArrayBufferAllocator* SharedInstance();
  21. };
  22. class GIN_EXPORT ArrayBuffer {
  23. public:
  24. ArrayBuffer();
  25. ArrayBuffer(v8::Isolate* isolate, v8::Local<v8::ArrayBuffer> buffer);
  26. ArrayBuffer(const ArrayBuffer&) = delete;
  27. ~ArrayBuffer();
  28. ArrayBuffer& operator=(const ArrayBuffer& other);
  29. void* bytes() const {
  30. return backing_store_ ? backing_store_->Data() : nullptr;
  31. }
  32. size_t num_bytes() const {
  33. return backing_store_ ? backing_store_->ByteLength() : 0;
  34. }
  35. private:
  36. std::shared_ptr<v8::BackingStore> backing_store_;
  37. };
  38. template<>
  39. struct GIN_EXPORT Converter<ArrayBuffer> {
  40. static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
  41. ArrayBuffer* out);
  42. };
  43. class GIN_EXPORT ArrayBufferView {
  44. public:
  45. ArrayBufferView();
  46. ArrayBufferView(v8::Isolate* isolate, v8::Local<v8::ArrayBufferView> view);
  47. ArrayBufferView(const ArrayBufferView&) = delete;
  48. ~ArrayBufferView();
  49. ArrayBufferView& operator=(const ArrayBufferView& other);
  50. void* bytes() const {
  51. return static_cast<uint8_t*>(array_buffer_.bytes()) + offset_;
  52. }
  53. size_t num_bytes() const { return num_bytes_; }
  54. private:
  55. ArrayBuffer array_buffer_;
  56. size_t offset_;
  57. size_t num_bytes_;
  58. };
  59. template<>
  60. struct GIN_EXPORT Converter<ArrayBufferView> {
  61. static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
  62. ArrayBufferView* out);
  63. };
  64. } // namespace gin
  65. #endif // GIN_ARRAY_BUFFER_H_