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,29 +132,30 @@ 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)?;
|
||||
let digits = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
.unwrap_or(Value::Unsigned(0))
|
||||
.coerce_to_u32(activation)? as usize;
|
||||
if let Value::Integer(number) = this.clone() {
|
||||
let digits = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
.unwrap_or(Value::Unsigned(0))
|
||||
.coerce_to_u32(activation)? as usize;
|
||||
|
||||
if digits > 20 {
|
||||
return Err("toExponential can only print with 0 through 20 digits.".into());
|
||||
if digits > 20 {
|
||||
return Err("toExponential can only print with 0 through 20 digits.".into());
|
||||
}
|
||||
|
||||
return Ok(AvmString::new_utf8(
|
||||
activation.context.gc_context,
|
||||
format!("{0:.1$e}", number, digits)
|
||||
.replace("e", "e+")
|
||||
.replace("e+-", "e-")
|
||||
.replace("e+0", ""),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
return Ok(AvmString::new_utf8(
|
||||
activation.context.gc_context,
|
||||
format!("{0:.1$e}", number, digits)
|
||||
.replace("e", "e+")
|
||||
.replace("e+-", "e-")
|
||||
.replace("e+0", ""),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::Undefined)
|
||||
Err("int.prototype.toExponential has been called on an incompatible object".into())
|
||||
}
|
||||
|
||||
/// Implements `int.toFixed`
|
||||
|
@ -95,26 +166,27 @@ 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)?;
|
||||
let digits = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
.unwrap_or(Value::Unsigned(0))
|
||||
.coerce_to_u32(activation)? as usize;
|
||||
if let Value::Integer(number) = this.clone() {
|
||||
let digits = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
.unwrap_or(Value::Unsigned(0))
|
||||
.coerce_to_u32(activation)? as usize;
|
||||
|
||||
if digits > 20 {
|
||||
return Err("toFixed can only print with 0 through 20 digits.".into());
|
||||
if digits > 20 {
|
||||
return Err("toFixed can only print with 0 through 20 digits.".into());
|
||||
}
|
||||
|
||||
return Ok(AvmString::new_utf8(
|
||||
activation.context.gc_context,
|
||||
format!("{0:.1$}", number as f64, digits),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
return Ok(AvmString::new_utf8(
|
||||
activation.context.gc_context,
|
||||
format!("{0:.1$}", number as f64, digits),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::Undefined)
|
||||
Err("int.prototype.toFixed has been called on an incompatible object".into())
|
||||
}
|
||||
|
||||
/// Implements `int.toPrecision`
|
||||
|
@ -125,22 +197,23 @@ 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)?;
|
||||
let wanted_digits = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
.unwrap_or(Value::Unsigned(0))
|
||||
.coerce_to_u32(activation)? as usize;
|
||||
if let Value::Integer(number) = this.clone() {
|
||||
let wanted_digits = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
.unwrap_or(Value::Unsigned(0))
|
||||
.coerce_to_u32(activation)? as usize;
|
||||
|
||||
if wanted_digits < 1 || wanted_digits > 21 {
|
||||
return Err("toPrecision can only print with 1 through 21 digits.".into());
|
||||
if wanted_digits < 1 || wanted_digits > 21 {
|
||||
return Err("toPrecision can only print with 1 through 21 digits.".into());
|
||||
}
|
||||
|
||||
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`
|
||||
|
@ -151,22 +224,23 @@ 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)?;
|
||||
let radix = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
.unwrap_or(Value::Unsigned(10))
|
||||
.coerce_to_u32(activation)? as usize;
|
||||
if let Value::Integer(number) = this.clone() {
|
||||
let radix = args
|
||||
.get(0)
|
||||
.cloned()
|
||||
.unwrap_or(Value::Unsigned(10))
|
||||
.coerce_to_u32(activation)? as usize;
|
||||
|
||||
if radix < 2 || radix > 36 {
|
||||
return Err("toString can only print in bases 2 thru 36.".into());
|
||||
if radix < 2 || radix > 36 {
|
||||
return Err("toString can only print in bases 2 thru 36.".into());
|
||||
}
|
||||
|
||||
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`
|
||||
|
|
Loading…
Reference in New Issue