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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#include "node_buffer.h"
#include "node_internals.h"
#include "libplatform/libplatform.h"
#include <string>
#include "gtest/gtest.h"
#include "node_test_fixture.h"
using node::AtExit;
using node::RunAtExit;
static bool called_cb_1 = false;
static bool called_cb_2 = false;
static bool called_cb_ordered_1 = false;
static bool called_cb_ordered_2 = false;
static bool called_at_exit_js = false;
static void at_exit_callback1(void* arg);
static void at_exit_callback2(void* arg);
static void at_exit_callback_ordered1(void* arg);
static void at_exit_callback_ordered2(void* arg);
static void at_exit_js(void* arg);
static std::string cb_1_arg; // NOLINT(runtime/string)
class EnvironmentTest : public EnvironmentTestFixture {
private:
void TearDown() override {
NodeTestFixture::TearDown();
called_cb_1 = false;
called_cb_2 = false;
called_cb_ordered_1 = false;
called_cb_ordered_2 = false;
}
};
// TODO(codebytere): re-enable this test.
// TEST_F(EnvironmentTest, PreExeuctionPreparation) {
// const v8::HandleScope handle_scope(isolate_);
// const Argv argv;
// Env env {handle_scope, argv};
// v8::Local<v8::Context> context = isolate_->GetCurrentContext();
// const char* run_script = "process.argv0";
// v8::Local<v8::Script> script = v8::Script::Compile(
// context,
// v8::String::NewFromOneByte(isolate_,
// reinterpret_cast<const uint8_t*>(run_script),
// v8::NewStringType::kNormal).ToLocalChecked())
// .ToLocalChecked();
// v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
// CHECK(result->IsString());
// }
TEST_F(EnvironmentTest, AtExitWithEnvironment) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, argv};
AtExit(*env, at_exit_callback1);
RunAtExit(*env);
EXPECT_TRUE(called_cb_1);
}
TEST_F(EnvironmentTest, AtExitWithoutEnvironment) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, argv};
AtExit(at_exit_callback1); // No Environment is passed to AtExit.
RunAtExit(*env);
EXPECT_TRUE(called_cb_1);
}
TEST_F(EnvironmentTest, AtExitOrder) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, argv};
// Test that callbacks are run in reverse order.
AtExit(*env, at_exit_callback_ordered1);
AtExit(*env, at_exit_callback_ordered2);
RunAtExit(*env);
EXPECT_TRUE(called_cb_ordered_1);
EXPECT_TRUE(called_cb_ordered_2);
}
TEST_F(EnvironmentTest, AtExitWithArgument) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, argv};
std::string arg{"some args"};
AtExit(*env, at_exit_callback1, static_cast<void*>(&arg));
RunAtExit(*env);
EXPECT_EQ(arg, cb_1_arg);
}
TEST_F(EnvironmentTest, AtExitRunsJS) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, argv};
AtExit(*env, at_exit_js, static_cast<void*>(isolate_));
EXPECT_FALSE(called_at_exit_js);
RunAtExit(*env);
EXPECT_TRUE(called_at_exit_js);
}
TEST_F(EnvironmentTest, MultipleEnvironmentsPerIsolate) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env1 {handle_scope, argv};
Env env2 {handle_scope, argv};
AtExit(*env1, at_exit_callback1);
AtExit(*env2, at_exit_callback2);
RunAtExit(*env1);
EXPECT_TRUE(called_cb_1);
EXPECT_FALSE(called_cb_2);
RunAtExit(*env2);
EXPECT_TRUE(called_cb_2);
}
TEST_F(EnvironmentTest, NoEnvironmentSanity) {
const v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context = v8::Context::New(isolate_);
EXPECT_EQ(node::Environment::GetCurrent(context), nullptr);
EXPECT_EQ(node::GetCurrentEnvironment(context), nullptr);
EXPECT_EQ(node::Environment::GetCurrent(isolate_), nullptr);
v8::Context::Scope context_scope(context);
EXPECT_EQ(node::Environment::GetCurrent(context), nullptr);
EXPECT_EQ(node::GetCurrentEnvironment(context), nullptr);
EXPECT_EQ(node::Environment::GetCurrent(isolate_), nullptr);
}
TEST_F(EnvironmentTest, NonNodeJSContext) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env {handle_scope, argv};
EXPECT_EQ(node::Environment::GetCurrent(v8::Local<v8::Context>()), nullptr);
node::Environment* env = *test_env;
EXPECT_EQ(node::Environment::GetCurrent(isolate_), env);
EXPECT_EQ(node::Environment::GetCurrent(env->context()), env);
EXPECT_EQ(node::GetCurrentEnvironment(env->context()), env);
v8::Local<v8::Context> context = v8::Context::New(isolate_);
EXPECT_EQ(node::Environment::GetCurrent(context), nullptr);
EXPECT_EQ(node::GetCurrentEnvironment(context), nullptr);
EXPECT_EQ(node::Environment::GetCurrent(isolate_), env);
v8::Context::Scope context_scope(context);
EXPECT_EQ(node::Environment::GetCurrent(context), nullptr);
EXPECT_EQ(node::GetCurrentEnvironment(context), nullptr);
EXPECT_EQ(node::Environment::GetCurrent(isolate_), nullptr);
}
static void at_exit_callback1(void* arg) {
called_cb_1 = true;
if (arg) {
cb_1_arg = *static_cast<std::string*>(arg);
}
}
static void at_exit_callback2(void* arg) {
called_cb_2 = true;
}
static void at_exit_callback_ordered1(void* arg) {
EXPECT_TRUE(called_cb_ordered_2);
called_cb_ordered_1 = true;
}
static void at_exit_callback_ordered2(void* arg) {
EXPECT_FALSE(called_cb_ordered_1);
called_cb_ordered_2 = true;
}
static void at_exit_js(void* arg) {
v8::Isolate* isolate = static_cast<v8::Isolate*>(arg);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> obj = v8::Object::New(isolate);
assert(!obj.IsEmpty()); // Assert VM is still alive.
assert(obj->IsObject());
called_at_exit_js = true;
}
// TODO(addaleax): Re-enable this test once it is possible to initialize the
// Environment properly.
/*
TEST_F(EnvironmentTest, SetImmediateCleanup) {
int called = 0;
int called_unref = 0;
{
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, argv};
(*env)->SetImmediate([&](node::Environment* env_arg) {
EXPECT_EQ(env_arg, *env);
called++;
});
(*env)->SetUnrefImmediate([&](node::Environment* env_arg) {
EXPECT_EQ(env_arg, *env);
called_unref++;
});
}
EXPECT_EQ(called, 1);
EXPECT_EQ(called_unref, 0);
}*/
static char hello[] = "hello";
TEST_F(EnvironmentTest, BufferWithFreeCallbackIsDetached) {
// Test that a Buffer allocated with a free callback is detached after
// its callback has been called.
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
int callback_calls = 0;
v8::Local<v8::ArrayBuffer> ab;
{
Env env {handle_scope, argv};
v8::Local<v8::Object> buf_obj = node::Buffer::New(
isolate_,
hello,
sizeof(hello),
[](char* data, void* hint) {
CHECK_EQ(data, hello);
++*static_cast<int*>(hint);
},
&callback_calls).ToLocalChecked();
CHECK(buf_obj->IsUint8Array());
ab = buf_obj.As<v8::Uint8Array>()->Buffer();
CHECK_EQ(ab->ByteLength(), sizeof(hello));
}
CHECK_EQ(callback_calls, 1);
CHECK_EQ(ab->ByteLength(), 0);
}
|