-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from calcit-lang/calcit-err
refactor to CalcitErr
- Loading branch information
Showing
28 changed files
with
1,015 additions
and
991 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
|
||
max_width = 120 | ||
max_width = 136 | ||
tab_spaces = 2 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,65 @@ | ||
use cirru_edn::Edn; | ||
|
||
use calcit_runner::{ | ||
builtins, | ||
data::edn::{calcit_to_edn, edn_to_calcit}, | ||
primes::{Calcit, CalcitItems, CrListWrap}, | ||
primes::{Calcit, CalcitErr, CalcitItems, CrListWrap}, | ||
}; | ||
|
||
/// FFI protocol types | ||
type EdnFfi = fn(args: Vec<Edn>) -> Result<Edn, String>; | ||
|
||
pub fn inject_platform_apis() { | ||
builtins::register_import_proc("&call-dylib-edn", call_dylib_edn); | ||
builtins::register_import_proc("echo", echo); | ||
builtins::register_import_proc("println", echo); | ||
} | ||
|
||
// &call-dylib-edn | ||
pub fn call_dylib_edn(xs: &CalcitItems) -> Result<Calcit, String> { | ||
pub fn call_dylib_edn(xs: &CalcitItems) -> Result<Calcit, CalcitErr> { | ||
if xs.is_empty() { | ||
return Err(format!( | ||
return Err(CalcitErr::use_string(format!( | ||
"&call-dylib-edn expected >2 arguments, got {}", | ||
CrListWrap(xs.to_owned()) | ||
)); | ||
))); | ||
} | ||
let lib_name = if let Calcit::Str(s) = &xs[0] { | ||
s.to_owned() | ||
} else { | ||
return Err(format!("&call-dylib-edn expected a lib_name, got {}", xs[0])); | ||
return Err(CalcitErr::use_string(format!("&call-dylib-edn expected a lib_name, got {}", xs[0]))); | ||
}; | ||
|
||
let method: String = if let Calcit::Str(s) = &xs[1] { | ||
s.to_owned() | ||
} else { | ||
return Err(format!("&call-dylib-edn expected a method name, got {}", xs[1])); | ||
return Err(CalcitErr::use_string(format!( | ||
"&call-dylib-edn expected a method name, got {}", | ||
xs[1] | ||
))); | ||
}; | ||
let mut ys: Vec<Edn> = vec![]; | ||
for (idx, v) in xs.iter().enumerate() { | ||
if idx > 1 { | ||
ys.push(calcit_to_edn(v)?); | ||
ys.push(calcit_to_edn(v).map_err(CalcitErr::use_string)?); | ||
} | ||
} | ||
|
||
unsafe { | ||
let lib = libloading::Library::new(&lib_name).expect("dylib not found"); | ||
let func: libloading::Symbol<EdnFfi> = lib.get(method.as_bytes()).expect("dy function not found"); | ||
let ret = func(ys.to_owned())?; | ||
let ret = func(ys.to_owned()).map_err(CalcitErr::use_string)?; | ||
Ok(edn_to_calcit(&ret)) | ||
} | ||
} | ||
|
||
pub fn echo(xs: &CalcitItems) -> Result<Calcit, CalcitErr> { | ||
let mut s = String::from(""); | ||
for (idx, x) in xs.iter().enumerate() { | ||
if idx > 0 { | ||
s.push(' '); | ||
} | ||
s.push_str(&x.turn_string()); | ||
} | ||
println!("{}", s); | ||
Ok(Calcit::Nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.