Update to latest version of Porygon lib

This commit is contained in:
Deukhoofd 2019-07-21 12:07:17 +02:00
parent f23f682d52
commit 58da41852d
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
6 changed files with 37 additions and 19 deletions

View File

@ -53,8 +53,7 @@ namespace PorygonSharp.DiagnosticHandling
public string GetFullDiagnosticMessage(DiagnosticItem item) public string GetFullDiagnosticMessage(DiagnosticItem item)
{ {
var ptr = GetFullDiagnostic(_handle, item.GetHandle()); return GetFullDiagnostic(_handle, item.GetHandle());
return Marshal.PtrToStringUTF8(ptr);
} }
[DllImport("libPorygonLang", EntryPoint = "GetDiagnosticsCount", CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticsCount", CallingConvention = CallingConvention.Cdecl)]
@ -64,6 +63,6 @@ namespace PorygonSharp.DiagnosticHandling
private static extern IntPtr GetDiagnosticAt(IntPtr ptr, int position); private static extern IntPtr GetDiagnosticAt(IntPtr ptr, int position);
[DllImport("libPorygonLang", EntryPoint = "GetFullDiagnostic", CallingConvention = CallingConvention.Cdecl)] [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);
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using PorygonSharp.UserData; using PorygonSharp.UserData;
namespace PorygonSharp namespace PorygonSharp
@ -86,8 +87,16 @@ namespace PorygonSharp
public string EvaluateString() public string EvaluateString()
{ {
var ptr = EvaluateString(_handle); var stringLength = GetStringLength(_handle);
return Marshal.PtrToStringUni(ptr); // 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() public object EvaluateGenericObject()
@ -133,8 +142,11 @@ namespace PorygonSharp
private static extern double EvaluateFloat(IntPtr ptr); private static extern double EvaluateFloat(IntPtr ptr);
[DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueBool", CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueBool", CallingConvention = CallingConvention.Cdecl)]
private static extern bool EvaluateBool(IntPtr ptr); 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)] [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)] [DllImport("libPorygonLang", EntryPoint = "EvaluateUserDataObj",CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr EvaluateUserDataObj(IntPtr ptr); private static extern IntPtr EvaluateUserDataObj(IntPtr ptr);

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>

View File

@ -1,10 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using PorygonSharp.DiagnosticHandling; using PorygonSharp.DiagnosticHandling;
namespace PorygonSharp namespace PorygonSharp
@ -14,16 +10,26 @@ namespace PorygonSharp
{ {
private readonly IntPtr _evaluator; private readonly IntPtr _evaluator;
private readonly IntPtr _scriptVariables; private readonly IntPtr _scriptVariables;
private readonly IntPtr _boundScript; private readonly SharedPointer<object> _boundScript;
private readonly SharedPointer _returnValue; private readonly SharedPointer<object> _returnType;
internal readonly IntPtr Diagnostics; internal readonly SharedPointer<Diagnostics> Diagnostics;
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal struct SharedPointer internal struct SharedPointer<T>
{ {
private IntPtr _pointer; private readonly IntPtr _pointer;
private int _references; private readonly int _references;
public static implicit operator IntPtr(SharedPointer<T> o)
{
return o._pointer;
}
public T Unwrap()
{
return Marshal.PtrToStructure<T>(_pointer);
}
} }
public class Script : IDisposable public class Script : IDisposable
@ -43,7 +49,6 @@ namespace PorygonSharp
public void Dispose() public void Dispose()
{ {
Marshal.FreeHGlobal(_internalScriptHandle); Marshal.FreeHGlobal(_internalScriptHandle);
Diagnostics.Dispose();
} }
public void Evaluate() public void Evaluate()

View File

@ -116,7 +116,9 @@ namespace PorygonSharpTests
{ {
var diagnostics = script.Diagnostics.GetDiagnostics().ToArray(); var diagnostics = script.Diagnostics.GetDiagnostics().ToArray();
Assert.IsNotEmpty(diagnostics); 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]));
} }
} }

Binary file not shown.