From def65ffd3637207fb755afb2880e3ef81c8a5405 Mon Sep 17 00:00:00 2001 From: johnche Date: Fri, 24 May 2024 17:07:05 +0800 Subject: [PATCH] =?UTF-8?q?[unreal]unity=E7=9A=84il2cpp=20pinvoke=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=9C=AC=E8=BA=AB=E5=B0=B1=E6=9C=89=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E6=88=AA=E6=96=AD=E7=9A=84bug=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E8=AF=A5bug=E4=BC=9A=E5=A2=9E=E5=8A=A0gc=EF=BC=8C?= =?UTF-8?q?=E5=85=88=E5=9B=9E=E9=80=80=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=88=AA?= =?UTF-8?q?=E6=96=AD=E7=9A=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/upm/Runtime/Src/Default/JsEnv.cs | 2 +- .../Runtime/Src/Default/Native/PuertsDLL.cs | 37 +++++----- unity/native_src/Inc/IPuertsPlugin.h | 6 +- unity/native_src/Src/JSFunction.cpp | 2 +- unity/native_src/Src/PluginImpl.cpp | 18 ++--- unity/native_src/Src/Puerts.cpp | 14 ++-- unity/native_src/Src/PuertsMultBackend.cpp | 14 ++-- .../test/Src/Cases/CrossLang/CrossLangTest.cs | 69 ------------------- 8 files changed, 46 insertions(+), 116 deletions(-) diff --git a/unity/Assets/core/upm/Runtime/Src/Default/JsEnv.cs b/unity/Assets/core/upm/Runtime/Src/Default/JsEnv.cs index 4036385705..05abfafa2f 100644 --- a/unity/Assets/core/upm/Runtime/Src/Default/JsEnv.cs +++ b/unity/Assets/core/upm/Runtime/Src/Default/JsEnv.cs @@ -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) { diff --git a/unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs b/unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs index 4cc611fb62..685200e600 100644 --- a/unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs +++ b/unity/Assets/core/upm/Runtime/Src/Default/Native/PuertsDLL.cs @@ -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) @@ -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 } } @@ -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) { @@ -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) @@ -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 @@ -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) @@ -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'); } } #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) @@ -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); diff --git a/unity/native_src/Inc/IPuertsPlugin.h b/unity/native_src/Inc/IPuertsPlugin.h index d71fdf8c7f..f369a5c7ad 100644 --- a/unity/native_src/Inc/IPuertsPlugin.h +++ b/unity/native_src/Inc/IPuertsPlugin.h @@ -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; @@ -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; @@ -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; diff --git a/unity/native_src/Src/JSFunction.cpp b/unity/native_src/Src/JSFunction.cpp index 2d73b58dbf..17a918259e 100644 --- a/unity/native_src/Src/JSFunction.cpp +++ b/unity/native_src/Src/JSFunction.cpp @@ -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: diff --git a/unity/native_src/Src/PluginImpl.cpp b/unity/native_src/Src/PluginImpl.cpp index 7930182750..556520f59f 100644 --- a/unity/native_src/Src/PluginImpl.cpp +++ b/unity/native_src/Src/PluginImpl.cpp @@ -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; @@ -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; @@ -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; @@ -448,7 +448,7 @@ 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; @@ -456,7 +456,7 @@ void V8Plugin::SetStringToOutValue(void* pValue, const char *Str, int size) { 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)); } } @@ -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& Info = *(const v8::FunctionCallbackInfo*)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) @@ -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)); } diff --git a/unity/native_src/Src/Puerts.cpp b/unity/native_src/Src/Puerts.cpp index d7e1aeedda..c8ba73c490 100644 --- a/unity/native_src/Src/Puerts.cpp +++ b/unity/native_src/Src/Puerts.cpp @@ -9,7 +9,7 @@ #include "V8Utils.h" #include "Log.h" -#define API_LEVEL 35 +#define API_LEVEL 34 using puerts::JSEngine; using puerts::FValue; @@ -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)); } } @@ -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& Info, const char* String, int size) +V8_EXPORT void ReturnString(v8::Isolate* Isolate, const v8::FunctionCallbackInfo& 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& Info, int64_t BigInt) @@ -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)); } diff --git a/unity/native_src/Src/PuertsMultBackend.cpp b/unity/native_src/Src/PuertsMultBackend.cpp index 5411098d09..12bd55151d 100644 --- a/unity/native_src/Src/PuertsMultBackend.cpp +++ b/unity/native_src/Src/PuertsMultBackend.cpp @@ -33,7 +33,7 @@ #endif #endif // _WIN32 -#define API_LEVEL 35 +#define API_LEVEL 34 LogCallback GLogCallback = nullptr; LogCallback GLogWarningCallback = nullptr; @@ -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) @@ -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) @@ -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) diff --git a/unity/test/Src/Cases/CrossLang/CrossLangTest.cs b/unity/test/Src/Cases/CrossLang/CrossLangTest.cs index f544c564c0..43e93bfa81 100644 --- a/unity/test/Src/Cases/CrossLang/CrossLangTest.cs +++ b/unity/test/Src/Cases/CrossLang/CrossLangTest.cs @@ -217,23 +217,6 @@ public string StringTestPipeLine(string initialValue, out string outArg, Func cb) - { - cb(StringContainNull); - } public string stringTestField = null; protected string _stringTestProp = null; @@ -652,58 +635,6 @@ public void StringInstanceTest() jsEnv.Tick(); } [Test] - public void StringContainNullTest() - { - var jsEnv = UnitTestEnv.GetEnv(); - int len = jsEnv.Eval(@" - (function() { - const TestHelper = CS.Puerts.UnitTest.TestHelper; - const assertAndPrint = TestHelper.AssertAndPrint.bind(TestHelper); - - const testHelper = TestHelper.GetInstance(); - return testHelper.GetStringContainNull().length; - })() - "); - Assert.AreEqual(len, TestHelper.GetInstance().GetStringContainNull().Length); - - len = jsEnv.Eval(@" - (function() { - const TestHelper = CS.Puerts.UnitTest.TestHelper; - const assertAndPrint = TestHelper.AssertAndPrint.bind(TestHelper); - - const testHelper = TestHelper.GetInstance(); - return testHelper.StringContainNull.length; - })() - "); - Assert.AreEqual(len, TestHelper.GetInstance().StringContainNull.Length); - - len = jsEnv.Eval(@" - (function() { - const TestHelper = CS.Puerts.UnitTest.TestHelper; - const assertAndPrint = TestHelper.AssertAndPrint.bind(TestHelper); - - const testHelper = TestHelper.GetInstance(); - let o = puer.$ref(); - testHelper.OutStringContainNull(o); - return puer.$unref(o).length - })() - "); - Assert.AreEqual(len, TestHelper.GetInstance().StringContainNull.Length); - - len = jsEnv.Eval(@" - (function() { - const TestHelper = CS.Puerts.UnitTest.TestHelper; - const assertAndPrint = TestHelper.AssertAndPrint.bind(TestHelper); - - const testHelper = TestHelper.GetInstance(); - let str - testHelper.CallbackPassStringContainNull(s => str = s); - return str.length - })() - "); - Assert.AreEqual(len, TestHelper.GetInstance().StringContainNull.Length); - } - [Test] public void BoolInstanceTest() { var jsEnv = UnitTestEnv.GetEnv();