Use folly::dynamic::object and folly::dynamic::string exclusivly
[hiphop-php.git] / hphp / runtime / base / type-object.cpp
blob91db2100134efe5aad29173aacba7196398abc76
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/runtime/base/complex-types.h"
18 #include "hphp/runtime/base/type-conversions.h"
19 #include "hphp/runtime/base/builtin-functions.h"
20 #include "hphp/runtime/base/variable-serializer.h"
21 #include "hphp/runtime/base/array-iterator.h"
22 #include "hphp/runtime/base/strings.h"
23 #include "hphp/runtime/ext/ext_collections.h"
24 #include "hphp/runtime/ext/ext_datetime.h"
26 #include "hphp/system/systemlib.h"
28 namespace HPHP {
30 const Object Object::s_nullObject = Object();
32 ///////////////////////////////////////////////////////////////////////////////
34 Object::~Object() {
35 // force it out of line
38 ArrayIter Object::begin(const String& context /* = null_string */) const {
39 if (!m_px) throw_null_pointer_exception();
40 return m_px->begin(context);
43 MutableArrayIter Object::begin(Variant *key, Variant &val,
44 const String& context /*= null_string*/) const {
45 if (!m_px) throw_null_pointer_exception();
46 return m_px->begin(key, val, context);
49 Array Object::toArray() const {
50 return m_px ? m_px->o_toArray() : Array();
53 int64_t Object::toInt64ForCompare() const {
54 check_collection_compare(m_px);
55 return toInt64();
58 double Object::toDoubleForCompare() const {
59 check_collection_compare(m_px);
60 return toDouble();
63 bool Object::equal(CObjRef v2) const {
64 if (m_px == v2.get()) {
65 return true;
67 if (!m_px || !v2.get()) {
68 return false;
70 if (m_px->isCollection()) {
71 return collectionEquals(m_px, v2.get());
73 if (v2.get()->getVMClass() != m_px->getVMClass()) {
74 return false;
76 if (UNLIKELY(m_px->instanceof(SystemLib::s_ArrayObjectClass))) {
77 // Compare the whole object, not just the array representation
78 Array ar1(ArrayData::Create());
79 Array ar2(ArrayData::Create());
80 m_px->o_getArray(ar1, false);
81 v2->o_getArray(ar2, false);
82 return ar1->equal(ar2.get(), false);
84 if (UNLIKELY(m_px->instanceof(c_DateTime::classof()))) {
85 return getTyped<c_DateTime>()->gettimestamp() ==
86 v2.getTyped<c_DateTime>()->gettimestamp();
88 return toArray().equal(v2.toArray());
91 bool Object::less(CObjRef v2) const {
92 check_collection_compare(m_px, v2.get());
93 if (UNLIKELY(m_px->instanceof(c_DateTime::classof()))) {
94 return getTyped<c_DateTime>()->gettimestamp() <
95 v2.getTyped<c_DateTime>()->gettimestamp();
97 return m_px != v2.m_px && toArray().less(v2.toArray());
100 bool Object::more(CObjRef v2) const {
101 check_collection_compare(m_px, v2.get());
102 if (UNLIKELY(m_px->instanceof(c_DateTime::classof()))) {
103 return getTyped<c_DateTime>()->gettimestamp() >
104 v2.getTyped<c_DateTime>()->gettimestamp();
106 return m_px != v2.m_px && toArray().more(v2.toArray());
109 static Variant warn_non_object() {
110 raise_warning("Cannot access property on non-object");
111 return uninit_null();
114 Variant Object::o_get(const String& propName, bool error /* = true */,
115 const String& context /* = null_string */) const {
116 if (UNLIKELY(!m_px)) return warn_non_object();
117 return m_px->o_get(propName, error, context);
120 Variant Object::o_set(const String& propName, CVarRef val,
121 const String& context /* = null_string */) {
122 if (!m_px) {
123 setToDefaultObject();
125 return m_px->o_set(propName, val, context);
128 Variant Object::o_setRef(const String& propName, CVarRef val,
129 const String& context /* = null_string */) {
130 if (!m_px) {
131 setToDefaultObject();
133 return m_px->o_setRef(propName, val, context);
136 Variant Object::o_set(const String& propName, RefResult val,
137 const String& context /* = null_string */) {
138 return o_setRef(propName, variant(val), context);
141 ///////////////////////////////////////////////////////////////////////////////
142 // output
144 void Object::serialize(VariableSerializer *serializer) const {
145 if (m_px) {
146 m_px->serialize(serializer);
147 } else {
148 serializer->writeNull();
152 bool Object::unserialize(std::istream &in) {
153 throw NotImplementedException(__func__);
156 void Object::setToDefaultObject() {
157 raise_warning(Strings::CREATING_DEFAULT_OBJECT);
158 operator=(SystemLib::AllocStdClassObject());
161 ///////////////////////////////////////////////////////////////////////////////