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\NativeWrappers; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Wrappers\WrapperInterface; |
20
|
|
|
use V8\Context; |
21
|
|
|
use V8\FunctionObject; |
22
|
|
|
use V8\Isolate; |
23
|
|
|
use V8\ObjectValue; |
24
|
|
|
use V8\StringValue; |
25
|
|
|
use V8\Value; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class NativeObjectWrapper implements NativeObjectWrapperInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Isolate |
32
|
|
|
*/ |
33
|
|
|
protected $isolate; |
34
|
|
|
/** |
35
|
|
|
* @var Context |
36
|
|
|
*/ |
37
|
|
|
protected $context; |
38
|
|
|
/** |
39
|
|
|
* @var ObjectValue|FunctionObject |
40
|
|
|
*/ |
41
|
|
|
protected $target; |
42
|
|
|
/** |
43
|
|
|
* @var WrapperInterface |
44
|
|
|
*/ |
45
|
|
|
protected $wrapper; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
3 |
|
public function __construct(Isolate $isolate, Context $context, ObjectValue $target, WrapperInterface $wrapper) |
51
|
|
|
{ |
52
|
3 |
|
$this->isolate = $isolate; |
53
|
3 |
|
$this->context = $context; |
54
|
3 |
|
$this->target = $target; |
55
|
3 |
|
$this->wrapper = $wrapper; |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
1 |
|
public function set(string $key, $value): void |
62
|
|
|
{ |
63
|
1 |
|
$wrapped_key = $this->wrapKey($key); |
64
|
1 |
|
$wrapped_value = $this->wrapper->wrap($this->isolate, $this->context, $value); |
65
|
|
|
|
66
|
1 |
|
$this->target->set($this->context, $wrapped_key, $wrapped_value); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function get(string $key): Value |
73
|
|
|
{ |
74
|
1 |
|
$wrapped_key = $this->wrapKey($key); |
75
|
|
|
|
76
|
1 |
|
$value = $this->target->get($this->context, $wrapped_key); |
77
|
|
|
|
78
|
|
|
// TODO: could we extract PHP value from the native v8 value? |
79
|
1 |
|
return $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
1 |
|
public function has(string $key): bool |
86
|
|
|
{ |
87
|
1 |
|
$wrapped_key = $this->wrapKey($key); |
88
|
|
|
|
89
|
1 |
|
return $this->target->has($this->context, $wrapped_key); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
1 |
|
public function delete(string $key): bool |
96
|
|
|
{ |
97
|
1 |
|
$wrapped_key = $this->wrapKey($key); |
98
|
|
|
|
99
|
1 |
|
return $this->target->delete($this->context, $wrapped_key); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
protected function wrapKey(string $key): StringValue |
103
|
|
|
{ |
104
|
1 |
|
return new StringValue($this->isolate, $key); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|