-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
57 lines (49 loc) · 1.5 KB
/
test.php
File metadata and controls
57 lines (49 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
use Pjs\Runtime;
use Pjs\Context;
// use Pjs\Value;
use Pjs\Exception;
$rt = new Runtime();
$ctx = new Context($rt);
echo '-------- Tests --------', PHP_EOL;
echo 'eval: new Date', PHP_EOL;
echo 'result: ';
var_dump($ctx->eval("new Date"));
echo '--------', PHP_EOL;
echo 'eval: 1+1', PHP_EOL;
echo 'result: ';
var_dump($ctx->eval("1+1"));
echo '--------', PHP_EOL;
try {
echo 'eval: a()', PHP_EOL;
$ctx->eval("a()");
} catch(Exception $e) {
echo 'exception: ', 'class: ', $e::class, ', error: ', $e->getMessage(), PHP_EOL, $e->getTraceAsString(), PHP_EOL;
}
echo '--------', PHP_EOL;
// Load and execute JavaScript code from test.js file
echo '-------- Loading test.js --------', PHP_EOL;
$jsFile = __DIR__ . '/test.js';
if (file_exists($jsFile)) {
$jsCode = file_get_contents($jsFile);
echo 'eval: test.js', PHP_EOL;
echo 'result: ', PHP_EOL;
$result = $ctx->eval($jsCode);
var_dump($result);
// Test calling functions defined in test.js individually
echo '--------', PHP_EOL;
echo 'eval: greet("World")', PHP_EOL;
echo 'result: ';
var_dump($ctx->eval('greet("World")'));
echo '--------', PHP_EOL;
echo 'eval: fibonacci(8)', PHP_EOL;
echo 'result: ';
var_dump($ctx->eval('fibonacci(8)'));
echo '--------', PHP_EOL;
echo 'eval: person.introduce()', PHP_EOL;
echo 'result: ';
var_dump($ctx->eval('testResults.person.introduce()'));
} else {
echo 'test.js file not found!', PHP_EOL;
}