diff --git a/tests/ctx_lifetime.phpt b/tests/ctx_lifetime.phpt new file mode 100644 index 0000000..c3f75eb --- /dev/null +++ b/tests/ctx_lifetime.phpt @@ -0,0 +1,55 @@ +--TEST-- +Test V8::executeString() : Testing lifespan of V8Js context objects +--SKIPIF-- + +--FILE-- +foo = new Foo; + return $v8js->executeString("({ bar: 23, hello: function() { PHP.foo.__call('hello',[]); } })"); + // $v8js will be dereferenced here, but the result escapes. + } +} + +$t = new Testing(); + +$a = $t->onectx(); +/* $a is no longer valid, since the associated V8Js() object has been + * destroyed. Instead the property access will throw. */ +var_dump($a); + +try { + var_dump($a->bar); +} +catch(Exception $e) { + var_dump($e->getMessage()); +} + +$a->hello(); + +?> +===EOF=== +--EXPECTF-- +object(V8Object)#%d (0) { +} +string(55) "Can't access V8Object after V8Js instance is destroyed!" + +Warning: Uncaught exception 'V8JsScriptException' with message 'Can't access V8Object after V8Js instance is destroyed!' in %s/tests/ctx_lifetime.php:35 +Stack trace: +#0 %s/tests/ctx_lifetime.php(35): unknown() +#1 {main} + thrown in %s/tests/ctx_lifetime.php on line 35 + +Fatal error: Call to undefined method V8Object::hello() in %s/tests/ctx_lifetime.php on line 35 diff --git a/v8js.cc b/v8js.cc index 746bbf0..cd9ad87 100644 --- a/v8js.cc +++ b/v8js.cc @@ -205,6 +205,7 @@ static zval *php_v8js_v8_read_property(zval *object, zval *member, int type ZEND if (!obj->ctx) { zend_throw_exception(php_ce_v8js_script_exception, "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC); + ALLOC_INIT_ZVAL(retval); return retval; }