• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_JSPROXY_H
17 #define ECMASCRIPT_JSPROXY_H
18 
19 #include "ecmascript/tagged_array.h"
20 #include "js_object.h"
21 
22 namespace panda::ecmascript {
23 class JSProxy final : public ECMAObject {
24 public:
25     CAST_CHECK(JSProxy, IsJSProxy);
26 
27     // ES6 9.5.15 ProxyCreate(target, handler)
28     static JSHandle<JSProxy> ProxyCreate(JSThread *thread, const JSHandle<JSTaggedValue> &target,
29                                          const JSHandle<JSTaggedValue> &handler);
30     // ES6 9.5.1 [[GetPrototypeOf]] ( )
31     static JSTaggedValue GetPrototype(JSThread *thread, const JSHandle<JSProxy> &proxy);
32     // ES6 9.5.2 [[SetPrototypeOf]] (V)
33     static bool SetPrototype(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &proto);
34     // ES6 9.5.3 [[IsExtensible]] ( )
35     static bool IsExtensible(JSThread *thread, const JSHandle<JSProxy> &proxy);
36     // ES6 9.5.4 [[PreventExtensions]] ( )
37     static bool PreventExtensions(JSThread *thread, const JSHandle<JSProxy> &proxy);
38     // ES6 9.5.5 [[GetOwnProperty]] (P)
39     static bool GetOwnProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key,
40                                PropertyDescriptor &desc);
41     // ES6 9.5.6 [[DefineOwnProperty]] (P, Desc)
42     static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key,
43                                   const PropertyDescriptor &desc);
44     // ES6 9.5.7 [[HasProperty]] (P)
45     static bool HasProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key);
46     // ES6 9.5.8 [[Get]] (P, Receiver)
GetProperty(JSThread * thread,const JSHandle<JSProxy> & proxy,const JSHandle<JSTaggedValue> & key)47     static inline OperationResult GetProperty(JSThread *thread, const JSHandle<JSProxy> &proxy,
48                                               const JSHandle<JSTaggedValue> &key)
49     {
50         return GetProperty(thread, proxy, key, JSHandle<JSTaggedValue>::Cast(proxy));
51     }
52     static OperationResult GetProperty(JSThread *thread, const JSHandle<JSProxy> &proxy,
53                                        const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &receiver);
54     // ES6 9.5.9 [[Set]] ( P, V, Receiver)
55     static inline bool SetProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key,
56                                    const JSHandle<JSTaggedValue> &value, bool mayThrow = false)
57     {
58         return SetProperty(thread, proxy, key, value, JSHandle<JSTaggedValue>::Cast(proxy), mayThrow);
59     }
60     static bool SetProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key,
61                             const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &receiver,
62                             bool mayThrow = false);
63     // ES6 9.5.10 [[Delete]] (P)
64     static bool DeleteProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key);
65 
66     // ES6 9.5.12 [[OwnPropertyKeys]] ()
67     static JSHandle<TaggedArray> OwnPropertyKeys(JSThread *thread, const JSHandle<JSProxy> &proxy);
68 
SetCallable(bool callable)69     void SetCallable(bool callable) const
70     {
71         GetClass()->SetCallable(callable);
72     }
73 
SetConstructor(bool constructor)74     void SetConstructor(bool constructor) const
75     {
76         GetClass()->SetConstructor(constructor);
77     }
78 
SetCallTarget(const JSThread * thread,JSMethod * p)79     void SetCallTarget([[maybe_unused]] const JSThread *thread, JSMethod *p)
80     {
81         SetMethod(p);
82     }
83 
84     JSHandle<JSTaggedValue> GetSourceTarget(JSThread *thread) const;
85 
86     // ES6 9.5.13 [[Call]] (thisArgument, argumentsList)
87     static JSTaggedValue CallInternal(JSThread *thread, const JSHandle<JSProxy> &proxy,
88                                       const JSHandle<JSTaggedValue> &thisArg, uint32_t argc,
89                                       const JSTaggedType argv[]);
90     // ES6 9.5.14 [[Construct]] ( argumentsList, newTarget)
91     static JSTaggedValue ConstructInternal(JSThread *thread, const JSHandle<JSProxy> &proxy, uint32_t argc,
92                                            const JSTaggedType argv[], const JSHandle<JSTaggedValue> &newTarget);
93 
94     bool IsArray(JSThread *thread) const;
95 
96     static constexpr size_t TARGET_OFFSET = ECMAObject::SIZE;
97     ACCESSORS(Target, TARGET_OFFSET, HANDLER_OFFSET)
98     ACCESSORS(Handler, HANDLER_OFFSET, METHOD_OFFSET)
99     ACCESSORS_NATIVE_FIELD(Method, JSMethod, METHOD_OFFSET, LAST_OFFSET)
100     DEFINE_ALIGN_SIZE(LAST_OFFSET);
101 
102     DECL_DUMP()
103 
104     DECL_VISIT_OBJECT(TARGET_OFFSET, METHOD_OFFSET)
105 };
106 }  // namespace panda::ecmascript
107 
108 #endif  // ECMASCRIPT_JSPROXY_H
109