`coerce_string` may return a static string, which we shouldn't clone.

This commit is contained in:
David Wendt 2020-07-10 23:58:41 -04:00
parent f0c633fe81
commit ae26615bb4
1 changed files with 6 additions and 5 deletions

View File

@ -4,6 +4,7 @@ use crate::avm2::names::Namespace;
use crate::avm2::object::Object; use crate::avm2::object::Object;
use crate::avm2::Error; use crate::avm2::Error;
use gc_arena::Collect; use gc_arena::Collect;
use std::borrow::Cow;
use std::f64::NAN; use std::f64::NAN;
use swf::avm2::types::{AbcFile, DefaultValue as AbcDefaultValue, Index}; use swf::avm2::types::{AbcFile, DefaultValue as AbcDefaultValue, Index};
@ -257,12 +258,12 @@ impl<'gc> Value<'gc> {
} }
/// Coerce a value into a string. /// Coerce a value into a string.
pub fn coerce_string(self) -> String { pub fn coerce_string(self) -> Cow<'static, str> {
match self { match self {
Value::String(s) => s, Value::String(s) => Cow::Owned(s),
Value::Bool(true) => "true".to_string(), Value::Bool(true) => Cow::Borrowed("true"),
Value::Bool(false) => "false".to_string(), Value::Bool(false) => Cow::Borrowed("false"),
_ => "".to_string(), _ => Cow::Borrowed(""),
} }
} }