Skip to content

Commit

Permalink
解决long参数不传bigint会crash的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Sep 13, 2020
1 parent 0ce7229 commit f9a060d
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 8 deletions.
4 changes: 2 additions & 2 deletions unity/Assets/Puerts/Src/ArgumentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void SetByRefValue(uint val)

public long GetInt64(bool isByRef)
{
return PuertsDLL.GetBigIntFromValue(isolate, value, isByRef);
return PuertsDLL.GetBigIntFromValueChecked(isolate, value, isByRef);
}

public void SetByRefValue(long val)
Expand All @@ -133,7 +133,7 @@ public void SetByRefValue(long val)

public ulong GetUInt64(bool isByRef)
{
return (ulong)PuertsDLL.GetBigIntFromValue(isolate, value, isByRef);
return (ulong)PuertsDLL.GetBigIntFromValueChecked(isolate, value, isByRef);
}

public void SetByRefValue(ulong val)
Expand Down
2 changes: 1 addition & 1 deletion unity/Assets/Puerts/Src/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public JsEnv() : this(new DefaultLoader(), -1)

public JsEnv(ILoader loader, int debugPort = -1)
{
const int libVersionExpect = 3;
const int libVersionExpect = 5;
int libVersion = PuertsDLL.GetLibVersion();
if (libVersion != libVersionExpect)
{
Expand Down
4 changes: 2 additions & 2 deletions unity/Assets/Puerts/Src/NativeValueApiGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class GetValueFromResultImpl : IGetValueFromJs
{
public long GetBigInt(IntPtr isolate, IntPtr holder, bool isByRef)
{
return PuertsDLL.GetBigIntFromResult(holder);
return PuertsDLL.GetBigIntFromResultCheck(holder);
}

public bool GetBoolean(IntPtr isolate, IntPtr holder, bool isByRef)
Expand Down Expand Up @@ -144,7 +144,7 @@ public class GetValueFromArgumentImpl : IGetValueFromJs
{
public long GetBigInt(IntPtr isolate, IntPtr holder, bool isByRef)
{
return PuertsDLL.GetBigIntFromValue(isolate, holder, isByRef);
return PuertsDLL.GetBigIntFromValueChecked(isolate, holder, isByRef);
}

public bool GetBoolean(IntPtr isolate, IntPtr holder, bool isByRef)
Expand Down
24 changes: 24 additions & 0 deletions unity/Assets/Puerts/Src/PuertsDLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,21 @@ public static string GetStringFromValue(IntPtr isolate, IntPtr value, bool isByR
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern bool GetBooleanFromValue(IntPtr isolate, IntPtr value, bool isByRef);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern bool ValueIsBigInt(IntPtr isolate, IntPtr value, bool isByRef);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern long GetBigIntFromValue(IntPtr isolate, IntPtr value, bool isByRef);

public static long GetBigIntFromValueChecked(IntPtr isolate, IntPtr value, bool isByRef)
{
if (!ValueIsBigInt(isolate, value, isByRef))
{
throw new InvalidOperationException("expect a bigint");
}
return GetBigIntFromValue(isolate, value, isByRef);
}

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetObjectFromValue(IntPtr isolate, IntPtr value, bool isByRef);

Expand Down Expand Up @@ -301,9 +313,21 @@ public static string GetStringFromResult(IntPtr resultInfo)
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern bool GetBooleanFromResult(IntPtr resultInfo);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern bool ResultIsBigInt(IntPtr resultInfo);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern long GetBigIntFromResult(IntPtr resultInfo);

public static long GetBigIntFromResultCheck(IntPtr resultInfo)
{
if (!ResultIsBigInt(resultInfo))
{
throw new InvalidOperationException("expect a bigint");
}
return GetBigIntFromResult(resultInfo);
}

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetObjectFromResult(IntPtr resultInfo);

Expand Down
4 changes: 2 additions & 2 deletions unity/Assets/Puerts/Src/TypeRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ bool FastArraySet(IntPtr isolate, IntPtr info, IntPtr self, object obj, uint ind
else if (type == typeof(long[]) && jsType == JsValueType.BigInt)
{
long[] array = obj as long[];
array[index] = PuertsDLL.GetBigIntFromValue(isolate, value, false);
array[index] = PuertsDLL.GetBigIntFromValueChecked(isolate, value, false);
}
else if (type == typeof(ulong[]) && jsType == JsValueType.BigInt)
{
ulong[] array = obj as ulong[];
array[index] = (ulong)PuertsDLL.GetBigIntFromValue(isolate, value, false);
array[index] = (ulong)PuertsDLL.GetBigIntFromValueChecked(isolate, value, false);
}
else if (type == typeof(sbyte[]) && jsType == JsValueType.Number)
{
Expand Down
5 changes: 5 additions & 0 deletions unity/general/Src/UnitTest/TestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public int Add(int a, int b)
public bool IsStringNull(string str)
{
return str == null;
}

public long Long(long l)
{
return l;
}
}

Expand Down
33 changes: 33 additions & 0 deletions unity/general/Src/UnitTest/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,39 @@ public void Array()

Assert.AreEqual("240hellojohntruefalsetruefalse480", ret);
}

[Test]
public void Long()
{
Assert.Catch(() => {
using (var jsEnv1 = new JsEnv(new TxtLoader()))
{
jsEnv1.Eval(@"
const CS = require('csharp');
let obj = new CS.Puerts.UnitTest.DerivedClass();
obj.Long(1);
");
}
});

var jsEnv = new JsEnv(new TxtLoader());
var ret = jsEnv.Eval<long>(@"
const CS = require('csharp');
let obj = new CS.Puerts.UnitTest.DerivedClass();
obj.Long(1n);
");
Assert.AreEqual((long)1, ret);

Assert.Catch(() =>
{
using (var jsEnv2 = new JsEnv(new TxtLoader()))
{
jsEnv2.Eval<long>("1");
}
});

jsEnv.Dispose();
}
}
}

30 changes: 29 additions & 1 deletion unity/native_src/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C" {

V8_EXPORT int GetLibVersion()
{
return 3;
return 5;
}

V8_EXPORT v8::Isolate *CreateJSEngine()
Expand Down Expand Up @@ -252,6 +252,22 @@ V8_EXPORT void SetBooleanToOutValue(v8::Isolate* Isolate, v8::Value *Value, int
}
}

V8_EXPORT int ValueIsBigInt(v8::Isolate* Isolate, v8::Value *Value, int IsOut)
{
if (IsOut)
{
auto Context = Isolate->GetCurrentContext();
auto Outer = Value->ToObject(Context).ToLocalChecked();
auto Realvalue = Outer->Get(Context, FV8Utils::V8String(Isolate, "value")).ToLocalChecked();
return ValueIsBigInt(Isolate, *Realvalue, false);
}
else
{
auto Context = Isolate->GetCurrentContext();
return Value->IsBigInt() ? 1 : 0;
}
}

V8_EXPORT int64_t GetBigIntFromValue(v8::Isolate* Isolate, v8::Value *Value, int IsOut)
{
if (IsOut)
Expand Down Expand Up @@ -635,6 +651,18 @@ V8_EXPORT int GetBooleanFromResult(FResultInfo *ResultInfo)
return Result->BooleanValue(Isolate) ? 1 : 0;
}

V8_EXPORT int ResultIsBigInt(FResultInfo *ResultInfo)
{
v8::Isolate* Isolate = ResultInfo->Isolate;
v8::Isolate::Scope IsolateScope(Isolate);
v8::HandleScope HandleScope(Isolate);
v8::Local<v8::Context> Context = ResultInfo->Context.Get(Isolate);
v8::Context::Scope ContextScope(Context);
auto Result = ResultInfo->Result.Get(Isolate);

return Result->IsBigInt() ? 1 : 0;
}

V8_EXPORT int64_t GetBigIntFromResult(FResultInfo *ResultInfo)
{
v8::Isolate* Isolate = ResultInfo->Isolate;
Expand Down

0 comments on commit f9a060d

Please sign in to comment.