-
Notifications
You must be signed in to change notification settings - Fork 105
/
value.c
382 lines (312 loc) · 8.3 KB
/
value.c
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2017 Alexander Palaistras. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <errno.h>
#include <stdbool.h>
#include <main/php.h>
#include "value.h"
// Creates a new value and initializes type to null.
engine_value *value_new() {
engine_value *val = malloc(sizeof(engine_value));
if (val == NULL) {
errno = 1;
return NULL;
}
val->internal = _value_init();
val->kind = KIND_NULL;
errno = 0;
return val;
}
// Creates a complete copy of a zval.
// The destination zval needs to be correctly initialized before use.
void value_copy(zval *dst, zval *src) {
ZVAL_COPY_VALUE(dst, src);
zval_copy_ctor(dst);
}
// Returns engine value type. Usually compared against KIND_* constants, defined
// in the `value.h` header file.
int value_kind(engine_value *val) {
return val->kind;
}
// Set type and value to null.
void value_set_null(engine_value *val) {
ZVAL_NULL(val->internal);
val->kind = KIND_NULL;
}
// Set type and value to integer.
void value_set_long(engine_value *val, long int num) {
ZVAL_LONG(val->internal, num);
val->kind = KIND_LONG;
}
// Set type and value to floating point.
void value_set_double(engine_value *val, double num) {
ZVAL_DOUBLE(val->internal, num);
val->kind = KIND_DOUBLE;
}
// Set type and value to boolean.
void value_set_bool(engine_value *val, bool status) {
ZVAL_BOOL(val->internal, status);
val->kind = KIND_BOOL;
}
// Set type and value to string.
void value_set_string(engine_value *val, char *str) {
_value_set_string(&val->internal, str);
val->kind = KIND_STRING;
}
// Set type and value to array with a preset initial size.
void value_set_array(engine_value *val, unsigned int size) {
array_init_size(val->internal, size);
val->kind = KIND_ARRAY;
}
// Set type and value to object.
void value_set_object(engine_value *val) {
object_init(val->internal);
val->kind = KIND_OBJECT;
}
// Set type and value from zval. The source zval is copied and is otherwise not
// affected.
void value_set_zval(engine_value *val, zval *src) {
int kind;
// Determine concrete type from source zval.
switch (Z_TYPE_P(src)) {
case IS_NULL:
kind = KIND_NULL;
break;
case IS_LONG:
kind = KIND_LONG;
break;
case IS_DOUBLE:
kind = KIND_DOUBLE;
break;
case IS_STRING:
kind = KIND_STRING;
break;
case IS_OBJECT:
kind = KIND_OBJECT;
break;
case IS_ARRAY:
kind = KIND_ARRAY;
HashTable *h = (Z_ARRVAL_P(src));
// Determine if array is associative or indexed. In the simplest case, a
// associative array will have different values for the number of elements
// and the index of the next free element. In cases where the number of
// elements and the next free index is equal, we must iterate through
// the hash table and check the keys themselves.
if (h->nNumOfElements != h->nNextFreeElement) {
kind = KIND_MAP;
break;
}
unsigned long i = 0;
for (zend_hash_internal_pointer_reset(h); i < h->nNumOfElements; i++) {
unsigned long index;
int type = _value_current_key_get(h, NULL, &index);
if (type == HASH_KEY_IS_STRING || index != i) {
kind = KIND_MAP;
break;
}
zend_hash_move_forward(h);
}
break;
default:
// Booleans need special handling for different PHP versions.
if (_value_truth(src) != -1) {
kind = KIND_BOOL;
break;
}
errno = 1;
return;
}
value_copy(val->internal, src);
val->kind = kind;
errno = 0;
}
// Set next index of array or map value.
void value_array_next_set(engine_value *arr, engine_value *val) {
add_next_index_zval(arr->internal, val->internal);
}
void value_array_index_set(engine_value *arr, unsigned long idx, engine_value *val) {
add_index_zval(arr->internal, idx, val->internal);
arr->kind = KIND_MAP;
}
void value_array_key_set(engine_value *arr, const char *key, engine_value *val) {
add_assoc_zval(arr->internal, key, val->internal);
arr->kind = KIND_MAP;
}
void value_object_property_set(engine_value *obj, const char *key, engine_value *val) {
add_property_zval(obj->internal, key, val->internal);
}
int value_get_long(engine_value *val) {
zval tmp;
// Return value directly if already in correct type.
if (val->kind == KIND_LONG) {
return Z_LVAL_P(val->internal);
}
value_copy(&tmp, val->internal);
convert_to_long(&tmp);
return Z_LVAL(tmp);
}
double value_get_double(engine_value *val) {
zval tmp;
// Return value directly if already in correct type.
if (val->kind == KIND_DOUBLE) {
return Z_DVAL_P(val->internal);
}
value_copy(&tmp, val->internal);
convert_to_double(&tmp);
return Z_DVAL(tmp);
}
bool value_get_bool(engine_value *val) {
zval tmp;
// Return value directly if already in correct type.
if (val->kind == KIND_BOOL) {
return _value_truth(val->internal);
}
value_copy(&tmp, val->internal);
convert_to_boolean(&tmp);
return _value_truth(&tmp);
}
char *value_get_string(engine_value *val) {
zval tmp;
int result;
switch (val->kind) {
case KIND_STRING:
value_copy(&tmp, val->internal);
break;
case KIND_OBJECT:
result = zend_std_cast_object_tostring(val->internal, &tmp, IS_STRING);
if (result == FAILURE) {
ZVAL_EMPTY_STRING(&tmp);
}
break;
default:
value_copy(&tmp, val->internal);
convert_to_cstring(&tmp);
}
int len = Z_STRLEN(tmp) + 1;
char *str = malloc(len);
memcpy(str, Z_STRVAL(tmp), len);
zval_dtor(&tmp);
return str;
}
unsigned int value_array_size(engine_value *arr) {
switch (arr->kind) {
case KIND_ARRAY:
case KIND_MAP:
return Z_ARRVAL_P(arr->internal)->nNumOfElements;
case KIND_OBJECT:
// Object size is determined by the number of properties, regardless of
// visibility.
return Z_OBJPROP_P(arr->internal)->nNumOfElements;
case KIND_NULL:
// Null values are considered empty.
return 0;
}
// Non-array or object values are considered to be single-value arrays.
return 1;
}
engine_value *value_array_keys(engine_value *arr) {
HashTable *h = NULL;
engine_value *keys = value_new();
value_set_array(keys, value_array_size(arr));
switch (arr->kind) {
case KIND_ARRAY:
case KIND_MAP:
case KIND_OBJECT:
if (arr->kind == KIND_OBJECT) {
h = Z_OBJPROP_P(arr->internal);
} else {
h = Z_ARRVAL_P(arr->internal);
}
unsigned long i = 0;
for (zend_hash_internal_pointer_reset(h); i < h->nNumOfElements; i++) {
_value_current_key_set(h, keys);
zend_hash_move_forward(h);
}
break;
case KIND_NULL:
// Null values are considered empty.
break;
default:
// Non-array or object values are considered to contain a single key, '0'.
add_next_index_long(keys->internal, 0);
}
return keys;
}
void value_array_reset(engine_value *arr) {
HashTable *h = NULL;
switch (arr->kind) {
case KIND_ARRAY:
case KIND_MAP:
h = Z_ARRVAL_P(arr->internal);
break;
case KIND_OBJECT:
h = Z_OBJPROP_P(arr->internal);
break;
default:
return;
}
zend_hash_internal_pointer_reset(h);
}
engine_value *value_array_next_get(engine_value *arr) {
HashTable *ht = NULL;
engine_value *val = value_new();
switch (arr->kind) {
case KIND_ARRAY:
case KIND_MAP:
ht = Z_ARRVAL_P(arr->internal);
break;
case KIND_OBJECT:
ht = Z_OBJPROP_P(arr->internal);
break;
default:
// Attempting to return the next index of a non-array value will return
// the value itself, allowing for implicit conversions of scalar values
// to arrays.
value_set_zval(val, arr->internal);
return val;
}
_value_array_next_get(ht, val);
return val;
}
engine_value *value_array_index_get(engine_value *arr, unsigned long idx) {
HashTable *ht = NULL;
engine_value *val = value_new();
switch (arr->kind) {
case KIND_ARRAY:
case KIND_MAP:
ht = Z_ARRVAL_P(arr->internal);
break;
case KIND_OBJECT:
ht = Z_OBJPROP_P(arr->internal);
break;
default:
// Attempting to return the first index of a non-array value will return
// the value itself, allowing for implicit conversions of scalar values
// to arrays.
if (idx == 0) {
value_set_zval(val, arr->internal);
return val;
}
return val;
}
_value_array_index_get(ht, idx, val);
return val;
}
engine_value *value_array_key_get(engine_value *arr, char *key) {
HashTable *ht = NULL;
engine_value *val = value_new();
switch (arr->kind) {
case KIND_ARRAY:
case KIND_MAP:
ht = Z_ARRVAL_P(arr->internal);
break;
case KIND_OBJECT:
ht = Z_OBJPROP_P(arr->internal);
break;
default:
return val;
}
_value_array_key_get(ht, key, val);
return val;
}
#include "_value.c"