Added support for calling script functions

This commit is contained in:
Deukhoofd 2019-06-07 13:52:28 +02:00
parent cd185ed2e5
commit d8bf2c2994
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 84 additions and 0 deletions

View File

@ -12,6 +12,43 @@ namespace PorygonSharp
_handle = handle; _handle = handle;
} }
public EvalValue(object o)
{
var type = o.GetType();
var typeCode = Type.GetTypeCode(type);
switch (typeCode)
{
case TypeCode.Boolean:
_handle = CreateBoolEvalValue((bool)o);
break;
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
_handle = CreateIntegerEvalValue(Convert.ToInt64(o));
break;
case TypeCode.Char:
case TypeCode.String:
_handle = CreateStringEvalValue(o.ToString());
break;
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
_handle = CreateFloatEvalValue(Convert.ToDouble(o));
break;
case TypeCode.DateTime:
case TypeCode.DBNull:
case TypeCode.Empty:
case TypeCode.Object:
default:
throw new Exception($"Type {type} is not currently available as EvalValue");
}
}
public void Dispose() public void Dispose()
{ {
if (_handle != IntPtr.Zero) if (_handle != IntPtr.Zero)
@ -47,6 +84,11 @@ namespace PorygonSharp
return Marshal.PtrToStringUTF8(ptr); return Marshal.PtrToStringUTF8(ptr);
} }
internal IntPtr GetPointer()
{
return _handle;
}
[DllImport("libPorygonLang", EntryPoint = "GetEvalValueTypeClass", CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "GetEvalValueTypeClass", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetTypeClass(IntPtr ptr); private static extern int GetTypeClass(IntPtr ptr);
[DllImport("libPorygonLang", EntryPoint = "GetEvalValueType", CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "GetEvalValueType", CallingConvention = CallingConvention.Cdecl)]
@ -60,5 +102,13 @@ namespace PorygonSharp
[DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueString",CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueString",CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr EvaluateString(IntPtr ptr); private static extern IntPtr EvaluateString(IntPtr ptr);
[DllImport("libPorygonLang", EntryPoint = "CreateIntegerEvalValue",CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr CreateIntegerEvalValue(long l);
[DllImport("libPorygonLang", EntryPoint = "CreateFloatEvalValue",CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr CreateFloatEvalValue(double d);
[DllImport("libPorygonLang", EntryPoint = "CreateBoolEvalValue",CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr CreateBoolEvalValue(bool b);
[DllImport("libPorygonLang", EntryPoint = "CreateStringEvalValue",CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr CreateStringEvalValue(string s);
} }
} }

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using PorygonSharp.DiagnosticHandling; using PorygonSharp.DiagnosticHandling;
@ -56,6 +58,18 @@ namespace PorygonSharp
return new EvalValue(ptr); return new EvalValue(ptr);
} }
public bool HasFunction(string key)
{
return HasFunction(_internalScriptHandle, key);
}
public EvalValue CallFunction(string key, params object[] parameters)
{
var scriptParameters = parameters.Select(x => new EvalValue(x).GetPointer()).ToArray();
var ptr = CallFunction(_internalScriptHandle, key, scriptParameters, scriptParameters.Length);
return new EvalValue(ptr);
}
[DllImport("libPorygonLang", EntryPoint = "CreateScript", CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "CreateScript", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr Create(StringBuilder s); private static extern IntPtr Create(StringBuilder s);
[DllImport("libPorygonLang", EntryPoint = "EvaluateScript", CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "EvaluateScript", CallingConvention = CallingConvention.Cdecl)]
@ -66,5 +80,11 @@ namespace PorygonSharp
private static extern bool HasVariable(IntPtr script, string key); private static extern bool HasVariable(IntPtr script, string key);
[DllImport("libPorygonLang", EntryPoint = "GetVariable", CallingConvention = CallingConvention.Cdecl)] [DllImport("libPorygonLang", EntryPoint = "GetVariable", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr GetVariable(IntPtr script, string key); private static extern IntPtr GetVariable(IntPtr script, string key);
[DllImport("libPorygonLang", EntryPoint = "HasFunction", CallingConvention = CallingConvention.Cdecl)]
private static extern bool HasFunction(IntPtr script, string key);
[DllImport("libPorygonLang", EntryPoint = "CallFunction", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr CallFunction(IntPtr script, string key, IntPtr[] parameters, int parameterCount);
} }
} }

View File

@ -93,5 +93,19 @@ namespace PorygonSharpTests
} }
} }
[Test]
public void Test8()
{
using (var script = new Script("function add(number a, number b) result = a + b end"))
{
script.Evaluate();
script.CallFunction("add", 100, 50);
var variable = script.GetVariable("result");
Assert.AreEqual(TypeClass.Number, variable.GetTypeClass());
var val = variable.EvaluateInteger();
Assert.AreEqual(150, val);
}
}
} }
} }

Binary file not shown.