Skip to content

Commit

Permalink
[unreal]尽可能减少std::string的使用,减少此类问题(https://github.com/Tencent/puerts/…
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Dec 6, 2024
1 parent 8674ea8 commit 012005d
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 31 deletions.
5 changes: 3 additions & 2 deletions unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "CppObjectMapper.h"
#include "DataTransfer.h"
#include "PString.h"

namespace PUERTS_NAMESPACE
{
Expand All @@ -31,7 +32,7 @@ void FCppObjectMapper::LoadCppType(const v8::FunctionCallbackInfo<v8::Value>& In
return;
}

std::string TypeName = *(v8::String::Utf8Value(Isolate, Info[0]));
String TypeName = *(v8::String::Utf8Value(Isolate, Info[0]));

auto ClassDef = FindCppTypeClassByName(TypeName);
if (ClassDef)
Expand All @@ -40,7 +41,7 @@ void FCppObjectMapper::LoadCppType(const v8::FunctionCallbackInfo<v8::Value>& In
}
else
{
const std::string ErrMsg = "can not find type: " + TypeName;
String ErrMsg = "can not find type: " + TypeName;
ThrowException(Isolate, ErrMsg.c_str());
}
}
Expand Down
24 changes: 12 additions & 12 deletions unreal/Puerts/Source/JsEnv/Private/JSClassRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,22 @@ class JSClassRegister
return clsDef;
}

const JSClassDefinition* FindCppTypeClassByName(const std::string& Name);
const JSClassDefinition* FindCppTypeClassByName(const String& Name);

#if USING_IN_UNREAL_ENGINE
void RegisterAddon(const std::string& Name, AddonRegisterFunc RegisterFunc);
void RegisterAddon(const String& Name, AddonRegisterFunc RegisterFunc);

AddonRegisterFunc FindAddonRegisterFunc(const std::string& Name);
AddonRegisterFunc FindAddonRegisterFunc(const String& Name);

const JSClassDefinition* FindClassByType(UStruct* Type);
#endif

private:
std::map<const void*, JSClassDefinition*> CDataIdToClassDefinition;
std::map<std::string, JSClassDefinition*> CDataNameToClassDefinition;
std::map<String, JSClassDefinition*> CDataNameToClassDefinition;
pesapi_class_not_found_callback ClassNotFoundCallback = nullptr;
#if USING_IN_UNREAL_ENGINE
std::map<std::string, AddonRegisterFunc> AddonRegisterInfos;
std::map<String, AddonRegisterFunc> AddonRegisterInfos;
std::map<FString, JSClassDefinition*> StructNameToClassDefinition;
#endif
};
Expand Down Expand Up @@ -150,7 +150,7 @@ void JSClassRegister::RegisterClass(const JSClassDefinition& ClassDefinition)
JSClassDefinitionDelete(cd_iter->second);
}
CDataIdToClassDefinition[ClassDefinition.TypeId] = JSClassDefinitionDuplicate(&ClassDefinition);
std::string SN = ClassDefinition.ScriptName;
String SN = ClassDefinition.ScriptName;
CDataNameToClassDefinition[SN] = CDataIdToClassDefinition[ClassDefinition.TypeId];
CDataIdToClassDefinition[ClassDefinition.TypeId]->ScriptName = CDataNameToClassDefinition.find(SN)->first.c_str();
}
Expand All @@ -170,7 +170,7 @@ void JSClassRegister::RegisterClass(const JSClassDefinition& ClassDefinition)

void SetReflectoinInfo(JSFunctionInfo* Methods, const NamedFunctionInfo* MethodInfos)
{
std::map<std::string, std::tuple<int, const NamedFunctionInfo*>> InfoMap;
std::map<String, std::tuple<int, const NamedFunctionInfo*>> InfoMap;
const NamedFunctionInfo* MethodInfo = MethodInfos;
while (MethodInfo->Name)
{
Expand Down Expand Up @@ -232,7 +232,7 @@ const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId)
}
}

