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\Specs\FunctionSpec; |
20
|
|
|
use Pinepain\JsSandbox\Specs\ObjectSpecsCollectionInterface; |
21
|
|
|
use Pinepain\JsSandbox\Specs\ObjectSpecInterface; |
22
|
|
|
use Pinepain\JsSandbox\Wrappers\ObjectComponents\WrappersObjectStoreInterface; |
23
|
|
|
use Pinepain\JsSandbox\Wrappers\ObjectComponents\PropertiesHandlerInterface; |
24
|
|
|
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeObject; |
25
|
|
|
use Pinepain\JsSandbox\Wrappers\ObjectComponents\WrappedObject; |
26
|
|
|
use V8\Context; |
27
|
|
|
use V8\FunctionObject; |
28
|
|
|
use V8\FunctionTemplate; |
29
|
|
|
use V8\Isolate; |
30
|
|
|
use V8\ObjectValue; |
31
|
|
|
use V8\PropertyAttribute; |
32
|
|
|
use V8\StringValue; |
33
|
|
|
use V8\Value; |
34
|
|
|
use function is_object; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class ObjectWrapper implements WrapperInterface, WrapperAwareInterface |
38
|
|
|
{ |
39
|
|
|
use WrapperAwareTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ObjectSpecsCollectionInterface |
43
|
|
|
*/ |
44
|
|
|
private $specs; |
45
|
|
|
/** |
46
|
|
|
* @var WrappersObjectStoreInterface |
47
|
|
|
*/ |
48
|
|
|
private $wrappers_cache; |
49
|
|
|
/** |
50
|
|
|
* @var PropertiesHandlerInterface |
51
|
|
|
*/ |
52
|
|
|
private $properties_handler; |
53
|
|
|
|
54
|
|
|
public function __construct(WrappersObjectStoreInterface $wrappers_cache, ObjectSpecsCollectionInterface $specs, PropertiesHandlerInterface $properties_handler) |
55
|
|
|
{ |
56
|
|
|
$this->wrappers_cache = $wrappers_cache; |
57
|
|
|
$this->specs = $specs; |
58
|
|
|
$this->properties_handler = $properties_handler; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Isolate $isolate |
63
|
|
|
* @param Context $context |
64
|
|
|
* @param object $object |
65
|
|
|
* |
66
|
|
|
* @return ObjectValue |
67
|
|
|
* @throws WrapperException |
68
|
|
|
*/ |
69
|
|
|
public function wrap(Isolate $isolate, Context $context, $object) |
70
|
|
|
{ |
71
|
|
|
if (!is_object($object)) { |
72
|
|
|
// UNLIKELY |
73
|
|
|
throw new WrapperException('Value to wrap must be an object, ' . gettype($object) . ' given instead'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($object instanceof Value) { |
77
|
|
|
// UNLIKELY |
78
|
|
|
throw new WrapperException('Wrapping JS native values is not supported'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$js_object = null; |
82
|
|
|
$spec = null; |
83
|
|
|
|
84
|
|
|
if ($object instanceof WrappedObject) { |
85
|
|
|
$js_object = $object->getValue(); |
86
|
|
|
$spec = $object->getSpec(); |
87
|
|
|
$object = $object->getObject(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($this->wrappers_cache->has($object)) { |
91
|
|
|
return $this->wrappers_cache->get($object); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!$spec) { |
95
|
|
|
$spec_name = $this->getObjectSpecName($object); |
96
|
|
|
$spec = $this->getSpec($spec_name); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$bridge = new RuntimeObject($object, $spec, $this->wrapper); |
100
|
|
|
|
101
|
|
|
if ($js_object) { |
102
|
|
|
// we have wrapped js object, but without property set |
103
|
|
|
$this->setProperties($isolate, $context, $spec, $js_object, $bridge); |
104
|
|
|
} else { |
105
|
|
|
// when we have already wrapped object (e.g. when we build function which also acts as an object) |
106
|
|
|
// we make this wrapped php object tied to it initial js wrapper |
107
|
|
|
$js_object = $this->createWrapper($isolate, $context, $spec, $bridge); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->wrappers_cache->put($object, $js_object); |
111
|
|
|
|
112
|
|
|
return $js_object; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function getObjectSpecName($object): string |
116
|
|
|
{ |
117
|
|
|
return get_class($object); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
* |
123
|
|
|
* @return ObjectSpecInterface |
124
|
|
|
*/ |
125
|
|
|
protected function getSpec(string $name): ObjectSpecInterface |
126
|
|
|
{ |
127
|
|
|
return $this->specs->get($name); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function getWrapperFunction(Isolate $isolate, Context $context, ObjectSpecInterface $spec, RuntimeObject $bridge): FunctionObject |
131
|
|
|
{ |
132
|
|
|
$tpl = new FunctionTemplate($isolate); |
133
|
|
|
$tpl->setClassName(new StringValue($isolate, $spec->getName())); |
134
|
|
|
$tpl->instanceTemplate()->setHandlerForNamedProperty($this->properties_handler->createConfiguration($bridge)); |
135
|
|
|
// NOTE: we don't handle static properties here |
136
|
|
|
// NOTE: we don't handle inheritance here |
137
|
|
|
|
138
|
|
|
// TODO: add support to call Object as Function |
139
|
|
|
//$tpl->InstanceTemplate()->SetCallAsFunctionHandler(); |
|
|
|
|
140
|
|
|
|
141
|
|
|
$func = $tpl->getFunction($context); |
142
|
|
|
|
143
|
|
|
return $func; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function createWrapper(Isolate $isolate, Context $context, ObjectSpecInterface $spec, RuntimeObject $bridge): ObjectValue |
147
|
|
|
{ |
148
|
|
|
return $this->getWrapperFunction($isolate, $context, $spec, $bridge)->newInstance($context); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function setProperties(Isolate $isolate, Context $context, ObjectSpecInterface $spec, ObjectValue $js_object, RuntimeObject $bridge) |
152
|
|
|
{ |
153
|
|
|
$getter = $this->properties_handler->createGetter($bridge); |
154
|
|
|
$setter = $this->properties_handler->createSetter($bridge); |
155
|
|
|
|
156
|
|
|
foreach ($spec->getProperties() as $name => $property_spec) { |
157
|
|
|
$js_name = new StringValue($isolate, $name); |
158
|
|
|
$attributes = PropertyAttribute::DONT_DELETE; |
159
|
|
|
|
160
|
|
|
if ($property_spec instanceof FunctionSpec || $property_spec->isReadonly()) { |
161
|
|
|
$attributes |= PropertyAttribute::READ_ONLY; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$js_object->setNativeDataProperty($context, $js_name, $getter, $setter, $attributes); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.