Skip to content

Commit

Permalink
[unreal]unity的il2cpp pinvoke实现本身就有字符串截断的bug,修正该bug会增加gc,先回退字符串截断的修改
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed May 24, 2024
1 parent af2463b commit def65ff
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 116 deletions.
2 changes: 1 addition & 1 deletion unity/Assets/core/upm/Runtime/Src/Default/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public JsEnv(ILoader loader, IntPtr externalRuntime, IntPtr externalContext)

public JsEnv(ILoader loader, int debugPort, BackendType backend, IntPtr externalRuntime, IntPtr externalContext)
{
const int libVersionExpect = 35;
const int libVersionExpect = 34;
int libVersion = PuertsDLL.GetApiLevel();
if (libVersion != libVersionExpect)
{
Expand Down
37 changes: 18 additions & 19 deletions unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ public static bool RegisterProperty(IntPtr isolate, int classID, string name, bo

#if PUERTS_GENERAL && !PUERTS_GENERAL_OSX
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ReturnString")]
public static extern void __ReturnString(IntPtr isolate, IntPtr info, byte[] str, int len);
public static extern void __ReturnString(IntPtr isolate, IntPtr info, byte[] str);
#else
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ReturnString")]
public static extern void __ReturnString(IntPtr isolate, IntPtr info, string str, int len);
public static extern void __ReturnString(IntPtr isolate, IntPtr info, string str);
#endif

public static void ReturnString(IntPtr isolate, IntPtr info, string str)
Expand All @@ -313,10 +313,9 @@ public static void ReturnString(IntPtr isolate, IntPtr info, string str)
else
{
#if PUERTS_GENERAL && !PUERTS_GENERAL_OSX
__ReturnString(isolate, info, Encoding.UTF8.GetBytes(str), Encoding.UTF8.GetByteCount(str));
__ReturnString(isolate, info, Encoding.UTF8.GetBytes(str + '\0'));
#else
//目前为止Unity下pinvoke string转换默认是UTF8,直接声明为string有可能能剩下一个byte[]的分配和gc
__ReturnString(isolate, info, str, Encoding.UTF8.GetByteCount(str));
__ReturnString(isolate, info, str);
#endif
}
}
Expand Down Expand Up @@ -413,7 +412,7 @@ public static long GetBigIntFromValueChecked(IntPtr isolate, IntPtr value, bool

#if PUERTS_GENERAL && !PUERTS_GENERAL_OSX
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetStringToOutValue(IntPtr isolate, IntPtr value, byte[] str, int size);
public static extern void SetStringToOutValue(IntPtr isolate, IntPtr value, byte[] str);

public static void SetStringToOutValue(IntPtr isolate, IntPtr value, string str)
{
Expand All @@ -423,12 +422,12 @@ public static void SetStringToOutValue(IntPtr isolate, IntPtr value, string str)
}
else
{
SetStringToOutValue(isolate, value, Encoding.UTF8.GetBytes(str), Encoding.UTF8.GetByteCount(str));
SetStringToOutValue(isolate, value, Encoding.UTF8.GetBytes(str + '\0'));
}
}
#else
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SetStringToOutValue")]
protected static extern void __SetStringToOutValue(IntPtr isolate, IntPtr value, string str, int size);
protected static extern void __SetStringToOutValue(IntPtr isolate, IntPtr value, string str);
public static void SetStringToOutValue(IntPtr isolate, IntPtr value, string str)
{
if (str == null)
Expand All @@ -437,7 +436,7 @@ public static void SetStringToOutValue(IntPtr isolate, IntPtr value, string str)
}
else
{
__SetStringToOutValue(isolate, value, str, Encoding.UTF8.GetByteCount(str));
__SetStringToOutValue(isolate, value, str);
}
}
#endif
Expand Down Expand Up @@ -479,13 +478,12 @@ public static void ThrowException(IntPtr isolate, string message)
public static extern void PushBooleanForJSFunction(IntPtr function, bool b);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void PushBigIntForJSFunction(IntPtr function, long l);



public static extern void PushBigIntForJSFunction(IntPtr function, long l);

#if PUERTS_GENERAL && !PUERTS_GENERAL_OSX
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PushStringForJSFunction")]
public static extern void __PushStringForJSFunction(IntPtr function, byte[] str, int size);
public static extern void __PushStringForJSFunction(IntPtr function, byte[] str);

public static void PushStringForJSFunction(IntPtr function, string str)
{
if (str == null)
Expand All @@ -494,12 +492,13 @@ public static void PushStringForJSFunction(IntPtr function, string str)
}
else
{
__PushStringForJSFunction(function, Encoding.UTF8.GetBytes(str), Encoding.UTF8.GetByteCount(str));
__PushStringForJSFunction(function, str + '\0');

Check failure on line 495 in unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs

View workflow job for this annotation

GitHub Actions / unittest-win-dotnet

Argument 2: cannot convert from 'string' to 'byte[]'

Check failure on line 495 in unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs

View workflow job for this annotation

GitHub Actions / unittest-win-dotnet

Argument 2: cannot convert from 'string' to 'byte[]'

Check failure on line 495 in unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs

View workflow job for this annotation

GitHub Actions / unittest-osx-dotnet

Argument 2: cannot convert from 'string' to 'byte[]'

Check failure on line 495 in unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs

View workflow job for this annotation

GitHub Actions / unittest-osx-dotnet

Argument 2: cannot convert from 'string' to 'byte[]'

Check failure on line 495 in unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs

View workflow job for this annotation

GitHub Actions / unittest-linux-dotnet

Argument 2: cannot convert from 'string' to 'byte[]'

Check failure on line 495 in unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs

View workflow job for this annotation

GitHub Actions / unittest-linux-dotnet

Argument 2: cannot convert from 'string' to 'byte[]'
}
}
#else
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PushStringForJSFunction")]
public static extern void __PushStringForJSFunction(IntPtr function, string str, int size);
public static extern void __PushStringForJSFunction(IntPtr function, string str);

