-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvrzno_object.c
794 lines (629 loc) · 17.5 KB
/
vrzno_object.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
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
zend_class_entry *vrzno_class_entry;
zend_object_handlers vrzno_object_handlers;
ZEND_BEGIN_ARG_INFO_EX(arginfo___get, 0, 0, 1)
ZEND_ARG_INFO(0, property_name)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo___call, 0, 0, 2)
ZEND_ARG_INFO(0, method_name)
ZEND_ARG_INFO(0, args)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo___invoke, 0, 0, -1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo___construct, 0, 0, -1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo___destruct, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo___toString, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
static const zend_function_entry vrzno_vrzno_methods[] = {
PHP_ME(Vrzno, __get, arginfo___get, ZEND_ACC_PUBLIC)
PHP_ME(Vrzno, __call, arginfo___call, ZEND_ACC_PUBLIC)
PHP_ME(Vrzno, __invoke, arginfo___invoke, ZEND_ACC_PUBLIC)
PHP_ME(Vrzno, __construct, arginfo___construct, ZEND_ACC_PUBLIC)
PHP_ME(Vrzno, __destruct, arginfo___destruct, ZEND_ACC_PUBLIC)
PHP_ME(Vrzno, __toString, arginfo___toString, ZEND_ACC_PUBLIC)
PHP_FE_END
};
vrzno_object *vrzno_fetch_object(zend_object *obj)
{
return (vrzno_object*)((char*)(obj) - XtOffsetOf(vrzno_object, zo));
}
static struct _zend_object *vrzno_create_object(zend_class_entry *class_type)
{
vrzno_object *vrzno = zend_object_alloc(sizeof(vrzno_object), class_type);
zend_object_std_init(&vrzno->zo, class_type);
vrzno->zo.handlers = &vrzno_object_handlers;
vrzno->targetId = (jstarget*) EM_ASM_INT({
const _class = Module._classes.get($0);
if(_class)
{
return Module.targets.getId(_class);
}
return Module.targets.add(globalThis);
}, class_type);
vrzno->isConstructor = 0;
vrzno->isFunction = 0;
return &vrzno->zo;
}
static struct _zend_class_entry *vrzno_create_class(jstarget *targetId)
{
zend_class_entry ce;
zend_class_entry *vrzno_subclass_entry;
char name[256];
snprintf(name, 256, "Vrzno:::{%p}", targetId);
INIT_CLASS_ENTRY(ce, name, vrzno_vrzno_methods);
vrzno_subclass_entry = zend_register_internal_class_ex(&ce, vrzno_class_entry);
return vrzno_subclass_entry;
}
static vrzno_object *vrzno_create_object_for_target(jstarget *targetId, jstarget *isFunction, bool isConstructor)
{
zend_class_entry *ce = vrzno_class_entry;
if(isConstructor)
{
zend_class_entry *existing = EM_ASM_PTR({
const target = Module.targets.get($0);
return Module.classes.get(target);
}, targetId);
if(!existing)
{
ce = vrzno_create_class(targetId);
EM_ASM({
const target = Module.targets.get($0);
Module.classes.set(target, $1);
Module._classes.set($1, target);
}, targetId, ce);
}
else
{
ce = existing;
}
}
vrzno_object *vrzno = zend_object_alloc(sizeof(vrzno_object), ce);
zend_object_std_init(&vrzno->zo, ce);
vrzno->zo.handlers = &vrzno_object_handlers;
vrzno->targetId = targetId;
vrzno->isConstructor = isConstructor;
vrzno->isFunction = isFunction;
return vrzno;
}
static void vrzno_object_free(zend_object *zobj)
{
EM_ASM({
const target = Module.targets.get($0);
if(target)
{
Module.tacked.delete(target);
}
}, vrzno_fetch_object(zobj)->targetId);
zend_object_std_dtor(zobj);
}
zval *vrzno_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv)
{
vrzno_object *vrzno = vrzno_fetch_object(object);
jstarget *targetId = vrzno->targetId;
char *name = ZSTR_VAL(member);
EM_ASM({
const target = Module.targets.get($0);
const property = UTF8ToString($1);
const rv = $2;
if(!(property in target))
{
return Module.jsToZval(undefined, rv);
}
Module.jsToZval(target[property], rv);
}, targetId, name, rv);
return rv;
}
zval *vrzno_write_property(zend_object *object, zend_string *member, zval *newValue, void **cache_slot)
{
char *name = ZSTR_VAL(member);
vrzno_object *vrzno = vrzno_fetch_object(object);
jstarget *targetId = vrzno->targetId;
bool isCallable = 0;
char *errstr = NULL;
zend_fcall_info_cache fcc;
if(Z_TYPE_P(newValue) == IS_OBJECT && Z_OBJCE_P(newValue) == vrzno_class_entry)
{
isCallable = vrzno_fetch_object(Z_OBJ_P(newValue))->isFunction;
}
else if(Z_TYPE_P(newValue) == IS_OBJECT && Z_OBJCE_P(newValue)->parent == vrzno_class_entry)
{
isCallable = vrzno_fetch_object(Z_OBJ_P(newValue))->isFunction;
}
else if(Z_TYPE_P(newValue) != IS_STRING)
{
isCallable = zend_is_callable_ex(newValue, NULL, 0, NULL, &fcc, &errstr);
}
if(isCallable)
{
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
const funcPtr = $2;
const zvalPtr = $3;
const paramCount = $4;
target[property] = Module.callableToJs(funcPtr);
const gc = Module.ccall(
'vrzno_expose_closure'
, 'number'
, ['number']
, [funcPtr]
);
Module.fRegistry.register(target[property], gc, target[property]);
})() }, targetId, name, fcc.function_handler, newValue, fcc.function_handler->common.num_args);
return newValue;
}
switch(Z_TYPE_P(newValue))
{
case IS_OBJECT:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
const zvalPtr = $2;
const zo = Module.ccall(
'vrzno_expose_object'
, 'number'
, ['number']
, [zvalPtr]
);
target[property] = Module.marshalZObject(zo);
})() }, targetId, name, newValue);
break;
case IS_ARRAY:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
const zvalPtr = $2;
const za = Module.ccall(
'vrzno_expose_array'
, 'number'
, ['number']
, [zvalPtr]
);
target[property] = Module.marshalZArray(za);
})() }, targetId, name, newValue);
break;
case IS_UNDEF:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
delete target[property];
})() }, targetId, name);
break;
case IS_NULL:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
target[property] = null;
})() }, targetId, name);
break;
case IS_FALSE:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
target[property] = false;
})() }, targetId, name);
break;
case IS_TRUE:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
target[property] = true;
})() }, targetId, name);
break;
case IS_DOUBLE:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
target[property] = $2;
})() }, targetId, name, Z_DVAL_P(newValue));
break;
case IS_LONG:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
target[property] = $2;
})() }, targetId, name, Z_LVAL_P(newValue));
break;
case IS_STRING:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
const newValue = UTF8ToString($2);
target[property] = newValue;
})() }, targetId, name, Z_STRVAL_P(newValue));
break;
}
return newValue;
}
zval *vrzno_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
{
EM_ASM({
let target = Module.targets.get($0);
const property = $1;
const rv = $2;
if(target instanceof ArrayBuffer)
{
if(!Module.bufferMaps.has(target))
{
Module.bufferMaps.set(target, new Uint8Array(target));
}
target = Module.bufferMaps.get(target);
}
if(!(property in target))
{
return Module.jsToZval(undefined, rv);
}
Module.jsToZval(target[property], rv);
}, vrzno_fetch_object(object)->targetId, Z_LVAL_P(offset), rv);
return rv;
}
void vrzno_write_dimension(zend_object *object, zval *offset, zval *newValue)
{
vrzno_object *vrzno = vrzno_fetch_object(object);
jstarget *targetId = vrzno->targetId;
long index = Z_LVAL_P(offset);
bool isCallable = 0;
char *errstr = NULL;
zend_fcall_info_cache fcc;
if(Z_OBJCE_P(newValue) == vrzno_class_entry)
{
isCallable = vrzno_fetch_object(Z_OBJ_P(newValue))->isFunction;
}
else if(Z_OBJCE_P(newValue)->parent == vrzno_class_entry)
{
isCallable = vrzno_fetch_object(Z_OBJ_P(newValue))->isFunction;
}
else if(Z_TYPE_P(newValue) != IS_STRING)
{
isCallable = zend_is_callable_ex(newValue, NULL, 0, NULL, &fcc, &errstr);
}
if(isCallable)
{
EM_ASM({ (() => {
const target = Module.targets.get($0);
const property = $1;
const funcPtr = $2;
const zvalPtr = $3;
target[property] = Module.callableToJs(funcPtr);
const gc = Module.ccall(
'vrzno_expose_closure'
, 'number'
, ['number']
, [funcPtr]
);
Module.fRegistry.register(target[property], gc, target[property]);
})() }, targetId, index, fcc.function_handler, newValue);
return;
}
switch(Z_TYPE_P(newValue))
{
case IS_OBJECT:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
const zvalPtr = $2;
const zo = Module.ccall(
'vrzno_expose_object'
, 'number'
, ['number']
, [zvalPtr]
);
target[property] = Module.marshalZObject(zo);
})() }, targetId, index, newValue);
break;
case IS_ARRAY:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
const zvalPtr = $2;
const za = Module.ccall(
'vrzno_expose_array'
, 'number'
, ['number']
, [zvalPtr]
);
target[property] = Module.marshalZArray(za);
})() }, targetId, index, newValue);
break;
case IS_UNDEF:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
delete target[property];
})() }, targetId, index);
break;
case IS_NULL:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
target[property] = null;
})() }, targetId, index);
break;
case IS_FALSE:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
target[property] = false;
})() }, targetId, index);
break;
case IS_TRUE:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
target[property] = true;
})() }, targetId, index);
break;
case IS_DOUBLE:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
target[property] = $2;
})() }, targetId, index, Z_DVAL_P(newValue));
break;
case IS_LONG:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
target[property] = $2;
})() }, targetId, index, Z_LVAL_P(newValue));
break;
case IS_STRING:
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
const newValue = UTF8ToString($2);
target[property] = newValue;
})() }, targetId, index, Z_STRVAL_P(newValue));
break;
}
}
int vrzno_has_dimension(zend_object *object, zval *offset, int check_empty)
{
return EM_ASM_INT({
const target = Module.targets.get($0);
const property = $1;
const check_empty = $2;
if(Array.isArray(target))
{
return typeof target[property] !== 'undefined';
}
if(target instanceof ArrayBuffer)
{
if(!Module.bufferMaps.has(target))
{
Module.bufferMaps.set(target, new Uint8Array(target));
}
const targetBytes = Module.bufferMaps.get(target);
return targetBytes[property] !== 'undefined';
}
if(!check_empty)
{
return property in target;
}
else
{
return !!target[property];
}
}, vrzno_fetch_object(object)->targetId, Z_LVAL_P(offset), check_empty);
}
void vrzno_unset_property(zend_object *object, zend_string *member, void **cache_slot)
{
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = UTF8ToString($1);
delete target[property];
})() }, vrzno_fetch_object(object)->targetId, ZSTR_VAL(member));
}
void vrzno_unset_dimension(zend_object *object, zval *offset)
{
EM_ASM({ (() =>{
const target = Module.targets.get($0);
const property = $1;
delete target[property];
})() }, vrzno_fetch_object(object)->targetId, Z_LVAL_P(offset));
}
HashTable *vrzno_get_properties_for(zend_object *object, zend_prop_purpose purpose)
{
vrzno_object *vrzno = vrzno_fetch_object(object);
jstarget *targetId = vrzno->targetId;
char *js_ret = EM_ASM_INT({
const target = Module.targets.get($0);
let json;
if(typeof target === 'function')
{
json = JSON.stringify({});
}
else
{
try{ json = JSON.stringify({...target}); }
catch { json = JSON.stringify({}); }
}
const str = String(json);
const len = 1 + lengthBytesUTF8(str);
const loc = _malloc(len);
stringToUTF8(str, loc, len);
return loc;
}, targetId);
php_json_parser parser;
zval js_object;
php_json_parser_init(&parser, &js_object, js_ret, strlen(js_ret), PHP_JSON_OBJECT_AS_ARRAY, PHP_JSON_PARSER_DEFAULT_DEPTH);
int parseFailed = php_json_yyparse(&parser);
if(parseFailed)
{
// FAIL
}
free(js_ret);
return Z_ARR(js_object);
}
int vrzno_has_property(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot)
{
return EM_ASM_INT({
const target = Module.targets.get($0);
const property = UTF8ToString($1);
return property in target;
}, vrzno_fetch_object(object)->targetId, ZSTR_VAL(member));
}
zend_string *vrzno_get_class_name(const zend_object *object)
{
char *className = EM_ASM_INT({
const target = Module.targets.get($0);
const str = (target.constructor && target.constructor.name) || 'Object';
const loc = 1 + lengthBytesUTF8(name);
const len = _malloc(name);
stringToUTF8(str, loc, len);
return namePtr;
}, vrzno_fetch_object((zend_object*) object)->targetId);
zend_string *retVal = zend_string_init(className, strlen(className), 0);
free(*className);
return retVal;
}
PHP_METHOD(Vrzno, __get)
{
zval *object = getThis();
zend_object *zObject = Z_OBJ_P(object);
vrzno_object *vrzno = vrzno_fetch_object(zObject);
char *js_property_name = "";
size_t js_property_name_len = 0;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(js_property_name, js_property_name_len)
ZEND_PARSE_PARAMETERS_END();
EM_ASM({
const target = Module.targets.get($0);
const property_name = UTF8ToString($1);
const rv = $2;
return Module.jsToZval(target[property_name], rv);
}, vrzno->targetId, js_property_name, return_value);
}
PHP_METHOD(Vrzno, __call)
{
zval *object = getThis();
zend_object *zObject = Z_OBJ_P(object);
vrzno_object *vrzno = vrzno_fetch_object(zObject);
HashTable *argv;
char *method_name = "";
size_t method_name_len = 0;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STRING(method_name, method_name_len)
Z_PARAM_ARRAY_HT(argv)
ZEND_PARSE_PARAMETERS_END();
size_t size = sizeof(zval*);
int argc = zend_hash_num_elements(argv);
int i = 0;
zval *args[argc];
if(argc)
{
zval *arg;
#if PHP_MAJOR_VERSION >= 8 && PHP_MINOR_VERSION >= 2
ZEND_HASH_PACKED_FOREACH_VAL(argv, arg) {
#else
ZEND_HASH_FOREACH_VAL(argv, arg) {
#endif
args[i] = arg;
i++;
} ZEND_HASH_FOREACH_END();
}
EM_ASM({
const target = Module.targets.get($0);
const method_name = UTF8ToString($1);
const argp = $2;
const argc = $3;
const size = $4;
const rv = $5;
const args = [];
for(let i = 0; i < argc; i++)
{
const loc = argp + i * size;
const ptr = Module.getValue(loc, '*');
const arg = Module.zvalToJS(ptr);
args.push(arg);
}
Module.jsToZval(target[method_name](...args), rv);
}, vrzno->targetId, method_name, args, argc, size, return_value);
}
PHP_METHOD(Vrzno, __invoke)
{
zval *object = getThis();
zend_object *zObject = Z_OBJ_P(object);
vrzno_object *vrzno = vrzno_fetch_object(zObject);;
int argc = 0;
zval *argv;
ZEND_PARSE_PARAMETERS_START(0, -1)
Z_PARAM_VARIADIC('*', argv, argc)
ZEND_PARSE_PARAMETERS_END();
EM_ASM({
const target = Module.targets.get($0);
const argv = $1;
const argc = $2;
const size = $3;
const rv = $4;
const args = [];
for(let i = 0; i < argc; i++)
{
args.push(Module.zvalToJS(argv + i * size));
}
return Module.jsToZval(target(...args), rv);
}, vrzno->targetId, argv, argc, sizeof(zval), return_value);
}
PHP_METHOD(Vrzno, __construct)
{
zval *object = getThis();
zend_object *zObject = Z_OBJ_P(object);
vrzno_object *vrzno = vrzno_fetch_object(zObject);
if(Z_OBJCE_P(object) == vrzno_class_entry)
{
return;
}
zval *argv;
int argc = 0;
ZEND_PARSE_PARAMETERS_START(0, -1)
Z_PARAM_VARIADIC('*', argv, argc)
ZEND_PARSE_PARAMETERS_END();
vrzno->targetId = EM_ASM_INT({
const _class = Module._classes.get($0);
const argv = $1;
const argc = $2;
const size = $3;
const args = [];
for(let i = 0; i < argc; i++)
{
args.push(Module.zvalToJS(argv + i * size));
}
const _object = new _class(...args);
const index = Module.targets.add(_object);
Module.tacked.add(_object);
return index;
}, Z_OBJCE_P(object), argv, argc, sizeof(zval));
}
PHP_METHOD(Vrzno, __destruct)
{
zval *object = getThis();
zend_object *zObject = Z_OBJ_P(object);
vrzno_object *vrzno = vrzno_fetch_object(zObject);
EM_ASM({
const target = Module.targets.get($0);
Module.tacked.delete(target);
Module.targets.remove(target);
}, vrzno->targetId);
}
PHP_METHOD(Vrzno, __toString)
{
zval *object = getThis();
zend_object *zObject = Z_OBJ_P(object);
vrzno_object *vrzno = vrzno_fetch_object(zObject);
if(zend_parse_parameters_none() == FAILURE)
{
RETURN_THROWS();
}
char *str = EM_ASM_PTR({
const target = Module.targets.get($0);
const str = String(target);
const len = 1 + lengthBytesUTF8(str);
const loc = _malloc(len);
stringToUTF8(str, loc, len);
return loc;
}, vrzno->targetId);
zend_string *zstr = zend_string_init(str, strlen(str), 0);
RETVAL_STR_COPY(zstr);
free(str);
}