avm2: Also do the same for `int`

This commit is contained in:
David Wendt 2021-11-16 17:55:55 -05:00 committed by kmeisthax
parent 593158cb04
commit 640763bfda
1 changed files with 132 additions and 58 deletions

View File

@ -5,7 +5,7 @@ use crate::avm2::class::Class;
use crate::avm2::globals::number::{print_with_precision, print_with_radix}; use crate::avm2::globals::number::{print_with_precision, print_with_radix};
use crate::avm2::method::{Method, NativeMethodImpl}; use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::names::{Namespace, QName}; use crate::avm2::names::{Namespace, QName};
use crate::avm2::object::{primitive_allocator, Object, TObject}; use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject};
use crate::avm2::value::Value; use crate::avm2::value::Value;
use crate::avm2::{AvmString, Error}; use crate::avm2::{AvmString, Error};
use gc_arena::{GcCell, MutationContext}; use gc_arena::{GcCell, MutationContext};
@ -47,10 +47,80 @@ pub fn native_instance_init<'gc>(
/// Implements `int`'s class initializer. /// Implements `int`'s class initializer.
pub fn class_init<'gc>( pub fn class_init<'gc>(
_activation: &mut Activation<'_, 'gc, '_>, activation: &mut Activation<'_, 'gc, '_>,
_this: Option<Object<'gc>>, this: Option<Object<'gc>>,
_args: &[Value<'gc>], _args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> { ) -> Result<Value<'gc>, Error> {
if let Some(this) = this {
let mut int_proto = this
.get_property(this, &QName::dynamic_name("prototype").into(), activation)?
.coerce_to_object(activation)?;
let scope = activation.create_scopechain();
let gc_context = activation.context.gc_context;
let this_class = this.as_class_object().unwrap();
int_proto.install_dynamic_property(
gc_context,
QName::new(Namespace::public(), "toExponential"),
FunctionObject::from_method(
activation,
Method::from_builtin(to_exponential, "toExponential", gc_context),
scope,
None,
Some(this_class),
)
.into(),
)?;
int_proto.install_dynamic_property(
gc_context,
QName::new(Namespace::public(), "toFixed"),
FunctionObject::from_method(
activation,
Method::from_builtin(to_fixed, "toFixed", gc_context),
scope,
None,
Some(this_class),
)
.into(),
)?;
int_proto.install_dynamic_property(
gc_context,
QName::new(Namespace::public(), "toPrecision"),
FunctionObject::from_method(
activation,
Method::from_builtin(to_precision, "toPrecision", gc_context),
scope,
None,
Some(this_class),
)
.into(),
)?;
int_proto.install_dynamic_property(
gc_context,
QName::new(Namespace::public(), "toString"),
FunctionObject::from_method(
activation,
Method::from_builtin(to_string, "toString", gc_context),
scope,
None,
Some(this_class),
)
.into(),
)?;
int_proto.install_dynamic_property(
gc_context,
QName::new(Namespace::public(), "valueOf"),
FunctionObject::from_method(
activation,
Method::from_builtin(value_of, "valueOf", gc_context),
scope,
None,
Some(this_class),
)
.into(),
)?;
}
Ok(Value::Undefined) Ok(Value::Undefined)
} }
@ -62,7 +132,7 @@ pub fn to_exponential<'gc>(
) -> Result<Value<'gc>, Error> { ) -> Result<Value<'gc>, Error> {
if let Some(this) = this { if let Some(this) = this {
if let Some(this) = this.as_primitive() { if let Some(this) = this.as_primitive() {
let number = this.coerce_to_i32(activation)?; if let Value::Integer(number) = this.clone() {
let digits = args let digits = args
.get(0) .get(0)
.cloned() .cloned()
@ -83,8 +153,9 @@ pub fn to_exponential<'gc>(
.into()); .into());
} }
} }
}
Ok(Value::Undefined) Err("int.prototype.toExponential has been called on an incompatible object".into())
} }
/// Implements `int.toFixed` /// Implements `int.toFixed`
@ -95,7 +166,7 @@ pub fn to_fixed<'gc>(
) -> Result<Value<'gc>, Error> { ) -> Result<Value<'gc>, Error> {
if let Some(this) = this { if let Some(this) = this {
if let Some(this) = this.as_primitive() { if let Some(this) = this.as_primitive() {
let number = this.coerce_to_i32(activation)?; if let Value::Integer(number) = this.clone() {
let digits = args let digits = args
.get(0) .get(0)
.cloned() .cloned()
@ -113,8 +184,9 @@ pub fn to_fixed<'gc>(
.into()); .into());
} }
} }
}
Ok(Value::Undefined) Err("int.prototype.toFixed has been called on an incompatible object".into())
} }
/// Implements `int.toPrecision` /// Implements `int.toPrecision`
@ -125,7 +197,7 @@ pub fn to_precision<'gc>(
) -> Result<Value<'gc>, Error> { ) -> Result<Value<'gc>, Error> {
if let Some(this) = this { if let Some(this) = this {
if let Some(this) = this.as_primitive() { if let Some(this) = this.as_primitive() {
let number = this.coerce_to_i32(activation)?; if let Value::Integer(number) = this.clone() {
let wanted_digits = args let wanted_digits = args
.get(0) .get(0)
.cloned() .cloned()
@ -139,8 +211,9 @@ pub fn to_precision<'gc>(
return Ok(print_with_precision(activation, number as f64, wanted_digits)?.into()); return Ok(print_with_precision(activation, number as f64, wanted_digits)?.into());
} }
} }
}
Ok(Value::Undefined) Err("int.prototype.toPrecision has been called on an incompatible object".into())
} }
/// Implements `int.toString` /// Implements `int.toString`
@ -151,7 +224,7 @@ pub fn to_string<'gc>(
) -> Result<Value<'gc>, Error> { ) -> Result<Value<'gc>, Error> {
if let Some(this) = this { if let Some(this) = this {
if let Some(this) = this.as_primitive() { if let Some(this) = this.as_primitive() {
let number = this.coerce_to_i32(activation)?; if let Value::Integer(number) = this.clone() {
let radix = args let radix = args
.get(0) .get(0)
.cloned() .cloned()
@ -165,8 +238,9 @@ pub fn to_string<'gc>(
return Ok(print_with_radix(activation, number as f64, radix)?.into()); return Ok(print_with_radix(activation, number as f64, radix)?.into());
} }
} }
}
Ok(Value::Undefined) Err("int.prototype.toString has been called on an incompatible object".into())
} }
/// Implements `int.valueOf` /// Implements `int.valueOf`