phpv8 / v8js

V8 Javascript Engine for PHP — This PHP extension embeds the Google V8 Javascript Engine
http://pecl.php.net/package/v8js
MIT License
1.83k stars 200 forks source link

unregistering extensions #42

Closed rbro closed 7 years ago

rbro commented 10 years ago

Once an extension is loaded with registerExtension, is there a way to unregister an extension or overwrite it with new JS code? I was able to do it by restarting apache, but is there another way that it can be done that doesn't require restarting apache? Thanks for your help.

stesie commented 10 years ago

No, extensions can neither be removed nor can they be added once V8 is fully initialized -- this is a limitation of V8, not of V8Js.

What are you trying to do? It shouldn't be needed to restart the Apache server completely, after all restarting the PHP process should be sufficient (i.e. next http request) as those don't share a V8 instance.

rbro commented 10 years ago

Thanks for your help. I am running both apache/mod_php and nginx/php-fpm. In both web servers, extensions registered with registerExtension seem to persist from request to request.

For example, using apache/mod_php, if I run the following script:

<?php V8Js::registerExtension('test', 'function test() { print("test1"); }', array(), true); $v8 = new V8Js(); $v8->executeString('test()', 'test_call'); ?>

I see test1 printed as expected.

I then edited the script to change the test1 to test2 as in:

<?php V8Js::registerExtension('test', 'function test() { print("test2"); }', array(), true); $v8 = new V8Js(); $v8->executeString('test()', 'test_call'); ?>

If I run the script repeatedly, I sometimes get test1 and other times get test2. I believe it's related to the number of child processes apache is running and which one serves my request, but I haven't confirmed.

Also, if I change my script to be:

<?php $v8 = new V8Js(); $v8->executeString('test()', 'test_call'); ?>

I also get a mix of test1 and test2 output even though the extension wasn't registered explicitly in my script. The extension appears to be persisting outside of the request.

Is this intended? That was the reason I was asking about the unregister functionality as right now, I have to restart apache to update my script.

Please let me know if I can supply more information. Thanks again for your help.

stesie commented 10 years ago

hey

I had another look at the issue. My previous statement was not quiet correct, since a single php process indeed serves multiple requests at least with the mentioned SAPIs. And since v8 is only started once per process (i.e. not per request) loaded extensions are shared between multiple requests.

Unfortunately v8 cannot be shutdown and restarted within a single process, neither can an extension be unloaded. Hence I would consider this behavior unwanted but unfixable also (with mod_php and fpm)

You might want to use the cgi version of php, which has performance implications on the other hand, but serves only one request per process and hence should not show that behavior.

cheers stesie

cscott commented 10 years ago

It might be worth reworking the extensions API in v8js more accurately as a 'persistent connection' sort of thing -- and possibly raising an error if you open a V8Js object without the registered extensions, due to the way the v8 API works. Currently there is some odd behavior with respect to the lifetime of the 'extensions' global compared to the lifetime of the v8 instance -- if you're running under ZTS you can have a thread which does not share the extensions global but which does inherit the extensions configuration from the other threads.

satoshi75nakamoto commented 10 years ago

@cscott — This seems like a very reasonable approach to me and I'm all for it. I'd like to fix as much as the odd behavior as possible. Once again @stesie and @cscott I really appreciate all that you've done thus far.

stesie commented 7 years ago

Won't fix, see discusssion above + extensions now marked deprecated (with pull #330)