Skip to content

Commit

Permalink
fix dead lock in defatom; alpha release
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Apr 26, 2021
1 parent af04065 commit e79a93f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_runner"
version = "0.3.0-a4"
version = "0.3.0-a5"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion lib/calcit.procs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ export let pr_str = (...args: CrDataValue[]): string => {
return args.map((x) => toString(x, true)).join(" ");
};

/** helper function for println */
/** helper function for println, js only */
export let printable = (...args: CrDataValue[]): string => {
return args.map((x) => toString(x, false)).join(" ");
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@calcit/procs",
"version": "0.3.0-a4",
"version": "0.3.0-a5",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^14.14.41",
Expand Down
46 changes: 25 additions & 21 deletions src/builtins/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ lazy_static! {
static ref REFS_DICT: Mutex<HashMap<String, Calcit>> = Mutex::new(HashMap::new());
}

// need functions with shorter lifetime to escape dead lock
fn read_ref(path: &str) -> Option<Calcit> {
let dict = &REFS_DICT.lock().unwrap();
match dict.get(path) {
Some(v) => Some(v.to_owned()),
None => None,
}
}

fn write_to_ref(path: String, v: Calcit) {
let dict = &mut REFS_DICT.lock().unwrap();
let _ = dict.insert(path, v);
}

/// syntax to prevent expr re-evaluating
pub fn defatom(
expr: &CalcitItems,
Expand All @@ -24,15 +38,11 @@ pub fn defatom(
path.push('/');
path.push_str(s);

let dict = &mut REFS_DICT.lock().unwrap();

if dict.contains_key(&path) {
Ok(Calcit::Ref(path))
} else {
if read_ref(&path).is_none() {
let v = runner::evaluate_expr(code, scope, file_ns, program_code)?;
dict.insert(path.clone(), v);
Ok(Calcit::Ref(path))
write_to_ref(path.to_owned(), v)
}
Ok(Calcit::Ref(path))
}
(Some(a), Some(b)) => Err(format!("defref expected a symbol and an expression: {} , {}", a, b)),
_ => Err(String::from("defref expected 2 nodes")),
Expand All @@ -41,13 +51,10 @@ pub fn defatom(

pub fn deref(xs: &CalcitItems) -> Result<Calcit, String> {
match xs.get(0) {
Some(Calcit::Ref(path)) => {
let dict = &REFS_DICT.lock().unwrap();
match dict.get(path) {
Some(v) => Ok(v.clone()),
None => Err(format!("found nothing after refer &{}", path)),
}
}
Some(Calcit::Ref(path)) => match read_ref(path) {
Some(v) => Ok(v),
None => Err(format!("found nothing after refer &{}", path)),
},
Some(a) => Err(format!("deref expected a ref, got: {}", a)),
_ => Err(String::from("deref expected 1 argument, got nothing")),
}
Expand All @@ -56,14 +63,11 @@ pub fn deref(xs: &CalcitItems) -> Result<Calcit, String> {
pub fn reset_bang(xs: &CalcitItems) -> Result<Calcit, String> {
match (xs.get(0), xs.get(1)) {
(Some(Calcit::Ref(path)), Some(v)) => {
let dict = &mut REFS_DICT.lock().unwrap();

if dict.contains_key(path) {
dict.insert(path.clone(), v.clone());
Ok(Calcit::Nil)
} else {
Err(format!("missing pre-exisiting data for path &{}", path))
if read_ref(path).is_none() {
return Err(format!("missing pre-exisiting data for path &{}", path));
}
write_to_ref(path.to_owned(), v.to_owned());
Ok(Calcit::Nil)
}
(Some(a), Some(b)) => Err(format!("reset! expected a ref and a value, got: {} {}", a, b)),
(a, b) => Err(format!("reset! expected 2 arguments, got: {:?} {:?}", a, b)),
Expand Down
7 changes: 6 additions & 1 deletion src/codegen/emit_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ fn gen_call_code(
file_imports: &RefCell<ImportsDict>,
) -> Result<String, String> {
let var_prefix = if ns == "calcit.core" { "" } else { "$calcit." };
let proc_prefix = if ns == primes::CORE_NS {
"$calcit_procs."
} else {
"$calcit."
};
if ys.is_empty() {
println!("[Warn] Unexpected empty list inside {}", xs);
return Ok(String::from("()"));
Expand Down Expand Up @@ -337,7 +342,7 @@ fn gen_call_code(
// not core syntax, but treat as macro for better debugging experience
let args = ys.skip(1);
let args_code = gen_args_code(&args, ns, local_defs, file_imports)?;
Ok(format!("console.log({}printable({}))", var_prefix, args_code))
Ok(format!("console.log({}printable({}))", proc_prefix, args_code))
}
"exists?" => {
// not core syntax, but treat as macro for availability
Expand Down
2 changes: 1 addition & 1 deletion src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl PartialEq for Calcit {
pub const CORE_NS: &str = "calcit.core";
pub const GENERATED_NS: &str = "calcit.gen";

pub const CALCI_VERSION: &str = "0.3.0-a4";
pub const CALCI_VERSION: &str = "0.3.0-a5";

impl Calcit {
pub fn turn_string(&self) -> String {
Expand Down

0 comments on commit e79a93f

Please sign in to comment.