rusty_racer 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -148,6 +148,35 @@ pub(crate) enum Request {
148
148
  // instead of V8's default lazy top-level-only compile. Ignored when
149
149
  // cached_data is set (V8 forbids ConsumeCodeCache + EagerCompile).
150
150
  eager: bool,
151
+ // A primitive its ScriptOrigin carries as host-defined options, which V8
152
+ // hands the dynamic-import hook of an import() it makes.
153
+ host_defined: Option<JsVal>,
154
+ },
155
+ // A synthetic module (v8::Module::CreateSyntheticModule) exporting
156
+ // `default` alone, of the value `export` makes, in the context.
157
+ CreateSyntheticModule {
158
+ context_id: i32,
159
+ filename: String,
160
+ export: SyntheticExport,
161
+ timeout_ms: u64,
162
+ },
163
+ // The module's script id (v8::Module::ScriptId): what a stack frame of its
164
+ // code names it by (StackFrame::GetScriptId); none for a synthetic module
165
+ // or an errored one.
166
+ ModuleScriptId {
167
+ module_id: i32,
168
+ },
169
+ // The module's requests (v8::Module::GetModuleRequests): each specifier and
170
+ // its import attributes, in source order.
171
+ ModuleRequests {
172
+ module_id: i32,
173
+ },
174
+ // Settle a waiting import() (ImportReg) with `settle`; replies whether it
175
+ // was still waiting.
176
+ SettleImport {
177
+ import_id: u64,
178
+ settle: Settle,
179
+ timeout_ms: u64,
151
180
  },
152
181
  // instantiate: V8 walks imports, calling back to the Ruby resolve block
153
182
  // (parked in the slot for the op) per edge via resolve_imported.
