diff --git a/NeoLua.Dbg/NeoLua.Dbg.csproj b/NeoLua.Dbg/NeoLua.Dbg.csproj
index 1e7826f..68e1990 100644
--- a/NeoLua.Dbg/NeoLua.Dbg.csproj
+++ b/NeoLua.Dbg/NeoLua.Dbg.csproj
@@ -4,7 +4,7 @@
Neo.Lua.Dbg
Neo.IronLua
true
- net45;netcoreapp2.1
+ net45;netcoreapp3.1
true
NeoLua.snk
NeoLuaDebug
diff --git a/NeoLua.NuGet/common.targets b/NeoLua.NuGet/common.targets
index 1a1f0f1..22aff57 100644
--- a/NeoLua.NuGet/common.targets
+++ b/NeoLua.NuGet/common.targets
@@ -12,9 +12,9 @@
git
5.3.0.0
- 1.3.14.0
+ 1.4.0.0
-
+ beta.0
^(\d+)\.(\d+)\.(\d+)
$([System.Text.RegularExpressions.Regex]::Match($(FileVersion), $(SimpleVersionPattern)))
diff --git a/NeoLua.Test/ControlStructures.cs b/NeoLua.Test/ControlStructures.cs
index b998312..da8970c 100644
--- a/NeoLua.Test/ControlStructures.cs
+++ b/NeoLua.Test/ControlStructures.cs
@@ -362,6 +362,12 @@ public void Control19()
TestCode(GetLines("Lua.Control19.lua"), 4);
}
+ [TestMethod]
+ public void Control20()
+ {
+ TestCode(GetLines("Lua.Control20.lua"), 500000500000, 500000500001);
+ }
+
[TestMethod]
public void TestVariableAssign01()
{
diff --git a/NeoLua.Test/Lua/Control20.lua b/NeoLua.Test/Lua/Control20.lua
new file mode 100644
index 0000000..64e6d8c
--- /dev/null
+++ b/NeoLua.Test/Lua/Control20.lua
@@ -0,0 +1,8 @@
+local t = {};
+local j = 1;
+for i = 500000500000, 500000500001 do
+ print(tostring(i));
+ t[j] = i;
+ j = j + 1;
+end;
+return t[1], t[2];
\ No newline at end of file
diff --git a/NeoLua.Test/LuaTable.cs b/NeoLua.Test/LuaTable.cs
index e7eb500..ebdd3f1 100644
--- a/NeoLua.Test/LuaTable.cs
+++ b/NeoLua.Test/LuaTable.cs
@@ -3,8 +3,6 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
-using System.Text;
-using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IronLua;
@@ -13,6 +11,45 @@ namespace LuaDLR.Test
[TestClass]
public class LuaTableTests : TestHelper
{
+ #region -- class ObjectInit ---------------------------------------------------
+
+ public const string ObjectInitLua = "";
+
+ public class ObjectInit
+ {
+ private readonly object[] values = new object[10];
+
+ public void TestEvent()
+ {
+ Assert.IsNotNull(Event);
+ }
+
+ public object this[int idx] { get => values[idx]; set => values[idx] = value; }
+
+ public int fieldValue;
+
+ public int Value { get; set; } = 100;
+ public Func Action { get; set; }
+
+ public event EventHandler Event;
+ }
+
+ #endregion
+
+ #region -- class ObjectInitS --------------------------------------------------
+
+ public class ObjectInitS
+ {
+ public ObjectInitS(LuaTable t)
+ {
+ Value = (int)t.GetMemberValue(nameof(Value));
+ }
+
+ public int Value { get; }
+ }
+
+ #endregion
+
#region -- TestMember -------------------------------------------------------------
[TestMethod]
@@ -393,19 +430,83 @@ public void TestMetaTable11()
#region -- TestConvert ------------------------------------------------------------
[TestMethod]
- public void TestConvert01()
+ public void TestConvert01a()
+ => TestCode("return cast(LuaDLR.Test.LuaTypeTests.SubStruct, { Value = 2 }).Value", 2);
+
+ [TestMethod]
+ public void TestConvert01b()
{
- using (Lua l = new Lua())
+ using (var l = new Lua())
{
l.PrintExpressionTree = Console.Out;
var g = l.CreateEnvironment();
- var r = g.DoChunk("return cast(System.Diagnostics.ProcessStartInfo, { FileName = 'Test.exe', Arguments = 'aaa' });", "dummy");
- ProcessStartInfo psi = (ProcessStartInfo)r[0];
- Assert.IsTrue(psi.FileName == "Test.exe");
- Assert.IsTrue(psi.Arguments == "aaa");
+ var r = g.DoChunk("return cast(LuaDLR.Test.LuaTableTests.ObjectInit, { Value = 42, 1, 2, 3 });", "dummy");
+ var o = (ObjectInit)r[0];
+ Assert.AreEqual(o.Value, 42);
+ Assert.AreEqual(o[0], 1);
+ Assert.AreEqual(o[1], 2);
+ Assert.AreEqual(o[2], 3);
}
} // func TestConvert01
+ [TestMethod]
+ public void TestConvert02a()
+ {
+ using (var l = new Lua())
+ {
+ var g = l.CreateEnvironment();
+ var t = (LuaTable)g.DoChunk("return { fieldValue = 42, Value = 23, Action = function() : int return 44 end, Event = function(s, e) : void print('test') end, 1, 2, 3 };", "dummy")[0];
+
+ var o = t.InitObject(true);
+ Assert.AreEqual(o.Value, 23);
+ Assert.AreEqual(o.fieldValue, 42);
+ Assert.AreEqual(o.Action(), 44);
+ o.TestEvent();
+ Assert.AreEqual(o[0], 1);
+ Assert.AreEqual(o[1], 2);
+ Assert.AreEqual(o[2], 3);
+ }
+ }
+
+ [TestMethod]
+ public void TestConvert02b()
+ {
+ using (var l = new Lua())
+ {
+ var g = l.CreateEnvironment();
+ var t = (LuaTable)g.DoChunk("return { fieldValue = 42, err = 23, Value = 23, Action = function() : int return 44 end, Event = function(s, e) : void print('test') end, 1, 2, 3 };", "dummy")[0];
+
+ var o = t.InitObject(false);
+ Assert.AreEqual(o.Value, 23);
+ Assert.AreEqual(o.fieldValue, 42);
+ Assert.AreEqual(o.Action(), 44);
+ o.TestEvent();
+ Assert.AreEqual(o[0], 1);
+ Assert.AreEqual(o[1], 2);
+ Assert.AreEqual(o[2], 3);
+ }
+ }
+
+ [TestMethod]
+ public void TestConvert02c()
+ {
+ using (var l = new Lua())
+ {
+ var g = l.CreateEnvironment();
+ var t = (LuaTable)g.DoChunk("return { err = 23 };", "dummy")[0];
+
+ try
+ {
+ var o = t.InitObject(true);
+ Assert.Fail();
+ }
+ catch (InvalidOperationException e)
+ {
+ Console.WriteLine(e.Message);
+ }
+ }
+ }
+
#endregion
#region -- Length -----------------------------------------------------------------
diff --git a/NeoLua.Test/LuaType.cs b/NeoLua.Test/LuaType.cs
index c8d7651..d6e90cb 100644
--- a/NeoLua.Test/LuaType.cs
+++ b/NeoLua.Test/LuaType.cs
@@ -2,12 +2,11 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
-using System.Linq;
using System.Reflection;
using System.Text;
-using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IronLua;
+using static LuaDLR.Test.LuaTableTests;
namespace LuaDLR.Test
{
@@ -35,6 +34,9 @@ public override int Foo()
public class DataTypeTest
{
public Type DataType;
+
+ public static explicit operator DataTypeTest(LuaTable t)
+ => t.SetObjectMember(new DataTypeTest());
}
public class Graph
@@ -308,14 +310,14 @@ public void TypeTest07()
public void TypeTest08()
{
TestCode(Lines("local t : System.Type = clr.System.Text.StringBuilder;",
- "return t"), typeof(System.Text.StringBuilder));
+ "return t"), typeof(StringBuilder));
}
[TestMethod]
public void TypeTest09()
{
TestCode(Lines("local t : LuaDLR.Test.LuaTypeTests.DataTypeTest = { DataType = clr.System.Text.StringBuilder };",
- "return t.DataType"), typeof(System.Text.StringBuilder));
+ "return t.DataType"), typeof(StringBuilder));
}
[TestMethod]
@@ -497,9 +499,32 @@ public void CtorTest02()
}
[TestMethod]
- public void CtorTest03()
+ public void CtorTest10()
{
- TestCode("return cast(LuaDLR.Test.LuaTypeTests.SubStruct, { Value = 2 }).Value", 2);
+ using (var l = new Lua())
+ {
+ l.PrintExpressionTree = Console.Out;
+ var g = l.CreateEnvironment();
+ var r = g.DoChunk("return clr.LuaDLR.Test.LuaTableTests.ObjectInit(){ Value = 42, 1, 2, 3 };", "dummy");
+ var o = (ObjectInit)r[0];
+ Assert.AreEqual(o.Value, 42);
+ Assert.AreEqual(o[0], 1);
+ Assert.AreEqual(o[1], 2);
+ Assert.AreEqual(o[2], 3);
+ }
+ }
+
+ [TestMethod]
+ public void CtorTest11()
+ {
+ using (var l = new Lua())
+ {
+ l.PrintExpressionTree = Console.Out;
+ var g = l.CreateEnvironment();
+ var r = g.DoChunk("return clr.LuaDLR.Test.LuaTableTests.ObjectInitS{ Value = 42, 1, 2, 3 };", "dummy");
+ var o = (ObjectInitS)r[0];
+ Assert.AreEqual(o.Value, 42);
+ }
}
[TestMethod]
diff --git a/NeoLua.Test/NeoLua.Test.csproj b/NeoLua.Test/NeoLua.Test.csproj
index 4020210..ec26d0a 100644
--- a/NeoLua.Test/NeoLua.Test.csproj
+++ b/NeoLua.Test/NeoLua.Test.csproj
@@ -25,6 +25,7 @@
+
@@ -51,6 +52,7 @@
+
diff --git a/NeoLua.Test/RunLuaTests.cs b/NeoLua.Test/RunLuaTests.cs
index 09949a2..1cee925 100644
--- a/NeoLua.Test/RunLuaTests.cs
+++ b/NeoLua.Test/RunLuaTests.cs
@@ -1,10 +1,7 @@
using System;
-using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IronLua;
diff --git a/NeoLua.Test/Runtime.cs b/NeoLua.Test/Runtime.cs
index bb3fb0b..9fd02e8 100644
--- a/NeoLua.Test/Runtime.cs
+++ b/NeoLua.Test/Runtime.cs
@@ -1,14 +1,10 @@
using System;
using System.Collections.Generic;
-using System.Dynamic;
-using System.Globalization;
using System.IO;
-using System.Linq;
using System.Text;
-using System.Text.RegularExpressions;
-using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IronLua;
+using static LuaDLR.Test.LuaTableTests;
namespace LuaDLR.Test
{
@@ -229,7 +225,7 @@ public void TestToNumber01()
[TestMethod]
public void TestDateTime01()
{
- using (Lua l = new Lua())
+ using (var l = new Lua())
{
dynamic g = l.CreateEnvironment();
@@ -283,7 +279,7 @@ public void TestConvert01()
{
var t = new LuaTable();
t["DataType"] = typeof(StringBuilder);
- var r = Lua.RtConvertValue(t, typeof(LuaDLR.Test.LuaTypeTests.DataTypeTest));
+ var r = Lua.RtConvertValue(t, typeof(LuaTypeTests.DataTypeTest));
Assert.AreEqual(typeof(LuaTypeTests.DataTypeTest), r.GetType());
Assert.AreEqual(typeof(StringBuilder), ((LuaTypeTests.DataTypeTest)r).DataType);
}
@@ -561,7 +557,73 @@ public void Invoke01()
Assert.AreEqual(r[0], "a");
Assert.AreEqual(r[1], null);
}
+
+ [TestMethod]
+ public void Invoke02()
+ {
+ using (var l = new Lua())
+ {
+ var g = new LuaGlobal(l);
+ l.PrintExpressionTree = Console.Out;
+ var o = new ObjectInit();
+ var r = g.DoChunk(Lines("local function f456() return 4,5,6; end; local t = o{ fieldValue = 42, Value = 23, Action = function() : int return 44 end, Event = function(s, e) : void print('test') end, 1, 2, 3, f456() }.Value; return t;"), "dummy", new KeyValuePair("o", o));
+
+ Assert.AreEqual(r[0], 23);
+ Assert.AreEqual(o.Value, 23);
+ Assert.AreEqual(o.fieldValue, 42);
+ Assert.AreEqual(o.Action(), 44);
+ o.TestEvent();
+ Assert.AreEqual(o[0], 1);
+ Assert.AreEqual(o[1], 2);
+ Assert.AreEqual(o[2], 3);
+ Assert.AreEqual(o[3], 4);
+ Assert.AreEqual(o[4], 5);
+ Assert.AreEqual(o[5], 6);
+ }
+ }
+
+ [TestMethod]
+ public void Invoke03()
+ {
+ using (var l = new Lua())
+ {
+ var g = new LuaGlobal(l);
+ l.PrintExpressionTree = Console.Out;
+
+ var o = new ObjectInit();
+ var c = l.CompileChunk(Lines("o{ fieldValue = 42, Value = 23, Action = function() : int return 44 end, Event = function(s, e) : void print('test') end, 1, 2, 3 }"), "dummy", null, new KeyValuePair("o", typeof(object)));
+ c.Run(g, o);
+
+ Assert.AreEqual(o.Value, 23);
+ Assert.AreEqual(o.fieldValue, 42);
+ Assert.AreEqual(o.Action(), 44);
+ o.TestEvent();
+ Assert.AreEqual(o[0], 1);
+ Assert.AreEqual(o[1], 2);
+ Assert.AreEqual(o[2], 3);
+ }
+ }
+
+ [TestMethod]
+ public void Invoke04()
+ {
+ using (var l = new Lua())
+ {
+ var g = new LuaGlobal(l);
+ l.PrintExpressionTree = Console.Out;
+ var o = new ObjectInit();
+ g.DoChunk(Lines("local t : table = { fieldValue = 42, Value = 23, Action = function() : int return 44 end, Event = function(s, e) : void print('test') end, 1, 2, 3 }; o(t)"), "dummy", new KeyValuePair("o", o));
+
+ Assert.AreEqual(o.Value, 23);
+ Assert.AreEqual(o.fieldValue, 42);
+ Assert.AreEqual(o.Action(), 44);
+ o.TestEvent();
+ Assert.AreEqual(o[0], 1);
+ Assert.AreEqual(o[1], 2);
+ Assert.AreEqual(o[2], 3);
+ }
+ }
}
- } //class Runtime
+} //class Runtime
diff --git a/NeoLua/Lua.Binder.cs b/NeoLua/Lua.Binder.cs
index 8ee0086..cdf16ea 100644
--- a/NeoLua/Lua.Binder.cs
+++ b/NeoLua/Lua.Binder.cs
@@ -284,15 +284,8 @@ public override DynamicMetaObject FallbackInvoke(DynamicMetaObject target, Dynam
{
var restrictions = GetMethodSignatureRestriction(target, args);
Expression expr;
- var invokeTarget = target.Value as Delegate;
- if (invokeTarget == null)
- {
- if (errorSuggestion != null)
- return errorSuggestion;
- expr = ThrowExpression(LuaEmitException.GetMessageText(LuaEmitException.InvokeNoDelegate, target.LimitType.Name), typeof(object));
- }
- else
+ if (target.Value is Delegate invokeTarget)
{
var methodParameters = invokeTarget.GetMethodInfo().GetParameters();
var parameters = (ParameterInfo[])null;
@@ -333,6 +326,16 @@ public override DynamicMetaObject FallbackInvoke(DynamicMetaObject target, Dynam
expr = ThrowExpression(e.Message, ReturnType);
}
}
+ else if (IsTypeInitializable(target.LimitType) && args.Length == 1 && args[0].LimitType == typeof(LuaTable))
+ {
+ expr = Expression.Call(EnsureType(args[0].Expression, typeof(LuaTable)), TableSetObjectMemberMethodInfo, EnsureType(target.Expression, typeof(object)), Expression.Constant(false, typeof(bool)));
+ }
+ else
+ {
+ if (errorSuggestion != null)
+ return errorSuggestion;
+ expr = ThrowExpression(LuaEmitException.GetMessageText(LuaEmitException.InvokeNoDelegate, target.LimitType.Name), typeof(object));
+ }
return new DynamicMetaObject(expr, restrictions);
}
@@ -852,6 +855,9 @@ internal static Expression EnsureType(Expression expr, Type exprType, Type retur
return EnsureType(expr, returnType, forResult);
} // func Expression
+ internal static bool IsTypeInitializable(Type type)
+ => !type.IsPrimitive && type != typeof(string);
+
#endregion
} // class Lua
diff --git a/NeoLua/Lua.Runtime.cs b/NeoLua/Lua.Runtime.cs
index cca3927..fbe46ea 100644
--- a/NeoLua/Lua.Runtime.cs
+++ b/NeoLua/Lua.Runtime.cs
@@ -185,7 +185,7 @@ static Lua()
TableDefineMethodLightMethodInfo = tiLuaTable.FindDeclaredMethod(nameof(LuaTable.DefineMethodLight), ReflectionFlag.None, typeof(string), typeof(Delegate));
TableGetCallMemberMethodInfo = tiLuaTable.FindDeclaredMethod(nameof(LuaTable.GetCallMember), ReflectionFlag.NoArguments);
- TableSetObjectMemberMethodInfo = tiLuaTable.FindDeclaredMethod(nameof(LuaTable.SetObjectMember), ReflectionFlag.None, typeof(object));
+ TableSetObjectMemberMethodInfo = tiLuaTable.FindDeclaredMethod(nameof(LuaTable.SetObjectMember), ReflectionFlag.None, typeof(object), typeof(bool));
TableEntriesFieldInfo = tiLuaTable.FindDeclaredField("entries", ReflectionFlag.None);
TablePropertyChangedMethodInfo = tiLuaTable.FindDeclaredMethod("OnPropertyChanged", ReflectionFlag.None, typeof(string));
@@ -256,7 +256,7 @@ static Lua()
GetResultValuesMethodInfo = tiLua.FindDeclaredMethod(nameof(Lua.RtGetResultValues), ReflectionFlag.None, typeof(LuaResult), typeof(int), typeof(Type));
CombineArrayWithResultMethodInfo = tiLua.FindDeclaredMethod(nameof(Lua.RtCombineArrayWithResult), ReflectionFlag.None, typeof(Array), typeof(LuaResult), typeof(Type));
ConvertArrayMethodInfo = tiLua.FindDeclaredMethod(nameof(Lua.RtConvertArray), ReflectionFlag.None, typeof(Array), typeof(Type));
- TableSetObjectsMethod = tiLua.FindDeclaredMethod(nameof(Lua.RtTableSetObjects), ReflectionFlag.None, typeof(LuaTable), typeof(object), typeof(int));
+ TableSetObjectsMethod = tiLua.FindDeclaredMethod(nameof(Lua.RtTableSetObjects), ReflectionFlag.None, typeof(object), typeof(object), typeof(int));
ConcatStringMethodInfo = tiLua.FindDeclaredMethod(nameof(Lua.RtConcatString), ReflectionFlag.None | ReflectionFlag.NoArguments);
ConvertDelegateMethodInfo = tiLua.FindDeclaredMethod(nameof(Lua.RtConvertDelegate), ReflectionFlag.None | ReflectionFlag.NoArguments);
InitArray1MethodInfo = tiLua.FindDeclaredMethod(nameof(Lua.RtInitArray), ReflectionFlag.None, typeof(Type), typeof(object));
@@ -1219,7 +1219,7 @@ public static int RtLength(object v)
#region -- RtTableSetObjects --------------------------------------------------
- internal static object RtTableSetObjects(LuaTable t, object value, int startIndex)
+ internal static LuaTable RtTableSetObjects(LuaTable t, object value, int startIndex)
{
if (value is LuaResult r)
{
@@ -1231,6 +1231,34 @@ internal static object RtTableSetObjects(LuaTable t, object value, int startInde
return t;
} // func RtTableSetObjects
+ internal static object RtTableSetObjects(object o, object value, int startIndex)
+ {
+ if (o is LuaTable t)
+ return RtTableSetObjects(t, value, startIndex);
+ else
+ {
+ var type = o.GetType();
+ var indexProperty = type.GetRuntimeProperties().Where(LuaTable.IsIndexSetter).FirstOrDefault()
+ ?? throw new LuaEmitException(LuaEmitException.IndexNotFound, type.Name);
+
+ var idx = new object[1];
+ if (value is LuaResult r)
+ {
+ for (var i = 0; i < r.Count; i++)
+ {
+ idx[0] = startIndex++;
+ indexProperty.SetValue(o, r[i], idx);
+ }
+ }
+ else if (value != null)
+ {
+ idx[0] = startIndex;
+ indexProperty.SetValue(o, value, idx);
+ }
+ }
+ return o;
+ } // func RtTableSetObjects
+
#endregion
#region -- RtInvoke -----------------------------------------------------------
diff --git a/NeoLua/Lua.cs b/NeoLua/Lua.cs
index 2635d4c..838007d 100644
--- a/NeoLua/Lua.cs
+++ b/NeoLua/Lua.cs
@@ -285,7 +285,7 @@ public static bool IsConstantScript(ILuaLexer code)
internal LuaChunk CompileChunkCore(ILuaLexer lex, LuaCompileOptions options, IEnumerable> args)
{
if (options == null)
- options = new LuaCompileOptions();
+ options = DefaultCompileOptions ?? new LuaCompileOptions();
var registerMethods = options.DebugEngine != null && (options.DebugEngine.Level & LuaDebugLevel.RegisterMethods) == LuaDebugLevel.RegisterMethods;
if (registerMethods)
@@ -323,7 +323,7 @@ public Delegate CreateLambda(string name, string code, Type delegateType, Type r
{
using (var l = LuaLexer.Create(name, new StringReader(code)))
{
- var expr = Parser.ParseChunk(this, new LuaCompileOptions(), false, l, delegateType, returnType, arguments);
+ var expr = Parser.ParseChunk(this, DefaultCompileOptions ?? new LuaCompileOptions(), false, l, delegateType, returnType, arguments);
if (printExpressionTree != null)
{
@@ -547,7 +547,15 @@ public static Version Version
}
} // prop Version
-#if !NETSTANDARD2_0 && !NETCOREAPP2_1 && !NET5_0
+ ///
+ /// Get/Set the default compile options
+ ///
+ public LuaCompileOptions DefaultCompileOptions
+ {
+ get; set;
+ } = new LuaCompileOptions();
+
+#if !NETSTANDARD2_1 && !NETCOREAPP1_0_OR_GREATER
/// Stack trace compile options.
public static LuaCompileOptions StackTraceCompileOptions { get; } = new LuaCompileOptions { DebugEngine = LuaStackTraceDebugger.Default };
#endif
diff --git a/NeoLua/LuaChunk.cs b/NeoLua/LuaChunk.cs
index 157aaba..47d350a 100644
--- a/NeoLua/LuaChunk.cs
+++ b/NeoLua/LuaChunk.cs
@@ -72,7 +72,7 @@ public LuaResult Run(LuaTable env, params object[] callArgs)
try
{
var r = chunk.DynamicInvoke(args);
- return r is LuaResult ? (LuaResult)r : new LuaResult(r);
+ return r is LuaResult t ? t : new LuaResult(r);
}
catch (TargetInvocationException e)
{
diff --git a/NeoLua/LuaEmit.cs b/NeoLua/LuaEmit.cs
index 1bdbf8c..0b8a830 100644
--- a/NeoLua/LuaEmit.cs
+++ b/NeoLua/LuaEmit.cs
@@ -1798,6 +1798,13 @@ public static LuaTrySetMemberReturn TrySetMember(Expression target, Type targetT
result = Expression.Assign(Expression.Field(target != null ? Lua.EnsureType(target, targetType) : null, fieldInfo), set(fieldInfo.FieldType));
return LuaTrySetMemberReturn.ValidExpression;
}
+ else if (memberInfo is EventInfo eventInfo)
+ {
+ result = target == null
+ ? Expression.Call(eventInfo.AddMethod, set(eventInfo.EventHandlerType))
+ : Expression.Call(target, eventInfo.AddMethod, set(eventInfo.EventHandlerType));
+ return LuaTrySetMemberReturn.ValidExpression;
+ }
else
{
result = null;
@@ -1831,7 +1838,7 @@ public static Expression SetIndex(Lua runtime, TARG instance, TARG[] argum
if (isParse && IsDynamicType(instanceType))
{
return DynamicExpression.Dynamic(runtime.GetSetIndexMember(new CallInfo(arguments.Length)), typeof(object),
- CreateDynamicArgs(getExpr(instance), instanceType, arguments, setTo, getExpr, getType)
+ CreateDynamicArgs(getExpr(instance), instanceType, arguments, setTo, getExpr, getType)
);
}
@@ -2769,7 +2776,7 @@ internal static Expression InvokeMethod(Lua lua, MethodInfo methodInfo, TA
emitCall = convertedArguments => Expression.Call(null, methodInfo, convertedArguments);
}
- result = BindParameter(lua,
+ result = BindParameter(lua,
emitCall,
methodInfo.GetParameters(),
callInfo,
diff --git a/NeoLua/LuaGlobal.cs b/NeoLua/LuaGlobal.cs
index 9c8fd93..9c2325a 100644
--- a/NeoLua/LuaGlobal.cs
+++ b/NeoLua/LuaGlobal.cs
@@ -29,6 +29,8 @@ public class LuaGlobal : LuaTable
public LuaGlobal(Lua lua)
{
this.lua = lua ?? throw new ArgumentNullException(nameof(lua));
+
+ InitializeLibraries();
} // ctor
/// Redirects the invoke binder to the script manager/compiler.
@@ -37,14 +39,119 @@ public LuaGlobal(Lua lua)
protected override CallSiteBinder GetInvokeBinder(CallInfo callInfo)
=> lua.GetInvokeBinder(callInfo);
- #endregion
-
- #region -- void RegisterPackage -----------------------------------------------
-
- /// Registers a type as an library.
- ///
- ///
- public void RegisterPackage(string name, Type type)
+ #endregion
+
+ #region -- Initialize libraries-------------------------------------------------
+
+ ///
+ /// Initialize the library tables with the appropriate functions (and constants)
+ ///
+ private void InitializeLibraries()
+ {
+ InitializeMathLibrary();
+ InitializeStringLibrary();
+ InitializeTableLibrary();
+ }
+
+ #region --- Math Lib ----------------------------------------------------------
+ private void InitializeMathLibrary()
+ {
+ LuaTable math = new LuaTable();
+ this["math"] = math;
+
+ math["huge"] = LuaLibraryMath.huge;
+ math["pi"] = LuaLibraryMath.pi;
+ math["e"] = LuaLibraryMath.e;
+ math["mininteger"] = LuaLibraryMath.mininteger;
+ math["maxinteger"] = LuaLibraryMath.maxinteger;
+
+ math["abs"] = new Func(LuaLibraryMath.abs);
+ math["acos"] = new Func(LuaLibraryMath.acos);
+ math["asin"] = new Func(LuaLibraryMath.asin);
+ math["atan"] = new Func(LuaLibraryMath.atan);
+ math["atan2"] = new Func(LuaLibraryMath.atan2);
+ math["ceil"] = new Func(LuaLibraryMath.ceil);
+ math["cos"] = new Func(LuaLibraryMath.cos);
+ math["cosh"] = new Func(LuaLibraryMath.cosh);
+ math["deg"] = new Func(LuaLibraryMath.deg);
+ math["exp"] = new Func(LuaLibraryMath.exp);
+ math["floor"] = new Func(LuaLibraryMath.floor);
+ math["fmod"] = new Func(LuaLibraryMath.fmod);
+ math["frexp"] = new Func(LuaLibraryMath.frexp);
+ math["ldexp"] = new Func(LuaLibraryMath.ldexp);
+ math["log"] = new Func(LuaLibraryMath.log);
+ math["max"] = new Func(LuaLibraryMath.max);
+ math["min"] = new Func(LuaLibraryMath.min);
+ math["modf"] = new Func(LuaLibraryMath.modf);
+ math["pow"] = new Func(LuaLibraryMath.pow);
+ math["rad"] = new Func(LuaLibraryMath.rad);
+ math["random"] = new Func