-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallableVariant.cpp
More file actions
211 lines (188 loc) · 6.92 KB
/
Copy pathCallableVariant.cpp
File metadata and controls
211 lines (188 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// @copyright 2017-2018 zzu_softboy <zzu_softboy@163.com>
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Created by softboy on 2017/08/21.
#include "zapi/ds/CallableVariant.h"
#include "zapi/ds/ObjectVariant.h"
#include "zapi/kernel/Exception.h"
#include "zapi/kernel/OrigException.h"
#include "zapi/lang/Parameters.h"
#include <string>
using zapi::ds::Variant;
using zapi::kernel::Exception;
using zapi::kernel::OrigException;
namespace
{
Variant do_execute(zval *func, int argc, zval *argv)
{
zval retval;
zend_object *oldException = EG(exception);
if (ZAPI_SUCCESS != call_user_function_ex(CG(function_table), nullptr, func, &retval,
argc, argv, 1, nullptr)) {
std::string msg("Invalid call to ");
msg.append(Z_STRVAL_P(func), Z_STRLEN_P(func));
throw Exception(std::move(msg));
return nullptr; // just for prevent some compiler warnings
} else {
// detect whether has exception throw from PHP code, if we got,
// we throw an native c++ exception, let's c++ code can
// handle it
if (oldException != EG(exception) && EG(exception)) {
throw OrigException(EG(exception));
}
if (Z_ISUNDEF(retval)) {
return nullptr;
}
// wrap the retval into Variant
Variant result(&retval);
// here we decrease the refcounter
zval_ptr_dtor(&retval);
return result;
}
}
Variant do_nothing(zapi::lang::Parameters ¶m)
{
return nullptr;
}
}
namespace zapi
{
namespace ds
{
CallableVariant::CallableVariant(HaveArgCallable callable)
: Variant(ObjectVariant(Closure::getClassEntry(), std::make_shared<Closure>(std::function<HaveArgCallable>(callable))))
{}
CallableVariant::CallableVariant(NoArgCallable callable)
: Variant(ObjectVariant(Closure::getClassEntry(), std::make_shared<Closure>([callable](Parameters ¶ms) -> Variant {
return callable();
})))
{}
CallableVariant::CallableVariant(const Variant &other)
{
zval *from = const_cast<zval *>(other.getZvalPtr());
zval *self = getUnDerefZvalPtr();
zend_class_entry *classEntry = nullptr;
if (other.getType() == Type::Object &&
(classEntry = Z_OBJCE_P(from)) && 0 == std::memcmp("ZapiClosure", classEntry->name->val, classEntry->name->len)) {
ZVAL_COPY(self, from);
} else {
ObjectVariant defaultCallable(Closure::getClassEntry(), std::make_shared<Closure>(do_nothing));
zval temp = defaultCallable.detach(true);
ZVAL_COPY_VALUE(self, &temp);
}
}
CallableVariant::CallableVariant(const CallableVariant &other)
: Variant(other)
{}
CallableVariant::CallableVariant(Variant &&other)
: Variant(std::move(other))
{
zval *self = getUnDerefZvalPtr();
zend_class_entry *classEntry = nullptr;
if (getUnDerefType() != Type::Object ||
((classEntry = Z_OBJCE_P(self)) && 0 != std::memcmp("ZapiClosure", classEntry->name->val, classEntry->name->len))) {
ObjectVariant defaultCallable(Closure::getClassEntry(), std::make_shared<Closure>(do_nothing));
zval temp = defaultCallable.detach(true);
ZVAL_COPY_VALUE(self, &temp);
}
}
CallableVariant::CallableVariant(CallableVariant &&other) ZAPI_DECL_NOEXCEPT
: Variant(std::move(other))
{}
CallableVariant::CallableVariant(zval &other)
: CallableVariant(&other)
{}
CallableVariant::CallableVariant(zval &&other)
: CallableVariant(&other)
{}
CallableVariant::CallableVariant(zval *other)
{
zval *self = getUnDerefZvalPtr();
if (nullptr != other && Z_TYPE_P(other) != IS_NULL) {
if ((Z_TYPE_P(other) == IS_OBJECT ||
(Z_TYPE_P(other) == IS_REFERENCE && Z_TYPE_P(Z_REFVAL_P(other)) == IS_OBJECT))) {
ZVAL_DEREF(other);
zend_class_entry *classEntry = Z_OBJCE_P(other);
if (classEntry && 0 == std::memcmp("ZapiClosure", classEntry->name->val, classEntry->name->len)) {
ZVAL_COPY(self, other);
return;
}
}
}
// construct default
ObjectVariant defaultCallable(Closure::getClassEntry(), std::make_shared<Closure>(do_nothing));
zval temp = defaultCallable.detach(true);
ZVAL_COPY_VALUE(self, &temp);
}
CallableVariant &CallableVariant::operator =(const CallableVariant &other)
{
if (this != &other) {
Variant::operator =(const_cast<zval *>(other.getZvalPtr()));
}
return *this;
}
CallableVariant &CallableVariant::operator =(const Variant &other)
{
if (this != &other) {
zval *self = getZvalPtr();
zval *from = const_cast<zval *>(other.getZvalPtr());
zend_class_entry *classEntry = nullptr;
if (other.getType() == Type::Object &&
(classEntry = Z_OBJCE_P(from)) && 0 == std::memcmp("ZapiClosure", classEntry->name->val, classEntry->name->len)) {
// standard copy
Variant::operator =(from);
} else {
ObjectVariant defaultCallable(Closure::getClassEntry(), std::make_shared<Closure>(do_nothing));
zval temp = defaultCallable.detach(true);
zval_dtor(self);
ZVAL_COPY_VALUE(self, &temp);
}
}
return *this;
}
CallableVariant &CallableVariant::operator =(CallableVariant &&other) ZAPI_DECL_NOEXCEPT
{
assert(this != &other);
m_implPtr = std::move(other.m_implPtr);
return *this;
}
CallableVariant &CallableVariant::operator =(Variant &&other)
{
assert(this != &other);
m_implPtr = std::move(other.m_implPtr);
zval *self = getUnDerefZvalPtr();
zend_class_entry *classEntry = nullptr;
if (getUnDerefType() != Type::Object ||
((classEntry = Z_OBJCE_P(self)) && 0 == std::memcmp("ZapiClosure", classEntry->name->val, classEntry->name->len))) {
ObjectVariant defaultCallable(Closure::getClassEntry(), std::make_shared<Closure>(do_nothing));
zval temp = defaultCallable.detach(true);
ZVAL_COPY_VALUE(self, &temp);
}
return *this;
}
Variant CallableVariant::exec(int argc, Variant *argv) const
{
std::unique_ptr<zval[]> params(new zval[argc]);
for (int i = 0; i < argc; i++) {
params[i] = *argv[i].getZvalPtr();
}
return do_execute(const_cast<zval *>(getUnDerefZvalPtr()), argc, params.get());
}
Variant CallableVariant::operator ()() const
{
return do_execute(const_cast<zval *>(getUnDerefZvalPtr()), 0, nullptr);
}
CallableVariant::~CallableVariant()
{}
} // ds
} // zapi