summaryrefslogtreecommitdiff
path: root/src/globals.cpp
blob: 23803a54f79dc64a6a649028163dedef87ea0f3b (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
 *  Globals.cpp
 *
 *  Implementation of the globals class
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */
#include "includes.h"

/**
 *  Namespace
 */
namespace Php {

/**
 *  Get access to the globals single instance
 *  @return Globals
 */
Globals &Globals::instance()
{
    static Globals globals;
    return globals;
}

/**
 *  The one and only instance
 *  @var    Globals
 */
Globals &globals = Globals::instance();

/**
 *  Get access to a global variable
 *  @param  name
 *  @return Global
 */
Global Globals::operator[](const char *name)
{
    // pointer to a zval
    zval **varvalue;
    
    // check if the variable already exists
    if (zend_hash_find(&EG(symbol_table), name, strlen(name)+1, (void**)&varvalue) == FAILURE) 
    {
        // the variable does not already exist, return a global object
        // that will automatically set the value when it is updated
        return Global(name);
    }
    else
    {
        // we are in the happy situation that the variable exists, we turn
        // this value into a reference value, and return that
        return Global(name, *varvalue);
    }
}

/**
 *  Get access to a global variable
 *  @param  name
 *  @return Global
 */
Global Globals::operator[](const std::string &name)
{
    // pointer to a zval
    zval **varvalue;
    
    // check if the variable already exists
    if (zend_hash_find(&EG(symbol_table), name.c_str(), name.size()+1, (void**)&varvalue) == FAILURE) 
    {
        // the variable does not already exist, return a global object
        // that will automatically set the value when it is updated
        return Global(name);
    }
    else
    {
        // we are in the happy situation that the variable exists, we turn
        // this value into a reference value, and return that
        return Global(name, *varvalue);
    }
}

/**
 *  Call function with a number of parameters
 *  @param  name        Function name
 *  @param  argc        Number of parameters
 *  @param  argv        The parameters
 *  @return Value
 */
Value Globals::exec(const Value &name, int argc, zval ***params)
{
    // the return zval
    zval *retval = nullptr;
    
    // call the function
    if (call_user_function_ex(CG(function_table), NULL, name._val, &retval, argc, params, 1, NULL) != SUCCESS) return nullptr;
    
    // was a value returned?
    if (retval) return Value(retval);
    
    // no value was returned, return NULL
    return nullptr;
}

/**
 *  Call a function in PHP that does not have any parameters
 *  @param  name        Name of the function
 *  @return Value
 */
Value Globals::call(const Value &name)
{
    // call with zero parameters
    return exec(name, 0, NULL);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0)
{
    // array of parameters
    zval **params[1] = { &p0._val };

    // call the function
    return exec(name, 1, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1)
{
    // array of parameters
    zval **params[2] = { &p0._val, &p1._val };
    
    // call the function
    return exec(name, 2, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @param  p2          The third parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1, Value p2)
{
    // array of parameters
    zval **params[3] = { &p0._val, &p1._val, &p2._val };
    
    // call the function
    return exec(name, 3, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @param  p2          The third parameter
 *  @param  p3          The fourth parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3)
{
    // array of parameters
    zval **params[4] = { &p0._val, &p1._val, &p2._val, &p3._val };
    
    // call the function
    return exec(name, 4, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @param  p2          The third parameter
 *  @param  p3          The fourth parameter
 *  @param  p4          The fifth parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4)
{
    // array of parameters
    zval **params[5] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val };
    
    // call the function
    return exec(name, 5, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @param  p2          The third parameter
 *  @param  p3          The fourth parameter
 *  @param  p4          The fifth parameter
 *  @param  p5          The sixth parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5)
{
    // array of parameters
    zval **params[6] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val };
    
    // call the function
    return exec(name, 6, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @param  p2          The third parameter
 *  @param  p3          The fourth parameter
 *  @param  p4          The fifth parameter
 *  @param  p5          The sixth parameter
 *  @param  p6          The seventh parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6)
{
    // array of parameters
    zval **params[7] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val };
    
    // call the function
    return exec(name, 7, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @param  p2          The third parameter
 *  @param  p3          The fourth parameter
 *  @param  p4          The fifth parameter
 *  @param  p5          The sixth parameter
 *  @param  p6          The seventh parameter
 *  @param  p7          The eight parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7)
{
    // array of parameters
    zval **params[8] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val };
    
    // call the function
    return exec(name, 8, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @param  p2          The third parameter
 *  @param  p3          The fourth parameter
 *  @param  p4          The fifth parameter
 *  @param  p5          The sixth parameter
 *  @param  p6          The seventh parameter
 *  @param  p7          The eight parameter
 *  @param  p8          The nineth parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8)
{
    // array of parameters
    zval **params[9] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val, &p8._val };
    
    // call the function
    return exec(name, 9, params);
}

/**
 *  Call a function in PHP
 *  @param  name        Name of the function
 *  @param  p0          The first parameter
 *  @param  p1          The second parameter
 *  @param  p2          The third parameter
 *  @param  p3          The fourth parameter
 *  @param  p4          The fifth parameter
 *  @param  p5          The sixth parameter
 *  @param  p6          The seventh parameter
 *  @param  p7          The eight parameter
 *  @param  p8          The nineth parameter
 *  @param  p9          The tenth parameter
 *  @return Value
 */
Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9)
{
    // array of parameters
    zval **params[10] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val, &p8._val, &p9._val };
    
    // call the function
    return exec(name, 10, params);
}

/**
 *  End of namespace
 */
}