From 637f47d2599f9905dbb31c5176865e47203e7a39 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 9 Jan 2021 15:20:07 +0800 Subject: [PATCH] Split AST::set_source into set_source/clear_source. --- RELEASES.md | 1 + src/ast.rs | 16 ++++++++++++---- src/bin/rhai-repl.rs | 2 +- src/module/resolvers/file.rs | 2 +- tests/modules.rs | 24 +++++++++++++++--------- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index debb5010..3975648a 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -10,6 +10,7 @@ Breaking changes * The error variant `EvalAltResult::ErrorInFunctionCall` has a new parameter holding the _source_ of the function. * `ParseErrorType::WrongFnDefinition` is renamed `FnWrongDefinition`. * Redefining an existing function within the same script now throws a new `ParseErrorType::FnDuplicatedDefinition`. This is to prevent accidental overwriting an earlier function definition. +* `AST::set_source` is now split into `AST::set_source` and `AST::clear_source`. New features ------------ diff --git a/src/ast.rs b/src/ast.rs index 72eedf1e..6a5f478e 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -218,24 +218,32 @@ impl AST { resolver: None, } } - /// Get the source. + /// Get the source, if any. #[inline(always)] pub fn source(&self) -> Option<&str> { self.source.as_ref().map(|s| s.as_str()) } - /// Clone the source. + /// Clone the source, if any. #[inline(always)] pub(crate) fn clone_source(&self) -> Option { self.source.clone() } /// Set the source. #[inline(always)] - pub fn set_source>(&mut self, source: Option) { - self.source = source.map(|s| s.into()); + pub fn set_source(&mut self, source: impl Into) -> &mut Self { + self.source = Some(source.into()); if let Some(module) = Shared::get_mut(&mut self.functions) { module.set_id(self.source.clone()); } + + self + } + /// Clear the source. + #[inline(always)] + pub fn clear_source(&mut self) -> &mut Self { + self.source = None; + self } /// Get the statements. #[cfg(not(feature = "internals"))] diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index 67f39957..0f911027 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -92,7 +92,7 @@ fn main() { .compile(&contents) .map_err(|err| err.into()) .and_then(|mut ast| { - ast.set_source(Some(&filename)); + ast.set_source(&filename); Module::eval_ast_as_new(Default::default(), &ast, &engine) }) { Err(err) => { diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index af4cc98e..c3fc4a5a 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -212,7 +212,7 @@ impl ModuleResolver for FileModuleResolver { _ => Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos)), })?; - ast.set_source(Some(path)); + ast.set_source(path); // Make a module from the AST let m: Shared = Module::eval_ast_as_new(scope, &ast, engine) diff --git a/tests/modules.rs b/tests/modules.rs index 96733e3f..4f6baa55 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -247,19 +247,25 @@ fn test_module_resolver() -> Result<(), Box> { )?; } - let script = r#" - import "hello" as h; - h::answer - "#; - let mut scope = Scope::new(); + #[cfg(not(feature = "no_function"))] + { + let script = r#" + fn foo() { + import "hello" as h; + h::answer + } + foo() + { import "hello" as h; h::answer } + "#; + let mut scope = Scope::new(); - let ast = engine.compile_into_self_contained(&mut scope, script)?; + let ast = engine.compile_into_self_contained(&mut scope, script)?; - engine.set_module_resolver(DummyModuleResolver::new()); + engine.set_module_resolver(DummyModuleResolver::new()); - assert_eq!(engine.eval_ast::(&ast)?, 42); + assert_eq!(engine.eval_ast::(&ast)?, 84); - assert!(engine.eval::(script).is_err()); + assert!(engine.eval::(script).is_err()); + } Ok(()) }