2019-10-25 03:21:35 +00:00
|
|
|
//! Object trait to expose objects to AVM
|
|
|
|
|
|
|
|
use crate::avm1::function::Executable;
|
2019-10-21 22:37:04 +00:00
|
|
|
use crate::avm1::return_value::ReturnValue;
|
2019-10-25 03:21:35 +00:00
|
|
|
use crate::avm1::{Avm1, Error, ScriptObject, UpdateContext, Value};
|
2019-08-28 23:29:43 +00:00
|
|
|
use crate::display_object::DisplayNode;
|
2019-10-25 03:21:35 +00:00
|
|
|
use gc_arena::{Collect, GcCell};
|
|
|
|
use std::fmt::Debug;
|
2019-08-26 22:53:50 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
pub type ObjectCell<'gc> = GcCell<'gc, Box<dyn Object<'gc> + 'gc>>;
|
2019-08-28 23:29:43 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Represents an object that can be directly interacted with by the AVM
|
|
|
|
/// runtime.
|
|
|
|
pub trait Object<'gc>: 'gc + Collect + Debug {
|
|
|
|
/// Retrieve a named property from the object.
|
2019-10-06 21:46:49 +00:00
|
|
|
///
|
2019-10-25 03:21:35 +00:00
|
|
|
/// This function takes a redundant `this` parameter which should be
|
|
|
|
/// the object's own `GcCell`, so that it can pass it to user-defined
|
|
|
|
/// overrides that may need to interact with the underlying object.
|
|
|
|
fn get(
|
|
|
|
&self,
|
2019-09-27 13:46:53 +00:00
|
|
|
name: &str,
|
|
|
|
avm: &mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
this: ObjectCell<'gc>,
|
|
|
|
) -> Result<ReturnValue<'gc>, Error>;
|
2019-09-02 20:19:09 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Set a named property on the object.
|
2019-10-23 18:15:46 +00:00
|
|
|
///
|
2019-10-25 03:21:35 +00:00
|
|
|
/// This function takes a redundant `this` parameter which should be
|
|
|
|
/// the object's own `GcCell`, so that it can pass it to user-defined
|
|
|
|
/// overrides that may need to interact with the underlying object.
|
|
|
|
fn set(
|
2019-09-27 13:46:53 +00:00
|
|
|
&mut self,
|
|
|
|
name: &str,
|
2019-10-25 03:21:35 +00:00
|
|
|
value: Value<'gc>,
|
2019-09-27 13:46:53 +00:00
|
|
|
avm: &mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
this: ObjectCell<'gc>,
|
|
|
|
) -> Result<(), Error>;
|
2019-08-30 17:57:52 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Call the underlying object.
|
|
|
|
///
|
|
|
|
/// This function takes a redundant `this` parameter which should be
|
|
|
|
/// the object's own `GcCell`, so that it can pass it to user-defined
|
|
|
|
/// overrides that may need to interact with the underlying object.
|
|
|
|
fn call(
|
2019-08-28 23:29:43 +00:00
|
|
|
&self,
|
2019-09-03 00:44:24 +00:00
|
|
|
avm: &mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
this: ObjectCell<'gc>,
|
2019-08-28 23:29:43 +00:00
|
|
|
args: &[Value<'gc>],
|
2019-10-25 03:21:35 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error>;
|
2019-08-31 12:09:37 +00:00
|
|
|
|
2019-10-25 18:35:07 +00:00
|
|
|
/// Construct a host object of some kind and return it's cell.
|
|
|
|
///
|
|
|
|
/// This is called on constructor functions to obtain a given host object.
|
|
|
|
/// The returned object will then be passed to `call` in order to run
|
|
|
|
/// initialization code. This function cannot be user-defined (and thus,
|
|
|
|
/// does not yield a ReturnValue).
|
|
|
|
///
|
|
|
|
/// The arguments passed to the constructor are provided here; however, all
|
|
|
|
/// object construction should happen in `call`, not `new`. `new` exists
|
|
|
|
/// purely so that host objects can be constructed by the VM.
|
|
|
|
fn new(
|
|
|
|
&self,
|
|
|
|
avm: &mut Avm1<'gc>,
|
|
|
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
|
|
|
this: ObjectCell<'gc>,
|
|
|
|
args: &[Value<'gc>],
|
|
|
|
) -> Result<ObjectCell<'gc>, Error>;
|
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Delete a named property from the object.
|
2019-10-19 03:20:43 +00:00
|
|
|
///
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Returns false if the property cannot be deleted.
|
|
|
|
fn delete(&mut self, name: &str) -> bool;
|
2019-10-08 13:24:57 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Checks if the object has a given named property.
|
|
|
|
fn has_property(&self, name: &str) -> bool;
|
2019-10-19 02:53:29 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Checks if the object has a given named property on itself (and not,
|
|
|
|
/// say, the object's prototype or superclass)
|
|
|
|
fn has_own_property(&self, name: &str) -> bool;
|
2019-09-27 13:46:53 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Checks if a named property can be overwritten.
|
|
|
|
fn is_property_overwritable(&self, name: &str) -> bool;
|
2019-09-27 13:46:53 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Checks if a named property appears when enumerating the object.
|
|
|
|
fn is_property_enumerable(&self, name: &str) -> bool;
|
2019-10-08 12:21:07 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Enumerate the object.
|
|
|
|
fn get_keys(&self) -> Vec<String>;
|
2019-10-08 12:21:07 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Coerce the object into a string.
|
|
|
|
fn as_string(&self) -> String;
|
2019-10-08 12:21:07 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Get the object's type string.
|
|
|
|
fn type_of(&self) -> &'static str;
|
2019-10-08 12:21:07 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Get the underlying script object, if it exists.
|
|
|
|
fn as_script_object(&self) -> Option<&ScriptObject<'gc>>;
|
2019-10-08 14:34:08 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Get the underlying script object, if it exists.
|
|
|
|
fn as_script_object_mut(&mut self) -> Option<&mut ScriptObject<'gc>>;
|
2019-10-08 14:34:08 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Get the underlying display node for this object, if it exists.
|
|
|
|
fn as_display_node(&self) -> Option<DisplayNode<'gc>>;
|
2019-10-08 14:34:08 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
/// Get the underlying executable for this object, if it exists.
|
|
|
|
fn as_executable(&self) -> Option<Executable<'gc>>;
|
2019-09-27 13:46:53 +00:00
|
|
|
}
|