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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_TYPES_H 17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_TYPES_H 18 19 #include <string> 20 #include <variant> 21 22 #include "frameworks/bridge/declarative_frontend/engine/quickjs/qjs_declarative_engine.h" 23 #include "frameworks/bridge/declarative_frontend/engine/quickjs/qjs_fwd.h" 24 #include "frameworks/bridge/declarative_frontend/engine/quickjs/qjs_value_conversions.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif // __cplusplus 29 #include "third_party/quickjs/cutils.h" 30 #include "third_party/quickjs/quickjs-libc.h" 31 32 #ifdef __cplusplus 33 } 34 #endif // __cplusplus 35 36 #define FAKE_PTR_FOR_FUNCTION_ACCESS(klass) \ 37 const klass* operator->() const \ 38 { \ 39 return this; \ 40 } 41 42 #define ENABLE_CAST_FROM_THIS(klass) \ 43 static klass Cast(const QJSValue& other) \ 44 { \ 45 return klass(other.GetHandle()); \ 46 } 47 48 namespace OHOS::Ace::Framework { 49 50 using FunctionCallback = JSValue (*)(JSContext*, JSValueConst, int, JSValueConst*); 51 52 class QJSValue { 53 public: 54 QJSValue(); 55 explicit QJSValue(JSValue rhs); 56 virtual ~QJSValue(); 57 QJSValue(const QJSValue& rhs); 58 QJSValue(QJSValue&& rhs); 59 QJSValue& operator=(const QJSValue& rhs); 60 QJSValue& operator=(QJSValue&& rhs); 61 // operator JSValue() const; 62 QJSValue Dup() const; 63 void Free(); 64 void Reset(); 65 bool IsEmpty() const; 66 bool IsFunction() const; 67 bool IsNumber() const; 68 bool IsString() const; 69 bool IsBoolean() const; 70 bool IsArray() const; 71 bool IsObject() const; 72 bool IsUndefined() const; 73 bool IsNull() const; 74 std::string ToString() const; 75 bool ToBoolean() const; 76 JSContext* GetJsContext() const; 77 78 template<typename T> 79 T ToNumber() const; 80 GetHandle()81 JSValue GetHandle() const 82 { 83 return value_; 84 } 85 86 FAKE_PTR_FOR_FUNCTION_ACCESS(QJSValue) 87 88 private: 89 JSValue value_; 90 }; 91 92 class QJSArray : public QJSValue { 93 public: 94 QJSArray(); 95 explicit QJSArray(JSValue arr); 96 ~QJSArray() override = default; 97 QJSRef<QJSValue> GetValueAt(size_t index) const; 98 void SetValueAt(size_t index, QJSRef<QJSValue> value) const; 99 size_t Length() const; 100 FAKE_PTR_FOR_FUNCTION_ACCESS(QJSArray) 101 ENABLE_CAST_FROM_THIS(QJSArray) 102 103 static QJSArray New(); 104 }; 105 106 class QJSObject : public QJSValue { 107 public: 108 QJSObject(); 109 explicit QJSObject(JSValue val); 110 ~QJSObject() override = default; 111 112 template<typename U> 113 U* Unwrap() const; 114 115 template<typename U> 116 void Wrap(U* data) const; 117 118 template<typename T> 119 void SetProperty(const char* prop, const T value) const; 120 void SetPropertyJsonObject(const char* prop, const char* value) const; 121 void SetPropertyObject(const char* prop, QJSRef<QJSValue> value) const; 122 QJSRef<QJSArray> GetPropertyNames() const; 123 QJSRef<QJSValue> GetProperty(const char* prop) const; 124 QJSRef<QJSValue> ToJsonObject(const char* value) const; 125 126 FAKE_PTR_FOR_FUNCTION_ACCESS(QJSObject) 127 ENABLE_CAST_FROM_THIS(QJSObject) 128 129 static QJSObject New(); 130 }; 131 132 class QJSObjTemplate : public QJSValue { 133 public: 134 QJSObjTemplate() = default; 135 explicit QJSObjTemplate(JSValue val); 136 137 void SetInternalFieldCount(int32_t count) const; 138 QJSRef<QJSObject> NewInstance() const; 139 static JSValue New(); 140 141 FAKE_PTR_FOR_FUNCTION_ACCESS(QJSObjTemplate) 142 }; 143 144 class QJSFunction : public QJSValue { 145 public: 146 QJSFunction(); 147 explicit QJSFunction(JSValue val); 148 ~QJSFunction() override = default; 149 150 QJSRef<QJSValue> Call(QJSRef<QJSValue> thisVal, int argc = 0, QJSRef<QJSValue> argv[] = nullptr) const; 151 static JSValue New(FunctionCallback func); 152 153 FAKE_PTR_FOR_FUNCTION_ACCESS(QJSFunction) 154 ENABLE_CAST_FROM_THIS(QJSFunction) 155 private: 156 JSContext* ctx_ = nullptr; 157 }; 158 159 struct QJSExecutionContext { 160 JSContext* context; 161 }; 162 163 class QJSCallbackInfo { 164 public: 165 QJSCallbackInfo(JSContext* ctx, JSValueConst thisObj, int argc, JSValueConst* argv); 166 ~QJSCallbackInfo() = default; 167 QJSCallbackInfo(const QJSCallbackInfo&) = delete; 168 QJSCallbackInfo& operator=(const QJSCallbackInfo&) = delete; 169 170 QJSRef<QJSValue> operator[](size_t index) const; 171 QJSRef<QJSObject> This() const; 172 int Length() const; 173 174 template<typename T> 175 void SetReturnValue(T* instance) const; 176 177 template<typename T> 178 void SetReturnValue(QJSRef<T> existing) const; 179 180 void ReturnSelf() const; 181 182 std::variant<void*, JSValue> GetReturnValue() 183 { 184 return retVal_; 185 } 186 187 QJSExecutionContext GetExecutionContext() const 188 { 189 return QJSExecutionContext { ctx_ }; 190 } 191 192 private: 193 JSContext* ctx_; 194 JSValueConst thisObj_; 195 int argc_; 196 JSValueConst* argv_; 197 198 mutable std::variant<void*, JSValue> retVal_; 199 }; 200 201 class QJSGCMarkCallbackInfo { 202 public: 203 QJSGCMarkCallbackInfo(JSRuntime* rt, JS_MarkFunc* markFunc); 204 ~QJSGCMarkCallbackInfo() = default; 205 206 QJSGCMarkCallbackInfo(const QJSGCMarkCallbackInfo&) = delete; 207 QJSGCMarkCallbackInfo& operator=(const QJSGCMarkCallbackInfo&) = delete; 208 209 template<typename T> 210 void Mark(const QJSRef<T>& val) const; 211 void Mark(JSValue val) const; 212 213 private: 214 JSRuntime* rt_; 215 JS_MarkFunc* markFunc_; 216 }; 217 218 class QJSException { 219 public: 220 template<typename... Args> 221 static void Throw(const char* format, Args... args); 222 template<typename... Args> 223 static void ThrowRangeError(const char* format, Args... args); 224 template<typename... Args> 225 static void ThrowReferenceError(const char* format, Args... args); 226 template<typename... Args> 227 static void ThrowSyntaxError(const char* format, Args... args); 228 template<typename... Args> 229 static void ThrowTypeError(const char* format, Args... args); 230 }; 231 232 } // namespace OHOS::Ace::Framework 233 234 #include "qjs_types.inl" 235 236 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_TYPES_H 237