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
//! §16.2.1.7 `HostLoadImportedModule` — the one fact about a module the language cannot know.
//!
//! A `ModuleSpecifier` is text. Whether it names a file, a URL, a database row or an entry in a map
//! the embedder built is the host's business and nothing the specification will ever settle — the
//! same division DR-0014 makes for the local time zone: ViperJS implements the clause and the host
//! supplies the fact.
//!
//! # Why a trait, when a graph was enough before
//!
//! A *static* `import` names its modules in the source, so a host can read the whole graph before
//! anything runs and hand it over — which is what [`Graph`](super::Graph) is for, and it remains the
//! simplest way to run a module. A **dynamic** `import()` cannot be answered that way: the specifier
//! is whatever an expression evaluated to, so the host has to be reachable *while the program runs*.
//!
//! A machine with no loader is not an error until a program asks for one. `import("x")` then rejects
//! its promise, which is what §16.2.1.7 says a host that cannot load a module does — so an embedder
//! that wants no modules at all gets that behaviour by doing nothing.
use crateChunk;
use crateHeap;
use crateValue;
use Rc;
/// How the host answers for a `ModuleSpecifier` — §16.2.1.7.
///
/// Called with the specifier as the source wrote it, after `ToString`. The implementation resolves
/// it however it likes, parses it as a **Module** (§16.2), compiles it, and answers with the chunk.
///
/// The heap is lent because compiling needs one: a chunk's constants — its strings, its regular
/// expressions — live on it, so a module compiled against a different heap would hold handles this
/// one cannot read. See [`crate::compile::compile_module`].
///
/// # Answering twice for one specifier
///
/// The engine memoises: a specifier it has already loaded is never asked for again, so a host need
/// not keep a cache of its own to make a module evaluate once. What a host **must** do is answer
/// consistently, since a specifier that answered differently later would put two records where the
/// specification has one.
/// What the engine remembers about one module — as much of §16.2.1's Cyclic Module Record as it has.
///
/// Kept on the [`Vm`](super::Vm) rather than in a local of the link, because §16.2.1.6's "each body
/// once" is a fact about the whole execution: a dynamic `import()` arriving later has to find what
/// an earlier one evaluated, and find the *same* namespace object for it.
pub