/* * Tencent is pleased to support the open source community by making Puerts available. * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. * Puerts is licensed under the BSD 3-Clause License, except for the third-party components listed in the file 'LICENSE' which may * be subject to their corresponding license terms. This file is subject to the terms and conditions defined in file 'LICENSE', * which is part of this source code package. */ #pragma once #include #include #include #include "CoreMinimal.h" #include "CoreUObject.h" #include "PropertyTranslator.h" #include "FunctionTranslator.h" #include "JSClassRegister.h" #include "NamespaceDef.h" #define PUERTS_REUSE_STRUCTWRAPPER_FUNCTIONTEMPLATE 1 namespace PUERTS_NAMESPACE { class FStructWrapper { public: explicit FStructWrapper(UStruct* InStruct) : ExternalInitialize(nullptr), ExternalFinalize(nullptr), Struct(InStruct) { } FORCEINLINE void Init(UStruct* InStruct, bool IsReuseTemplate) { Struct = InStruct; if (!IsReuseTemplate) { ExternalInitialize = nullptr; ExternalFinalize = nullptr; Properties.clear(); ExtensionMethods.clear(); #if PUERTS_REUSE_STRUCTWRAPPER_FUNCTIONTEMPLATE CachedFunctionTemplate.Reset(); #endif } } void AddExtensionMethods(const std::vector& InExtensionMethods); #if PUERTS_REUSE_STRUCTWRAPPER_FUNCTIONTEMPLATE v8::UniquePersistent CachedFunctionTemplate; #endif protected: std::vector> Properties; TMap> PropertiesMap; std::shared_ptr GetPropertyTranslator(PropertyMacro* InProperty); TMap> FunctionsMap; TMap> MethodsMap; std::shared_ptr GetFunctionTranslator(UFunction* InFunction); std::shared_ptr GetMethodTranslator(UFunction* InFunction, bool IsExtension); void RefreshMethod(UFunction* InFunction); void InitTemplateProperties( v8::Isolate* Isolate, UStruct* InStruct, v8::Local Template, bool IsReuseTemplate); v8::Local ToFunctionTemplate(v8::Isolate* Isolate, v8::FunctionCallback Construtor); std::vector ExtensionMethods; typedef void* (*V8InitializeFuncType)(const v8::FunctionCallbackInfo& Info); V8InitializeFuncType ExternalInitialize; pesapi_finalize ExternalFinalize; TWeakObjectPtr Struct; #if PUERTS_KEEP_UOBJECT_REFERENCE bool IsNativeTakeJsRef = false; #else bool IsNativeTakeJsRef = true; #endif bool IsTypeScriptGeneratedClass = false; static void StaticClass(const v8::FunctionCallbackInfo& Info); static void Find(const v8::FunctionCallbackInfo& Info); static void Load(const v8::FunctionCallbackInfo& Info); friend class FJsEnvImpl; }; class FScriptStructWrapper : public FStructWrapper { public: explicit FScriptStructWrapper(UScriptStruct* InScriptStruct) : FStructWrapper(InScriptStruct) { } static void OnGarbageCollectedWithFree(const v8::WeakCallbackInfo& Data); static void OnGarbageCollected(const v8::WeakCallbackInfo& Data); static void* Alloc(UScriptStruct* InScriptStruct); static void Free(TWeakObjectPtr InStruct, pesapi_finalize InExternalFinalize, void* Ptr); void Free(void* Ptr) { Free(Struct, ExternalFinalize, Ptr); } static void New(const v8::FunctionCallbackInfo& Info); void New(v8::Isolate* Isolate, v8::Local& Context, const v8::FunctionCallbackInfo& Info); }; class FClassWrapper : public FStructWrapper { public: explicit FClassWrapper(UClass* InClass) : FStructWrapper(InClass) { } static void OnGarbageCollected(const v8::WeakCallbackInfo& Data); static void New(const v8::FunctionCallbackInfo& Info); void New(v8::Isolate* Isolate, v8::Local& Context, const v8::FunctionCallbackInfo& Info); }; } // namespace PUERTS_NAMESPACE