diff --git a/php_v8js_macros.h b/php_v8js_macros.h index 5f7a517..d0e8cf7 100644 --- a/php_v8js_macros.h +++ b/php_v8js_macros.h @@ -142,8 +142,18 @@ v8::Handle zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC); /* Convert V8 value into zval */ int v8js_to_zval(v8::Handle, zval *, int, v8::Isolate * TSRMLS_DC); +struct php_v8js_accessor_ctx +{ + char *variable_name_string; + uint variable_name_string_len; + v8::Isolate *isolate; +}; + +void php_v8js_accessor_ctx_dtor(php_v8js_accessor_ctx * TSRMLS_DC); + /* Register accessors into passed object */ -void php_v8js_register_accessors(v8::Local, zval *, v8::Isolate * TSRMLS_DC); +void php_v8js_register_accessors(std::vector *accessor_list, v8::Local, zval *, v8::Isolate * TSRMLS_DC); + /* {{{ Context container */ struct php_v8js_ctx { @@ -161,6 +171,7 @@ struct php_v8js_ctx { std::vector modules_stack; std::vector modules_base; std::map template_cache; + std::vector accessor_list; #ifdef ZTS void ***zts_ctx; #endif diff --git a/v8js.cc b/v8js.cc index 9aa9ba8..ec56e00 100644 --- a/v8js.cc +++ b/v8js.cc @@ -554,6 +554,13 @@ static void php_v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ } c->template_cache.~map(); + /* Clear contexts */ + for (std::vector::iterator it = c->accessor_list.begin(); + it != c->accessor_list.end(); ++it) { + php_v8js_accessor_ctx_dtor(*it TSRMLS_CC); + } + c->accessor_list.~vector(); + /* Clear global object, dispose context */ if (!c->context.IsEmpty()) { c->context.Reset(); @@ -600,6 +607,7 @@ static zend_object_value php_v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */ new(&c->modules_stack) std::vector(); new(&c->modules_base) std::vector(); new(&c->template_cache) std::map(); + new(&c->accessor_list) std::vector(); retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_free_storage, NULL TSRMLS_CC); retval.handlers = &v8js_object_handlers; @@ -818,7 +826,7 @@ static PHP_METHOD(V8Js, __construct) /* Register Get accessor for passed variables */ if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) { - php_v8js_register_accessors(php_obj_t, vars_arr, isolate TSRMLS_CC); + php_v8js_register_accessors(&c->accessor_list, php_obj_t, vars_arr, isolate TSRMLS_CC); } /* Set name for the PHP JS object */ diff --git a/v8js_variables.cc b/v8js_variables.cc index a223f69..3e6ddf3 100644 --- a/v8js_variables.cc +++ b/v8js_variables.cc @@ -25,13 +25,6 @@ extern "C" { #include #include -struct php_v8js_accessor_ctx -{ - char *variable_name_string; - uint variable_name_string_len; - v8::Isolate *isolate; -}; - static void php_v8js_fetch_php_variable(v8::Local name, const v8::PropertyCallbackInfo& info) /* {{{ */ { v8::Handle data = v8::Handle::Cast(info.Data()); @@ -50,7 +43,14 @@ static void php_v8js_fetch_php_variable(v8::Local name, const v8::Pr } /* }}} */ -void php_v8js_register_accessors(v8::Local php_obj_t, zval *array, v8::Isolate *isolate TSRMLS_DC) /* {{{ */ +void php_v8js_accessor_ctx_dtor(php_v8js_accessor_ctx *ctx TSRMLS_DC) /* {{{ */ +{ + efree(ctx->variable_name_string); + efree(ctx); +} +/* }}} */ + +void php_v8js_register_accessors(std::vector *accessor_list, v8::Local php_obj_t, zval *array, v8::Isolate *isolate TSRMLS_DC) /* {{{ */ { char *property_name; uint property_name_len; @@ -87,6 +87,9 @@ void php_v8js_register_accessors(v8::Local php_obj_t, zval /* Set the variable fetch callback for given symbol on named property */ php_obj->SetAccessor(V8JS_STRL(property_name, property_name_len - 1), php_v8js_fetch_php_variable, NULL, v8::External::New(ctx), v8::PROHIBITS_OVERWRITING, v8::ReadOnly, v8::AccessorSignature::New(php_obj_t)); + + /* record the context so we can free it later */ + accessor_list->push_back(ctx); } } /* }}} */