Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Extensions in php v8js?

I have started using v8js with php for a while now but the documentation is really thin.

One thing that is not explained is Extensions.

It is possible to registerExtension but it is not explained in detail how these behave or whats their purpose or benefits.

Can anyone provide a good description or link to a documentation that explains Extensions?

Thanks to everyone for taking time to read and answer :-)

like image 999
favo Avatar asked Jan 26 '12 19:01

favo


1 Answers

Original Answer

My original answer indicated that the extension was called every time executeString was.

Corrected answer

An extension is a bit of code that is executed before the first executeString call for a given V8Js instance. Extension can be global to all V8Js instances or local to a specific instance.

I have experimentally determined that this isn't always very reliable. If you frantically refresh a page, you may not always see the extension get run... This is probably why this is beta quality software.

Here are two examples that I whipped up

Global Extension Example

Code

V8Js::registerExtension('say_hi', 'print("hey from extension! "); var said_hi=true;', array(), true);
$v8 = new V8Js();
$v8->executeString('print("hello from regular code!")', 'test.php');
$v8->executeString('if (said_hi) { print(" extension already said hi"); }');

Output

hey from extension! hello from regular code! extension already said hi

Non-Global Example

Code

V8Js::registerExtension('say_hi', 'print("hey from non global extension! "); var said_hi=true;');
$v8 = new V8Js('PHP', array(), array('say_hi'));
$v8->executeString('print("hello from regular code!");', 'test.php');
$v8->executeString('if (said_hi) { print(" extension already said hi"); }');

Output

hey from non global extension! hello from regular code! extension already said hi

like image 147
wmarbut Avatar answered Sep 30 '22 13:09

wmarbut