avm2: Also do the same for `int`
This commit is contained in:
parent
593158cb04
commit
640763bfda
|
@ -5,7 +5,7 @@ use crate::avm2::class::Class;
|
|||
use crate::avm2::globals::number::{print_with_precision, print_with_radix};
|
||||
use crate::avm2::method::{Method, NativeMethodImpl};
|
||||
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::{AvmString, Error};
|
||||
use gc_arena::{GcCell, MutationContext};
|
||||
|
@ -47,10 +47,80 @@ pub fn native_instance_init<'gc>(
|
|||
|
||||
/// Implements `int`'s class initializer.
|
||||
pub fn class_init<'gc>(
|
||||
_activation: &mut Activation<'_, 'gc, '_>,
|
||||
_this: Option<Object<'gc>>,
|
||||
activation: &mut Activation<'_, 'gc, '_>,
|
||||
this: Option<Object<'gc>>,
|
||||
_args: &[Value<'gc>],
|
||||
) -> 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)
|
||||
}
|
||||
|
||||
|
@ -62,7 +132,7 @@ pub fn to_exponential<'gc>(
|
|||
) -> Result<Value<'gc>, Error> {
|
||||
if let Some(this) = this {
|
||||
if let Some(this) = this.as_primitive() {
|
||||
let number = this.coerce_to_i32(activation)?;
|
||||
if let Value::Integer(number) = this.clone() {
|
||||
let digits = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
|
@ -83,8 +153,9 @@ pub fn to_exponential<'gc>(
|
|||
.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::Undefined)
|
||||
Err("int.prototype.toExponential has been called on an incompatible object".into())
|
||||
}
|
||||
|
||||
/// Implements `int.toFixed`
|
||||
|
@ -95,7 +166,7 @@ pub fn to_fixed<'gc>(
|
|||
) -> Result<Value<'gc>, Error> {
|
||||
if let Some(this) = this {
|
||||
if let Some(this) = this.as_primitive() {
|
||||
let number = this.coerce_to_i32(activation)?;
|
||||
if let Value::Integer(number) = this.clone() {
|
||||
let digits = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
|
@ -113,8 +184,9 @@ pub fn to_fixed<'gc>(
|
|||
.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::Undefined)
|
||||
Err("int.prototype.toFixed has been called on an incompatible object".into())
|
||||
}
|
||||
|
||||
/// Implements `int.toPrecision`
|
||||
|
@ -125,7 +197,7 @@ pub fn to_precision<'gc>(
|
|||
) -> Result<Value<'gc>, Error> {
|
||||
if let Some(this) = this {
|
||||
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
|
||||
.get(0)
|
||||
.cloned()
|
||||
|
@ -139,8 +211,9 @@ pub fn to_precision<'gc>(
|
|||
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`
|
||||
|
@ -151,7 +224,7 @@ pub fn to_string<'gc>(
|
|||
) -> Result<Value<'gc>, Error> {
|
||||
if let Some(this) = this {
|
||||
if let Some(this) = this.as_primitive() {
|
||||
let number = this.coerce_to_i32(activation)?;
|
||||
if let Value::Integer(number) = this.clone() {
|
||||
let radix = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
|
@ -165,8 +238,9 @@ pub fn to_string<'gc>(
|
|||
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`
|
||||
|
|
Loading…
Reference in New Issue