public static void PushStringForJSFunction(IntPtr function, string str)
{
if (str == null)
Expand All @@ -508,11 +507,11 @@ public static void PushStringForJSFunction(IntPtr function, string str)
}
else
{
__PushStringForJSFunction(function, str, Encoding.UTF8.GetByteCount(str));
__PushStringForJSFunction(function, str);
}
}
}
#endif


[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void PushNumberForJSFunction(IntPtr function, double d);

Expand Down
6 changes: 3 additions & 3 deletions unity/native_src/Inc/IPuertsPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class IPuertsPlugin

virtual const char *GetStringFromValue(void* Value, int *Length, int IsOut) = 0;

virtual void SetStringToOutValue(void* Value, const char *Str, int size) = 0;
virtual void SetStringToOutValue(void* Value, const char *Str) = 0;

virtual int GetBooleanFromValue(void* Value, int IsOut) = 0;

Expand Down Expand Up @@ -108,7 +108,7 @@ class IPuertsPlugin

virtual void ReturnNumber(const void* Info, double Number) = 0;

virtual void ReturnString(const void* Info, const char* String, int size) = 0;
virtual void ReturnString(const void* Info, const char* String) = 0;

virtual void ReturnBigInt(const void* Info, int64_t BigInt) = 0;

Expand Down Expand Up @@ -139,7 +139,7 @@ class IPuertsPlugin

virtual void PushArrayBufferForJSFunction(void* Function, unsigned char * Bytes, int Length) = 0;

virtual void PushStringForJSFunction(void* Function, const char* S, int size) = 0;
virtual void PushStringForJSFunction(void* Function, const char* S) = 0;

virtual void PushNumberForJSFunction(void* Function, double D) = 0;

Expand Down
2 changes: 1 addition & 1 deletion unity/native_src/Src/JSFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace PUERTS_NAMESPACE
case puerts::Date:
return v8::Date::New(Context, Value.Number).ToLocalChecked();
case puerts::String:
return v8::String::NewFromUtf8(Isolate, Value.Str.c_str(), v8::NewStringType::kNormal, Value.Str.size()).ToLocalChecked();
return FV8Utils::V8String(Isolate, Value.Str.c_str());
case puerts::NativeObject:
return Value.Persistent.Get(Isolate);
case puerts::Function:
Expand Down
18 changes: 9 additions & 9 deletions unity/native_src/Src/PluginImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class V8Plugin : public puerts::IPuertsPlugin

virtual const char *GetStringFromValue(void* Value, int *Length, int IsOut) override;

virtual void SetStringToOutValue(void* Value, const char *Str, int size) override;
virtual void SetStringToOutValue(void* Value, const char *Str) override;

virtual int GetBooleanFromValue(void* Value, int IsOut) override;

Expand Down Expand Up @@ -117,7 +117,7 @@ class V8Plugin : public puerts::IPuertsPlugin

virtual void ReturnNumber(const void* Info, double Number) override;

virtual void ReturnString(const void* Info, const char* String, int size) override;
virtual void ReturnString(const void* Info, const char* String) override;

virtual void ReturnBigInt(const void* Info, int64_t BigInt) override;

Expand Down Expand Up @@ -148,7 +148,7 @@ class V8Plugin : public puerts::IPuertsPlugin

virtual void PushArrayBufferForJSFunction(void* Function, unsigned char * Bytes, int Length) override;

virtual void PushStringForJSFunction(void* Function, const char* S, int size) override;
virtual void PushStringForJSFunction(void* Function, const char* S) override;

virtual void PushNumberForJSFunction(void* Function, double D) override;

Expand Down Expand Up @@ -448,15 +448,15 @@ const char *V8Plugin::GetStringFromValue(void* pValue, int *Length, int IsOut)
}
}

