mini_racer_rs 1.0.5 → 1.1.0

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.
@@ -6,6 +6,7 @@ use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
6
6
  use std::sync::{Arc, Mutex};
7
7
  use std::time::Duration;
8
8
 
9
+ use mini_racer_sourcemap::{Frame, Match, Placement, SourceMap};
9
10
  use mini_racer_sync::{
10
11
  DispatchId, DispatchKind, Halt, Interrupt, Phase, Side, TerminationCause, ToV8, lock,
11
12
  };
@@ -71,8 +72,25 @@ struct Runtime {
71
72
  hoisting: Mutex<Hoisting>,
72
73
  // The values of each fast call running, innermost last, for `hoisted`.
73
74
  frames: Mutex<Vec<v8::Global<v8::Array>>>,
75
+ // Scripts that hold a bundle with a source map, latest last.
76
+ matched: Mutex<Vec<Matched>>,
74
77
  }
75
78
 
79
+ #[derive(Clone)]
80
+ struct Matched {
81
+ script_id: i32,
82
+ // As V8 prints the script in a frame: its source URL or its file name.
83
+ name: Arc<str>,
84
+ map: Arc<SourceMap>,
85
+ placement: Placement,
86
+ }
87
+
88
+ // A context evaluates its bundle once or a few times, each a new script.
89
+ const MATCHED: usize = 16;
90
+ // V8's default `Error.stackTraceLimit`, so the detailed trace holds every
91
+ // frame of a default stack.
92
+ const DETAILED_FRAMES: i32 = 10;
93
+
76
94
  struct Uncached {
77
95
  script: v8::Global<v8::UnboundScript>,
78
96
  building: Building,
@@ -149,6 +167,7 @@ impl Runtime {
149
167
  uncached: Mutex::default(),
150
168
  hoisting: Mutex::default(),
151
169
  frames: Mutex::default(),
170
+ matched: Mutex::default(),
152
171
  }
153
172
  }
154
173
  }
@@ -305,6 +324,10 @@ fn adopt(
305
324
  (inherited.context.clone(), inherited.filter.clone()),
306
325
  (key, inherited.snapshot),
307
326
  ));
327
+ // A child only try-locks what it inherited.
328
+ if let Ok(matched) = inherited.matched.try_lock() {
329
+ lock(&runtime.matched).clone_from(&matched);
330
+ }
308
331
  locker.set_slot(Arc::clone(&runtime));
309
332
  runtime
310
333
  };
@@ -784,6 +807,7 @@ fn evaluate(
784
807
  Plan::Compile(None)
785
808
  };
786
809
  let too_large = || EngineError::Internal("source too large".to_owned());
810
+ let text = source;
787
811
  let source = shared_source(tc, runtime, plan.key(), source)
788
812
  .or_else(|| string(tc, source))
789
813
  .ok_or_else(too_large)?;
