diff --git a/PorygonSharp/CoreSetup.cs b/PorygonSharp/CoreSetup.cs
index f764f4d..83297bc 100644
--- a/PorygonSharp/CoreSetup.cs
+++ b/PorygonSharp/CoreSetup.cs
@@ -27,7 +27,7 @@ namespace PorygonSharp
Print(message);
}
- [DllImport("libPorygonLang", EntryPoint = "SetPrintFunc", CallingConvention = CallingConvention.Cdecl)]
+ [DllImport("libPorygonLang", EntryPoint = "SetDefaultPrintFunc", CallingConvention = CallingConvention.Cdecl)]
private static extern double InternalSetPrintFunc(IntPtr ptr);
}
}
\ No newline at end of file
diff --git a/PorygonSharp/EvalValue.cs b/PorygonSharp/EvalValues/EvalValue.cs
similarity index 55%
rename from PorygonSharp/EvalValue.cs
rename to PorygonSharp/EvalValues/EvalValue.cs
index 97394ac..e9289c6 100644
--- a/PorygonSharp/EvalValue.cs
+++ b/PorygonSharp/EvalValues/EvalValue.cs
@@ -1,9 +1,8 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
-using PorygonSharp.UserData;
-namespace PorygonSharp
+namespace PorygonSharp.EvalValues
{
public class EvalValue : IDisposable
{
@@ -14,48 +13,7 @@ namespace PorygonSharp
_handle = handle;
}
- public static EvalValue CreateValue(object o)
- {
- var type = o.GetType();
- var typeCode = Type.GetTypeCode(type);
- switch (typeCode)
- {
- case TypeCode.Boolean:
- return new EvalValue(CreateBoolEvalValue((bool)o));
- case TypeCode.Byte:
- case TypeCode.SByte:
- case TypeCode.Int16:
- case TypeCode.Int32:
- case TypeCode.Int64:
- case TypeCode.UInt16:
- case TypeCode.UInt32:
- case TypeCode.UInt64:
- return new EvalValue(CreateIntegerEvalValue(Convert.ToInt64(o)));
- case TypeCode.Char:
- case TypeCode.String:
- return new EvalValue(CreateStringEvalValue(o.ToString()));
- case TypeCode.Decimal:
- case TypeCode.Double:
- case TypeCode.Single:
- return new EvalValue(CreateFloatEvalValue(Convert.ToDouble(o)));
- case TypeCode.Object:
- var typeHash = UserDataHandler.GetTypeId(type);
- var handle = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection);
- return new EvalValue(CreateUserDataEvalValue(typeHash, GCHandle.ToIntPtr(handle)));
- case TypeCode.DateTime:
- case TypeCode.DBNull:
- case TypeCode.Empty:
- default:
- throw new Exception($"Type {type} is not currently available as EvalValue");
- }
- }
-
- public static EvalValue FunctionEvalValue(IntPtr func, IntPtr parent)
- {
- return new EvalValue(CreateFunctionEvalValue(func, parent));
- }
-
-
+
public void Dispose()
{
if (_handle != IntPtr.Zero)
@@ -119,9 +77,6 @@ namespace PorygonSharp
return EvaluateString();
case TypeClass.UserData:
return EvaluateGenericObject();
- case TypeClass.Function:
- case TypeClass.Table:
- case TypeClass.Error:
default:
throw new ArgumentOutOfRangeException();
}
@@ -150,19 +105,5 @@ namespace PorygonSharp
[DllImport("libPorygonLang", EntryPoint = "EvaluateUserDataObj",CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr EvaluateUserDataObj(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);
- [DllImport("libPorygonLang", EntryPoint = "CreateUserDataEvalValue",CallingConvention = CallingConvention.Cdecl)]
- private static extern IntPtr CreateUserDataEvalValue(uint typeHash, IntPtr obj);
-
- [DllImport("libPorygonLang", EntryPoint = "CreateFunctionEvalValue",CallingConvention = CallingConvention.Cdecl)]
- private static extern IntPtr CreateFunctionEvalValue(IntPtr func, IntPtr parent);
-
}
}
\ No newline at end of file
diff --git a/PorygonSharp/EvalValues/EvalValueCreator.cs b/PorygonSharp/EvalValues/EvalValueCreator.cs
new file mode 100644
index 0000000..559d9fd
--- /dev/null
+++ b/PorygonSharp/EvalValues/EvalValueCreator.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+using PorygonSharp.UserData;
+
+namespace PorygonSharp.EvalValues
+{
+ public static class EvalValueCreator
+ {
+ public static EvalValue CreateValue(object o)
+ {
+ var type = o.GetType();
+ var typeCode = Type.GetTypeCode(type);
+ switch (typeCode)
+ {
+ case TypeCode.Boolean:
+ return new EvalValue(CreateBoolEvalValue((bool)o));
+ case TypeCode.Byte:
+ case TypeCode.SByte:
+ case TypeCode.Int16:
+ case TypeCode.Int32:
+ case TypeCode.Int64:
+ case TypeCode.UInt16:
+ case TypeCode.UInt32:
+ case TypeCode.UInt64:
+ return new EvalValue(CreateIntegerEvalValue(Convert.ToInt64(o)));
+ case TypeCode.Char:
+ case TypeCode.String:
+ return new EvalValue(CreateStringEvalValue(o.ToString()));
+ case TypeCode.Decimal:
+ case TypeCode.Double:
+ case TypeCode.Single:
+ return new EvalValue(CreateFloatEvalValue(Convert.ToDouble(o)));
+ case TypeCode.Object:
+ if (typeof(IList).IsAssignableFrom(type))
+ return CreateListEvalValue((IList)o, type);
+ var typeHash = UserDataHandler.GetTypeId(type);
+ var handle = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection);
+ return new EvalValue(CreateUserDataEvalValue(typeHash, GCHandle.ToIntPtr(handle)));
+ default:
+ throw new Exception($"Type {type} is not currently available as EvalValue");
+ }
+ }
+
+ public static EvalValue FunctionEvalValue(IntPtr func, IntPtr parent)
+ {
+ return new EvalValue(CreateFunctionEvalValue(func, parent));
+ }
+
+ private static EvalValue CreateListEvalValue(IList collection, Type type)
+ {
+ var helper = new ListEvalValue(collection, type);
+ var ptr = CreateCollectionValue(IntPtr.Zero, helper.ParentCollection, helper.Getter, helper.Setter,
+ helper.GetIterator);
+ return new EvalValue(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);
+ [DllImport("libPorygonLang", EntryPoint = "CreateUserDataEvalValue",CallingConvention = CallingConvention.Cdecl)]
+ private static extern IntPtr CreateUserDataEvalValue(uint typeHash, IntPtr obj);
+
+ [DllImport("libPorygonLang", EntryPoint = "CreateFunctionEvalValue",CallingConvention = CallingConvention.Cdecl)]
+ private static extern IntPtr CreateFunctionEvalValue(IntPtr func, IntPtr parent);
+
+ [DllImport("libPorygonLang", EntryPoint = "CreateCollectionValue", CallingConvention = CallingConvention.Cdecl)]
+ private static extern IntPtr CreateCollectionValue(IntPtr type, IntPtr parent, IntPtr getter, IntPtr setter,
+ IntPtr iterator);
+ }
+}
\ No newline at end of file
diff --git a/PorygonSharp/EvalValues/ListEvalValue.cs b/PorygonSharp/EvalValues/ListEvalValue.cs
new file mode 100644
index 0000000..419847d
--- /dev/null
+++ b/PorygonSharp/EvalValues/ListEvalValue.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Collections;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+
+namespace PorygonSharp.EvalValues
+{
+ internal class ListEvalValue
+ {
+ private delegate IntPtr GetterDelegate(IntPtr parent, IntPtr index);
+ private delegate void SetterDelegate(IntPtr parent, IntPtr index, IntPtr value);
+ public delegate IntPtr GetIteratorDelegate(IntPtr parent);
+
+ public readonly IntPtr ParentCollection;
+ public readonly IntPtr Getter;
+ public readonly IntPtr Setter;
+ public readonly IntPtr GetIterator;
+
+ public ListEvalValue(IList list, Type type)
+ {
+ var valueType = type.IsArray ? type.GetElementType() : type.GetGenericArguments()[0];
+ if (valueType == null)
+ throw new ArgumentNullException();
+
+ var parentHandle = GCHandle.Alloc(list, GCHandleType.WeakTrackResurrection);
+ ParentCollection = GCHandle.ToIntPtr(parentHandle);
+ Getter = Marshal.GetFunctionPointerForDelegate(new GetterDelegate((parent, index) =>
+ {
+ var val = new EvalValue(index).EvaluateInteger();
+ return EvalValueCreator.CreateValue(list[(int) val - 1]).GetPointer();
+ }));
+ Setter = Marshal.GetFunctionPointerForDelegate(new SetterDelegate((parent, index, value) =>
+ {
+ var key = new EvalValue(index).EvaluateInteger();
+ var val = new EvalValue(value).GetObjectValue();
+ list[(int) key - 1] = Convert.ChangeType(val, valueType);
+ }));
+ GetIterator =
+ Marshal.GetFunctionPointerForDelegate(new GetIteratorDelegate(parent =>
+ ListIterator.CreateListIterator(list)));
+ }
+ }
+}
\ No newline at end of file
diff --git a/PorygonSharp/EvalValues/ListIterator.cs b/PorygonSharp/EvalValues/ListIterator.cs
new file mode 100644
index 0000000..203b9e5
--- /dev/null
+++ b/PorygonSharp/EvalValues/ListIterator.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections;
+using System.Runtime.InteropServices;
+
+namespace PorygonSharp.EvalValues
+{
+ public static class ListIterator
+ {
+ public static IntPtr CreateListIterator(IList list)
+ {
+ return CreateCollectionRangeIterator(0, list.Count);
+ }
+
+ [DllImport("libPorygonLang", EntryPoint = "CreateCollectionRangeIterator", CallingConvention = CallingConvention.Cdecl)]
+ private static extern IntPtr CreateCollectionRangeIterator(int start, int end);
+ }
+}
\ No newline at end of file
diff --git a/PorygonSharp/EvaluateResult.cs b/PorygonSharp/EvaluateResult.cs
index d63afe2..dc9e151 100644
--- a/PorygonSharp/EvaluateResult.cs
+++ b/PorygonSharp/EvaluateResult.cs
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
+using PorygonSharp.EvalValues;
namespace PorygonSharp
{
@@ -9,7 +10,7 @@ namespace PorygonSharp
private struct EvaluateResultInternal
{
public readonly IntPtr Value;
- public readonly byte Result;
+ public readonly bool Result;
}
private readonly IntPtr _ptr;
@@ -22,12 +23,12 @@ namespace PorygonSharp
public EvalValue GetValue()
{
- return !IsSuccess() ? null : new EvalValue(_internal.Value);
+ return new EvalValue(_internal.Value);
}
public bool IsSuccess()
{
- return _internal.Result == 0;
+ return !_internal.Result;
}
public string GetError()
diff --git a/PorygonSharp/PorygonSharp.csproj b/PorygonSharp/PorygonSharp.csproj
index e306e6a..e67c354 100644
--- a/PorygonSharp/PorygonSharp.csproj
+++ b/PorygonSharp/PorygonSharp.csproj
@@ -5,4 +5,10 @@
true
+
+
+ PreserveNewest
+
+
+
diff --git a/PorygonSharp/Script.cs b/PorygonSharp/Script.cs
index 54f843b..fd2cdc2 100644
--- a/PorygonSharp/Script.cs
+++ b/PorygonSharp/Script.cs
@@ -1,10 +1,9 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
-using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
-using System.Security;
using PorygonSharp.DiagnosticHandling;
+using PorygonSharp.EvalValues;
namespace PorygonSharp
{
@@ -13,8 +12,10 @@ namespace PorygonSharp
{
private readonly IntPtr _evaluator;
private readonly IntPtr _scriptVariables;
+ private readonly IntPtr _scriptTypes;
private readonly SharedPointer