Support properties and readonly fields

This commit is contained in:
Deukhoofd 2019-06-15 16:01:06 +02:00
parent 21e6d3af24
commit d01f264a0d
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 222 additions and 68 deletions

View File

@ -17,31 +17,82 @@ namespace PorygonSharp.UserData
{ {
var hash = name.ScriptHash(); var hash = name.ScriptHash();
RegisterUserDataType(hash); RegisterUserDataType(hash);
if (UserDataLookup.ContainsKey(type))
return;
UserDataLookup.Add(type, hash); UserDataLookup.Add(type, hash);
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance |
BindingFlags.FlattenHierarchy | BindingFlags.Static;
var fields = type.GetFields(bindingFlags);
foreach (var field in fields) foreach (var field in fields)
{ {
var getter = new GetterDelegate(ptr => RegisterField(field, hash);
{ }
var obj = GCHandle.FromIntPtr(ptr).Target;
var value = field.GetValue(obj); var properties = type.GetProperties(bindingFlags);
return new EvalValue(value).GetPointer(); foreach (var property in properties)
}); {
RegisterProperty(property, hash);
}
}
private static void RegisterField(FieldInfo field, uint typeHash)
{
var getter = new GetterDelegate(ptr =>
{
var obj = GCHandle.FromIntPtr(ptr).Target;
var value = field.GetValue(obj);
return new EvalValue(value).GetPointer();
});
var setterPtr = IntPtr.Zero;
if (!field.IsInitOnly)
{
var setter = new SetterDelegate((ptr, val) => var setter = new SetterDelegate((ptr, val) =>
{ {
var obj = GCHandle.FromIntPtr(ptr).Target; var obj = GCHandle.FromIntPtr(ptr).Target;
var evalValue = Convert.ChangeType(new EvalValue(val).GetObjectValue(), field.FieldType); var evalValue = Convert.ChangeType(new EvalValue(val).GetObjectValue(), field.FieldType);
field.SetValue(obj, evalValue); field.SetValue(obj, evalValue);
}); });
var scriptType = ScriptType.ScriptType.GetScriptType(field.FieldType); setterPtr = Marshal.GetFunctionPointerForDelegate(setter);
var userDataField = CreateUserDataField(scriptType, Marshal.GetFunctionPointerForDelegate(getter),
Marshal.GetFunctionPointerForDelegate(setter));
var fieldName = field.Name.ScriptHash();
RegisterUserDataField(hash, fieldName, userDataField);
} }
var scriptType = ScriptType.ScriptType.GetScriptType(field.FieldType);
var userDataField = CreateUserDataField(scriptType, Marshal.GetFunctionPointerForDelegate(getter),
setterPtr);
var fieldName = field.Name.ScriptHash();
RegisterUserDataField(typeHash, fieldName, userDataField);
}
private static void RegisterProperty(PropertyInfo property, uint typeHash)
{
var getterPtr = IntPtr.Zero;
if (property.GetGetMethod(false) != null)
{
var getter = new GetterDelegate(ptr =>
{
var obj = GCHandle.FromIntPtr(ptr).Target;
var value = property.GetValue(obj);
return new EvalValue(value).GetPointer();
});
getterPtr = Marshal.GetFunctionPointerForDelegate(getter);
}
var setterPtr = IntPtr.Zero;
if (property.GetSetMethod(false) != null)
{
var setter = new SetterDelegate((ptr, val) =>
{
var obj = GCHandle.FromIntPtr(ptr).Target;
var evalValue = Convert.ChangeType(new EvalValue(val).GetObjectValue(), property.PropertyType);
property.SetValue(obj, evalValue);
});
setterPtr = Marshal.GetFunctionPointerForDelegate(setter);
}
var scriptType = ScriptType.ScriptType.GetScriptType(property.PropertyType);
var userDataField = CreateUserDataField(scriptType, getterPtr, setterPtr);
var fieldName = property.Name.ScriptHash();
RegisterUserDataField(typeHash, fieldName, userDataField);
} }
public static uint GetTypeId(Type t) public static uint GetTypeId(Type t)
@ -49,6 +100,12 @@ namespace PorygonSharp.UserData
return UserDataLookup[t]; return UserDataLookup[t];
} }
public static int GetUserDataFieldCount(Type t)
{
var hash = GetTypeId(t);
return GetUserDataFieldCount(hash);
}
[DllImport("libPorygonLang", EntryPoint = "RegisterUserDataType", CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "RegisterUserDataType", CallingConvention = CallingConvention.Cdecl)]
private static extern void RegisterUserDataType(uint hashId); private static extern void RegisterUserDataType(uint hashId);

