Skip to content

Commit

Permalink
avm2: Add a default implementation of TObject::value_of
Browse files Browse the repository at this point in the history
Most native classes simply return themselves as an `Value::Object` when
`value_of` is called, so this default behavior can be moved to the trait itself.
  • Loading branch information
moulins authored and torokati44 committed Oct 3, 2024
1 parent 13beb4d commit a51c76e
Show file tree
Hide file tree
Showing 36 changed files with 25 additions and 175 deletions.
2 changes: 1 addition & 1 deletion core/src/avm2/multiname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl<'gc> Multiname<'gc> {
None => WStr::empty(),
};

uri.push_str(&ns);
uri.push_str(ns);

if let Some(name) = self.name {
if !uri.is_empty() {
Expand Down
7 changes: 6 additions & 1 deletion core/src/avm2/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,12 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
///
/// `valueOf` is a method used to request an object be coerced to a
/// primitive value. Typically, this would be a number of some kind.
fn value_of(&self, mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>>;
///
/// The default implementation wraps the object in a `Value`, using the
/// `Into<Object<'gc>>` implementation.
fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object((*self).into()))
}

/// Determine if this object is an instance of a given type.
///
Expand Down
4 changes: 0 additions & 4 deletions core/src/avm2/object/array_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,6 @@ impl<'gc> TObject<'gc> for ArrayObject<'gc> {
|| self.base().property_is_enumerable(name)
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_array_object(&self) -> Option<ArrayObject<'gc>> {
Some(*self)
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/avm2/object/bitmapdata_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::avm2::activation::Activation;
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{ClassObject, Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::bitmap::bitmap_data::BitmapDataWrapper;
use core::fmt;
Expand Down Expand Up @@ -115,10 +114,6 @@ impl<'gc> TObject<'gc> for BitmapDataObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_bitmap_data(&self) -> Option<BitmapDataWrapper<'gc>> {
self.0.bitmap_data.get()
}
Expand Down
6 changes: 1 addition & 5 deletions core/src/avm2/object/bytearray_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::avm2::Error;
use crate::avm2::Multiname;
use crate::character::Character;
use core::fmt;
use gc_arena::{Collect, Gc, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcWeak};
use std::cell::{Ref, RefCell, RefMut};

/// A class instance allocator that allocates ByteArray objects.
Expand Down Expand Up @@ -221,10 +221,6 @@ impl<'gc> TObject<'gc> for ByteArrayObject<'gc> {
self.base().has_own_property(name)
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_bytearray(&self) -> Option<Ref<ByteArrayStorage>> {
Some(self.0.storage.borrow())
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/avm2/object/class_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,6 @@ impl<'gc> TObject<'gc> for ClassObject<'gc> {
self.to_string(activation)
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn call(
self,
_receiver: Value<'gc>,
Expand Down
6 changes: 1 addition & 5 deletions core/src/avm2/object/context3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::avm2::Error;
use crate::avm2_stub_method;
use crate::bitmap::bitmap_data::BitmapData;
use crate::context::RenderContext;
use gc_arena::{Collect, Gc, GcCell, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcCell, GcWeak};
use ruffle_render::backend::{
BufferUsage, Context3D, Context3DBlendFactor, Context3DCommand, Context3DCompareMode,
Context3DTextureFormat, Context3DTriangleFace, Context3DVertexBufferFormat, ProgramType,
Expand Down Expand Up @@ -507,10 +507,6 @@ impl<'gc> TObject<'gc> for Context3DObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_context_3d(&self) -> Option<Context3DObject<'gc>> {
Some(*self)
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/avm2/object/dictionary_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ impl<'gc> TObject<'gc> for DictionaryObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Object::from(*self).into())
}

fn as_dictionary_object(self) -> Option<DictionaryObject<'gc>> {
Some(self)
}
Expand Down
7 changes: 0 additions & 7 deletions core/src/avm2/object/domain_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::avm2::activation::Activation;
use crate::avm2::domain::Domain;
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{ClassObject, Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use core::fmt;
use gc_arena::barrier::unlock;
Expand Down Expand Up @@ -109,10 +108,4 @@ impl<'gc> TObject<'gc> for DomainObject<'gc> {
fn init_application_domain(&self, mc: &Mutation<'gc>, domain: Domain<'gc>) {
unlock!(Gc::write(mc, self.0), DomainObjectData, domain).set(domain);
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
let this: Object<'gc> = Object::DomainObject(*self);

Ok(this.into())
}
}
6 changes: 1 addition & 5 deletions core/src/avm2/object/error_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::string::WString;
use core::fmt;
use gc_arena::{Collect, Gc, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcWeak};
use std::fmt::Debug;
use tracing::{enabled, Level};

Expand Down Expand Up @@ -129,10 +129,6 @@ impl<'gc> TObject<'gc> for ErrorObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_error_object(&self) -> Option<ErrorObject<'gc>> {
Some(*self)
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/avm2/object/event_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,6 @@ impl<'gc> TObject<'gc> for EventObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object((*self).into()))
}

