Skip to content

Commit

Permalink
avm2: Declare correct signature for builtin rust-side getters/setters
Browse files Browse the repository at this point in the history
This only affects the handful of classes that we still implement
entirely in Rust (with only an ActionScript stub when compiling
playerglobals).

We now expose the proper signatures to `describeType`, and have
more type information for future use in the optimizer.
  • Loading branch information
Aaron1011 authored and torokati44 committed Aug 13, 2024
1 parent 3b9d9cf commit 06ac6fc
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 72 deletions.
25 changes: 20 additions & 5 deletions core/src/avm2/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,24 +1162,39 @@ impl<'gc> Class<'gc> {
&'static str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
Multiname<'gc>,
)],
) {
for &(name, getter, setter) in items {
for (name, getter, setter, prop_type) in items {
if let Some(getter) = getter {
self.define_instance_trait(
mc,
Trait::from_getter(
QName::new(namespace, name),
Method::from_builtin(getter, name, mc),
QName::new(namespace, *name),
Method::from_builtin_and_params(
*getter,
name,
vec![],
prop_type.clone(),
false,
mc,
),
),
);
}
if let Some(setter) = setter {
self.define_instance_trait(
mc,
Trait::from_setter(
QName::new(namespace, name),
Method::from_builtin(setter, name, mc),
QName::new(namespace, *name),
Method::from_builtin_and_params(
*setter,
name,
vec![ParamConfig::of_type("val", prop_type.clone())],
QName::new(namespace, "void").into(),
false,
mc,
),
),
);
}
Expand Down
15 changes: 8 additions & 7 deletions core/src/avm2/globals/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::avm2::error::range_error;
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{array_allocator, ArrayObject, FunctionObject, Object, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::QName;
use crate::avm2::{Error, Multiname};
use crate::string::AvmString;
use bitflags::bitflags;
use std::cmp::{min, Ordering};
Expand Down Expand Up @@ -1259,15 +1259,16 @@ pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> Class<'gc> {
Method::from_builtin(class_call, "<Array call handler>", mc),
);

const PUBLIC_INSTANCE_PROPERTIES: &[(
&str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
)] = &[("length", Some(length), Some(set_length))];
let public_instance_properties = &[(
"length",
Some(length as _),
Some(set_length as _),
Multiname::new(activation.avm2().public_namespace_base_version, "uint"),
)];
class.define_builtin_instance_properties(
mc,
activation.avm2().public_namespace_base_version,
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);

class.define_builtin_instance_methods(
Expand Down
17 changes: 9 additions & 8 deletions core/src/avm2/globals/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::method::Method;
use crate::avm2::object::{Object, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::QName;
use crate::avm2::{Error, Multiname};

/// Implements `Class`'s instance initializer.
///
Expand Down Expand Up @@ -54,15 +54,16 @@ pub fn create_i_class<'gc>(
gc_context,
);

const PUBLIC_INSTANCE_PROPERTIES: &[(
&str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
)] = &[("prototype", Some(prototype), None)];
let public_instance_properties = &[(
"prototype",
Some(prototype as _),
None,
Multiname::any(activation.context.gc_context),
)];
class_i_class.define_builtin_instance_properties(
gc_context,
activation.avm2().public_namespace_base_version,
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);

class_i_class.mark_traits_loaded(activation.context.gc_context);
Expand Down
110 changes: 90 additions & 20 deletions core/src/avm2/globals/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::avm2::class::Class;
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{date_allocator, DateObject, FunctionObject, Object, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::QName;
use crate::avm2::{Error, Multiname};
use crate::locale::{get_current_date_time, get_timezone};
use crate::string::{utils as string_utils, AvmString, WStr};
use chrono::{DateTime, Datelike, Duration, FixedOffset, LocalResult, TimeZone, Timelike, Utc};
Expand Down Expand Up @@ -1338,38 +1338,108 @@ pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> Class<'gc> {
Method::from_builtin(call_handler, "<Date call handler>", mc),
);

const PUBLIC_INSTANCE_PROPERTIES: &[(
let number_multiname =
Multiname::new(activation.avm2().public_namespace_base_version, "Number");

let public_instance_properties: &[(
&str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
Multiname<'gc>,
)] = &[
("time", Some(time), Some(set_time)),
("milliseconds", Some(milliseconds), Some(set_milliseconds)),
("seconds", Some(seconds), Some(set_seconds)),
("minutes", Some(minutes), Some(set_minutes)),
("hours", Some(hours), Some(set_hours)),
("date", Some(date), Some(set_date)),
("month", Some(month), Some(set_month)),
("fullYear", Some(full_year), Some(set_full_year)),
("time", Some(time), Some(set_time), number_multiname.clone()),
(
"milliseconds",
Some(milliseconds),
Some(set_milliseconds),
number_multiname.clone(),
),
(
"seconds",
Some(seconds),
Some(set_seconds),
number_multiname.clone(),
),
(
"minutes",
Some(minutes),
Some(set_minutes),
number_multiname.clone(),
),
(
"hours",
Some(hours),
Some(set_hours),
number_multiname.clone(),
),
("date", Some(date), Some(set_date), number_multiname.clone()),
(
"month",
Some(month),
Some(set_month),
number_multiname.clone(),
),
(
"fullYear",
Some(full_year),
Some(set_full_year),
number_multiname.clone(),
),
(
"millisecondsUTC",
Some(milliseconds_utc),
Some(set_milliseconds_utc),
number_multiname.clone(),
),
("day", Some(day), None, number_multiname.clone()),
(
"secondsUTC",
Some(seconds_utc),
Some(set_seconds_utc),
number_multiname.clone(),
),
(
"minutesUTC",
Some(minutes_utc),
Some(set_minutes_utc),
number_multiname.clone(),
),
(
"hoursUTC",
Some(hours_utc),
Some(set_hours_utc),
number_multiname.clone(),
),
(
"dateUTC",
Some(date_utc),
Some(set_date_utc),
number_multiname.clone(),
),
(
"monthUTC",
Some(month_utc),
Some(set_month_utc),
number_multiname.clone(),
),
(
"fullYearUTC",
Some(full_year_utc),
Some(set_full_year_utc),
number_multiname.clone(),
),
("dayUTC", Some(day_utc), None, number_multiname.clone()),
(
"timezoneOffset",
Some(timezone_offset),
None,
number_multiname.clone(),
),
("day", Some(day), None),
("secondsUTC", Some(seconds_utc), Some(set_seconds_utc)),
("minutesUTC", Some(minutes_utc), Some(set_minutes_utc)),
("hoursUTC", Some(hours_utc), Some(set_hours_utc)),
("dateUTC", Some(date_utc), Some(set_date_utc)),
("monthUTC", Some(month_utc), Some(set_month_utc)),
("fullYearUTC", Some(full_year_utc), Some(set_full_year_utc)),
("dayUTC", Some(day_utc), None),
("timezoneOffset", Some(timezone_offset), None),
];
class.define_builtin_instance_properties(
mc,
activation.avm2().public_namespace_base_version,
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);
class.define_builtin_instance_methods(
mc,
Expand Down
24 changes: 15 additions & 9 deletions core/src/avm2/globals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::avm2::globals::array::resolve_array_hole;
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{function_allocator, FunctionObject, Object, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::QName;
use crate::avm2::{Error, Multiname};

/// Implements `Function`'s instance initializer.
pub fn instance_init<'gc>(
Expand Down Expand Up @@ -252,18 +252,24 @@ pub fn create_class<'gc>(
AS3_INSTANCE_METHODS,
);

const PUBLIC_INSTANCE_PROPERTIES: &[(
&str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
)] = &[
("prototype", Some(prototype), Some(set_prototype)),
("length", Some(length), None),
let public_instance_properties = &[
(
"prototype",
Some(prototype as _),
Some(set_prototype as _),
Multiname::any(activation.context.gc_context),
),
(
"length",
Some(length as _),
None,
Multiname::new(activation.avm2().public_namespace_base_version, "int"),
),
];
function_i_class.define_builtin_instance_properties(
gc_context,
activation.avm2().public_namespace_base_version,
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);

const CONSTANTS_INT: &[(&str, i32)] = &[("length", 1)];
Expand Down
23 changes: 16 additions & 7 deletions core/src/avm2/globals/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::avm2::error::make_error_1098;
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{namespace_allocator, FunctionObject, Object, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::Namespace;
use crate::avm2::QName;
use crate::avm2::{Error, Multiname};
use crate::string::AvmString;

// All of these methods will be defined as both
Expand Down Expand Up @@ -194,15 +194,24 @@ pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> Class<'gc> {
Method::from_builtin(class_call, "<Namespace call handler>", mc),
);

const PUBLIC_INSTANCE_PROPERTIES: &[(
&str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
)] = &[("prefix", Some(prefix), None), ("uri", Some(uri), None)];
let public_instance_properties = &[
(
"prefix",
Some(prefix as _),
None,
Multiname::any(activation.context.gc_context),
),
(
"uri",
Some(uri as _),
None,
Multiname::new(activation.avm2().public_namespace_base_version, "String"),
),
];
class.define_builtin_instance_properties(
mc,
activation.avm2().public_namespace_base_version,
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);

const CONSTANTS_INT: &[(&str, i32)] = &[("length", 2)];
Expand Down
15 changes: 8 additions & 7 deletions core/src/avm2/globals/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject};
use crate::avm2::regexp::RegExpFlags;
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::QName;
use crate::avm2::{ArrayObject, ArrayStorage};
use crate::avm2::{Error, Multiname};
use crate::string::{AvmString, WString};

// All of these methods will be defined as both
Expand Down Expand Up @@ -702,15 +702,16 @@ pub fn create_class<'gc>(activation: &mut Activation<'_, 'gc>) -> Class<'gc> {
Method::from_builtin(call_handler, "<String call handler>", mc),
);

const PUBLIC_INSTANCE_PROPERTIES: &[(
&str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
)] = &[("length", Some(length), None)];
let public_instance_properties = &[(
"length",
Some(length as _),
None,
Multiname::new(activation.avm2().public_namespace_base_version, "int"),
)];
class.define_builtin_instance_properties(
mc,
activation.avm2().public_namespace_base_version,
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);
class.define_builtin_instance_methods(
mc,
Expand Down
Loading

0 comments on commit 06ac6fc

Please sign in to comment.