Processor_UtilityFunctions.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MoonSharp.Interpreter.DataStructs;
  6. using MoonSharp.Interpreter.Debugging;
  7. using MoonSharp.Interpreter.Interop;
  8. namespace MoonSharp.Interpreter.Execution.VM
  9. {
  10. sealed partial class Processor
  11. {
  12. private DynValue[] Internal_AdjustTuple(IList<DynValue> values)
  13. {
  14. if (values == null || values.Count == 0)
  15. return new DynValue[0];
  16. if (values[values.Count - 1].Type == DataType.Tuple)
  17. {
  18. int baseLen = values.Count - 1 + values[values.Count - 1].Tuple.Length;
  19. DynValue[] result = new DynValue[baseLen];
  20. for (int i = 0; i < values.Count - 1; i++)
  21. {
  22. result[i] = values[i].ToScalar();
  23. }
  24. for (int i = 0; i < values[values.Count - 1].Tuple.Length; i++)
  25. {
  26. result[values.Count + i - 1] = values[values.Count - 1].Tuple[i];
  27. }
  28. if (result[result.Length - 1].Type == DataType.Tuple)
  29. return Internal_AdjustTuple(result);
  30. else
  31. return result;
  32. }
  33. else
  34. {
  35. DynValue[] result = new DynValue[values.Count];
  36. for (int i = 0; i < values.Count; i++)
  37. {
  38. result[i] = values[i].ToScalar();
  39. }
  40. return result;
  41. }
  42. }
  43. private int Internal_InvokeUnaryMetaMethod(DynValue op1, string eventName, int instructionPtr)
  44. {
  45. DynValue m = null;
  46. var op1_MetaTable = GetMetatable(op1);
  47. if (op1_MetaTable != null)
  48. {
  49. DynValue meta1 = op1_MetaTable.RawGet(eventName);
  50. if (meta1 != null && meta1.IsNotNil())
  51. m = meta1;
  52. }
  53. if (m != null)
  54. {
  55. m_ValueStack.Push(m);
  56. m_ValueStack.Push(op1);
  57. return Internal_ExecCall(1, instructionPtr);
  58. }
  59. else
  60. {
  61. return -1;
  62. }
  63. }
  64. private int Internal_InvokeBinaryMetaMethod(DynValue l, DynValue r, string eventName, int instructionPtr, DynValue extraPush = null)
  65. {
  66. var m = GetBinaryMetamethod(l, r, eventName);
  67. if (m != null)
  68. {
  69. if (extraPush != null)
  70. m_ValueStack.Push(extraPush);
  71. m_ValueStack.Push(m);
  72. m_ValueStack.Push(l);
  73. m_ValueStack.Push(r);
  74. return Internal_ExecCall(2, instructionPtr);
  75. }
  76. else
  77. {
  78. return -1;
  79. }
  80. }
  81. private DynValue[] StackTopToArray(int items, bool pop)
  82. {
  83. DynValue[] values = new DynValue[items];
  84. if (pop)
  85. {
  86. for (int i = 0; i < items; i++)
  87. {
  88. values[i] = m_ValueStack.Pop();
  89. }
  90. }
  91. else
  92. {
  93. for (int i = 0; i < items; i++)
  94. {
  95. values[i] = m_ValueStack[m_ValueStack.Count - 1 - i];
  96. }
  97. }
  98. return values;
  99. }
  100. private DynValue[] StackTopToArrayReverse(int items, bool pop)
  101. {
  102. DynValue[] values = new DynValue[items];
  103. if (pop)
  104. {
  105. for (int i = 0; i < items; i++)
  106. {
  107. values[items - 1 - i] = m_ValueStack.Pop();
  108. }
  109. }
  110. else
  111. {
  112. for (int i = 0; i < items; i++)
  113. {
  114. values[items - 1 - i] = m_ValueStack[m_ValueStack.Count - 1 - i];
  115. }
  116. }
  117. return values;
  118. }
  119. }
  120. }