php_vm 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ext/php_vm/php_vm.c +156 -3
- data/ext/php_vm/php_vm.h +9 -1
- metadata +1 -1
data/ext/php_vm/php_vm.c
CHANGED
@@ -7,6 +7,7 @@
|
|
7
7
|
// global
|
8
8
|
|
9
9
|
VALUE rb_mPHPVM;
|
10
|
+
VALUE rb_mPHPGlobal;
|
10
11
|
VALUE rb_cPHPClass;
|
11
12
|
VALUE rb_cPHPObject;
|
12
13
|
VALUE rb_ePHPError;
|
@@ -456,11 +457,155 @@ VALUE rb_php_vm_exec(VALUE cls, VALUE code)
|
|
456
457
|
return Qnil;
|
457
458
|
}
|
458
459
|
|
459
|
-
VALUE
|
460
|
+
VALUE rb_php_vm_get_class(VALUE cls, VALUE v_class_name)
|
460
461
|
{
|
461
462
|
return rb_php_class_get(rb_cPHPClass, v_class_name);
|
462
463
|
}
|
463
464
|
|
465
|
+
VALUE rb_php_vm_define_global_constants(VALUE cls)
|
466
|
+
{
|
467
|
+
TSRMLS_FETCH();
|
468
|
+
|
469
|
+
// method
|
470
|
+
zend_function *mptr;
|
471
|
+
find_zend_function(NULL, "get_defined_constants", strlen("get_defined_constants"), &mptr);
|
472
|
+
if (!mptr) {
|
473
|
+
return Qfalse;
|
474
|
+
}
|
475
|
+
|
476
|
+
// call method
|
477
|
+
zval *z_val;
|
478
|
+
int result = call_php_method(NULL, NULL, mptr, 0, NULL, &z_val TSRMLS_CC);
|
479
|
+
if (result==FAILURE) {
|
480
|
+
return Qfalse;
|
481
|
+
}
|
482
|
+
|
483
|
+
HashTable* ht = Z_ARRVAL_P(z_val);
|
484
|
+
HashPosition pos;
|
485
|
+
zval** data;
|
486
|
+
|
487
|
+
zend_hash_internal_pointer_reset_ex(ht, &pos);
|
488
|
+
while (SUCCESS == zend_hash_get_current_data_ex(ht, (void **)&data, &pos)) {
|
489
|
+
char *string_key;
|
490
|
+
ulong num_index;
|
491
|
+
|
492
|
+
switch(zend_hash_get_current_key_ex(ht, &string_key, NULL, &num_index, 0, &pos)) {
|
493
|
+
case HASH_KEY_IS_STRING:{
|
494
|
+
if (0x61<=string_key[0] && string_key[0]<=0x7a) {
|
495
|
+
// lower case
|
496
|
+
char *string_key2 = malloc(strlen(string_key));
|
497
|
+
string_key2[0] = string_key2[0]-32;
|
498
|
+
if (!rb_const_defined(rb_mPHPGlobal, rb_intern(string_key2))) {
|
499
|
+
rb_define_const(rb_mPHPGlobal, string_key2, zval_to_value(*data));
|
500
|
+
}
|
501
|
+
free(string_key2);
|
502
|
+
} else {
|
503
|
+
// upper case
|
504
|
+
if (!rb_const_defined(rb_mPHPGlobal, rb_intern(string_key))) {
|
505
|
+
rb_define_const(rb_mPHPGlobal, string_key, zval_to_value(*data));
|
506
|
+
}
|
507
|
+
}
|
508
|
+
break;
|
509
|
+
}
|
510
|
+
}
|
511
|
+
|
512
|
+
zend_hash_move_forward_ex(ht, &pos);
|
513
|
+
}
|
514
|
+
return Qtrue;
|
515
|
+
}
|
516
|
+
|
517
|
+
VALUE rb_php_vm_define_global_functions(VALUE cls)
|
518
|
+
{
|
519
|
+
TSRMLS_FETCH();
|
520
|
+
|
521
|
+
// method
|
522
|
+
zend_function *mptr;
|
523
|
+
find_zend_function(NULL, "get_defined_functions", strlen("get_defined_functions"), &mptr);
|
524
|
+
if (!mptr) {
|
525
|
+
return Qfalse;
|
526
|
+
}
|
527
|
+
|
528
|
+
// call method
|
529
|
+
zval *z_val;
|
530
|
+
int result = call_php_method(NULL, NULL, mptr, 0, NULL, &z_val TSRMLS_CC);
|
531
|
+
if (result==FAILURE) {
|
532
|
+
return Qfalse;
|
533
|
+
}
|
534
|
+
|
535
|
+
HashTable* ht = Z_ARRVAL_P(z_val);
|
536
|
+
HashPosition pos;
|
537
|
+
zval** data;
|
538
|
+
|
539
|
+
zend_hash_internal_pointer_reset_ex(ht, &pos);
|
540
|
+
while (SUCCESS == zend_hash_get_current_data_ex(ht, (void **)&data, &pos)) {
|
541
|
+
HashTable* ht2 = Z_ARRVAL_P(*data);
|
542
|
+
HashPosition pos2;
|
543
|
+
zval** data2;
|
544
|
+
|
545
|
+
zend_hash_internal_pointer_reset_ex(ht2, &pos2);
|
546
|
+
while (SUCCESS == zend_hash_get_current_data_ex(ht2, (void **)&data2, &pos2)) {
|
547
|
+
rb_define_module_function(rb_mPHPGlobal, Z_STRVAL_P(*data2), rb_php_global_function_call, -1);
|
548
|
+
zend_hash_move_forward_ex(ht2, &pos2);
|
549
|
+
}
|
550
|
+
|
551
|
+
zend_hash_move_forward_ex(ht, &pos);
|
552
|
+
}
|
553
|
+
return Qtrue;
|
554
|
+
}
|
555
|
+
|
556
|
+
VALUE rb_php_vm_define_global_classes(VALUE cls)
|
557
|
+
{
|
558
|
+
TSRMLS_FETCH();
|
559
|
+
|
560
|
+
// method
|
561
|
+
zend_function *mptr;
|
562
|
+
find_zend_function(NULL, "get_declared_classes", strlen("get_declared_classes"), &mptr);
|
563
|
+
if (!mptr) {
|
564
|
+
return Qfalse;
|
565
|
+
}
|
566
|
+
|
567
|
+
// call method
|
568
|
+
zval *z_val;
|
569
|
+
int result = call_php_method(NULL, NULL, mptr, 0, NULL, &z_val TSRMLS_CC);
|
570
|
+
if (result==FAILURE) {
|
571
|
+
return Qfalse;
|
572
|
+
}
|
573
|
+
|
574
|
+
HashTable* ht = Z_ARRVAL_P(z_val);
|
575
|
+
HashPosition pos;
|
576
|
+
zval** data;
|
577
|
+
|
578
|
+
zend_hash_internal_pointer_reset_ex(ht, &pos);
|
579
|
+
while (SUCCESS == zend_hash_get_current_data_ex(ht, (void **)&data, &pos)) {
|
580
|
+
rb_define_module_function(rb_mPHPGlobal, Z_STRVAL_P(*data), rb_php_global_class_call, 0);
|
581
|
+
zend_hash_move_forward_ex(ht, &pos);
|
582
|
+
}
|
583
|
+
return Qtrue;
|
584
|
+
}
|
585
|
+
|
586
|
+
VALUE rb_php_vm_define_global(VALUE cls)
|
587
|
+
{
|
588
|
+
VALUE res1 = rb_php_vm_define_global_constants(cls);
|
589
|
+
VALUE res2 = rb_php_vm_define_global_functions(cls);
|
590
|
+
VALUE res3 = rb_php_vm_define_global_classes(cls);
|
591
|
+
return res1==Qtrue && res2==Qtrue && res3==Qtrue;
|
592
|
+
}
|
593
|
+
|
594
|
+
|
595
|
+
// module PHPVM::PHPGlobal
|
596
|
+
|
597
|
+
VALUE rb_php_global_function_call(int argc, VALUE *argv, VALUE self)
|
598
|
+
{
|
599
|
+
VALUE callee = get_callee_name();
|
600
|
+
return call_php_method_bridge(NULL, NULL, callee, argc, argv);
|
601
|
+
}
|
602
|
+
|
603
|
+
VALUE rb_php_global_class_call(VALUE self)
|
604
|
+
{
|
605
|
+
VALUE callee = get_callee_name();
|
606
|
+
return rb_php_class_get(rb_cPHPClass, callee);
|
607
|
+
}
|
608
|
+
|
464
609
|
|
465
610
|
// class PHPVM::PHPClass
|
466
611
|
|
@@ -654,9 +799,17 @@ void Init_php_vm()
|
|
654
799
|
rb_define_singleton_method(rb_mPHPVM, "require", rb_php_vm_require, 1);
|
655
800
|
rb_define_singleton_method(rb_mPHPVM, "require_once", rb_php_vm_require_once, 1);
|
656
801
|
rb_define_singleton_method(rb_mPHPVM, "exec", rb_php_vm_exec, 1);
|
657
|
-
rb_define_singleton_method(rb_mPHPVM, "
|
802
|
+
rb_define_singleton_method(rb_mPHPVM, "get_class", rb_php_vm_get_class, 1);
|
803
|
+
rb_define_singleton_method(rb_mPHPVM, "define_global_constants", rb_php_vm_define_global_constants, 0);
|
804
|
+
rb_define_singleton_method(rb_mPHPVM, "define_global_functions", rb_php_vm_define_global_functions, 0);
|
805
|
+
rb_define_singleton_method(rb_mPHPVM, "define_global_classes", rb_php_vm_define_global_classes, 0);
|
806
|
+
rb_define_singleton_method(rb_mPHPVM, "define_global", rb_php_vm_define_global, 0);
|
807
|
+
|
808
|
+
rb_define_const(rb_mPHPVM, "VERSION", rb_str_new2("1.2.0"));
|
658
809
|
|
659
|
-
|
810
|
+
// module PHPVM::PHPGlobal
|
811
|
+
rb_mPHPGlobal = rb_define_module_under(rb_mPHPVM, "PHPGlobal");
|
812
|
+
rb_php_vm_define_global(rb_mPHPVM);
|
660
813
|
|
661
814
|
// class PHPVM::PHPClass
|
662
815
|
rb_cPHPClass = rb_define_class_under(rb_mPHPVM, "PHPClass", rb_cObject);
|
data/ext/php_vm/php_vm.h
CHANGED
@@ -43,7 +43,15 @@ extern zval* get_zval(VALUE self);
|
|
43
43
|
// module PHPVM
|
44
44
|
extern VALUE rb_php_vm_require(VALUE cls, VALUE rbv_filepath);
|
45
45
|
extern VALUE rb_php_vm_exec(VALUE cls, VALUE rbv_code);
|
46
|
-
extern VALUE
|
46
|
+
extern VALUE rb_php_vm_get_class(VALUE cls, VALUE rbv_class_name);
|
47
|
+
extern VALUE rb_php_vm_define_global_constants(VALUE cls);
|
48
|
+
extern VALUE rb_php_vm_define_global_functions(VALUE cls);
|
49
|
+
extern VALUE rb_php_vm_define_global_classes(VALUE cls);
|
50
|
+
extern VALUE rb_php_vm_define_global(VALUE cls);
|
51
|
+
|
52
|
+
// module PHPVM::PHPGlobal
|
53
|
+
extern VALUE rb_php_global_function_call(int argc, VALUE *argv, VALUE self);
|
54
|
+
extern VALUE rb_php_global_class_call(VALUE self);
|
47
55
|
|
48
56
|
// class PHPVM::PHPClass
|
49
57
|
extern VALUE rb_php_class_get(VALUE cls, VALUE rbv_name);
|