|
| 1 | +use std::io::Write; |
| 2 | + |
| 3 | +use firefly_rt::function::ErlangResult; |
| 4 | +use firefly_rt::process::ProcessLock; |
| 5 | +use firefly_rt::term::{OpaqueTerm, Term}; |
| 6 | + |
| 7 | +use crate::badarg; |
| 8 | + |
| 9 | +#[export_name = "erlang:display/1"] |
| 10 | +pub extern "C-unwind" fn display(_process: &mut ProcessLock, term: OpaqueTerm) -> ErlangResult { |
| 11 | + let term: Term = term.into(); |
| 12 | + println!("{}", &term); |
| 13 | + ErlangResult::Ok(true.into()) |
| 14 | +} |
| 15 | + |
| 16 | +#[export_name = "erlang:debug/1"] |
| 17 | +pub extern "C-unwind" fn debug(_process: &mut ProcessLock, term: OpaqueTerm) -> ErlangResult { |
| 18 | + let term: Term = term.into(); |
| 19 | + println!("{:?}", &term); |
| 20 | + ErlangResult::Ok(true.into()) |
| 21 | +} |
| 22 | + |
| 23 | +#[export_name = "erlang:display_nl/0"] |
| 24 | +pub extern "C-unwind" fn display_nl(_process: &mut ProcessLock) -> ErlangResult { |
| 25 | + println!(); |
| 26 | + ErlangResult::Ok(true.into()) |
| 27 | +} |
| 28 | + |
| 29 | +#[export_name = "erlang:display_string/1"] |
| 30 | +pub extern "C-unwind" fn display_string( |
| 31 | + process: &mut ProcessLock, |
| 32 | + term: OpaqueTerm, |
| 33 | +) -> ErlangResult { |
| 34 | + let list: Term = term.into(); |
| 35 | + match list { |
| 36 | + Term::Nil => ErlangResult::Ok(true.into()), |
| 37 | + Term::Cons(cons) => { |
| 38 | + match cons.as_ref().to_string() { |
| 39 | + Some(ref s) => print!("{}", s), |
| 40 | + None => badarg!(process, term), |
| 41 | + } |
| 42 | + ErlangResult::Ok(true.into()) |
| 43 | + } |
| 44 | + _other => badarg!(process, term), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[export_name = "erlang:puts/1"] |
| 49 | +pub extern "C-unwind" fn puts(_process: &mut ProcessLock, printable: OpaqueTerm) -> ErlangResult { |
| 50 | + let printable: Term = printable.into(); |
| 51 | + |
| 52 | + let bits = printable.as_bitstring().unwrap(); |
| 53 | + assert!(bits.is_aligned()); |
| 54 | + assert!(bits.is_binary()); |
| 55 | + let bytes = unsafe { bits.as_bytes_unchecked() }; |
| 56 | + let mut stdout = std::io::stdout().lock(); |
| 57 | + stdout.write_all(bytes).unwrap(); |
| 58 | + ErlangResult::Ok(true.into()) |
| 59 | +} |
0 commit comments