refactor(jit): use source cache instead of string

This commit is contained in:
Romain Paquet 2025-11-05 20:23:17 +01:00
parent efb8607569
commit f5f4da31e5

View file

@ -73,11 +73,14 @@ impl JIT {
/// Compile source code into machine code. /// Compile source code into machine code.
pub fn compile( pub fn compile(
&mut self, &mut self,
input: &str,
namespace: ModulePath, namespace: ModulePath,
id: SourceId, id: SourceId,
mut source_cache: &mut SourceCache,
) -> Result<*const u8, String> { ) -> Result<*const u8, String> {
let mut source_cache = (0u32, ariadne::Source::from(input)); let input = source_cache
.fetch(&id)
.map(|s| s.text())
.map_err(|e| format!("{e:?}"))?;
// Parse the source code into an AST // Parse the source code into an AST
let mut ast = self let mut ast = self
@ -85,13 +88,12 @@ impl JIT {
.parse_as_module(input, namespace, id) .parse_as_module(input, namespace, id)
.map_err(|x| format!("Parsing error: {x}"))?; .map_err(|x| format!("Parsing error: {x}"))?;
ast.type_check() ast.type_check().map_err(|errors| {
.map_err(|errors| {
errors errors
.iter() .iter()
.for_each(|e| e.to_report(&ast).eprint(&mut source_cache).unwrap()); .for_each(|e| e.to_report(&ast).eprint(&mut source_cache).unwrap());
}) "Typing errors, cannot compile"
.unwrap(); })?;
// Translate the AST into Cranelift IR // Translate the AST into Cranelift IR
self.translate(&ast)?; self.translate(&ast)?;
@ -117,12 +119,9 @@ impl JIT {
source_cache: &mut SourceCache, source_cache: &mut SourceCache,
) -> Result<*const u8, String> { ) -> Result<*const u8, String> {
self.compile( self.compile(
source_cache
.fetch(&id)
.map(|s| s.text())
.map_err(|e| format!("{:?}", e))?,
AsRef::<std::path::Path>::as_ref(path).into(), AsRef::<std::path::Path>::as_ref(path).into(),
id, id,
source_cache,
) )
} }