diff --git a/core/src/avm2/globals.rs b/core/src/avm2/globals.rs index 7dcf3b68b..01a49346c 100644 --- a/core/src/avm2/globals.rs +++ b/core/src/avm2/globals.rs @@ -21,7 +21,7 @@ fn trace<'gc>( args: &[Value<'gc>], ) -> Result, 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()) diff --git a/core/src/avm2/value.rs b/core/src/avm2/value.rs index 19d29c186..7754aa8e6 100644 --- a/core/src/avm2/value.rs +++ b/core/src/avm2/value.rs @@ -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 { match self { Value::Number(f) => Ok(*f),