void V8Plugin::SetStringToOutValue(void* pValue, const char *Str, int size)
void V8Plugin::SetStringToOutValue(void* pValue, const char *Str)
{
v8::Isolate* Isolate = jsEngine.MainIsolate;
const v8::Value *Value = (const v8::Value *)pValue;
if (Value->IsObject())
{
auto Context = Isolate->GetCurrentContext();
auto Outer = Value->ToObject(Context).ToLocalChecked();
auto ReturnVal = Outer->Set(Context, 0, v8::String::NewFromUtf8(Isolate, Str, v8::NewStringType::kNormal, size).ToLocalChecked());
auto ReturnVal = Outer->Set(Context, 0, FV8Utils::V8String(Isolate, Str));
}
}

Expand Down Expand Up @@ -742,11 +742,11 @@ void V8Plugin::ReturnNumber(const void* pInfo, double Number)
Info.GetReturnValue().Set(Number);
}

void V8Plugin::ReturnString(const void* pInfo, const char* String, int size)
void V8Plugin::ReturnString(const void* pInfo, const char* String)
{
v8::Isolate* Isolate = jsEngine.MainIsolate;
const v8::FunctionCallbackInfo<v8::Value>& Info = *(const v8::FunctionCallbackInfo<v8::Value>*)pInfo;
Info.GetReturnValue().Set(v8::String::NewFromUtf8(Isolate, String, v8::NewStringType::kNormal, size).ToLocalChecked());
Info.GetReturnValue().Set(PUERTS_NAMESPACE::FV8Utils::V8String(Isolate, String));
}

void V8Plugin::ReturnBigInt(const void* pInfo, int64_t BigInt)
Expand Down Expand Up @@ -868,12 +868,12 @@ void V8Plugin::PushArrayBufferForJSFunction(void* pFunction, unsigned char * Byt
Function->Arguments.push_back(std::move(Value));
}

void V8Plugin::PushStringForJSFunction(void* pFunction, const char* S, int size)
void V8Plugin::PushStringForJSFunction(void* pFunction, const char* S)
{
PUERTS_NAMESPACE::JSFunction *Function = (PUERTS_NAMESPACE::JSFunction *)pFunction;
FValue Value;
Value.Type = puerts::String;
Value.Str = std::string(S, size);
Value.Str = S;
Function->Arguments.push_back(std::move(Value));
}

Expand Down
14 changes: 7 additions & 7 deletions unity/native_src/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "V8Utils.h"
#include "Log.h"

#define API_LEVEL 35
#define API_LEVEL 34

using puerts::JSEngine;
using puerts::FValue;
Expand Down Expand Up @@ -294,13 +294,13 @@ V8_EXPORT const char *GetStringFromValue(v8::Isolate* Isolate, v8::Value *Value,
}
}

V8_EXPORT void SetStringToOutValue(v8::Isolate* Isolate, v8::Value *Value, const char *Str, int size)
V8_EXPORT void SetStringToOutValue(v8::Isolate* Isolate, v8::Value *Value, const char *Str)
{
if (Value->IsObject())
{
auto Context = Isolate->GetCurrentContext();
auto Outer = Value->ToObject(Context).ToLocalChecked();
auto ReturnVal = Outer->Set(Context, 0, v8::String::NewFromUtf8(Isolate, Str, v8::NewStringType::kNormal, size).ToLocalChecked());
auto ReturnVal = Outer->Set(Context, 0, FV8Utils::V8String(Isolate, Str));
}
}

