-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathmodules_test.cc
426 lines (367 loc) · 14.4 KB
/
modules_test.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
#include "test.h"
static int exec_count = 0;
void recv_cb(void* user_data, deno_op_id op_id, deno_buf buf,
deno_pinned_buf zero_copy_buf) {
// We use this to check that scripts have executed.
EXPECT_EQ(1u, buf.data_len);
EXPECT_EQ(42u, op_id);
EXPECT_EQ(buf.data_ptr[0], 4);
EXPECT_EQ(zero_copy_buf.data_ptr, nullptr);
EXPECT_EQ(zero_copy_buf.data_len, 0u);
EXPECT_EQ(zero_copy_buf.pin, nullptr);
exec_count++;
}
TEST(ModulesTest, Resolution) {
exec_count = 0; // Reset
Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb, nullptr});
EXPECT_EQ(0, exec_count);
static deno_mod a = deno_mod_new(d, true, "a.js",
"import { b } from 'b.js'\n"
"if (b() != 'b') throw Error();\n"
"Deno.core.send(42, new Uint8Array([4]));");
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
const char* b_src = "export function b() { return 'b' }";
static deno_mod b = deno_mod_new(d, false, "b.js", b_src);
EXPECT_NE(b, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(0, exec_count);
EXPECT_EQ(1u, deno_mod_imports_len(d, a));
EXPECT_EQ(0u, deno_mod_imports_len(d, b));
EXPECT_STREQ("b.js", deno_mod_imports_get(d, a, 0));
EXPECT_EQ(nullptr, deno_mod_imports_get(d, a, 1));
EXPECT_EQ(nullptr, deno_mod_imports_get(d, b, 0));
static int resolve_count = 0;
auto resolve_cb = [](void* user_data, const char* specifier,
deno_mod referrer) {
EXPECT_EQ(referrer, a);
EXPECT_STREQ(specifier, "b.js");
resolve_count++;
return b;
};
deno_mod_instantiate(d, d, b, resolve_cb);
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(0, resolve_count);
EXPECT_EQ(0, exec_count);
deno_mod_instantiate(d, d, a, resolve_cb);
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(1, resolve_count);
EXPECT_EQ(0, exec_count);
deno_mod_evaluate(d, d, a);
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(1, resolve_count);
EXPECT_EQ(1, exec_count);
deno_delete(d);
}
TEST(ModulesTest, ResolutionError) {
exec_count = 0; // Reset
Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb, nullptr});
EXPECT_EQ(0, exec_count);
static deno_mod a = deno_mod_new(d, true, "a.js",
"import 'bad'\n"
"Deno.core.send(42, new Uint8Array([4]));");
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(0, exec_count);
EXPECT_EQ(1u, deno_mod_imports_len(d, a));
EXPECT_STREQ("bad", deno_mod_imports_get(d, a, 0));
static int resolve_count = 0;
auto resolve_cb = [](void* user_data, const char* specifier,
deno_mod referrer) {
EXPECT_EQ(referrer, a);
EXPECT_STREQ(specifier, "bad");
resolve_count++;
return 0;
};
deno_mod_instantiate(d, d, a, resolve_cb);
EXPECT_NE(nullptr, deno_last_exception(d));
EXPECT_EQ(1, resolve_count);
EXPECT_EQ(0, exec_count);
deno_delete(d);
}
TEST(ModulesTest, ImportMetaUrl) {
exec_count = 0; // Reset
Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb, nullptr});
EXPECT_EQ(0, exec_count);
static deno_mod a =
deno_mod_new(d, true, "a.js",
"if ('a.js' != import.meta.url) throw 'hmm'\n"
"Deno.core.send(42, new Uint8Array([4]));");
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, a, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(0, exec_count);
deno_mod_evaluate(d, d, a);
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(1, exec_count);
}
TEST(ModulesTest, ImportMetaMain) {
Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb, nullptr});
const char* throw_not_main_src = "if (!import.meta.main) throw 'err'";
static deno_mod throw_not_main =
deno_mod_new(d, true, "a.js", throw_not_main_src);
EXPECT_NE(throw_not_main, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, throw_not_main, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_evaluate(d, d, throw_not_main);
EXPECT_EQ(nullptr, deno_last_exception(d));
const char* throw_main_src = "if (import.meta.main) throw 'err'";
static deno_mod throw_main = deno_mod_new(d, false, "b.js", throw_main_src);
EXPECT_NE(throw_main, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, throw_main, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_evaluate(d, d, throw_main);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_delete(d);
}
TEST(ModulesTest, DynamicImportSuccess) {
exec_count = 0;
static int dyn_import_count = 0;
static deno_mod b = 0;
auto dyn_import_cb = [](auto user_data, const char* specifier,
const char* referrer, deno_dyn_import_id import_id) {
auto d = reinterpret_cast<Deno*>(user_data);
dyn_import_count++;
EXPECT_STREQ(specifier, "foo");
EXPECT_STREQ(referrer, "a.js");
deno_dyn_import_done(d, d, import_id, b, nullptr);
};
const char* src =
"(async () => { \n"
" let mod = await import('foo'); \n"
" assert(mod.b() === 'b'); \n"
// Send a message to signify that we're done.
" Deno.core.send(42, new Uint8Array([4])); \n"
"})(); \n";
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, dyn_import_cb});
static deno_mod a = deno_mod_new(d, true, "a.js", src);
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, a, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
const char* b_src = "export function b() { return 'b' }";
b = deno_mod_new(d, false, "b.js", b_src);
EXPECT_NE(b, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, b, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_evaluate(d, d, b);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_evaluate(d, d, a);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_check_promise_errors(d);
EXPECT_EQ(deno_last_exception(d), nullptr);
deno_delete(d);
EXPECT_EQ(1, exec_count);
EXPECT_EQ(1, dyn_import_count);
}
TEST(ModulesTest, DynamicImportError) {
exec_count = 0;
static int dyn_import_count = 0;
auto dyn_import_cb = [](auto user_data, const char* specifier,
const char* referrer, deno_dyn_import_id import_id) {
auto d = reinterpret_cast<Deno*>(user_data);
dyn_import_count++;
EXPECT_STREQ(specifier, "foo");
EXPECT_STREQ(referrer, "a.js");
// We indicate there was an error resolving by returning mod_id 0.
deno_dyn_import_done(d, d, import_id, 0, "foo not found");
};
const char* src =
"(async () => { \n"
" let mod = await import('foo'); \n"
// The following should be unreachable.
" Deno.core.send(42, new Uint8Array([4])); \n"
"})(); \n";
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, dyn_import_cb});
static deno_mod a = deno_mod_new(d, true, "a.js", src);
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, a, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
// No error when evaluating, because it's an async error.
deno_mod_evaluate(d, d, a);
EXPECT_EQ(nullptr, deno_last_exception(d));
// Now we should get an error.
deno_check_promise_errors(d);
EXPECT_NE(deno_last_exception(d), nullptr);
std::string e(deno_last_exception(d));
EXPECT_NE(e.find("Uncaught TypeError: foo not found"), std::string::npos);
deno_delete(d);
EXPECT_EQ(0, exec_count);
EXPECT_EQ(1, dyn_import_count);
}
TEST(ModulesTest, DynamicImportAsync) {
exec_count = 0;
static int dyn_import_count = 0;
static deno_mod b = 0;
static std::vector<deno_dyn_import_id> import_ids = {};
auto dyn_import_cb = [](auto user_data, const char* specifier,
const char* referrer, deno_dyn_import_id import_id) {
// auto d = reinterpret_cast<Deno*>(user_data);
dyn_import_count++;
EXPECT_STREQ(specifier, "foo");
EXPECT_STREQ(referrer, "a.js");
// We don't call deno_dyn_import_done until later.
import_ids.push_back(import_id);
};
const char* src =
"(async () => { \n"
" let mod = await import('foo'); \n"
" assert(mod.b() === 'b'); \n"
// AGAIN!
" mod = await import('foo'); \n"
" assert(mod.b() === 'b'); \n"
// Send a message to signify that we're done.
" Deno.core.send(42, new Uint8Array([4])); \n"
"})(); \n";
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, dyn_import_cb});
static deno_mod a = deno_mod_new(d, true, "a.js", src);
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, a, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
// Evaluate. We check that there are no errors, and Deno.core.send has not
// been called.
deno_mod_evaluate(d, d, a);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_check_promise_errors(d);
EXPECT_EQ(deno_last_exception(d), nullptr);
EXPECT_EQ(0, exec_count);
EXPECT_EQ(1, dyn_import_count);
// Instantiate b.js
const char* b_src = "export function b() { return 'b' }";
b = deno_mod_new(d, false, "b.js", b_src);
EXPECT_NE(b, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, b, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_evaluate(d, d, b);
EXPECT_EQ(nullptr, deno_last_exception(d));
// Now we resolve the import.
EXPECT_EQ(1u, import_ids.size());
auto import_id = import_ids.back();
import_ids.pop_back();
deno_dyn_import_done(d, d, import_id, b, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_check_promise_errors(d);
EXPECT_EQ(deno_last_exception(d), nullptr);
EXPECT_EQ(1u, import_ids.size());
EXPECT_EQ(2, dyn_import_count);
EXPECT_EQ(0, exec_count);
import_id = import_ids.back();
import_ids.pop_back();
deno_dyn_import_done(d, d, import_id, b, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_check_promise_errors(d);
EXPECT_EQ(deno_last_exception(d), nullptr);
// We still have to resolve the second one
EXPECT_EQ(2, dyn_import_count);
EXPECT_EQ(1, exec_count);
deno_delete(d);
}
TEST(ModulesTest, DynamicImportThrows) {
exec_count = 0;
static int dyn_import_count = 0;
static deno_mod b = 0;
static std::vector<deno_dyn_import_id> import_ids = {};
auto dyn_import_cb = [](auto user_data, const char* specifier,
const char* referrer, deno_dyn_import_id import_id) {
dyn_import_count++;
EXPECT_STREQ(specifier, "b.js");
EXPECT_STREQ(referrer, "a.js");
// We don't call deno_dyn_import_done until later.
import_ids.push_back(import_id);
};
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, dyn_import_cb});
// Instantiate and evaluate the root module. This should succeed.
const char* a_src =
"(async () => { \n"
" let mod = await import('b.js'); \n"
// unreachable
" Deno.core.send(42, new Uint8Array([4])); \n"
"})(); \n";
static deno_mod a = deno_mod_new(d, true, "a.js", a_src);
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, a, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_evaluate(d, d, a);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_check_promise_errors(d);
EXPECT_EQ(deno_last_exception(d), nullptr);
// Instantiate b.js, which should succeed.
const char* b_src = "throw new Error('foo')";
b = deno_mod_new(d, false, "b.js", b_src);
EXPECT_NE(b, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, b, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
// Evaluate b.js. It throws in the global scope, so deno_last_exception()
// should be non-null afterwards.
deno_mod_evaluate(d, d, b);
EXPECT_NE(deno_last_exception(d), nullptr);
// Resolve the dynamic import of b.js. Since deno_mod_evaluate() failed,
// we indicate failure to deno_dyn_import_done() by setting mod_id to 0.
// The last error should be picked up and cleared by deno_dyn_import_done().
EXPECT_EQ(1u, import_ids.size());
auto import_id = import_ids.back();
import_ids.pop_back();
deno_dyn_import_done(d, d, import_id, 0, nullptr);
EXPECT_EQ(deno_last_exception(d), nullptr);
// Since the dynamically imported module threw an error,
// it should show up as an unhandled promise rejection.
deno_check_promise_errors(d);
EXPECT_NE(deno_last_exception(d), nullptr);
std::string e(deno_last_exception(d));
EXPECT_NE(e.find("Uncaught Error: foo"), std::string::npos);
EXPECT_EQ(1, dyn_import_count);
EXPECT_EQ(0, exec_count);
deno_delete(d);
}
TEST(ModulesTest, DynamicImportSyntaxError) {
exec_count = 0;
static int dyn_import_count = 0;
auto dyn_import_cb = [](auto user_data, const char* specifier,
const char* referrer, deno_dyn_import_id import_id) {
auto d = reinterpret_cast<Deno*>(user_data);
dyn_import_count++;
EXPECT_STREQ(specifier, "b.js");
EXPECT_STREQ(referrer, "a.js");
// Compile b.js, which should fail because of the syntax error.
deno_mod b = deno_mod_new(d, false, "b.js", "syntax error");
EXPECT_EQ(b, 0);
EXPECT_NE(nullptr, deno_last_exception(d));
// `deno_dyn_import_done` should consume the last exception, and use it
// to reject the dynamic import promise.
deno_dyn_import_done(d, d, import_id, 0, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
};
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, dyn_import_cb});
// Instantiate and evaluate the root module. This should succeed.
const char* src =
"(async () => { \n"
" let mod = await import('b.js'); \n"
// unreachable
" Deno.core.send(42, new Uint8Array([4])); \n"
"})(); \n";
static deno_mod a = deno_mod_new(d, true, "a.js", src);
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_instantiate(d, d, a, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));
deno_mod_evaluate(d, d, a);
EXPECT_EQ(nullptr, deno_last_exception(d));
// The failed dynamic import should cause an unhandled promise rejection.
deno_check_promise_errors(d);
EXPECT_NE(deno_last_exception(d), nullptr);
EXPECT_NE(std::string(deno_last_exception(d)).find("Syntax"),
std::string::npos);
EXPECT_EQ(1, dyn_import_count);
EXPECT_EQ(0, exec_count);
deno_delete(d);
}