Skip to content

Commit

Permalink
avm2: Fix get_own_class_definition users to use either instance_of or…
Browse files Browse the repository at this point in the history
… as_.
  • Loading branch information
adrian17 committed Sep 18, 2021
1 parent 392e19e commit 8de7c55
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 51 deletions.
10 changes: 5 additions & 5 deletions core/src/avm2/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
.ok_or_else(|| format!("Could not resolve parameter type {:?}", type_name))?
.coerce_to_object(self)?;

if class.get_own_class_definition().is_none() {
if class.as_class_object().is_none() {
return Err(format!("Resolved parameter type {:?} is not a class", type_name).into());
}

Expand Down Expand Up @@ -1271,7 +1271,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
// dynamic properties not yet set
if name.is_err()
&& !object
.get_own_class_definition()
.instance_of_class_definition()
.map(|c| c.read().is_sealed())
.unwrap_or(false)
{
Expand Down Expand Up @@ -1349,7 +1349,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
// Unknown properties on a dynamic class delete successfully.
self.context.avm2.push(
!object
.get_own_class_definition()
.instance_of_class_definition()
.map(|c| c.read().is_sealed())
.unwrap_or(false),
)
Expand Down Expand Up @@ -2506,7 +2506,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
});
let class = found?.coerce_to_object(self)?;

