14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_BUILTINS_BUILTINS_SENDABLE_ARRAYBUFFER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_SENDABLE_ARRAYBUFFER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h"
204514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_arraybuffer.h"
214514f5e3Sopenharmony_ci#include "ecmascript/base/number_helper.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_dataview.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_typed_array.h"
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_ci// List of functions in ArrayBuffer, excluding the '@@' properties.
264514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
274514f5e3Sopenharmony_ci// where BuiltinsArrayBuffer::func refers to the native implementation of ArrayBuffer[name].
284514f5e3Sopenharmony_ci//       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
294514f5e3Sopenharmony_ci#define BUILTIN_ARRAY_BUFFER_FUNCTIONS(V)                                           \
304514f5e3Sopenharmony_ci    /* ArrayBuffer.isView ( arg ) */                                                \
314514f5e3Sopenharmony_ci    V("isView", IsView, 1, ArrayBufferIsView)
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
344514f5e3Sopenharmony_ciusing DataViewType = ecmascript::DataViewType;
354514f5e3Sopenharmony_ciusing BuiltinFunctionEntry = base::BuiltinFunctionEntry;
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ciclass BuiltinsSendableArrayBuffer : public BuiltinsArrayBuffer {
384514f5e3Sopenharmony_cipublic:
394514f5e3Sopenharmony_ci    // 24.1.2.1 ArrayBuffer(length)
404514f5e3Sopenharmony_ci    static JSTaggedValue ArrayBufferConstructor(EcmaRuntimeCallInfo *argv);
414514f5e3Sopenharmony_ci    // 24.1.3.1 ArrayBuffer.isView(arg)
424514f5e3Sopenharmony_ci    static JSTaggedValue IsView(EcmaRuntimeCallInfo *argv);
434514f5e3Sopenharmony_ci    // 24.1.3.3 get ArrayBuffer[@@species]
444514f5e3Sopenharmony_ci    static JSTaggedValue Species(EcmaRuntimeCallInfo *argv);
454514f5e3Sopenharmony_ci    // 24.1.4.1 get ArrayBuffer.prototype.byteLength
464514f5e3Sopenharmony_ci    static JSTaggedValue GetByteLength(EcmaRuntimeCallInfo *argv);
474514f5e3Sopenharmony_ci    // 24.1.4.3 ArrayBuffer.prototype.slice()
484514f5e3Sopenharmony_ci    static JSTaggedValue Slice(EcmaRuntimeCallInfo *argv);
494514f5e3Sopenharmony_ci    // 24.1.1.2 IsDetachedBuffer(arrayBuffer)
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    static bool IsDetachedBuffer(JSTaggedValue arrayBuffer);
524514f5e3Sopenharmony_ci    // 24.1.1.4 CloneArrayBuffer( srcBuffer, srcByteOffset [, cloneConstructor] )
534514f5e3Sopenharmony_ci    static JSTaggedValue CloneArrayBuffer(JSThread *thread, const JSHandle<JSTaggedValue> &srcBuffer,
544514f5e3Sopenharmony_ci                                          uint32_t srcByteOffset, JSHandle<JSTaggedValue> constructor);
554514f5e3Sopenharmony_ci    // 24.1.1.1 AllocateArrayBuffer(constructor, byteLength)
564514f5e3Sopenharmony_ci    static JSTaggedValue AllocateSendableArrayBuffer(
574514f5e3Sopenharmony_ci        JSThread *thread, const JSHandle<JSTaggedValue> &newTarget, uint64_t byteLength);
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    // Excluding the '@@' internal properties
604514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetArrayBufferFunctions()
614514f5e3Sopenharmony_ci    {
624514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(ARRAY_BUFFER_FUNCTIONS);
634514f5e3Sopenharmony_ci    }
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    static size_t GetNumPrototypeInlinedProperties()
664514f5e3Sopenharmony_ci    {
674514f5e3Sopenharmony_ci        // 3 : 3 more inline properties in Set.prototype
684514f5e3Sopenharmony_ci        //  (1) ArrayBuffer.prototype.slice
694514f5e3Sopenharmony_ci        //  (2) get ArrayBuffer.prototype.byteLength
704514f5e3Sopenharmony_ci        //  (3) ArrayBuffer.prototype [ @@toStringTag ]
714514f5e3Sopenharmony_ci        return 3;
724514f5e3Sopenharmony_ci    }
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    static Span<const std::pair<std::string_view, bool>> GetPrototypeProperties()
754514f5e3Sopenharmony_ci    {
764514f5e3Sopenharmony_ci        return Span<const std::pair<std::string_view, bool>>(ARRAYBUFFER_PROTOTYPE_PROPERTIES);
774514f5e3Sopenharmony_ci    }
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci    static Span<const std::pair<std::string_view, bool>> GetFunctionProperties()
804514f5e3Sopenharmony_ci    {
814514f5e3Sopenharmony_ci        return Span<const std::pair<std::string_view, bool>>(ARRAYBUFFER_FUNCTION_PROPERTIES);
824514f5e3Sopenharmony_ci    }
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    static void *GetDataPointFromBuffer(JSTaggedValue arrBuf, uint32_t byteOffset = 0);
854514f5e3Sopenharmony_ciprivate:
864514f5e3Sopenharmony_ci#define BUILTIN_ARRAY_BUFFER_ENTRY(name, func, length, id) \
874514f5e3Sopenharmony_ci    BuiltinFunctionEntry::Create(name, BuiltinsSendableArrayBuffer::func, length, kungfu::BuiltinsStubCSigns::id),
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci    static constexpr std::array ARRAY_BUFFER_FUNCTIONS = {
904514f5e3Sopenharmony_ci        BUILTIN_ARRAY_BUFFER_FUNCTIONS(BUILTIN_ARRAY_BUFFER_ENTRY)
914514f5e3Sopenharmony_ci    };
924514f5e3Sopenharmony_ci#undef BUILTIN_ARRAY_BUFFER_ENTRY
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci#define ARRAYBUFFER_PROPERTIES_PAIR(name, func, length, id) \
954514f5e3Sopenharmony_ci    std::pair<std::string_view, bool>(name, false),
964514f5e3Sopenharmony_ci
974514f5e3Sopenharmony_ci    static constexpr std::array ARRAYBUFFER_PROTOTYPE_PROPERTIES = {
984514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("slice", false),
994514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("byteLength", true),
1004514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("[Symbol.toStringTag]", false),
1014514f5e3Sopenharmony_ci    };
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ci    static constexpr std::array ARRAYBUFFER_FUNCTION_PROPERTIES = {
1044514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("length", false),
1054514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("name", false),
1064514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("prototype", false),
1074514f5e3Sopenharmony_ci        std::pair<std::string_view, bool>("[Symbol.species]", true),
1084514f5e3Sopenharmony_ci        BUILTIN_ARRAY_BUFFER_FUNCTIONS(ARRAYBUFFER_PROPERTIES_PAIR)
1094514f5e3Sopenharmony_ci    };
1104514f5e3Sopenharmony_ci#undef ARRAYBUFFER_PROPERTIES_PAIR
1114514f5e3Sopenharmony_ci};
1124514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
1134514f5e3Sopenharmony_ci
1144514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_BUILTINS_BUILTINS_SENDABLE_ARRAYBUFFER_H
115