PHP code example of chenos / execjs

1. Go to this page and download the library: Download chenos/execjs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chenos / execjs example snippets


make example

use Chenos\ExecJs\Context;
use Chenos\V8JsModuleLoader\ModuleLoader;

$context = new Context('PHP');

$context->getLoader()
    ->setEntryDir(__DIR__)
    ->addVendorDir(__DIR__.'/node_modules')
    ->addOverride('vue', 'vue/dist/vue.runtime.common.js')
    ;
// or
$loader = new ModuleLoader();
$loader->setEntryDir(__DIR__)
    ->addVendorDir(__DIR__.'/node_modules')
    ->addOverride('vue', 'vue/dist/vue.runtime.common.js')
    ;
$context->setLoader($loader);

$context->eval(string $script);
$context->load(string $module);
$context->

public mixed function eval(string $script, int $flags = V8Js::FLAG_NONE, int $timeLimit = 0, int $memoryLimit = 0)

public mixed function V8Js::executeString(string $script, string $identifier = '', int $flags = V8Js::FLAG_NONE, int $timeLimit = 0, int $memoryLimit = 0)

$context->eval('1+1'); // 2

public mixed function load(string $module, int $flags = V8Js::FLAG_NONE, int $timeLimit = 0, int $memoryLimit = 0)

$context->load('./foo.js');
// equals
$str = $context->getLoader()->loadModule('./foo.js');
$context->eval($str);

public mixed function 

$yaml = $context->'); // ['a' => 'b']

$yaml = $context->t->eval("var jsyaml = => 'b']
$context->eval(sprintf('jsyaml.dump(%s)', json_encode(['a' => 'b']))); // 'a: b'

$context->
$context->eval("var {load, dump} = b']
$context->eval(sprintf('dump(%s)', json_encode(['a' => 'b']))); // 'a: b'

$context->"yamlLoad('a: b')"); // ['a' => 'b']

public Context function set(string $key, mixed $value, $global = false)

$context->set('foo', 'bar');
$context->eval('PHP.foo'); // bar
$context->eval('foo'); // error undefined

$context->set('bar', 'baz', true);
$context->eval('PHP.bar'); // baz
$context->eval('bar'); // baz

$context->set('process', [
    'env' => [
        'NODE_ENV' => 'production',
    ],
], true);

$context->eval('process.env.NODE_ENV'); // production