Skip to content

Commit

Permalink
[unreal]v8 和 UE 字符串传递默认使用 UTF16 避免编码转换 (#1628)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BlurryLight authored Jan 8, 2024
1 parent 3cab273 commit 1b4768d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
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

0 comments on commit 1b4768d

Please sign in to comment.