fn as_event(&self) -> Option<Ref<Event<'gc>>> {
Some(self.0.event.borrow())
}
Expand Down
7 changes: 1 addition & 6 deletions core/src/avm2/object/file_reference_object.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{ClassObject, Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::{Activation, Error};
use crate::backend::ui::FileDialogResult;
use gc_arena::GcWeak;
use gc_arena::{Collect, Gc};
use gc_arena::{GcWeak, Mutation};
use std::cell::{Cell, Ref, RefCell};
use std::fmt;

Expand Down Expand Up @@ -46,10 +45,6 @@ impl<'gc> TObject<'gc> for FileReferenceObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_file_reference(&self) -> Option<FileReferenceObject<'gc>> {
Some(*self)
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/avm2/object/font_object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::{Activation, ClassObject, Error};
use crate::character::Character;
use crate::font::Font;
Expand Down Expand Up @@ -75,10 +74,6 @@ impl<'gc> TObject<'gc> for FontObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_font(&self) -> Option<Font<'gc>> {
self.0.font
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/avm2/object/function_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ impl<'gc> TObject<'gc> for FunctionObject<'gc> {
self.to_string(activation)
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_executable(&self) -> Option<Ref<BoundMethod<'gc>>> {
Some(self.0.exec.borrow())
}
Expand Down
7 changes: 1 addition & 6 deletions core/src/avm2/object/index_buffer_3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use crate::avm2::activation::Activation;
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use gc_arena::{Collect, Gc, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcWeak};
use ruffle_render::backend::IndexBuffer;
use std::cell::{Cell, RefCell, RefMut};

Expand Down Expand Up @@ -92,10 +91,6 @@ impl<'gc> TObject<'gc> for IndexBuffer3DObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_index_buffer(&self) -> Option<IndexBuffer3DObject<'gc>> {
Some(*self)
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/avm2/object/loaderinfo_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::avm2::activation::Activation;
use crate::avm2::error::argument_error;
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{ClassObject, Object, ObjectPtr, StageObject, TObject};
use crate::avm2::value::Value;
use crate::avm2::Avm2;
use crate::avm2::Error;
use crate::avm2::EventObject;
Expand Down Expand Up @@ -411,10 +410,6 @@ impl<'gc> TObject<'gc> for LoaderInfoObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object((*self).into()))
}

fn as_loader_info_object(&self) -> Option<&LoaderInfoObject<'gc>> {
Some(self)
}
Expand Down
6 changes: 1 addition & 5 deletions core/src/avm2/object/local_connection_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::local_connection::{LocalConnectionHandle, LocalConnections};
use crate::string::AvmString;
use core::fmt;
use flash_lso::types::Value as AmfValue;
use gc_arena::{Collect, Gc, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcWeak};
use std::cell::RefCell;

/// A class instance allocator that allocates LocalConnection objects.
Expand Down Expand Up @@ -161,10 +161,6 @@ impl<'gc> TObject<'gc> for LocalConnectionObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object((*self).into()))
}

fn as_local_connection_object(&self) -> Option<LocalConnectionObject<'gc>> {
Some(*self)
}
Expand Down
7 changes: 1 addition & 6 deletions core/src/avm2/object/net_connection_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use crate::avm2::activation::Activation;
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{ClassObject, Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::net_connection::NetConnectionHandle;
use gc_arena::{Collect, Gc, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcWeak};
use std::cell::Cell;
use std::fmt;
use std::fmt::Debug;
Expand Down Expand Up @@ -63,10 +62,6 @@ impl<'gc> TObject<'gc> for NetConnectionObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_net_connection(self) -> Option<NetConnectionObject<'gc>> {
Some(self)
}
Expand Down
7 changes: 1 addition & 6 deletions core/src/avm2/object/netstream_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use crate::avm2::activation::Activation;
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{ClassObject, Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::streams::NetStream;
use gc_arena::{Collect, Gc, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcWeak};
use std::fmt::Debug;

pub fn netstream_allocator<'gc>(
Expand Down Expand Up @@ -63,10 +62,6 @@ impl<'gc> TObject<'gc> for NetStreamObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object((*self).into()))
}

fn as_netstream(self) -> Option<NetStream<'gc>> {
Some(self.0.ns)
}
Expand Down
7 changes: 1 addition & 6 deletions core/src/avm2/object/program_3d_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use crate::avm2::activation::Activation;
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use gc_arena::{Collect, Gc, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcWeak};
use ruffle_render::backend::ShaderModule;
use std::cell::RefCell;
use std::rc::Rc;
Expand Down Expand Up @@ -82,10 +81,6 @@ impl<'gc> TObject<'gc> for Program3DObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_program_3d(&self) -> Option<Program3DObject<'gc>> {
Some(*self)
}
Expand Down
6 changes: 1 addition & 5 deletions core/src/avm2/object/proxy_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::Multiname;
use core::fmt;
use gc_arena::{Collect, Gc, GcWeak, Mutation};
use gc_arena::{Collect, Gc, GcWeak};

/// A class instance allocator that allocates Proxy objects.
pub fn proxy_allocator<'gc>(
Expand Down Expand Up @@ -65,10 +65,6 @@ impl<'gc> TObject<'gc> for ProxyObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Object::from(*self).into())
}

fn get_property_local(
self,
multiname: &Multiname<'gc>,
Expand Down
4 changes: 0 additions & 4 deletions core/src/avm2/object/qname_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ impl<'gc> TObject<'gc> for QNameObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn as_qname_object(self) -> Option<QNameObject<'gc>> {
Some(self)
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/avm2/object/responder_object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::avm2::object::script_object::ScriptObjectData;
use crate::avm2::object::{ClassObject, FunctionObject, Object, ObjectPtr, TObject};
use crate::avm2::value::Value;
use crate::avm2::{Activation, Error};
use crate::context::UpdateContext;
use crate::net_connection::ResponderCallback;
Expand Down Expand Up @@ -48,10 +47,6 @@ impl<'gc> TObject<'gc> for ResponderObject<'gc> {
Gc::as_ptr(self.0) as *const ObjectPtr
}

fn value_of(&self, _mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object((*self).into()))
}

fn as_responder(self) -> Option<ResponderObject<'gc>> {
Some(self)
}
Expand Down
Loading

0 comments on commit a51c76e

Please sign in to comment.