Skip to content

Commit

Permalink
Switch to RObject ref
Browse files Browse the repository at this point in the history
  • Loading branch information
dfalbel committed Nov 27, 2024
1 parent 9aa5d05 commit 94eba52
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
12 changes: 6 additions & 6 deletions crates/ark/src/variables/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,7 @@ mod tests {
fn inspect_from_expr(code: &str) -> Vec<Variable> {
let env = Environment::new(harp::parse_eval_base("new.env(parent = emptyenv())").unwrap());
let value = harp::parse_eval_base(code).unwrap();
env.bind("x".into(), value.sexp);
env.bind("x".into(), &value);
// Inspect the S4 object
let path = vec![String::from("x")];
PositronVariable::inspect(env.into(), &path).unwrap()
Expand Down Expand Up @@ -1973,7 +1973,7 @@ mod tests {
r_task(|| {
let env = Environment::new_empty().unwrap();
let value = harp::parse_eval_base("matrix(0, nrow = 10000, ncol = 10000)").unwrap();
env.bind("x".into(), value.sexp);
env.bind("x".into(), &value);

// Inspect the matrix, we should see the list of columns truncated
let path = vec![String::from("x")];
Expand All @@ -1993,7 +1993,7 @@ mod tests {
r_task(|| {
let env = Environment::new_empty().unwrap();
let value = harp::parse_eval_base("paste(1:5e6, collapse = ' - ')").unwrap();
env.bind("x".into(), value.sexp);
env.bind("x".into(), &value);

let path = vec![];
let vars = PositronVariable::inspect(env.into(), &path).unwrap();
Expand All @@ -2004,7 +2004,7 @@ mod tests {
// Test for the empty string
let env = Environment::new_empty().unwrap();
let value = harp::parse_eval_base("''").unwrap();
env.bind("x".into(), value.sexp);
env.bind("x".into(), &value);

let path = vec![];
let vars = PositronVariable::inspect(env.into(), &path).unwrap();
Expand All @@ -2014,7 +2014,7 @@ mod tests {
// Test for the single elment matrix, but with a large character
let env = Environment::new_empty().unwrap();
let value = harp::parse_eval_base("matrix(paste(1:5e6, collapse = ' - '))").unwrap();
env.bind("x".into(), value.sexp);
env.bind("x".into(), &value);
let path = vec![];
let vars = PositronVariable::inspect(env.into(), &path).unwrap();
assert_eq!(vars.len(), 1);
Expand All @@ -2024,7 +2024,7 @@ mod tests {
// Test for the empty matrix
let env = Environment::new_empty().unwrap();
let value = harp::parse_eval_base("matrix(NA, ncol = 0, nrow = 0)").unwrap();
env.bind("x".into(), value.sexp);
env.bind("x".into(), &value);
let path = vec![];
let vars = PositronVariable::inspect(env.into(), &path).unwrap();
assert_eq!(vars.len(), 1);
Expand Down
6 changes: 3 additions & 3 deletions crates/harp/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ impl Environment {
std::iter::successors(Some(self.clone()), |p| p.parent())
}

pub fn bind(&self, name: RSymbol, value: SEXP) {
pub fn bind(&self, name: RSymbol, value: &RObject) {
unsafe {
Rf_defineVar(name.sexp, value.into(), self.inner.sexp);
Rf_defineVar(name.sexp, value.sexp, self.inner.sexp);
}
}

pub fn force_bind(&self, name: RSymbol, value: SEXP) {
pub fn force_bind(&self, name: RSymbol, value: &RObject) {
let locked = self.is_locked_binding(name);
if locked {
self.unlock_binding(name);
Expand Down
12 changes: 7 additions & 5 deletions crates/harp/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,9 @@ pub fn save_rds(x: SEXP, path: &str) {
let path = RObject::from(path);

let env = Environment::new(harp::parse_eval_base("new.env()").unwrap());
env.bind("x".into(), x);
env.bind("path".into(), path.sexp);
let x = RObject::from(x);
env.bind("x".into(), &x);
env.bind("path".into(), &path);

let res = harp::parse_eval0("base::saveRDS(x, path)", env);

Expand All @@ -701,9 +702,10 @@ pub fn push_rds(x: SEXP, path: &str, context: &str) {

let env = Environment::new(harp::parse_eval_base("new.env()").unwrap());

env.bind("x".into(), x);
env.bind("path".into(), path.sexp);
env.bind("context".into(), context.sexp);
let x = RObject::from(x);
env.bind("x".into(), &x);
env.bind("path".into(), &path);
env.bind("context".into(), &context);

let res = harp::parse_eval0(".ps.internal(push_rds(x, path, context))", env);

Expand Down

0 comments on commit 94eba52

Please sign in to comment.