avm2: Add class call handlers for Boolean, Number, and (u)int

This commit is contained in:
Lord-McSweeney 2023-10-20 18:44:39 -07:00 committed by Lord-McSweeney
parent 043f7cdea7
commit 0c6a0e80af
4 changed files with 68 additions and 0 deletions

View File

@ -82,6 +82,19 @@ fn class_init<'gc>(
Ok(Value::Undefined)
}
pub fn call_handler<'gc>(
_activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(args
.get(0)
.cloned()
.unwrap_or(Value::Bool(false))
.coerce_to_boolean()
.into())
}
/// Implements `Boolean.toString`
fn to_string<'gc>(
_activation: &mut Activation<'_, 'gc>,
@ -131,6 +144,11 @@ pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> GcCell<'gc, Cl
"<Boolean native instance initializer>",
mc,
));
write.set_call_handler(Method::from_builtin(
call_handler,
"<Boolean call handler>",
mc,
));
const AS3_INSTANCE_METHODS: &[(&str, NativeMethodImpl)] =
&[("toString", to_string), ("valueOf", value_of)];

View File

@ -135,6 +135,19 @@ fn class_init<'gc>(
Ok(Value::Undefined)
}
pub fn call_handler<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(args
.get(0)
.cloned()
.unwrap_or(Value::Integer(0))
.coerce_to_i32(activation)?
.into())
}
/// Implements `int.toExponential`
use crate::avm2::globals::number::to_exponential;
@ -228,6 +241,7 @@ pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> GcCell<'gc, Cl
"<int native instance initializer>",
mc,
));
write.set_call_handler(Method::from_builtin(call_handler, "<int call handler>", mc));
// 'length' is a weird undocumented constant in int.
// We need to define it, since it shows up in 'describeType'

View File

@ -135,6 +135,19 @@ fn class_init<'gc>(
Ok(Value::Undefined)
}
pub fn call_handler<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(args
.get(0)
.cloned()
.unwrap_or(Value::Number(0.0))
.coerce_to_number(activation)?
.into())
}
/// Implements `Number.toExponential`
pub fn to_exponential<'gc>(
activation: &mut Activation<'_, 'gc>,
@ -373,6 +386,11 @@ pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> GcCell<'gc, Cl
"<Number native instance initializer>",
mc,
));
write.set_call_handler(Method::from_builtin(
call_handler,
"<Number call handler>",
mc,
));
const CLASS_CONSTANTS_NUMBER: &[(&str, f64)] = &[
("MAX_VALUE", f64::MAX),

View File

@ -135,6 +135,19 @@ fn class_init<'gc>(
Ok(Value::Undefined)
}
pub fn call_handler<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(args
.get(0)
.cloned()
.unwrap_or(Value::Integer(0))
.coerce_to_u32(activation)?
.into())
}
/// Implements `uint.toExponential`
use crate::avm2::globals::number::to_exponential;
@ -229,6 +242,11 @@ pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> GcCell<'gc, Cl
"<uint native instance initializer>",
mc,
));
write.set_call_handler(Method::from_builtin(
call_handler,
"<uint call handler>",
mc,
));
const CLASS_CONSTANTS_UINT: &[(&str, u32)] =
&[("MAX_VALUE", u32::MAX), ("MIN_VALUE", u32::MIN)];