Merge pull request #7144 from dotCooCoo/fix/script-line-numbers

Scripts: $.line and $.stack give the line of the file the code is in
This commit is contained in:
BFlat
2026-11-14 00:51:19 +07:00
committed by GitHub
11 changed files with 182 additions and 8 deletions
+35
View File
@@ -823,6 +823,34 @@ impl Host {
}
}
/// The file and line that line `line` of the source with tag `tag` came from, as `$.line` and
/// `$.stack` give them: the run's script, a file it included or a file `$.evalFile` gave, in
/// this run or in an earlier run of its named engine, with the run's name standing for a script
/// given as text. Code that `eval` or `Function` compiled has no map and keeps its own line,
/// named `[eval]`; code whose map is gone, such as one the named engine let go to stay within
/// [`KEPT_MAP_BYTES`], is at line 0 of `[unknown]`.
fn source_place(&self, tag: u32, line: u32) -> (String, u32) {
if tag == vectorcraft_js::UNKNOWN_SOURCE {
return ("[eval]".to_string(), line);
}
let named = |map: &include::SourceMap, script: Option<&Arc<str>>| {
let (index, file, line, _) = map.locate(line, 1);
match script {
Some(name) if index == 0 && file.is_empty() => (name.to_string(), line),
_ => (file.to_string(), line),
}
};
let script = self.script_tag.get();
let placed = match tag.checked_sub(script).filter(|_| script != vectorcraft_js::UNKNOWN_SOURCE) {
Some(0) => self.script_map.try_borrow().ok().map(|m| named(&m, Some(&self.report_name))),
Some(n) => self.eval_maps.try_borrow().ok().and_then(|maps| maps.get(n as usize - 1).map(|m| named(m, None))),
None => {
self.kept_maps.as_ref().and_then(|k| k.try_borrow().ok()).and_then(|k| k.get(tag).map(|kept| named(&kept.map, kept.script.as_ref())))
}
};
placed.unwrap_or_else(|| ("[unknown]".to_string(), 0))
}
/// `$.fileName` read by the code at `site`: the file that code is in, by the source map of the
/// source it came from (this run's script, a file `$.evalFile` gave, or code an earlier run of
/// the named engine compiled), so a function keeps the file it was defined in. Code from a file
@@ -1891,6 +1919,13 @@ impl Session {
Ok(expanded) => {
*host.script_map.borrow_mut() = expanded.map;
realm.set_strict_writes(expanded.strict == Some(true));
if fresh {
let l = link.clone();
realm.set_source_locator(Rc::new(move |tag, line| match current(&l) {
Ok(h) => h.source_place(tag, line),
Err(_) => ("[unknown]".to_string(), 0),
}));
}
(if fresh { realm.add_script_extensions() } else { Ok(()) })
.and_then(|()| if fresh { realm.define_host_object("vc", vc_object(&link)) } else { Ok(()) })
.and_then(|()| if fresh { realm.define_host_object("$", dollar_object(&link, info, &localization)) } else { Ok(()) })