-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathlinker.cpp
More file actions
130 lines (107 loc) · 2.86 KB
/
linker.cpp
File metadata and controls
130 lines (107 loc) · 2.86 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Linker.cpp
*
* Implementation file for the Linker class
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2025 Copernica BV
*/
/**
* Dependencies
*/
#include "linker.h"
#include "link.h"
#include "core.h"
/**
* Begin of namespace
*/
namespace JS {
/**
* Constructor
* @param isolate the active isolate
* @param key the private symbol that is associated storing external pointers
* @param object the javascript object to be linked
*/
Linker::Linker(v8::Isolate *isolate, const v8::Global<v8::Private> &key, const v8::Local<v8::Object> &object) :
_isolate(isolate), _key(key.Get(isolate)), _object(object) {}
/**
* Constructor
* @param isolage the isolate
* @param object the javascript object to be linked
*/
Linker::Linker(v8::Isolate *isolate, const v8::Local<v8::Object> &object) :
Linker(isolate, Core::upgrade(isolate)->symbol(), object) {}
/**
* Get the internal pointer
* @return Link
*/
Link *Linker::pointer() const
{
// check if the private propery exists
v8::MaybeLocal<v8::Value> property = _object->GetPrivate(_isolate->GetCurrentContext(), _key);
// if not set, or if not a pointer to an external thing
if (property.IsEmpty()) return nullptr;
// turn into a local
v8::Local<v8::Value> value = property.ToLocalChecked();
// should be external
if (!value->IsExternal()) return nullptr;
// get the external property, and cast to a Php::Value
return static_cast<Link*>(value.As<v8::External>()->Value());
}
/**
* Is the linker associated with a PHP object?
* @return bool
*/
bool Linker::valid() const
{
// check the pointer
return pointer() != nullptr;
}
/**
* Associate the object with a PHP variable
* @param value
* @param weak
* @return Php::Value
*/
const Php::Value &Linker::attach(const Php::Value &value, bool weak)
{
// remove the old pointer
detach();
// make a brand new link
auto *link = new Link(_isolate, _object, value, weak);
// associate property
_object->SetPrivate(_isolate->GetCurrentContext(), _key, v8::External::New(_isolate, link));
// done, expose the same value
return value;
}
/**
* Detach the PHP object from the javascript object
*/
void Linker::detach()
{
// get the link pointer
Link *link = pointer();
// if not even set
if (link == nullptr) return;
// remove the link
delete link;
// remove private property
_object->DeletePrivate(_isolate->GetCurrentContext(), _key);
}
/**
* Expose the object in PHP space
* @return Php::Value
*/
Php::Value Linker::value() const
{
// get the link pointer
Link *link = pointer();
// if not set
if (link == nullptr) return nullptr;
// get the php value
return link->value();
}
/**
* End of namespace
*/
}