-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathapi.cc
228 lines (193 loc) · 6.51 KB
/
api.cc
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "third_party/v8/include/libplatform/libplatform.h"
#include "third_party/v8/include/v8.h"
#include "third_party/v8/src/base/logging.h"
#include "deno.h"
#include "exceptions.h"
#include "internal.h"
extern "C" {
Deno* deno_new_snapshotter(deno_config config) {
CHECK(config.will_snapshot);
// TODO(ry) Support loading snapshots before snapshotting.
CHECK_NULL(config.load_snapshot.data_ptr);
auto* creator = new v8::SnapshotCreator(deno::external_references);
auto* isolate = creator->GetIsolate();
auto* d = new deno::DenoIsolate(config);
d->snapshot_creator_ = creator;
d->AddIsolate(isolate);
{
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
auto context = v8::Context::New(isolate);
d->context_.Reset(isolate, context);
creator->SetDefaultContext(context,
v8::SerializeInternalFieldsCallback(
deno::SerializeInternalFields, nullptr));
deno::InitializeContext(isolate, context);
}
return reinterpret_cast<Deno*>(d);
}
Deno* deno_new(deno_config config) {
if (config.will_snapshot) {
return deno_new_snapshotter(config);
}
deno::DenoIsolate* d = new deno::DenoIsolate(config);
v8::Isolate::CreateParams params;
params.array_buffer_allocator = d->array_buffer_allocator_;
params.external_references = deno::external_references;
if (config.load_snapshot.data_ptr) {
params.snapshot_blob = &d->snapshot_;
}
v8::Isolate* isolate = v8::Isolate::New(params);
d->AddIsolate(isolate);
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
{
v8::HandleScope handle_scope(isolate);
auto context =
v8::Context::New(isolate, nullptr, v8::MaybeLocal<v8::ObjectTemplate>(),
v8::MaybeLocal<v8::Value>(),
v8::DeserializeInternalFieldsCallback(
deno::DeserializeInternalFields, nullptr));
if (!config.load_snapshot.data_ptr) {
// If no snapshot is provided, we initialize the context with empty
// main source code and source maps.
deno::InitializeContext(isolate, context);
}
d->context_.Reset(isolate, context);
}
return reinterpret_cast<Deno*>(d);
}
deno::DenoIsolate* unwrap(Deno* d_) {
return reinterpret_cast<deno::DenoIsolate*>(d_);
}
void deno_lock(Deno* d_) {
auto* d = unwrap(d_);
CHECK_NULL(d->locker_);
d->locker_ = new v8::Locker(d->isolate_);
}
void deno_unlock(Deno* d_) {
auto* d = unwrap(d_);
CHECK_NOT_NULL(d->locker_);
delete d->locker_;
d->locker_ = nullptr;
}
deno_buf deno_get_snapshot(Deno* d_) {
auto* d = unwrap(d_);
CHECK_NOT_NULL(d->snapshot_creator_);
d->ClearModules();
d->context_.Reset();
auto blob = d->snapshot_creator_->CreateBlob(
v8::SnapshotCreator::FunctionCodeHandling::kKeep);
return {nullptr, 0, reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)),
blob.raw_size, 0};
}
static std::unique_ptr<v8::Platform> platform;
void deno_init() {
CHECK_NULL(platform.get());
platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
}
const char* deno_v8_version() { return v8::V8::GetVersion(); }
void deno_set_v8_flags(int* argc, char** argv) {
v8::V8::SetFlagsFromCommandLine(argc, argv, true);
}
const char* deno_last_exception(Deno* d_) {
auto* d = unwrap(d_);
if (d->last_exception_.length() > 0) {
return d->last_exception_.c_str();
} else {
return nullptr;
}
}
void deno_execute(Deno* d_, void* user_data, const char* js_filename,
const char* js_source) {
auto* d = unwrap(d_);
deno::UserDataScope user_data_scope(d, user_data);
auto* isolate = d->isolate_;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
auto context = d->context_.Get(d->isolate_);
CHECK(!context.IsEmpty());
deno::Execute(context, js_filename, js_source);
}
void deno_zero_copy_release(Deno* d_, size_t zero_copy_id) {
auto* d = unwrap(d_);
v8::Isolate::Scope isolate_scope(d->isolate_);
v8::Locker locker(d->isolate_);
v8::HandleScope handle_scope(d->isolate_);
d->DeleteZeroCopyRef(zero_copy_id);
}
void deno_respond(Deno* d_, void* user_data, deno_buf buf) {
auto* d = unwrap(d_);
if (d->current_args_ != nullptr) {
// Synchronous response.
if (buf.data_ptr != nullptr) {
DCHECK_EQ(buf.zero_copy_id, 0);
auto ab = deno::ImportBuf(d, buf);
d->current_args_->GetReturnValue().Set(ab);
}
d->current_args_ = nullptr;
return;
}
// Asynchronous response.
deno::UserDataScope user_data_scope(d, user_data);
v8::Locker locker(d->isolate_);
v8::Isolate::Scope isolate_scope(d->isolate_);
v8::HandleScope handle_scope(d->isolate_);
auto context = d->context_.Get(d->isolate_);
v8::Context::Scope context_scope(context);
v8::TryCatch try_catch(d->isolate_);
auto recv_ = d->recv_.Get(d->isolate_);
if (recv_.IsEmpty()) {
d->last_exception_ = "libdeno.recv_ has not been called.";
return;
}
v8::Local<v8::Value> args[1];
int argc = 0;
// You cannot use zero_copy_buf with deno_respond(). Use
// deno_zero_copy_release() instead.
DCHECK_EQ(buf.zero_copy_id, 0);
if (buf.data_ptr != nullptr) {
args[0] = deno::ImportBuf(d, buf);
argc = 1;
}
auto v = recv_->Call(context, context->Global(), argc, args);
if (try_catch.HasCaught()) {
CHECK(v.IsEmpty());
deno::HandleException(context, try_catch.Exception());
}
}
void deno_check_promise_errors(Deno* d_) {
auto* d = unwrap(d_);
if (d->pending_promise_map_.size() > 0) {
auto* isolate = d->isolate_;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
auto context = d->context_.Get(d->isolate_);
v8::Context::Scope context_scope(context);
auto it = d->pending_promise_map_.begin();
while (it != d->pending_promise_map_.end()) {
auto error = it->second.Get(isolate);
deno::HandleException(context, error);
it = d->pending_promise_map_.erase(it);
}
}
}
void deno_delete(Deno* d_) {
deno::DenoIsolate* d = reinterpret_cast<deno::DenoIsolate*>(d_);
delete d;
}
void deno_terminate_execution(Deno* d_) {
deno::DenoIsolate* d = reinterpret_cast<deno::DenoIsolate*>(d_);
d->isolate_->TerminateExecution();
}
}