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 QJSString : public QJSValue { 145 public: 146 explicit QJSString(const char* str); 147 explicit QJSString(JSValue str); 148 149 FAKE_PTR_FOR_FUNCTION_ACCESS(QJSString) 150 ENABLE_CAST_FROM_THIS(QJSString) 151 152 static QJSString New(const char* str); 153 }; 154 155 class QJSFunction : public QJSValue { 156 public: 157 QJSFunction(); 158 explicit QJSFunction(JSValue val); 159 ~QJSFunction() override = default; 160 161 QJSRef<QJSValue> Call(QJSRef<QJSValue> thisVal, int argc = 0, QJSRef<QJSValue> argv[] = nullptr) const; 162 static JSValue New(FunctionCallback func); 163 164 FAKE_PTR_FOR_FUNCTION_ACCESS(QJSFunction) 165 ENABLE_CAST_FROM_THIS(QJSFunction) 166 private: 167 JSContext* ctx_ = nullptr; 168 }; 169 170 struct QJSExecutionContext { 171 JSContext* context; 172 }; 173 174 class QJSCallbackInfo { 175 public: 176 QJSCallbackInfo(JSContext* ctx, JSValueConst thisObj, int argc, JSValueConst* argv); 177 ~QJSCallbackInfo() = default; 178 QJSCallbackInfo(const QJSCallbackInfo&) = delete; 179 QJSCallbackInfo& operator=(const QJSCallbackInfo&) = delete; 180 181 QJSRef<QJSValue> operator[](size_t index) const; 182 QJSRef<QJSObject> This() const; 183 int Length() const; 184 185 template<typename T> 186 void SetReturnValue(T* instance) const; 187 188 template<typename T> 189 void SetReturnValue(QJSRef<T> existing) const; 190 191 template<typename S> 192 void SetReturnValue(S any) const; 193 194 void ReturnSelf() const; 195 196 std::variant<void*, JSValue> GetReturnValue() 197 { 198 return retVal_; 199 } 200 201 QJSExecutionContext GetExecutionContext() const 202 { 203 return QJSExecutionContext { ctx_ }; 204 } 205 206 private: 207 JSContext* ctx_; 208 JSValueConst thisObj_; 209 int argc_; 210 JSValueConst* argv_; 211 212 mutable std::variant<void*, JSValue> retVal_; 213 }; 214 215 class QJSGCMarkCallbackInfo { 216 public: 217 QJSGCMarkCallbackInfo(JSRuntime* rt, JS_MarkFunc* markFunc); 218 ~QJSGCMarkCallbackInfo() = default; 219 220 QJSGCMarkCallbackInfo(const QJSGCMarkCallbackInfo&) = delete; 221 QJSGCMarkCallbackInfo& operator=(const QJSGCMarkCallbackInfo&) = delete; 222 223 template<typename T> 224 void Mark(const QJSRef<T>& val) const; 225 void Mark(JSValue val) const; 226 227 private: 228 JSRuntime* rt_; 229 JS_MarkFunc* markFunc_; 230 }; 231 232 class QJSException { 233 public: 234 template<typename... Args> 235 static void Throw(const char* format, Args... args); 236 template<typename... Args> 237 static void ThrowRangeError(const char* format, Args... args); 238 template<typename... Args> 239 static void ThrowReferenceError(const char* format, Args... args); 240 template<typename... Args> 241 static void ThrowSyntaxError(const char* format, Args... args); 242 template<typename... Args> 243 static void ThrowTypeError(const char* format, Args... args); 244 }; 245 246 } // namespace OHOS::Ace::Framework 247 248 #include "qjs_types.inl" 249 250 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_QJS_TYPES_H 251