• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-2025 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 PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JS_PROXY_JS_PROXY_H_
17 #define PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JS_PROXY_JS_PROXY_H_
18 
19 #include "include/mem/panda_containers.h"
20 #include "runtime/include/class.h"
21 #include "runtime/include/method.h"
22 
23 namespace ark::ets {
24 class EtsClass;
25 class EtsObject;
26 }  // namespace ark::ets
27 
28 namespace ark::ets::interop::js::js_proxy {
29 
30 class JSProxy {
31 public:
32     static JSProxy *CreateBuiltinProxy(EtsClass *etsClass, Span<Method *> targetMethods);
33     static JSProxy *CreateFunctionProxy(EtsClass *functionInterface);
34     static JSProxy *CreateInterfaceProxy(const PandaSet<Class *> &interfaces, std::string &interfaceName);
35 
GetProxyClass()36     EtsClass *GetProxyClass() const
37     {
38         return proxyKlass_;
39     }
40 
IsProxyClass(Class * klass)41     static bool IsProxyClass(Class *klass)
42     {
43         // For class descriptor "Lname;" proxy descriptor is "L$name$;"
44         const uint8_t *descriptor = klass->GetDescriptor();
45         Span<const uint8_t> desc(descriptor, utf::Mutf8Size(descriptor));
46         return desc[0] == 'L' && desc[1] == '$' && desc[desc.size() - 1] == ';' && desc[desc.size() - 2U] == '$';
47     }
48 
49     ~JSProxy() = default;
50 
51     NO_COPY_SEMANTIC(JSProxy);
52     NO_MOVE_SEMANTIC(JSProxy);
53 
54 private:
JSProxy(EtsClass * proxyKlass)55     explicit JSProxy(EtsClass *proxyKlass) : proxyKlass_(proxyKlass) {}
56 
57     static JSProxy *CreateProxy(const uint8_t *descriptor, Class *baseClass, Span<Method *> targetMethods,
58                                 const PandaVector<Class *> &interfaces, void *callBridge);
59     EtsClass *const proxyKlass_ {};
60     // NOTE(vpukhov): add flag if original class has final methods or public fields
61     // NOTE(vpukhov): must ensure compat-class methods except accessors do not access its private state
62 
63     // Allocator calls our private ctor
64     friend class mem::Allocator;
65 };
66 
67 }  // namespace ark::ets::interop::js::js_proxy
68 
69 #endif  // !PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JS_PROXY_JS_PROXY_H_
70