public function testDynCSS() { $vm = new \V8Js('PHP', array(), array()); \V8Js::registerExtension('browser_test', file_get_contents(MODULE_PATH . '/rz_root/module/lib/dyncss/js/browserEmulator.js')); \V8Js::registerExtension('absurdhat_test', file_get_contents(MODULE_PATH . '/rz_root/assets/notlive/dyncss/absurdhat.js')); \V8Js::registerExtension('absurd_test', file_get_contents(MODULE_PATH . '/rz_root/assets/notlive/dyncss/absurd.js')); $vm = new \V8Js('PHP', array(), array('browser_test', 'absurdhat_test', 'absurd_test')); $code = file_get_contents(MODULE_PATH . '/rz_root/assets/notlive/dyncss/dyncss.js'); $vm->executeString($code, 'dyncss_TEST'); }
/** * JS VM * @return \V8Js * @throws JSV8AppException */ protected function getVM() { if (is_null($this->vm)) { // cache buster $version = rand(); // emulate a (very poor) browser \V8Js::registerExtension('browserEmulator' . $version, file_get_contents($this->basePath . 'browserEmulator.js')); // load absurd.js browser version (shared with client) \V8Js::registerExtension('absurd' . $version, file_get_contents($this->assetPath . 'absurd.js')); \V8Js::registerExtension('absurdHat' . $version, file_get_contents($this->assetPath . 'absurdhat.js')); // load generic dyncss implementation \V8Js::registerExtension('dyncss' . $version, file_get_contents($this->assetPath . 'dyncss.js')); // create variable mapping $extensions = array('browserEmulator' . $version, 'absurd' . $version, 'absurdHat' . $version, 'dyncss' . $version); // start engine (discard any output via print) ob_start(); $vm = new \V8Js('PHP', array(), $extensions); ob_end_clean(); // add error handler $vm->error = function ($error) { throw new JSV8AppException($error); }; $this->vm = $vm; } return $this->vm; }
/** * Registers all distribution library modules as extensions. * These can be accessed from scripts like this: * * require("lodash"); * * var a = [ 'one', 'two', 'three' ]; * * _.each( a, function( element ) { * print( "Found " + element + " in array\n" ); * }); * * Please note that this requires a version of the V8 library above any that are currently * distributed with popular distributions. As such, if this feature is not available * (module loading), the "lodash" library will be automatically registered and injected * into all script contexts. * * @param array $extensions * * @return array * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException */ protected static function registerExtensions(array $extensions = []) { /** @noinspection PhpUndefinedClassInspection */ $existing = \V8Js::getExtensions(); $registered = array_diff($extensions, $existing); foreach ($registered as $module) { /** @noinspection PhpUndefinedClassInspection */ if (false === \V8Js::registerExtension($module, static::loadScriptingModule($module), [], false)) { throw new InternalServerErrorException('Failed to register V8Js extension script: ' . $module); } } return $registered; }
<?php /* $v8 = new V8Js; $v8->func = function ($a) { return var_export(func_get_args(), TRUE); }; $v8->executeString("PHP.func();", "arg_test1.js"); exit; */ var_dump(V8Js::registerExtension('myparser.js', 'function foo() { print("foobar!\\n"}', array('jstparser.js', 'json-template.js'), false)); var_dump(V8Js::registerExtension('myparser.js', 'function foo() { print("foobar!\\n"}', array('jstparser.js', 'json-template.js'), false)); var_dump(V8Js::registerExtension('jstparser.js', file_get_contents('js/jstparser.js'), array(), false)); //V8Js::registerExtension('json-template.js', file_get_contents('js/json-template.js'), array(), false); var_dump(V8JS::getExtensions()); $a = new V8Js('myobj', array(), array('jstparser.js')); $jstparser = <<<'EOT' var template = 'Gold & Hot Rod Red, as seen in the new <a href="http://blog.markturansky.com/archives/51">Iron Man trailer</a>!' + "\n" + '<table cellspacing="0" cellpadding="4">' + "\n" + ' <% for(var i = 0; i < 10; i++){ %> ' + "\n" + ' <tr>' + "\n" + ' <td style="background-color: <%= i % 2 == 0 ? \'red\' : \'gold\' %>">' + "\n" + ' Hi, <%=name%>! i is <%= i %>' + "\n" + ' </td>' + "\n" + ' </tr>' + "\n" + ' <% } %>' + "\n" + '</table>' + "\n" + 'Note that name is HTML escaped by default. Here it is without escaping:'+ '<%+ name %>'; Jst.evaluateSingleShot(template, {"name":"foobar"}); EOT; echo $a->executeString($jstparser, "ext_test1.js"), "\n"; $a->_SERVER = $_SERVER;
<?php V8Js::registerExtension('a', file_get_contents('js/json-template.js'), array('b')); V8Js::registerExtension('b', file_get_contents('js/jstparser.js'), array('a')); var_dump(V8JS::getExtensions()); $a = new V8Js('myobj', array(), array('b'));
public function registerExt($ext, $script, $dependencies = array(), $auto_enable = false) { return $this->v8->registerExtension($ext, $script, $dependencies, $auto_enable); }