2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2015 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 +----------------------------------------------------------------------+
16 #include "hphp/runtime/base/apc-handle.h"
18 #include "hphp/runtime/base/apc-typed-value.h"
19 #include "hphp/runtime/base/apc-string.h"
20 #include "hphp/runtime/base/apc-array.h"
21 #include "hphp/runtime/base/apc-object.h"
22 #include "hphp/runtime/base/apc-collection.h"
23 #include "hphp/runtime/base/mixed-array.h"
24 #include "hphp/runtime/ext/apc/ext_apc.h"
28 //////////////////////////////////////////////////////////////////////
30 APCHandle::Pair
APCHandle::Create(const Variant
& source
,
33 bool unserializeObj
) {
34 auto type
= source
.getType(); // this gets rid of the ref, if it was one
37 auto value
= APCTypedValue::tvUninit();
38 return {value
->getHandle(), sizeof(APCTypedValue
)};
41 auto value
= APCTypedValue::tvNull();
42 return {value
->getHandle(), sizeof(APCTypedValue
)};
45 auto value
= source
.getBoolean() ? APCTypedValue::tvTrue()
46 : APCTypedValue::tvFalse();
47 return {value
->getHandle(), sizeof(APCTypedValue
)};
50 auto value
= new APCTypedValue(source
.getInt64());
51 return {value
->getHandle(), sizeof(APCTypedValue
)};
54 auto value
= new APCTypedValue(source
.getDouble());
55 return {value
->getHandle(), sizeof(APCTypedValue
)};
57 case KindOfStaticString
: {
58 StringData
* s
= source
.getStringData();
60 // It is priming, and there might not be the right class definitions
61 // for unserialization.
62 return APCObject::MakeSerializedObj(apc_reserialize(String
{s
}));
64 auto value
= new APCTypedValue(APCTypedValue::StaticStr
{}, s
);
65 return {value
->getHandle(), sizeof(APCTypedValue
)};
68 StringData
* s
= source
.getStringData();
70 // It is priming, and there might not be the right class definitions
71 // for unserialization.
72 return APCObject::MakeSerializedObj(apc_reserialize(String
{s
}));
75 auto const st
= lookupStaticString(s
);
77 auto value
= new APCTypedValue(APCTypedValue::StaticStr
{}, st
);
78 return {value
->getHandle(), sizeof(APCTypedValue
)};
81 assert(!s
->isStatic()); // would've been handled above
82 if (!inner
&& apcExtension::UseUncounted
) {
83 auto st
= StringData::MakeUncounted(s
->slice());
84 auto value
= new APCTypedValue(APCTypedValue::UncountedStr
{}, st
);
85 return {value
->getHandle(), st
->size() + sizeof(APCTypedValue
)};
87 return APCString::MakeSharedString(type
, s
);
91 return APCArray::MakeSharedArray(source
.getArrayData(), inner
,
95 if (source
.getObjectData()->isCollection()) {
96 return APCCollection::Make(source
.getObjectData(),
100 return unserializeObj
? APCObject::Construct(source
.getObjectData()) :
101 APCObject::MakeSerializedObj(apc_serialize(source
));
104 // TODO Task #2661075: Here and elsewhere in the runtime, we convert
105 // Resources to the empty array during various serialization operations,
106 // which does not match Zend behavior. We should fix this.
107 return APCArray::MakeSharedEmptyArray();
116 Variant
APCHandle::toLocal() const {
120 return init_null(); // shortcut.. no point to forward
122 return APCTypedValue::fromHandle(this)->getBoolean();
124 return APCTypedValue::fromHandle(this)->getInt64();
126 return APCTypedValue::fromHandle(this)->getDouble();
127 case KindOfStaticString
:
128 return Variant
{APCTypedValue::fromHandle(this)->getStringData()};
130 return APCString::MakeString(this);
132 return APCArray::MakeArray(this);
134 return APCObject::MakeObject(this);
143 void APCHandle::deleteShared() {
144 assert(!isUncounted());
152 case KindOfStaticString
:
153 delete APCTypedValue::fromHandle(this);
157 APCString::Delete(APCString::fromHandle(this));
161 APCArray::Delete(this);
165 if (isAPCCollection()) {
166 APCCollection::Delete(this);
169 APCObject::Delete(this);
180 //////////////////////////////////////////////////////////////////////