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