Skip to content

Commit

Permalink
Merge pull request #165 from dwango/work_aot_2
Browse files Browse the repository at this point in the history
Removed Expression.Compile
  • Loading branch information
yutopp authored Feb 4, 2019
2 parents 1e7c340 + cc9de91 commit 0e5ea32
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 226 deletions.
31 changes: 24 additions & 7 deletions Assets/VRM/UniJSON/Editor/Tests/GenericCallUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public void Set(int value)
{
Value = value;
}

public int Get(int _)
{
return Value;
}
}


Expand All @@ -29,15 +34,27 @@ public void GenericCallUtilityTestsSimplePasses()
{
var s = new Sample();

var mi = s.GetType().GetMethod("Set");

var invoke = (Action<Sample, int>)GenericInvokeCallFactory.Create<Sample, int>(mi);
invoke(s, 1);
Assert.AreEqual(1, s.Value);
{
var mi = s.GetType().GetMethod("Set");
var action = GenericInvokeCallFactory.OpenAction<Sample, int>(mi);
action(s, 1);
Assert.AreEqual(1, s.Value);
}

{
var mi = s.GetType().GetMethod("Get");
var func = GenericInvokeCallFactory.OpenFunc<Sample, int, int>(mi);
var value = func(s, 1);
Assert.AreEqual(1, s.Value);
}

var exp = (Action<Sample, int>)GenericExpressionCallFactory.Create<Sample, int>(mi);
exp(s, 2);
Assert.AreEqual(2, s.Value);
{
var mi = s.GetType().GetMethod("Set");
var action = GenericExpressionCallFactory.Create<Sample, int>(mi);
action(s, 2);
Assert.AreEqual(2, s.Value);
}
}
}
}
27 changes: 12 additions & 15 deletions Assets/VRM/UniJSON/Scripts/FormatterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,20 @@ static MethodInfo GetMethod<T>(Expression<Func<T>> expression)
return method.MakeGenericMethod(typeof(T));
}

[Obsolete("error when AOT. use Key, Value")]
//
// https://stackoverflow.com/questions/238765/given-a-type-expressiontype-memberaccess-how-do-i-get-the-field-value
//
public static void KeyValue<T>(this IFormatter f, Expression<Func<T>> expression)
{
var func = expression.Compile();
var value = func();
if (value != null)
{
var body = expression.Body as MemberExpression;
if (body == null)
{
body = ((UnaryExpression)expression.Body).Operand as MemberExpression;
}
f.Key(body.Member.Name);
f.Serialize(expression.Compile()());
//var method = GetMethod(expression);
//method.Invoke(this, new object[] { value });
}
MemberExpression outerMember = (MemberExpression)expression.Body;
var outerProp = (FieldInfo)outerMember.Member;
MemberExpression innerMember = (MemberExpression)outerMember.Expression;
var innerField = (FieldInfo)innerMember.Member;
ConstantExpression ce = (ConstantExpression)innerMember.Expression;
object innerObj = ce.Value;
object outerObj = innerField.GetValue(innerObj);
f.Key(outerProp.Name);
f.Serialize(outerProp.GetValue(outerObj));
}
}
}
10 changes: 5 additions & 5 deletions Assets/VRM/UniJSON/Scripts/FormatterExtensionsSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static Action<IFormatter, T> GetSerializer(Type t)
if (typeof(T) == typeof(object) && t.GetType() != typeof(object))
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject");
return GenericInvokeCallFactory.Create<IFormatter, T>(mi);
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}

try
Expand All @@ -93,7 +93,7 @@ static Action<IFormatter, T> GetSerializer(Type t)
var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t });
if (mi != null)
{
return GenericInvokeCallFactory.Create<IFormatter, T>(mi);
return GenericInvokeCallFactory.OpenAction<IFormatter, T>(mi);
}
}
catch (AmbiguousMatchException)
Expand All @@ -111,7 +111,7 @@ static Action<IFormatter, T> GetSerializer(Type t)
if (idictionary != null)
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary");
return GenericInvokeCallFactory.Create<IFormatter, T>(mi);
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}

Expand All @@ -120,7 +120,7 @@ static Action<IFormatter, T> GetSerializer(Type t)
if (t == typeof(object[]))
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray");
return GenericInvokeCallFactory.Create<IFormatter, T>(mi);
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}

Expand All @@ -134,7 +134,7 @@ static Action<IFormatter, T> GetSerializer(Type t)
{
var g = FormatterExtensionsSerializer.GetMethod("SerializeArray");
var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments());
return GenericInvokeCallFactory.Create<IFormatter, T>(mi);
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}

Expand Down
Loading

0 comments on commit 0e5ea32

Please sign in to comment.