diff --git a/PorygonSharp/EvalValue.cs b/PorygonSharp/EvalValue.cs index 2d27b10..2742c8d 100644 --- a/PorygonSharp/EvalValue.cs +++ b/PorygonSharp/EvalValue.cs @@ -12,6 +12,43 @@ namespace PorygonSharp _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() { if (_handle != IntPtr.Zero) @@ -47,6 +84,11 @@ namespace PorygonSharp return Marshal.PtrToStringUTF8(ptr); } + internal IntPtr GetPointer() + { + return _handle; + } + [DllImport("libPorygonLang", EntryPoint = "GetEvalValueTypeClass", CallingConvention = CallingConvention.Cdecl)] private static extern int GetTypeClass(IntPtr ptr); [DllImport("libPorygonLang", EntryPoint = "GetEvalValueType", CallingConvention = CallingConvention.Cdecl)] @@ -60,5 +102,13 @@ namespace PorygonSharp [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueString",CallingConvention = CallingConvention.Cdecl)] 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); } } \ No newline at end of file diff --git a/PorygonSharp/Script.cs b/PorygonSharp/Script.cs index 9d10360..223399b 100644 --- a/PorygonSharp/Script.cs +++ b/PorygonSharp/Script.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using System.Text; using PorygonSharp.DiagnosticHandling; @@ -56,6 +58,18 @@ namespace PorygonSharp 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)] private static extern IntPtr Create(StringBuilder s); [DllImport("libPorygonLang", EntryPoint = "EvaluateScript", CallingConvention = CallingConvention.Cdecl)] @@ -66,5 +80,11 @@ namespace PorygonSharp private static extern bool HasVariable(IntPtr script, string key); [DllImport("libPorygonLang", EntryPoint = "GetVariable", CallingConvention = CallingConvention.Cdecl)] 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); + } } \ No newline at end of file diff --git a/PorygonSharpTests/UnitTest1.cs b/PorygonSharpTests/UnitTest1.cs index 2731d38..4535bbf 100644 --- a/PorygonSharpTests/UnitTest1.cs +++ b/PorygonSharpTests/UnitTest1.cs @@ -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); + } + } + } } \ No newline at end of file diff --git a/libPorygonLang.so b/libPorygonLang.so index 76c34b0..e2fa29f 100755 Binary files a/libPorygonLang.so and b/libPorygonLang.so differ