| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- using System.Globalization;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using Lua.Internal;
- using Lua.Runtime;
- namespace Lua;
- public enum LuaValueType : byte
- {
- Nil,
- Boolean,
- String,
- Number,
- Function,
- Thread,
- LightUserData,
- UserData,
- Table
- }
- [StructLayout(LayoutKind.Auto)]
- public readonly struct LuaValue : IEquatable<LuaValue>
- {
- public static readonly LuaValue Nil = default;
- public readonly LuaValueType Type;
- readonly double value;
- readonly object? referenceValue;
- internal LuaValue(LuaValueType type, double value, object? referenceValue)
- {
- Type = type;
- this.value = value;
- this.referenceValue = referenceValue;
- }
- public bool TryRead<T>(out T result)
- {
- var t = typeof(T);
- switch (Type)
- {
- case LuaValueType.Number:
- if (t == typeof(float))
- {
- var v = (float)value;
- result = Unsafe.As<float, T>(ref v);
- return true;
- }
- else if (t == typeof(double))
- {
- var v = value;
- result = Unsafe.As<double, T>(ref v);
- return true;
- }
- else if (t == typeof(int))
- {
- if (!MathEx.IsInteger(value))
- {
- break;
- }
- var v = (int)value;
- result = Unsafe.As<int, T>(ref v);
- return true;
- }
- else if (t == typeof(long))
- {
- if (!MathEx.IsInteger(value))
- {
- break;
- }
- var v = (long)value;
- result = Unsafe.As<long, T>(ref v);
- return true;
- }
- else if (t == typeof(object))
- {
- result = (T)(object)value;
- return true;
- }
- else
- {
- break;
- }
- case LuaValueType.Boolean:
- if (t == typeof(bool))
- {
- var v = value != 0;
- result = Unsafe.As<bool, T>(ref v);
- return true;
- }
- else if (t == typeof(object))
- {
- result = (T)(object)value;
- return true;
- }
- else
- {
- break;
- }
- case LuaValueType.String:
- if (t == typeof(string))
- {
- var v = referenceValue!;
- result = Unsafe.As<object, T>(ref v);
- return true;
- }
- else if (t == typeof(double))
- {
- result = default!;
- return TryParseToDouble(out Unsafe.As<T, double>(ref result));
- }
- else if (t == typeof(object))
- {
- result = (T)referenceValue!;
- return true;
- }
- else
- {
- break;
- }
- case LuaValueType.Function:
- if (t == typeof(LuaFunction) || t.IsSubclassOf(typeof(LuaFunction)))
- {
- var v = referenceValue!;
- result = Unsafe.As<object, T>(ref v);
- return true;
- }
- else if (t == typeof(object))
- {
- result = (T)referenceValue!;
- return true;
- }
- else
- {
- break;
- }
- case LuaValueType.Thread:
- if (t == typeof(LuaThread))
- {
- var v = referenceValue!;
- result = Unsafe.As<object, T>(ref v);
- return true;
- }
- else if (t == typeof(object))
- {
- result = (T)referenceValue!;
- return true;
- }
- else
- {
- break;
- }
- case LuaValueType.LightUserData:
- {
- if (referenceValue is T tValue)
- {
- result = tValue;
- return true;
- }
- break;
- }
- case LuaValueType.UserData:
- if (t == typeof(ILuaUserData) || typeof(ILuaUserData).IsAssignableFrom(t))
- {
- if (referenceValue is T tValue)
- {
- result = tValue;
- return true;
- }
- break;
- }
- else if (t == typeof(object))
- {
- result = (T)referenceValue!;
- return true;
- }
- else
- {
- break;
- }
- case LuaValueType.Table:
- if (t == typeof(LuaTable))
- {
- var v = referenceValue!;
- result = Unsafe.As<object, T>(ref v);
- return true;
- }
- else if (t == typeof(object))
- {
- result = (T)referenceValue!;
- return true;
- }
- else
- {
- break;
- }
- }
- result = default!;
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool TryReadBool(out bool result)
- {
- if (Type == LuaValueType.Boolean)
- {
- result = value != 0;
- return true;
- }
- result = default!;
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool TryReadNumber(out double result)
- {
- if (Type == LuaValueType.Number)
- {
- result = value;
- return true;
- }
- result = default!;
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool TryReadTable(out LuaTable result)
- {
- if (Type == LuaValueType.Table)
- {
- var v = referenceValue!;
- result = Unsafe.As<object, LuaTable>(ref v);
- return true;
- }
- result = default!;
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool TryReadFunction(out LuaFunction result)
- {
- if (Type == LuaValueType.Function)
- {
- var v = referenceValue!;
- result = Unsafe.As<object, LuaFunction>(ref v);
- return true;
- }
- result = default!;
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool TryReadString(out string result)
- {
- if (Type == LuaValueType.String)
- {
- var v = referenceValue!;
- result = Unsafe.As<object, string>(ref v);
- return true;
- }
- result = default!;
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool TryReadDouble(out double result)
- {
- if (Type == LuaValueType.Number)
- {
- result = value;
- return true;
- }
- return TryParseToDouble(out result);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static bool TryReadOrSetDouble(ref LuaValue luaValue, out double result)
- {
- if (luaValue.Type == LuaValueType.Number)
- {
- result = luaValue.value;
- return true;
- }
- if (luaValue.TryParseToDouble(out result))
- {
- luaValue = result;
- return true;
- }
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal double UnsafeReadDouble()
- {
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal string UnsafeReadString()
- {
- return Unsafe.As<string>(referenceValue!);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal object UnsafeReadObject()
- {
- return Unsafe.As<object>(referenceValue!);
- }
- bool TryParseToDouble(out double result)
- {
- if (Type != LuaValueType.String)
- {
- result = default!;
- return false;
- }
- var str = Unsafe.As<string>(referenceValue!);
- var span = str.AsSpan().Trim();
- if (span.Length == 0)
- {
- result = default!;
- return false;
- }
- var sign = 1;
- var first = span[0];
- if (first is '+')
- {
- sign = 1;
- span = span[1..];
- }
- else if (first is '-')
- {
- sign = -1;
- span = span[1..];
- }
- if (span.Length > 2 && span[0] is '0' && span[1] is 'x' or 'X')
- {
- // TODO: optimize
- try
- {
- var d = HexConverter.ToDouble(span) * sign;
- result = d;
- return true;
- }
- catch (FormatException)
- {
- result = default!;
- return false;
- }
- }
- else
- {
- return double.TryParse(str, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
- }
- }
- public T Read<T>()
- {
- if (!TryRead<T>(out var result))
- {
- throw new InvalidOperationException($"Cannot convert LuaValueType.{Type} to {typeof(T).FullName}.");
- }
- return result;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal T UnsafeRead<T>()
- {
- switch (Type)
- {
- case LuaValueType.Boolean:
- {
- var v = value != 0;
- return Unsafe.As<bool, T>(ref v);
- }
- case LuaValueType.Number:
- {
- var v = value;
- return Unsafe.As<double, T>(ref v);
- }
- case LuaValueType.String:
- case LuaValueType.Thread:
- case LuaValueType.Function:
- case LuaValueType.Table:
- case LuaValueType.LightUserData:
- case LuaValueType.UserData:
- {
- var v = referenceValue!;
- return Unsafe.As<object, T>(ref v);
- }
- }
- return default!;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool ToBoolean()
- {
- if (Type == LuaValueType.Boolean)
- {
- return value != 0;
- }
- if (Type is LuaValueType.Nil)
- {
- return false;
- }
- return true;
- }
- public static LuaValue FromObject(object obj)
- {
- return obj switch
- {
- null => Nil,
- LuaValue luaValue => luaValue,
- bool boolValue => boolValue,
- double doubleValue => doubleValue,
- string stringValue => stringValue,
- LuaFunction luaFunction => luaFunction,
- LuaTable luaTable => luaTable,
- LuaThread luaThread => luaThread,
- ILuaUserData userData => FromUserData(userData),
- int intValue => intValue,
- long longValue => longValue,
- float floatValue => floatValue,
- _ => new(obj)
- };
- }
- public static LuaValue FromUserData(ILuaUserData? userData)
- {
- if (userData is null)
- {
- return Nil;
- }
- return new(userData);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- LuaValue(object obj)
- {
- Type = LuaValueType.LightUserData;
- referenceValue = obj;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public LuaValue(bool value)
- {
- Type = LuaValueType.Boolean;
- this.value = value ? 1 : 0;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public LuaValue(double value)
- {
- Type = LuaValueType.Number;
- this.value = value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public LuaValue(string value)
- {
- Type = LuaValueType.String;
- referenceValue = value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public LuaValue(LuaFunction value)
- {
- Type = LuaValueType.Function;
- referenceValue = value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public LuaValue(LuaTable value)
- {
- Type = LuaValueType.Table;
- referenceValue = value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public LuaValue(LuaThread value)
- {
- Type = LuaValueType.Thread;
- referenceValue = value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public LuaValue(ILuaUserData value)
- {
- Type = LuaValueType.UserData;
- referenceValue = value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static implicit operator LuaValue(bool value)
- {
- return new(value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static implicit operator LuaValue(double value)
- {
- return new(value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static implicit operator LuaValue(string value)
- {
- return new(value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static implicit operator LuaValue(LuaTable value)
- {
- return new(value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static implicit operator LuaValue(LuaFunction value)
- {
- return new(value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static implicit operator LuaValue(LuaThread value)
- {
- return new(value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override int GetHashCode()
- {
- return Type switch
- {
- LuaValueType.Nil => 0,
- LuaValueType.Boolean or LuaValueType.Number => value.GetHashCode(),
- LuaValueType.String => Unsafe.As<string>(referenceValue)!.GetHashCode(),
- _ => referenceValue!.GetHashCode()
- };
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Equals(LuaValue other)
- {
- if (other.Type != Type)
- {
- return false;
- }
- return Type switch
- {
- LuaValueType.Nil => true,
- LuaValueType.Boolean or LuaValueType.Number => other.value == value,
- LuaValueType.String => Unsafe.As<string>(other.referenceValue) == Unsafe.As<string>(referenceValue),
- _ => other.referenceValue == referenceValue
- };
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool EqualsForDict(LuaValue other)
- {
- return other.Type == Type && Type switch
- {
- LuaValueType.Boolean or LuaValueType.Number => other.value == value,
- LuaValueType.String => Unsafe.As<string>(other.referenceValue) == Unsafe.As<string>(referenceValue),
- _ => other.referenceValue == referenceValue
- };
- }
- public override bool Equals(object? obj)
- {
- return obj is LuaValue value1 && Equals(value1);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool operator ==(LuaValue a, LuaValue b)
- {
- return a.Equals(b);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool operator !=(LuaValue a, LuaValue b)
- {
- return !a.Equals(b);
- }
- public override string ToString()
- {
- return Type switch
- {
- LuaValueType.Nil => "nil",
- LuaValueType.Boolean => Read<bool>() ? "true" : "false",
- LuaValueType.String => Read<string>(),
- LuaValueType.Number => Read<double>().ToString(CultureInfo.InvariantCulture),
- LuaValueType.Function => $"function: {referenceValue!.GetHashCode()}",
- LuaValueType.Thread => $"thread: {referenceValue!.GetHashCode()}",
- LuaValueType.Table => $"table: {referenceValue!.GetHashCode()}",
- LuaValueType.LightUserData => $"userdata: {referenceValue!.GetHashCode()}",
- LuaValueType.UserData => $"userdata: {referenceValue!.GetHashCode()}",
- _ => ""
- };
- }
- public string TypeToString()
- {
- return ToString(Type);
- }
- public static string ToString(LuaValueType type)
- {
- return type switch
- {
- LuaValueType.Nil => "nil",
- LuaValueType.Boolean => "boolean",
- LuaValueType.String => "string",
- LuaValueType.Number => "number",
- LuaValueType.Function => "function",
- LuaValueType.Thread => "thread",
- LuaValueType.Table => "table",
- LuaValueType.LightUserData => "light userdata",
- LuaValueType.UserData => "userdata",
- _ => ""
- };
- }
- public static bool TryGetLuaValueType(Type type, out LuaValueType result)
- {
- if (type == typeof(double) || type == typeof(float) || type == typeof(int) || type == typeof(long))
- {
- result = LuaValueType.Number;
- return true;
- }
- else if (type == typeof(bool))
- {
- result = LuaValueType.Boolean;
- return true;
- }
- else if (type == typeof(string))
- {
- result = LuaValueType.String;
- return true;
- }
- else if (type == typeof(LuaFunction) || type.IsSubclassOf(typeof(LuaFunction)))
- {
- result = LuaValueType.Function;
- return true;
- }
- else if (type == typeof(LuaTable))
- {
- result = LuaValueType.Table;
- return true;
- }
- else if (type == typeof(LuaThread))
- {
- result = LuaValueType.Thread;
- return true;
- }
- else if (type == typeof(ILuaUserData) || type.IsAssignableFrom(typeof(ILuaUserData)))
- {
- result = LuaValueType.UserData;
- return true;
- }
- result = default;
- return false;
- }
- internal ValueTask<int> CallToStringAsync(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
- {
- if (this.TryGetMetamethod(context.State, Metamethods.ToString, out var metamethod))
- {
- var stack = context.Thread.Stack;
- stack.Push(metamethod);
- stack.Push(this);
- return LuaVirtualMachine.Call(context.Thread, stack.Count - 2, stack.Count - 2, cancellationToken);
- }
- else
- {
- context.Thread.Stack.Push(ToString());
- return default;
- }
- }
- }
|