14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2024 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_SHARED_ARRAY_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_SHARED_ARRAY_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ci// List of functions in Shared Array, excluding the '@@' properties. 224514f5e3Sopenharmony_ci// V(name, func, length, stubIndex) 234514f5e3Sopenharmony_ci// where BuiltinsSharedArray::func refers to the native implementation of SharedArray[name]. 244514f5e3Sopenharmony_ci// kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available. 254514f5e3Sopenharmony_ci#define BUILTIN_SHARED_ARRAY_FUNCTIONS(V) \ 264514f5e3Sopenharmony_ci /* SharedArray.from ( items [ , mapfn [ , thisArg ] ] ) */ \ 274514f5e3Sopenharmony_ci V("from", From, 1, INVALID) \ 284514f5e3Sopenharmony_ci V("create", Create, 2, INVALID) \ 294514f5e3Sopenharmony_ci /* SendableArray.isArray ( arg ) */ \ 304514f5e3Sopenharmony_ci V("isArray", IsArray, 1, INVALID) \ 314514f5e3Sopenharmony_ci // fixme(hzzhouzebin) Support later. 324514f5e3Sopenharmony_ci // /* SharedArray.of ( ...items ) */ \ 334514f5e3Sopenharmony_ci // V("of", Of, 0, INVALID) 344514f5e3Sopenharmony_ci 354514f5e3Sopenharmony_ci// List of functions in SharedArray.prototype, excluding the constructor and '@@' properties. 364514f5e3Sopenharmony_ci// V(name, func, length, stubIndex) 374514f5e3Sopenharmony_ci// where BuiltinsSharedArray::func refers to the native implementation of SharedArray.prototype[name]. 384514f5e3Sopenharmony_ci#define BUILTIN_SHARED_ARRAY_PROTOTYPE_FUNCTIONS(V) \ 394514f5e3Sopenharmony_ci /* SharedArray.prototype.at ( index ) */ \ 404514f5e3Sopenharmony_ci V("at", At, 1, INVALID) \ 414514f5e3Sopenharmony_ci /* SharedArray.prototype.concat ( ...items ) */ \ 424514f5e3Sopenharmony_ci V("concat", Concat, 1, INVALID) \ 434514f5e3Sopenharmony_ci /* SharedArray.prototype.entries ( ) */ \ 444514f5e3Sopenharmony_ci V("entries", Entries, 0, INVALID) \ 454514f5e3Sopenharmony_ci /* SharedArray.prototype.fill ( value [ , start [ , end ] ] ) */ \ 464514f5e3Sopenharmony_ci V("fill", Fill, 1, INVALID) \ 474514f5e3Sopenharmony_ci /* SharedArray.prototype.filter ( callbackfn [ , thisArg ] ) */ \ 484514f5e3Sopenharmony_ci V("filter", Filter, 1, INVALID) \ 494514f5e3Sopenharmony_ci /* SharedArray.prototype.find ( predicate [ , thisArg ] ) */ \ 504514f5e3Sopenharmony_ci V("find", Find, 1, INVALID) \ 514514f5e3Sopenharmony_ci /* SharedArray.prototype.findIndex ( predicate [ , thisArg ] ) */ \ 524514f5e3Sopenharmony_ci V("findIndex", FindIndex, 1, INVALID) \ 534514f5e3Sopenharmony_ci /* SharedArray.prototype.forEach ( callbackfn [ , thisArg ] ) */ \ 544514f5e3Sopenharmony_ci V("forEach", ForEach, 1, INVALID) \ 554514f5e3Sopenharmony_ci /* SharedArray.prototype.includes ( searchElement [ , fromIndex ] ) */ \ 564514f5e3Sopenharmony_ci V("includes", Includes, 1, INVALID) \ 574514f5e3Sopenharmony_ci /* SharedArray.prototype.indexOf ( searchElement [ , fromIndex ] ) */ \ 584514f5e3Sopenharmony_ci V("indexOf", IndexOf, 1, INVALID) \ 594514f5e3Sopenharmony_ci /* SharedArray.prototype.join ( separator ) */ \ 604514f5e3Sopenharmony_ci V("join", Join, 1, INVALID) \ 614514f5e3Sopenharmony_ci /* SharedArray.prototype.keys ( ) */ \ 624514f5e3Sopenharmony_ci V("keys", Keys, 0, INVALID) \ 634514f5e3Sopenharmony_ci /* SharedArray.prototype.map ( callbackfn [ , thisArg ] ) */ \ 644514f5e3Sopenharmony_ci V("map", Map, 1, INVALID) \ 654514f5e3Sopenharmony_ci /* SharedArray.prototype.pop ( ) */ \ 664514f5e3Sopenharmony_ci V("pop", Pop, 0, INVALID) \ 674514f5e3Sopenharmony_ci /* SharedArray.prototype.push ( ...items ) */ \ 684514f5e3Sopenharmony_ci V("push", Push, 1, INVALID) \ 694514f5e3Sopenharmony_ci /* SharedArray.prototype.reduce ( callbackfn [ , initialValue ] ) */ \ 704514f5e3Sopenharmony_ci V("reduce", Reduce, 1, INVALID) \ 714514f5e3Sopenharmony_ci /* SharedArray.prototype.shift ( ) */ \ 724514f5e3Sopenharmony_ci V("shift", Shift, 0, INVALID) \ 734514f5e3Sopenharmony_ci /* SharedArray.prototype.slice ( start, end ) */ \ 744514f5e3Sopenharmony_ci V("slice", Slice, 2, INVALID) \ 754514f5e3Sopenharmony_ci /* SharedArray.prototype.sort ( comparefn ) */ \ 764514f5e3Sopenharmony_ci V("sort", Sort, 1, INVALID) \ 774514f5e3Sopenharmony_ci /* SharedArray.prototype.toString ( ) */ \ 784514f5e3Sopenharmony_ci V("toString", ToString, 0, INVALID) \ 794514f5e3Sopenharmony_ci /* SharedArray.prototype.values ( ) */ \ 804514f5e3Sopenharmony_ci /* SharedArray.prototype.unshift ( ...items ) */ \ 814514f5e3Sopenharmony_ci V("unshift", Unshift, 1, INVALID) \ 824514f5e3Sopenharmony_ci V("values", Values, 0, INVALID) \ 834514f5e3Sopenharmony_ci V("shrinkTo", ShrinkTo, 0, INVALID) \ 844514f5e3Sopenharmony_ci V("extendTo", ExtendTo, 0, INVALID) \ 854514f5e3Sopenharmony_ci /* SharedArray.prototype.splice ( start, deleteCount, ...items ) */ \ 864514f5e3Sopenharmony_ci V("splice", Splice, 2, INVALID) \ 874514f5e3Sopenharmony_ci /* SharedArray.prototype.every ( callbackfn [ , thisArg ] ) */ \ 884514f5e3Sopenharmony_ci V("every", Every, 1, INVALID) \ 894514f5e3Sopenharmony_ci /* SharedArray.prototype.some ( callbackfn [ , thisArg ] ) */ \ 904514f5e3Sopenharmony_ci V("some", Some, 1, INVALID) \ 914514f5e3Sopenharmony_ci /* SendableArray.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) */ \ 924514f5e3Sopenharmony_ci V("lastIndexOf", LastIndexOf, 1, INVALID) 934514f5e3Sopenharmony_ci // fixme(hzzhouzebin) Support later. 944514f5e3Sopenharmony_ci // /* SharedArray.prototype.with ( index, value ) */ \ 954514f5e3Sopenharmony_ci // V("with", With, 2, INVALID) \ 964514f5e3Sopenharmony_ci // /* SharedArray.prototype.reduceRight ( callbackfn [ , initialValue ] ) */ \ 974514f5e3Sopenharmony_ci // V("reduceRight", ReduceRight, 1, INVALID) \ 984514f5e3Sopenharmony_ci // /* SharedArray.prototype.reverse ( ) */ \ 994514f5e3Sopenharmony_ci // V("reverse", Reverse, 0, INVALID) \ 1004514f5e3Sopenharmony_ci // /* SharedArray.prototype.copyWithin ( target, start [ , end ] ) */ \ 1014514f5e3Sopenharmony_ci // V("copyWithin", CopyWithin, 2, INVALID) \ 1024514f5e3Sopenharmony_ci // /* SharedArray.prototype.findLast ( predicate [ , thisArg ] ) */ \ 1034514f5e3Sopenharmony_ci // V("findLast", FindLast, 1, INVALID) \ 1044514f5e3Sopenharmony_ci // /* SharedArray.prototype.findLastIndex ( predicate [ , thisArg ] ) */ \ 1054514f5e3Sopenharmony_ci // V("findLastIndex", FindLastIndex, 1, INVALID) \ 1064514f5e3Sopenharmony_ci // /* SharedArray.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) */ \ 1074514f5e3Sopenharmony_ci // V("toLocaleString", ToLocaleString, 0, INVALID) \ 1084514f5e3Sopenharmony_ci // /* SharedArray.prototype.toReversed ( ) */ \ 1094514f5e3Sopenharmony_ci // V("toReversed", ToReversed, 0, INVALID) \ 1104514f5e3Sopenharmony_ci // /* SharedArray.prototype.toSorted ( comparefn ) */ \ 1114514f5e3Sopenharmony_ci // V("toSorted", ToSorted, 1, INVALID) \ 1124514f5e3Sopenharmony_ci // /* SharedArray.prototype.toSpliced ( start, skipCount, ...items ) */ \ 1134514f5e3Sopenharmony_ci // V("toSpliced", ToSpliced, 2, INVALID) 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 1164514f5e3Sopenharmony_ciclass BuiltinsSharedArray : public base::BuiltinsBase { 1174514f5e3Sopenharmony_cipublic: 1184514f5e3Sopenharmony_ci static JSTaggedValue ArrayConstructor(EcmaRuntimeCallInfo *argv); 1194514f5e3Sopenharmony_ci static JSTaggedValue From(EcmaRuntimeCallInfo *argv); 1204514f5e3Sopenharmony_ci static JSTaggedValue Create(EcmaRuntimeCallInfo *argv); 1214514f5e3Sopenharmony_ci static JSTaggedValue IsArray(EcmaRuntimeCallInfo *argv); 1224514f5e3Sopenharmony_ci static JSTaggedValue Species(EcmaRuntimeCallInfo *argv); 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci // prototype 1254514f5e3Sopenharmony_ci static JSTaggedValue Concat(EcmaRuntimeCallInfo *argv); 1264514f5e3Sopenharmony_ci static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv); 1274514f5e3Sopenharmony_ci static JSTaggedValue Fill(EcmaRuntimeCallInfo *argv); 1284514f5e3Sopenharmony_ci static JSTaggedValue Filter(EcmaRuntimeCallInfo *argv); 1294514f5e3Sopenharmony_ci static JSTaggedValue Find(EcmaRuntimeCallInfo *argv); 1304514f5e3Sopenharmony_ci static JSTaggedValue FindIndex(EcmaRuntimeCallInfo *argv); 1314514f5e3Sopenharmony_ci static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv); 1324514f5e3Sopenharmony_ci static JSTaggedValue IndexOf(EcmaRuntimeCallInfo *argv); 1334514f5e3Sopenharmony_ci static JSTaggedValue Join(EcmaRuntimeCallInfo *argv); 1344514f5e3Sopenharmony_ci static JSTaggedValue Keys(EcmaRuntimeCallInfo *argv); 1354514f5e3Sopenharmony_ci static JSTaggedValue Map(EcmaRuntimeCallInfo *argv); 1364514f5e3Sopenharmony_ci static JSTaggedValue Pop(EcmaRuntimeCallInfo *argv); 1374514f5e3Sopenharmony_ci static JSTaggedValue Push(EcmaRuntimeCallInfo *argv); 1384514f5e3Sopenharmony_ci static JSTaggedValue Reduce(EcmaRuntimeCallInfo *argv); 1394514f5e3Sopenharmony_ci static JSTaggedValue Shift(EcmaRuntimeCallInfo *argv); 1404514f5e3Sopenharmony_ci static JSTaggedValue Slice(EcmaRuntimeCallInfo *argv); 1414514f5e3Sopenharmony_ci static JSTaggedValue Splice(EcmaRuntimeCallInfo *argv); 1424514f5e3Sopenharmony_ci static JSTaggedValue Sort(EcmaRuntimeCallInfo *argv); 1434514f5e3Sopenharmony_ci static JSTaggedValue ToString(EcmaRuntimeCallInfo *argv); 1444514f5e3Sopenharmony_ci static JSTaggedValue Unshift(EcmaRuntimeCallInfo *argv); 1454514f5e3Sopenharmony_ci static JSTaggedValue Values(EcmaRuntimeCallInfo *argv); 1464514f5e3Sopenharmony_ci static JSTaggedValue Unscopables(EcmaRuntimeCallInfo *argv); 1474514f5e3Sopenharmony_ci static JSTaggedValue Includes(EcmaRuntimeCallInfo *argv); 1484514f5e3Sopenharmony_ci static JSTaggedValue At(EcmaRuntimeCallInfo *argv); 1494514f5e3Sopenharmony_ci static JSTaggedValue ShrinkTo(EcmaRuntimeCallInfo *argv); 1504514f5e3Sopenharmony_ci static JSTaggedValue ExtendTo(EcmaRuntimeCallInfo *argv); 1514514f5e3Sopenharmony_ci static JSTaggedValue Every(EcmaRuntimeCallInfo *argv); 1524514f5e3Sopenharmony_ci static JSTaggedValue Some(EcmaRuntimeCallInfo *argv); 1534514f5e3Sopenharmony_ci static JSTaggedValue LastIndexOf(EcmaRuntimeCallInfo *argv); 1544514f5e3Sopenharmony_ci 1554514f5e3Sopenharmony_ci // Excluding the '@@' internal properties 1564514f5e3Sopenharmony_ci static Span<const base::BuiltinFunctionEntry> GetSharedArrayFunctions() 1574514f5e3Sopenharmony_ci { 1584514f5e3Sopenharmony_ci return Span<const base::BuiltinFunctionEntry>(SENDABLE_ARRAY_FUNCTIONS); 1594514f5e3Sopenharmony_ci } 1604514f5e3Sopenharmony_ci 1614514f5e3Sopenharmony_ci // Excluding the constructor and '@@' internal properties. 1624514f5e3Sopenharmony_ci static Span<const base::BuiltinFunctionEntry> GetSharedArrayPrototypeFunctions() 1634514f5e3Sopenharmony_ci { 1644514f5e3Sopenharmony_ci return Span<const base::BuiltinFunctionEntry>(SENDABLE_ARRAY_PROTOTYPE_FUNCTIONS); 1654514f5e3Sopenharmony_ci } 1664514f5e3Sopenharmony_ci 1674514f5e3Sopenharmony_ci static size_t GetNumPrototypeInlinedProperties() 1684514f5e3Sopenharmony_ci { 1694514f5e3Sopenharmony_ci // 4 : 4 More inlined entries in SharedArray.prototype for the following functions/accessors: 1704514f5e3Sopenharmony_ci // (1) 'length' accessor 1714514f5e3Sopenharmony_ci // (2) SharedArray.prototype.constructor, i.e. Array() 1724514f5e3Sopenharmony_ci // (3) SharedArray.prototype[@@iterator]() 1734514f5e3Sopenharmony_ci // (4) SharedArray.prototype[@@unscopables]() 1744514f5e3Sopenharmony_ci return GetSharedArrayPrototypeFunctions().Size() + 4; 1754514f5e3Sopenharmony_ci } 1764514f5e3Sopenharmony_ci static JSTaggedValue ReduceUnStableJSArray(JSThread *thread, JSHandle<JSTaggedValue> &thisHandle, 1774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> &thisObjVal, int64_t k, int64_t len, JSMutableHandle<JSTaggedValue> &accumulator, 1784514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> &callbackFnHandle); 1794514f5e3Sopenharmony_ci 1804514f5e3Sopenharmony_ci static JSTaggedValue FilterUnStableJSArray(JSThread *thread, JSHandle<JSTaggedValue> &thisArgHandle, 1814514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> &thisObjVal, int64_t k, int64_t len, uint32_t toIndex, 1824514f5e3Sopenharmony_ci JSHandle<JSObject> newArrayHandle, JSHandle<JSTaggedValue> &callbackFnHandle); 1834514f5e3Sopenharmony_ci 1844514f5e3Sopenharmony_ci static Span<const std::pair<std::string_view, bool>> GetPrototypeProperties() 1854514f5e3Sopenharmony_ci { 1864514f5e3Sopenharmony_ci return Span<const std::pair<std::string_view, bool>>(ARRAY_PROTOTYPE_PROPERTIES); 1874514f5e3Sopenharmony_ci } 1884514f5e3Sopenharmony_ci static Span<const std::pair<std::string_view, bool>> GetFunctionProperties() 1894514f5e3Sopenharmony_ci { 1904514f5e3Sopenharmony_ci return Span<const std::pair<std::string_view, bool>>(ARRAY_FUNCTION_PROPERTIES); 1914514f5e3Sopenharmony_ci } 1924514f5e3Sopenharmony_ciprivate: 1934514f5e3Sopenharmony_ci static JSTaggedValue PopInner(EcmaRuntimeCallInfo *argv, JSHandle<JSTaggedValue> &thisHandle, 1944514f5e3Sopenharmony_ci JSHandle<JSObject> &thisObjHandle); 1954514f5e3Sopenharmony_ci#define BUILTIN_SENDABLE_ARRAY_FUNCTION_ENTRY(name, method, length, id) \ 1964514f5e3Sopenharmony_ci base::BuiltinFunctionEntry::Create(name, BuiltinsSharedArray::method, length, kungfu::BuiltinsStubCSigns::id), 1974514f5e3Sopenharmony_ci 1984514f5e3Sopenharmony_ci static constexpr std::array SENDABLE_ARRAY_FUNCTIONS = { 1994514f5e3Sopenharmony_ci BUILTIN_SHARED_ARRAY_FUNCTIONS(BUILTIN_SENDABLE_ARRAY_FUNCTION_ENTRY) 2004514f5e3Sopenharmony_ci }; 2014514f5e3Sopenharmony_ci static constexpr std::array SENDABLE_ARRAY_PROTOTYPE_FUNCTIONS = { 2024514f5e3Sopenharmony_ci BUILTIN_SHARED_ARRAY_PROTOTYPE_FUNCTIONS(BUILTIN_SENDABLE_ARRAY_FUNCTION_ENTRY) 2034514f5e3Sopenharmony_ci }; 2044514f5e3Sopenharmony_ci#undef BUILTIN_SENDABLE_ARRAY_FUNCTION_ENTRY 2054514f5e3Sopenharmony_ci 2064514f5e3Sopenharmony_ci static JSTaggedValue IndexOfSlowPath( 2074514f5e3Sopenharmony_ci EcmaRuntimeCallInfo *argv, JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle); 2084514f5e3Sopenharmony_ci 2094514f5e3Sopenharmony_ci static JSTaggedValue IndexOfSlowPath( 2104514f5e3Sopenharmony_ci EcmaRuntimeCallInfo *argv, JSThread *thread, const JSHandle<JSTaggedValue> &thisObjVal, 2114514f5e3Sopenharmony_ci int64_t length, int64_t fromIndex); 2124514f5e3Sopenharmony_ci 2134514f5e3Sopenharmony_ci static JSTaggedValue LastIndexOfSlowPath(EcmaRuntimeCallInfo *argv, JSThread *thread, 2144514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &thisHandle); 2154514f5e3Sopenharmony_ci static JSTaggedValue LastIndexOfSlowPath(EcmaRuntimeCallInfo *argv, JSThread *thread, 2164514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &thisObjVal, int64_t fromIndex); 2174514f5e3Sopenharmony_ci 2184514f5e3Sopenharmony_ci#define ARRAY_PROPERTIES_PAIR(name, func, length, id) \ 2194514f5e3Sopenharmony_ci std::pair<std::string_view, bool>(name, false), 2204514f5e3Sopenharmony_ci 2214514f5e3Sopenharmony_ci static constexpr std::array ARRAY_PROTOTYPE_PROPERTIES = { 2224514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("length", false), 2234514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("constructor", false), 2244514f5e3Sopenharmony_ci BUILTIN_SHARED_ARRAY_PROTOTYPE_FUNCTIONS(ARRAY_PROPERTIES_PAIR) 2254514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("[Symbol.iterator]", false), 2264514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("[Symbol.unscopables]", false) 2274514f5e3Sopenharmony_ci }; 2284514f5e3Sopenharmony_ci static constexpr std::array ARRAY_FUNCTION_PROPERTIES = { 2294514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("length", false), 2304514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("name", false), 2314514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("prototype", false), 2324514f5e3Sopenharmony_ci BUILTIN_SHARED_ARRAY_FUNCTIONS(ARRAY_PROPERTIES_PAIR) 2334514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("[Symbol.species]", true), 2344514f5e3Sopenharmony_ci }; 2354514f5e3Sopenharmony_ci static JSTaggedValue CheckElementForEvery(JSThread *thread, 2364514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> &thisObjVal, 2374514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> &callbackFnHandle, 2384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> &thisArgHandle, 2394514f5e3Sopenharmony_ci uint32_t &k); 2404514f5e3Sopenharmony_ci#undef ARRAY_PROPERTIES_PAIR 2414514f5e3Sopenharmony_ci}; 2424514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins 2434514f5e3Sopenharmony_ci 2444514f5e3Sopenharmony_ci#endif // ECMASCRIPT_BUILTINS_BUILTINS_SHARED_ARRAY_H 245