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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
#ifndef INTERNAL_H_
#define INTERNAL_H_
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "buffer.h"
#include "deno.h"
#include "v8/include/v8.h"
#include "v8/src/base/logging.h"
namespace deno {
struct ModuleInfo {
bool main;
std::string name;
v8::Persistent<v8::Module> handle;
std::vector<std::string> import_specifiers;
ModuleInfo(v8::Isolate* isolate, v8::Local<v8::Module> module, bool main_,
const char* name_, std::vector<std::string> import_specifiers_)
: main(main_), name(name_), import_specifiers(import_specifiers_) {
handle.Reset(isolate, module);
}
};
// deno_s = Wrapped Isolate.
class DenoIsolate {
public:
explicit DenoIsolate(deno_config config)
: isolate_(nullptr),
locker_(nullptr),
shared_(config.shared),
current_args_(nullptr),
snapshot_creator_(nullptr),
global_import_buf_ptr_(nullptr),
recv_cb_(config.recv_cb),
user_data_(nullptr),
resolve_cb_(nullptr),
next_dyn_import_id_(0),
dyn_import_cb_(config.dyn_import_cb),
has_snapshotted_(false) {
if (config.load_snapshot.data_ptr) {
snapshot_.data =
reinterpret_cast<const char*>(config.load_snapshot.data_ptr);
snapshot_.raw_size = static_cast<int>(config.load_snapshot.data_len);
}
}
~DenoIsolate() {
last_exception_handle_.Reset();
shared_ab_.Reset();
if (locker_) {
delete locker_;
}
if (snapshot_creator_) {
// TODO(ry) V8 has a strange assert which prevents a SnapshotCreator from
// being deallocated if it hasn't created a snapshot yet.
// https://github.com/v8/v8/blob/73212783fbd534fac76cc4b66aac899c13f71fc8/src/api.cc#L603
// If that assert is removed, this if guard could be removed.
// WARNING: There may be false positive LSAN errors here.
if (has_snapshotted_) {
delete snapshot_creator_;
}
} else {
isolate_->Dispose();
}
}
static inline DenoIsolate* FromIsolate(v8::Isolate* isolate) {
return static_cast<DenoIsolate*>(isolate->GetData(0));
}
void AddIsolate(v8::Isolate* isolate);
deno_mod RegisterModule(bool main, const char* name, const char* source);
void ClearModules();
ModuleInfo* GetModuleInfo(deno_mod id) {
if (id == 0) {
return nullptr;
}
auto it = mods_.find(id);
if (it != mods_.end()) {
return &it->second;
} else {
return nullptr;
}
}
v8::Isolate* isolate_;
v8::Locker* locker_;
deno_buf shared_;
const v8::FunctionCallbackInfo<v8::Value>* current_args_;
v8::SnapshotCreator* snapshot_creator_;
void* global_import_buf_ptr_;
deno_recv_cb recv_cb_;
void* user_data_;
std::map<deno_mod, ModuleInfo> mods_;
std::map<std::string, deno_mod> mods_by_name_;
deno_resolve_cb resolve_cb_;
deno_dyn_import_id next_dyn_import_id_;
deno_dyn_import_cb dyn_import_cb_;
std::map<deno_dyn_import_id, v8::Persistent<v8::Promise::Resolver>>
dyn_import_map_;
v8::Persistent<v8::Context> context_;
std::map<int, v8::Persistent<v8::Value>> pending_promise_map_;
std::string last_exception_;
v8::Persistent<v8::Value> last_exception_handle_;
v8::Persistent<v8::Function> recv_;
v8::StartupData snapshot_;
v8::Persistent<v8::ArrayBuffer> global_import_buf_;
v8::Persistent<v8::SharedArrayBuffer> shared_ab_;
bool has_snapshotted_;
};
class UserDataScope {
DenoIsolate* deno_;
void* prev_data_;
void* data_; // Not necessary; only for sanity checking.
public:
UserDataScope(DenoIsolate* deno, void* data) : deno_(deno), data_(data) {
CHECK(deno->user_data_ == nullptr || deno->user_data_ == data_);
prev_data_ = deno->user_data_;
deno->user_data_ = data;
}
~UserDataScope() {
CHECK(deno_->user_data_ == data_);
deno_->user_data_ = prev_data_;
}
};
struct InternalFieldData {
uint32_t data;
};
static inline v8::Local<v8::String> v8_str(const char* x) {
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x,
v8::NewStringType::kNormal)
.ToLocalChecked();
}
static inline DenoIsolate* unwrap(Deno* d_) {
return reinterpret_cast<deno::DenoIsolate*>(d_);
}
const char* ToCString(const v8::String::Utf8Value& value);
void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args);
void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
void EvalContext(const v8::FunctionCallbackInfo<v8::Value>& args);
void ErrorToJSON(const v8::FunctionCallbackInfo<v8::Value>& args);
void Shared(v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
void MessageCallback(v8::Local<v8::Message> message, v8::Local<v8::Value> data);
void QueueMicrotask(const v8::FunctionCallbackInfo<v8::Value>& args);
static intptr_t external_references[] = {
reinterpret_cast<intptr_t>(Print),
reinterpret_cast<intptr_t>(Recv),
reinterpret_cast<intptr_t>(Send),
reinterpret_cast<intptr_t>(EvalContext),
reinterpret_cast<intptr_t>(ErrorToJSON),
reinterpret_cast<intptr_t>(Shared),
reinterpret_cast<intptr_t>(MessageCallback),
reinterpret_cast<intptr_t>(QueueMicrotask),
0};
static const deno_buf empty_buf = {nullptr, 0};
static const deno_snapshot empty_snapshot = {nullptr, 0};
Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb);
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context);
void DeserializeInternalFields(v8::Local<v8::Object> holder, int index,
v8::StartupData payload, void* data);
v8::StartupData SerializeInternalFields(v8::Local<v8::Object> holder, int index,
void* data);
v8::Local<v8::Uint8Array> ImportBuf(DenoIsolate* d, deno_buf buf);
bool Execute(v8::Local<v8::Context> context, const char* js_filename,
const char* js_source);
bool ExecuteMod(v8::Local<v8::Context> context, const char* js_filename,
const char* js_source, bool resolve_only);
} // namespace deno
extern "C" {
// This is just to workaround the linker.
struct deno_s {
deno::DenoIsolate isolate;
};
}
#endif // INTERNAL_H_
|