UpValue.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Runtime.CompilerServices;
  2. namespace Lua.Runtime;
  3. public sealed class UpValue
  4. {
  5. LuaValue value;
  6. public bool IsClosed { get; private set; }
  7. public int RegisterIndex { get; private set; }
  8. UpValue()
  9. {
  10. }
  11. public static UpValue Open(int registerIndex)
  12. {
  13. return new()
  14. {
  15. RegisterIndex = registerIndex
  16. };
  17. }
  18. public static UpValue Closed(LuaValue value)
  19. {
  20. return new()
  21. {
  22. IsClosed = true,
  23. value = value
  24. };
  25. }
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. public LuaValue GetValue(LuaState state)
  28. {
  29. if (IsClosed)
  30. {
  31. return value;
  32. }
  33. else
  34. {
  35. return state.Stack.UnsafeGet(RegisterIndex);
  36. }
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public void SetValue(LuaState state, LuaValue value)
  40. {
  41. if (IsClosed)
  42. {
  43. this.value = value;
  44. }
  45. else
  46. {
  47. state.Stack.UnsafeGet(RegisterIndex) = value;
  48. }
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public void Close(LuaState state)
  52. {
  53. if (!IsClosed)
  54. {
  55. value = state.Stack.UnsafeGet(RegisterIndex);
  56. }
  57. IsClosed = true;
  58. }
  59. }