From 645b0df999edd2b0a0fc80e38cda2c8ec494ac2a Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 18 Oct 2013 23:57:26 +0200 Subject: [PATCH 1/7] Add test from issue #54 --- tests/derived_class_properties.phpt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/derived_class_properties.phpt diff --git a/tests/derived_class_properties.phpt b/tests/derived_class_properties.phpt new file mode 100644 index 0000000..c1a24ce --- /dev/null +++ b/tests/derived_class_properties.phpt @@ -0,0 +1,23 @@ +--TEST-- +Test V8::executeString() : Properties on derived class +--SKIPIF-- + +--FILE-- +testing = 23; + } +} + +$v8 = new V8Wrapper(); +$v8->executeString('print("foo\n");'); +?> +===EOF=== +--EXPECT-- +foo +===EOF=== From 1f8b6055858b1577b3aa0782bb4f51bf2a399802 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 19 Oct 2013 02:15:37 +0200 Subject: [PATCH 2/7] Add test on initially initialized properties on V8Js object. --- tests/derived_class_properties_init.phpt | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/derived_class_properties_init.phpt diff --git a/tests/derived_class_properties_init.phpt b/tests/derived_class_properties_init.phpt new file mode 100644 index 0000000..413cc3c --- /dev/null +++ b/tests/derived_class_properties_init.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test V8::executeString() : Initialized properties on derived class +--SKIPIF-- + +--FILE-- +testing."\n"); +$v8->executeString('print(PHP.testing + "\n");'); +$v8->executeString('print(PHP.protectedVar + "\n");'); +$v8->executeString('print(PHP.privateVar + "\n");'); +?> +===EOF=== +--EXPECT-- +23 +23 +undefined +undefined +===EOF=== From b888cfce0d540e3f0b3aac63aa5641cb42bc4aca Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 19 Oct 2013 02:17:10 +0200 Subject: [PATCH 3/7] Add test on protected & private properties on derived classes --- tests/derived_class_properties_protected.phpt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/derived_class_properties_protected.phpt diff --git a/tests/derived_class_properties_protected.phpt b/tests/derived_class_properties_protected.phpt new file mode 100644 index 0000000..054fbf0 --- /dev/null +++ b/tests/derived_class_properties_protected.phpt @@ -0,0 +1,27 @@ +--TEST-- +Test V8::executeString() : Protected and private properties on derived class +--SKIPIF-- + +--FILE-- +testing = 23; + $this->privTesting = 42; + } +} + +$v8 = new V8Wrapper(); +$v8->executeString('print(PHP.testing + "\n");'); +$v8->executeString('print(PHP.privTesting + "\n");'); +?> +===EOF=== +--EXPECT-- +undefined +undefined +===EOF=== From 257233be51501f500fa6702737162115811d8ea1 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 19 Oct 2013 02:22:13 +0200 Subject: [PATCH 4/7] Make php_v8js_write_property export only public properties Before a property would have been exported, even if a derived class declared it either private or protected. --- v8js.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/v8js.cc b/v8js.cc index daccd7f..a86b195 100644 --- a/v8js.cc +++ b/v8js.cc @@ -1296,12 +1296,16 @@ static void php_v8js_write_property(zval *object, zval *member, zval *value ZEND { V8JS_BEGIN_CTX(c, object) - /* Global PHP JS object */ - v8::Local object_name_js = v8::Local::New(c->isolate, c->object_name); - v8::Local jsobj = V8JS_GLOBAL->Get(object_name_js)->ToObject(); + /* Check whether member is public, if so, export to V8. */ + zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1 TSRMLS_CC); + if(property_info->flags & ZEND_ACC_PUBLIC) { + /* Global PHP JS object */ + v8::Local object_name_js = v8::Local::New(c->isolate, c->object_name); + v8::Local jsobj = V8JS_GLOBAL->Get(object_name_js)->ToObject(); - /* Write value to PHP JS object */ - jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, c->isolate TSRMLS_CC), v8::ReadOnly); + /* Write value to PHP JS object */ + jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, c->isolate TSRMLS_CC), v8::ReadOnly); + } /* Write value to PHP object */ std_object_handlers.write_property(object, member, value ZEND_HASH_KEY_CC TSRMLS_CC); From c77cdf4b6f70c05308b41863f1ebfb9ab4e8812e Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 19 Oct 2013 02:24:43 +0200 Subject: [PATCH 5/7] Export initially initialized public properties to V8 Before a property was exported to V8 if it was assigned a value during normal code execution. However if the value was assigned (hard coded) on class level it wasn't exported. --- v8js.cc | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/v8js.cc b/v8js.cc index a86b195..4f59490 100644 --- a/v8js.cc +++ b/v8js.cc @@ -762,7 +762,35 @@ static PHP_METHOD(V8Js, __construct) c->object_name.Reset(c->isolate, object_name_js); /* Add the PHP object into global object */ - V8JS_GLOBAL->Set(object_name_js, php_obj_t->InstanceTemplate()->NewInstance(), v8::ReadOnly); + v8::Local php_obj = php_obj_t->InstanceTemplate()->NewInstance(); + V8JS_GLOBAL->Set(object_name_js, php_obj, v8::ReadOnly); + + /* Export public property values */ + HashTable *properties = zend_std_get_properties(getThis()); + HashPosition pos; + zval **value; + ulong index; + char *member; + uint member_len; + + for(zend_hash_internal_pointer_reset_ex(properties, &pos); + zend_hash_get_current_data_ex(properties, (void **) &value, &pos) == SUCCESS; + zend_hash_move_forward_ex(properties, &pos)) { + if(zend_hash_get_current_key_ex(properties, &member, &member_len, &index, 0, &pos) != HASH_KEY_IS_STRING) { + continue; + } + + zval zmember; + ZVAL_STRING(&zmember, member, 0); + + zend_property_info *property_info = zend_get_property_info(c->std.ce, &zmember, 1 TSRMLS_CC); + if(property_info && property_info->flags & ZEND_ACC_PUBLIC) { + /* Write value to PHP JS object */ + php_obj->ForceSet(V8JS_SYML(member, member_len - 1), zval_to_v8js(*value, c->isolate TSRMLS_CC), v8::ReadOnly); + } + } + + } /* }}} */ From 683ac347c731e838f17d1408bf4f4c4fcbf98233 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 19 Oct 2013 02:25:25 +0200 Subject: [PATCH 6/7] Test export of undeclared properties in derived classes --- tests/derived_class_properties_extra.phpt | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/derived_class_properties_extra.phpt diff --git a/tests/derived_class_properties_extra.phpt b/tests/derived_class_properties_extra.phpt new file mode 100644 index 0000000..bbd4804 --- /dev/null +++ b/tests/derived_class_properties_extra.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test V8::executeString() : Extra properties on derived class +--SKIPIF-- + +--FILE-- +testing = 23; + $this->extraTesting = 42; + } +} + +$v8 = new V8Wrapper(); +$v8->executeString('print(PHP.testing + "\n");'); +$v8->executeString('print(PHP.extraTesting + "\n");'); +?> +===EOF=== +--EXPECT-- +undefined +42 +===EOF=== From 8eb5721c35466111635b98c22cde72406e31b3f5 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 19 Oct 2013 02:52:19 +0200 Subject: [PATCH 7/7] Copy default_properties to new object (PHP 5.3) --- v8js.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/v8js.cc b/v8js.cc index 4f59490..4cf2f08 100644 --- a/v8js.cc +++ b/v8js.cc @@ -536,6 +536,10 @@ static zend_object_value php_v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */ zend_object_std_init(&c->std, ce TSRMLS_CC); #if PHP_VERSION_ID >= 50400 object_properties_init(&c->std, ce); +#else + zval *tmp; + zend_hash_copy(c->std.properties, &ce->default_properties, + (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); #endif retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_free_storage, NULL TSRMLS_CC);