Table.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.DataTypes;
  7. namespace MoonSharp.Interpreter
  8. {
  9. public class Table : IScriptPrivateResource
  10. {
  11. LinkedList<TablePair> m_Values;
  12. LinkedListIndex<DynValue, TablePair> m_ValueMap;
  13. LinkedListIndex<string, TablePair> m_StringMap;
  14. LinkedListIndex<int, TablePair> m_ArrayMap;
  15. Script m_Owner;
  16. int m_InitArray = 0;
  17. int m_CachedLength = -1;
  18. public Table(Script owner)
  19. {
  20. m_Values = new LinkedList<TablePair>();
  21. m_StringMap = new LinkedListIndex<string, TablePair>(m_Values);
  22. m_ArrayMap = new LinkedListIndex<int, TablePair>(m_Values);
  23. m_ValueMap = new LinkedListIndex<DynValue, TablePair>(m_Values);
  24. m_Owner = owner;
  25. }
  26. public Script OwnerScript
  27. {
  28. get { return m_Owner; }
  29. }
  30. private int GetIntegralKey(double d)
  31. {
  32. int v = ((int)d);
  33. if (d >= 1.0 && d == v)
  34. return v;
  35. return -1;
  36. }
  37. public DynValue this[DynValue key]
  38. {
  39. get
  40. {
  41. if (key.Type == DataType.Number)
  42. {
  43. int idx = GetIntegralKey(key.Number);
  44. if (idx > 0)
  45. {
  46. return GetValueOrNil(m_ArrayMap.Find(idx));
  47. }
  48. }
  49. else if (key.Type == DataType.String)
  50. {
  51. return GetValueOrNil(m_StringMap.Find(key.String));
  52. }
  53. return GetValueOrNil(m_ValueMap.Find(key));
  54. }
  55. set
  56. {
  57. if (key.IsNilOrNan())
  58. {
  59. if (key.IsNil())
  60. throw ScriptRuntimeException.TableIndexIsNil();
  61. else
  62. throw ScriptRuntimeException.TableIndexIsNaN();
  63. }
  64. if (key.Type == DataType.String)
  65. {
  66. this[key.String] = value;
  67. return;
  68. }
  69. if (key.Type == DataType.Number)
  70. {
  71. int idx = GetIntegralKey(key.Number);
  72. if (idx > 0)
  73. {
  74. this[idx] = value;
  75. return;
  76. }
  77. }
  78. CheckValueOwner(key);
  79. CheckValueOwner(value);
  80. if (m_ValueMap.Set(key, new TablePair(key, value)))
  81. CollectDeadKeys();
  82. }
  83. }
  84. private DynValue GetValueOrNil(LinkedListNode<TablePair> linkedListNode)
  85. {
  86. if (linkedListNode != null)
  87. return linkedListNode.Value.Value;
  88. return DynValue.Nil;
  89. }
  90. public DynValue this[string key]
  91. {
  92. get
  93. {
  94. return GetValueOrNil(m_StringMap.Find(key));
  95. }
  96. set
  97. {
  98. CheckValueOwner(value);
  99. if (m_StringMap.Set(key, new TablePair(DynValue.NewString(key), value)))
  100. CollectDeadKeys();
  101. }
  102. }
  103. public DynValue RawGet(string key)
  104. {
  105. var linkedListNode = m_StringMap.Find(key);
  106. if (linkedListNode != null)
  107. return linkedListNode.Value.Value;
  108. return null;
  109. }
  110. public DynValue this[int key]
  111. {
  112. get
  113. {
  114. return GetValueOrNil(m_ArrayMap.Find(key));
  115. }
  116. set
  117. {
  118. CheckValueOwner(value);
  119. if (m_ArrayMap.Set(key, new TablePair(DynValue.NewNumber(key), value)))
  120. {
  121. CollectDeadKeys();
  122. m_CachedLength = -1;
  123. }
  124. else if (value.IsNil())
  125. m_CachedLength = -1;
  126. }
  127. }
  128. private void CheckValueOwner(DynValue value)
  129. {
  130. // +++ value.AssertOwner(m_Owner);
  131. }
  132. private void CollectDeadKeys()
  133. {
  134. for (LinkedListNode<TablePair> node = m_Values.First; node != null; node = node.Next)
  135. {
  136. if (node.Value.Value.Type == DataType.Nil)
  137. {
  138. if (node.Value.Key.Type == DataType.Number)
  139. {
  140. int idx = GetIntegralKey(node.Value.Key.Number);
  141. if (idx > 0)
  142. {
  143. m_ArrayMap.Remove(idx);
  144. continue;
  145. }
  146. }
  147. if (node.Value.Key.Type == DataType.String)
  148. {
  149. m_StringMap.Remove(node.Value.Key.String);
  150. }
  151. else
  152. {
  153. m_ValueMap.Remove(node.Value.Key);
  154. }
  155. }
  156. }
  157. }
  158. public IEnumerable<TablePair> DebugPairs()
  159. {
  160. return m_Values;
  161. }
  162. public TablePair NextKey(DynValue v)
  163. {
  164. if (v.Type == DataType.Nil)
  165. {
  166. LinkedListNode<TablePair> node = m_Values.First;
  167. if (node == null)
  168. return TablePair.Nil;
  169. else
  170. return node.Value;
  171. }
  172. if (v.Type == DataType.String)
  173. {
  174. return GetNextOf(m_StringMap.Find(v.String));
  175. }
  176. if (v.Type == DataType.Number)
  177. {
  178. int idx = GetIntegralKey(v.Number);
  179. if (idx > 0)
  180. {
  181. return GetNextOf(m_ArrayMap.Find(idx));
  182. }
  183. }
  184. return GetNextOf(m_ValueMap.Find(v));
  185. }
  186. private TablePair GetNextOf(LinkedListNode<TablePair> linkedListNode)
  187. {
  188. if (linkedListNode == null || linkedListNode.Next == null)
  189. return TablePair.Nil;
  190. return linkedListNode.Next.Value;
  191. }
  192. public bool HasStringSymbol(string symbol)
  193. {
  194. return m_StringMap.ContainsKey(symbol);
  195. }
  196. public double Length
  197. {
  198. get
  199. {
  200. if (m_CachedLength < 0)
  201. {
  202. m_CachedLength = 0;
  203. for (int i = 1; m_ArrayMap.ContainsKey(i) && !m_ArrayMap.Find(i).Value.Value.IsNil(); i++)
  204. m_CachedLength = i;
  205. }
  206. return m_CachedLength;
  207. }
  208. }
  209. internal void InitNextArrayKeys(DynValue val, bool lastpos)
  210. {
  211. if (val.Type == DataType.Tuple && lastpos)
  212. {
  213. foreach (DynValue v in val.Tuple)
  214. InitNextArrayKeys(v, true);
  215. }
  216. else
  217. {
  218. this[++m_InitArray] = val.ToScalar();
  219. }
  220. }
  221. /// <summary>
  222. /// Gets the meta-table associated with this instance.
  223. /// </summary>
  224. public Table MetaTable { get; set; }
  225. }
  226. }