Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v8 和 UE 字符串传递默认使用 UTF16 避免编码转换 #1628

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions unreal/Puerts/Source/JsEnv/Private/V8Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <V8Utils.h>

v8::Local<v8::String> 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<v8::Value> Value)
{
#if WITH_QUICKJS
return UTF8_TO_TCHAR(*(v8::String::Utf8Value(Isolate, Value)));
#else
return UTF16_TO_TCHAR(*(v8::String::Value(Isolate, Value)));
#endif
}
14 changes: 4 additions & 10 deletions unreal/Puerts/Source/JsEnv/Public/V8Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum ArgType
EArgObject
};

class FV8Utils
class JSENV_API FV8Utils
{
public:
FORCEINLINE static void ThrowException(v8::Isolate* Isolate, const FString& Message)
Expand Down Expand Up @@ -96,18 +96,15 @@ class FV8Utils

FORCEINLINE static v8::Local<v8::String> 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<v8::String> InternalString(v8::Isolate* Isolate, const char* String)
{
return v8::String::NewFromUtf8(Isolate, String, v8::NewStringType::kNormal).ToLocalChecked();
}

FORCEINLINE static FString ToFString(v8::Isolate* Isolate, v8::Local<v8::Value> Value)
{
return UTF8_TO_TCHAR(*(v8::String::Utf8Value(Isolate, Value)));
}
static FString ToFString(v8::Isolate* Isolate, v8::Local<v8::Value> Value);

FORCEINLINE static FName ToFName(v8::Isolate* Isolate, v8::Local<v8::Value> Value)
{
Expand Down Expand Up @@ -146,10 +143,7 @@ class FV8Utils
return ToV8String(Isolate, String.ToString());
}

FORCEINLINE static v8::Local<v8::String> ToV8String(v8::Isolate* Isolate, const TCHAR* String)
{
return v8::String::NewFromUtf8(Isolate, TCHAR_TO_UTF8(String), v8::NewStringType::kNormal).ToLocalChecked();
}
static v8::Local<v8::String> ToV8String(v8::Isolate* Isolate, const TCHAR* String);

FORCEINLINE static v8::Local<v8::String> ToV8String(v8::Isolate* Isolate, const char* String)
{
Expand Down