diff --git a/PorygonSharp/DiagnosticHandling/DiagnosticItem.cs b/PorygonSharp/DiagnosticHandling/DiagnosticItem.cs index 694eb84..8c1fb24 100644 --- a/PorygonSharp/DiagnosticHandling/DiagnosticItem.cs +++ b/PorygonSharp/DiagnosticHandling/DiagnosticItem.cs @@ -1,18 +1,55 @@ +using System; using System.Runtime.InteropServices; namespace PorygonSharp.DiagnosticHandling { - [StructLayout(LayoutKind.Sequential)] - public struct DiagnosticItem + public class DiagnosticItem { - private readonly int _severity; - private readonly DiagnosticCode _code; - private readonly uint _start; - private readonly uint _length; + [StructLayout(LayoutKind.Sequential)] + public struct InternalRepresentation + { + private readonly int _severity; + private readonly DiagnosticCode _code; + private readonly uint _start; + private readonly uint _length; + + public DiagnosticCode GetCode() + { + return _code; + } + } + + private InternalRepresentation _internalRepresentation; + private readonly IntPtr _handle; + + private bool _initialed; + + private InternalRepresentation Internal + { + get + { + if (!_initialed) + { + _internalRepresentation = Marshal.PtrToStructure(_handle); + } + _initialed = true; + return _internalRepresentation; + } + } + + internal DiagnosticItem(IntPtr handle) + { + _handle = handle; + } + + internal IntPtr GetHandle() + { + return _handle; + } public DiagnosticCode GetCode() { - return _code; + return Internal.GetCode(); } } } \ No newline at end of file diff --git a/PorygonSharp/DiagnosticHandling/Diagnostics.cs b/PorygonSharp/DiagnosticHandling/Diagnostics.cs index e39c5f0..21ddeb4 100644 --- a/PorygonSharp/DiagnosticHandling/Diagnostics.cs +++ b/PorygonSharp/DiagnosticHandling/Diagnostics.cs @@ -47,14 +47,23 @@ namespace PorygonSharp.DiagnosticHandling for (var i = 0; i < count; i++) { var ptr = GetDiagnosticAt(_handle, i); - yield return Marshal.PtrToStructure(ptr); + yield return new DiagnosticItem(ptr); } } - [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticsCount")] + public string GetFullDiagnosticMessage(DiagnosticItem item) + { + var ptr = GetFullDiagnostic(_handle, item.GetHandle()); + return Marshal.PtrToStringUTF8(ptr); + } + + [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticsCount", CallingConvention = CallingConvention.Cdecl)] private static extern int GetDiagnosticsCount(IntPtr ptr); - [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticAt")] + [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticAt", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr GetDiagnosticAt(IntPtr ptr, int position); + + [DllImport("libPorygonLang", EntryPoint = "GetFullDiagnostic", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr GetFullDiagnostic(IntPtr diagnosticsHolder, IntPtr diagnostic); } } \ No newline at end of file diff --git a/PorygonSharp/EvalValue.cs b/PorygonSharp/EvalValue.cs index 24d40cd..7490c78 100644 --- a/PorygonSharp/EvalValue.cs +++ b/PorygonSharp/EvalValue.cs @@ -86,7 +86,7 @@ namespace PorygonSharp public string EvaluateString() { var ptr = EvaluateString(_handle); - return Marshal.PtrToStringUTF8(ptr); + return Marshal.PtrToStringUni(ptr); } public object GetObjectValue() diff --git a/PorygonSharpTests/UnitTest1.cs b/PorygonSharpTests/UnitTest1.cs index e96056a..de3a542 100644 --- a/PorygonSharpTests/UnitTest1.cs +++ b/PorygonSharpTests/UnitTest1.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using NUnit.Framework; using PorygonSharp; using PorygonSharp.UserData; @@ -16,7 +17,7 @@ namespace PorygonSharpTests var diags = script.Diagnostics.GetDiagnostics(); foreach (var diag in diags) { - throw new Exception(diag.GetCode().ToString()); + throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag)); } Assert.False(script.Diagnostics.HasErrors()); } @@ -109,6 +110,18 @@ namespace PorygonSharpTests } } + [Test] + public void TestDiagnostic() + { + using (var script = new Script("\n'\\x'")) + { + var diagnostics = script.Diagnostics.GetDiagnostics().ToArray(); + Assert.IsNotEmpty(diagnostics); + Assert.AreEqual("[Error] (1, 3) '\\x' is not a valid control character.", script.Diagnostics.GetFullDiagnosticMessage(diagnostics[0])); + } + } + + [Test] public void TestHash() { diff --git a/PorygonSharpTests/UserDataTests.cs b/PorygonSharpTests/UserDataTests.cs index 4019617..1f88174 100644 --- a/PorygonSharpTests/UserDataTests.cs +++ b/PorygonSharpTests/UserDataTests.cs @@ -30,7 +30,7 @@ end var diags = script.Diagnostics.GetDiagnostics(); foreach (var diag in diags) { - throw new Exception(diag.GetCode().ToString()); + throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag)); } script.Evaluate(); @@ -54,7 +54,7 @@ end var diags = script.Diagnostics.GetDiagnostics(); foreach (var diag in diags) { - throw new Exception(diag.GetCode().ToString()); + throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag)); } script.Evaluate(); @@ -78,7 +78,7 @@ end var diags = script.Diagnostics.GetDiagnostics(); foreach (var diag in diags) { - throw new Exception(diag.GetCode().ToString()); + throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag)); } script.Evaluate(); @@ -119,7 +119,7 @@ end var diags = script.Diagnostics.GetDiagnostics(); foreach (var diag in diags) { - throw new Exception(diag.GetCode().ToString()); + throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag)); } script.Evaluate(); diff --git a/libPorygonLang.so b/libPorygonLang.so index 55f3687..4ef9583 100755 Binary files a/libPorygonLang.so and b/libPorygonLang.so differ