const JSClassDefinition* JSClassRegister::FindCppTypeClassByName(const std::string& Name)
const JSClassDefinition* JSClassRegister::FindCppTypeClassByName(const String& Name)
{
auto Iter = CDataNameToClassDefinition.find(Name);
if (Iter == CDataNameToClassDefinition.end())
Expand All @@ -246,12 +246,12 @@ const JSClassDefinition* JSClassRegister::FindCppTypeClassByName(const std::stri
}

#if USING_IN_UNREAL_ENGINE
void JSClassRegister::RegisterAddon(const std::string& Name, AddonRegisterFunc RegisterFunc)
void JSClassRegister::RegisterAddon(const String& Name, AddonRegisterFunc RegisterFunc)
{
AddonRegisterInfos[Name] = RegisterFunc;
}

AddonRegisterFunc JSClassRegister::FindAddonRegisterFunc(const std::string& Name)
AddonRegisterFunc JSClassRegister::FindAddonRegisterFunc(const String& Name)
{
auto Iter = AddonRegisterInfos.find(Name);
if (Iter == AddonRegisterInfos.end())
Expand Down Expand Up @@ -329,7 +329,7 @@ const JSClassDefinition* LoadClassByID(const void* TypeId)
return GetJSClassRegister()->LoadClassByID(TypeId);
}

const JSClassDefinition* FindCppTypeClassByName(const std::string& Name)
const JSClassDefinition* FindCppTypeClassByName(const String& Name)
{
return GetJSClassRegister()->FindCppTypeClassByName(Name);
}
Expand Down Expand Up @@ -378,7 +378,7 @@ void RegisterAddon(const char* Name, AddonRegisterFunc RegisterFunc)
GetJSClassRegister()->RegisterAddon(Name, RegisterFunc);
}

AddonRegisterFunc FindAddonRegisterFunc(const std::string& Name)
AddonRegisterFunc FindAddonRegisterFunc(const String& Name)
{
return GetJSClassRegister()->FindAddonRegisterFunc(Name);
}
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/JsEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void FJsEnv::ReloadModule(FName ModuleName, const FString& JsSource)
GameScript->ReloadModule(ModuleName, JsSource);
}

void FJsEnv::ReloadSource(const FString& Path, const std::string& JsSource)
void FJsEnv::ReloadSource(const FString& Path, const String& JsSource)
{
GameScript->ReloadSource(Path, JsSource);
}
Expand Down
4 changes: 2 additions & 2 deletions unreal/Puerts/Source/JsEnv/Private/JsEnvImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ void FJsEnvImpl::ReloadModule(FName ModuleName, const FString& JsSource)
JsHotReload(ModuleName, JsSource);
}

void FJsEnvImpl::ReloadSource(const FString& Path, const std::string& JsSource)
void FJsEnvImpl::ReloadSource(const FString& Path, const String& JsSource)
{
#ifdef SINGLE_THREAD_VERIFY
ensureMsgf(BoundThreadId == FPlatformTLS::GetCurrentThreadId(), TEXT("Access by illegal thread!"));
Expand Down Expand Up @@ -4462,7 +4462,7 @@ void FJsEnvImpl::FindModule(const v8::FunctionCallbackInfo<v8::Value>& Info)

CHECK_V8_ARGS(EArgString);

std::string Name = *(v8::String::Utf8Value(Isolate, Info[0]));
String Name = *(v8::String::Utf8Value(Isolate, Info[0]));

auto Func = FindAddonRegisterFunc(Name);

Expand Down
4 changes: 2 additions & 2 deletions unreal/Puerts/Source/JsEnv/Private/JsEnvImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class FJsEnvImpl : public IJsEnv, IObjectMapper, public FUObjectArray::FUObjectD

virtual void ReloadModule(FName ModuleName, const FString& JsSource) override;

virtual void ReloadSource(const FString& Path, const std::string& JsSource) override;
virtual void ReloadSource(const FString& Path, const String& JsSource) override;

std::function<void(const FString&)> OnSourceLoadedCallback;

Expand Down Expand Up @@ -372,7 +372,7 @@ class FJsEnvImpl : public IJsEnv, IObjectMapper, public FUObjectArray::FUObjectD

struct ObjectMerger
{
std::map<std::string, std::unique_ptr<FPropertyTranslator>> Fields;
std::map<String, std::unique_ptr<FPropertyTranslator>> Fields;
UStruct* Struct;
FJsEnvImpl* Parent;

Expand Down
5 changes: 3 additions & 2 deletions unreal/Puerts/Source/JsEnv/Private/PesapiAddonLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "pesapi.h"
#include "CoreMinimal.h"
#include "V8Utils.h"
#include "PString.h"

#include <map>
#include <string>
Expand All @@ -19,10 +20,10 @@
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)

static std::map<std::string, std::string> LoadedModules;
static std::map<puerts::String, puerts::String> LoadedModules;

#if PLATFORM_IOS
int PesapiLoadFramework(std::string frameworkName, std::string entryClassName, pesapi_func_ptr* funcPtrArray);
int PesapiLoadFramework(puerts::String frameworkName, puerts::String entryClassName, pesapi_func_ptr* funcPtrArray);
#endif

MSVC_PRAGMA(warning(push))
Expand Down
11 changes: 6 additions & 5 deletions unreal/Puerts/Source/JsEnv/Private/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <sstream>
#include <vector>
#include <cstring>
#include "PString.h"

struct pesapi_env_ref__
{
Expand Down Expand Up @@ -53,7 +54,7 @@ struct pesapi_scope__
}
v8::HandleScope scope;
v8::TryCatch trycatch;
std::string errinfo;
puerts::String errinfo;
};