@@ -157,9 +186,9 @@ pub(crate) enum Request {
157
186
  EvaluateModule {
158
187
  module_id: i32,
159
188
  timeout_ms: u64,
160
- // The name of a function on the module's context's global that reports its evaluation's rejection — HTML "run
161
- // a module script": upon rejection, report the exception — now or as a top-level-await module settles; none:
162
- // the rejection is the op's error, as far as the drain gets.
189
+ // The name of a function on the module's context's global (an `@` path: on its host object) that reports its
190
+ // evaluation's rejection — HTML "run a module script": upon rejection, report the exception — now or as a
191
+ // top-level-await module settles; none: the rejection is the op's error, as far as the drain gets.
163
192
  report: Option<String>,
164
193
  },
165
194
  ModuleNamespace {
@@ -185,6 +214,7 @@ pub(crate) enum Request {
185
214
  cached_data: Option<Vec<u8>>,
186
215
  produce_cache: bool,
187
216
  eager: bool,
217
+ host_defined: Option<JsVal>,
188
218
  },
189
219
  // Bind the script to its context and run it; returns the completion value
190
220
  // (unless |void| — see Eval).
@@ -201,6 +231,11 @@ pub(crate) enum Request {
201
231
  ScriptCodeCache {
202
232
  script_id: i32,
203
233
  },
234
+ // The script's V8 script id (v8::UnboundScript::GetId): what a stack frame
235
+ // of its code names it by (StackFrame::GetScriptId).
236
+ UnboundScriptId {
237
+ script_id: i32,
238
+ },
204
239
  ModuleCodeCache {
205
240
  module_id: i32,
206
241
  },
@@ -225,6 +260,25 @@ pub(crate) enum Request {
225
260
  },
226
261
  }
227
262
 
263
+ // What a synthetic module's default export is: a value marshalled from Ruby, the
264
+ // value %JSON.parse% makes of a JSON text (ECMA-262 ParseJSONModule), or what
265
+ // the function at a path (as Context#call names one) returns, called with args.
266
+ pub(crate) enum SyntheticExport {
267
+ Value(JsVal),
268
+ Json(String),
269
+ Call { path: String, args: Vec<JsVal> },
270
+ }
271
+
272
+ // How a waiting import() is settled: with a module (linked by the block
273
+ // DynamicImport#resolve parked, and evaluated), rejected with a value
274
+ // marshalled from Ruby, or rejected with what the function at a path returns
275
+ // (or throws), called with args.
276
+ pub(crate) enum Settle {
277
+ Module(i32),
278
+ Reject(JsVal),
279
+ RejectWith { path: String, args: Vec<JsVal> },
280
+ }
281
+
228
282
  // compile_module result: the module's id plus any produced bytecode cache and
229
283
  // whether a supplied cache was rejected.
230
284
  pub(crate) struct Compiled {
@@ -274,7 +328,7 @@ pub(crate) fn run_source(
274
328
  let Some(code) = v8::String::new(tc, source) else {
275
329
  return Err(VmError::Parse("source too large".into(), None));
276
330
  };
277
- let origin = script_origin(tc, filename);
331
+ let origin = script_origin(tc, filename, None);
278
332
  let script = match v8::Script::compile(tc, code, Some(&origin)) {
279
333
  Some(script) => script,
280
334
  None if tc.has_terminated() => return Err(VmError::Terminated),
@@ -294,19 +348,20 @@ pub(crate) fn run_source(
294
348
  }
295
349
  }
296
350
 
297
- // Resolve a dotted property path on the current realm's globalThis to a
298
- // function and the holder it was read off — its `this`, so `a.b.f` gets the right
299
- // receiver. Each segment is an ordinary [[Get]], so a getter or Proxy trap on the
300
- // path runs; when one throws, the error is Err(None): the caller's TryCatch holds
301
- // it (`caught!`).
351
+ // Resolve a dotted property path on the current realm's globalThis — or, for an
352
+ // `@` path, on its host object (`host_object`) — to a function and the holder it
353
+ // was read off — its `this`, so `a.b.f` gets the right receiver. Each segment is
354
+ // an ordinary [[Get]] (an `@` path's of an own property alone: path_segment), so
355
+ // a getter or Proxy trap on the path runs; when one throws, the error is
356
+ // Err(None): the caller's TryCatch holds it (`caught!`).
302
357
  fn resolve_function<'s>(
303
358
  scope: &mut v8::PinScope<'s, '_>,
304
359
  path: &str,
305
360
  ) -> Result<(v8::Local<'s, v8::Function>, v8::Local<'s, v8::Value>), Option<VmError>> {
306
- let global = scope.get_current_context().global(scope);
307
- let mut holder: v8::Local<v8::Value> = global.into();
308
- let mut target: v8::Local<v8::Value> = global.into();
309
- for part in path.split('.') {
361
+ let (root, rest, hidden) = path_root(scope, path);
362
+ let mut holder: v8::Local<v8::Value> = root.into();
363
+ let mut target: v8::Local<v8::Value> = root.into();
364
+ for part in rest.split('.') {
310
365
  let Some(obj) = target.to_object(scope) else {
311
366
  // The holder of `part` (a preceding segment) was null/undefined, so
312
367
  // there's nothing to read `part` from — name the holder, not `part`.
@@ -317,7 +372,7 @@ fn resolve_function<'s>(
317
372
  let Some(key) = v8::String::new(scope, part) else {
318
373
  return Err(Some(VmError::Runtime("property name too large".into())));
319
374
  };
320
- let Some(next) = obj.get(scope, key.into()) else {
375
+ let Some(next) = path_segment(scope, obj, key, hidden) else {
321
376
  return Err(None);
322
377
  };
323
378
  holder = target;
@@ -463,7 +518,11 @@ fn request_realm(state: &IsolateState, request: &Request) -> Option<i32> {
463
518
  | Request::Attach { context_id, .. }
464
519
  | Request::AttachMany { context_id, .. }
465
520
  | Request::CompileModule { context_id, .. }
521
+ | Request::CreateSyntheticModule { context_id, .. }
466
522
  | Request::CompileScript { context_id, .. } => Some(*context_id),
523
+ Request::SettleImport { import_id, .. } => {
524
+ state.imports.by_id.get(import_id).map(|(_, cid)| *cid)
525
+ }
467
526
  Request::DrainMicrotasks { .. } => Some(0),
468
527
  Request::InstantiateModule { module_id, .. }
469
528
  | Request::EvaluateModule { module_id, .. }
@@ -477,8 +536,11 @@ fn request_realm(state: &IsolateState, request: &Request) -> Option<i32> {
477
536
  | Request::CreateContext
478
537
  | Request::DisposeContext { .. }
479
538
  | Request::ModuleStatus { .. }
539
+ | Request::ModuleRequests { .. }
540
+ | Request::ModuleScriptId { .. }
480
541
  | Request::ModuleGraphAsync { .. }
481
542
  | Request::ScriptCodeCache { .. }
543
+ | Request::UnboundScriptId { .. }
482
544
  | Request::ModuleCodeCache { .. }
483
545
  | Request::HeapStatistics
484
546
  | Request::LowMemoryNotification
@@ -553,6 +615,7 @@ fn dispatch_one(
553
615
  cached_data,
554
616
  produce_cache,
555
617
  eager,
618
+ host_defined,
556
619
  } => op_compile_module(
557
620
  scope,
558
621
  context_id,
@@ -561,7 +624,21 @@ fn dispatch_one(
561
624
  cached_data,
562
625
  produce_cache,
563
626
  eager,
627
+ host_defined,
564
628
  ),
629
+ Request::CreateSyntheticModule {
630
+ context_id,
631
+ filename,
632
+ export,
633
+ timeout_ms,
634
+ } => op_create_synthetic_module(scope, context_id, filename, export, timeout_ms, outermost),
635
+ Request::ModuleRequests { module_id } => op_module_requests(scope, module_id),
636
+ Request::ModuleScriptId { module_id } => op_module_script_id(scope, module_id),
637
+ Request::SettleImport {
638
+ import_id,
639
+ settle,
640
+ timeout_ms,
641
+ } => op_settle_import(scope, import_id, settle, timeout_ms, outermost),
565
642
  Request::InstantiateModule { module_id } => op_instantiate_module(scope, module_id),
566
643
  Request::EvaluateModule {
567
644
  module_id,
@@ -578,6 +655,7 @@ fn dispatch_one(
578
655
  cached_data,
579
656
  produce_cache,
580
657
  eager,
658
+ host_defined,
581
659
  } => op_compile_script(
582
660
  scope,
583
661
  context_id,
@@ -586,6 +664,7 @@ fn dispatch_one(
586
664
  cached_data,
587
665
  produce_cache,
588
666
  eager,
667
+ host_defined,
589
668
  ),
590
669
  Request::RunScript {
591
670
  script_id,
@@ -599,6 +678,7 @@ fn dispatch_one(
599
678
  // can't serialize, or when the realm was reset/disposed out from under
600
679
  // the script (its handle is gone): produce nil, not an error.
601
680
  Request::ScriptCodeCache { script_id } => op_script_code_cache(scope, script_id),
681
+ Request::UnboundScriptId { script_id } => op_unbound_script_id(scope, script_id),
602
682
  // Same, for a module: get_unbound_module_script gives the shared
603
683
  // compiled script, which evaluate() fills with inner-function bytecode.
604
684
  // It needs the module's context entered (unlike UnboundScript), so
@@ -811,6 +891,9 @@ fn forget_released_handles(scope: &mut v8::PinScope<'_, '_, ()>) {
811
891
  Released::Script(id) => {
812
892
  state.scripts.by_id.remove(&id);
813
893
  }
894
+ Released::Import(id) => {
895
+ state.imports.by_id.remove(&id);
896
+ }
814
897
  }
815
898
  }
816
899
  }
@@ -883,8 +966,8 @@ fn op_attach(
883
966
  timeout_ms: u64,
884
967
  outermost: bool,
885
968
  ) -> VmReply {
886
- // attach_at_path writes onto globalThis (and walks a dotted
887
- // path), which can fire a user-defined accessor or Proxy trap —
969
+ // attach_at_path writes onto globalThis or the host object (and walks
970
+ // a dotted path), which can fire a user-defined accessor or Proxy trap —
888
971
  // arbitrary JS. So it goes through the same bracket as Eval: a
889
972
  // host fn the trap calls routes back, and a looping trap is
890
973
  // time-capped.
@@ -908,7 +991,7 @@ fn op_attach(
908
991
  // under a namespace object, creating missing
909
992
  // intermediates, so host fns needn't pollute the
910
993
  // bare global.
911
- Some(function) => attach_at_path(scope, context, &name, function),
994
+ Some(function) => attach_at_path(scope, &name, function),
912
995
  None => Err(VmError::Runtime("failed to build function".into())),
913
996
  };
914
997
  auto_drain(scope, outermost);
@@ -955,7 +1038,7 @@ fn op_attach_many(
955
1038
  .data(external.into())
956
1039
  .build(scope)
957
1040
  {
958
- Some(function) => attach_at_path(scope, context, name, function),
1041
+ Some(function) => attach_at_path(scope, name, function),
959
1042
  None => Err(VmError::Runtime(format!(
960
1043
  "failed to build function for `{name}`"
961
1044
  ))),
@@ -1078,6 +1161,7 @@ fn op_compile_module(
1078
1161
  cached_data: Option<Vec<u8>>,
1079
1162
  produce_cache: bool,
1080
1163
  eager: bool,
1164
+ host_defined: Option<JsVal>,
1081
1165
  ) -> VmReply {
1082
1166
  let ctx = context_for(istate!(scope), context_id);
1083
1167
  let outcome = match ctx {
@@ -1089,7 +1173,8 @@ fn op_compile_module(
1089
1173
  match v8::String::new(tc, &source) {
1090
1174
  None => Err(VmError::Runtime("module source too large".into())),
1091
1175
  Some(code) => {
1092
- let origin = module_origin(tc, &filename);
1176
+ let options = host_defined_options(tc, host_defined.as_ref());
1177
+ let origin = module_origin(tc, &filename, options);
1093
1178
  // Consume a supplied bytecode cache (skip reparse),
1094
1179
  // eager-compile every function, or compile fresh
1095
1180
  // (lazy). cached_data wins: V8 forbids combining
@@ -1116,17 +1201,19 @@ fn op_compile_module(
1116
1201
  } else {
1117
1202
  None
1118
1203
  };
1119
- let hash = module.get_identity_hash().get();
1120
- let g = v8::Global::new(tc, module);
1121
- let weak = v8::Weak::new(tc, module);
1122
- let id = {
1123
- let m = &mut istate!(tc).modules;
1124
- let id = m.next_id;
1125
- m.next_id += 1;
1126
- m.by_id.insert(id, (g, context_id));
1127
- m.add_url(hash, weak, filename.clone());
1128
- id
1129
- };
1204
+ let host_defined = host_defined.map(|value| {
1205
+ let value = jsval_to_js(tc, value);
1206
+ v8::Global::new(tc, value)
1207
+ });
1208
+ let id = register_module(
1209
+ tc,
1210
+ module,
1211
+ context_id,
1212
+ ModuleOrigin {
1213
+ url: filename.clone(),
1214
+ host_defined,
1215
+ },
1216
+ );
1130
1217
  Ok(Compiled {
1131
1218
  id,
1132
1219
  cached_data: produced,
@@ -1272,7 +1359,7 @@ fn op_evaluate_module(
1272
1359
  did_eval = true;
1273
1360
  v8::tc_scope!(let tc, scope);
1274
1361
  let reporter =
1275
- report.as_deref().and_then(|name| global_function(tc, name));
1362
+ report.as_deref().and_then(|name| named_function(tc, name));
1276
1363
  match module.evaluate(tc) {
1277
1364
  // A synchronous top-level throw yields a
1278
1365
  // *rejected* promise (not None); a pending
@@ -1339,11 +1426,18 @@ fn op_evaluate_module(
1339
1426
  VmReply::Done(outcome)
1340
1427
  }
1341
1428
 
1342
- // The function the current context's global has under `name`, or None.
1343
- fn global_function<'s>(
1429
+ // The function the current context's global has under `name` (one property,
1430
+ // dots and all, as it always was) — or, for an `@` path, the one the path names
1431
+ // on its host object, as `Context#function` finds it — or None.
1432
+ fn named_function<'s>(
1344
1433
  scope: &mut v8::PinScope<'s, '_>,
1345
1434
  name: &str,
1346
1435
  ) -> Option<v8::Local<'s, v8::Function>> {
1436
+ if name.starts_with('@') {
1437
+ return resolve_function(scope, name)
1438
+ .ok()
1439
+ .map(|(function, _)| function);
1440
+ }
1347
1441
  let key = v8::String::new(scope, name)?;
1348
1442
  let global = scope.get_current_context().global(scope);
1349
1443
  v8::Local::<v8::Function>::try_from(global.get(scope, key.into())?).ok()
@@ -1448,6 +1542,7 @@ fn op_compile_script(
1448
1542
  cached_data: Option<Vec<u8>>,
1449
1543
  produce_cache: bool,
1450
1544
  eager: bool,
1545
+ host_defined: Option<JsVal>,
1451
1546
  ) -> VmReply {
1452
1547
  let ctx = context_for(istate!(scope), context_id);
1453
1548
  let outcome = match ctx {
@@ -1459,7 +1554,8 @@ fn op_compile_script(
1459
1554
  match v8::String::new(tc, &source) {
1460
1555
  None => Err(VmError::Runtime("script source too large".into())),
1461
1556
  Some(code) => {
1462
- let origin = script_origin(tc, &filename);
1557
+ let options = host_defined_options(tc, host_defined.as_ref());
1558
+ let origin = script_origin(tc, &filename, options);
1463
1559
  let (mut src, opts) = compile_source(code, &origin, &cached_data, eager);
1464
1560
  match v8::script_compiler::compile_unbound_script(
1465
1561
  tc,
@@ -1565,6 +1661,24 @@ fn op_script_code_cache(scope: &mut v8::PinScope<'_, '_, ()>, script_id: i32) ->
1565
1661
  VmReply::CodeCache(outcome)
1566
1662
  }
1567
1663
 
1664
+ // rusty_v8 doesn't wrap v8::UnboundScript::GetId, so link V8's exported member
1665
+ // function directly (`this` is the first argument), as stack.rs does its own.
1666
+ unsafe extern "C" {
1667
+ #[link_name = "_ZNK2v813UnboundScript5GetIdEv"]
1668
+ fn v8__UnboundScript__GetId(script: *const v8::UnboundScript) -> i32;
1669
+ }
1670
+
1671
+ // Script#script_id: the script's V8 script id. No context is entered: it reads
1672
+ // the unbound script.
1673
+ fn op_unbound_script_id(scope: &mut v8::PinScope<'_, '_, ()>, script_id: i32) -> VmReply {
1674
+ let Some((g, _)) = script_handle(istate!(scope), script_id) else {
1675
+ return VmReply::Done(Err(VmError::Runtime("unknown script".into())));
1676
+ };
1677
+ let unbound = v8::Local::new(scope, &g);
1678
+ let id = unsafe { v8__UnboundScript__GetId(&*unbound) };
1679
+ VmReply::Done(Ok(JsVal::Int(i64::from(id))))
1680
+ }
1681
+
1568
1682
  fn op_module_code_cache(scope: &mut v8::PinScope<'_, '_, ()>, module_id: i32) -> VmReply {
1569
1683
  let mh = module_handle(istate!(scope), module_id);
1570
1684
  let handle = mh.and_then(|(g, cid)| context_for(istate!(scope), cid).map(|cx| (g, cx)));
@@ -1580,3 +1694,258 @@ fn op_module_code_cache(scope: &mut v8::PinScope<'_, '_, ()>, module_id: i32) ->
1580
1694
  };
1581
1695
  VmReply::CodeCache(outcome)
1582
1696
  }
1697
+
1698
+ // Context#synthetic_module: a module exporting `default` alone (v8::Module::
1699
+ // CreateSyntheticModule — what HTML makes of a JSON, CSS or text module script),
1700
+ // filed as a compiled one is, under |filename|; its value made now — JSON
1701
+ // parsed (a JSON text that is none: a parse error, as a module's SyntaxError
1702
+ // is), a function called — and set as the module evaluates
1703
+ // (synthetic_evaluation_steps).
1704
+ fn op_create_synthetic_module(
1705
+ scope: &mut v8::PinScope<'_, '_, ()>,
1706
+ context_id: i32,
1707
+ filename: String,
1708
+ export: SyntheticExport,
1709
+ timeout_ms: u64,
1710
+ outermost: bool,
1711
+ ) -> VmReply {
1712
+ // (…the id it was filed under, taken back where the op ends terminated after all — a watchdog firing once the
1713
+ // function returned — as no wrapper will ever let go of it: op_resolve_function's `registered`)
1714
+ let mut registered = None;
1715
+ let outcome = run_js_bracketed(
1716
+ scope,
1717
+ outermost,
1718
+ timeout_ms,
1719
+ "synthetic_module",
1720
+ |scope, outermost| {
1721
+ let Some(cx) = context_for(istate!(scope), context_id) else {
1722
+ return (
1723
+ false,
1724
+ Err(VmError::Runtime("context disposed or unknown".into())),
1725
+ );
1726
+ };
1727
+ let context = v8::Local::new(scope, &cx);
1728
+ let scope = &mut v8::ContextScope::new(scope, context);
1729
+ v8::tc_scope!(let tc, scope);
1730
+ let (ran_js, value) = match export {
1731
+ SyntheticExport::Value(value) => (false, Ok(jsval_to_js(tc, value))),
1732
+ SyntheticExport::Json(text) => match v8::String::new(tc, &text) {
1733
+ None => (false, Err(VmError::Runtime("JSON source too large".into()))),
1734
+ Some(text) => match v8::json::parse(tc, text) {
1735
+ Some(value) => (true, Ok(value)),
1736
+ None if tc.has_terminated() => (true, Err(VmError::Terminated)),
1737
+ None => {
1738
+ let exception = tc.exception();
1739
+ (
1740
+ true,
1741
+ Err(parse_error(
1742
+ tc,
1743
+ exception,
1744
+ None,
1745
+ &filename,
1746
+ "JSON parse error",
1747
+ )),
1748
+ )
1749
+ }
1750
+ },
1751
+ },
1752
+ SyntheticExport::Call { path, args } => match resolve_function(tc, &path) {
1753
+ Err(failure) => (true, Err(failure.unwrap_or_else(|| caught!(&mut *tc)))),
1754
+ Ok((function, receiver)) => {
1755
+ let argv: Vec<v8::Local<v8::Value>> =
1756
+ args.into_iter().map(|a| jsval_to_js(tc, a)).collect();
1757
+ match function.call(tc, receiver, &argv) {
1758
+ Some(value) => (true, Ok(value)),
1759
+ None => (true, Err(caught!(&mut *tc))),
1760
+ }
1761
+ }
1762
+ },
1763
+ };
1764
+ let value = match value {
1765
+ Ok(value) => value,
1766
+ Err(e) => return (ran_js, Err(e)),
1767
+ };
1768
+ let (Some(name), Some(default)) = (
1769
+ v8::String::new(tc, &filename),
1770
+ v8::String::new(tc, "default"),
1771
+ ) else {
1772
+ return (
1773
+ ran_js,
1774
+ Err(VmError::Runtime("module name too large".into())),
1775
+ );
1776
+ };
1777
+ let module = v8::Module::create_synthetic_module(
1778
+ tc,
1779
+ name,
1780
+ &[default],
1781
+ synthetic_evaluation_steps,
1782
+ );
1783
+ let hash = module.get_identity_hash().get();
1784
+ let (weak, kept) = (v8::Weak::new(tc, module), v8::Global::new(tc, value));
1785
+ istate!(tc).synthetic.add(hash, weak, kept, context_id);
1786
+ let origin = ModuleOrigin {
1787
+ url: filename.clone(),
1788
+ host_defined: None,
1789
+ };
1790
+ let id = register_module(tc, module, context_id, origin);
1791
+ registered = Some(id);
1792
+ // (…what the function queued drained, as any op's at the outermost)
1793
+ auto_drain(tc, outermost);
1794
+ (ran_js, Ok(JsVal::Int(id as i64)))
1795
+ },
1796
+ );
1797
+ if let (Err(_), Some(id)) = (&outcome, registered) {
1798
+ istate!(scope).modules.by_id.remove(&id);
1799
+ }
1800
+ VmReply::Done(outcome)
1801
+ }
1802
+
1803
+ // Module#script_id: the module's script id, nil where it has none (a synthetic
1804
+ // or an errored module). No context is entered: it reads the module's script.
1805
+ fn op_module_script_id(scope: &mut v8::PinScope<'_, '_, ()>, module_id: i32) -> VmReply {
1806
+ let Some((g, _)) = module_handle(istate!(scope), module_id) else {
1807
+ return VmReply::Done(Err(VmError::Runtime("unknown module".into())));
1808
+ };
1809
+ let module = v8::Local::new(scope, &g);
1810
+ VmReply::Done(Ok(module
1811
+ .script_id()
1812
+ .map_or(JsVal::Null, |id| JsVal::Int(i64::from(id)))))
1813
+ }
1814
+
1815
+ // Module#requests: [[specifier, {key => value, …}], …] — V8's module requests,
1816
+ // each with its import attributes, as the source has them.
1817
+ fn op_module_requests(scope: &mut v8::PinScope<'_, '_, ()>, module_id: i32) -> VmReply {
1818
+ let Some((g, cid)) = module_handle(istate!(scope), module_id) else {
1819
+ return VmReply::Done(Err(VmError::Runtime("unknown module".into())));
1820
+ };
1821
+ let Some(cx) = context_for(istate!(scope), cid) else {
1822
+ return VmReply::Done(Err(VmError::Runtime("module's context is gone".into())));
1823
+ };
1824
+ let context = v8::Local::new(scope, &cx);
1825
+ let scope = &mut v8::ContextScope::new(scope, context);
1826
+ let module = v8::Local::new(scope, &g);
1827
+ let requests = module.get_module_requests();
1828
+ let mut ids = 0u32..;
1829
+ let mut items = Vec::with_capacity(requests.length());
1830
+ for i in 0..requests.length() {
1831
+ let Some(request) = requests
1832
+ .get(scope, i)
1833
+ .and_then(|r| v8::Local::<v8::ModuleRequest>::try_from(r).ok())
1834
+ else {
1835
+ continue;
1836
+ };
1837
+ let specifier = request.get_specifier().to_rust_string_lossy(scope);
1838
+ let attributes = request.get_import_attributes();
1839
+ let entries = import_attributes_of(scope, attributes, 3)
1840
+ .into_iter()
1841
+ .map(|(key, value)| (key, JsVal::Str(value)))
1842
+ .collect();
1843
+ let attributes = JsVal::Obj {
1844
+ id: ids.next().unwrap_or(0),
1845
+ entries,
1846
+ };
1847
+ items.push(JsVal::Array {
1848
+ id: ids.next().unwrap_or(0),
1849
+ items: vec![JsVal::Str(specifier), attributes],
1850
+ });
1851
+ }
1852
+ VmReply::Done(Ok(JsVal::Array {
1853
+ id: ids.next().unwrap_or(0),
1854
+ items,
1855
+ }))
1856
+ }
1857
+
1858
+ // DynamicImport#resolve / #reject / #reject_with: the waiting import() |import_id|
1859
+ // settled — replying whether it was still waiting (its realm reset or disposed,
1860
+ // or settled already: it is not). Linking a module runs the resolve block
1861
+ // DynamicImport#resolve parked; evaluating it, and a reject_with function, run
1862
+ // JS, so this is bracketed as an evaluation is.
1863
+ fn op_settle_import(
1864
+ scope: &mut v8::PinScope<'_, '_, ()>,
1865
+ import_id: u64,
1866
+ settle: Settle,
1867
+ timeout_ms: u64,
1868
+ outermost: bool,
1869
+ ) -> VmReply {
1870
+ let outcome = run_js_bracketed(
1871
+ scope,
1872
+ outermost,
1873
+ timeout_ms,
1874
+ "import",
1875
+ |scope, outermost| {
1876
+ let Some((resolver, cid)) = istate!(scope).imports.by_id.remove(&import_id) else {
1877
+ return (false, Ok(JsVal::Bool(false)));
1878
+ };
1879
+ let Some(cx) = context_for(istate!(scope), cid) else {
1880
+ return (false, Ok(JsVal::Bool(false)));
1881
+ };
1882
+ let context = v8::Local::new(scope, &cx);
1883
+ let scope = &mut v8::ContextScope::new(scope, context);
1884
+ let resolver = v8::Local::new(scope, resolver);
1885
+ match settle {
1886
+ Settle::Module(module_id) => match module_handle(istate!(scope), module_id) {
1887
+ Some((g, mcid)) if mcid == cid => {
1888
+ let module = v8::Local::new(scope, &g);
1889
+ let settling = std::mem::replace(&mut istate!(scope).settling_import, true);
1890
+ finish_dynamic_import(scope, module, resolver);
1891
+ istate!(scope).settling_import = settling;
1892
+ }
1893
+ Some(_) => reject_with_error(
1894
+ scope,
1895
+ resolver,
1896
+ "import() settled with a module of another realm",
1897
+ ),
1898
+ None => reject_with_error(
1899
+ scope,
1900
+ resolver,
1901
+ "import() settled with an unknown module",
1902
+ ),
1903
+ },
1904
+ Settle::Reject(value) => {
1905
+ let value = jsval_to_js(scope, value);
1906
+ resolver.reject(scope, value);
1907
+ }
1908
+ Settle::RejectWith { path, args } => {
1909
+ v8::tc_scope!(let tc, scope);
1910
+ let made = match resolve_function(tc, &path) {
1911
+ Ok((function, receiver)) => {
1912
+ let argv: Vec<v8::Local<v8::Value>> =
1913
+ args.into_iter().map(|a| jsval_to_js(tc, a)).collect();
1914
+ function.call(tc, receiver, &argv)
1915
+ }
1916
+ Err(Some(e)) => {
1917
+ let message = match e {
1918
+ VmError::Runtime(m) => m,
1919
+ _ => format!("`{path}` could not be called"),
1920
+ };
1921
+ if let Some(message) = v8::String::new(tc, &message) {
1922
+ let error = v8::Exception::error(tc, message);
1923
+ tc.throw_exception(error);
1924
+ }
1925
+ None
1926
+ }
1927
+ Err(None) => None,
1928
+ };
1929
+ // (…what the function throws rejects it as well, its value)
1930
+ match made {
1931
+ Some(value) => {
1932
+ resolver.reject(tc, value);
1933
+ }
1934
+ None if tc.has_terminated() => {}
1935
+ None => {
1936
+ if let Some(exception) = tc.exception() {
1937
+ resolver.reject(tc, exception);
1938
+ }
1939
+ }
1940
+ }
1941
+ }
1942
+ }
1943
+ if scope.is_execution_terminating() {
1944
+ return (true, Err(VmError::Terminated));
1945
+ }
1946
+ auto_drain(scope, outermost);
1947
+ (true, Ok(JsVal::Bool(true)))
1948
+ },
1949
+ );
1950
+ VmReply::Done(outcome)
1951
+ }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RustyRacer
4
- VERSION = "0.2.7"
4
+ VERSION = "0.2.9"
5
5
  end