forked from do-aki/php-ext-perl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_perl.c
More file actions
1355 lines (1195 loc) · 44.9 KB
/
php_perl.c
File metadata and controls
1355 lines (1195 loc) · 44.9 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
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
/**
* php-ext-perl
*
* Embeds perl interpreter into PHP and allows execution and manipulation of
* Perl from within PHP code
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if HAVE_PERL
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
#include <perliol.h> /* from the Perl distribution */
#include <perlapi.h> /* from the Perl distribution */
#undef END_EXTERN_C /* bypass macros redeclaration warning */
#include "php.h"
#include "ext/standard/info.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_smart_str.h"
#include "SAPI.h"
#include "php_perl.h"
#include "perl_arginfo.h"
#include "sv_to_zv.h"
#include "zv_to_sv.h"
ZEND_DECLARE_MODULE_GLOBALS(perl)
/****************************************************************************/
/* This code was produced by `perl -MExtUtils::Embed -e xsinit` */
EXTERN_C void xs_init (pTHX);
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void
xs_init(pTHX)
{
static const char file[] = __FILE__;
dXSUB_SYS;
PERL_UNUSED_CONTEXT;
/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}
/****************************************************************************/
/** Perl global management and extension definition below */
// PerlIO functions - redirects perl stdout to zend output buffer
static SV* PerlIOPHP_getarg(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
{
Perl_croak(aTHX_ "an attempt to getarg from a stale io handle");
return NULL;
}
static SSize_t PerlIOPHP_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
return zend_write(vbuf, count);
}
static IV PerlIOPHP_flush(pTHX_ PerlIO *f)
{
sapi_flush();
return 0;
}
static IV PerlIOPHP_noop_fail(pTHX_ PerlIO *f)
{
return -1;
}
static PerlIO_funcs PerlIO_PHP = {
sizeof(PerlIO_funcs),
"PHP",
sizeof(struct _PerlIO),
PERLIO_K_MULTIARG | PERLIO_K_RAW,
PerlIOBase_pushed,
PerlIOBase_popped,
NULL,
PerlIOBase_binmode,
PerlIOPHP_getarg,
PerlIOBase_fileno,
PerlIOBase_dup,
PerlIOBase_read,
NULL,
PerlIOPHP_write,
NULL, /* can't seek on STD{IN|OUT}, fail on call*/
NULL, /* can't tell on STD{IN|OUT}, fail on call*/
PerlIOBase_close,
PerlIOPHP_flush,
PerlIOPHP_noop_fail, /* fill */
PerlIOBase_eof,
PerlIOBase_error,
PerlIOBase_clearerr,
PerlIOBase_setlinebuf,
NULL, /* get_base */
NULL, /* get_bufsiz */
NULL, /* get_ptr */
NULL, /* get_cnt */
NULL, /* set_ptrcnt */
};
/* Creates or returns perl interpreter */
static PerlInterpreter *php_perl_init()
{
PerlInterpreter *my_perl = PERL_G(my_perl);
if (my_perl == NULL) {
char *embedding[] = { "", "-e", "0" };
my_perl = perl_alloc();
perl_construct(my_perl);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_parse(my_perl, xs_init, 3, embedding, NULL);
PerlIO_define_layer(aTHX_ &PerlIO_PHP);
PerlIO_apply_layers(aTHX_ PerlIO_stdout(), "w", ":PHP");
PERL_G(my_perl) = my_perl;
zend_hash_init(&PERL_G(perl_objects), 0, NULL, NULL, 0);
}
return my_perl;
}
static inline SV* php_perl_dereference_sv(SV* sv)
{
while (SvROK(sv)) {
sv = SvRV(sv);
}
return sv;
}
/* Destroys perl interpreter */
static void php_perl_destroy()
{
PerlInterpreter *my_perl = PERL_G(my_perl);
if (my_perl != NULL) {
zend_hash_destroy(&PERL_G(perl_objects));
perl_destruct(my_perl);
perl_free(my_perl);
PERL_G(my_perl) = NULL;
}
}
/**
* Allocates and returns a zend_object of the Perl class
*/
zend_object* _php_perl_new(zend_class_entry *ce)
{
DEBUG_PRINT("php_perl_new");
php_perl_t *perl_class = zend_object_alloc(sizeof(php_perl_t), ce);
perl_class->sv = NULL;
perl_class->properties = NULL;
perl_class->context = PERL_SCALAR;
zend_object_std_init(&perl_class->std, ce);
object_properties_init(&perl_class->std, ce);
perl_class->std.handlers = &php_perl_object_handlers;
return &perl_class->std;
}
zend_object* php_perl_new()
{
return _php_perl_new(perl_ce);
}
bool zval_is_perl_obj(zval* zv)
{
return
zv != NULL
&& Z_TYPE_P(zv) == IS_OBJECT
&& Z_OBJCE_P(zv) == perl_ce;
}
static zval* php_perl_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot)
{
// Since all our properties are proxies, the VM can't ever do direct r/w
// access, so we always return null.
return NULL;
}
/* Returns all properties of Perl's object */
static HashTable* php_perl_get_properties(zend_object *obj)
{
DEBUG_PRINT("get_properties. zo %p", obj);
HashTable *props;
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(obj);
props = zend_array_dup(zend_std_get_properties(obj));
SV* sv = perlobj->sv;
if (sv == NULL) {
return props;
}
PerlInterpreter* my_perl = php_perl_init();
sv = php_perl_dereference_sv(sv);
// Non hash or array type SVs don't have properties. e.g. someone's made a
// proxy to a string and tried to var_dump it.
if (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV) {
return props;
}
zval* zv = malloc(sizeof(zval));
php_perl_sv_to_zval(my_perl, sv, zv);
props = Z_ARRVAL_P(zv);
perlobj->properties = props;
DEBUG_PRINT("finished: get_properties. zo %p. props in %p", obj, props);
return props;
}
/** Add perl internal object name to var dumps **/
static zend_string* php_perl_get_class_name(const zend_object *obj)
{
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(obj);
PerlInterpreter* my_perl = php_perl_init();
SV* sv = perlobj->sv;
smart_str class_name = {0};
smart_str_appends(&class_name, "Perl");
if (sv == NULL) {
smart_str_0(&class_name);
return class_name.s;
}
smart_str_appends(&class_name, "::");
if (!sv_isobject(sv)) {
smart_str_appends(&class_name, "proxy::");
if (SvTYPE(sv) == SVt_PVAV) {
smart_str_appends(&class_name, "array");
} else if (SvTYPE(sv) == SVt_PVHV) {
smart_str_appends(&class_name, "hash");
} else {
smart_str_appends(&class_name, "scalar");
}
smart_str_0(&class_name);
return class_name.s;
}
sv = php_perl_dereference_sv(sv);
HV* stash;
if ((stash = SvSTASH(sv)) != NULL) {
smart_str_appends(&class_name, HvNAME(stash));
} else {
smart_str_appends(&class_name, "unknown");
}
smart_str_0(&class_name);
return class_name.s;
}
/* Used to check if a property of the object exists */
/* param has_set_exists:
* 0 (has) whether property exists and is not NULL
* 1 (set) whether property exists and is true
* 2 (exists) whether property exists
*/
static int php_perl_has_property(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(object);
SV* sv = perlobj->sv;
DEBUG_PRINT("has_property for %s, %d", ZSTR_VAL(member), has_set_exists);
if (sv == NULL) {
// Check for glob in perl context
if (perlobj->context == PERL_ARRAY) {
sv = (SV*)get_av(ZSTR_VAL(member), FALSE);
if (sv && !AvARRAY(sv))
return FALSE;
} else if (perlobj->context == PERL_HASH) {
sv = (SV*)get_hv(ZSTR_VAL(member), FALSE);
if (sv && !HvARRAY(sv))
return FALSE;
} else {
sv = get_sv(ZSTR_VAL(member), FALSE);
if (sv && !SvOK(sv))
return FALSE;
}
} else {
sv = php_perl_dereference_sv(sv);
if (SvTYPE(sv) == SVt_PVHV) {
HV *hv = (HV*)sv;
SV** prop_val = hv_fetch(hv, ZSTR_VAL(member), ZSTR_LEN(member), 0);
if (prop_val == NULL)
return FALSE;
sv = *prop_val;
} else {
// TODO: Should we do this? PHP doesn't seem to even throw a notice
zend_error(E_WARNING, "[perl] Object is not a hash");
return FALSE;
}
}
if (sv == NULL)
return FALSE;
// Exists - is true if we got this far
if (has_set_exists == ZEND_PROPERTY_EXISTS)
return TRUE;
zval* zv = malloc(sizeof(zval));
php_perl_sv_to_zval(my_perl, sv, zv);
// has - property exists and is not NULL
if (has_set_exists == ZEND_PROPERTY_ISSET)
return !ZVAL_IS_NULL(zv);
// set - property exists and is true
if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY)
return zval_is_true(zv);
}
/** Convert perlObject->sv to zval **/
static zval* php_perl_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t* perl_obj = php_perl_fetch_object(object);
SV* sv = NULL;
zend_bool write =
perl_obj->context != PERL_SCALAR &&
type != BP_VAR_R &&
type != BP_VAR_IS;
DEBUG_PRINT("[%p] getting member %s (call type %d)", object, ZSTR_VAL(member), type);
if (perl_obj->context == PERL_SCALAR) {
perl_context new_context;
if (
(zend_string_equals_literal(member, "array") && (new_context = PERL_ARRAY))
|| (zend_string_equals_literal(member, "hash") && (new_context = PERL_HASH))
|| (zend_string_equals_literal(member, "scalar") && (new_context = PERL_SCALAR))
) {
DEBUG_PRINT("[%p] getting proxy obj type %d", object, new_context);
// Create new proxy object
zend_object* new_obj = php_perl_new();
php_perl_t* new_class = php_perl_fetch_object(new_obj);
new_class->context = new_context;
if (perl_obj->sv != NULL) {
new_class->sv = perl_obj->sv;
}
SvREFCNT_inc(new_class->sv);
ZVAL_OBJ(rv, new_obj);
return rv;
}
}
if (perl_obj->sv == NULL) {
if (perl_obj->context == PERL_ARRAY) {
sv = (SV*)get_av(ZSTR_VAL(member), write);
if (sv && !AvARRAY(sv)) {if (write) {av_clear((AV*)sv);} else {sv = NULL;}}
} else if (perl_obj->context == PERL_HASH) {
sv = (SV*)get_hv(ZSTR_VAL(member), write);
if (sv && !HvARRAY(sv)) {if (write) {hv_clear((HV*)sv);} else {sv = NULL;}}
} else {
sv = get_sv(ZSTR_VAL(member), FALSE);
if (sv && !SvOK(sv)) {sv = NULL;}
}
if (sv == NULL) {
if (perl_obj->context == PERL_ARRAY) {
zend_error(E_NOTICE, "[perl] Undefined variable: '@%s'", ZSTR_VAL(member));
} else if (perl_obj->context == PERL_HASH) {
zend_error(E_NOTICE, "[perl] Undefined variable: '%%%s'", ZSTR_VAL(member));
} else {
zend_error(E_NOTICE, "[perl] Undefined variable: '$%s'", ZSTR_VAL(member));
}
}
} else {
sv = perl_obj->sv;
sv = php_perl_dereference_sv(sv);
if (SvTYPE(sv) == SVt_PVHV) { /** Hash **/
HV* hv = (HV*)sv;
SV** prop_val;
prop_val = hv_fetch(hv, ZSTR_VAL(member), ZSTR_LEN(member), 0);
if (prop_val != NULL) {
sv = *prop_val;
write = FALSE;
if (sv != NULL && type != BP_VAR_R && type != BP_VAR_IS) {
write = TRUE;
SV* tmp_sv = sv;
while (1) {
if (sv_isobject(tmp_sv) ||
SvTYPE(tmp_sv) == SVt_PVAV ||
SvTYPE(tmp_sv) == SVt_PVHV) {
write = TRUE; // Return proxy object
break;
} else if (!SvROK(tmp_sv)) {
break; // Return directly translated zval
}
tmp_sv = SvRV(tmp_sv);
}
}
}
} else {
// TODO: We need to reimplement proxy objects here. Look for special
// "data" prop being set and use it to assign to the underlying SV.
// This would be the same idea as FFI::cdata.
zend_error(E_WARNING, "[perl] Object is not a hash");
}
}
if (sv != NULL) {
if (write && !sv_isobject(sv)) {
DEBUG_PRINT("[%p] getting proxy obj", object);
// Create new proxy object
zend_object* new_obj = php_perl_new();
php_perl_t* new_class = php_perl_fetch_object(new_obj);
new_class->sv = newRV(sv);
DEBUG_PRINT("[%p] proxy obj has SvRV %p", object, new_class->sv);
ZVAL_OBJ_COPY(rv, new_obj);
return rv;
} else {
php_perl_sv_to_zval(my_perl, sv, rv);
return rv;
}
}
ZVAL_NULL(rv);
return rv;
}
static zval* php_perl_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t* perl_obj = php_perl_fetch_object(object);
SV* sv = perl_obj->sv;
char* property = ZSTR_VAL(member);
DEBUG_PRINT("Writing property %s on perl_obj %p with context type %d", property, perl_obj, perl_obj->context);
if (sv == NULL && perl_obj->context == PERL_ARRAY) {
DEBUG_PRINT("perl_obj is an array proxy, sv is NULL");
if (Z_TYPE_P(value) != IS_ARRAY) {
zend_error(E_WARNING, "[perl] array assignment ignored, array required");
// "You must return the final value of the assigned property."
zval* ret = malloc(sizeof(ret)); ZVAL_NULL(ret); return ret;
}
AV* av = get_av(property, TRUE);
php_perl_zval_to_sv(my_perl, value, (SV*)av);
return value;
} else if (sv == NULL && perl_obj->context == PERL_HASH) {
DEBUG_PRINT("perl_obj is a hash proxy, sv is NULL");
if (Z_TYPE_P(value) != IS_ARRAY) {
zend_error(E_WARNING, "[perl] hash assignment ignored, array required");
zval* ret = malloc(sizeof(ret)); ZVAL_NULL(ret); return ret;
}
HV *hv = get_hv(property, TRUE);
php_perl_zval_to_sv(my_perl, value, (SV*)hv);
return value;
} else if (sv == NULL) {
DEBUG_PRINT("perl_obj is not a proxy, sv is NULL");
SV *sv = get_sv(property, TRUE);
php_perl_zval_to_sv(my_perl, value, sv);
return value;
} else {
DEBUG_PRINT("perl_obj is a scalar proxy, sv is %p", sv);
SV* origSv = sv;
sv = php_perl_dereference_sv(sv);
if (SvTYPE(sv) == SVt_PVHV) {
DEBUG_PRINT("sv is hash %p", sv);
HV* hv = (HV*)sv;
// Special check for recursion
if (zval_is_perl_obj(value) && Z_PHPPERL_P(value)->sv == perl_obj->sv) {
DEBUG_PRINT("detected set_property recursion, reacting accordingly");
hv_store(hv, property, ZSTR_LEN(member), origSv, 0);
SvREFCNT_inc(origSv);
return value;
}
SV* el_sv = newSV(0);
php_perl_zval_to_sv(my_perl, value, el_sv);
hv_store(hv, property, ZSTR_LEN(member), el_sv, 0);
return value;
} else {
zend_error(E_WARNING, "[perl] Object is not a hash");
zval* ret = malloc(sizeof(ret)); ZVAL_NULL(ret); return ret;
}
}
}
static void php_perl_unset_property(zend_object *object, zend_string *member, void **cache_slot)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(object);
SV* sv = perlobj->sv;
if (sv == NULL) {
if (perlobj->context == PERL_ARRAY) {
AV *av = get_av(ZSTR_VAL(member), FALSE);
av_undef(av);
} else if (perlobj->context == PERL_HASH) {
HV *hv = get_hv(ZSTR_VAL(member), FALSE);
hv_undef(hv);
} else {
SV *sv = get_sv(ZSTR_VAL(member), FALSE);
sv_setsv(sv, &PL_sv_undef);
}
} else {
sv = php_perl_dereference_sv(sv);
if (SvTYPE(sv) == SVt_PVHV) {
HV* hv = (HV*)sv;
hv_delete(hv, ZSTR_VAL(member), ZSTR_LEN(member), G_DISCARD);
} else {
// TODO: PHP doesn't seem to throw a notice or anything in equiv
// circumstances, should we just ignore this?
zend_error(E_WARNING, "[perl] Object is not a hash");
}
}
}
static int php_perl_has_dimension(zend_object *object, zval *member, int check_empty)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(object);
SV* sv = perlobj->sv;
DEBUG_PRINT("has_dimension for %d, %d", Z_LVAL_P(member), check_empty);
if (sv == NULL) {
zend_error(E_ERROR, "[perl] Can't do array access on a non proxy object");
return FALSE;
}
sv = php_perl_dereference_sv(sv);
if (SvTYPE(sv) == SVt_PVHV) {
// Old lib doesn't do this convert to string step but I think it should?
convert_to_string(member);
return php_perl_has_property(object, zval_get_string(member), check_empty ? ZEND_PROPERTY_NOT_EMPTY : ZEND_PROPERTY_ISSET, NULL);
}
if (SvTYPE(sv) == SVt_PVAV) {
AV* av = (AV*)sv;
convert_to_long(member);
int exists = av_exists(av, Z_LVAL_P(member));
if (exists && !check_empty)
return TRUE;
if (!exists)
return FALSE;
// It exists, but we need to check "emptiness" of the value
SV** el_sv = av_fetch(av, Z_LVAL_P(member), 0);
if (el_sv != NULL) {
zval *el_zv = malloc(sizeof(zval));
php_perl_sv_to_zval(my_perl, *el_sv, el_zv);
int ret = zend_is_true(el_zv);
zval_ptr_dtor(el_zv);
return ret;
}
return FALSE;
}
// TODO: What sort of error is most like PHP here?
zend_error(E_WARNING, "[perl] Can't do array access on a scalar proxy object");
return FALSE;
}
static void php_perl_write_dimension(zend_object *object, zval *offset, zval *value)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(object);
SV* sv = perlobj->sv;
DEBUG_PRINT("write_dimension for %d, sv %p", Z_LVAL_P(offset), sv);
if (sv == NULL) {
zend_error(E_ERROR, "[perl] Can't do array access on a non proxy object");
return;
}
sv = php_perl_dereference_sv(sv);
if (SvTYPE(sv) == SVt_PVHV) {
// Old lib doesn't do this convert to string step but I think it should?
convert_to_string(offset);
php_perl_write_property(object, zval_get_string(offset), value, NULL);
return;
}
if (SvTYPE(sv) == SVt_PVAV) {
AV* av = (AV*)sv;
convert_to_long(offset);
SV* el_sv = newSV(0);
php_perl_zval_to_sv(my_perl, value, el_sv);
av_store(av, Z_LVAL_P(offset), el_sv);
return;
}
zend_error(E_WARNING, "[perl] Object is not an array");
}
static zval* php_perl_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(object);
SV* sv = perlobj->sv;
zend_bool write =
type != BP_VAR_R &&
type != BP_VAR_IS;
DEBUG_PRINT("read_dimension for %d, write = %d", Z_LVAL_P(offset), write);
if (sv == NULL) {
zend_error(E_ERROR, "[perl] Can't do array access on a non proxy object");
ZVAL_NULL(rv);
return rv;
}
sv = php_perl_dereference_sv(sv);
if (SvTYPE(sv) == SVt_PVHV) {
// Old lib doesn't do this convert to string step but I think it should?
convert_to_string(offset);
return php_perl_read_property(object, zval_get_string(offset), type, NULL, rv);
}
if (SvTYPE(sv) != SVt_PVAV) {
zend_error(E_ERROR, "[perl] Can't do array access on a non-array proxy object");
ZVAL_NULL(rv);
return rv;
}
convert_to_long(offset);
long idx = Z_LVAL_P(offset);
AV* av = (AV*)sv;
SV** el_sv;
el_sv = av_fetch(av, idx, 0);
// We want to emulate PHP's behaviour, where a set but undef array access
// does NOT throw a warning, but an unset array access does. We could pass
// "write" as the third param to av_fetch above but we wouldn't be able to
// tell the difference between `$self->[0] = undef` and something freshly
// created
if (el_sv && !SvOK(*el_sv)) {
DEBUG_PRINT("existing but undef array key %d was accessed (el_sv %p)", *el_sv);
if (write) {
DEBUG_PRINT("accessed for write, so setting IV = 0");
sv_setiv(*el_sv, 0);
// Continue down to the proxy object stuff
} else {
DEBUG_PRINT("accessed for read, returning null");
ZVAL_NULL(rv);
return rv;
}
}
if (!el_sv) {
// Was not set, throw a warning
DEBUG_PRINT("non existing array key %d was accessed, throwing a warning", idx);
zend_error(E_WARNING, "[perl] Undefined array key %d", idx);
if (write) {
DEBUG_PRINT("accessed for write, so creating");
SV* newsv = newSViv(0);
el_sv = av_store(av, idx, newsv);
// Continue down to proxy object stuff
} else {
DEBUG_PRINT("accessed for read, returning null");
ZVAL_NULL(rv);
return rv;
}
}
if (write && !sv_isobject(sv)) {
DEBUG_PRINT("[%p] getting proxy obj for el_sv %p", object, *el_sv);
// Create new proxy object
zend_object* new_obj = php_perl_new();
php_perl_t* new_class = php_perl_fetch_object(new_obj);
new_class->sv = newRV(*el_sv);
ZVAL_OBJ(rv, new_obj);
return rv;
} else {
DEBUG_PRINT("Converting el_sv %p into rv %p", *el_sv, rv);
php_perl_sv_to_zval(my_perl, *el_sv, rv);
return rv;
}
}
static void php_perl_unset_dimension(zend_object *object, zval *offset)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(object);
SV* sv = perlobj->sv;
DEBUG_PRINT("unset_dimension %d", Z_LVAL_P(offset), write);
if (sv == NULL) {
zend_error(E_ERROR, "[perl] Can't do array access on a non proxy object");
return;
}
sv = php_perl_dereference_sv(sv);
if (SvTYPE(sv) == SVt_PVHV) {
// Old lib doesn't do this convert to string step but I think it should?
convert_to_string(offset);
php_perl_unset_property(object, zval_get_string(offset), NULL);
return;
}
if (SvTYPE(sv) != SVt_PVAV) {
zend_error(E_ERROR, "[perl] Can't do array access on a non-array proxy object");
return;
}
AV* av = (AV*)sv;
convert_to_long(offset);
av_delete(av, Z_LVAL_P(offset), G_DISCARD);
}
static void php_perl_call_method_or_pv(
php_perl_t* obj, char* func, zval* argv, int argc, zval* return_value
) {
DEBUG_PRINT("in php_perl_call_method_or_pv");
PerlInterpreter* my_perl = php_perl_init();
// Make an SV for perl to assign the stacked list to
SV* returnSv;
if (obj->context == PERL_ARRAY) {
AV* av = newAV();
returnSv = (SV*) av;
} else if (obj->context == PERL_HASH) {
HV* hv = newHV();
returnSv = (SV*) hv;
} else {
returnSv = newSV(0);
}
// Call the method or function
dSP; /* initialize stack pointer */
ENTER; /* everything created after here */
SAVETMPS; /* ...is a temporary variable. */
PUSHMARK(SP); /* remember the stack pointer */
if (obj->sv != NULL) {
DEBUG_PRINT("call: object context -> %s", func);
XPUSHs(obj->sv); // @_[0] = self
} else {
DEBUG_PRINT("call: main context -> %s", func);
}
for (int i = 0; i < argc; i++) {
SV* el_sv = newSV(0);
php_perl_zval_to_sv(my_perl, &argv[i], el_sv);
XPUSHs(sv_2mortal(el_sv));
}
PUTBACK; /* make local stack pointer global */
int count;
if (obj->sv == NULL)
count = call_pv(func, (obj->context == PERL_SCALAR ? G_SCALAR : G_ARRAY) | G_EVAL); /* call the function */
else
count = call_method(func, (obj->context == PERL_SCALAR ? G_SCALAR : G_ARRAY) | G_EVAL); /* call the method */
if(SvTRUE(ERRSV)) {
zend_throw_exception_ex(
perl_exception_ce,
0,
"[perl] method call error: %s",
SvPV_nolen(ERRSV)
);
SvREFCNT_dec(returnSv);
FREETMPS;
LEAVE;
return;
}
// Get updated stack pointer
SPAGAIN;
PUSHMARK(SP - count);
PUSHMARK(SP);
XPUSHs(returnSv);
PUTBACK;
// Make a fake assign op so we can call OP_AASSIGN and get back a hash or
// array depending on what our caller wants.
// Save the current PL_op
OP* oldop = PL_op;
BINOP myop;
Zero(&myop, 1, BINOP);
myop.op_flags = (obj->context == PERL_SCALAR ? OPf_WANT_SCALAR : OPf_WANT_LIST);
myop.op_ppaddr = PL_ppaddr[OP_AASSIGN];
myop.op_type = OP_AASSIGN;
myop.op_private = OPpASSIGN_TRUEBOOL;
PL_op = (OP *) &myop;
PL_ppaddr[OP_AASSIGN](aTHX);
// Put PL_op back where it was
PL_op = oldop;
SPAGAIN;
if (return_value != NULL) {
php_perl_sv_to_zval(my_perl, returnSv, return_value);
}
SvREFCNT_dec(returnSv);
PUTBACK;
FREETMPS; /* free that return value */
LEAVE; /* ...and the XPUSHed "mortal" args.*/
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_php_perl_overloaded_call, 0, 0, 0)
ZEND_ARG_VARIADIC_TYPE_INFO(0, args, IS_MIXED, 0)
ZEND_END_ARG_INFO()
static PHP_FUNCTION(php_perl_overloaded_call)
{
php_perl_t *obj = Z_PHPPERL_P(ZEND_THIS);
DEBUG_PRINT("%s->%s() called!", ZSTR_VAL(php_perl_get_class_name(&obj->std)), ZSTR_VAL(EX(func)->common.function_name));
zval* argv;
int argc = 0;
ZEND_PARSE_PARAMETERS_START(0, -1);
Z_PARAM_OPTIONAL
Z_PARAM_VARIADIC('+', argv, argc)
ZEND_PARSE_PARAMETERS_END();
php_perl_call_method_or_pv(obj, ZSTR_VAL(EX(func)->common.function_name), argv, argc, USED_RET() ? return_value : NULL);
/* Cleanup trampoline */
ZEND_ASSERT(EX(func)->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE);
zend_string_release(EX(func)->common.function_name);
zend_free_trampoline(EX(func));
EX(func) = NULL;
}
static zend_function* php_perl_get_method(zend_object **object, zend_string *method_name, const zval *key)
{
DEBUG_PRINT("in php_perl_get_method %s", ZSTR_VAL(method_name));
zend_function *retval = NULL;
if ((retval = zend_std_get_method(object, method_name, key)) != NULL) {
return retval;
}
zend_internal_function *fptr;
if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
fptr = (zend_internal_function *) &EG(trampoline);
} else {
fptr = emalloc(sizeof(zend_internal_function));
}
memset(fptr, 0, sizeof(zend_internal_function));
fptr->type = ZEND_INTERNAL_FUNCTION;
fptr->num_args = 1;
fptr->scope = (*object)->ce;
fptr->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
fptr->function_name = zend_string_copy(method_name);
fptr->handler = ZEND_FN(php_perl_overloaded_call);
fptr->arg_info = (zend_internal_arg_info*) arginfo_php_perl_overloaded_call;
return (zend_function*)fptr;
}
static int php_perl_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2)
{
DEBUG_PRINT("do_operation: opcode %d", opcode);
if (!zval_is_perl_obj(op1)) {
zend_error(E_ERROR, "[perl] invalid operation on non perl object");
return FAILURE;
}
php_perl_t* perlobj = Z_PHPPERL_P(op1);
PerlInterpreter* my_perl = php_perl_init();
SV* sv = perlobj->sv;
if (sv == NULL) {
zend_error(E_ERROR, "[perl] can't do overloaded operation on non proxy object");
return FAILURE;
}
switch (opcode) {
case ZEND_ADD:
case ZEND_SUB:
sv = php_perl_dereference_sv(sv);
I32 currentValue;
long resultValue;
if (SvIOK(sv)) {
currentValue = SvIV(sv);
} else {
zend_error(E_ERROR, "[perl] can't do arithmetic on sv type %d", SvTYPE(sv));
return FAILURE;
}
DEBUG_PRINT("op2 of type %d", Z_TYPE_P(op2));
convert_to_long(op2);
DEBUG_PRINT("op2 has long val %d", Z_LVAL_P(op2));
if (opcode == ZEND_ADD)
resultValue = currentValue + Z_LVAL_P(op2);
else
resultValue = currentValue - Z_LVAL_P(op2);
sv_setiv(sv, resultValue);
ZVAL_LONG(result, resultValue);
DEBUG_PRINT("result is set to %d", Z_LVAL_P(result));
return SUCCESS;
default:
zend_error(E_ERROR, "[perl] can't handle opcode %d", opcode);
return FAILURE;
}
}
static zend_object* php_perl_clone(zend_object *oldObject)
{
PerlInterpreter* my_perl = php_perl_init();
php_perl_t* oldClass = php_perl_fetch_object(oldObject);
zend_object* newObject = php_perl_new();
php_perl_t* newClass = php_perl_fetch_object(newObject);
if (oldClass->sv != NULL) {
SV* old_sv = SvRV(oldClass->sv);
SV* new_sv = NULL;
if (SvTYPE(old_sv) == SVt_PVAV) {
/* array */
I32 len = av_len((AV*)old_sv);
I32 i;
new_sv = (SV*)newAV();
for (i = 0; i <= len; i++) {
SV** el_sv = av_fetch((AV*)old_sv, i, 0);
if (el_sv != NULL && *el_sv != NULL) {
av_push((AV*)new_sv, newSVsv(*el_sv));
}
}
} else if (SvTYPE(old_sv) == SVt_PVHV) {
/* hash */
new_sv = (SV*)newHVhv((HV*)old_sv);
} else if (SvOK(old_sv)) {
/* scalar */
new_sv = newSVsv(old_sv);
} else {
/* unknown */
zend_error(E_ERROR, "[perl] Can't clone perl object (type [%ld])", SvTYPE(old_sv));
}
newClass->sv = sv_bless(newRV_noinc(new_sv), SvSTASH(old_sv));
SvREFCNT_inc(newClass->sv);
// Aaaah.... fuck
zval* newObjZ = malloc(sizeof(zval));
// zend_clone called us and is just about to stick zend_object inside
// a zval it already has, which is the one we probably want to return
// later, but.. i don't know, now we just keep this shite hanging around
ZVAL_OBJ(newObjZ, newObject);
Z_ADDREF_P(newObjZ);
zend_hash_str_add_ptr(&PERL_G(perl_objects), (char*)SvRV(newClass->sv), sizeof(SV*), newObjZ);
}
return newObject;
}
/* Destructor for overloaded Perl's objects */
static void php_perl_destructor(zend_object *object)
{
php_perl_t *perlobj;
perlobj = php_perl_fetch_object(object);
DEBUG_PRINT("Freeing %p", perlobj);
if (perlobj) {
if (perlobj->properties) {
/* removing properties */
zend_hash_destroy(perlobj->properties);
FREE_HASHTABLE(perlobj->properties);
}
PerlInterpreter* my_perl = PERL_G(my_perl);
// Check SvOK - in certain circumstances we might have caused perl to
// die and clean everything up already
if (my_perl != NULL && perlobj->sv != NULL && SvOK(perlobj->sv)) {
DEBUG_PRINT("Forgetting and freeing %p", perlobj->sv);
zend_hash_str_del(&PERL_G(perl_objects), (char*)SvRV(perlobj->sv), sizeof(SV*));
/* removing perl object */
if (SvREFCNT(perlobj->sv) >= 1)
SvREFCNT_dec(perlobj->sv);
}
efree(perlobj);
}
}
static void php_perl_iterator_dtor(zend_object_iterator *iterator)
{
zval_ptr_dtor(&iterator->data);
}
static int php_perl_iterator_valid(zend_object_iterator *iterator)
{
php_perl_t* perl_obj = Z_PHPPERL(iterator->data);
if (perl_obj->properties) {
return zend_hash_has_more_elements(perl_obj->properties);
}
return FAILURE;
}
static zval* php_perl_iterator_current_data(zend_object_iterator *iterator)
{
php_perl_t* perl_obj = Z_PHPPERL(iterator->data);
if (perl_obj->properties) {
return zend_hash_get_current_data(perl_obj->properties);
}
// todo: do we need to do this? try to think of a test
zval* ret = malloc(sizeof(zval));
ZVAL_NULL(ret);
return ret;
}
static void php_perl_iterator_current_key(zend_object_iterator *iterator, zval *key)
{
php_perl_t* perl_obj = Z_PHPPERL(iterator->data);