Expand Down Expand Up @@ -559,9 +559,9 @@ V8_EXPORT void ReturnNumber(v8::Isolate* Isolate, const v8::FunctionCallbackInfo
Info.GetReturnValue().Set(Number);
}

V8_EXPORT void ReturnString(v8::Isolate* Isolate, const v8::FunctionCallbackInfo<v8::Value>& Info, const char* String, int size)
V8_EXPORT void ReturnString(v8::Isolate* Isolate, const v8::FunctionCallbackInfo<v8::Value>& Info, const char* String)
{
Info.GetReturnValue().Set(v8::String::NewFromUtf8(Isolate, String, v8::NewStringType::kNormal, size).ToLocalChecked());
Info.GetReturnValue().Set(FV8Utils::V8String(Isolate, String));
}

V8_EXPORT void ReturnBigInt(v8::Isolate* Isolate, const v8::FunctionCallbackInfo<v8::Value>& Info, int64_t BigInt)
Expand Down Expand Up @@ -664,11 +664,11 @@ V8_EXPORT void PushArrayBufferForJSFunction(JSFunction *Function, unsigned char
Function->Arguments.push_back(std::move(Value));
}

V8_EXPORT void PushStringForJSFunction(JSFunction *Function, const char* S, int size)
V8_EXPORT void PushStringForJSFunction(JSFunction *Function, const char* S)
{
FValue Value;
Value.Type = puerts::String;
Value.Str = std::string(S, size);
Value.Str = S;
Function->Arguments.push_back(std::move(Value));
}

Expand Down
14 changes: 7 additions & 7 deletions unity/native_src/Src/PuertsMultBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#endif
#endif // _WIN32

#define API_LEVEL 35
#define API_LEVEL 34

LogCallback GLogCallback = nullptr;
LogCallback GLogWarningCallback = nullptr;
Expand Down Expand Up @@ -217,9 +217,9 @@ PUERTS_EXPORT const char *GetStringFromValue(puerts::IPuertsPlugin* plugin, void
return plugin->GetStringFromValue(Value, Length, IsOut);
}

PUERTS_EXPORT void SetStringToOutValue(puerts::IPuertsPlugin* plugin, void* Value, const char *Str, int size)
PUERTS_EXPORT void SetStringToOutValue(puerts::IPuertsPlugin* plugin, void* Value, const char *Str)
{
plugin->SetStringToOutValue(Value, Str, size);
plugin->SetStringToOutValue(Value, Str);
}

PUERTS_EXPORT int GetBooleanFromValue(puerts::IPuertsPlugin* plugin, void* Value, int IsOut)
Expand Down Expand Up @@ -316,9 +316,9 @@ PUERTS_EXPORT void ReturnNumber(puerts::IPuertsPlugin* plugin, const void* Info,
plugin->ReturnNumber(Info, Number);
}

PUERTS_EXPORT void ReturnString(puerts::IPuertsPlugin* plugin, const void* Info, const char* String, int size)
PUERTS_EXPORT void ReturnString(puerts::IPuertsPlugin* plugin, const void* Info, const char* String)
{
plugin->ReturnString(Info, String, size);
plugin->ReturnString(Info, String);
}

PUERTS_EXPORT void ReturnBigInt(puerts::IPuertsPlugin* plugin, const void* Info, int64_t BigInt)
Expand Down Expand Up @@ -390,9 +390,9 @@ PUERTS_EXPORT void PushArrayBufferForJSFunction(puerts::PuertsPluginStore* Funct
Function->PuertsPlugin->PushArrayBufferForJSFunction(Function, Bytes, Length);
}

PUERTS_EXPORT void PushStringForJSFunction(puerts::PuertsPluginStore* Function, const char* S, int size)
PUERTS_EXPORT void PushStringForJSFunction(puerts::PuertsPluginStore* Function, const char* S)
{
Function->PuertsPlugin->PushStringForJSFunction(Function, S, size);
Function->PuertsPlugin->PushStringForJSFunction(Function, S);
}

PUERTS_EXPORT void PushNumberForJSFunction(puerts::PuertsPluginStore* Function, double D)
Expand Down
Loading

0 comments on commit def65ff

Please sign in to comment.