Repository navigation
Expand file tree
/
Copy pathisolate.h
More file actions
154 lines (131 loc) · 3.08 KB
/
Copy pathisolate.h
File metadata and controls
154 lines (131 loc) · 3.08 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* isolate.h
*
* Simple getter for the (unique) v8 isolate. This
* method makes sure that a single isolate gets
* constructed and that the v8 engine is properly
* initialized once.
*
* Note that this is explicitly not thread-safe,
* but it is fast. Since none of our other extensions
* are properly thread-safe, this is an acceptable
* limitation
*
* @copyright 2015 - 2026 Copernica B.V.
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include <v8.h>
#include "template.h"
#include "platform.h"
/**
* Start namespace
*/
namespace JS {
/**
* Forward declarations
*/
class Core;
/**
* Private class
*/
class Isolate final
{
private:
/**
* Pointer to the full javascript v8 platform
* @var Platform
*/
Platform *_platform;
/**
* The create-params
* v8::Isolate::CreateParams
*/
static v8::Isolate::CreateParams _params;
/**
* The underlying isolate
* @var v8::Isolate*
*/
static v8::Isolate *_isolate;
/**
* Templates for wrapping objects
* @var std::vector
*/
static std::vector<Template> _templates;
/**
* Total number of instances
* @var size_t
*/
static size_t _instances;
public:
/**
* Constructor that is called every time a "core" is created that needs an isolate
* @paran context
*/
Isolate(Core *core) : _platform(Platform::instance())
{
// do we already have an isolate
if (_instances++ != 0) return;
// we need an allocator
_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
// construct the isolate
_isolate = v8::Isolate::New(_params);
}
/**
* No copying
* @param that
*/
Isolate(const Isolate &that) = delete;
/**
* Destructor
*/
virtual ~Isolate()
{
// was this the last reference
if (--_instances != 0) return;
// remove the templates first before we dispose the isolate
_templates.clear();
// free up the isolate
_isolate->Dispose();
// free up allocator
delete _params.array_buffer_allocator;
}
/**
* Look for an appropriate template
* @param object
* @return Template
*/
const Template &prototype(const Php::Value &object)
{
// check the prototypes that we have
for (const auto &prototype : _templates)
{
// is this one compatible with the object
if (!prototype.matches(object)) continue;
// we can apply this prototype
return prototype;
}
// we need a new template
_templates.emplace_back(_isolate, object);
// use it
return _templates.back();
}
/**
* Cast to the underlying isolate
* @return v8::Isolate*
*/
operator v8::Isolate* () const
{
// expose underlying pointer
return _isolate;
}
};
/**
* End namespace
*/
}