static_assert(sizeof(pesapi_scope_memory) >= sizeof(pesapi_scope__), "sizeof(pesapi_scope__) > sizeof(pesapi_scope_memory__)");
Expand Down Expand Up @@ -567,7 +568,7 @@ const char* pesapi_get_exception_as_string(pesapi_scope scope, bool with_stack)
std::ostringstream stm;
v8::String::Utf8Value fileName(isolate, message->GetScriptResourceName());
int lineNum = message->GetLineNumber(context).FromJust();
stm << *fileName << ":" << lineNum << ": " << scope->errinfo;
stm << *fileName << ":" << lineNum << ": " << scope->errinfo.c_str();

stm << std::endl;

Expand All @@ -578,7 +579,7 @@ const char* pesapi_get_exception_as_string(pesapi_scope scope, bool with_stack)
v8::String::Utf8Value stackTraceVal(isolate, stackTrace);
stm << std::endl << *stackTraceVal;
}
scope->errinfo = stm.str();
scope->errinfo = stm.str().c_str();
}
return scope->errinfo.c_str();
}
Expand Down Expand Up @@ -943,7 +944,7 @@ void pesapi_define_class(const void* type_id, const void* super_type_id, const c
puerts::JSClassDefinition classDef = JSClassEmptyDefinition;
classDef.TypeId = type_id;
classDef.SuperTypeId = super_type_id;
std::string ScriptNameWithModuleName = GPesapiModuleName == nullptr ? std::string() : GPesapiModuleName;
puerts::String ScriptNameWithModuleName = GPesapiModuleName == nullptr ? puerts::String() : GPesapiModuleName;
if (GPesapiModuleName)
{
ScriptNameWithModuleName += ".";
Expand Down Expand Up @@ -1040,7 +1041,7 @@ void pesapi_class_type_info(const char* proto_magic_id, const void* type_id, con

const void* pesapi_find_type_id(const char* module_name, const char* type_name)
{
std::string fullname = module_name;
puerts::String fullname = module_name;
fullname += ".";
fullname += type_name;
const auto class_def = puerts::FindCppTypeClassByName(fullname);
Expand Down
5 changes: 3 additions & 2 deletions unreal/Puerts/Source/JsEnv/Public/JSClassRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PRAGMA_ENABLE_UNDEFINED_IDENTIFIER_WARNINGS

#include "pesapi.h"
#include "TypeInfo.hpp"
#include "PString.h"

#if USING_IN_UNREAL_ENGINE
static const FAnsiStringView EditorOnlyPropertySuffix = "_EditorOnly";
Expand Down Expand Up @@ -146,14 +147,14 @@ JSENV_API void OnClassNotFound(pesapi_class_not_found_callback Callback);

JSENV_API const JSClassDefinition* LoadClassByID(const void* TypeId);

JSENV_API const JSClassDefinition* FindCppTypeClassByName(const std::string& Name);
JSENV_API const JSClassDefinition* FindCppTypeClassByName(const String& Name);

JSENV_API bool TraceObjectLifecycle(const void* TypeId, pesapi_on_native_object_enter OnEnter, pesapi_on_native_object_exit OnExit);

#if USING_IN_UNREAL_ENGINE
typedef void (*AddonRegisterFunc)(v8::Local<v8::Context> Context, v8::Local<v8::Object> Exports);

AddonRegisterFunc FindAddonRegisterFunc(const std::string& Name);
AddonRegisterFunc FindAddonRegisterFunc(const String& Name);

void RegisterAddon(const char* Name, AddonRegisterFunc RegisterFunc);

Expand Down
5 changes: 3 additions & 2 deletions unreal/Puerts/Source/JsEnv/Public/JsEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "CoreMinimal.h"
#include "JSLogger.h"
#include "JSModuleLoader.h"
#include "PString.h"
#if !defined(ENGINE_INDEPENDENT_JSENV)
#include "ExtensionMethods.h"
#endif
Expand Down Expand Up @@ -44,7 +45,7 @@ class JSENV_API IJsEnv

virtual void ReloadModule(FName ModuleName, const FString& JsSource) = 0;

virtual void ReloadSource(const FString& Path, const std::string& JsSource) = 0;
virtual void ReloadSource(const FString& Path, const String& JsSource) = 0;

virtual void OnSourceLoaded(std::function<void(const FString&)> Callback) = 0;

Expand Down Expand Up @@ -85,7 +86,7 @@ class JSENV_API FJsEnv // : public TSharedFromThis<FJsEnv> // only a wrapper

void ReloadModule(FName ModuleName, const FString& JsSource);

void ReloadSource(const FString& Path, const std::string& JsSource);
void ReloadSource(const FString& Path, const String& JsSource);

void OnSourceLoaded(std::function<void(const FString&)> Callback);

Expand Down
Loading

0 comments on commit 012005d

Please sign in to comment.