| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MoonSharp.Interpreter.DataStructs;
- using MoonSharp.Interpreter.DataTypes;
- namespace MoonSharp.Interpreter
- {
- public class Table : IScriptPrivateResource
- {
- LinkedList<TablePair> m_Values;
- LinkedListIndex<DynValue, TablePair> m_ValueMap;
- LinkedListIndex<string, TablePair> m_StringMap;
- LinkedListIndex<int, TablePair> m_ArrayMap;
- Script m_Owner;
- int m_InitArray = 0;
- int m_CachedLength = -1;
- public Table(Script owner)
- {
- m_Values = new LinkedList<TablePair>();
- m_StringMap = new LinkedListIndex<string, TablePair>(m_Values);
- m_ArrayMap = new LinkedListIndex<int, TablePair>(m_Values);
- m_ValueMap = new LinkedListIndex<DynValue, TablePair>(m_Values);
- m_Owner = owner;
- }
- public Script OwnerScript
- {
- get { return m_Owner; }
- }
- private int GetIntegralKey(double d)
- {
- int v = ((int)d);
- if (d >= 1.0 && d == v)
- return v;
- return -1;
- }
- public DynValue this[DynValue key]
- {
- get
- {
- if (key.Type == DataType.Number)
- {
- int idx = GetIntegralKey(key.Number);
- if (idx > 0)
- {
- return GetValueOrNil(m_ArrayMap.Find(idx));
- }
- }
- else if (key.Type == DataType.String)
- {
- return GetValueOrNil(m_StringMap.Find(key.String));
- }
- return GetValueOrNil(m_ValueMap.Find(key));
- }
- set
- {
- if (key.IsNilOrNan())
- {
- if (key.IsNil())
- throw ScriptRuntimeException.TableIndexIsNil();
- else
- throw ScriptRuntimeException.TableIndexIsNaN();
- }
- if (key.Type == DataType.String)
- {
- this[key.String] = value;
- return;
- }
- if (key.Type == DataType.Number)
- {
- int idx = GetIntegralKey(key.Number);
- if (idx > 0)
- {
- this[idx] = value;
- return;
- }
- }
- CheckValueOwner(key);
- CheckValueOwner(value);
- if (m_ValueMap.Set(key, new TablePair(key, value)))
- CollectDeadKeys();
- }
- }
- private DynValue GetValueOrNil(LinkedListNode<TablePair> linkedListNode)
- {
- if (linkedListNode != null)
- return linkedListNode.Value.Value;
- return DynValue.Nil;
- }
- public DynValue this[string key]
- {
- get
- {
- return GetValueOrNil(m_StringMap.Find(key));
- }
- set
- {
- CheckValueOwner(value);
- if (m_StringMap.Set(key, new TablePair(DynValue.NewString(key), value)))
- CollectDeadKeys();
- }
- }
- public DynValue RawGet(string key)
- {
- var linkedListNode = m_StringMap.Find(key);
- if (linkedListNode != null)
- return linkedListNode.Value.Value;
- return null;
- }
- public DynValue this[int key]
- {
- get
- {
- return GetValueOrNil(m_ArrayMap.Find(key));
- }
- set
- {
- CheckValueOwner(value);
- if (m_ArrayMap.Set(key, new TablePair(DynValue.NewNumber(key), value)))
- {
- CollectDeadKeys();
- m_CachedLength = -1;
- }
- else if (value.IsNil())
- m_CachedLength = -1;
- }
- }
- private void CheckValueOwner(DynValue value)
- {
- // +++ value.AssertOwner(m_Owner);
- }
- private void CollectDeadKeys()
- {
- for (LinkedListNode<TablePair> node = m_Values.First; node != null; node = node.Next)
- {
- if (node.Value.Value.Type == DataType.Nil)
- {
- if (node.Value.Key.Type == DataType.Number)
- {
- int idx = GetIntegralKey(node.Value.Key.Number);
- if (idx > 0)
- {
- m_ArrayMap.Remove(idx);
- continue;
- }
- }
- if (node.Value.Key.Type == DataType.String)
- {
- m_StringMap.Remove(node.Value.Key.String);
- }
- else
- {
- m_ValueMap.Remove(node.Value.Key);
- }
- }
- }
- }
- public IEnumerable<TablePair> DebugPairs()
- {
- return m_Values;
- }
- public TablePair NextKey(DynValue v)
- {
- if (v.Type == DataType.Nil)
- {
- LinkedListNode<TablePair> node = m_Values.First;
- if (node == null)
- return TablePair.Nil;
- else
- return node.Value;
- }
- if (v.Type == DataType.String)
- {
- return GetNextOf(m_StringMap.Find(v.String));
- }
- if (v.Type == DataType.Number)
- {
- int idx = GetIntegralKey(v.Number);
- if (idx > 0)
- {
- return GetNextOf(m_ArrayMap.Find(idx));
- }
- }
- return GetNextOf(m_ValueMap.Find(v));
- }
- private TablePair GetNextOf(LinkedListNode<TablePair> linkedListNode)
- {
- if (linkedListNode == null || linkedListNode.Next == null)
- return TablePair.Nil;
- return linkedListNode.Next.Value;
- }
-
- public bool HasStringSymbol(string symbol)
- {
- return m_StringMap.ContainsKey(symbol);
- }
- public double Length
- {
- get
- {
- if (m_CachedLength < 0)
- {
- m_CachedLength = 0;
- for (int i = 1; m_ArrayMap.ContainsKey(i) && !m_ArrayMap.Find(i).Value.Value.IsNil(); i++)
- m_CachedLength = i;
- }
- return m_CachedLength;
- }
- }
- internal void InitNextArrayKeys(DynValue val, bool lastpos)
- {
- if (val.Type == DataType.Tuple && lastpos)
- {
- foreach (DynValue v in val.Tuple)
- InitNextArrayKeys(v, true);
- }
- else
- {
- this[++m_InitArray] = val.ToScalar();
- }
- }
- /// <summary>
- /// Gets the meta-table associated with this instance.
- /// </summary>
- public Table MetaTable { get; set; }
-
-
- }
- }
|