Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Tencent/puerts
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Feb 25, 2021
2 parents ae4ad3a + 4b3bc19 commit 486d618
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 4 deletions.
10 changes: 9 additions & 1 deletion unity/Assets/Puerts/Src/DataTranslate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ internal object AnyTranslator(IntPtr isolate, IGetValueFromJs getValueApi, IntPt
}
}
var objPtr = getValueApi.GetObject(isolate, value, isByRef);
return objectPool.Get(objPtr.ToInt32());
var result = objectPool.Get(objPtr.ToInt32());

var typedValueResult = result as TypedValue;
if (typedValueResult != null)
{
return typedValueResult.Target;
}

return result;
case JsValueType.Number:
return getValueApi.GetNumber(isolate, value, isByRef);
case JsValueType.String:
Expand Down
102 changes: 102 additions & 0 deletions unity/Assets/Puerts/Src/TypedValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Tencent is pleased to support the open source community by making Puerts available.
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Puerts is licensed under the BSD 3-Clause License, except for the third-party components listed in the file 'LICENSE' which may be subject to their corresponding license terms.
* This file is subject to the terms and conditions defined in file 'LICENSE', which is part of this source code package.
*/
namespace Puerts
{
public interface TypedValue
{
object Target { get; }
}


public class Any<T> : TypedValue
{
T mTarget;

public Any(T i)
{
mTarget = i;
}

public object Target
{
get
{
return mTarget;
}
}
}

public class ByteValue : Any<byte>
{
public ByteValue(byte i) : base(i)
{
}
}

public class SByteValue : Any<sbyte>
{
public SByteValue(sbyte i) : base(i)
{
}
}

public class CharValue : Any<char>
{
public CharValue(char i) : base(i)
{
}
}

public class Int16Value : Any<short>
{
public Int16Value(short i) : base(i)
{
}
}

public class UInt16Value : Any<ushort>
{
public UInt16Value(ushort i) : base(i)
{
}
}

public class Int32Value : Any<int>
{
public Int32Value(int i) : base(i)
{
}
}

public class UInt32Value : Any<uint>
{
public UInt32Value(uint i) : base(i)
{
}
}

public class Int64Value : Any<long>
{
public Int64Value(long i) : base(i)
{
}
}

public class UInt64Value : Any<ulong>
{
public UInt64Value(ulong i) : base(i)
{
}
}

public class FloatValue : Any<float>
{
public FloatValue(float i) : base(i)
{
}
}
}
6 changes: 5 additions & 1 deletion unity/general/Src/Helloworld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
*/

using System.IO;
using System.Reflection;
using Puerts;

public class TxtLoader : ILoader
{
private string root = "../../Assets/Puerts/Src/Resources";
private string root = Path.Combine(
System.Text.RegularExpressions.Regex.Replace(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "^file:(\\\\)?", ""),
"../../Assets/Puerts/Src/Resources"
);

public bool FileExists(string filepath)
{
Expand Down
13 changes: 13 additions & 0 deletions unity/general/Src/UnitTest/TestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,17 @@ public bool Call(bool b)
return true;
}
}

public class TypedValue
{
static object lastCallbackValue = null;
public static void Callback(object o)
{
lastCallbackValue = o;
}
public static Type GetLastCallbackValueType()
{
return lastCallbackValue == null ? null : lastCallbackValue.GetType();
}
}
}
31 changes: 30 additions & 1 deletion unity/general/Src/UnitTest/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class TxtLoader : ILoader
{
private string root = Path.Combine(
System.Text.RegularExpressions.Regex.Replace(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "^file:\\\\", ""),
"../../Assets/Puerts/Src/Resources");
"../../Assets/Puerts/Src/Resources"
);

public bool FileExists(string filepath)
{
Expand Down Expand Up @@ -1004,6 +1005,34 @@ function dosomething(){}
");
jsEnv.Dispose();
}
[Test]
public void Int64Value()
{
var jsEnv = new JsEnv(new TxtLoader());

jsEnv.Eval(@"
const CS = require('csharp');
let value = new CS.Puerts.Int64Value(512n);
CS.Puerts.UnitTest.TypedValue.Callback(value);
");

Assert.True(UnitTest.TypedValue.GetLastCallbackValueType() == typeof(System.Int64));
Assert.False(UnitTest.TypedValue.GetLastCallbackValueType() == typeof(System.Int32));
}
[Test]
public void FloatValue()
{
var jsEnv = new JsEnv(new TxtLoader());

jsEnv.Eval(@"
const CS = require('csharp');
let value = new CS.Puerts.FloatValue(512.256);
CS.Puerts.UnitTest.TypedValue.Callback(value);
");

Assert.True(UnitTest.TypedValue.GetLastCallbackValueType() == typeof(System.Single));
Assert.False(UnitTest.TypedValue.GetLastCallbackValueType() == typeof(System.Int32));
}
}
}

4 changes: 4 additions & 0 deletions unity/general/vs2013/Puerts.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<AssemblyName>Puerts.Core</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<BaseIntermediateOutputPath>obj\Any CPU\Debug\Puerts.Core</BaseIntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -85,6 +86,9 @@
<Compile Include="..\..\Assets\Puerts\Src\Utils.cs">
<Link>Assets\Puerts\Src\Utils.cs</Link>
</Compile>
<Compile Include="..\..\Assets\Puerts\Src\TypedValue.cs">
<Link>Assets\Puerts\Src\TypedValue.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
</ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion unity/native_src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ if ( APPLE )
)
set_xcode_property (puerts IPHONEOS_DEPLOYMENT_TARGET "7.0" "all")
else ()
set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_64_BIT)")
# set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_64_BIT)")
# 某些mac机器上会默认开始编译arm64版本,暂时先写死x86版本,后续再支持arm64
set(CMAKE_OSX_ARCHITECTURES x86_64)
# add_library(puerts SHARED
add_library(puerts MODULE
${PUERTS_SRC} ${PUERTS_INC}
)
Expand Down

0 comments on commit 486d618

Please sign in to comment.