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
//! Js runtime hooks: promise, import and import.meta, etc.
use crateJsRuntime;
use cratethrow_type_error;
use crateresolve_import;
use crate*;
/// Called during `Module::instantiate_module`, see:
/// - Rusty V8 API:
/// - <https://docs.rs/v8/latest/v8/struct.Module.html#method.instantiate_module>.
/// - <https://docs.rs/rusty_v8/latest/rusty_v8/type.ResolveModuleCallback.html>
/// - Node V8 API: <https://v8docs.nodesource.com/node-24.1/df/d74/classv8_1_1_module.html#a3313f8faa14b6dc5d37c340d45273bf1>.
/// Called the first time import.meta is accessed for a module.
/// See: <https://docs.rs/v8/0.49.0/v8/type.HostInitializeImportMetaObjectCallback.html>.
pub extern "C"
/// Called when a promise rejects with no rejection handler specified.
/// See: <https://docs.rs/v8/0.49.0/v8/type.PromiseRejectCallback.html>.
/// See: <https://v8.dev/features/promise-combinators>.
pub extern "C"
// // Called when we require the embedder to load a module.
// // https://docs.rs/v8/0.56.1/v8/trait.HostImportModuleDynamicallyCallback.html
// // https://v8.dev/features/dynamic-import
// pub fn host_import_module_dynamically_cb<'s>(
// scope: &mut v8::HandleScope<'s>,
// _: v8::Local<'s, v8::Data>,
// base: v8::Local<'s, v8::Value>,
// specifier: v8::Local<'s, v8::String>,
// _: v8::Local<v8::FixedArray>,
// ) -> Option<v8::Local<'s, v8::Promise>> {
// // Get module base and specifier as strings.
// let base = base.to_rust_string_lossy(scope);
// let specifier = specifier.to_rust_string_lossy(scope);
//
// // Create the import promise.
// let promise_resolver = v8::PromiseResolver::new(scope).unwrap();
// let promise = promise_resolver.get_promise(scope);
//
// let state_rc = JsRuntime::state(scope);
// let mut state = state_rc.borrow_mut();
//
// let import_map = state.options.import_map.clone();
//
// let resolved = resolve_import(Some(&base), &specifier, false, import_map);
// if resolved.is_err() {
// let e = resolved.err().unwrap();
// drop(state);
// let exception = v8::String::new(scope, &e.to_string()).unwrap();
// let exception = v8::Exception::error(scope, exception);
// set_exception_code(scope, exception, &e);
// promise_resolver.reject(scope, exception);
// return Some(promise);
// }
//
// let specifier = resolved.unwrap();
//
// let dynamic_import_being_fetched = state
// .module_map
// .pending
// .iter()
// .any(|graph_rc| graph_rc.borrow().root_rc.borrow().path == specifier);
//
// // Check if the requested dynamic module is already resolved.
// if state.module_map.index.contains_key(&specifier) && !dynamic_import_being_fetched {
// // Create a local handle for the module.
// let module = state.module_map.get(&specifier).unwrap();
// let module = module.open(scope);
//
// // Note: Since this is a dynamic import will resolve the promise
// // with the module's namespace object instead of it's evaluation result.
// promise_resolver.resolve(scope, module.get_module_namespace());
// return Some(promise);
// }
//
// let global_promise = v8::Global::new(scope, promise_resolver);
//
// if dynamic_import_being_fetched {
// // Find the graph with the same root that is being resolved
// // and declare this graph as same origin.
// state
// .module_map
// .pending
// .iter()
// .find(|graph_rc| graph_rc.borrow().root_rc.borrow().path == specifier)
// .unwrap()
// .borrow_mut()
// .same_origin
// .push_back(global_promise);
//
// return Some(promise);
// }
//
// let graph = ModuleGraph::dynamic_import(&specifier, global_promise);
// let graph_rc = Rc::new(RefCell::new(graph));
// let status = ModuleStatus::Fetching;
//
// state.module_map.pending.push(Rc::clone(&graph_rc));
// state.module_map.seen.insert(specifier.clone(), status);
//
// let handle_task_err = |e: anyhow::Error| {
// let module = Rc::clone(&graph_rc.borrow().root_rc);
// if module.is_dynamic_import {
// module.exception.borrow_mut().replace(e.to_string());
// }
// };
//
// let task = |source: ModuleSource| {
// let tc_scope = &mut v8::TryCatch::new(scope);
// let origin = create_origin(tc_scope, &specifier, true);
// let root_module_rc = Rc::clone(&graph_rc.borrow().root_rc);
//
// // Compile source and get it's dependencies.
// let source = v8::String::new(tc_scope, &source).unwrap();
// let mut source = v8::script_compiler::Source::new(source, Some(&origin));
//
// let module = match v8::script_compiler::compile_module(tc_scope, &mut source) {
// Some(module) => module,
// None => {
// assert!(tc_scope.has_caught());
// let exception = tc_scope.exception().unwrap();
// let exception = JsError::from_v8_exception(tc_scope, exception, None);
// let exception = format!("{} ({})", exception.message, exception.resource_name);
//
// handle_task_err(anyhow::Error::msg(exception));
// return;
// }
// };
//
// let new_status = ModuleStatus::Resolving;
// let module_ref = v8::Global::new(tc_scope, module);
//
// state.module_map.insert(specifier.as_str(), module_ref);
// state.module_map.seen.insert(specifier.clone(), new_status);
//
// let import_map = state.options.import_map.clone();
//
// let skip_cache = match root_module_rc.borrow().is_dynamic_import {
// true => !state.options.test_mode,
// false => false,
// };
//
// let mut dependencies = vec![];
//
// let requests = module.get_module_requests();
// let base = specifier.clone();
//
// for i in 0..requests.length() {
// // Get import request from the `module_requests` array.
// let request = requests.get(tc_scope, i).unwrap();
// let request = v8::Local::<v8::ModuleRequest>::try_from(request).unwrap();
//
// // Transform v8's ModuleRequest into Rust string.
// let base = Some(base.as_str());
// let specifier = request.get_specifier().to_rust_string_lossy(tc_scope);
// let specifier = match resolve_import(base, &specifier, false, import_map.clone()) {
// Ok(specifier) => specifier,
// Err(e) => {
// handle_task_err(anyhow::Error::msg(e.to_string()));
// return;
// }
// };
//
// // Check if requested module has been seen already.
// let seen_module = state.module_map.seen.get(&specifier);
// let status = match seen_module {
// Some(ModuleStatus::Ready) => continue,
// Some(_) => ModuleStatus::Duplicate,
// None => ModuleStatus::Fetching,
// };
//
// // Create a new ES module instance.
// let es_module = Rc::new(RefCell::new(EsModule {
// path: specifier.clone(),
// status,
// dependencies: vec![],
// exception: Rc::clone(&root_module_rc.borrow().exception),
// is_dynamic_import: root_module_rc.borrow().is_dynamic_import,
// }));
//
// dependencies.push(Rc::clone(&es_module));
//
// // If the module is newly seen, use the event-loop to load
// // the requested module.
// if seen_module.is_none() {
// // Recursively going down.
// state.module_map.seen.insert(specifier, status);
// state.task_tracker.spawn_local(async move {
// let specifier = specifier.clone();
// move || match load_import(&specifier, false) {
// Ok(source) => state.task_tracker.spawn_local(async move { task(source) }),
// Err(e) => handle_task_err(e),
// }
// })
// }
// }
//
// root_module_rc.borrow_mut().status = ModuleStatus::Resolving;
// root_module_rc.borrow_mut().dependencies = dependencies;
// };
//
// /* Use the event-loop to asynchronously load the requested module. */
// state.task_tracker.spawn_local(async move {
// let specifier = specifier.clone();
// move || match load_import(&specifier, true) {
// AnyResult::Ok(source) => {
// // Successful load module source
// task(source)
// }
// Err(e) => {
// // Failed to load module source
// handle_task_err(e)
// }
// }
// });
//
// Some(promise)
// }