Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion implants/lib/eldritch/src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ fn methods(builder: &mut MethodsBuilder) {
Ok(NoneType{})
}


#[allow(unused_variables)]
fn process_list(this: &ReportLibrary, starlark_eval: &mut Evaluator<'v, '_>, process_list: UnpackList<SmallMap<String, Value>>) -> anyhow::Result<NoneType> {
let env = crate::runtime::Environment::from_extra(starlark_eval.extra)?;
Expand Down
65 changes: 65 additions & 0 deletions implants/lib/eldritch/src/runtime/eprint_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::runtime::{messages::ReportErrorMessage, Environment};
use anyhow::Result;

pub fn eprint(env: &Environment, message: String) -> Result<()> {
env.send(ReportErrorMessage {
id: env.id(),
error: message.clone(),
})?;

#[cfg(feature = "print_stdout")]
eprintln!("{}", message);

Ok(())
}

#[cfg(test)]
mod test {
use crate::runtime::Message;
use pb::eldritch::Tome;
use std::collections::HashMap;

macro_rules! test_cases {
($($name:ident: $value:expr,)*) => {
$(
#[tokio::test]
async fn $name() {
let tc: TestCase = $value;

// Run Eldritch (until finished)
let mut runtime = crate::start(tc.id, tc.tome).await;
runtime.finish().await;

// Read Messages
let mut found = false;
for msg in runtime.messages() {
if let Message::ReportError(m) = msg {
assert_eq!(tc.id, m.id);
assert_eq!(tc.want_error, m.error);
found = true;
}
}
assert!(found);
}
)*
}
}

struct TestCase {
pub id: i64,
pub tome: Tome,
pub want_error: String,
}

test_cases! {
one_error: TestCase{
id: 123,
tome: Tome{
eldritch: String::from(r#"eprint(message="Beep Boop an error occured")"#),
parameters: HashMap::new(),
file_names: Vec::new(),
},
want_error: String::from("Beep Boop an error occured"),
},
}
}
15 changes: 13 additions & 2 deletions implants/lib/eldritch/src/runtime/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
regex::RegexLibrary,
report::ReportLibrary,
runtime::{
eprint_impl,
messages::{reduce, Message, ReportErrorMessage, ReportFinishMessage, ReportStartMessage},
Environment,
},
Expand All @@ -24,8 +25,7 @@ use starlark::{
eval::Evaluator,
starlark_module,
syntax::{AstModule, Dialect},
values::dict::Dict,
values::AllocValue,
values::{dict::Dict, none::NoneType, AllocValue},
};
use std::sync::mpsc::{channel, Receiver};
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -142,6 +142,16 @@ pub struct Runtime {
rx: Receiver<Message>,
}

#[starlark_module]
fn error_handler(builder: &mut GlobalsBuilder) {
#[allow(unused_variables)]
fn eprint(starlark_eval: &mut Evaluator<'v, '_>, message: String) -> anyhow::Result<NoneType> {
let env = crate::runtime::Environment::from_extra(starlark_eval.extra)?;
eprint_impl::eprint(env, message)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love that we pass env here, greatly demonstrates the separation between eprint (starlark) and eprint_impl (native rust)

Ok(NoneType {})
}
}

impl Runtime {
/*
* Globals available to eldritch code.
Expand Down Expand Up @@ -177,6 +187,7 @@ impl Runtime {
LibraryExtension::Typing,
])
.with(eldritch)
.with(error_handler)
.build()
}

Expand Down
1 change: 1 addition & 0 deletions implants/lib/eldritch/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod drain;
mod environment;
mod eprint_impl;
mod eval;
pub mod messages;

Expand Down