diff --git a/PorygonSharp/DiagnosticHandling/Diagnostics.cs b/PorygonSharp/DiagnosticHandling/Diagnostics.cs index 21ddeb4..c138052 100644 --- a/PorygonSharp/DiagnosticHandling/Diagnostics.cs +++ b/PorygonSharp/DiagnosticHandling/Diagnostics.cs @@ -53,8 +53,7 @@ namespace PorygonSharp.DiagnosticHandling public string GetFullDiagnosticMessage(DiagnosticItem item) { - var ptr = GetFullDiagnostic(_handle, item.GetHandle()); - return Marshal.PtrToStringUTF8(ptr); + return GetFullDiagnostic(_handle, item.GetHandle()); } [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticsCount", CallingConvention = CallingConvention.Cdecl)] @@ -64,6 +63,6 @@ namespace PorygonSharp.DiagnosticHandling 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); + private static extern string GetFullDiagnostic(IntPtr diagnosticsHolder, IntPtr diagnostic); } } \ No newline at end of file diff --git a/PorygonSharp/EvalValue.cs b/PorygonSharp/EvalValue.cs index 1ab1872..97394ac 100644 --- a/PorygonSharp/EvalValue.cs +++ b/PorygonSharp/EvalValue.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using System.Text; using PorygonSharp.UserData; namespace PorygonSharp @@ -86,8 +87,16 @@ namespace PorygonSharp public string EvaluateString() { - var ptr = EvaluateString(_handle); - return Marshal.PtrToStringUni(ptr); + var stringLength = GetStringLength(_handle); + // The library uses 16 bit chars for strings, so create a byte array of twice the length given. + var byteArr = new byte[stringLength * 2]; + var result = EvaluateString(_handle, byteArr, stringLength); + if (result != 0) + { + throw new Exception("Something went wrong while evaluating a string."); + } + var str = Encoding.Unicode.GetString(byteArr); + return str; } public object EvaluateGenericObject() @@ -133,8 +142,11 @@ namespace PorygonSharp private static extern double EvaluateFloat(IntPtr ptr); [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueBool", CallingConvention = CallingConvention.Cdecl)] private static extern bool EvaluateBool(IntPtr ptr); + [DllImport("libPorygonLang", EntryPoint = "GetEvalValueStringLength",CallingConvention = CallingConvention.Cdecl)] + private static extern int GetStringLength(IntPtr ptr); + [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueString",CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr EvaluateString(IntPtr ptr); + private static extern int EvaluateString(IntPtr ptr, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] buffer, int capacity); [DllImport("libPorygonLang", EntryPoint = "EvaluateUserDataObj",CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr EvaluateUserDataObj(IntPtr ptr); diff --git a/PorygonSharp/PorygonSharp.csproj b/PorygonSharp/PorygonSharp.csproj index aa2ce34..e306e6a 100644 --- a/PorygonSharp/PorygonSharp.csproj +++ b/PorygonSharp/PorygonSharp.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netstandard2.0 true diff --git a/PorygonSharp/Script.cs b/PorygonSharp/Script.cs index aeed0bd..f940fa2 100644 --- a/PorygonSharp/Script.cs +++ b/PorygonSharp/Script.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; -using System.Security; -using System.Text; using PorygonSharp.DiagnosticHandling; namespace PorygonSharp @@ -14,16 +10,26 @@ namespace PorygonSharp { private readonly IntPtr _evaluator; private readonly IntPtr _scriptVariables; - private readonly IntPtr _boundScript; - private readonly SharedPointer _returnValue; - internal readonly IntPtr Diagnostics; + private readonly SharedPointer _boundScript; + private readonly SharedPointer _returnType; + internal readonly SharedPointer Diagnostics; } [StructLayout(LayoutKind.Sequential)] - internal struct SharedPointer + internal struct SharedPointer { - private IntPtr _pointer; - private int _references; + private readonly IntPtr _pointer; + private readonly int _references; + + public static implicit operator IntPtr(SharedPointer o) + { + return o._pointer; + } + + public T Unwrap() + { + return Marshal.PtrToStructure(_pointer); + } } public class Script : IDisposable @@ -43,7 +49,6 @@ namespace PorygonSharp public void Dispose() { Marshal.FreeHGlobal(_internalScriptHandle); - Diagnostics.Dispose(); } public void Evaluate() diff --git a/PorygonSharpTests/UnitTest1.cs b/PorygonSharpTests/UnitTest1.cs index 995b6e6..ce7a244 100644 --- a/PorygonSharpTests/UnitTest1.cs +++ b/PorygonSharpTests/UnitTest1.cs @@ -116,7 +116,9 @@ namespace PorygonSharpTests { 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])); + + Assert.AreEqual("[Error] (2, 2) '\\x' is not a valid control character.", + script.Diagnostics.GetFullDiagnosticMessage(diagnostics[0])); } } diff --git a/libPorygonLang.so b/libPorygonLang.so index 4acd13c..1d2149a 100755 Binary files a/libPorygonLang.so and b/libPorygonLang.so differ