Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avm2: Declare correct signature for builtin rust-side getters/setters #17494

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions core/src/avm2/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,7 @@ impl<'gc> Class<'gc> {
}

#[inline(never)]
#[allow(clippy::type_complexity)]
pub fn define_builtin_instance_properties(
self,
mc: &Mutation<'gc>,
Expand All @@ -1155,24 +1156,39 @@ impl<'gc> Class<'gc> {
&'static str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
Option<Gc<'gc, Multiname<'gc>>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be simpler if this accepted Option<&' static str> instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the simplest thing here would be to use the CommonNamespaces fields in the getter/setter definitions.

)],
) {
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,
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)],
Some(Gc::new(mc, QName::new(namespace, "void").into())),
false,
mc,
),
),
);
}
Expand Down
19 changes: 12 additions & 7 deletions core/src/avm2/globals/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ 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 gc_arena::Gc;
use std::cmp::{min, Ordering};
use std::mem::swap;

Expand Down Expand Up @@ -1349,15 +1350,19 @@ 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 _),
Some(Gc::new(
mc,
Multiname::new(activation.avm2().namespaces.public_all(), "uint"),
)),
)];
class.define_builtin_instance_properties(
mc,
activation.avm2().namespaces.public_all(),
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);

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

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;
Expand Down Expand Up @@ -79,15 +79,11 @@ pub fn create_i_class<'gc>(
),
);

const PUBLIC_INSTANCE_PROPERTIES: &[(
&str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
)] = &[("prototype", Some(prototype), None)];
let public_instance_properties = &[("prototype", Some(prototype as _), None, None)];
class_i_class.define_builtin_instance_properties(
gc_context,
namespaces.public_all(),
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);

class_i_class.mark_traits_loaded(activation.context.gc_context);
Expand Down
29 changes: 20 additions & 9 deletions core/src/avm2/globals/function.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! Function builtin and prototype

use gc_arena::Gc;

use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::error::eval_error;
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 @@ -258,18 +260,27 @@ 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 _),
None,
),
(
"length",
Some(length as _),
None,
Some(Gc::new(
gc_context,
Multiname::new(namespaces.public_all(), "int"),
)),
),
];
function_i_class.define_builtin_instance_properties(
gc_context,
namespaces.public_all(),
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);

const CONSTANTS_INT: &[(&str, i32)] = &[("length", 1)];
Expand Down
17 changes: 10 additions & 7 deletions core/src/avm2/globals/string.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! `String` impl

use gc_arena::Gc;

use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::error::make_error_1004;
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{primitive_allocator, FunctionObject, Object, TObject};
use crate::avm2::regexp::{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 @@ -705,15 +707,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,
Some(Gc::new(mc, Multiname::new(namespaces.public_all(), "int"))),
)];
class.define_builtin_instance_properties(
mc,
namespaces.public_all(),
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);
class.define_builtin_instance_methods(mc, namespaces.as3, PUBLIC_INSTANCE_AND_PROTO_METHODS);

Expand Down
29 changes: 20 additions & 9 deletions core/src/avm2/globals/vector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! `Vector` builtin/prototype

use gc_arena::Gc;

use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::error::{argument_error, type_error};
Expand All @@ -13,8 +15,8 @@ use crate::avm2::object::{
};
use crate::avm2::value::Value;
use crate::avm2::vector::VectorStorage;
use crate::avm2::Error;
use crate::avm2::QName;
use crate::avm2::{Error, Multiname};
use crate::string::AvmString;
use std::cmp::{max, min, Ordering};

Expand Down Expand Up @@ -963,18 +965,27 @@ pub fn create_builtin_class<'gc>(
Method::from_builtin(class_call, "<Vector.<T> call handler>", mc),
);

const PUBLIC_INSTANCE_PROPERTIES: &[(
&str,
Option<NativeMethodImpl>,
Option<NativeMethodImpl>,
)] = &[
("length", Some(length), Some(set_length)),
("fixed", Some(fixed), Some(set_fixed)),
let public_instance_properties = &[
(
"length",
Some(length as _),
Some(set_length as _),
Some(Gc::new(mc, Multiname::new(namespaces.public_all(), "uint"))),
),
(
"fixed",
Some(fixed as _),
Some(set_fixed as _),
Some(Gc::new(
mc,
Multiname::new(namespaces.public_all(), "Boolean"),
)),
),
];
class.define_builtin_instance_properties(
mc,
namespaces.public_all(),
PUBLIC_INSTANCE_PROPERTIES,
public_instance_properties,
);

const AS3_INSTANCE_METHODS: &[(&str, NativeMethodImpl)] = &[
Expand Down