64 lines
1.6 KiB
Rust
64 lines
1.6 KiB
Rust
use dvm_lib::vm::DvmContext;
|
|
use dvm_lib::vm::value::Value;
|
|
use std::error::Error;
|
|
use std::fmt::{Debug, Display, Formatter};
|
|
use std::rc::Rc;
|
|
|
|
pub fn add_all_std_core(context: &mut DvmContext) {
|
|
let platform_functions = context.platform_functions_mut();
|
|
platform_functions.insert(Rc::from("println"), std_core_println);
|
|
}
|
|
|
|
struct StdCoreError {
|
|
message: String,
|
|
}
|
|
|
|
impl StdCoreError {
|
|
pub fn new(message: &str) -> StdCoreError {
|
|
Self {
|
|
message: message.into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Debug for StdCoreError {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}", self.message)
|
|
}
|
|
}
|
|
|
|
impl Display for StdCoreError {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}", self.message)
|
|
}
|
|
}
|
|
|
|
impl Error for StdCoreError {}
|
|
|
|
pub fn std_core_println(args: &[Value]) -> Result<Value, Box<dyn Error>> {
|
|
let maybe_to_print = args.get(0);
|
|
match maybe_to_print {
|
|
None => Err(Box::new(StdCoreError::new("Missing to_print arg"))),
|
|
Some(to_print) => {
|
|
match to_print {
|
|
Value::Object(o) => {
|
|
println!("{}", o.borrow());
|
|
}
|
|
Value::Int(i) => {
|
|
println!("{}", i);
|
|
}
|
|
Value::Double(d) => {
|
|
println!("{}", d);
|
|
}
|
|
Value::String(s) => {
|
|
println!("{}", s);
|
|
}
|
|
Value::Null => {
|
|
println!("null");
|
|
}
|
|
}
|
|
Ok(Value::Null)
|
|
}
|
|
}
|
|
}
|