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_JS_VALUE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_VALUE_H 18 19 #include <functional> 20 #include <string> 21 #include <memory> 22 23 // NOLINTNEXTLINE(readability-identifier-naming) 24 namespace OHOS::Ace::Framework { 25 using std::shared_ptr; 26 class JsRuntime; 27 /* A class that represent all types of js value. Since we don't have separate class for each type, please check it's 28 * type before calling a type specific method such as GetProperty. 29 */ 30 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions) 31 class JsValue { 32 public: 33 JsValue() = default; 34 virtual ~JsValue() = default; 35 36 virtual int32_t ToInt32(shared_ptr<JsRuntime> runtime) = 0; 37 virtual double ToDouble(shared_ptr<JsRuntime> runtime) = 0; 38 virtual std::string ToString(shared_ptr<JsRuntime> runtime) = 0; 39 virtual bool ToBoolean(shared_ptr<JsRuntime> runtime) = 0; 40 41 virtual bool IsObject(shared_ptr<JsRuntime> runtime) = 0; 42 virtual bool IsArray(shared_ptr<JsRuntime> runtime) = 0; 43 virtual bool IsUndefined(shared_ptr<JsRuntime> runtime) = 0; 44 virtual bool IsNull(shared_ptr<JsRuntime> runtime) = 0; 45 virtual bool IsNumber(shared_ptr<JsRuntime> runtime) = 0; 46 virtual bool IsInt32(shared_ptr<JsRuntime> runtime) = 0; 47 virtual bool WithinInt32(shared_ptr<JsRuntime> runtime) = 0; 48 virtual bool IsBoolean(shared_ptr<JsRuntime> runtime) = 0; 49 virtual bool IsString(shared_ptr<JsRuntime> runtime) = 0; 50 virtual bool IsFunction(shared_ptr<JsRuntime> runtime) = 0; 51 virtual bool IsException(shared_ptr<JsRuntime> runtime) = 0; 52 53 // NOLINTNEXTLINE(google-runtime-references) 54 virtual bool GetPropertyNames(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> &propertyNames, int32_t &len) = 0; 55 virtual bool GetEnumerablePropertyNames(shared_ptr<JsRuntime> runtime, 56 shared_ptr<JsValue> &propertyNames, int32_t &len) = 0; 57 58 // Get object property by name or index, returns an nullptr if failed to get the specified property. 59 virtual shared_ptr<JsValue> GetProperty(shared_ptr<JsRuntime> runtime, int32_t idx) = 0; 60 virtual shared_ptr<JsValue> GetProperty(shared_ptr<JsRuntime> runtime, const std::string &name) = 0; 61 virtual shared_ptr<JsValue> GetProperty(shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue> &name) = 0; 62 63 // Set a property with the given name and value, returns true if success. 64 virtual bool SetProperty(shared_ptr<JsRuntime> runtime, const std::string &name, 65 const shared_ptr<JsValue> &value) = 0; 66 virtual bool SetProperty(shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue> &name, 67 const shared_ptr<JsValue> &value) = 0; 68 virtual bool SetAccessorProperty(shared_ptr<JsRuntime> runtime, const std::string &name, 69 const shared_ptr<JsValue> &getter, const shared_ptr<JsValue> &setter) = 0; 70 virtual bool SetAccessorProperty(shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue> &name, 71 const shared_ptr<JsValue> &getter, const shared_ptr<JsValue> &setter) = 0; 72 73 // Has object property by name, returns true if has the specified property. 74 virtual bool HasProperty(shared_ptr<JsRuntime> runtime, const std::string &name) = 0; 75 virtual bool HasProperty(shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue> &name) = 0; 76 77 // Get an array's length. Return -1 if fails. 78 virtual int32_t GetArrayLength(shared_ptr<JsRuntime> runtime) = 0; 79 // Get an array element by index, Return nullptr if fails. 80 virtual shared_ptr<JsValue> GetElement(shared_ptr<JsRuntime> runtime, int32_t idx) = 0; 81 82 virtual shared_ptr<JsValue> Call(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj, 83 std::vector<shared_ptr<JsValue>> argv, int32_t argc) = 0; 84 virtual std::string GetJsonString(const shared_ptr<JsRuntime>& runtime) = 0; 85 }; 86 } // namespace OHOS::Ace::Framework 87 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_VALUE_H 88