14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 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_OBJECT_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_OBJECT_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h" 204514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h" 224514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_ci// List of functions in Object, excluding the '@@' properties. 254514f5e3Sopenharmony_ci// V(name, func, length, stubIndex) 264514f5e3Sopenharmony_ci// where BuiltinsObject::func refers to the native implementation of Object[name]. 274514f5e3Sopenharmony_ci// kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available. 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ci#define BUILTIN_OBJECT_FUNCTIONS(V) \ 304514f5e3Sopenharmony_ci /* Object.assign ( target, ...sources ) */ \ 314514f5e3Sopenharmony_ci V("assign", Assign, 2, ObjectAssign) \ 324514f5e3Sopenharmony_ci /* Object.create ( O, Properties ) */ \ 334514f5e3Sopenharmony_ci V("create", Create, 2, ObjectCreate) \ 344514f5e3Sopenharmony_ci /* Object.defineProperties ( O, Properties ) */ \ 354514f5e3Sopenharmony_ci V("defineProperties", DefineProperties, 2, INVALID) \ 364514f5e3Sopenharmony_ci /* Object.defineProperty ( O, P, Attributes ) */ \ 374514f5e3Sopenharmony_ci V("defineProperty", DefineProperty, 3, INVALID) \ 384514f5e3Sopenharmony_ci /* Object.entries ( O ) */ \ 394514f5e3Sopenharmony_ci V("entries", Entries, 1, ObjectEntries) \ 404514f5e3Sopenharmony_ci /* Object.freeze ( O ) */ \ 414514f5e3Sopenharmony_ci V("freeze", Freeze, 1, INVALID) \ 424514f5e3Sopenharmony_ci /* Object.fromEntries ( iterable ) */ \ 434514f5e3Sopenharmony_ci V("fromEntries", FromEntries, 1, INVALID) \ 444514f5e3Sopenharmony_ci /* Object.getOwnPropertyDescriptor ( O, P ) */ \ 454514f5e3Sopenharmony_ci V("getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2, INVALID) \ 464514f5e3Sopenharmony_ci /* Object.getOwnPropertyDescriptors ( O ) */ \ 474514f5e3Sopenharmony_ci V("getOwnPropertyDescriptors", GetOwnPropertyDescriptors, 1, ObjectGetOwnPropertyDescriptors) \ 484514f5e3Sopenharmony_ci /* Object.getOwnPropertyNames ( O ) */ \ 494514f5e3Sopenharmony_ci V("getOwnPropertyNames", GetOwnPropertyNames, 1, ObjectGetOwnPropertyNames) \ 504514f5e3Sopenharmony_ci /* Object.getOwnPropertySymbols ( O ) */ \ 514514f5e3Sopenharmony_ci V("getOwnPropertySymbols", GetOwnPropertySymbols, 1, ObjectGetOwnPropertySymbols) \ 524514f5e3Sopenharmony_ci /* Object.getPrototypeOf ( O ) */ \ 534514f5e3Sopenharmony_ci V("getPrototypeOf", GetPrototypeOf, 1, ObjectGetPrototypeOf) \ 544514f5e3Sopenharmony_ci /* Object.hasOwn ( O, P ) */ \ 554514f5e3Sopenharmony_ci V("hasOwn", HasOwn, 2, INVALID) \ 564514f5e3Sopenharmony_ci /* Object.is ( value1, value2 ) */ \ 574514f5e3Sopenharmony_ci V("is", Is, 2, ObjectIs) \ 584514f5e3Sopenharmony_ci /* Object.isExtensible ( O ) */ \ 594514f5e3Sopenharmony_ci V("isExtensible", IsExtensible, 1, INVALID) \ 604514f5e3Sopenharmony_ci /* Object.isFrozen ( O ) */ \ 614514f5e3Sopenharmony_ci V("isFrozen", IsFrozen, 1, ObjectIsFrozen) \ 624514f5e3Sopenharmony_ci /* Object.isSealed ( O ) */ \ 634514f5e3Sopenharmony_ci V("isSealed", IsSealed, 1, ObjectIsSealed) \ 644514f5e3Sopenharmony_ci /* Object.keys ( O ) */ \ 654514f5e3Sopenharmony_ci V("keys", Keys, 1, ObjectKeys) \ 664514f5e3Sopenharmony_ci /* Object.preventExtensions ( O ) */ \ 674514f5e3Sopenharmony_ci V("preventExtensions", PreventExtensions, 1, INVALID) \ 684514f5e3Sopenharmony_ci /* Object.seal ( O ) */ \ 694514f5e3Sopenharmony_ci V("seal", Seal, 1, INVALID) \ 704514f5e3Sopenharmony_ci /* Object.setPrototypeOf ( O, proto ) */ \ 714514f5e3Sopenharmony_ci V("setPrototypeOf", SetPrototypeOf, 2, ObjectSetPrototypeOf) \ 724514f5e3Sopenharmony_ci /* Object.values ( O ) */ \ 734514f5e3Sopenharmony_ci V("values", Values, 1, INVALID) 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_ci// List of functions in Object.prototype, excluding the constructor and '@@' properties. 764514f5e3Sopenharmony_ci// V(name, func, length, stubIndex) 774514f5e3Sopenharmony_ci// where BuiltinsObject::func refers to the native implementation of Object.prototype[name]. 784514f5e3Sopenharmony_ci#define BUILTIN_OBJECT_PROTOTYPE_FUNCTIONS(V) \ 794514f5e3Sopenharmony_ci V("createRealm", CreateRealm, 0, INVALID) \ 804514f5e3Sopenharmony_ci /* Object.prototype.hasOwnProperty ( V ) */ \ 814514f5e3Sopenharmony_ci V("hasOwnProperty", HasOwnProperty, 1, ObjectHasOwnProperty) \ 824514f5e3Sopenharmony_ci /* Object.prototype.isPrototypeOf ( V ) */ \ 834514f5e3Sopenharmony_ci V("isPrototypeOf", IsPrototypeOf, 1, ObjectIsPrototypeOf) \ 844514f5e3Sopenharmony_ci /* Object.prototype.propertyIsEnumerable ( V ) */ \ 854514f5e3Sopenharmony_ci V("propertyIsEnumerable", PropertyIsEnumerable, 1, INVALID) \ 864514f5e3Sopenharmony_ci /* Object.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) */ \ 874514f5e3Sopenharmony_ci V("toLocaleString", ToLocaleString, 0, INVALID) \ 884514f5e3Sopenharmony_ci /* Object.prototype.toString ( ) */ \ 894514f5e3Sopenharmony_ci V("toString", ToString, 0, ObjectToString) \ 904514f5e3Sopenharmony_ci /* Object.prototype.valueOf ( ) */ \ 914514f5e3Sopenharmony_ci V("valueOf", ValueOf, 0, INVALID) 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 944514f5e3Sopenharmony_cienum class KeyType : uint8_t { 954514f5e3Sopenharmony_ci STRING_TYPE = 0, 964514f5e3Sopenharmony_ci SYMBOL_TYPE, 974514f5e3Sopenharmony_ci}; 984514f5e3Sopenharmony_ci 994514f5e3Sopenharmony_ciclass BuiltinsObject : public base::BuiltinsBase { 1004514f5e3Sopenharmony_cipublic: 1014514f5e3Sopenharmony_ci // 19.1.1.1Object ( [ value ] ) 1024514f5e3Sopenharmony_ci static JSTaggedValue ObjectConstructor(EcmaRuntimeCallInfo *argv); 1034514f5e3Sopenharmony_ci 1044514f5e3Sopenharmony_ci // 19.1.2.1Object.assign ( target, ...sources ) 1054514f5e3Sopenharmony_ci static JSTaggedValue Assign(EcmaRuntimeCallInfo *argv); 1064514f5e3Sopenharmony_ci // 19.1.2.2Object.create ( O [ , Properties ] ) 1074514f5e3Sopenharmony_ci static JSTaggedValue Create(EcmaRuntimeCallInfo *argv); 1084514f5e3Sopenharmony_ci // 19.1.2.3Object.defineProperties ( O, Properties ) 1094514f5e3Sopenharmony_ci static JSTaggedValue DefineProperties(EcmaRuntimeCallInfo *argv); 1104514f5e3Sopenharmony_ci // 19.1.2.4Object.defineProperty ( O, P, Attributes ) 1114514f5e3Sopenharmony_ci static JSTaggedValue DefineProperty(EcmaRuntimeCallInfo *argv); 1124514f5e3Sopenharmony_ci // 19.1.2.5Object.freeze ( O ) 1134514f5e3Sopenharmony_ci static JSTaggedValue Freeze(EcmaRuntimeCallInfo *argv); 1144514f5e3Sopenharmony_ci // 19.1.2.6Object.getOwnPropertyDescriptor ( O, P ) 1154514f5e3Sopenharmony_ci static JSTaggedValue GetOwnPropertyDescriptor(EcmaRuntimeCallInfo *argv); 1164514f5e3Sopenharmony_ci // 19.1.2.7Object.getOwnPropertyNames ( O ) 1174514f5e3Sopenharmony_ci static JSTaggedValue GetOwnPropertyNames(EcmaRuntimeCallInfo *argv); 1184514f5e3Sopenharmony_ci // 19.1.2.8Object.getOwnPropertySymbols ( O ) 1194514f5e3Sopenharmony_ci static JSTaggedValue GetOwnPropertySymbols(EcmaRuntimeCallInfo *argv); 1204514f5e3Sopenharmony_ci // 19.1.2.9Object.getPrototypeOf ( O ) 1214514f5e3Sopenharmony_ci static JSTaggedValue GetPrototypeOf(EcmaRuntimeCallInfo *argv); 1224514f5e3Sopenharmony_ci // 19.1.2.10Object.is ( value1, value2 ) 1234514f5e3Sopenharmony_ci static JSTaggedValue Is(EcmaRuntimeCallInfo *argv); 1244514f5e3Sopenharmony_ci // 19.1.2.11Object.isExtensible ( O ) 1254514f5e3Sopenharmony_ci static JSTaggedValue IsExtensible(EcmaRuntimeCallInfo *argv); 1264514f5e3Sopenharmony_ci // 19.1.2.12Object.isFrozen ( O ) 1274514f5e3Sopenharmony_ci static JSTaggedValue IsFrozen(EcmaRuntimeCallInfo *argv); 1284514f5e3Sopenharmony_ci // 19.1.2.13Object.isSealed ( O ) 1294514f5e3Sopenharmony_ci static JSTaggedValue IsSealed(EcmaRuntimeCallInfo *argv); 1304514f5e3Sopenharmony_ci // 19.1.2.14 Object.keys(O) 1314514f5e3Sopenharmony_ci static JSTaggedValue Keys(EcmaRuntimeCallInfo *argv); 1324514f5e3Sopenharmony_ci // 20.1.2.22 Object.values(O) 1334514f5e3Sopenharmony_ci static JSTaggedValue Values(EcmaRuntimeCallInfo *argv); 1344514f5e3Sopenharmony_ci // 19.1.2.15 Object.preventExtensions(O) 1354514f5e3Sopenharmony_ci static JSTaggedValue PreventExtensions(EcmaRuntimeCallInfo *argv); 1364514f5e3Sopenharmony_ci // 19.1.2.17 Object.seal(O) 1374514f5e3Sopenharmony_ci static JSTaggedValue Seal(EcmaRuntimeCallInfo *argv); 1384514f5e3Sopenharmony_ci // 19.1.2.18 Object.setPrototypeOf(O, proto) 1394514f5e3Sopenharmony_ci static JSTaggedValue SetPrototypeOf(EcmaRuntimeCallInfo *argv); 1404514f5e3Sopenharmony_ci 1414514f5e3Sopenharmony_ci static JSTaggedValue GetOwnPropertyDescriptors(EcmaRuntimeCallInfo *argv); 1424514f5e3Sopenharmony_ci 1434514f5e3Sopenharmony_ci // 19.1.3.2 Object.prototype.hasOwnProperty(V) 1444514f5e3Sopenharmony_ci static JSTaggedValue HasOwnProperty(EcmaRuntimeCallInfo *argv); 1454514f5e3Sopenharmony_ci static JSTaggedValue HasOwnPropertyInternal(JSThread *thread, JSHandle<JSTaggedValue> thisValue, 1464514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> prop); 1474514f5e3Sopenharmony_ci // 19.1.3.3 Object.prototype.isPrototypeOf(V) 1484514f5e3Sopenharmony_ci static JSTaggedValue IsPrototypeOf(EcmaRuntimeCallInfo *argv); 1494514f5e3Sopenharmony_ci // 19.1.3.4 Object.prototype.propertyIsEnumerable(V) 1504514f5e3Sopenharmony_ci static JSTaggedValue PropertyIsEnumerable(EcmaRuntimeCallInfo *argv); 1514514f5e3Sopenharmony_ci // 19.1.3.5 Object.prototype.toLocaleString([reserved1[, reserved2]]) 1524514f5e3Sopenharmony_ci static JSTaggedValue ToLocaleString(EcmaRuntimeCallInfo *argv); 1534514f5e3Sopenharmony_ci // 19.1.3.6 Object.prototype.toString() 1544514f5e3Sopenharmony_ci static JSTaggedValue ToString(EcmaRuntimeCallInfo *argv); 1554514f5e3Sopenharmony_ci // 19.1.3.7 Object.prototype.valueOf() 1564514f5e3Sopenharmony_ci static JSTaggedValue ValueOf(EcmaRuntimeCallInfo *argv); 1574514f5e3Sopenharmony_ci 1584514f5e3Sopenharmony_ci static JSTaggedValue CreateRealm(EcmaRuntimeCallInfo *argv); 1594514f5e3Sopenharmony_ci // 20.1.2.5 Object.entries ( O ) 1604514f5e3Sopenharmony_ci static JSTaggedValue Entries(EcmaRuntimeCallInfo *argv); 1614514f5e3Sopenharmony_ci // 20.1.2.7 Object.fromEntries ( iterable ) 1624514f5e3Sopenharmony_ci static JSTaggedValue FromEntries(EcmaRuntimeCallInfo *argv); 1634514f5e3Sopenharmony_ci // B.2.2.1 Object.prototype.__proto__ 1644514f5e3Sopenharmony_ci static JSTaggedValue ProtoGetter(EcmaRuntimeCallInfo *argv); 1654514f5e3Sopenharmony_ci static JSTaggedValue ProtoSetter(EcmaRuntimeCallInfo *argv); 1664514f5e3Sopenharmony_ci 1674514f5e3Sopenharmony_ci // 20.1.2.7.1 CreateDataPropertyOnObject Functions 1684514f5e3Sopenharmony_ci static JSTaggedValue CreateDataPropertyOnObjectFunctions(EcmaRuntimeCallInfo *argv); 1694514f5e3Sopenharmony_ci // 20.1.2.13 Object.hasOwn ( O, P ) 1704514f5e3Sopenharmony_ci static JSTaggedValue HasOwn(EcmaRuntimeCallInfo *argv); 1714514f5e3Sopenharmony_ci 1724514f5e3Sopenharmony_ci static Span<const base::BuiltinFunctionEntry> GetObjectFunctions() 1734514f5e3Sopenharmony_ci { 1744514f5e3Sopenharmony_ci return Span<const base::BuiltinFunctionEntry>(OBJECT_FUNCTIONS); 1754514f5e3Sopenharmony_ci } 1764514f5e3Sopenharmony_ci 1774514f5e3Sopenharmony_ci // Excluding the constructor and '@@' internal properties 1784514f5e3Sopenharmony_ci static Span<const base::BuiltinFunctionEntry> GetObjectPrototypeFunctions() 1794514f5e3Sopenharmony_ci { 1804514f5e3Sopenharmony_ci return Span<const base::BuiltinFunctionEntry>(OBJECT_PROTOTYPE_FUNCTIONS); 1814514f5e3Sopenharmony_ci } 1824514f5e3Sopenharmony_ci 1834514f5e3Sopenharmony_ci static JSTaggedValue AssignTaggedValue(JSThread *thread, const JSHandle<JSTaggedValue> &source, 1844514f5e3Sopenharmony_ci const JSHandle<JSObject> &toAssign); 1854514f5e3Sopenharmony_ci 1864514f5e3Sopenharmony_ci static Span<const std::pair<std::string_view, bool>> GetFunctionPrototypeProperties() 1874514f5e3Sopenharmony_ci { 1884514f5e3Sopenharmony_ci return Span<const std::pair<std::string_view, bool>>(OBJECT_PROTOTYPE_PROPERTIES); 1894514f5e3Sopenharmony_ci } 1904514f5e3Sopenharmony_ci 1914514f5e3Sopenharmony_ci static Span<const std::pair<std::string_view, bool>> GetFunctionProperties() 1924514f5e3Sopenharmony_ci { 1934514f5e3Sopenharmony_ci return Span<const std::pair<std::string_view, bool>>(OBJECT_PROPERTIES); 1944514f5e3Sopenharmony_ci } 1954514f5e3Sopenharmony_ci 1964514f5e3Sopenharmony_ciprivate: 1974514f5e3Sopenharmony_ci#define BUILTIN_OBJECT_FUNCTION_ENTRY(name, func, length, id) \ 1984514f5e3Sopenharmony_ci base::BuiltinFunctionEntry::Create(name, BuiltinsObject::func, length, kungfu::BuiltinsStubCSigns::id), 1994514f5e3Sopenharmony_ci 2004514f5e3Sopenharmony_ci static constexpr std::array OBJECT_FUNCTIONS = { 2014514f5e3Sopenharmony_ci BUILTIN_OBJECT_FUNCTIONS(BUILTIN_OBJECT_FUNCTION_ENTRY) 2024514f5e3Sopenharmony_ci }; 2034514f5e3Sopenharmony_ci static constexpr std::array OBJECT_PROTOTYPE_FUNCTIONS = { 2044514f5e3Sopenharmony_ci BUILTIN_OBJECT_PROTOTYPE_FUNCTIONS(BUILTIN_OBJECT_FUNCTION_ENTRY) 2054514f5e3Sopenharmony_ci }; 2064514f5e3Sopenharmony_ci#undef BUILTIN_OBJECT_FUNCTION_ENTRY 2074514f5e3Sopenharmony_ci 2084514f5e3Sopenharmony_ci#define OBJECT_PROPERTIES_PAIR(name, func, length, id) \ 2094514f5e3Sopenharmony_ci std::pair<std::string_view, bool>(name, false), 2104514f5e3Sopenharmony_ci 2114514f5e3Sopenharmony_ci static constexpr std::array OBJECT_PROTOTYPE_PROPERTIES = { 2124514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("constructor", false), 2134514f5e3Sopenharmony_ci BUILTIN_OBJECT_PROTOTYPE_FUNCTIONS(OBJECT_PROPERTIES_PAIR) 2144514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("__proto__", true), 2154514f5e3Sopenharmony_ci }; 2164514f5e3Sopenharmony_ci 2174514f5e3Sopenharmony_ci static constexpr std::array OBJECT_PROPERTIES = { 2184514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("length", false), 2194514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("name", false), 2204514f5e3Sopenharmony_ci std::pair<std::string_view, bool>("prototype", false), 2214514f5e3Sopenharmony_ci BUILTIN_OBJECT_FUNCTIONS(OBJECT_PROPERTIES_PAIR) 2224514f5e3Sopenharmony_ci 2234514f5e3Sopenharmony_ci }; 2244514f5e3Sopenharmony_ci#undef OBJECT_PROPERTIES_PAIR 2254514f5e3Sopenharmony_ci 2264514f5e3Sopenharmony_ci static JSTaggedValue ObjectDefineProperties(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 2274514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &prop); 2284514f5e3Sopenharmony_ci static JSTaggedValue GetOwnPropertyKeys(JSThread *thread, const JSHandle<JSTaggedValue> &obj, const KeyType &type); 2294514f5e3Sopenharmony_ci static JSTaggedValue GetBuiltinObjectToString(JSThread *thread, const JSHandle<JSObject> &object); 2304514f5e3Sopenharmony_ci}; 2314514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins 2324514f5e3Sopenharmony_ci#endif // ECMASCRIPT_BUILTINS_BUILTINS_OBJECT_H 233