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/interp/JsArray.h> 00018 #include <spl/interp/JsObject.h> 00019 #include <spl/Variant.h> 00020 00021 IJsObject::IJsObject() 00022 { 00023 } 00024 00025 IJsObject::~IJsObject() 00026 { 00027 } 00028 00029 JsObject::JsObject() 00030 : m_properties() 00031 { 00032 } 00033 00034 JsObject::JsObject(const JsObject& obj) 00035 : m_properties() 00036 { 00037 foreach(v,obj.m_properties) 00038 { 00039 m_properties.Set(v.CurrentKey(), v.Current()->Clone()); 00040 } 00041 } 00042 00043 JsObject::~JsObject() 00044 { 00045 } 00046 00047 IJsObjectPtr JsObject::New() const 00048 { 00049 return IJsObjectPtr(new JsObject(*this)); 00050 } 00051 00052 VariantPtr JsObject::GetProperty(const String& idx) 00053 { 00054 if (! m_properties.ContainsKey(idx)) 00055 { 00056 m_properties.Set(idx, VariantPtr(new Variant(Undefined::Instance()))); 00057 } 00058 return m_properties.Get(idx); 00059 } 00060 00061 bool JsObject::HasProperty(const String& idx) const 00062 { 00063 return m_properties.ContainsKey(idx); 00064 } 00065 00066 void JsObject::SetProperty(const String& idx, VariantPtr obj) 00067 { 00068 m_properties.Set(idx, obj); 00069 } 00070 00071 StringPtr JsObject::ToString() const 00072 { 00073 return StringPtr(new String("[object Object]")); 00074 } 00075 00076 bool JsObject::Equals( const IComparable& a ) const 00077 { 00078 // doesn't consider properties 00079 if (a.MajicNumber() == MajicNumber()) 00080 { 00081 const JsObject &j2 = static_cast<const JsObject &>(a); 00082 j2.ValidateMem(); 00083 return (void *)this == (void *)&j2; 00084 } 00085 return false; 00086 } 00087 00088 int JsObject::Compare( const IComparable& a ) const 00089 { 00090 // doesn't consider properties 00091 if (a.MajicNumber() == MajicNumber()) 00092 { 00093 const JsObject &j2 = static_cast<const JsObject &>(a); 00094 j2.ValidateMem(); 00095 return ToString()->Compare(*j2.ToString()); 00096 } 00097 return -1; 00098 } 00099 00100 int32 JsObject::MajicNumber() const 00101 { 00102 return JSOBJECT_MAJIC; 00103 } 00104 00105 int32 JsObject::HashCode() const 00106 { 00107 return *static_cast<const int32 *>(static_cast<const void *>(this)); 00108 } 00109 00110 #if defined(DEBUG) || defined(_DEBUG) 00111 void JsObject::CheckMem() const 00112 { 00113 m_properties.CheckMem(); 00114 } 00115 00116 void JsObject::ValidateMem() const 00117 { 00118 m_properties.ValidateMem(); 00119 } 00120 #endif 00121 00122 //JsNull::JsNull() 00123 //{ 00124 // m_val = new Variant(Undefined()); 00125 //} 00126 // 00127 //JsNull::JsNull(const JsNull& obj) 00128 //{ 00129 // m_val = new Variant(Undefined()); 00130 //} 00131 // 00132 //JsNull::~JsNull() 00133 //{ 00134 //} 00135 // 00136 //VariantPtr JsNull::GetProperty(const String& idx) 00137 //{ 00138 // return VariantPtr(new Variant(*(Variant *)m_val)); 00139 //} 00140 // 00141 //void JsNull::SetProperty(const String& idx, VariantPtr obj) 00142 //{ 00143 //} 00144 // 00145 //bool JsNull::HasProperty(const String& idx) const 00146 //{ 00147 // return false; 00148 //} 00149 // 00150 // 00151 //StringPtr JsNull::ToString() const 00152 //{ 00153 // return StringPtr(new String("null")); 00154 //} 00155 // 00156 //bool JsNull::Equals( const IComparable& a ) const 00157 //{ 00158 // // doesn't consider properties 00159 // return a.MajicNumber() == MajicNumber(); 00160 //} 00161 // 00162 //int JsNull::Compare( const IComparable& a ) const 00163 //{ 00164 // // doesn't consider properties 00165 // return (a.MajicNumber() == MajicNumber()) ? 0 : -1; 00166 //} 00167 // 00168 //int32 JsNull::MajicNumber() const 00169 //{ 00170 // return JSNULL_MAJIC; 00171 //} 00172 // 00173 //int32 JsNull::HashCode() const 00174 //{ 00175 // return 0; 00176 //} 00177 // 00178 //#if defined(DEBUG) || defined(_DEBUG) 00179 //void JsNull::CheckMem() const 00180 //{ 00181 // DEBUG_NOTE_MEM(m_val); 00182 // m_val->CheckMem(); 00183 //} 00184 // 00185 //void JsNull::ValidateMem() const 00186 //{ 00187 // ASSERT_MEM(m_val, sizeof(Variant)); 00188 // m_val->ValidateMem(); 00189 //} 00190 //#endif 00191 // 00192 //JsUndefined::JsUndefined() 00193 //{ 00194 // m_val = new Variant(Undefined()); 00195 //} 00196 // 00197 //JsUndefined::JsUndefined(const JsUndefined& obj) 00198 //{ 00199 // m_val = new Variant(Undefined()); 00200 //} 00201 // 00202 //JsUndefined::~JsUndefined() 00203 //{ 00204 // delete m_val; 00205 //} 00206 // 00207 //VariantPtr JsUndefined::GetProperty(const String& idx) 00208 //{ 00209 // return VariantPtr(new Variant(*(Variant *)m_val)); 00210 //} 00211 // 00212 //bool JsUndefined::HasProperty(const String& idx) const 00213 //{ 00214 // return false; 00215 //} 00216 // 00217 //void JsUndefined::SetProperty(const String& idx, VariantPtr obj) 00218 //{ 00219 //} 00220 // 00221 //StringPtr JsUndefined::ToString() const 00222 //{ 00223 // return StringPtr(new String("undefined")); 00224 //} 00225 // 00226 //bool JsUndefined::Equals( const IComparable& a ) const 00227 //{ 00228 // // doesn't consider properties 00229 // return a.MajicNumber() == MajicNumber(); 00230 //} 00231 // 00232 //int JsUndefined::Compare( const IComparable& a ) const 00233 //{ 00234 // // doesn't consider properties 00235 // return (a.MajicNumber() == MajicNumber()) ? 0 : -1; 00236 //} 00237 // 00238 //int32 JsUndefined::MajicNumber() const 00239 //{ 00240 // return JSUNDEFINED_MAJIC; 00241 //} 00242 // 00243 //int32 JsUndefined::HashCode() const 00244 //{ 00245 // return -1; 00246 //} 00247 // 00248 //#if defined(DEBUG) || defined(_DEBUG) 00249 //void JsUndefined::CheckMem() const 00250 //{ 00251 // DEBUG_NOTE_MEM(m_val); 00252 // m_val->CheckMem(); 00253 //} 00254 // 00255 //void JsUndefined::ValidateMem() const 00256 //{ 00257 // ASSERT_MEM(m_val, sizeof(Variant)); 00258 // m_val->ValidateMem(); 00259 //} 00260 //#endif