1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/js-sandbox PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the |
11
|
|
|
* LICENSE file that was distributed with this source or visit |
12
|
|
|
* http://opensource.org/licenses/MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace Pinepain\JsSandbox\Wrappers\CallbackGuards; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Exceptions\EscapableException; |
20
|
|
|
use Pinepain\JsSandbox\Exceptions\NativeException; |
21
|
|
|
use Throwable; |
22
|
|
|
use V8\CallbackInfoInterface; |
23
|
|
|
use V8\ExceptionManager; |
24
|
|
|
use V8\Exceptions\TryCatchException; |
25
|
|
|
use V8\ObjectValue; |
26
|
|
|
use V8\StringValue; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class CallbackGuard implements CallbackGuardInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function guard(callable $callback): callable |
35
|
|
|
{ |
36
|
|
|
return function (...$arguments) use ($callback) { |
37
|
|
|
/** @var CallbackInfoInterface $args */ |
38
|
|
|
$args = end($arguments); // By convention, CallbackInfoInterface (FunctionCallbackInfo or PropertyCallbackInfo) is the last argument passed to callback |
39
|
|
|
|
40
|
|
|
assert($args instanceof CallbackInfoInterface); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$isolate = $args->getIsolate(); |
43
|
|
|
$context = $args->getContext(); |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
return $callback(...$arguments); |
47
|
|
|
} catch (TryCatchException $e) { |
|
|
|
|
48
|
|
|
$native_exception = $e->getTryCatch()->getException(); |
49
|
|
|
|
50
|
|
|
if (!$native_exception) { |
51
|
|
|
$message = $this->getMessageFromException($e); |
52
|
|
|
$isolate->throwException($context, ExceptionManager::createError($context, new StringValue($isolate, $message)), $e); |
53
|
|
|
} elseif ($native_exception instanceof ObjectValue) { |
|
|
|
|
54
|
|
|
$isolate->throwException($context, $native_exception, $e); |
55
|
|
|
} else { |
56
|
|
|
// TODO: convert non-object native exception to object and wire with external exception $e ? |
57
|
|
|
$isolate->throwException($context, $native_exception); |
58
|
|
|
} |
59
|
|
|
} catch (NativeException $e) { |
60
|
|
|
$isolate->throwException($context, ExceptionManager::createError($context, new StringValue($isolate, $e->getMessage())), $e); |
61
|
|
|
} catch (EscapableException $e) { |
62
|
|
|
throw $e->getOriginal(); |
63
|
|
|
} catch (Throwable $e) { |
64
|
|
|
// UNEXPECTED |
65
|
|
|
$message = $this->getMessageFromException($e); |
66
|
|
|
$isolate->throwException($context, ExceptionManager::createError($context, new StringValue($isolate, $message)), $e); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
}; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getMessageFromException(Throwable $e): string |
74
|
|
|
{ |
75
|
|
|
return 'An internal exception occurred'; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.