Browse Source

Add: implicit operator

AnnulusGames 1 year ago
parent
commit
ee8131df30

+ 1 - 1
sandbox/ConsoleApp1/Program.cs

@@ -7,7 +7,7 @@ using Lua.Standard;
 var state = LuaState.Create();
 state.OpenStandardLibraries();
 
-state.Environment["vec3"] = new LVec3().AsLuaValue();
+state.Environment["vec3"] = new LVec3();
 
 try
 {

+ 6 - 1
src/Lua.SourceGenerator/LuaObjectGenerator.Emit.cs

@@ -75,7 +75,7 @@ partial class LuaObjectGenerator
 
             // add ILuaUserData impl
             builder.Append(
-"""
+$$"""
         global::Lua.LuaTable? global::Lua.ILuaUserData.Metatable
         {
             get
@@ -94,6 +94,11 @@ partial class LuaObjectGenerator
         }
         static global::Lua.LuaTable? __metatable;
 
+        public static implicit operator global::Lua.LuaValue({{typeMetadata.FullTypeName}} value)
+        {
+            return new(value);
+        }
+
 """, false);
 
             if (!TryEmitMethods(typeMetadata, builder, context))

+ 8 - 0
src/Lua/LuaObjectGenerateOptions.cs

@@ -0,0 +1,8 @@
+namespace Lua;
+
+[Flags]
+public enum LuaObjectGenerateOptions
+{
+    None = 0,
+    
+}

+ 0 - 9
src/Lua/LuaUserDataExtensions.cs

@@ -1,9 +0,0 @@
-namespace Lua;
-
-public static class LuaUserDataExtensions
-{
-    public static LuaValue AsLuaValue(this ILuaUserData userData)
-    {
-        return new(userData);
-    }
-}

+ 11 - 11
src/Lua/Standard/IOLibrary.cs

@@ -13,9 +13,9 @@ public static class IOLibrary
             io[func.Name] = func;
         }
 
-        io["stdio"] = new FileHandle(Console.OpenStandardInput()).AsLuaValue();
-        io["stdout"] = new FileHandle(Console.OpenStandardOutput()).AsLuaValue();
-        io["stderr"] = new FileHandle(Console.OpenStandardError()).AsLuaValue();
+        io["stdio"] = new LuaValue(new FileHandle(Console.OpenStandardInput()));
+        io["stdout"] = new LuaValue(new FileHandle(Console.OpenStandardOutput()));
+        io["stderr"] = new LuaValue(new FileHandle(Console.OpenStandardError()));
 
         state.Environment["io"] = io;
         state.LoadedModules["io"] = io;
@@ -86,16 +86,16 @@ public static class IOLibrary
         var arg = context.Arguments[0];
         if (arg.TryRead<FileHandle>(out var file))
         {
-            io["stdio"] = file.AsLuaValue();
-            buffer.Span[0] = file.AsLuaValue();
+            io["stdio"] = new(file);
+            buffer.Span[0] = new(file);
             return new(1);
         }
         else
         {
             var stream = File.Open(arg.ToString()!, FileMode.Open, FileAccess.ReadWrite);
             var handle = new FileHandle(stream);
-            io["stdio"] = handle.AsLuaValue();
-            buffer.Span[0] = handle.AsLuaValue();
+            io["stdio"] = new(handle);
+            buffer.Span[0] = new(handle);
             return new(1);
         }
     }
@@ -164,16 +164,16 @@ public static class IOLibrary
         var arg = context.Arguments[0];
         if (arg.TryRead<FileHandle>(out var file))
         {
-            io["stdout"] = file.AsLuaValue();
-            buffer.Span[0] = file.AsLuaValue();
+            io["stdout"] = new(file);
+            buffer.Span[0] = new(file);
             return new(1);
         }
         else
         {
             var stream = File.Open(arg.ToString()!, FileMode.Open, FileAccess.ReadWrite);
             var handle = new FileHandle(stream);
-            io["stdout"] = handle.AsLuaValue();
-            buffer.Span[0] = handle.AsLuaValue();
+            io["stdout"] = new(handle);
+            buffer.Span[0] = new(handle);
             return new(1);
         }
     }

+ 2 - 2
src/Lua/Standard/Internal/IOHelper.cs

@@ -25,7 +25,7 @@ internal static class IOHelper
         try
         {
             var stream = File.Open(fileName, fileMode, fileAccess);
-            buffer.Span[0] = new FileHandle(stream).AsLuaValue();
+            buffer.Span[0] = new LuaValue(new FileHandle(stream));
             return 1;
         }
         catch (IOException ex)
@@ -76,7 +76,7 @@ internal static class IOHelper
             return 3;
         }
 
-        buffer.Span[0] = file.AsLuaValue();
+        buffer.Span[0] = new(file);
         return 1;
     }
 

+ 2 - 2
src/Lua/Standard/MathematicsLibrary.cs

@@ -4,7 +4,7 @@ public static class MathematicsLibrary
 {
     public static void OpenMathLibrary(this LuaState state)
     {
-        state.Environment[RandomInstanceKey] = new RandomUserData(new Random()).AsLuaValue();
+        state.Environment[RandomInstanceKey] = new(new RandomUserData(new Random()));
 
         var math = new LuaTable(0, Functions.Length);
         foreach (var func in Functions)
@@ -258,7 +258,7 @@ public static class MathematicsLibrary
     public static ValueTask<int> RandomSeed(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
     {
         var arg0 = context.GetArgument<double>(0);
-        context.State.Environment[RandomInstanceKey] = new RandomUserData(new Random((int)BitConverter.DoubleToInt64Bits(arg0))).AsLuaValue();
+        context.State.Environment[RandomInstanceKey] = new(new RandomUserData(new Random((int)BitConverter.DoubleToInt64Bits(arg0))));
         return new(0);
     }