7

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 :-)

favo
  • 5,426
  • 9
  • 42
  • 61

1 Answers1

5

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

COUGH
  • 3
  • 2
wmarbut
  • 4,595
  • 7
  • 42
  • 72
  • Thank you for answering! I tried the examples. In the global example it is only executed once, all subsequent calls on executeString did not execute the extension code again. – favo May 31 '12 at 06:55
  • Thanks, you are correct. I've updated my answer to reflect that :) – wmarbut May 31 '12 at 12:31