From 8e327ff9de02bf309c01ae59357c3114a5b1052d Mon Sep 17 00:00:00 2001 From: maxmindlin <35264981+maxmindlin@users.noreply.github.com> Date: Mon, 19 Aug 2024 19:46:07 -0400 Subject: [PATCH] fix block return bug --- scout-interpreter/Cargo.toml | 2 +- scout-interpreter/src/env.rs | 2 +- scout-interpreter/src/eval.rs | 24 +++++++++++++++--------- scout-interpreter/src/object.rs | 12 ++++++++++++ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/scout-interpreter/Cargo.toml b/scout-interpreter/Cargo.toml index f673fc0..4a822a7 100644 --- a/scout-interpreter/Cargo.toml +++ b/scout-interpreter/Cargo.toml @@ -12,7 +12,7 @@ keywords = [ "web-scraping", "crawling", "scraping", -] +] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/scout-interpreter/src/env.rs b/scout-interpreter/src/env.rs index 087f6a0..eddd2f8 100644 --- a/scout-interpreter/src/env.rs +++ b/scout-interpreter/src/env.rs @@ -11,7 +11,7 @@ pub type EnvPointer = Arc>; #[derive(Debug, Default)] pub struct Env { - store: HashMap>, + pub store: HashMap>, outer: Mutex>>, } diff --git a/scout-interpreter/src/eval.rs b/scout-interpreter/src/eval.rs index 46bf08a..552d026 100644 --- a/scout-interpreter/src/eval.rs +++ b/scout-interpreter/src/eval.rs @@ -35,7 +35,7 @@ macro_rules! check_return_eval { if let $crate::object::Object::Return(ret) = &*eval_block($block, $crawler, $env, $results).await? { - return Ok(ret.clone()); + return Ok(Arc::new($crate::object::Object::Return(ret.clone()))); } }; } @@ -156,7 +156,7 @@ fn eval_statement<'a>( .await .as_deref() { - Ok(Object::Return(ret)) => return Ok(ret.clone()), + Ok(Object::Return(ret)) => return Ok(Arc::new(Object::Return(ret.clone()))), // if it was successful but not a return, do nothing Ok(_) => {} Err(_) if catch_block.is_some() => { @@ -544,15 +544,21 @@ async fn eval_block( StmtKind::Return(rv) => { return match rv { None => Ok(Arc::new(Object::Return(Arc::new(Object::Null)))), - Some(expr) => Ok(Arc::new(Object::Return( - eval_expression(expr, crawler, env.clone(), results.clone()).await?, - ))), - } + Some(expr) => { + let r = + eval_expression(expr, crawler, env.clone(), results.clone()).await?; + Ok(Arc::new(Object::Return(r))) + } + }; } _ => { - res = eval_statement(stmt, crawler, env.clone(), results.clone()) - .await? - .clone() + let temp = eval_statement(stmt, crawler, env.clone(), results.clone()).await?; + match &*temp { + Object::Return(_) => return Ok(temp.clone()), + _ => { + res = temp.clone(); + } + } } } } diff --git a/scout-interpreter/src/object.rs b/scout-interpreter/src/object.rs index 05f4ed9..d8b48f2 100644 --- a/scout-interpreter/src/object.rs +++ b/scout-interpreter/src/object.rs @@ -32,6 +32,7 @@ impl Object { Boolean(_) => "bool", Number(_) => "number", Fn(_, _) => "fn", + Module(_) => "module", _ => "object", } } @@ -52,6 +53,17 @@ impl Object { Some(new_vec) } + Module(ev) => { + let inner = ev.lock().await; + let mut iterable = Vec::new(); + for (id, obj) in &inner.store { + if obj.type_str() == "module" { + let item = vec![Arc::new(Str(id.clone())), obj.clone()]; + iterable.push(Arc::new(Object::List(Mutex::new(item)))) + } + } + Some(iterable) + } _ => None, } }