11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef INCLUDE_V8_ARRAY_BUFFER_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_V8_ARRAY_BUFFER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <stddef.h>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include <memory>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "v8-local-handle.h"  // NOLINT(build/include_directory)
131cb0ef41Sopenharmony_ci#include "v8-object.h"        // NOLINT(build/include_directory)
141cb0ef41Sopenharmony_ci#include "v8config.h"         // NOLINT(build/include_directory)
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cinamespace v8 {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciclass SharedArrayBuffer;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
211cb0ef41Sopenharmony_ci// The number of required internal fields can be defined by embedder.
221cb0ef41Sopenharmony_ci#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
231cb0ef41Sopenharmony_ci#endif
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cienum class ArrayBufferCreationMode { kInternalized, kExternalized };
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci/**
281cb0ef41Sopenharmony_ci * A wrapper around the backing store (i.e. the raw memory) of an array buffer.
291cb0ef41Sopenharmony_ci * See a document linked in http://crbug.com/v8/9908 for more information.
301cb0ef41Sopenharmony_ci *
311cb0ef41Sopenharmony_ci * The allocation and destruction of backing stores is generally managed by
321cb0ef41Sopenharmony_ci * V8. Clients should always use standard C++ memory ownership types (i.e.
331cb0ef41Sopenharmony_ci * std::unique_ptr and std::shared_ptr) to manage lifetimes of backing stores
341cb0ef41Sopenharmony_ci * properly, since V8 internal objects may alias backing stores.
351cb0ef41Sopenharmony_ci *
361cb0ef41Sopenharmony_ci * This object does not keep the underlying |ArrayBuffer::Allocator| alive by
371cb0ef41Sopenharmony_ci * default. Use Isolate::CreateParams::array_buffer_allocator_shared when
381cb0ef41Sopenharmony_ci * creating the Isolate to make it hold a reference to the allocator itself.
391cb0ef41Sopenharmony_ci */
401cb0ef41Sopenharmony_ciclass V8_EXPORT BackingStore : public v8::internal::BackingStoreBase {
411cb0ef41Sopenharmony_ci public:
421cb0ef41Sopenharmony_ci  ~BackingStore();
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  /**
451cb0ef41Sopenharmony_ci   * Return a pointer to the beginning of the memory block for this backing
461cb0ef41Sopenharmony_ci   * store. The pointer is only valid as long as this backing store object
471cb0ef41Sopenharmony_ci   * lives.
481cb0ef41Sopenharmony_ci   */
491cb0ef41Sopenharmony_ci  void* Data() const;
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  /**
521cb0ef41Sopenharmony_ci   * The length (in bytes) of this backing store.
531cb0ef41Sopenharmony_ci   */
541cb0ef41Sopenharmony_ci  size_t ByteLength() const;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  /**
571cb0ef41Sopenharmony_ci   * Indicates whether the backing store was created for an ArrayBuffer or
581cb0ef41Sopenharmony_ci   * a SharedArrayBuffer.
591cb0ef41Sopenharmony_ci   */
601cb0ef41Sopenharmony_ci  bool IsShared() const;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  /**
631cb0ef41Sopenharmony_ci   * Prevent implicit instantiation of operator delete with size_t argument.
641cb0ef41Sopenharmony_ci   * The size_t argument would be incorrect because ptr points to the
651cb0ef41Sopenharmony_ci   * internal BackingStore object.
661cb0ef41Sopenharmony_ci   */
671cb0ef41Sopenharmony_ci  void operator delete(void* ptr) { ::operator delete(ptr); }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  /**
701cb0ef41Sopenharmony_ci   * Wrapper around ArrayBuffer::Allocator::Reallocate that preserves IsShared.
711cb0ef41Sopenharmony_ci   * Assumes that the backing_store was allocated by the ArrayBuffer allocator
721cb0ef41Sopenharmony_ci   * of the given isolate.
731cb0ef41Sopenharmony_ci   */
741cb0ef41Sopenharmony_ci  static std::unique_ptr<BackingStore> Reallocate(
751cb0ef41Sopenharmony_ci      v8::Isolate* isolate, std::unique_ptr<BackingStore> backing_store,
761cb0ef41Sopenharmony_ci      size_t byte_length);
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  /**
791cb0ef41Sopenharmony_ci   * This callback is used only if the memory block for a BackingStore cannot be
801cb0ef41Sopenharmony_ci   * allocated with an ArrayBuffer::Allocator. In such cases the destructor of
811cb0ef41Sopenharmony_ci   * the BackingStore invokes the callback to free the memory block.
821cb0ef41Sopenharmony_ci   */
831cb0ef41Sopenharmony_ci  using DeleterCallback = void (*)(void* data, size_t length,
841cb0ef41Sopenharmony_ci                                   void* deleter_data);
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  /**
871cb0ef41Sopenharmony_ci   * If the memory block of a BackingStore is static or is managed manually,
881cb0ef41Sopenharmony_ci   * then this empty deleter along with nullptr deleter_data can be passed to
891cb0ef41Sopenharmony_ci   * ArrayBuffer::NewBackingStore to indicate that.
901cb0ef41Sopenharmony_ci   *
911cb0ef41Sopenharmony_ci   * The manually managed case should be used with caution and only when it
921cb0ef41Sopenharmony_ci   * is guaranteed that the memory block freeing happens after detaching its
931cb0ef41Sopenharmony_ci   * ArrayBuffer.
941cb0ef41Sopenharmony_ci   */
951cb0ef41Sopenharmony_ci  static void EmptyDeleter(void* data, size_t length, void* deleter_data);
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci private:
981cb0ef41Sopenharmony_ci  /**
991cb0ef41Sopenharmony_ci   * See [Shared]ArrayBuffer::GetBackingStore and
1001cb0ef41Sopenharmony_ci   * [Shared]ArrayBuffer::NewBackingStore.
1011cb0ef41Sopenharmony_ci   */
1021cb0ef41Sopenharmony_ci  BackingStore();
1031cb0ef41Sopenharmony_ci};
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
1061cb0ef41Sopenharmony_ci// Use v8::BackingStore::DeleterCallback instead.
1071cb0ef41Sopenharmony_ciusing BackingStoreDeleterCallback = void (*)(void* data, size_t length,
1081cb0ef41Sopenharmony_ci                                             void* deleter_data);
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci#endif
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci/**
1131cb0ef41Sopenharmony_ci * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
1141cb0ef41Sopenharmony_ci */
1151cb0ef41Sopenharmony_ciclass V8_EXPORT ArrayBuffer : public Object {
1161cb0ef41Sopenharmony_ci public:
1171cb0ef41Sopenharmony_ci  /**
1181cb0ef41Sopenharmony_ci   * A thread-safe allocator that V8 uses to allocate |ArrayBuffer|'s memory.
1191cb0ef41Sopenharmony_ci   * The allocator is a global V8 setting. It has to be set via
1201cb0ef41Sopenharmony_ci   * Isolate::CreateParams.
1211cb0ef41Sopenharmony_ci   *
1221cb0ef41Sopenharmony_ci   * Memory allocated through this allocator by V8 is accounted for as external
1231cb0ef41Sopenharmony_ci   * memory by V8. Note that V8 keeps track of the memory for all internalized
1241cb0ef41Sopenharmony_ci   * |ArrayBuffer|s. Responsibility for tracking external memory (using
1251cb0ef41Sopenharmony_ci   * Isolate::AdjustAmountOfExternalAllocatedMemory) is handed over to the
1261cb0ef41Sopenharmony_ci   * embedder upon externalization and taken over upon internalization (creating
1271cb0ef41Sopenharmony_ci   * an internalized buffer from an existing buffer).
1281cb0ef41Sopenharmony_ci   *
1291cb0ef41Sopenharmony_ci   * Note that it is unsafe to call back into V8 from any of the allocator
1301cb0ef41Sopenharmony_ci   * functions.
1311cb0ef41Sopenharmony_ci   */
1321cb0ef41Sopenharmony_ci  class V8_EXPORT Allocator {
1331cb0ef41Sopenharmony_ci   public:
1341cb0ef41Sopenharmony_ci    virtual ~Allocator() = default;
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci    /**
1371cb0ef41Sopenharmony_ci     * Allocate |length| bytes. Return nullptr if allocation is not successful.
1381cb0ef41Sopenharmony_ci     * Memory should be initialized to zeroes.
1391cb0ef41Sopenharmony_ci     */
1401cb0ef41Sopenharmony_ci    virtual void* Allocate(size_t length) = 0;
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci    /**
1431cb0ef41Sopenharmony_ci     * Allocate |length| bytes. Return nullptr if allocation is not successful.
1441cb0ef41Sopenharmony_ci     * Memory does not have to be initialized.
1451cb0ef41Sopenharmony_ci     */
1461cb0ef41Sopenharmony_ci    virtual void* AllocateUninitialized(size_t length) = 0;
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci    /**
1491cb0ef41Sopenharmony_ci     * Free the memory block of size |length|, pointed to by |data|.
1501cb0ef41Sopenharmony_ci     * That memory is guaranteed to be previously allocated by |Allocate|.
1511cb0ef41Sopenharmony_ci     */
1521cb0ef41Sopenharmony_ci    virtual void Free(void* data, size_t length) = 0;
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci    /**
1551cb0ef41Sopenharmony_ci     * Reallocate the memory block of size |old_length| to a memory block of
1561cb0ef41Sopenharmony_ci     * size |new_length| by expanding, contracting, or copying the existing
1571cb0ef41Sopenharmony_ci     * memory block. If |new_length| > |old_length|, then the new part of
1581cb0ef41Sopenharmony_ci     * the memory must be initialized to zeros. Return nullptr if reallocation
1591cb0ef41Sopenharmony_ci     * is not successful.
1601cb0ef41Sopenharmony_ci     *
1611cb0ef41Sopenharmony_ci     * The caller guarantees that the memory block was previously allocated
1621cb0ef41Sopenharmony_ci     * using Allocate or AllocateUninitialized.
1631cb0ef41Sopenharmony_ci     *
1641cb0ef41Sopenharmony_ci     * The default implementation allocates a new block and copies data.
1651cb0ef41Sopenharmony_ci     */
1661cb0ef41Sopenharmony_ci    virtual void* Reallocate(void* data, size_t old_length, size_t new_length);
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci    /**
1691cb0ef41Sopenharmony_ci     * ArrayBuffer allocation mode. kNormal is a malloc/free style allocation,
1701cb0ef41Sopenharmony_ci     * while kReservation is for larger allocations with the ability to set
1711cb0ef41Sopenharmony_ci     * access permissions.
1721cb0ef41Sopenharmony_ci     */
1731cb0ef41Sopenharmony_ci    enum class AllocationMode { kNormal, kReservation };
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci    /**
1761cb0ef41Sopenharmony_ci     * Convenience allocator.
1771cb0ef41Sopenharmony_ci     *
1781cb0ef41Sopenharmony_ci     * When the sandbox is enabled, this allocator will allocate its backing
1791cb0ef41Sopenharmony_ci     * memory inside the sandbox. Otherwise, it will rely on malloc/free.
1801cb0ef41Sopenharmony_ci     *
1811cb0ef41Sopenharmony_ci     * Caller takes ownership, i.e. the returned object needs to be freed using
1821cb0ef41Sopenharmony_ci     * |delete allocator| once it is no longer in use.
1831cb0ef41Sopenharmony_ci     */
1841cb0ef41Sopenharmony_ci    static Allocator* NewDefaultAllocator();
1851cb0ef41Sopenharmony_ci  };
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci  /**
1881cb0ef41Sopenharmony_ci   * Data length in bytes.
1891cb0ef41Sopenharmony_ci   */
1901cb0ef41Sopenharmony_ci  size_t ByteLength() const;
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  /**
1931cb0ef41Sopenharmony_ci   * Create a new ArrayBuffer. Allocate |byte_length| bytes.
1941cb0ef41Sopenharmony_ci   * Allocated memory will be owned by a created ArrayBuffer and
1951cb0ef41Sopenharmony_ci   * will be deallocated when it is garbage-collected,
1961cb0ef41Sopenharmony_ci   * unless the object is externalized.
1971cb0ef41Sopenharmony_ci   */
1981cb0ef41Sopenharmony_ci  static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  /**
2011cb0ef41Sopenharmony_ci   * Create a new ArrayBuffer with an existing backing store.
2021cb0ef41Sopenharmony_ci   * The created array keeps a reference to the backing store until the array
2031cb0ef41Sopenharmony_ci   * is garbage collected. Note that the IsExternal bit does not affect this
2041cb0ef41Sopenharmony_ci   * reference from the array to the backing store.
2051cb0ef41Sopenharmony_ci   *
2061cb0ef41Sopenharmony_ci   * In future IsExternal bit will be removed. Until then the bit is set as
2071cb0ef41Sopenharmony_ci   * follows. If the backing store does not own the underlying buffer, then
2081cb0ef41Sopenharmony_ci   * the array is created in externalized state. Otherwise, the array is created
2091cb0ef41Sopenharmony_ci   * in internalized state. In the latter case the array can be transitioned
2101cb0ef41Sopenharmony_ci   * to the externalized state using Externalize(backing_store).
2111cb0ef41Sopenharmony_ci   */
2121cb0ef41Sopenharmony_ci  static Local<ArrayBuffer> New(Isolate* isolate,
2131cb0ef41Sopenharmony_ci                                std::shared_ptr<BackingStore> backing_store);
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci  /**
2161cb0ef41Sopenharmony_ci   * Returns a new standalone BackingStore that is allocated using the array
2171cb0ef41Sopenharmony_ci   * buffer allocator of the isolate. The result can be later passed to
2181cb0ef41Sopenharmony_ci   * ArrayBuffer::New.
2191cb0ef41Sopenharmony_ci   *
2201cb0ef41Sopenharmony_ci   * If the allocator returns nullptr, then the function may cause GCs in the
2211cb0ef41Sopenharmony_ci   * given isolate and re-try the allocation. If GCs do not help, then the
2221cb0ef41Sopenharmony_ci   * function will crash with an out-of-memory error.
2231cb0ef41Sopenharmony_ci   */
2241cb0ef41Sopenharmony_ci  static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
2251cb0ef41Sopenharmony_ci                                                       size_t byte_length);
2261cb0ef41Sopenharmony_ci  /**
2271cb0ef41Sopenharmony_ci   * Returns a new standalone BackingStore that takes over the ownership of
2281cb0ef41Sopenharmony_ci   * the given buffer. The destructor of the BackingStore invokes the given
2291cb0ef41Sopenharmony_ci   * deleter callback.
2301cb0ef41Sopenharmony_ci   *
2311cb0ef41Sopenharmony_ci   * The result can be later passed to ArrayBuffer::New. The raw pointer
2321cb0ef41Sopenharmony_ci   * to the buffer must not be passed again to any V8 API function.
2331cb0ef41Sopenharmony_ci   */
2341cb0ef41Sopenharmony_ci  static std::unique_ptr<BackingStore> NewBackingStore(
2351cb0ef41Sopenharmony_ci      void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
2361cb0ef41Sopenharmony_ci      void* deleter_data);
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci  /**
2391cb0ef41Sopenharmony_ci   * Returns true if this ArrayBuffer may be detached.
2401cb0ef41Sopenharmony_ci   */
2411cb0ef41Sopenharmony_ci  bool IsDetachable() const;
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci  /**
2441cb0ef41Sopenharmony_ci   * Returns true if this ArrayBuffer has been detached.
2451cb0ef41Sopenharmony_ci   */
2461cb0ef41Sopenharmony_ci  bool WasDetached() const;
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci  /**
2491cb0ef41Sopenharmony_ci   * Detaches this ArrayBuffer and all its views (typed arrays).
2501cb0ef41Sopenharmony_ci   * Detaching sets the byte length of the buffer and all typed arrays to zero,
2511cb0ef41Sopenharmony_ci   * preventing JavaScript from ever accessing underlying backing store.
2521cb0ef41Sopenharmony_ci   * ArrayBuffer should have been externalized and must be detachable.
2531cb0ef41Sopenharmony_ci   */
2541cb0ef41Sopenharmony_ci  void Detach();
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci  /**
2571cb0ef41Sopenharmony_ci   * Get a shared pointer to the backing store of this array buffer. This
2581cb0ef41Sopenharmony_ci   * pointer coordinates the lifetime management of the internal storage
2591cb0ef41Sopenharmony_ci   * with any live ArrayBuffers on the heap, even across isolates. The embedder
2601cb0ef41Sopenharmony_ci   * should not attempt to manage lifetime of the storage through other means.
2611cb0ef41Sopenharmony_ci   *
2621cb0ef41Sopenharmony_ci   * The returned shared pointer will not be empty, even if the ArrayBuffer has
2631cb0ef41Sopenharmony_ci   * been detached. Use |WasDetached| to tell if it has been detached instead.
2641cb0ef41Sopenharmony_ci   */
2651cb0ef41Sopenharmony_ci  std::shared_ptr<BackingStore> GetBackingStore();
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci  /**
2681cb0ef41Sopenharmony_ci   * More efficient shortcut for GetBackingStore()->Data(). The returned pointer
2691cb0ef41Sopenharmony_ci   * is valid as long as the ArrayBuffer is alive.
2701cb0ef41Sopenharmony_ci   */
2711cb0ef41Sopenharmony_ci  void* Data() const;
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ci  V8_INLINE static ArrayBuffer* Cast(Value* value) {
2741cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS
2751cb0ef41Sopenharmony_ci    CheckCast(value);
2761cb0ef41Sopenharmony_ci#endif
2771cb0ef41Sopenharmony_ci    return static_cast<ArrayBuffer*>(value);
2781cb0ef41Sopenharmony_ci  }
2791cb0ef41Sopenharmony_ci
2801cb0ef41Sopenharmony_ci  static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
2811cb0ef41Sopenharmony_ci  static const int kEmbedderFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ci private:
2841cb0ef41Sopenharmony_ci  ArrayBuffer();
2851cb0ef41Sopenharmony_ci  static void CheckCast(Value* obj);
2861cb0ef41Sopenharmony_ci};
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_ci#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
2891cb0ef41Sopenharmony_ci// The number of required internal fields can be defined by embedder.
2901cb0ef41Sopenharmony_ci#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
2911cb0ef41Sopenharmony_ci#endif
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_ci/**
2941cb0ef41Sopenharmony_ci * A base class for an instance of one of "views" over ArrayBuffer,
2951cb0ef41Sopenharmony_ci * including TypedArrays and DataView (ES6 draft 15.13).
2961cb0ef41Sopenharmony_ci */
2971cb0ef41Sopenharmony_ciclass V8_EXPORT ArrayBufferView : public Object {
2981cb0ef41Sopenharmony_ci public:
2991cb0ef41Sopenharmony_ci  /**
3001cb0ef41Sopenharmony_ci   * Returns underlying ArrayBuffer.
3011cb0ef41Sopenharmony_ci   */
3021cb0ef41Sopenharmony_ci  Local<ArrayBuffer> Buffer();
3031cb0ef41Sopenharmony_ci  /**
3041cb0ef41Sopenharmony_ci   * Byte offset in |Buffer|.
3051cb0ef41Sopenharmony_ci   */
3061cb0ef41Sopenharmony_ci  size_t ByteOffset();
3071cb0ef41Sopenharmony_ci  /**
3081cb0ef41Sopenharmony_ci   * Size of a view in bytes.
3091cb0ef41Sopenharmony_ci   */
3101cb0ef41Sopenharmony_ci  size_t ByteLength();
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci  /**
3131cb0ef41Sopenharmony_ci   * Copy the contents of the ArrayBufferView's buffer to an embedder defined
3141cb0ef41Sopenharmony_ci   * memory without additional overhead that calling ArrayBufferView::Buffer
3151cb0ef41Sopenharmony_ci   * might incur.
3161cb0ef41Sopenharmony_ci   *
3171cb0ef41Sopenharmony_ci   * Will write at most min(|byte_length|, ByteLength) bytes starting at
3181cb0ef41Sopenharmony_ci   * ByteOffset of the underlying buffer to the memory starting at |dest|.
3191cb0ef41Sopenharmony_ci   * Returns the number of bytes actually written.
3201cb0ef41Sopenharmony_ci   */
3211cb0ef41Sopenharmony_ci  size_t CopyContents(void* dest, size_t byte_length);
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci  /**
3241cb0ef41Sopenharmony_ci   * Returns true if ArrayBufferView's backing ArrayBuffer has already been
3251cb0ef41Sopenharmony_ci   * allocated.
3261cb0ef41Sopenharmony_ci   */
3271cb0ef41Sopenharmony_ci  bool HasBuffer() const;
3281cb0ef41Sopenharmony_ci
3291cb0ef41Sopenharmony_ci  V8_INLINE static ArrayBufferView* Cast(Value* value) {
3301cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS
3311cb0ef41Sopenharmony_ci    CheckCast(value);
3321cb0ef41Sopenharmony_ci#endif
3331cb0ef41Sopenharmony_ci    return static_cast<ArrayBufferView*>(value);
3341cb0ef41Sopenharmony_ci  }
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ci  static const int kInternalFieldCount =
3371cb0ef41Sopenharmony_ci      V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT;
3381cb0ef41Sopenharmony_ci  static const int kEmbedderFieldCount =
3391cb0ef41Sopenharmony_ci      V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT;
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ci private:
3421cb0ef41Sopenharmony_ci  ArrayBufferView();
3431cb0ef41Sopenharmony_ci  static void CheckCast(Value* obj);
3441cb0ef41Sopenharmony_ci};
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci/**
3471cb0ef41Sopenharmony_ci * An instance of DataView constructor (ES6 draft 15.13.7).
3481cb0ef41Sopenharmony_ci */
3491cb0ef41Sopenharmony_ciclass V8_EXPORT DataView : public ArrayBufferView {
3501cb0ef41Sopenharmony_ci public:
3511cb0ef41Sopenharmony_ci  static Local<DataView> New(Local<ArrayBuffer> array_buffer,
3521cb0ef41Sopenharmony_ci                             size_t byte_offset, size_t length);
3531cb0ef41Sopenharmony_ci  static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
3541cb0ef41Sopenharmony_ci                             size_t byte_offset, size_t length);
3551cb0ef41Sopenharmony_ci  V8_INLINE static DataView* Cast(Value* value) {
3561cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS
3571cb0ef41Sopenharmony_ci    CheckCast(value);
3581cb0ef41Sopenharmony_ci#endif
3591cb0ef41Sopenharmony_ci    return static_cast<DataView*>(value);
3601cb0ef41Sopenharmony_ci  }
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci private:
3631cb0ef41Sopenharmony_ci  DataView();
3641cb0ef41Sopenharmony_ci  static void CheckCast(Value* obj);
3651cb0ef41Sopenharmony_ci};
3661cb0ef41Sopenharmony_ci
3671cb0ef41Sopenharmony_ci/**
3681cb0ef41Sopenharmony_ci * An instance of the built-in SharedArrayBuffer constructor.
3691cb0ef41Sopenharmony_ci */
3701cb0ef41Sopenharmony_ciclass V8_EXPORT SharedArrayBuffer : public Object {
3711cb0ef41Sopenharmony_ci public:
3721cb0ef41Sopenharmony_ci  /**
3731cb0ef41Sopenharmony_ci   * Data length in bytes.
3741cb0ef41Sopenharmony_ci   */
3751cb0ef41Sopenharmony_ci  size_t ByteLength() const;
3761cb0ef41Sopenharmony_ci
3771cb0ef41Sopenharmony_ci  /**
3781cb0ef41Sopenharmony_ci   * Create a new SharedArrayBuffer. Allocate |byte_length| bytes.
3791cb0ef41Sopenharmony_ci   * Allocated memory will be owned by a created SharedArrayBuffer and
3801cb0ef41Sopenharmony_ci   * will be deallocated when it is garbage-collected,
3811cb0ef41Sopenharmony_ci   * unless the object is externalized.
3821cb0ef41Sopenharmony_ci   */
3831cb0ef41Sopenharmony_ci  static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ci  /**
3861cb0ef41Sopenharmony_ci   * Create a new SharedArrayBuffer with an existing backing store.
3871cb0ef41Sopenharmony_ci   * The created array keeps a reference to the backing store until the array
3881cb0ef41Sopenharmony_ci   * is garbage collected. Note that the IsExternal bit does not affect this
3891cb0ef41Sopenharmony_ci   * reference from the array to the backing store.
3901cb0ef41Sopenharmony_ci   *
3911cb0ef41Sopenharmony_ci   * In future IsExternal bit will be removed. Until then the bit is set as
3921cb0ef41Sopenharmony_ci   * follows. If the backing store does not own the underlying buffer, then
3931cb0ef41Sopenharmony_ci   * the array is created in externalized state. Otherwise, the array is created
3941cb0ef41Sopenharmony_ci   * in internalized state. In the latter case the array can be transitioned
3951cb0ef41Sopenharmony_ci   * to the externalized state using Externalize(backing_store).
3961cb0ef41Sopenharmony_ci   */
3971cb0ef41Sopenharmony_ci  static Local<SharedArrayBuffer> New(
3981cb0ef41Sopenharmony_ci      Isolate* isolate, std::shared_ptr<BackingStore> backing_store);
3991cb0ef41Sopenharmony_ci
4001cb0ef41Sopenharmony_ci  /**
4011cb0ef41Sopenharmony_ci   * Returns a new standalone BackingStore that is allocated using the array
4021cb0ef41Sopenharmony_ci   * buffer allocator of the isolate. The result can be later passed to
4031cb0ef41Sopenharmony_ci   * SharedArrayBuffer::New.
4041cb0ef41Sopenharmony_ci   *
4051cb0ef41Sopenharmony_ci   * If the allocator returns nullptr, then the function may cause GCs in the
4061cb0ef41Sopenharmony_ci   * given isolate and re-try the allocation. If GCs do not help, then the
4071cb0ef41Sopenharmony_ci   * function will crash with an out-of-memory error.
4081cb0ef41Sopenharmony_ci   */
4091cb0ef41Sopenharmony_ci  static std::unique_ptr<BackingStore> NewBackingStore(Isolate* isolate,
4101cb0ef41Sopenharmony_ci                                                       size_t byte_length);
4111cb0ef41Sopenharmony_ci  /**
4121cb0ef41Sopenharmony_ci   * Returns a new standalone BackingStore that takes over the ownership of
4131cb0ef41Sopenharmony_ci   * the given buffer. The destructor of the BackingStore invokes the given
4141cb0ef41Sopenharmony_ci   * deleter callback.
4151cb0ef41Sopenharmony_ci   *
4161cb0ef41Sopenharmony_ci   * The result can be later passed to SharedArrayBuffer::New. The raw pointer
4171cb0ef41Sopenharmony_ci   * to the buffer must not be passed again to any V8 functions.
4181cb0ef41Sopenharmony_ci   */
4191cb0ef41Sopenharmony_ci  static std::unique_ptr<BackingStore> NewBackingStore(
4201cb0ef41Sopenharmony_ci      void* data, size_t byte_length, v8::BackingStore::DeleterCallback deleter,
4211cb0ef41Sopenharmony_ci      void* deleter_data);
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_ci  /**
4241cb0ef41Sopenharmony_ci   * Get a shared pointer to the backing store of this array buffer. This
4251cb0ef41Sopenharmony_ci   * pointer coordinates the lifetime management of the internal storage
4261cb0ef41Sopenharmony_ci   * with any live ArrayBuffers on the heap, even across isolates. The embedder
4271cb0ef41Sopenharmony_ci   * should not attempt to manage lifetime of the storage through other means.
4281cb0ef41Sopenharmony_ci   */
4291cb0ef41Sopenharmony_ci  std::shared_ptr<BackingStore> GetBackingStore();
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci  /**
4321cb0ef41Sopenharmony_ci   * More efficient shortcut for GetBackingStore()->Data(). The returned pointer
4331cb0ef41Sopenharmony_ci   * is valid as long as the ArrayBuffer is alive.
4341cb0ef41Sopenharmony_ci   */
4351cb0ef41Sopenharmony_ci  void* Data() const;
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_ci  V8_INLINE static SharedArrayBuffer* Cast(Value* value) {
4381cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS
4391cb0ef41Sopenharmony_ci    CheckCast(value);
4401cb0ef41Sopenharmony_ci#endif
4411cb0ef41Sopenharmony_ci    return static_cast<SharedArrayBuffer*>(value);
4421cb0ef41Sopenharmony_ci  }
4431cb0ef41Sopenharmony_ci
4441cb0ef41Sopenharmony_ci  static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
4451cb0ef41Sopenharmony_ci
4461cb0ef41Sopenharmony_ci private:
4471cb0ef41Sopenharmony_ci  SharedArrayBuffer();
4481cb0ef41Sopenharmony_ci  static void CheckCast(Value* obj);
4491cb0ef41Sopenharmony_ci};
4501cb0ef41Sopenharmony_ci
4511cb0ef41Sopenharmony_ci}  // namespace v8
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci#endif  // INCLUDE_V8_ARRAY_BUFFER_H_
454