-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathisolate.h
More file actions
173 lines (149 loc) · 3.56 KB
/
isolate.h
File metadata and controls
173 lines (149 loc) · 3.56 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* 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 Copernica B.V.
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
//#include <v8-platform.h>
//#include <chrono>
#include <v8.h>
//#include <map>
/**
* Start namespace
*/
namespace JS {
/**
* Forward declarations
*/
class Core;
/**
* Private class
*/
class Isolate final
{
private:
/**
* The create-params
* v8::Isolate::CreateParams
*/
v8::Isolate::CreateParams _params;
/**
* The underlying isolate
* @var v8::Isolate*
*/
v8::Isolate *_isolate;
/**
* Indexes for storing pointers
* @var int
*/
static const int ISOLATE_INDEX = 0;
static const int CORE_INDEX = 1;
/**
* List of tasks to execute
* @var std::multimap<std::chrono::system_clock::time_point, std::unique_ptr<v8::Task>>
*/
// std::multimap<std::chrono::system_clock::time_point, std::unique_ptr<v8::Task>> _tasks;
/**
* Perform all waiting tasks for this isolate
*/
// void runTasks();
public:
/**
* Constructor
* A core-pointer has to be passed to the isolate, to make the Core::upgrade() method work
* @paran context
*/
Isolate(Core *core)
{
// we need an allocator
_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
// construct the isolate
_isolate = v8::Isolate::New(_params);
// store a pointer to the core
_isolate->SetData(CORE_INDEX, core);
// store a pointer to the isolate
_isolate->SetData(ISOLATE_INDEX, this);
}
/**
* No copying
* @param that
*/
Isolate(const Isolate &that) = delete;
/**
* Destructor
*/
virtual ~Isolate()
{
// free up the isolate
_isolate->Dispose();
// free up allocator
delete _params.array_buffer_allocator;
}
/**
* Get access to the isolate
* @param isolate
* @return Isolate
*/
static Isolate *update(v8::Isolate *isolate)
{
// is stored in a data field
return static_cast<Isolate *>(isolate->GetData(ISOLATE_INDEX));
}
/**
* Get access to the core
* @param isolate
* @return Core
*/
static Core *core(v8::Isolate *isolate)
{
// is stored in a data field
return static_cast<Core *>(isolate->GetData(CORE_INDEX));
}
/**
* Schedule a task to be executed
*
* @param isolate The isolate to schedule the task under
* @param task The task to execute
* @param delay Number of seconds to wait before executing
*/
//static void scheduleTask(v8::Isolate *isolate, v8::Task *task, double delay);
/**
* Get the isolate for this thread
*
* @return The thread-local isolate instance
*/
//static v8::Isolate *get();
/**
* Clean up the isolate - if any - for this thread
*/
//static void destroy();
/**
* Cast to the underlying isolate
* @return v8::Isolate*
*/
operator v8::Isolate* () const
{
// expose underlying pointer
return _isolate;
}
};
/**
* End namespace
*/
}