mirror of https://github.com/boa-dev/boa.git
Browse Source
Slightly related to #2411 since we need an API to pass module files, but more useful for #1760, #1313 and other error reporting issues. It changes the following: - Introduces a new `Source` API to store the path of a provided file or `None` if the source is a plain string. - Improves the display of `boa_tester` to show the path of the tests being run. This also enables hyperlinks to directly jump to the tested file from the VS terminal. - Adjusts the repo to this change. Hopefully, this will improve our error display in the future.pull/2580/head
José Julián Espina
2 years ago
34 changed files with 458 additions and 265 deletions
@ -0,0 +1,88 @@
|
||||
use std::{ |
||||
fs::File, |
||||
io::{self, BufReader, Read}, |
||||
path::Path, |
||||
}; |
||||
|
||||
/// A source of ECMAScript code.
|
||||
///
|
||||
/// [`Source`]s can be created from plain [`str`]s, file [`Path`]s or more generally, any [`Read`]
|
||||
/// instance.
|
||||
#[derive(Debug)] |
||||
pub struct Source<'path, R> { |
||||
pub(crate) reader: R, |
||||
pub(crate) path: Option<&'path Path>, |
||||
} |
||||
|
||||
impl<'bytes> Source<'static, &'bytes [u8]> { |
||||
/// Creates a new `Source` from any type equivalent to a slice of bytes e.g. [`&str`][str],
|
||||
/// <code>[Vec]<[u8]></code>, <code>[Box]<[\[u8\]][slice]></code> or a plain slice
|
||||
/// <code>[&\[u8\]][slice]</code>.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use boa_parser::Source;
|
||||
/// let code = r#"var array = [5, 4, 3, 2, 1];"#;
|
||||
/// let source = Source::from_bytes(code);
|
||||
/// ```
|
||||
///
|
||||
/// [slice]: std::slice
|
||||
pub fn from_bytes<T: AsRef<[u8]> + ?Sized>(source: &'bytes T) -> Self { |
||||
Self { |
||||
reader: source.as_ref(), |
||||
path: None, |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl<'path> Source<'path, BufReader<File>> { |
||||
/// Creates a new `Source` from a `Path` to a file.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// See [`File::open`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use boa_parser::Source;
|
||||
/// # use std::{fs::File, path::Path};
|
||||
/// # fn main() -> std::io::Result<()> {
|
||||
/// let path = Path::new("script.js");
|
||||
/// let source = Source::from_filepath(path)?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn from_filepath(source: &'path Path) -> io::Result<Self> { |
||||
let reader = File::open(source)?; |
||||
Ok(Self { |
||||
reader: BufReader::new(reader), |
||||
path: Some(source), |
||||
}) |
||||
} |
||||
} |
||||
|
||||
impl<'path, R: Read> Source<'path, R> { |
||||
/// Creates a new `Source` from a [`Read`] instance and an optional [`Path`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use boa_parser::Source;
|
||||
/// # use std::{fs::File, io::Read, path::Path};
|
||||
/// # fn main() -> std::io::Result<()> {
|
||||
/// let strictler = r#""use strict""#;
|
||||
///
|
||||
/// let path = Path::new("no_strict.js");
|
||||
/// let file = File::open(path)?;
|
||||
/// let strict = strictler.as_bytes().chain(file);
|
||||
///
|
||||
/// let source = Source::from_reader(strict, Some(path));
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub const fn from_reader(reader: R, path: Option<&'path Path>) -> Self { |
||||
Self { reader, path } |
||||
} |
||||
} |
Loading…
Reference in new issue