1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ECMASCRIPT_BUILTINS_BUILTINS_REFLECT_H
17#define ECMASCRIPT_BUILTINS_BUILTINS_REFLECT_H
18
19#include "ecmascript/base/builtins_base.h"
20#include "ecmascript/js_function.h"
21#include "ecmascript/js_array.h"
22
23// List of functions in Reflect, excluding the '@@' properties.
24// V(name, func, length, stubIndex)
25// where BuiltinsRefject::func refers to the native implementation of Reflect[name].
26//       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
27#define BUILTIN_REFLECT_FUNCTIONS(V)                                                    \
28    /* Reflect.apply ( target, thisArgument, argumentsList ) */                         \
29    V("apply",                    ReflectApply,                    3, ReflectApply)     \
30    /* Reflect.construct ( target, argumentsList [ , newTarget ] ) */                   \
31    V("construct",                ReflectConstruct,                2, ReflectConstruct) \
32    /* Reflect.defineProperty ( target, propertyKey, attributes ) */                    \
33    V("defineProperty",           ReflectDefineProperty,           3, INVALID)          \
34    /* Reflect.deleteProperty ( target, propertyKey ) */                                \
35    V("deleteProperty",           ReflectDeleteProperty,           2, INVALID)          \
36    /* Reflect.get ( target, propertyKey [ , receiver ] ) */                            \
37    V("get",                      ReflectGet,                      2, ReflectGet)       \
38    /* Reflect.getOwnPropertyDescriptor ( target, propertyKey ) */                      \
39    V("getOwnPropertyDescriptor", ReflectGetOwnPropertyDescriptor, 2, INVALID)          \
40    /* Reflect.getPrototypeOf ( target ) */                                             \
41    V("getPrototypeOf",           ReflectGetPrototypeOf,           1, ReflectGetPrototypeOf)  \
42    /* Reflect.has ( target, propertyKey ) */                                           \
43    V("has",                      ReflectHas,                      2, ReflectHas)       \
44    /* Reflect.isExtensible ( target ) */                                               \
45    V("isExtensible",             ReflectIsExtensible,             1, INVALID)          \
46    /* Reflect.ownKeys ( target ) */                                                    \
47    V("ownKeys",                  ReflectOwnKeys,                  1, INVALID)          \
48    /* Reflect.preventExtensions ( target ) */                                          \
49    V("preventExtensions",        ReflectPreventExtensions,        1, INVALID)          \
50    /* Reflect.set ( target, propertyKey, V [ , receiver ] ) */                         \
51    V("set",                      ReflectSet,                      3, INVALID)          \
52    /* Reflect.setPrototypeOf ( target, proto ) */                                      \
53    V("setPrototypeOf",           ReflectSetPrototypeOf,           2, INVALID)
54
55namespace panda::ecmascript::builtins {
56class BuiltinsReflect : public base::BuiltinsBase {
57public:
58    // ecma 26.1.1
59    static JSTaggedValue ReflectApply(EcmaRuntimeCallInfo *argv);
60    static JSTaggedValue ReflectApplyInternal(JSThread *thread, JSHandle<JSTaggedValue> target,
61                                              JSHandle<JSTaggedValue> thisArgument,
62                                              JSHandle<JSTaggedValue> argumentsList);
63
64    // ecma 26.1.2
65    static JSTaggedValue ReflectConstruct(EcmaRuntimeCallInfo *argv);
66    static JSTaggedValue ReflectConstructInternal(JSThread *thread, JSHandle<JSTaggedValue> target,
67                                                  JSHandle<TaggedArray> args, JSHandle<JSTaggedValue> newTarget);
68
69    // ecma 26.1.3
70    static JSTaggedValue ReflectDefineProperty(EcmaRuntimeCallInfo *argv);
71
72    // ecma 26.1.4
73    static JSTaggedValue ReflectDeleteProperty(EcmaRuntimeCallInfo *argv);
74
75    // ecma 26.1.5
76    static JSTaggedValue ReflectGet(EcmaRuntimeCallInfo *argv);
77
78    // ecma 26.1.6
79    static JSTaggedValue ReflectGetOwnPropertyDescriptor(EcmaRuntimeCallInfo *argv);
80
81    // ecma 26.1.7
82    static JSTaggedValue ReflectGetPrototypeOf(EcmaRuntimeCallInfo *argv);
83
84    // ecma 26.1.8
85    static JSTaggedValue ReflectHas(EcmaRuntimeCallInfo *argv);
86    static JSTaggedValue ReflectHasInternal(JSThread *thread, JSHandle<JSTaggedValue> target,
87                                            JSHandle<JSTaggedValue> key);
88
89    // ecma 26.1.9
90    static JSTaggedValue ReflectIsExtensible(EcmaRuntimeCallInfo *argv);
91
92    // ecma 26.1.10
93    static JSTaggedValue ReflectOwnKeys(EcmaRuntimeCallInfo *argv);
94
95    // ecma 26.1.11
96    static JSTaggedValue ReflectPreventExtensions(EcmaRuntimeCallInfo *argv);
97
98    // ecma 26.1.12
99    static JSTaggedValue ReflectSet(EcmaRuntimeCallInfo *argv);
100
101    // ecma 26.1.13
102    static JSTaggedValue ReflectSetPrototypeOf(EcmaRuntimeCallInfo *argv);
103
104    // Excluding the '@@' internal properties.
105    static Span<const base::BuiltinFunctionEntry> GetReflectFunctions()
106    {
107        return Span<const base::BuiltinFunctionEntry>(REFLECT_FUNCTIONS);
108    }
109
110private:
111#define BUILTINS_REFLECT_FUNCTION_ENTRY(name, method, length, id) \
112    base::BuiltinFunctionEntry::Create(name, BuiltinsReflect::method, length, kungfu::BuiltinsStubCSigns::id),
113
114    static constexpr std::array REFLECT_FUNCTIONS  = {
115        BUILTIN_REFLECT_FUNCTIONS(BUILTINS_REFLECT_FUNCTION_ENTRY)
116    };
117#undef BUILTINS_REFLECT_FUNCTION_ENTRY
118};
119}  // namespace panda::ecmascript::builtins
120#endif  // ECMASCRIPT_BUILTINS_BUILTINS_REFLECT_H
121