@@ -791,6 +815,7 @@ fn evaluate(
791
815
  let origin = serialize::origin(tc, name.into());
792
816
  let (script, make) = compile(tc, runtime, source, &origin, plan);
793
817
  let script = script.ok_or_else(|| failure(tc, JsErrorKind::Parse))?;
818
+ match_bundle(tc, runtime, text, name, script);
794
819
  let value = script
795
820
  .run(tc)
796
821
  .ok_or_else(|| failure(tc, JsErrorKind::Runtime))?;
@@ -802,6 +827,90 @@ fn evaluate(
802
827
  Ok(reply)
803
828
  }
804
829
 
830
+ // A script holding a bundle with a source map is recorded before it runs,
831
+ // and from then on its isolate keeps V8's detailed trace with each error,
832
+ // which only the rewrite of a runtime error's message reads.
833
+ fn match_bundle(
834
+ tc: &mut Tc<'_, '_, '_, '_>,
835
+ runtime: &Runtime,
836
+ source: &JsText,
837
+ filename: v8::Local<'_, v8::String>,
838
+ script: v8::Local<'_, v8::Script>,
839
+ ) {
840
+ let JsText::Utf8(bytes) = source else {
841
+ return;
842
+ };
843
+ let Some((map, placement)) = runtime.generation.source_maps().find(bytes) else {
844
+ return;
845
+ };
846
+ let url = script.get_unbound_script(tc).get_source_url(tc);
847
+ let printed = v8::Local::<v8::String>::try_from(url)
848
+ .ok()
849
+ .filter(|url| url.length() > 0)
850
+ .unwrap_or(filename);
851
+ let name: Arc<str> = match printed.to_rust_string_lossy(tc) {
852
+ name if name.is_empty() => Arc::from("<anonymous>"),
853
+ name => Arc::from(name),
854
+ };
855
+ tc.set_capture_stack_trace_for_uncaught_exceptions(true, DETAILED_FRAMES);
856
+ let script_id = script.script_id();
857
+ let mut matched = lock(&runtime.matched);
858
+ // V8's compilation cache hands a source evaluated again its earlier
859
+ // script, id and all, whose entry gives way to this one and its map.
860
+ matched.retain(|kept| kept.script_id != script_id);
861
+ if matched.len() >= MATCHED {
862
+ matched.remove(0);
863
+ }
864
+ matched.push(Matched {
865
+ script_id,
866
+ name,
867
+ map,
868
+ placement,
869
+ });
870
+ }
871
+
872
+ // The locations of a runtime error's frames in a bundle with a source map,
873
+ // as its map gives them, paired with V8's detailed trace for the exception.
874
+ fn relocated(tc: &Tc<'_, '_, '_, '_>, message: String) -> String {
875
+ let Some(runtime) = tc.get_slot::<Arc<Runtime>>().cloned() else {
876
+ return message;
877
+ };
878
+ let scripts = lock(&runtime.matched).clone();
879
+ if scripts.is_empty() {
880
+ return message;
881
+ }
882
+ let trace = tc
883
+ .exception()
884
+ .and_then(|exception| v8::Exception::get_stack_trace(tc, exception))
885
+ .or_else(|| tc.message().and_then(|message| message.get_stack_trace(tc)));
886
+ let Some(trace) = trace else {
887
+ return message;
888
+ };
889
+ let frames: Vec<Frame> = (0..trace.get_frame_count())
890
+ .filter_map(|index| trace.get_frame(tc, index))
891
+ .filter_map(|frame| {
892
+ Some(Frame {
893
+ script_id: i32::try_from(frame.get_script_id()).ok()?,
894
+ line: u32::try_from(frame.get_line_number()).ok()?,
895
+ column: u32::try_from(frame.get_column()).ok()?,
896
+ })
897
+ })
898
+ .collect();
899
+ let matches: Vec<Match<'_>> = scripts
900
+ .iter()
901
+ .map(|script| Match {
902
+ script_id: script.script_id,
903
+ name: &script.name,
904
+ map: &script.map,
905
+ placement: script.placement,
906
+ })
907
+ .collect();
908
+ match mini_racer_sourcemap::rewrite(&message, &frames, &matches) {
909
+ Cow::Borrowed(_) => message,
910
+ Cow::Owned(text) => text,
911
+ }
912
+ }
913
+
805
914
  // `max_memory:` measures the heap, where mini_racer keeps the source, so a
806
915
  // context with it keeps its own copy.
807
916
  fn shares_sources(runtime: &Runtime) -> bool {
@@ -1588,6 +1697,11 @@ fn described(tc: &Tc<'_, '_, '_, '_>, kind: JsErrorKind) -> EngineError {
1588
1697
  |text| text.to_rust_string_lossy(tc),
1589
1698
  )
1590
1699
  });
1700
+ let message = if kind == JsErrorKind::Runtime {
1701
+ relocated(tc, message)
1702
+ } else {
1703
+ message
1704
+ };
1591
1705
  EngineError::from(JsError { kind, message })
1592
1706
  }
1593
1707
 
@@ -0,0 +1,18 @@
1
+ [package]
2
+ description = "Source maps for mini_racer_rs stack traces: parsing a bundle's map and rewriting V8's frames"
3
+ edition.workspace = true
4
+ license.workspace = true
5
+ name = "mini_racer_sourcemap"
6
+ publish.workspace = true
7
+ repository.workspace = true
8
+ rust-version.workspace = true
9
+ version.workspace = true
10
+
11
+ [dependencies]
12
+ memchr.workspace = true
13
+ serde.workspace = true
14
+ serde_json.workspace = true
15
+ thiserror.workspace = true
16
+
17
+ [lints]
18
+ workspace = true
@@ -0,0 +1,13 @@
1
+ #![forbid(unsafe_code)]
2
+ #![deny(
3
+ clippy::arithmetic_side_effects,
4
+ clippy::cast_possible_truncation,
5
+ clippy::indexing_slicing
6
+ )]
7
+
8
+ mod map;
9
+ mod rewrite;
10
+ mod vlq;
11
+
12
+ pub use map::{MapError, Original, Placement, SourceMap, check_length};
13
+ pub use rewrite::{Frame, Match, rewrite};