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 FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_ARK_JS_VALUE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_ARK_JS_VALUE_H
18 
19 #include "frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h"
20 #include "frameworks/bridge/js_frontend/engine/jsi/js_value.h"
21 
22 // NOLINTNEXTLINE(readability-identifier-naming)
23 namespace OHOS::Ace::Framework {
24 using panda::ArrayRef;
25 using panda::EscapeLocalScope;
26 using panda::FunctionRef;
27 using panda::Global;
28 using panda::JSON;
29 using panda::JSValueRef;
30 using panda::Local;
31 using panda::LocalScope;
32 using panda::NumberRef;
33 using panda::ObjectRef;
34 using panda::StringRef;
35 class PandaFunctionData;
36 
37 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
38 class ArkJSValue final : public JsValue {
39 public:
ArkJSValue(const shared_ptr<ArkJSRuntime> & runtime,Local<JSValueRef> value)40     ArkJSValue(const shared_ptr<ArkJSRuntime> &runtime, Local<JSValueRef> value) : value_(runtime->GetEcmaVm(), value)
41     {
42     }
~ArkJSValue()43     ~ArkJSValue() override
44     {
45         value_.FreeGlobalHandleAddr();
46     }
47 
48     int32_t ToInt32(shared_ptr<JsRuntime> runtime) override;
49     double ToDouble(shared_ptr<JsRuntime> runtime) override;
50     std::string ToString(shared_ptr<JsRuntime> runtime) override;
51     bool ToBoolean(shared_ptr<JsRuntime> runtime) override;
52 
53     bool IsUndefined(shared_ptr<JsRuntime> runtime) override;
54     bool IsNull(shared_ptr<JsRuntime> runtime) override;
55     bool IsBoolean(shared_ptr<JsRuntime> runtime) override;
56     bool IsInt32(shared_ptr<JsRuntime> runtime) override;
57     bool WithinInt32(shared_ptr<JsRuntime> runtime) override;
58     bool IsString(shared_ptr<JsRuntime> runtime) override;
59     bool IsNumber(shared_ptr<JsRuntime> runtime) override;
60     bool IsObject(shared_ptr<JsRuntime> runtime) override;
61     bool IsArray(shared_ptr<JsRuntime> runtime) override;
62     bool IsFunction(shared_ptr<JsRuntime> runtime) override;
63     bool IsException(shared_ptr<JsRuntime> runtime) override;
64 
65     shared_ptr<JsValue> Call(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
66                              std::vector<shared_ptr<JsValue>> argv, int32_t argc) override;
67 
68     bool GetPropertyNames(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> &propName, int32_t &len) override;
69     bool GetEnumerablePropertyNames(shared_ptr<JsRuntime> runtime,
70                                     shared_ptr<JsValue> &propName, int32_t &len) override;
71     shared_ptr<JsValue> GetProperty(shared_ptr<JsRuntime> runtime, int32_t idx) override;
72     shared_ptr<JsValue> GetProperty(shared_ptr<JsRuntime> runtime, const std::string &name) override;
73     shared_ptr<JsValue> GetProperty(shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue> &name) override;
74 
75     bool SetProperty(shared_ptr<JsRuntime> runtime, const std::string &name, const shared_ptr<JsValue> &value) override;
76     bool SetProperty(shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue> &name,
77                      const shared_ptr<JsValue> &value) override;
78     bool SetAccessorProperty(shared_ptr<JsRuntime> runtime, const std::string &name, const shared_ptr<JsValue> &getter,
79                              const shared_ptr<JsValue> &setter) override;
80     bool SetAccessorProperty(shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue> &name,
81                              const shared_ptr<JsValue> &getter, const shared_ptr<JsValue> &setter) override;
82 
83     bool HasProperty(shared_ptr<JsRuntime> runtime, const std::string &name) override;
84     bool HasProperty(shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue> &name) override;
85 
86     int32_t GetArrayLength(shared_ptr<JsRuntime> runtime) override;
87     shared_ptr<JsValue> GetElement(shared_ptr<JsRuntime> runtime, int32_t idx) override;
88     std::string GetJsonString(const shared_ptr<JsRuntime>& runtime) override;
89 
GetValue(const shared_ptr<ArkJSRuntime> & runtime)90     Local<JSValueRef> GetValue(const shared_ptr<ArkJSRuntime> &runtime)
91     {
92         return value_.ToLocal(runtime->GetEcmaVm());
93     }
94 
SetValue(const shared_ptr<ArkJSRuntime> & runtime,const Local<JSValueRef> & value)95     void SetValue(const shared_ptr<ArkJSRuntime> &runtime, const Local<JSValueRef> &value)
96     {
97         value_ = Global<JSValueRef>(runtime->GetEcmaVm(), value);
98     }
99 
100 private:
101     bool CheckException(const shared_ptr<ArkJSRuntime> &runtime, const Local<JSValueRef> &value) const;
102     bool CheckException(const shared_ptr<ArkJSRuntime> &runtime) const;
103 
104     Global<JSValueRef> value_ {};
105 };
106 }  // namespace OHOS::Ace::Framework
107 #endif  // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_ARK_JS_VALUE_H
108