1 /* 2 * Copyright (c) 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 ECMASCRIPT_NAPI_INCLUDE_JSNAPI_CLASS_FACTORY_H 17 #define ECMASCRIPT_NAPI_INCLUDE_JSNAPI_CLASS_FACTORY_H 18 19 #include <cstdint> 20 21 namespace panda { 22 class PropertyAttribute; 23 template <typename T> class Local; 24 class JSValueRef; 25 class JsiRuntimeCallInfo; 26 namespace ecmascript { 27 class JSThread; 28 class PropertyDescriptor; 29 template <typename T> class JSHandle; 30 class JSTaggedValue; 31 class JSHClass; 32 class JSObject; 33 class JSFunction; 34 35 using std::size_t; 36 using InternalFunctionCallback = JSValueRef(*)(JsiRuntimeCallInfo*); 37 38 class JSNApiClassCreationHelper { 39 public: 40 static JSHandle<JSFunction> CreateClassFuncWithProperties(JSThread *thread, const char *name, 41 InternalFunctionCallback nativeFunc, bool callNapi, 42 size_t propertyCount, size_t staticPropCount, 43 const Local<JSValueRef> *keys, PropertyAttribute *attrs, 44 PropertyDescriptor *descs); 45 private: 46 static inline void ConstructDescByAttr(const JSThread *thread, const PropertyAttribute &attr, 47 PropertyDescriptor *desc); 48 static inline void DestructDesc(PropertyDescriptor *desc); 49 static inline void DestructAttr(PropertyAttribute *attr); 50 static bool TryAddOriKeyAndOriAttrToHClass(const JSThread *thread, const Local<JSValueRef> &oriKey, 51 const PropertyAttribute &oriAttr, PropertyDescriptor &desc, 52 size_t &attrOffset, JSHandle<JSHClass> &hClass); 53 static JSHandle<JSObject> NewClassFuncProtoWithProperties(JSThread *thread, const JSHandle<JSFunction> &func, 54 size_t propertyCount, size_t nonStaticPropCount, 55 const Local<JSValueRef> *keys, PropertyAttribute *attrs, 56 PropertyDescriptor *descs); 57 }; 58 } // namespace ecmacript 59 } // namespace panda 60 61 #endif 62