if class.get_own_class_definition().is_none() {
if class.as_class_object().is_none() {
return Err("TypeError: The right-hand side of operator must be a class.".into());
}

Expand All @@ -2523,7 +2523,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
let class = self.context.avm2.pop().coerce_to_object(self)?;
let value = self.context.avm2.pop().coerce_to_object(self)?;

if class.get_own_class_definition().is_none() {
if class.as_class_object().is_none() {
return Err("TypeError: The right-hand side of operator must be a class.".into());
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/avm2/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ fn dynamic_class<'gc>(
script: Script<'gc>,
) -> Result<(), Error> {
let class = class_object
.get_own_class_definition()
.as_class_definition()
.ok_or("Attempted to create builtin dynamic class without class on it's constructor!")?;
let name = class.read().name().clone();

Expand Down Expand Up @@ -464,23 +464,23 @@ pub fn load_player_globals<'gc>(
object_class.install_traits(
activation,
object_class
.get_own_class_definition()
.as_class_definition()
.unwrap()
.read()
.class_traits(),
)?;
function_class.install_traits(
activation,
function_class
.get_own_class_definition()
.as_class_definition()
.unwrap()
.read()
.class_traits(),
)?;
class_class.install_traits(
activation,
class_class
.get_own_class_definition()
.as_class_definition()
.unwrap()
.read()
.class_traits(),
Expand Down
3 changes: 1 addition & 2 deletions core/src/avm2/globals/flash/display/bitmapdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub fn instance_init<'gc>(
activation.super_init(this, &[])?;

let name = this
.instance_of()
.and_then(|t| t.get_own_class_definition())
.instance_of_class_definition()
.map(|c| c.read().name().clone());
let character = this
.instance_of()
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/globals/flash/media/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn instance_init<'gc>(
{
this.set_sound(activation.context.gc_context, *sound);
} else {
log::warn!("Attempted to construct subclass of Sound, {}, which is associated with non-Sound character {}", class_object.get_own_class_definition().expect("Class object is also a class").read().name().local_name(), symbol);
log::warn!("Attempted to construct subclass of Sound, {}, which is associated with non-Sound character {}", class_object.as_class_definition().expect("Class object is also a class").read().name().local_name(), symbol);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/globals/flash/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn get_qualified_class_name<'gc>(
Ok(AvmString::new(
activation.context.gc_context,
class
.get_own_class_definition()
.as_class_definition()
.ok_or("This object does not have a class")?
.read()
.name()
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn get_qualified_super_class_name<'gc>(
Ok(AvmString::new(
activation.context.gc_context,
super_class
.get_own_class_definition()
.as_class_definition()
.ok_or("This object does not have a class")?
.read()
.name()
Expand Down
16 changes: 8 additions & 8 deletions core/src/avm2/globals/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn class_init<'gc>(
let int_vector_class = this.apply(activation, &[int_class.into()])?;
let int_vector_name = QName::new(Namespace::internal(NS_VECTOR), "Vector$int");
int_vector_class
.get_own_class_definition()
.as_class_definition()
.unwrap()
.write(activation.context.gc_context)
.set_name(int_vector_name.clone());
Expand All @@ -85,7 +85,7 @@ pub fn class_init<'gc>(
let uint_vector_class = this.apply(activation, &[uint_class.into()])?;
let uint_vector_name = QName::new(Namespace::internal(NS_VECTOR), "Vector$uint");
uint_vector_class
.get_own_class_definition()
.as_class_definition()
.unwrap()
.write(activation.context.gc_context)
.set_name(uint_vector_name.clone());
Expand All @@ -103,7 +103,7 @@ pub fn class_init<'gc>(
let number_vector_class = this.apply(activation, &[number_class.into()])?;
let number_vector_name = QName::new(Namespace::internal(NS_VECTOR), "Vector$double");
number_vector_class
.get_own_class_definition()
.as_class_definition()
.unwrap()
.write(activation.context.gc_context)
.set_name(number_vector_name.clone());
Expand All @@ -120,7 +120,7 @@ pub fn class_init<'gc>(
let object_vector_class = this.apply(activation, &[Value::Null])?;
let object_vector_name = QName::new(Namespace::internal(NS_VECTOR), "Vector$object");
object_vector_class
.get_own_class_definition()
.as_class_definition()
.unwrap()
.write(activation.context.gc_context)
.set_name(object_vector_name.clone());
Expand Down Expand Up @@ -282,14 +282,14 @@ pub fn concat<'gc>(
for arg in args.iter().map(|a| a.clone()) {
let arg_obj = arg.coerce_to_object(activation)?;
let arg_class = arg_obj
.get_own_class_definition()
.instance_of_class_definition()
.ok_or("TypeError: Tried to concat from a bare object")?;
if !arg.is_of_type(activation, my_class)? {
return Err(format!(
"TypeError: Cannot coerce argument of type {:?} to argument of type {:?}",
arg_class.read().name(),
my_class
.get_own_class_definition()
.as_class_definition()
.ok_or("TypeError: Tried to concat into a bare object")?
.read()
.name()
Expand All @@ -308,13 +308,13 @@ pub fn concat<'gc>(
if let Ok(val_obj) = val.coerce_to_object(activation) {
if !val.is_of_type(activation, val_class)? {
let other_val_class = val_obj
.get_own_class_definition()
.instance_of_class_definition()
.ok_or("TypeError: Tried to concat a bare object into a Vector")?;
return Err(format!(
"TypeError: Cannot coerce Vector value of type {:?} to type {:?}",
other_val_class.read().name(),
val_class
.get_own_class_definition()
.as_class_definition()
.ok_or("TypeError: Tried to concat into a bare object")?
.read()
.name()
Expand Down
37 changes: 20 additions & 17 deletions core/src/avm2/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
/// defined prototype methods for ES3-style classes.
fn find_class_for_trait(self, name: &QName<'gc>) -> Result<Option<Object<'gc>>, Error> {
let class = self
.get_own_class_definition()
.as_class_definition()
.ok_or("Cannot get base traits on non-class object")?;

if class.read().has_instance_trait(name) {
Expand Down Expand Up @@ -436,7 +436,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
}
}

if let Some(class) = from_class_object.get_own_class_definition() {
if let Some(class) = from_class_object.as_class_definition() {
self.install_traits(activation, class.read().instance_traits())?;
}

Expand Down Expand Up @@ -701,7 +701,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
})?;
let mut class_traits = Vec::new();
superclass_object
.get_own_class_definition()
.as_class_definition()
.unwrap()
.read()
.lookup_instance_traits(name, &mut class_traits)?;
Expand Down Expand Up @@ -767,7 +767,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
})?;
let mut class_traits = Vec::new();
superclass_object
.get_own_class_definition()
.as_class_definition()
.unwrap()
.read()
.lookup_instance_traits(name, &mut class_traits)?;
Expand Down Expand Up @@ -827,7 +827,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
})?;
let mut class_traits = Vec::new();
superclass_object
.get_own_class_definition()
.as_class_definition()
.unwrap()
.read()
.lookup_instance_traits(name, &mut class_traits)?;
Expand Down Expand Up @@ -926,7 +926,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
/// coercions.
fn to_string(&self, mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
let class_name = self
.get_own_class_definition()
.instance_of_class_definition()
.map(|c| c.read().name().local_name())
.unwrap_or_else(|| "Object".into());

Expand All @@ -943,7 +943,7 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
/// of the class that created this object).
fn to_locale_string(&self, mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
let class_name = self
.get_own_class_definition()
.instance_of_class_definition()
.map(|c| c.read().name().local_name())
.unwrap_or_else(|| "Object".into());

Expand Down Expand Up @@ -1085,22 +1085,25 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
/// Get a raw pointer value for this object.
fn as_ptr(&self) -> *const ObjectPtr;

/// Get this object's `Class`, if it has one.
/// This this object is already a ClassObject, return its own Class.
/// Note: this probably shouldn't be used in most cases.
fn get_own_class_definition(&self) -> Option<GcCell<'gc, Class<'gc>>> {
let class = self
.as_class_object()
.or_else(|| self.instance_of().and_then(|cls| cls.as_class_object()));
class.map(|cls| cls.inner_class_definition())
}

/// Get this object's class, if it has one.
fn instance_of(&self) -> Option<Object<'gc>>;

/// Get this object's class's `Class`, if it has one.
fn instance_of_class_definition(&self) -> Option<GcCell<'gc, Class<'gc>>> {
self.instance_of().and_then(|cls| cls.as_class_definition())
}

/// Try to corece this object into a `ClassObject`.
fn as_class_object(&self) -> Option<ClassObject<'gc>> {
None
}

/// Get this object's `Class`, if it's a `ClassObject`.
fn as_class_definition(&self) -> Option<GcCell<'gc, Class<'gc>>> {
self.as_class_object()
.map(|cls| cls.inner_class_definition())
}

/// Get this object's `Executable`, if it has one.
fn as_executable(&self) -> Option<Executable<'gc>> {
None
Expand Down
8 changes: 4 additions & 4 deletions core/src/avm2/object/class_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<'gc> ClassObject<'gc> {
superclass_object: Option<Object<'gc>>,
scope: Option<GcCell<'gc, Scope<'gc>>>,
) -> Result<Object<'gc>, Error> {
if let Some(base_class) = superclass_object.and_then(|b| b.get_own_class_definition()) {
if let Some(base_class) = superclass_object.and_then(|b| b.as_class_definition()) {
if base_class.read().is_final() {
return Err(format!(
"Base class {:?} is final and cannot be extended",
Expand Down Expand Up @@ -294,7 +294,7 @@ impl<'gc> ClassObject<'gc> {
}

let interface = interface.unwrap().coerce_to_object(activation)?;
if let Some(class) = interface.get_own_class_definition() {
if let Some(class) = interface.as_class_definition() {
if !class.read().is_interface() {
return Err(format!(
"Class {:?} is not an interface and cannot be implemented by classes",
Expand Down Expand Up @@ -530,7 +530,7 @@ impl<'gc> TObject<'gc> for ClassObject<'gc> {
nullable_params: &[Value<'gc>],
) -> Result<Object<'gc>, Error> {
let self_class = self
.get_own_class_definition()
.as_class_definition()
.ok_or("Attempted to apply type arguments to non-class!")?;

if !self_class.read().is_generic() {
Expand Down Expand Up @@ -570,7 +570,7 @@ impl<'gc> TObject<'gc> for ClassObject<'gc> {
class_params.push(
param
.unwrap_or(activation.avm2().classes().object)
.get_own_class_definition()
.as_class_definition()
.ok_or(format!(
"Cannot apply class {:?} with non-class parameter",
self_class.read().name()
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/object/loaderinfo_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'gc> TObject<'gc> for LoaderInfoObject<'gc> {
impl_avm2_custom_object_instance!(base);

fn value_of(&self, mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
if let Some(class) = self.get_own_class_definition() {
if let Some(class) = self.instance_of_class_definition() {
Ok(AvmString::new(mc, format!("[object {}]", class.read().name().local_name())).into())
} else {
Ok("[object Object]".into())
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/object/primitive_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'gc> TObject<'gc> for PrimitiveObject<'gc> {
val @ Value::Integer(_) | val @ Value::Unsigned(_) => Ok(val),
_ => {
let class_name = self
.get_own_class_definition()
.instance_of_class_definition()
.map(|c| c.read().name().local_name())
.unwrap_or_else(|| "Object".into());

Expand Down
8 changes: 4 additions & 4 deletions core/src/avm2/object/script_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<'gc> TObject<'gc> for ScriptObject<'gc> {
}

fn to_string(&self, mc: MutationContext<'gc, '_>) -> Result<Value<'gc>, Error> {
if let Some(class) = self.get_own_class_definition() {
if let Some(class) = self.instance_of_class_definition() {
Ok(AvmString::new(mc, format!("[object {}]", class.read().name().local_name())).into())
} else {
Ok("[object Object]".into())
Expand Down Expand Up @@ -512,7 +512,7 @@ impl<'gc> ScriptObjectData<'gc> {

while let Some(class) = cur_class {
let cur_static_class = class
.get_own_class_definition()
.as_class_definition()
.ok_or("Object is not a class constructor")?;
if cur_static_class.read().has_instance_trait(name) {
return Ok(true);
Expand Down Expand Up @@ -573,7 +573,7 @@ impl<'gc> ScriptObjectData<'gc> {

while let Some(class) = cur_class {
let cur_static_class = class
.get_own_class_definition()
.as_class_definition()
.ok_or("Object is not a class constructor")?;
if let Some(ns) = cur_static_class
.read()
Expand Down Expand Up @@ -761,7 +761,7 @@ impl<'gc> ScriptObjectData<'gc> {
value: Value<'gc>,
) -> Result<(), Error> {
if let Some(class) = self.instance_of() {
if let Some(class) = class.get_own_class_definition() {
if let Some(class) = class.as_class_definition() {
if class.read().is_sealed() {
return Err(format!(
"Objects of type {:?} are not dynamic",
Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ impl<'gc> Value<'gc> {
}
}

if let Some(static_class) = class.get_own_class_definition() {
if let Some(static_class) = class.as_class_definition() {
return Err(format!(
"Cannot coerce {:?} to an {:?}",
self,
Expand Down Expand Up @@ -671,7 +671,7 @@ impl<'gc> Value<'gc> {
activation: &mut Activation<'_, 'gc, '_>,
type_object: Object<'gc>,
) -> Result<bool, Error> {
if let Some(type_class) = type_object.get_own_class_definition() {
if let Some(type_class) = type_object.as_class_definition() {
if type_class.read().name() == &QName::new(Namespace::public(), "Number") {
return Ok(self.is_number());
}
Expand Down

0 comments on commit 8de7c55

Please sign in to comment.