|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the pinepain/php-v8-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; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunction; |
|
20
|
|
|
use V8\Context; |
|
21
|
|
|
use V8\Isolate; |
|
22
|
|
|
use V8\Value; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class Wrapper implements WrapperInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var WrapperInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $primitive_wrapper; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var WrapperInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $function_wrapper; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var WrapperInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $object_wrapper; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var WrapperInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $array_wrapper; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function wrap(Isolate $isolate, Context $context, $value) |
|
48
|
|
|
{ |
|
49
|
|
|
if (null === $value || is_scalar($value)) { |
|
50
|
|
|
return $this->primitive_wrapper->wrap($isolate, $context, $value); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (is_object($value)) { |
|
54
|
|
|
// Some functions may return V8 native value |
|
55
|
|
|
if ($value instanceof Value) { |
|
56
|
|
|
return $value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($value instanceof RuntimeFunction) { |
|
60
|
|
|
return $this->function_wrapper->wrap($isolate, $context, $value); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->object_wrapper->wrap($isolate, $context, $value); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (is_array($value)) { |
|
67
|
|
|
return $this->array_wrapper->wrap($isolate, $context, $value); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
throw new WrapperException('Vale type ' . gettype($value) . ' is not supported'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param WrapperInterface $primitive_wrapper |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setPrimitiveWrapper(WrapperInterface $primitive_wrapper) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->primitive_wrapper = $primitive_wrapper; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param WrapperInterface $function_wrapper |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setFunctionWrapper(WrapperInterface $function_wrapper) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->function_wrapper = $function_wrapper; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param WrapperInterface $object_wrapper |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setObjectWrapper(WrapperInterface $object_wrapper) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->object_wrapper = $object_wrapper; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param WrapperInterface $array_wrapper |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setArrayWrapper(WrapperInterface $array_wrapper) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->array_wrapper = $array_wrapper; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|