/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ECMASCRIPT_BUILTINS_BUILTINS_SHARED_ARRAY_H #define ECMASCRIPT_BUILTINS_BUILTINS_SHARED_ARRAY_H #include "ecmascript/base/builtins_base.h" // List of functions in Shared Array, excluding the '@@' properties. // V(name, func, length, stubIndex) // where BuiltinsSharedArray::func refers to the native implementation of SharedArray[name]. // kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available. #define BUILTIN_SHARED_ARRAY_FUNCTIONS(V) \ /* SharedArray.from ( items [ , mapfn [ , thisArg ] ] ) */ \ V("from", From, 1, INVALID) \ V("create", Create, 2, INVALID) \ /* SendableArray.isArray ( arg ) */ \ V("isArray", IsArray, 1, INVALID) \ // fixme(hzzhouzebin) Support later. // /* SharedArray.of ( ...items ) */ \ // V("of", Of, 0, INVALID) // List of functions in SharedArray.prototype, excluding the constructor and '@@' properties. // V(name, func, length, stubIndex) // where BuiltinsSharedArray::func refers to the native implementation of SharedArray.prototype[name]. #define BUILTIN_SHARED_ARRAY_PROTOTYPE_FUNCTIONS(V) \ /* SharedArray.prototype.at ( index ) */ \ V("at", At, 1, INVALID) \ /* SharedArray.prototype.concat ( ...items ) */ \ V("concat", Concat, 1, INVALID) \ /* SharedArray.prototype.entries ( ) */ \ V("entries", Entries, 0, INVALID) \ /* SharedArray.prototype.fill ( value [ , start [ , end ] ] ) */ \ V("fill", Fill, 1, INVALID) \ /* SharedArray.prototype.filter ( callbackfn [ , thisArg ] ) */ \ V("filter", Filter, 1, INVALID) \ /* SharedArray.prototype.find ( predicate [ , thisArg ] ) */ \ V("find", Find, 1, INVALID) \ /* SharedArray.prototype.findIndex ( predicate [ , thisArg ] ) */ \ V("findIndex", FindIndex, 1, INVALID) \ /* SharedArray.prototype.forEach ( callbackfn [ , thisArg ] ) */ \ V("forEach", ForEach, 1, INVALID) \ /* SharedArray.prototype.includes ( searchElement [ , fromIndex ] ) */ \ V("includes", Includes, 1, INVALID) \ /* SharedArray.prototype.indexOf ( searchElement [ , fromIndex ] ) */ \ V("indexOf", IndexOf, 1, INVALID) \ /* SharedArray.prototype.join ( separator ) */ \ V("join", Join, 1, INVALID) \ /* SharedArray.prototype.keys ( ) */ \ V("keys", Keys, 0, INVALID) \ /* SharedArray.prototype.map ( callbackfn [ , thisArg ] ) */ \ V("map", Map, 1, INVALID) \ /* SharedArray.prototype.pop ( ) */ \ V("pop", Pop, 0, INVALID) \ /* SharedArray.prototype.push ( ...items ) */ \ V("push", Push, 1, INVALID) \ /* SharedArray.prototype.reduce ( callbackfn [ , initialValue ] ) */ \ V("reduce", Reduce, 1, INVALID) \ /* SharedArray.prototype.shift ( ) */ \ V("shift", Shift, 0, INVALID) \ /* SharedArray.prototype.slice ( start, end ) */ \ V("slice", Slice, 2, INVALID) \ /* SharedArray.prototype.sort ( comparefn ) */ \ V("sort", Sort, 1, INVALID) \ /* SharedArray.prototype.toString ( ) */ \ V("toString", ToString, 0, INVALID) \ /* SharedArray.prototype.values ( ) */ \ /* SharedArray.prototype.unshift ( ...items ) */ \ V("unshift", Unshift, 1, INVALID) \ V("values", Values, 0, INVALID) \ V("shrinkTo", ShrinkTo, 0, INVALID) \ V("extendTo", ExtendTo, 0, INVALID) \ /* SharedArray.prototype.splice ( start, deleteCount, ...items ) */ \ V("splice", Splice, 2, INVALID) \ /* SharedArray.prototype.every ( callbackfn [ , thisArg ] ) */ \ V("every", Every, 1, INVALID) \ /* SharedArray.prototype.some ( callbackfn [ , thisArg ] ) */ \ V("some", Some, 1, INVALID) \ /* SendableArray.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) */ \ V("lastIndexOf", LastIndexOf, 1, INVALID) // fixme(hzzhouzebin) Support later. // /* SharedArray.prototype.with ( index, value ) */ \ // V("with", With, 2, INVALID) \ // /* SharedArray.prototype.reduceRight ( callbackfn [ , initialValue ] ) */ \ // V("reduceRight", ReduceRight, 1, INVALID) \ // /* SharedArray.prototype.reverse ( ) */ \ // V("reverse", Reverse, 0, INVALID) \ // /* SharedArray.prototype.copyWithin ( target, start [ , end ] ) */ \ // V("copyWithin", CopyWithin, 2, INVALID) \ // /* SharedArray.prototype.findLast ( predicate [ , thisArg ] ) */ \ // V("findLast", FindLast, 1, INVALID) \ // /* SharedArray.prototype.findLastIndex ( predicate [ , thisArg ] ) */ \ // V("findLastIndex", FindLastIndex, 1, INVALID) \ // /* SharedArray.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) */ \ // V("toLocaleString", ToLocaleString, 0, INVALID) \ // /* SharedArray.prototype.toReversed ( ) */ \ // V("toReversed", ToReversed, 0, INVALID) \ // /* SharedArray.prototype.toSorted ( comparefn ) */ \ // V("toSorted", ToSorted, 1, INVALID) \ // /* SharedArray.prototype.toSpliced ( start, skipCount, ...items ) */ \ // V("toSpliced", ToSpliced, 2, INVALID) namespace panda::ecmascript::builtins { class BuiltinsSharedArray : public base::BuiltinsBase { public: static JSTaggedValue ArrayConstructor(EcmaRuntimeCallInfo *argv); static JSTaggedValue From(EcmaRuntimeCallInfo *argv); static JSTaggedValue Create(EcmaRuntimeCallInfo *argv); static JSTaggedValue IsArray(EcmaRuntimeCallInfo *argv); static JSTaggedValue Species(EcmaRuntimeCallInfo *argv); // prototype static JSTaggedValue Concat(EcmaRuntimeCallInfo *argv); static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv); static JSTaggedValue Fill(EcmaRuntimeCallInfo *argv); static JSTaggedValue Filter(EcmaRuntimeCallInfo *argv); static JSTaggedValue Find(EcmaRuntimeCallInfo *argv); static JSTaggedValue FindIndex(EcmaRuntimeCallInfo *argv); static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv); static JSTaggedValue IndexOf(EcmaRuntimeCallInfo *argv); static JSTaggedValue Join(EcmaRuntimeCallInfo *argv); static JSTaggedValue Keys(EcmaRuntimeCallInfo *argv); static JSTaggedValue Map(EcmaRuntimeCallInfo *argv); static JSTaggedValue Pop(EcmaRuntimeCallInfo *argv); static JSTaggedValue Push(EcmaRuntimeCallInfo *argv); static JSTaggedValue Reduce(EcmaRuntimeCallInfo *argv); static JSTaggedValue Shift(EcmaRuntimeCallInfo *argv); static JSTaggedValue Slice(EcmaRuntimeCallInfo *argv); static JSTaggedValue Splice(EcmaRuntimeCallInfo *argv); static JSTaggedValue Sort(EcmaRuntimeCallInfo *argv); static JSTaggedValue ToString(EcmaRuntimeCallInfo *argv); static JSTaggedValue Unshift(EcmaRuntimeCallInfo *argv); static JSTaggedValue Values(EcmaRuntimeCallInfo *argv); static JSTaggedValue Unscopables(EcmaRuntimeCallInfo *argv); static JSTaggedValue Includes(EcmaRuntimeCallInfo *argv); static JSTaggedValue At(EcmaRuntimeCallInfo *argv); static JSTaggedValue ShrinkTo(EcmaRuntimeCallInfo *argv); static JSTaggedValue ExtendTo(EcmaRuntimeCallInfo *argv); static JSTaggedValue Every(EcmaRuntimeCallInfo *argv); static JSTaggedValue Some(EcmaRuntimeCallInfo *argv); static JSTaggedValue LastIndexOf(EcmaRuntimeCallInfo *argv); // Excluding the '@@' internal properties static Span GetSharedArrayFunctions() { return Span(SENDABLE_ARRAY_FUNCTIONS); } // Excluding the constructor and '@@' internal properties. static Span GetSharedArrayPrototypeFunctions() { return Span(SENDABLE_ARRAY_PROTOTYPE_FUNCTIONS); } static size_t GetNumPrototypeInlinedProperties() { // 4 : 4 More inlined entries in SharedArray.prototype for the following functions/accessors: // (1) 'length' accessor // (2) SharedArray.prototype.constructor, i.e. Array() // (3) SharedArray.prototype[@@iterator]() // (4) SharedArray.prototype[@@unscopables]() return GetSharedArrayPrototypeFunctions().Size() + 4; } static JSTaggedValue ReduceUnStableJSArray(JSThread *thread, JSHandle &thisHandle, JSHandle &thisObjVal, int64_t k, int64_t len, JSMutableHandle &accumulator, JSHandle &callbackFnHandle); static JSTaggedValue FilterUnStableJSArray(JSThread *thread, JSHandle &thisArgHandle, JSHandle &thisObjVal, int64_t k, int64_t len, uint32_t toIndex, JSHandle newArrayHandle, JSHandle &callbackFnHandle); static Span> GetPrototypeProperties() { return Span>(ARRAY_PROTOTYPE_PROPERTIES); } static Span> GetFunctionProperties() { return Span>(ARRAY_FUNCTION_PROPERTIES); } private: static JSTaggedValue PopInner(EcmaRuntimeCallInfo *argv, JSHandle &thisHandle, JSHandle &thisObjHandle); #define BUILTIN_SENDABLE_ARRAY_FUNCTION_ENTRY(name, method, length, id) \ base::BuiltinFunctionEntry::Create(name, BuiltinsSharedArray::method, length, kungfu::BuiltinsStubCSigns::id), static constexpr std::array SENDABLE_ARRAY_FUNCTIONS = { BUILTIN_SHARED_ARRAY_FUNCTIONS(BUILTIN_SENDABLE_ARRAY_FUNCTION_ENTRY) }; static constexpr std::array SENDABLE_ARRAY_PROTOTYPE_FUNCTIONS = { BUILTIN_SHARED_ARRAY_PROTOTYPE_FUNCTIONS(BUILTIN_SENDABLE_ARRAY_FUNCTION_ENTRY) }; #undef BUILTIN_SENDABLE_ARRAY_FUNCTION_ENTRY static JSTaggedValue IndexOfSlowPath( EcmaRuntimeCallInfo *argv, JSThread *thread, const JSHandle &thisHandle); static JSTaggedValue IndexOfSlowPath( EcmaRuntimeCallInfo *argv, JSThread *thread, const JSHandle &thisObjVal, int64_t length, int64_t fromIndex); static JSTaggedValue LastIndexOfSlowPath(EcmaRuntimeCallInfo *argv, JSThread *thread, const JSHandle &thisHandle); static JSTaggedValue LastIndexOfSlowPath(EcmaRuntimeCallInfo *argv, JSThread *thread, const JSHandle &thisObjVal, int64_t fromIndex); #define ARRAY_PROPERTIES_PAIR(name, func, length, id) \ std::pair(name, false), static constexpr std::array ARRAY_PROTOTYPE_PROPERTIES = { std::pair("length", false), std::pair("constructor", false), BUILTIN_SHARED_ARRAY_PROTOTYPE_FUNCTIONS(ARRAY_PROPERTIES_PAIR) std::pair("[Symbol.iterator]", false), std::pair("[Symbol.unscopables]", false) }; static constexpr std::array ARRAY_FUNCTION_PROPERTIES = { std::pair("length", false), std::pair("name", false), std::pair("prototype", false), BUILTIN_SHARED_ARRAY_FUNCTIONS(ARRAY_PROPERTIES_PAIR) std::pair("[Symbol.species]", true), }; static JSTaggedValue CheckElementForEvery(JSThread *thread, JSHandle &thisObjVal, JSHandle &callbackFnHandle, JSHandle &thisArgHandle, uint32_t &k); #undef ARRAY_PROPERTIES_PAIR }; } // namespace panda::ecmascript::builtins #endif // ECMASCRIPT_BUILTINS_BUILTINS_SHARED_ARRAY_H