avm2: Impl `Boolean.toString` and `Boolean.valueOf`.

This commit is contained in:
David Wendt 2021-11-15 20:29:48 -05:00 committed by kmeisthax
parent aa01a682e1
commit 7569994f1f
6 changed files with 63 additions and 1 deletions

View File

@ -2,7 +2,7 @@
use crate::avm2::activation::Activation;
use crate::avm2::class::Class;
use crate::avm2::method::Method;
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::names::{Namespace, QName};
use crate::avm2::object::{primitive_allocator, Object, TObject};
use crate::avm2::value::Value;
@ -53,6 +53,39 @@ pub fn class_init<'gc>(
Ok(Value::Undefined)
}
/// Implements `Boolean.toString`
pub fn to_string<'gc>(
_activation: &mut Activation<'_, 'gc, '_>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
if let Some(this) = this {
if let Some(this) = this.as_primitive() {
match this.coerce_to_boolean() {
true => return Ok("true".into()),
false => return Ok("false".into()),
};
}
}
Ok(Value::Undefined)
}
/// Implements `Boolean.valueOf`
pub fn value_of<'gc>(
_activation: &mut Activation<'_, 'gc, '_>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
if let Some(this) = this {
if let Some(this) = this.as_primitive() {
return Ok(this.clone());
}
}
Ok(Value::Undefined)
}
/// Construct `Boolean`'s class.
pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> {
let class = Class::new(
@ -71,5 +104,9 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>
mc,
));
const AS3_INSTANCE_METHODS: &[(&str, NativeMethodImpl)] =
&[("toString", to_string), ("valueOf", value_of)];
write.define_as3_builtin_instance_methods(mc, AS3_INSTANCE_METHODS);
class
}

View File

@ -742,6 +742,7 @@ swf_tests! {
(as3_uint_tostring, "avm2/uint_tostring", 1),
#[ignore] (as3_uint_toexponential, "avm2/uint_toexponential", 1), //Ignored because Flash Player has a print routine that adds extraneous zeros to things
#[ignore] (as3_uint_toprecision, "avm2/uint_toprecision", 1), //Ignored because Flash Player has a print routine that adds extraneous zeros to things
(as3_boolean_tostring, "avm2/boolean_tostring", 1),
}
// TODO: These tests have some inaccuracies currently, so we use approx_eq to test that numeric values are close enough.

View File

@ -0,0 +1,16 @@
package {
public class Test {
}
}
trace("//(true).toString()");
trace((true).toString());
trace("//(true).valueOf() === true");
trace((true).valueOf() === true);
trace("//(false).toString()");
trace((false).toString());
trace("//(false).valueOf() === false");
trace((false).valueOf() === false);

View File

@ -0,0 +1,8 @@
//(true).toString()
true
//(true).valueOf() === true
true
//(false).toString()
false
//(false).valueOf() === false
true

Binary file not shown.

Binary file not shown.