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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
|
/**
* ClassImpl.cpp
*
* Implementation file for the ClassImpl class
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2014 Copernica BV
*/
#include "includes.h"
/**
* Set up namespace
*/
namespace Php {
/**
* Destructor
*/
ClassImpl::~ClassImpl()
{
// destruct the entries
if (_entries) delete[] _entries;
// php 5.3 deallocates the doc_comment by iself
#if PHP_VERSION_ID >= 50400
if (_comment) free(_comment);
#endif
}
/**
* @todo refactor so that methods become simpler
*/
/**
* Retrieve our C++ implementation object
* @param entry
* @return ClassImpl
*/
static ClassImpl *self(zend_class_entry *entry)
{
// we need the base class (in user space the class may have been overridden,
// but we are not interested in these user space classes)
while (entry->parent) entry = entry->parent;
#if PHP_VERSION_ID >= 50400
// retrieve the comment (it has a pointer hidden in it to the ClassBase object)
const char *comment = entry->info.user.doc_comment;
#else
// retrieve the comment php5.3 style (it has a pointer hidden in it to the ClassBase object)
const char *comment = entry->doc_comment;
#endif
// the first byte of the comment is an empty string (null character), but
// the next bytes contain a pointer to the ClassBase class
return *((ClassImpl **)(comment + 1));
}
/**
* Extended zend_internal_function structure that we use to store an
* instance of the ClassBase object. We need this for static method calls
*/
struct CallData
{
// the internal function is the first member, so
// that it is possible to cast an instance of this
// struct to a zend_internal_function
zend_internal_function func;
// and a pointer to the ClassImpl object
ClassImpl *self;
};
/**
* Handler function that runs the __call function
* @param ... All normal parameters for function calls
*/
void ClassImpl::callMethod(INTERNAL_FUNCTION_PARAMETERS)
{
// retrieve the originally called (and by us allocated) function object
// (this was copied from the zend engine source code, code looks way too
// ugly to be made by me)
CallData *data = (CallData *)EG(current_execute_data)->function_state.function;
zend_internal_function *func = &data->func;
// retrieve the function name
const char *name = func->function_name;
ClassBase *meta = data->self->_base;
// the data structure was allocated by ourselves in the getMethod or
// getStaticMethod functions, we no longer need it now
efree(data);
// the function could throw an exception
try
{
// wrap the return value
Value result(return_value, true);
// construct parameters
ParametersImpl params(this_ptr, ZEND_NUM_ARGS() TSRMLS_CC);
// retrieve the base object
Base *base = params.object();
// is this a static, or a non-static call?
if (base) result = meta->callCall(base, name, params);
else result = meta->callCallStatic(name, params);
}
catch (const NotImplemented &exception)
{
// because of the two-step nature, we are going to report the error ourselves
zend_error(E_ERROR, "Undefined method %s", name);
}
catch (Exception &exception)
{
// process the exception
process(exception TSRMLS_CC);
}
}
/**
* Handler function that runs the __invoke function
* @param ... All normal parameters for function calls
*/
void ClassImpl::callInvoke(INTERNAL_FUNCTION_PARAMETERS)
{
// retrieve the originally called (and by us allocated) function object
// (this was copied from the zend engine source code, code looks way too
// ugly to be made by me)
CallData *data = (CallData *)EG(current_execute_data)->function_state.function;
// get self reference
ClassBase *meta = data->self->_base;
// the data structure was allocated by ourselves in the getMethod or
// getStaticMethod functions, we no longer need it now
efree(data);
// the function could throw an exception
try
{
// wrap the return value
Value result(return_value, true);
// construct parameters
ParametersImpl params(this_ptr, ZEND_NUM_ARGS() TSRMLS_CC);
// retrieve the base object
Base *base = params.object();
// call the actual __invoke method on the base object
result = meta->callInvoke(base, params);
}
catch (const NotImplemented &exception)
{
// because of the two-step nature, we are going to report the error ourselves
zend_error(E_ERROR, "Function name must be a string");
}
catch (Exception &exception)
{
// process the exception
process(exception TSRMLS_CC);
}
}
/**
* Method that returns the function definition of the __call function
* @param object_ptr
* @param method_name
* @param method_len
* @param tsrm_ls
* @return zend_function
*/
#if PHP_VERSION_ID < 50399
zend_function *ClassImpl::getMethod(zval **object_ptr, char *method_name, int method_len TSRMLS_DC)
#else
zend_function *ClassImpl::getMethod(zval **object_ptr, char *method_name, int method_len, const zend_literal *key TSRMLS_DC)
#endif
{
// something strange about the Zend engine (once more). The structure with
// object-handlers has a get_method and call_method member. When a function is
// called, the get_method function is called first, to retrieve information
// about the method (like the handler that should be called to execute it),
// after that, this returned handler is also called. The call_method property
// of the object_handlers structure however, never gets called. Typical.
// first we'll check if the default handler does not have an implementation,
// in that case the method is probably already implemented as a regular method
#if PHP_VERSION_ID < 50399
auto *defaultFunction = std_object_handlers.get_method(object_ptr, method_name, method_len TSRMLS_CC);
#else
auto *defaultFunction = std_object_handlers.get_method(object_ptr, method_name, method_len, key TSRMLS_CC);
#endif
// did the default implementation do anything?
if (defaultFunction) return defaultFunction;
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(*object_ptr TSRMLS_CC);
// this is peculiar behavior of the zend engine, we first are going to dynamically
// allocate memory holding all the properties of the __call method (we initially
// had an implementation here that used a static variable, and that worked too,
// but we'll follow thread safe implementation of the Zend engine here, although
// it is strange to allocate and free memory in one and the same method call (free()
// call happens in call_method())
auto *data = (CallData *)emalloc(sizeof(CallData));
auto *function = &data->func;
// we're going to set all properties
function->type = ZEND_INTERNAL_FUNCTION;
function->module = nullptr;
function->handler = &ClassImpl::callMethod;
function->arg_info = nullptr;
function->num_args = 0;
function->required_num_args = 0;
function->scope = entry;
function->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
function->function_name = method_name;
// store pointer to ourselves
data->self = self(entry);
// done (cast to zend_function* is allowed, because a zend_function is a union
// that has one member being a zend_internal_function)
return (zend_function *)data;
}
/**
* Method that is called right before a static method call is attempted
* @param entry
* @param method
* @param method_len
* @param tsrm_ls
* @return zend_function
*/
zend_function *ClassImpl::getStaticMethod(zend_class_entry *entry, char* method, int method_len TSRMLS_DC)
{
// first we'll check if the default handler does not have an implementation,
// in that case the method is probably already implemented as a regular method
#if PHP_VERSION_ID < 50399
auto *defaultFunction = zend_std_get_static_method(entry, method, method_len TSRMLS_CC);
#else
auto *defaultFunction = zend_std_get_static_method(entry, method, method_len, nullptr TSRMLS_CC);
#endif
// did the default implementation do anything?
if (defaultFunction) return defaultFunction;
// just like we did in getMethod() (see comment there) we are going to dynamically
// allocate data holding information about the function
auto *data = (CallData *)emalloc(sizeof(CallData));
auto *function = &data->func;
// we're going to set all properties
function->type = ZEND_INTERNAL_FUNCTION;
function->module = nullptr;
function->handler = ClassImpl::callMethod;
function->arg_info = nullptr;
function->num_args = 0;
function->required_num_args = 0;
function->scope = nullptr;
function->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
function->function_name = method;
// store pointer to ourselves
data->self = self(entry);
// done (cast to zend_function* is allowed, because a zend_function is a union
// that has one member being a zend_internal_function)
return (zend_function *)data;
}
/**
* Method that returns the closure -- this is the __invoke handler!
* @param object
* @param entry_ptr
* @param func
* @param object_ptr
* @param tsrm_ls
* @return int
*/
int ClassImpl::getClosure(zval *object, zend_class_entry **entry_ptr, zend_function **func, zval **object_ptr TSRMLS_DC)
{
// it is really unbelievable how the Zend engine manages to implement every feature
// in a complete different manner. You would expect the __invoke() and the
// __call() functions not to be very different from each other. However, they
// both have a completely different API. This getClosure method is supposed
// to fill the function parameter with all information about the invoke()
// method that is going to get called
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(object TSRMLS_CC);
// just like we did for getMethod(), we're going to dynamically allocate memory
// with all information about the function
auto *data = (CallData *)emalloc(sizeof(CallData));
auto *function = &data->func;
// we're going to set all properties
function->type = ZEND_INTERNAL_FUNCTION;
function->module = nullptr;
function->handler = &ClassImpl::callInvoke;
function->arg_info = nullptr;
function->num_args = 0;
function->required_num_args = 0;
function->scope = entry;
function->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
function->function_name = nullptr;
// store pointer to ourselves
data->self = self(entry);
// assign this dynamically allocated variable to the func parameter
// the case is ok, because zend_internal_function is a member of the
// zend_function union
*func = (zend_function *)data;
// the object_ptr should be filled with the object on which the method is
// called (otherwise the Zend engine tries to call the method statically)
*object_ptr = object;
// done
return SUCCESS;
};
/**
* Retrieve pointer to our own object handlers
* @return zend_object_handlers
*/
zend_object_handlers *ClassImpl::objectHandlers()
{
// already initialized?
if (_initialized) return &_handlers;
// initialize the handlers
memcpy(&_handlers, &std_object_handlers, sizeof(zend_object_handlers));
// install custom clone function
if (!_base->clonable()) _handlers.clone_obj = nullptr;
else _handlers.clone_obj = &ClassImpl::cloneObject;
// functions for the Countable interface
_handlers.count_elements = &ClassImpl::countElements;
// functions for the ArrayAccess interface
_handlers.write_dimension = &ClassImpl::writeDimension;
_handlers.read_dimension = &ClassImpl::readDimension;
_handlers.has_dimension = &ClassImpl::hasDimension;
_handlers.unset_dimension = &ClassImpl::unsetDimension;
// functions for the magic properties handlers (__get, __set, __isset and __unset)
_handlers.write_property = &ClassImpl::writeProperty;
_handlers.read_property = &ClassImpl::readProperty;
_handlers.has_property = &ClassImpl::hasProperty;
_handlers.unset_property = &ClassImpl::unsetProperty;
// when a method is called (__call and __invoke)
_handlers.get_method = &ClassImpl::getMethod;
_handlers.get_closure = &ClassImpl::getClosure;
// handler to cast to a different type
_handlers.cast_object = &ClassImpl::cast;
// method to compare two objects
_handlers.compare_objects = &ClassImpl::compare;
// remember that object is now initialized
_initialized = true;
// done
return &_handlers;
}
/**
* Alternative way to retrieve object handlers, given a class entry
* @param entry
* @return zend_object_handlers
*/
zend_object_handlers *ClassImpl::objectHandlers(zend_class_entry *entry)
{
return self(entry)->objectHandlers();
}
/**
* Function to compare two objects
* @param val1
* @param val2
* @param tsrm_ls
* @return int
*/
int ClassImpl::compare(zval *val1, zval *val2 TSRMLS_DC)
{
// prevent exceptions
try
{
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(val1 TSRMLS_CC);
// other object must be of the same type
if (entry != zend_get_class_entry(val2 TSRMLS_CC)) throw NotImplemented();
// we need the C++ class meta-information object
ClassBase *meta = self(entry)->_base;
// get the base objects
Base *object1 = ObjectImpl::find(val1 TSRMLS_CC)->object();
Base *object2 = ObjectImpl::find(val2 TSRMLS_CC)->object();
// run the compare method
return meta->callCompare(object1, object2);
}
catch (const NotImplemented &exception)
{
// it was not implemented, do we have a default?
if (!std_object_handlers.compare_objects) return 1;
// call default
return std_object_handlers.compare_objects(val1, val2 TSRMLS_CC);
}
catch (Exception &exception)
{
// a Php::Exception was thrown by the extension __compare function,
// pass this on to user space
process(exception TSRMLS_CC);
// what shall we return here...
return 1;
}
}
/**
* Function to cast the object to a different type
* @param val
* @param retval
* @param type
* @param tsrm_ls
* @return int
*/
int ClassImpl::cast(zval *val, zval *retval, int type TSRMLS_DC)
{
// get the base c++ object
Base *object = ObjectImpl::find(val TSRMLS_CC)->object();
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(val TSRMLS_CC);
// we need the C++ class meta-information object
ClassBase *meta = self(entry)->_base;
// retval it not yet initialized --- and again feelings of disbelief,
// frustration, wonder and anger come up when you see that there are not two
// functions in the Zend engine that have a comparable API
INIT_PZVAL(retval);
// wrap zval in value object
Value result(retval, true);
// when the magic function it not implemented, an exception will be thrown,
// and the extension may throw a Php::Exception
try
{
// the result zval
zval *result = nullptr;
// check type
switch ((Type)type) {
case Type::Numeric: result = meta->callToInteger(object).detach(); break;
case Type::Float: result = meta->callToFloat(object).detach(); break;
case Type::Bool: result = meta->callToBool(object).detach(); break;
case Type::String: result = meta->callToString(object).detach(); break;
default: throw NotImplemented(); break;
}
// @todo do we turn into endless conversion if the __toString object returns 'this' ??
// (and if it does: who cares? If the extension programmer is stupid, why do we have to suffer?)
// is the original parameter overwritten?
if (val == retval) zval_dtor(val);
// overwrite the result
ZVAL_ZVAL(retval, result, 1, 1);
// done
return SUCCESS;
}
catch (const NotImplemented &exception)
{
// is there a default?
if (!std_object_handlers.cast_object) return FAILURE;
// call default
return std_object_handlers.cast_object(val, retval, type TSRMLS_CC);
}
catch (Exception &exception)
{
// pass on the exception to php userspace
process(exception TSRMLS_CC);
// done
return FAILURE;
}
}
/**
* Function that is called to create space for a cloned object
* @param val The object to be cloned
* @return zend_obejct_value The object to be created
*/
zend_object_value ClassImpl::cloneObject(zval *val TSRMLS_DC)
{
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(val TSRMLS_CC);
// we need the C++ class meta-information object
ClassImpl *impl = self(entry);
ClassBase *meta = impl->_base;
// retrieve the old object, which we are going to copy
ObjectImpl *old_object = ObjectImpl::find(val TSRMLS_CC);
// create a new base c++ object
auto *cpp = meta->clone(old_object->object());
// report error on failure (this does not occur because the cloneObject()
// method is only installed as handler when we have seen that there is indeed
// a copy constructor). Because this function is directly called from the
// Zend engine, we can call zend_error() (which does a longjmp()) to throw
// an exception back to the Zend engine)
if (!cpp) zend_error(E_ERROR, "Unable to clone %s", entry->name);
// the thing we're going to return
zend_object_value result;
// set the handlers
result.handlers = impl->objectHandlers();
// store the object
ObjectImpl *new_object = new ObjectImpl(entry, cpp TSRMLS_CC);
// store the object in the object cache
result.handle = new_object->handle();
// clone the members (this will also call the __clone() function if the user
// had registered that as a visible method)
zend_objects_clone_members(new_object->php(), result, old_object->php(), Z_OBJ_HANDLE_P(val) TSRMLS_CC);
// was a custom clone method installed? If not we call the magic c++ __clone method
if (!entry->clone) meta->callClone(cpp);
// done
return result;
}
/**
* Function that is used to count the number of elements in the object
*
* If the user has implemented the Countable interface, this method will
* call the count() method
*
* @param val
* @param count
* @return int
*/
int ClassImpl::countElements(zval *object, long *count TSRMLS_DC)
{
// does it implement the countable interface?
Countable *countable = dynamic_cast<Countable*>(ObjectImpl::find(object TSRMLS_CC)->object());
// if it does not implement the Countable interface, we rely on the default implementation
if (countable)
{
// the user function may throw an exception that needs to be processed
try
{
// call the count function
*count = countable->count();
// done
return SUCCESS;
}
catch (Exception &exception)
{
// process the exception
process(exception TSRMLS_CC);
// unreachable
return FAILURE;
}
}
else
{
// Countable interface was not implemented, check if there is a default
if (!std_object_handlers.count_elements) return FAILURE;
// call default
return std_object_handlers.count_elements(object, count TSRMLS_CC);
}
}
/**
* Function that is called when the object is used as an array in PHP
*
* This is the [] operator in PHP, and mapped to the offsetGet() method
* of the ArrayAccess interface
*
* @param object The object on which it is called
* @param offset The name of the property
* @param type The type of the variable???
* @param tsrm_ls
* @return zval
*/
zval *ClassImpl::readDimension(zval *object, zval *offset, int type TSRMLS_DC)
{
// what to do with the type?
//
// the type parameter tells us whether the dimension was read in READ
// mode, WRITE mode, READWRITE mode or UNSET mode.
//
// In 99 out of 100 situations, it is called in regular READ mode (value 0),
// only when it is called from a PHP script that has statements like
// $x =& $object["x"], $object["x"]["y"] = "something" or unset($object["x"]["y"]),
// the type parameter is set to a different value.
//
// But we must ask ourselves the question what we should be doing with such
// cases. Internally, the object most likely has a full native implementation,
// and the property that is returned is just a string or integer or some
// other value, that is temporary WRAPPED into a zval to make it accessible
// from PHP. If someone wants to get a reference to such an internal variable,
// that is in most cases simply impossible.
// does it implement the arrayaccess interface?
ArrayAccess *arrayaccess = dynamic_cast<ArrayAccess*>(ObjectImpl::find(object TSRMLS_CC)->object());
// if it does not implement the ArrayAccess interface, we rely on the default implementation
if (arrayaccess)
{
// the C++ code may throw an exception
try
{
// ArrayAccess is implemented, call function
return toZval(arrayaccess->offsetGet(offset), type);
}
catch (Exception &exception)
{
// process the exception (send it to user space)
process(exception TSRMLS_CC);
// unreachable
return Value(nullptr).detach();
}
}
else
{
// ArrayAccess not implemented, check if there is a default handler
if (!std_object_handlers.read_dimension) return nullptr;
// call default
return std_object_handlers.read_dimension(object, offset, type TSRMLS_CC);
}
}
/**
* Function that is called when the object is used as an array in PHP
*
* This is the [] operator in PHP, and mapped to the offsetSet() method
* of the ArrayAccess interface
*
* @param object The object on which it is called
* @param offset The name of the property
* @param value The new value
* @param tsrm_ls
* @return zval
*/
void ClassImpl::writeDimension(zval *object, zval *offset, zval *value TSRMLS_DC)
{
// does it implement the arrayaccess interface?
ArrayAccess *arrayaccess = dynamic_cast<ArrayAccess*>(ObjectImpl::find(object TSRMLS_CC)->object());
// if it does not implement the ArrayAccess interface, we rely on the default implementation
if (arrayaccess)
{
// method may throw an exception
try
{
// set the value
arrayaccess->offsetSet(offset, value);
}
catch (Exception &exception)
{
// process the exception (send it to user space
process(exception TSRMLS_CC);
}
}
else
{
// ArrayAccess not interface, check if there is a default handler
if (!std_object_handlers.write_dimension) return;
// call the default
std_object_handlers.write_dimension(object, offset, value TSRMLS_CC);
}
}
/**
* Function that is called when the object is used as an array in PHP
*
* This is the [] operator in PHP, and mapped to the offsetExists() method
* of the ArrayAccess interface
*
* @param object The object on which it is called
* @param member The member to check
* @param check_empty Was this an isset() call, or an empty() call?
* @param tsrm_ls
* @return bool
*/
int ClassImpl::hasDimension(zval *object, zval *member, int check_empty TSRMLS_DC)
{
// does it implement the arrayaccess interface?
ArrayAccess *arrayaccess = dynamic_cast<ArrayAccess*>(ObjectImpl::find(object TSRMLS_CC)->object());
// if it does not implement the ArrayAccess interface, we rely on the default implementation
if (arrayaccess)
{
// user implemented callbacks could throw an exception
try
{
// check if the member exists
if (!arrayaccess->offsetExists(member)) return false;
// we know for certain that the offset exists, but should we check
// more, like whether the value is empty or not?
if (!check_empty) return true;
// the user wants to know if the property is empty
return empty(arrayaccess->offsetGet(member));
}
catch (Exception &exception)
{
// process the exception (send it to user space)
process(exception TSRMLS_CC);
// unreachable
return false;
}
}
else
{
// ArrayAccess interface is not implemented, check if there is a default handler
if (!std_object_handlers.has_dimension) return 0;
// call default
return std_object_handlers.has_dimension(object, member, check_empty TSRMLS_CC);
}
}
/**
* Function that is called when the object is used as an array in PHP
*
* This is the [] operator in PHP, and mapped to the offsetUnset() method
* of the ArrayAccess interface
*
* @param object The object on which it is called
* @param member The member to remove
* @param tsrm_ls
*/
void ClassImpl::unsetDimension(zval *object, zval *member TSRMLS_DC)
{
// does it implement the arrayaccess interface?
ArrayAccess *arrayaccess = dynamic_cast<ArrayAccess*>(ObjectImpl::find(object TSRMLS_CC)->object());
// if it does not implement the ArrayAccess interface, we rely on the default implementation
if (arrayaccess)
{
// user implemented code could throw an exception
try
{
// remove the member
arrayaccess->offsetUnset(member);
}
catch (Exception &exception)
{
// process the exception (send it to user space)
process(exception TSRMLS_CC);
}
}
else
{
// ArrayAccess is not implemented, is a default handler available?
if (!std_object_handlers.unset_dimension) return;
// call the default
std_object_handlers.unset_dimension(object, member TSRMLS_CC);
}
}
/**
* Helper method to turn a property into a zval
* @param value
* @param type
* @return Value
*/
zval *ClassImpl::toZval(Value &&value, int type)
{
// because we do not want the value object to destruct the zval when
// it falls out of scope, we detach the zval from it, if this is a regular
// read operation we can do this right away
if (type == 0) return value.detach();
// this is a more complicated read operation, the scripts wants to get
// deeper access to the returned value. This, however, is only possible
// if the value has more than once reference (if it has a refcount of one,
// the value object that we have here is the only instance of the zval,
// and it is simply impossible to return a reference or so
if (value.refcount() <= 1) return value.detach();
// we're dealing with an editable zval, return a reference variable
return Value(value.detach(), true).detach();
}
/**
* Function that is called when a property is read
* @param object
* @param name
* @param type
* @param key
* @param tsrm_ls
* @return val
*/
#if PHP_VERSION_ID < 50399
zval *ClassImpl::readProperty(zval *object, zval *name, int type TSRMLS_DC)
#else
zval *ClassImpl::readProperty(zval *object, zval *name, int type, const zend_literal *key TSRMLS_DC)
#endif
{
// what to do with the type?
//
// the type parameter tells us whether the property was read in READ
// mode, WRITE mode, READWRITE mode or UNSET mode.
//
// In 99 out of 100 situations, it is called in regular READ mode (value 0),
// only when it is called from a PHP script that has statements like
// $x =& $object->x, $object->x->y = "something" or unset($object->x->y)
// the type parameter is set to a different value.
//
// But we must ask ourselves the question what we should be doing with such
// cases. Internally, the object most likely has a full native implementation,
// and the property that is returned is just a string or integer or some
// other value, that is temporary WRAPPED into a zval to make it accessible
// from PHP. If someone wants to get a reference to such an internal variable,
// that is in most cases simply impossible.
// retrieve the object and class
Base *base = ObjectImpl::find(object TSRMLS_CC)->object();
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(object TSRMLS_CC);
// we need the C++ class meta-information object
ClassImpl *impl = self(entry);
ClassBase *meta = impl->_base;
// the default implementation throws an exception, so by catching
// the exception we know if the object was implemented by the user or not
try
{
// convert name to a Value object
Value key(name);
// is it a property with a callback?
auto iter = impl->_properties.find(key);
// was it found?
if (iter == impl->_properties.end())
{
// retrieve value from the __get method
return toZval(meta->callGet(base, key), type);
}
else
{
// get the value
return toZval(iter->second->get(base), type);
}
}
catch (const NotImplemented &exception)
{
// __get() function was not overridden by the user
if (!std_object_handlers.read_property) return nullptr;
// call default
#if PHP_VERSION_ID < 50399
return std_object_handlers.read_property(object, name, type TSRMLS_CC);
#else
return std_object_handlers.read_property(object, name, type, key TSRMLS_CC);
#endif
}
catch (Exception &exception)
{
// user threw an exception in its magic method
// implementation, send it to user space
process(exception TSRMLS_CC);
// unreachable
return Value(nullptr).detach();
}
}
/**
* Function that is called when a property is set / updated
*
* This is the handler for the __set() function, and is called when a property
* is updated.
*
* @param object The object on which it is called
* @param name The name of the property
* @param value The new value
* @param key ???
* @param tsrm_ls
* @return zval
*/
#if PHP_VERSION_ID < 50399
void ClassImpl::writeProperty(zval *object, zval *name, zval *value TSRMLS_DC)
#else
void ClassImpl::writeProperty(zval *object, zval *name, zval *value, const zend_literal *key TSRMLS_DC)
#endif
{
// retrieve the object and class
Base *base = ObjectImpl::find(object TSRMLS_CC)->object();
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(object TSRMLS_CC);
// we need the C++ class meta-information object
ClassImpl *impl = self(entry);
ClassBase *meta = impl->_base;
// the default implementation throws an exception, if we catch that
// we know for sure that the user has not overridden the __set method
try
{
// wrap the name
Value key(name);
// check if the property has a callback
auto iter = impl->_properties.find(key);
// is it set?
if (iter == impl->_properties.end())
{
// use the __set method
meta->callSet(base, key, value);
}
else
{
// check if it could be set
if (iter->second->set(base, value)) return;
// read-only property
zend_error(E_ERROR, "Unable to write to read-only property %s", (const char *)key);
}
}
catch (const NotImplemented &exception)
{
// __set() function was not overridden by user, check if there is a default
if (!std_object_handlers.write_property) return;
// call the default
#if PHP_VERSION_ID < 50399
std_object_handlers.write_property(object, name, value TSRMLS_CC);
#else
std_object_handlers.write_property(object, name, value, key TSRMLS_CC);
#endif
}
catch (Exception &exception)
{
// user threw an exception in its magic method
// implementation, send it to user space
process(exception TSRMLS_CC);
}
}
/**
* Function that is called to check whether a certain property is set
* for an object
*
* This is the handler for the __isset() function, and is called when a PHP
* script checks if a certain property is set.
*
* The has_set_exists parameter can have the following values:
*
* 0 (has) whether property exists and is not NULL
* 1 (set) whether property exists and is true
* 2 (exists) whether property exists
*
* @param object The object on which it is called
* @param name The name of the property to check
* @param has_set_exists See above
* @param key ???
* @param tsrm_ls
* @return bool
*/
#if PHP_VERSION_ID < 50399
int ClassImpl::hasProperty(zval *object, zval *name, int has_set_exists TSRMLS_DC)
#else
int ClassImpl::hasProperty(zval *object, zval *name, int has_set_exists, const zend_literal *key TSRMLS_DC)
#endif
{
// the default implementation throws an exception, if we catch that
// we know for sure that the user has not overridden the __isset method
try
{
// get the cpp object
Base *base = ObjectImpl::find(object TSRMLS_CC)->object();
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(object TSRMLS_CC);
// we need the C++ class meta-information object
ClassImpl *impl = self(entry);
ClassBase *meta = impl->_base;
// convert the name to a Value object
Value key(name);
// check if this is a callback property
if (impl->_properties.find(key) != impl->_properties.end()) return true;
// call the C++ object
if (!meta->callIsset(base, key)) return false;
// property exists, but what does the user want to know
if (has_set_exists == 2) return true;
// we have to retrieve the property
Value value = meta->callGet(base, key);
// should we check on NULL?
switch (has_set_exists) {
case 0: return value.type() != Type::Null;
default: return value.boolValue();
}
}
catch (const NotImplemented &exception)
{
// __isset was not implemented, do we have a default?
if (!std_object_handlers.has_property) return 0;
// call default
#if PHP_VERSION_ID < 50399
return std_object_handlers.has_property(object, name, has_set_exists TSRMLS_CC);
#else
return std_object_handlers.has_property(object, name, has_set_exists, key TSRMLS_CC);
#endif
}
catch (Exception &exception)
{
// user threw an exception in its magic method
// implementation, send it to user space
process(exception TSRMLS_CC);
// unreachable
return false;
}
}
/**
* Function that is called when a property is removed from the project
*
* This is the handler for the __unset() method
*
* @param object The object on which it is called
* @param member The member to remove
* @param key
* @param tsrm_ls
*/
#if PHP_VERSION_ID < 50399
void ClassImpl::unsetProperty(zval *object, zval *member TSRMLS_DC)
#else
void ClassImpl::unsetProperty(zval *object, zval *member, const zend_literal *key TSRMLS_DC)
#endif
{
// the default implementation throws an exception, if we catch that
// we know for sure that the user has not overridden the __unset method
try
{
// retrieve the class entry linked to this object
auto *entry = zend_get_class_entry(object TSRMLS_CC);
// we need the C++ class meta-information object
ClassImpl *impl = self(entry);
// property name
Value name(member);
// is this a callback property?
auto iter = impl->_properties.find(name);
// if the property does not exist, we forward to the __unset
if (iter == impl->_properties.end()) impl->_base->callUnset(ObjectImpl::find(object TSRMLS_CC)->object(), member);
// callback properties cannot be unset
zend_error(E_ERROR, "Property %s can not be unset", (const char *)name);
}
catch (const NotImplemented &exception)
{
// __unset was not implemented, do we have a default?
if (!std_object_handlers.unset_property) return;
// call the default
#if PHP_VERSION_ID < 50399
std_object_handlers.unset_property(object, member TSRMLS_CC);
#else
std_object_handlers.unset_property(object, member, key TSRMLS_CC);
#endif
}
catch (Exception &exception)
{
// user threw an exception in its magic method
// implementation, send it to user space
process(exception TSRMLS_CC);
}
}
/**
* Function that is called when an object is about to be destructed
* This will call the magic __destruct method
* @param object
* @param handle
* @param tsrm_ls
*/
void ClassImpl::destructObject(zend_object *object, zend_object_handle handle TSRMLS_DC)
{
// find object
ObjectImpl *obj = ObjectImpl::find(object);
// get meta info
ClassImpl *impl = self(object->ce);
// prevent exceptions
try
{
// call the destruct function
if (obj->object()) impl->_base->callDestruct(obj->object());
}
catch (const NotImplemented &exception)
{
// fallback on the default destructor call
zend_objects_destroy_object(object, handle TSRMLS_CC);
}
catch (Exception &exception)
{
// a regular Php::Exception was thrown by the extension, pass it on
// to PHP user space
process(exception TSRMLS_CC);
}
}
/**
* Function that is called to clean up space that is occupied by the object
* @param object The object to be deallocated
* @param tsrm_ls
*/
void ClassImpl::freeObject(zend_object *object TSRMLS_DC)
{
// allocate memory for the object
ObjectImpl *obj = ObjectImpl::find(object);
// no longer need it
obj->destruct(TSRMLS_C);
}
/**
* Function that is called when an instance of the class needs to be created.
* This function will create the C++ class, and the PHP object
* @param entry Pointer to the class information
* @param tsrm_ls
* @return zend_object_value The newly created object
*/
zend_object_value ClassImpl::createObject(zend_class_entry *entry TSRMLS_DC)
{
// we need the C++ class meta-information object
ClassImpl *impl = self(entry);
// create a new base C++ object
auto *cpp = impl->_base->construct();
// report error on failure, because this function is called directly from the
// Zend engine, we can call zend_error() here (which does a longjmp() back to
// the Zend engine)
if (!cpp) zend_error(E_ERROR, "Unable to instantiate %s", entry->name);
// the thing we're going to return
zend_object_value result;
// set the handlers
result.handlers = impl->objectHandlers();
// create the object in the zend engine
ObjectImpl *object = new ObjectImpl(entry, cpp TSRMLS_CC);
// store the object in the object cache
result.handle = object->handle();
// done
return result;
}
/**
* Function to create a new iterator to iterate over an object
* @param entry The class entry
* @param object The object to iterate over
* @param by_ref ?????
* @param tsrm_ls
* @return zend_object_iterator* Pointer to the iterator
*/
zend_object_iterator *ClassImpl::getIterator(zend_class_entry *entry, zval *object, int by_ref TSRMLS_DC)
{
// by-ref is not possible (copied from SPL), this function is called directly
// from the Zend engine, so we can use zend_error() to longjmp() back to the
// Zend engine)
if (by_ref) zend_error(E_ERROR, "Foreach by ref is not possible");
// retrieve the traversable object
Traversable *traversable = dynamic_cast<Traversable*>(ObjectImpl::find(object TSRMLS_CC)->object());
// user may throw an exception in the getIterator() function
try
{
// create an iterator
auto *iterator = new IteratorImpl(traversable->getIterator());
// return the implementation
return iterator->implementation();
}
catch (Exception &exception)
{
// user threw an exception in its method
// implementation, send it to user space
process(exception TSRMLS_CC);
// unreachable
return nullptr;
}
}
/**
* Method that is called to serialize an object
* @param object The object to be serialized
* @param buffer Buffer in which to store the data
* @param buf_len Size of the bufffer
* @param data ??
* @param tsrm_ls
* @return int
*/
int ClassImpl::serialize(zval *object, unsigned char **buffer, zend_uint *buf_len, zend_serialize_data *data TSRMLS_DC)
{
// get the serializable object
Serializable *serializable = dynamic_cast<Serializable*>(ObjectImpl::find(object TSRMLS_CC)->object());
// call the serialize method on the object
auto value = serializable->serialize();
// allocate the buffer, and copy the data into it (the zend engine will
// (hopefully) clean up the data for us - the default serialize method does
// it like this too)
*buffer = (unsigned char*)estrndup(value.c_str(), value.size());
*buf_len = value.size();
// done
return SUCCESS;
}
/**
* Method that is called to unserialize an object
* @param object The object to be unserialized
* @param entry The class entry to which is belongs
* @param buffer Buffer holding the unserialized data
* @param data All the unserialize data
* @param tsrm_ls
* @return int
*/
int ClassImpl::unserialize(zval **object, zend_class_entry *entry, const unsigned char *buffer, zend_uint buf_len, zend_unserialize_data *data TSRMLS_DC)
{
// create the PHP object
object_init_ex(*object, entry);
// turn this into a serializale
Serializable *serializable = dynamic_cast<Serializable*>(ObjectImpl::find(*object TSRMLS_CC)->object());
// call the unserialize method on it
serializable->unserialize((const char *)buffer, buf_len);
// done
return SUCCESS;
}
/**
* Retrieve an array of zend_function_entry objects that hold the
* properties for each method. This method is called at extension
* startup time to register all methods.
*
* @param classname The class name
* @return zend_function_entry[]
*/
const struct _zend_function_entry *ClassImpl::entries()
{
// already initialized?
if (_entries) return _entries;
// allocate memory for the functions
_entries = new zend_function_entry[_methods.size() + 1];
// keep iterator counter
int i = 0;
// loop through the functions
for (auto &method : _methods)
{
// retrieve entry
zend_function_entry *entry = &_entries[i++];
// let the function fill the entry
method->initialize(entry, _name);
}
// last entry should be set to all zeros
zend_function_entry *last = &_entries[i];
// all should be set to zero
memset(last, 0, sizeof(zend_function_entry));
// done
return _entries;
}
/**
* Initialize the class, given its name
*
* The module functions are registered on module startup, but classes are
* initialized afterwards. The Zend engine is a strange thing. Nevertheless,
* this means that this method is called after the module is already available.
* This function will inform the Zend engine about the existence of the
* class.
*
* @param base the c++ class object created in the extension
* @param prefix namespace prefix
* @param tsrm_ls
*/
void ClassImpl::initialize(ClassBase *base, const std::string &prefix TSRMLS_DC)
{
// store base pointer
_base = base;
// the class entry
zend_class_entry entry;
// update the name
if (prefix.size() > 0) _name = prefix + "\\" + _name;
// initialize the class entry
INIT_CLASS_ENTRY_EX(entry, _name.c_str(), _name.size(), entries());
// we need a special constructor
entry.create_object = &ClassImpl::createObject;
// register function that is called for static method calls
entry.get_static_method = &ClassImpl::getStaticMethod;
// for traversable classes we install a special method to get the iterator
if (_base->traversable()) entry.get_iterator = &ClassImpl::getIterator;
// for serializable classes, we install callbacks for serializing and unserializing
if (_base->serializable())
{
// add handlers to serialize and unserialize
entry.serialize = &ClassImpl::serialize;
entry.unserialize = &ClassImpl::unserialize;
}
// do we have a base class?
if (_parent)
{
// check if the base class was already defined
if (_parent->_entry)
{
// register the class
_entry = zend_register_internal_class_ex(&entry, _parent->_entry, const_cast<char*>(_parent->name().c_str()) TSRMLS_CC);
}
else
{
// report an error - the extension programmer probably made an error
std::cerr << "Derived class " << name() << " is initialized before base class " << _parent->name() << ": base class is ignored" << std::endl;
// register the class, but without the base class
_entry = zend_register_internal_class(&entry TSRMLS_CC);
}
}
else
{
// register the class
_entry = zend_register_internal_class(&entry TSRMLS_CC);
}
// register the classes
for (auto &interface : _interfaces)
{
// register this interface
if (interface->_entry) zend_class_implements(_entry TSRMLS_CC, 1, interface->_entry);
// otherwise report an error
else std::cerr << "Derived class " << name() << " is initialized before base class " << interface->name() << ": interface is ignored" << std::endl;
}
// allocate doc comment to contain an empty string + a hidden pointer
if (!_comment)
{
// allocate now
_comment = (char *)malloc(1 + sizeof(ClassBase *));
// empty string on first position
_comment[0] = '\0';
// this pointer has to be copied to temporary pointer, as &this causes compiler error
ClassImpl *impl = this;
// copy the 'this' pointer to the doc-comment
memcpy(_comment+1, &impl, sizeof(ClassImpl *));
}
// store pointer to the class in the unused doc_comment member
#if PHP_VERSION_ID >= 50400
_entry->info.user.doc_comment = _comment;
#else
// and store the wrapper inside the comment
_entry->doc_comment = _comment;
#endif
// set access types flags for class
_entry->ce_flags = (int)_type;
// declare all member variables
for (auto &member : _members) member->initialize(_entry TSRMLS_CC);
}
/**
* End namespace
*/
}
|