avm2: Partially implement `toLocaleString` insamuch as is necessary to run Array tests on it.

This appears to work almost like it's own TObject method; you can run `Object.prototype.toLocaleString` on all sorts of things and it has separate behavior to what the class method for it might be. I have attempted to match Flash Player as best as I can.
This commit is contained in:
David Wendt 2020-09-02 19:28:17 -04:00 committed by Mike Welsh
parent dbaef812fa
commit 2ae3b6445b
4 changed files with 36 additions and 1 deletions

View File

@ -46,7 +46,7 @@ fn to_locale_string<'gc>(
_: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
Ok(this
.map(|t| t.to_string(activation.context.gc_context))
.map(|t| t.to_locale_string(activation.context.gc_context))
.unwrap_or(Ok(Value::Undefined))?)
}

View File

@ -673,6 +673,23 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
/// coercions.
fn to_string(&self, mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error>;
/// Implement the result of calling `Object.prototype.toLocaleString` on this
/// object class.
///
/// `toLocaleString` is a method used to request an object be coerced to a
/// locale-dependent string value. The default implementation appears to
/// generate a debug-style string based on the name of the class this
/// object is, in the format of `[object Class]` (where `Class` is the name
/// of the class that created this object).
fn to_locale_string(&self, mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
let class_name = self
.as_proto_class()
.map(|c| c.read().name().local_name())
.unwrap_or_else(|| "Object".into());
Ok(AvmString::new(mc, format!("[object {}]", class_name)).into())
}
/// Implement the result of calling `Object.prototype.valueOf` on this
/// object class.
///

View File

@ -288,6 +288,10 @@ impl<'gc> TObject<'gc> for FunctionObject<'gc> {
}
}
fn to_locale_string(&self, mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
self.to_string(mc)
}
fn value_of(&self, _mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
Ok(Value::Object(Object::from(*self)))
}

View File

@ -79,6 +79,20 @@ impl<'gc> TObject<'gc> for PrimitiveObject<'gc> {
Ok(self.0.read().primitive.clone())
}
fn to_locale_string(&self, mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
match self.0.read().primitive.clone() {
val @ Value::Integer(_) | val @ Value::Unsigned(_) => Ok(val),
_ => {
let class_name = self
.as_proto_class()
.map(|c| c.read().name().local_name())
.unwrap_or_else(|| "Object".into());
Ok(AvmString::new(mc, format!("[object {}]", class_name)).into())
}
}
}
fn value_of(&self, _mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
Ok(self.0.read().primitive.clone())
}