From 1b4768dd360efdd89b60233a5cb60b68920d15dd Mon Sep 17 00:00:00 2001 From: BlurryLight <14940338+BlurryLight@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:31:30 +0800 Subject: [PATCH] =?UTF-8?q?[unreal]v8=20=E5=92=8C=20UE=20=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E4=BC=A0=E9=80=92=E9=BB=98=E8=AE=A4=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20UTF16=20=E9=81=BF=E5=85=8D=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=20(#1628)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * v8 <-> UE use UTF16-LE by default * clang-format remove empty space * quick js doesn't have native utf16 support * 实现不能放到Public 头文件 V8Utils.h里 * export symbols because FV8Utils is no more header only --- .../Puerts/Source/JsEnv/Private/V8Utils.cpp | 19 +++++++++++++++++++ unreal/Puerts/Source/JsEnv/Public/V8Utils.h | 14 ++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 unreal/Puerts/Source/JsEnv/Private/V8Utils.cpp diff --git a/unreal/Puerts/Source/JsEnv/Private/V8Utils.cpp b/unreal/Puerts/Source/JsEnv/Private/V8Utils.cpp new file mode 100644 index 0000000000..dd5f15eae3 --- /dev/null +++ b/unreal/Puerts/Source/JsEnv/Private/V8Utils.cpp @@ -0,0 +1,19 @@ +#include + +v8::Local puerts::FV8Utils::ToV8String(v8::Isolate* Isolate, const TCHAR* String) +{ +#if WITH_QUICKJS + return v8::String::NewFromUtf8(Isolate, TCHAR_TO_UTF8(String), v8::NewStringType::kNormal).ToLocalChecked(); +#else + return v8::String::NewFromTwoByte(Isolate, TCHAR_TO_UTF16(String), v8::NewStringType::kNormal).ToLocalChecked(); +#endif +} + +FString puerts::FV8Utils::ToFString(v8::Isolate* Isolate, v8::Local Value) +{ +#if WITH_QUICKJS + return UTF8_TO_TCHAR(*(v8::String::Utf8Value(Isolate, Value))); +#else + return UTF16_TO_TCHAR(*(v8::String::Value(Isolate, Value))); +#endif +} \ No newline at end of file diff --git a/unreal/Puerts/Source/JsEnv/Public/V8Utils.h b/unreal/Puerts/Source/JsEnv/Public/V8Utils.h index 6289b6c6c2..5bf0efab27 100644 --- a/unreal/Puerts/Source/JsEnv/Public/V8Utils.h +++ b/unreal/Puerts/Source/JsEnv/Public/V8Utils.h @@ -34,7 +34,7 @@ enum ArgType EArgObject }; -class FV8Utils +class JSENV_API FV8Utils { public: FORCEINLINE static void ThrowException(v8::Isolate* Isolate, const FString& Message) @@ -96,7 +96,7 @@ class FV8Utils FORCEINLINE static v8::Local InternalString(v8::Isolate* Isolate, const FString& String) { - return v8::String::NewFromUtf8(Isolate, TCHAR_TO_UTF8(*String), v8::NewStringType::kNormal).ToLocalChecked(); + return ToV8String(Isolate, String); } FORCEINLINE static v8::Local InternalString(v8::Isolate* Isolate, const char* String) @@ -104,10 +104,7 @@ class FV8Utils return v8::String::NewFromUtf8(Isolate, String, v8::NewStringType::kNormal).ToLocalChecked(); } - FORCEINLINE static FString ToFString(v8::Isolate* Isolate, v8::Local Value) - { - return UTF8_TO_TCHAR(*(v8::String::Utf8Value(Isolate, Value))); - } + static FString ToFString(v8::Isolate* Isolate, v8::Local Value); FORCEINLINE static FName ToFName(v8::Isolate* Isolate, v8::Local Value) { @@ -146,10 +143,7 @@ class FV8Utils return ToV8String(Isolate, String.ToString()); } - FORCEINLINE static v8::Local ToV8String(v8::Isolate* Isolate, const TCHAR* String) - { - return v8::String::NewFromUtf8(Isolate, TCHAR_TO_UTF8(String), v8::NewStringType::kNormal).ToLocalChecked(); - } + static v8::Local ToV8String(v8::Isolate* Isolate, const TCHAR* String); FORCEINLINE static v8::Local ToV8String(v8::Isolate* Isolate, const char* String) {