From f5f4da31e5b6db352d34275054569bffe1ca9ae4 Mon Sep 17 00:00:00 2001 From: Romain Paquet Date: Wed, 5 Nov 2025 20:23:17 +0100 Subject: [PATCH] refactor(jit): use source cache instead of string --- src/jit/mod.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/jit/mod.rs b/src/jit/mod.rs index bf86ccc..feb05f1 100644 --- a/src/jit/mod.rs +++ b/src/jit/mod.rs @@ -73,11 +73,14 @@ impl JIT { /// Compile source code into machine code. pub fn compile( &mut self, - input: &str, namespace: ModulePath, id: SourceId, + mut source_cache: &mut SourceCache, ) -> 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 let mut ast = self @@ -85,13 +88,12 @@ impl JIT { .parse_as_module(input, namespace, id) .map_err(|x| format!("Parsing error: {x}"))?; - ast.type_check() - .map_err(|errors| { - errors - .iter() - .for_each(|e| e.to_report(&ast).eprint(&mut source_cache).unwrap()); - }) - .unwrap(); + ast.type_check().map_err(|errors| { + errors + .iter() + .for_each(|e| e.to_report(&ast).eprint(&mut source_cache).unwrap()); + "Typing errors, cannot compile" + })?; // Translate the AST into Cranelift IR self.translate(&ast)?; @@ -117,12 +119,9 @@ impl JIT { source_cache: &mut SourceCache, ) -> Result<*const u8, String> { self.compile( - source_cache - .fetch(&id) - .map(|s| s.text()) - .map_err(|e| format!("{:?}", e))?, AsRef::::as_ref(path).into(), id, + source_cache, ) }