Allow tracing booleans.

This requires implementing *some level* of coercions, even though this isn't the way to do it.
This commit is contained in:
David Wendt 2020-03-06 15:32:47 -05:00
parent 00186f7602
commit c6265bb50c
2 changed files with 14 additions and 1 deletions

View File

@ -21,7 +21,7 @@ fn trace<'gc>(
args: &[Value<'gc>],
) -> Result<ReturnValue<'gc>, Error> {
if let Some(s) = args.get(0) {
log::info!(target: "avm_trace", "{}", s.as_string()?);
log::info!(target: "avm_trace", "{}", s.clone().coerce_string());
}
Ok(Value::Undefined.into())

View File

@ -245,6 +245,9 @@ impl<'gc> Value<'gc> {
}
}
/// Demand a string value, erroring out if one is not found.
///
/// TODO: This should be replaced with `coerce_string` where possible.
pub fn as_string(&self) -> Result<&String, Error> {
match self {
Value::String(s) => Ok(s),
@ -252,6 +255,16 @@ impl<'gc> Value<'gc> {
}
}
/// Coerce a value into a string.
pub fn coerce_string(self) -> String {
match self {
Value::String(s) => s,
Value::Bool(true) => "true".to_string(),
Value::Bool(false) => "false".to_string(),
_ => "".to_string(),
}
}
pub fn as_number(&self) -> Result<f64, Error> {
match self {
Value::Number(f) => Ok(*f),