From 21e6d3af24c2abe6b5eedd64e604ecbaca823eba Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 14 Jun 2019 22:40:15 +0200 Subject: [PATCH] Fixes for setting values in UserData --- PorygonSharp/UserData/UserDataHandler.cs | 4 ++-- PorygonSharpTests/UnitTest1.cs | 25 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/PorygonSharp/UserData/UserDataHandler.cs b/PorygonSharp/UserData/UserDataHandler.cs index 7a17121..7ca5f9d 100644 --- a/PorygonSharp/UserData/UserDataHandler.cs +++ b/PorygonSharp/UserData/UserDataHandler.cs @@ -30,8 +30,8 @@ namespace PorygonSharp.UserData }); var setter = new SetterDelegate((ptr, val) => { - var evalValue = new EvalValue(val).GetObjectValue(); - var obj = GCHandle.FromIntPtr(ptr).Target; + var obj = GCHandle.FromIntPtr(ptr).Target; + var evalValue = Convert.ChangeType(new EvalValue(val).GetObjectValue(), field.FieldType); field.SetValue(obj, evalValue); }); var scriptType = ScriptType.ScriptType.GetScriptType(field.FieldType); diff --git a/PorygonSharpTests/UnitTest1.cs b/PorygonSharpTests/UnitTest1.cs index 7a8f20e..deba67a 100644 --- a/PorygonSharpTests/UnitTest1.cs +++ b/PorygonSharpTests/UnitTest1.cs @@ -138,12 +138,35 @@ end } } + [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] public void TestHash() { var hash = HashedString.ScriptHash("Foo"); Assert.AreEqual(193501609, hash); } - } } \ No newline at end of file