• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/interp/JsArray.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/Int32.h>
00018 #include <spl/interp/JsArray.h>
00019 #include <spl/text/StringBuffer.h>
00020 
00021 JsArray::JsArray()
00022 : m_array()
00023 {
00024 }
00025 
00026 JsArray::JsArray(const JsArray& obj)
00027 : JsObject(obj), m_array()
00028 {
00029         foreach(a,obj.m_array)
00030         {
00031                 m_array.Add(a.CurrentRef()->Clone());
00032         }
00033 }
00034 
00035 JsArray::~JsArray()
00036 {
00037 }
00038 
00039 IJsObjectPtr JsArray::Dup() const
00040 {
00041         return IJsObjectPtr(new JsArray(*this));
00042 }
00043         
00044 VariantPtr JsArray::GetProperty(const String& idx)
00045 {
00046         if ("length" == idx)
00047         {
00048                 return VariantPtr(new Variant(m_array.Count()));
00049         }
00050         if (Int32::IsInt(idx))
00051         {
00052                 int iidx = Int32::Parse(idx);
00053                 m_array.EnsureElementTo(iidx);
00054                 
00055                 VariantPtr& var = m_array.ElementAtRef(iidx);
00056                 if (var.IsNull())
00057                 {
00058                         var = VariantPtr(new Variant());
00059                 }
00060                 return var;
00061         }
00062         else
00063         {
00064                 return JsObject::GetProperty(idx);
00065         }
00066 }
00067 
00068 bool JsArray::HasProperty(const String& idx) const
00069 {
00070         return "length" == idx || Int32::IsInt(idx) || JsObject::HasProperty(idx);
00071 }
00072 
00073 void JsArray::SetProperty(const String& idx, VariantPtr obj)
00074 {
00075         if ("length" == idx)
00076         {
00077                 return;
00078         }
00079         if (Int32::IsInt(idx))
00080         {
00081                 int iidx = Int32::Parse(idx);
00082                 m_array.EnsureElementTo(iidx);          
00083                 m_array.SetElementAt(obj, iidx);
00084         }
00085 }
00086 
00087 bool JsArray::Equals( const IComparable& a ) const
00088 {
00089         if (a.MajicNumber() != MajicNumber())
00090         {
00091                 return false;
00092         }
00093         
00094         const JsArray &arr = (const JsArray&)a;
00095         if (arr.m_array.Count() != m_array.Count())
00096         {
00097                 return false;
00098         }
00099         
00100         for (int x = 0; x < m_array.Count(); x++)
00101         {
00102                 VariantPtr& v = m_array.ElementAtRef(x);
00103                 if (! v->Equals(*arr.m_array.ElementAtRef(x)))
00104                 {
00105                         return false;
00106                 }
00107         }
00108         return true;
00109 }
00110 
00111 int JsArray::Compare( const IComparable& a ) const
00112 {
00113         if (a.MajicNumber() != MajicNumber())
00114         {
00115                 return -1;
00116         }
00117         
00118         const JsArray &arr = (const JsArray&)a;
00119         if (arr.m_array.Count() != m_array.Count())
00120         {
00121                 return arr.m_array.Count() - m_array.Count();
00122         }
00123         
00124         for (int x = 0; x < m_array.Count(); x++)
00125         {
00126                 VariantPtr& v = m_array.ElementAtRef(x);
00127                 int cmp = v->Compare(*arr.m_array.ElementAtRef(x));
00128                 if (0 != cmp)
00129                 {
00130                         return cmp;
00131                 }
00132         }
00133         return 0;
00134 }
00135 
00136 int32 JsArray::MajicNumber() const
00137 {
00138         return JSARRAY_MAJIC;
00139 }
00140 
00141 int32 JsArray::HashCode() const
00142 {
00143         int32 hc = 0;
00144         for(Vector<VariantPtr>::Iterator v = m_array.Begin(); v.Next();)
00145         {
00146                 hc ^= v.CurrentRef()->HashCode();
00147         }
00148         return hc;
00149 }
00150 
00151 StringPtr JsArray::ToString() const
00152 {
00153         StringBuffer buf;
00154         
00155         for(Vector<VariantPtr>::Iterator v = m_array.Begin(); v.Next();)
00156         {
00157                 if (buf.Length() > 0)
00158                 {
00159                         buf.Append(',');
00160                 }
00161                 buf.Append(v.CurrentRef()->ToString());
00162         }
00163         return buf.ToString();
00164 }
00165 
00166 #if defined(DEBUG) || defined(_DEBUG)
00167 void JsArray::CheckMem() const
00168 {
00169         JsObject::CheckMem();
00170         m_array.CheckMem();
00171 }
00172 
00173 void JsArray::ValidateMem() const
00174 {
00175         JsObject::ValidateMem();
00176         m_array.ValidateMem();
00177 }
00178 #endif
00179