Usage sample:
if (V8Js::registerExtension('myjs', 'var x = 1 + 1;', array(), true) === false) {
exit("Failed to register js extension script");
}
$v8js = new V8Js;
$jsExec = <<<EOD
x;
EOD;
echo $v8js->executeString($jsExec)."\n"; // print "2"
(PECL v8js >= 0.1.0)
V8Js::registerExtension — V8Js で使う JavaScript の拡張を登録する
$extension_name
,$script
,$dependencies
= array(),$auto_enable
= false
渡された JavaScript script
を
V8Js コンテキストで利用する拡張として登録します。
拡張の登録に成功した場合に true
、それ以外の場合に false
を返します。
Usage sample:
if (V8Js::registerExtension('myjs', 'var x = 1 + 1;', array(), true) === false) {
exit("Failed to register js extension script");
}
$v8js = new V8Js;
$jsExec = <<<EOD
x;
EOD;
echo $v8js->executeString($jsExec)."\n"; // print "2"
Note that since version 2.0.0 V8Js::registerExtension is deprecated and suggests use snapshots instead https://github.com/phpv8/v8js/releases/tag/2.0.0
Simple example using snapshots and the moment.js:
<?php
$script = file_get_contents('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js');
$snapshot = V8Js::createSnapshot($script);
$v8 = new V8Js('php', array(), array(), true, $snapshot);
echo $v8->executeString('moment().format()');
?>
Side-note: If you value speed, security and stability do not use file_get_contents to grab external javascripts on production servers.