Processor_UtilityFunctions.cs 3.1 KB

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