View File

@ -109,59 +109,6 @@ namespace PorygonSharpTests
} }
} }
private class UserDataTestObject
{
public int Foo = 200;
}
[Test]
public void Test9()
{
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
using (var script = Script.CreateScript(@"
function test(testObject v)
result = v['Foo']
end
"))
{
var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags)
{
throw new Exception(diag.GetCode().ToString());
}
script.Evaluate();
script.CallFunction("test", new UserDataTestObject());
var variable = script.GetVariable("result");
Assert.AreEqual(200, variable.EvaluateInteger());
}
}
[Test]
public void Test10()
{
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
using (var script = Script.CreateScript(@"
function test(testObject v)
v['Foo'] = 20000
end
"))
{
var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags)
{
throw new Exception(diag.GetCode().ToString());
}
script.Evaluate();
var parameter = new UserDataTestObject();
script.CallFunction("test", parameter);
Assert.AreEqual(20000, parameter.Foo);
}
}
[Test] [Test]
public void TestHash() public void TestHash()
{ {

View File

@ -0,0 +1,150 @@
using System;
using System.Linq;
using NUnit.Framework;
using PorygonSharp;
using PorygonSharp.UserData;
namespace PorygonSharpTests
{
[TestFixture]
public class UserDataTests
{
private class UserDataTestObject
{
public int Foo = 200;
public int Bar { get; set; }
public int GetOnly { get; } = 865;
public readonly int ReadOnly = 684;
}
[Test]
public void CanGetFromUserDataField()
{
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
using (var script = Script.CreateScript(@"
function test(testObject v)
result = v['Foo']
end
"))
{
var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags)
{
throw new Exception(diag.GetCode().ToString());
}
script.Evaluate();
script.CallFunction("test", new UserDataTestObject());
var variable = script.GetVariable("result");
Assert.AreEqual(200, variable.EvaluateInteger());
}
}
[Test]
public void CanSetToUserDataField()
{
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
using (var script = Script.CreateScript(@"
function test(testObject v)
v['Foo'] = 20000
end
"))
{
var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags)
{
throw new Exception(diag.GetCode().ToString());
}
script.Evaluate();
var parameter = new UserDataTestObject();
script.CallFunction("test", parameter);
Assert.AreEqual(20000, parameter.Foo);
}
}
[Test]
public void CanGetFromPropertyWithoutSetter()
{
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
using (var script = Script.CreateScript(@"
function test(testObject v)
result = v['GetOnly']
end
"))
{
var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags)
{
throw new Exception(diag.GetCode().ToString());
}
script.Evaluate();
var parameter = new UserDataTestObject();
script.CallFunction("test", parameter);
Assert.AreEqual(865, script.GetVariable("result").EvaluateInteger());
}
}
[Test]
public void CantSetToPropertyWithoutSetter()
{
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
using (var script = Script.CreateScript(@"
function test(testObject v)
v['GetOnly'] = 10000
end
"))
{
var diags = script.Diagnostics.GetDiagnostics().ToArray();
Assert.IsNotEmpty(diags);
Assert.AreEqual(1, diags.Length);
}
}
[Test]
public void CanGetFromReadonlyField()
{
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
using (var script = Script.CreateScript(@"
function test(testObject v)
result = v['ReadOnly']
end
"))
{
var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags)
{
throw new Exception(diag.GetCode().ToString());
}
script.Evaluate();
var parameter = new UserDataTestObject();
script.CallFunction("test", parameter);
Assert.AreEqual(684, script.GetVariable("result").EvaluateInteger());
}
}
[Test]
public void CantSetToReadonlyField()
{
UserDataHandler.RegisterType("testObject", typeof(UserDataTestObject));
using (var script = Script.CreateScript(@"
function test(testObject v)
v['ReadOnly'] = 10000
end
"))
{
var diags = script.Diagnostics.GetDiagnostics().ToArray();
Assert.IsNotEmpty(diags);
Assert.AreEqual(1, diags.Length);
}
}
}
}

Binary file not shown.