2019-10-08 18:35:23 +00:00
|
|
|
use self::Attribute::*;
|
2019-10-23 18:15:46 +00:00
|
|
|
use crate::avm1::function::{Executable, NativeFunction};
|
2019-10-21 22:37:04 +00:00
|
|
|
use crate::avm1::return_value::ReturnValue;
|
2019-10-26 03:21:14 +00:00
|
|
|
use crate::avm1::{Avm1, Error, UpdateContext, Value};
|
2019-08-28 23:29:43 +00:00
|
|
|
use crate::display_object::DisplayNode;
|
|
|
|
use core::fmt;
|
2019-10-08 12:21:07 +00:00
|
|
|
use enumset::{EnumSet, EnumSetType};
|
2019-08-28 23:29:43 +00:00
|
|
|
use gc_arena::{GcCell, MutationContext};
|
2019-09-27 13:46:53 +00:00
|
|
|
use std::collections::hash_map::Entry;
|
2019-08-26 22:53:50 +00:00
|
|
|
use std::collections::HashMap;
|
2019-09-27 13:46:53 +00:00
|
|
|
use std::mem::replace;
|
2019-08-26 22:53:50 +00:00
|
|
|
|
2019-08-31 12:09:37 +00:00
|
|
|
pub const TYPE_OF_OBJECT: &str = "object";
|
|
|
|
pub const TYPE_OF_FUNCTION: &str = "function";
|
|
|
|
pub const TYPE_OF_MOVIE_CLIP: &str = "movieclip";
|
|
|
|
|
2019-10-08 12:21:07 +00:00
|
|
|
#[derive(EnumSetType, Debug)]
|
|
|
|
pub enum Attribute {
|
|
|
|
DontDelete,
|
2019-10-08 14:34:08 +00:00
|
|
|
DontEnum,
|
2019-10-08 13:24:57 +00:00
|
|
|
ReadOnly,
|
2019-10-08 12:21:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 13:46:53 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum Property<'gc> {
|
|
|
|
Virtual {
|
2019-10-19 02:53:29 +00:00
|
|
|
get: Executable<'gc>,
|
|
|
|
set: Option<Executable<'gc>>,
|
2019-10-08 12:21:07 +00:00
|
|
|
attributes: EnumSet<Attribute>,
|
2019-09-27 13:46:53 +00:00
|
|
|
},
|
|
|
|
Stored {
|
|
|
|
value: Value<'gc>,
|
2019-10-08 12:21:07 +00:00
|
|
|
attributes: EnumSet<Attribute>,
|
2019-09-27 13:46:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'gc> Property<'gc> {
|
2019-10-19 02:53:29 +00:00
|
|
|
/// Get the value of a property slot.
|
|
|
|
///
|
|
|
|
/// This function yields `None` if the value is being determined on the AVM
|
|
|
|
/// stack. Otherwise, if the value can be determined on the Rust stack,
|
|
|
|
/// then this function returns the value.
|
2019-09-27 13:46:53 +00:00
|
|
|
pub fn get(
|
|
|
|
&self,
|
|
|
|
avm: &mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-09-27 13:46:53 +00:00
|
|
|
this: GcCell<'gc, Object<'gc>>,
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
2019-09-27 13:46:53 +00:00
|
|
|
match self {
|
2019-10-19 02:53:29 +00:00
|
|
|
Property::Virtual { get, .. } => get.exec(avm, context, this, &[]),
|
2019-10-31 00:25:52 +00:00
|
|
|
Property::Stored { value, .. } => Ok(value.to_owned().into()),
|
2019-09-27 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 02:53:29 +00:00
|
|
|
/// Set a property slot.
|
|
|
|
///
|
|
|
|
/// This function returns `true` if the set has completed, or `false` if
|
|
|
|
/// it has not yet occured. If `false`, and you need to run code after the
|
2019-10-31 03:42:19 +00:00
|
|
|
/// set has occured, you must recursively execute the top-most frame via
|
|
|
|
/// `run_current_frame`.
|
2019-09-27 13:46:53 +00:00
|
|
|
pub fn set(
|
|
|
|
&mut self,
|
|
|
|
avm: &mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-09-27 13:46:53 +00:00
|
|
|
this: GcCell<'gc, Object<'gc>>,
|
2019-10-21 10:48:18 +00:00
|
|
|
new_value: impl Into<Value<'gc>>,
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<bool, Error> {
|
2019-09-27 13:46:53 +00:00
|
|
|
match self {
|
2019-10-10 03:09:52 +00:00
|
|
|
Property::Virtual { set, .. } => {
|
2019-09-27 13:46:53 +00:00
|
|
|
if let Some(function) = set {
|
2019-10-26 03:21:14 +00:00
|
|
|
let return_value = function.exec(avm, context, this, &[new_value.into()])?;
|
|
|
|
Ok(return_value.is_immediate())
|
2019-10-19 02:53:29 +00:00
|
|
|
} else {
|
2019-10-26 03:21:14 +00:00
|
|
|
Ok(true)
|
2019-09-27 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-08 14:34:08 +00:00
|
|
|
Property::Stored {
|
|
|
|
value, attributes, ..
|
|
|
|
} => {
|
2019-10-08 18:35:23 +00:00
|
|
|
if !attributes.contains(ReadOnly) {
|
2019-10-21 10:48:18 +00:00
|
|
|
replace::<Value<'gc>>(value, new_value.into());
|
2019-10-08 13:24:57 +00:00
|
|
|
}
|
2019-10-19 02:53:29 +00:00
|
|
|
|
2019-10-26 03:21:14 +00:00
|
|
|
Ok(true)
|
2019-09-27 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 12:21:07 +00:00
|
|
|
|
|
|
|
pub fn can_delete(&self) -> bool {
|
|
|
|
match self {
|
2019-10-08 18:35:23 +00:00
|
|
|
Property::Virtual { attributes, .. } => !attributes.contains(DontDelete),
|
|
|
|
Property::Stored { attributes, .. } => !attributes.contains(DontDelete),
|
2019-10-08 12:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-08 14:34:08 +00:00
|
|
|
|
|
|
|
pub fn is_enumerable(&self) -> bool {
|
|
|
|
match self {
|
2019-10-08 18:35:23 +00:00
|
|
|
Property::Virtual { attributes, .. } => !attributes.contains(DontEnum),
|
|
|
|
Property::Stored { attributes, .. } => !attributes.contains(DontEnum),
|
2019-10-08 14:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 03:00:37 +00:00
|
|
|
|
|
|
|
pub fn is_overwritable(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Property::Virtual {
|
|
|
|
attributes, set, ..
|
|
|
|
} => !attributes.contains(ReadOnly) && !set.is_none(),
|
|
|
|
Property::Stored { attributes, .. } => !attributes.contains(ReadOnly),
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<'gc> gc_arena::Collect for Property<'gc> {
|
|
|
|
fn trace(&self, cc: gc_arena::CollectionContext) {
|
|
|
|
match self {
|
2019-10-08 12:21:07 +00:00
|
|
|
Property::Virtual { get, set, .. } => {
|
2019-09-27 13:46:53 +00:00
|
|
|
get.trace(cc);
|
|
|
|
set.trace(cc);
|
|
|
|
}
|
|
|
|
Property::Stored { value, .. } => value.trace(cc),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Property<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2019-10-08 12:21:07 +00:00
|
|
|
Property::Virtual {
|
|
|
|
get: _,
|
|
|
|
set,
|
|
|
|
attributes,
|
|
|
|
} => f
|
2019-09-27 13:46:53 +00:00
|
|
|
.debug_struct("Property::Virtual")
|
|
|
|
.field("get", &true)
|
|
|
|
.field("set", &set.is_some())
|
2019-10-08 12:21:07 +00:00
|
|
|
.field("attributes", &attributes)
|
2019-09-27 13:46:53 +00:00
|
|
|
.finish(),
|
2019-10-08 12:21:07 +00:00
|
|
|
Property::Stored { value, attributes } => f
|
2019-09-27 13:46:53 +00:00
|
|
|
.debug_struct("Property::Stored")
|
|
|
|
.field("value", &value)
|
2019-10-08 12:21:07 +00:00
|
|
|
.field("attributes", &attributes)
|
2019-09-27 13:46:53 +00:00
|
|
|
.finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 12:09:37 +00:00
|
|
|
#[derive(Clone)]
|
2019-08-26 22:53:50 +00:00
|
|
|
pub struct Object<'gc> {
|
2019-08-30 17:57:52 +00:00
|
|
|
prototype: Option<GcCell<'gc, Object<'gc>>>,
|
2019-08-28 23:29:43 +00:00
|
|
|
display_node: Option<DisplayNode<'gc>>,
|
2019-09-27 13:46:53 +00:00
|
|
|
values: HashMap<String, Property<'gc>>,
|
2019-09-16 01:21:57 +00:00
|
|
|
function: Option<Executable<'gc>>,
|
2019-08-31 12:09:37 +00:00
|
|
|
type_of: &'static str,
|
2019-08-28 23:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<'gc> gc_arena::Collect for Object<'gc> {
|
|
|
|
fn trace(&self, cc: gc_arena::CollectionContext) {
|
2019-08-30 17:57:52 +00:00
|
|
|
self.prototype.trace(cc);
|
2019-08-28 23:29:43 +00:00
|
|
|
self.display_node.trace(cc);
|
|
|
|
self.values.trace(cc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Object<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("Object")
|
2019-08-30 17:57:52 +00:00
|
|
|
.field("prototype", &self.prototype)
|
2019-08-28 23:29:43 +00:00
|
|
|
.field("display_node", &self.display_node)
|
|
|
|
.field("values", &self.values)
|
|
|
|
.field("function", &self.function.is_some())
|
|
|
|
.finish()
|
|
|
|
}
|
2019-08-26 22:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'gc> Object<'gc> {
|
2019-10-17 02:31:41 +00:00
|
|
|
pub fn object(
|
|
|
|
_gc_context: MutationContext<'gc, '_>,
|
|
|
|
proto: Option<GcCell<'gc, Object<'gc>>>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
prototype: proto, //TODO: Should be Object
|
2019-08-31 12:09:37 +00:00
|
|
|
type_of: TYPE_OF_OBJECT,
|
|
|
|
display_node: None,
|
|
|
|
values: HashMap::new(),
|
|
|
|
function: None,
|
2019-10-17 02:31:41 +00:00
|
|
|
}
|
2019-08-28 23:29:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 04:04:38 +00:00
|
|
|
/// Constructs an object with no values, not even builtins.
|
2019-10-06 21:46:49 +00:00
|
|
|
///
|
2019-09-17 04:04:38 +00:00
|
|
|
/// Intended for constructing scope chains, since they exclusively use the
|
|
|
|
/// object values, but can't just have a hashmap because of `with` and
|
|
|
|
/// friends.
|
|
|
|
pub fn bare_object() -> Self {
|
|
|
|
Self {
|
2019-08-30 17:57:52 +00:00
|
|
|
prototype: None,
|
2019-09-17 04:04:38 +00:00
|
|
|
type_of: TYPE_OF_OBJECT,
|
|
|
|
display_node: None,
|
|
|
|
values: HashMap::new(),
|
|
|
|
function: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:15:46 +00:00
|
|
|
/// Construct a function sans prototype.
|
|
|
|
pub fn bare_function(
|
|
|
|
function: impl Into<Executable<'gc>>,
|
|
|
|
fn_proto: Option<GcCell<'gc, Object<'gc>>>,
|
2019-10-17 02:31:41 +00:00
|
|
|
) -> Self {
|
2019-08-26 22:53:50 +00:00
|
|
|
Self {
|
2019-10-23 18:15:46 +00:00
|
|
|
prototype: fn_proto,
|
2019-08-31 12:09:37 +00:00
|
|
|
type_of: TYPE_OF_FUNCTION,
|
2019-10-23 18:15:46 +00:00
|
|
|
function: Some(function.into()),
|
2019-08-31 12:09:37 +00:00
|
|
|
display_node: None,
|
|
|
|
values: HashMap::new(),
|
2019-08-26 22:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:15:46 +00:00
|
|
|
/// Construct a function from an executable and associated protos.
|
|
|
|
///
|
|
|
|
/// Since prototypes need to link back to themselves, this function builds
|
|
|
|
/// both objects itself and returns the function to you, fully allocated.
|
|
|
|
///
|
|
|
|
/// `fn_proto` refers to the implicit proto of the function object, and the
|
|
|
|
/// `prototype` refers to the explicit prototype of the function. If
|
|
|
|
/// provided, the function and it's prototype will be linked to each other.
|
|
|
|
pub fn function(
|
|
|
|
gc_context: MutationContext<'gc, '_>,
|
|
|
|
function: impl Into<Executable<'gc>>,
|
|
|
|
fn_proto: Option<GcCell<'gc, Object<'gc>>>,
|
|
|
|
prototype: Option<GcCell<'gc, Object<'gc>>>,
|
|
|
|
) -> GcCell<'gc, Object<'gc>> {
|
|
|
|
let function = GcCell::allocate(gc_context, Self::bare_function(function, fn_proto));
|
|
|
|
|
|
|
|
if let Some(p) = prototype {
|
|
|
|
p.write(gc_context)
|
|
|
|
.force_set("constructor", function, EnumSet::empty());
|
|
|
|
function
|
|
|
|
.write(gc_context)
|
|
|
|
.force_set("prototype", p, EnumSet::empty());
|
2019-09-29 03:11:03 +00:00
|
|
|
}
|
2019-10-23 18:15:46 +00:00
|
|
|
|
|
|
|
function
|
2019-09-29 03:11:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 23:29:43 +00:00
|
|
|
pub fn set_display_node(&mut self, display_node: DisplayNode<'gc>) {
|
|
|
|
self.display_node = Some(display_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn display_node(&self) -> Option<DisplayNode<'gc>> {
|
|
|
|
self.display_node
|
2019-08-26 22:53:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 13:46:53 +00:00
|
|
|
pub fn set(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
2019-10-21 10:48:18 +00:00
|
|
|
value: impl Into<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-09-27 13:46:53 +00:00
|
|
|
this: GcCell<'gc, Object<'gc>>,
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-08-30 17:57:52 +00:00
|
|
|
if name == "__proto__" {
|
|
|
|
self.prototype = value.into().as_object().ok().to_owned();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
match self.values.entry(name.to_owned()) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
entry.get_mut().set(avm, context, this, value)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(Property::Stored {
|
|
|
|
value: value.into(),
|
|
|
|
attributes: Default::default(),
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-27 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 12:21:07 +00:00
|
|
|
pub fn force_set_virtual<A>(
|
2019-09-27 13:46:53 +00:00
|
|
|
&mut self,
|
|
|
|
name: &str,
|
2019-10-19 02:53:29 +00:00
|
|
|
get: Executable<'gc>,
|
|
|
|
set: Option<Executable<'gc>>,
|
2019-10-08 12:21:07 +00:00
|
|
|
attributes: A,
|
|
|
|
) where
|
|
|
|
A: Into<EnumSet<Attribute>>,
|
|
|
|
{
|
|
|
|
self.values.insert(
|
|
|
|
name.to_owned(),
|
|
|
|
Property::Virtual {
|
|
|
|
get,
|
|
|
|
set,
|
|
|
|
attributes: attributes.into(),
|
|
|
|
},
|
|
|
|
);
|
2019-09-02 20:19:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 10:48:18 +00:00
|
|
|
pub fn force_set<A>(&mut self, name: &str, value: impl Into<Value<'gc>>, attributes: A)
|
2019-10-08 12:21:07 +00:00
|
|
|
where
|
|
|
|
A: Into<EnumSet<Attribute>>,
|
|
|
|
{
|
|
|
|
self.values.insert(
|
|
|
|
name.to_string(),
|
|
|
|
Property::Stored {
|
2019-10-21 10:48:18 +00:00
|
|
|
value: value.into(),
|
2019-10-08 12:21:07 +00:00
|
|
|
attributes: attributes.into(),
|
|
|
|
},
|
|
|
|
);
|
2019-09-27 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 18:15:46 +00:00
|
|
|
/// Declare a native function on the current object.
|
|
|
|
///
|
|
|
|
/// This is intended for use with defining host object prototypes. Notably,
|
|
|
|
/// this creates a function object without an explicit `prototype`, which
|
|
|
|
/// is only possible when defining host functions. User-defined functions
|
|
|
|
/// always get a fresh explicit prototype, so you should never force set a
|
|
|
|
/// user-defined function.
|
2019-10-08 12:21:07 +00:00
|
|
|
pub fn force_set_function<A>(
|
2019-09-27 13:46:53 +00:00
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
function: NativeFunction<'gc>,
|
|
|
|
gc_context: MutationContext<'gc, '_>,
|
2019-10-08 12:21:07 +00:00
|
|
|
attributes: A,
|
2019-10-23 18:15:46 +00:00
|
|
|
fn_proto: Option<GcCell<'gc, Object<'gc>>>,
|
2019-10-08 12:21:07 +00:00
|
|
|
) where
|
|
|
|
A: Into<EnumSet<Attribute>>,
|
|
|
|
{
|
2019-09-27 13:46:53 +00:00
|
|
|
self.force_set(
|
2019-08-30 18:37:30 +00:00
|
|
|
name,
|
2019-10-23 18:15:46 +00:00
|
|
|
Value::Object(Object::function(gc_context, function, fn_proto, None)),
|
2019-10-08 12:21:07 +00:00
|
|
|
attributes,
|
2019-08-30 18:37:30 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-19 02:53:29 +00:00
|
|
|
/// Get the value of a particular property on this object.
|
|
|
|
///
|
|
|
|
/// The `avm`, `context`, and `this` parameters exist so that this object
|
2019-10-31 03:42:19 +00:00
|
|
|
/// can call virtual properties. Likewise, this function returns a
|
|
|
|
/// `ReturnValue` which allows pulling data from the return values of user
|
|
|
|
/// functions.
|
2019-09-27 13:46:53 +00:00
|
|
|
pub fn get(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
avm: &mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-09-27 13:46:53 +00:00
|
|
|
this: GcCell<'gc, Object<'gc>>,
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
2019-08-30 17:57:52 +00:00
|
|
|
if name == "__proto__" {
|
|
|
|
return Ok(self
|
|
|
|
.prototype
|
|
|
|
.map_or(Value::Undefined, Value::Object)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2019-08-28 23:29:43 +00:00
|
|
|
if let Some(value) = self.values.get(name) {
|
2019-09-27 13:46:53 +00:00
|
|
|
return value.get(avm, context, this);
|
2019-08-28 23:29:43 +00:00
|
|
|
}
|
2019-10-21 22:37:04 +00:00
|
|
|
|
2019-08-30 17:57:52 +00:00
|
|
|
self.prototype
|
|
|
|
.as_ref()
|
|
|
|
.map_or(Ok(Value::Undefined.into()), |p| {
|
|
|
|
p.read().get(name, avm, context, this)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_prototype(&mut self, prototype: GcCell<'gc, Object<'gc>>) {
|
|
|
|
self.prototype = Some(prototype);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prototype(&self) -> Option<&GcCell<'gc, Object<'gc>>> {
|
|
|
|
self.prototype.as_ref()
|
2019-08-28 23:29:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 18:33:44 +00:00
|
|
|
/// Delete a given value off the object.
|
2019-10-08 12:21:07 +00:00
|
|
|
pub fn delete(&mut self, name: &str) -> bool {
|
|
|
|
if let Some(prop) = self.values.get(name) {
|
|
|
|
if prop.can_delete() {
|
|
|
|
self.values.remove(name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2019-09-23 18:33:44 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 23:29:43 +00:00
|
|
|
pub fn has_property(&self, name: &str) -> bool {
|
2019-08-30 17:57:52 +00:00
|
|
|
self.has_own_property(name)
|
|
|
|
|| self
|
|
|
|
.prototype
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |p| p.read().has_property(name))
|
2019-08-28 23:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_own_property(&self, name: &str) -> bool {
|
2019-08-30 17:57:52 +00:00
|
|
|
if name == "__proto__" {
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-28 23:29:43 +00:00
|
|
|
self.values.contains_key(name)
|
|
|
|
}
|
|
|
|
|
2019-11-24 03:00:37 +00:00
|
|
|
pub fn is_property_overwritable(&self, name: &str) -> bool {
|
|
|
|
self.values
|
|
|
|
.get(name)
|
|
|
|
.map(|p| p.is_overwritable())
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:06:33 +00:00
|
|
|
pub fn is_property_enumerable(&self, name: &str) -> bool {
|
|
|
|
if let Some(prop) = self.values.get(name) {
|
|
|
|
prop.is_enumerable()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 14:34:08 +00:00
|
|
|
pub fn get_keys(&self) -> Vec<String> {
|
|
|
|
self.values
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(k, p)| {
|
|
|
|
if p.is_enumerable() {
|
|
|
|
Some(k.to_string())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
2019-09-17 04:04:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 23:29:43 +00:00
|
|
|
pub fn call(
|
|
|
|
&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-08-28 23:29:43 +00:00
|
|
|
this: GcCell<'gc, Object<'gc>>,
|
|
|
|
args: &[Value<'gc>],
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
2019-09-16 01:21:57 +00:00
|
|
|
if let Some(function) = &self.function {
|
|
|
|
function.exec(avm, context, this, args)
|
2019-08-28 23:29:43 +00:00
|
|
|
} else {
|
2019-10-31 00:25:52 +00:00
|
|
|
Ok(Value::Undefined.into())
|
2019-08-28 23:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-31 12:09:37 +00:00
|
|
|
|
2019-08-31 16:28:28 +00:00
|
|
|
pub fn as_string(&self) -> String {
|
|
|
|
if self.function.is_some() {
|
|
|
|
"[type Function]".to_string()
|
|
|
|
} else {
|
|
|
|
"[object Object]".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 12:09:37 +00:00
|
|
|
pub fn set_type_of(&mut self, type_of: &'static str) {
|
|
|
|
self.type_of = type_of;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_of(&self) -> &'static str {
|
|
|
|
self.type_of
|
|
|
|
}
|
2019-10-19 03:20:43 +00:00
|
|
|
|
|
|
|
/// Returns a copy of a given function.
|
|
|
|
///
|
|
|
|
/// TODO: We have to clone here because of how executables are stored on
|
2019-10-19 15:06:33 +00:00
|
|
|
/// objects directly. This might not be a good idea for performance.
|
2019-10-19 03:20:43 +00:00
|
|
|
pub fn as_executable(&self) -> Option<Executable<'gc>> {
|
|
|
|
self.function.clone()
|
|
|
|
}
|
2019-08-26 22:53:50 +00:00
|
|
|
}
|
2019-09-27 13:46:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2019-10-06 21:46:49 +00:00
|
|
|
use crate::avm1::activation::Activation;
|
2019-09-27 13:46:53 +00:00
|
|
|
use crate::backend::audio::NullAudioBackend;
|
|
|
|
use crate::backend::navigator::NullNavigatorBackend;
|
2019-10-26 07:15:57 +00:00
|
|
|
use crate::backend::render::NullRenderer;
|
2019-10-26 22:04:52 +00:00
|
|
|
use crate::display_object::{DisplayObject, MovieClip};
|
2019-10-26 07:15:57 +00:00
|
|
|
use crate::library::Library;
|
|
|
|
use crate::prelude::*;
|
2019-09-27 13:46:53 +00:00
|
|
|
use gc_arena::rootless_arena;
|
|
|
|
use rand::{rngs::SmallRng, SeedableRng};
|
2019-10-26 07:15:57 +00:00
|
|
|
use std::sync::Arc;
|
2019-09-27 13:46:53 +00:00
|
|
|
|
|
|
|
fn with_object<F, R>(swf_version: u8, test: F) -> R
|
|
|
|
where
|
|
|
|
F: for<'a, 'gc> FnOnce(
|
|
|
|
&mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
&mut UpdateContext<'a, 'gc, '_>,
|
2019-09-27 13:46:53 +00:00
|
|
|
GcCell<'gc, Object<'gc>>,
|
|
|
|
) -> R,
|
|
|
|
{
|
|
|
|
rootless_arena(|gc_context| {
|
2019-10-13 22:55:39 +00:00
|
|
|
let mut avm = Avm1::new(gc_context, swf_version);
|
2019-10-09 23:46:15 +00:00
|
|
|
let movie_clip: Box<dyn DisplayObject> =
|
|
|
|
Box::new(MovieClip::new(swf_version, gc_context));
|
2019-09-27 13:46:53 +00:00
|
|
|
let root = GcCell::allocate(gc_context, movie_clip);
|
2019-10-27 18:58:30 +00:00
|
|
|
let mut context = UpdateContext {
|
2019-09-27 13:46:53 +00:00
|
|
|
gc_context,
|
|
|
|
global_time: 0,
|
2019-10-09 00:07:00 +00:00
|
|
|
player_version: 32,
|
2019-10-27 18:58:30 +00:00
|
|
|
swf_version,
|
2019-09-27 13:46:53 +00:00
|
|
|
root,
|
|
|
|
start_clip: root,
|
|
|
|
active_clip: root,
|
|
|
|
target_clip: Some(root),
|
|
|
|
target_path: Value::Undefined,
|
|
|
|
rng: &mut SmallRng::from_seed([0u8; 16]),
|
2019-10-27 19:46:23 +00:00
|
|
|
action_queue: &mut crate::context::ActionQueue::new(),
|
2019-09-27 13:46:53 +00:00
|
|
|
audio: &mut NullAudioBackend::new(),
|
2019-10-26 07:15:57 +00:00
|
|
|
background_color: &mut Color {
|
|
|
|
r: 0,
|
|
|
|
g: 0,
|
|
|
|
b: 0,
|
|
|
|
a: 0,
|
|
|
|
},
|
|
|
|
library: &mut Library::new(),
|
2019-09-27 13:46:53 +00:00
|
|
|
navigator: &mut NullNavigatorBackend::new(),
|
2019-10-26 07:15:57 +00:00
|
|
|
renderer: &mut NullRenderer::new(),
|
|
|
|
swf_data: &mut Arc::new(vec![]),
|
2019-10-17 02:31:41 +00:00
|
|
|
system_prototypes: avm.prototypes().clone(),
|
2019-09-27 13:46:53 +00:00
|
|
|
};
|
2019-10-26 07:15:57 +00:00
|
|
|
|
2019-10-17 02:31:41 +00:00
|
|
|
let object = GcCell::allocate(
|
|
|
|
gc_context,
|
|
|
|
Object::object(gc_context, Some(avm.prototypes().object)),
|
|
|
|
);
|
2019-09-27 13:46:53 +00:00
|
|
|
|
2019-10-04 02:42:32 +00:00
|
|
|
let globals = avm.global_object_cell();
|
2019-10-21 22:37:04 +00:00
|
|
|
avm.insert_stack_frame(GcCell::allocate(
|
|
|
|
gc_context,
|
2019-10-10 02:58:53 +00:00
|
|
|
Activation::from_nothing(swf_version, globals, gc_context),
|
2019-10-21 22:37:04 +00:00
|
|
|
));
|
2019-10-04 02:42:32 +00:00
|
|
|
|
2019-09-27 13:46:53 +00:00
|
|
|
test(&mut avm, &mut context, object)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_undefined() {
|
|
|
|
with_object(0, |avm, context, object| {
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object
|
|
|
|
.read()
|
|
|
|
.get("not_defined", avm, context, object)
|
|
|
|
.unwrap(),
|
2019-10-31 00:25:52 +00:00
|
|
|
Value::Undefined.into()
|
2019-09-27 13:46:53 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_get() {
|
|
|
|
with_object(0, |avm, context, object| {
|
2019-10-21 10:48:18 +00:00
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
|
|
|
.force_set("forced", "forced", EnumSet::empty());
|
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
2019-10-26 03:21:14 +00:00
|
|
|
.set("natural", "natural", avm, context, object)
|
|
|
|
.unwrap();
|
2019-09-27 13:46:53 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("forced", avm, context, object).unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("forced".into())
|
2019-09-27 13:46:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("natural", avm, context, object).unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("natural".into())
|
2019-09-27 13:46:53 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-08 13:24:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_set_readonly() {
|
|
|
|
with_object(0, |avm, context, object| {
|
2019-10-21 10:48:18 +00:00
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
|
|
|
.force_set("normal", "initial", EnumSet::empty());
|
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
|
|
|
.force_set("readonly", "initial", ReadOnly);
|
|
|
|
|
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
2019-10-26 03:21:14 +00:00
|
|
|
.set("normal", "replaced", avm, context, object)
|
|
|
|
.unwrap();
|
2019-10-21 10:48:18 +00:00
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
2019-10-26 03:21:14 +00:00
|
|
|
.set("readonly", "replaced", avm, context, object)
|
|
|
|
.unwrap();
|
2019-10-08 13:24:57 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("normal", avm, context, object).unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("replaced".into())
|
2019-10-08 13:24:57 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("readonly", avm, context, object).unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("initial".into())
|
2019-10-08 13:24:57 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deletable_not_readonly() {
|
|
|
|
with_object(0, |avm, context, object| {
|
2019-10-21 10:48:18 +00:00
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
|
|
|
.force_set("test", "initial", DontDelete);
|
2019-10-08 13:24:57 +00:00
|
|
|
|
|
|
|
assert_eq!(object.write(context.gc_context).delete("test"), false);
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("test", avm, context, object).unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("initial".into())
|
2019-10-08 13:24:57 +00:00
|
|
|
);
|
|
|
|
|
2019-10-21 10:48:18 +00:00
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
2019-10-26 03:21:14 +00:00
|
|
|
.set("test", "replaced", avm, context, object)
|
|
|
|
.unwrap();
|
2019-10-08 13:24:57 +00:00
|
|
|
|
|
|
|
assert_eq!(object.write(context.gc_context).delete("test"), false);
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("test", avm, context, object).unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("replaced".into())
|
2019-10-08 13:24:57 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-27 13:46:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_virtual_get() {
|
|
|
|
with_object(0, |avm, context, object| {
|
2019-10-21 22:37:04 +00:00
|
|
|
let getter = Executable::Native(|_avm, _context, _this, _args| {
|
2019-10-26 03:21:14 +00:00
|
|
|
Ok(ReturnValue::Immediate("Virtual!".into()))
|
2019-10-21 22:37:04 +00:00
|
|
|
});
|
2019-10-19 02:53:29 +00:00
|
|
|
|
2019-10-08 12:21:07 +00:00
|
|
|
object.write(context.gc_context).force_set_virtual(
|
|
|
|
"test",
|
|
|
|
getter,
|
|
|
|
None,
|
|
|
|
EnumSet::empty(),
|
|
|
|
);
|
2019-09-27 13:46:53 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("test", avm, context, object).unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("Virtual!".into())
|
2019-09-27 13:46:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// This set should do nothing
|
2019-10-21 10:48:18 +00:00
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
2019-10-26 03:21:14 +00:00
|
|
|
.set("test", "Ignored!", avm, context, object)
|
|
|
|
.unwrap();
|
2019-09-27 13:46:53 +00:00
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("test", avm, context, object).unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("Virtual!".into())
|
2019-09-27 13:46:53 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
2019-10-08 12:21:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delete() {
|
|
|
|
with_object(0, |avm, context, object| {
|
2019-10-21 22:37:04 +00:00
|
|
|
let getter = Executable::Native(|_avm, _context, _this, _args| {
|
2019-10-26 03:21:14 +00:00
|
|
|
Ok(ReturnValue::Immediate("Virtual!".into()))
|
2019-10-21 22:37:04 +00:00
|
|
|
});
|
2019-10-08 12:21:07 +00:00
|
|
|
|
|
|
|
object.write(context.gc_context).force_set_virtual(
|
|
|
|
"virtual",
|
2019-10-19 02:53:29 +00:00
|
|
|
getter.clone(),
|
2019-10-08 12:21:07 +00:00
|
|
|
None,
|
|
|
|
EnumSet::empty(),
|
|
|
|
);
|
|
|
|
object.write(context.gc_context).force_set_virtual(
|
|
|
|
"virtual_un",
|
|
|
|
getter,
|
|
|
|
None,
|
2019-10-08 18:35:23 +00:00
|
|
|
DontDelete,
|
2019-10-08 12:21:07 +00:00
|
|
|
);
|
2019-10-21 10:48:18 +00:00
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
|
|
|
.force_set("stored", "Stored!", EnumSet::empty());
|
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
|
|
|
.force_set("stored_un", "Stored!", DontDelete);
|
2019-10-08 12:21:07 +00:00
|
|
|
|
|
|
|
assert_eq!(object.write(context.gc_context).delete("virtual"), true);
|
|
|
|
assert_eq!(object.write(context.gc_context).delete("virtual_un"), false);
|
|
|
|
assert_eq!(object.write(context.gc_context).delete("stored"), true);
|
|
|
|
assert_eq!(object.write(context.gc_context).delete("stored_un"), false);
|
2019-10-08 14:34:08 +00:00
|
|
|
assert_eq!(
|
|
|
|
object.write(context.gc_context).delete("non_existent"),
|
|
|
|
false
|
|
|
|
);
|
2019-10-08 12:21:07 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("virtual", avm, context, object).unwrap(),
|
2019-10-31 00:25:52 +00:00
|
|
|
Value::Undefined.into()
|
2019-10-08 12:21:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object
|
|
|
|
.read()
|
|
|
|
.get("virtual_un", avm, context, object)
|
|
|
|
.unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("Virtual!".into())
|
2019-10-08 12:21:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object.read().get("stored", avm, context, object).unwrap(),
|
2019-10-31 00:25:52 +00:00
|
|
|
Value::Undefined.into()
|
2019-10-08 12:21:07 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-10-26 03:21:14 +00:00
|
|
|
object
|
|
|
|
.read()
|
|
|
|
.get("stored_un", avm, context, object)
|
|
|
|
.unwrap(),
|
2019-10-21 22:37:04 +00:00
|
|
|
ReturnValue::Immediate("Stored!".into())
|
2019-10-08 12:21:07 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
2019-10-08 14:34:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iter_values() {
|
|
|
|
with_object(0, |_avm, context, object| {
|
2019-10-31 00:25:52 +00:00
|
|
|
let getter = Executable::Native(|_avm, _context, _this, _args| Ok(Value::Null.into()));
|
2019-10-08 14:34:08 +00:00
|
|
|
|
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
|
|
|
.force_set("stored", Value::Null, EnumSet::empty());
|
2019-10-08 18:35:23 +00:00
|
|
|
object
|
|
|
|
.write(context.gc_context)
|
|
|
|
.force_set("stored_hidden", Value::Null, DontEnum);
|
2019-10-08 14:34:08 +00:00
|
|
|
object.write(context.gc_context).force_set_virtual(
|
|
|
|
"virtual",
|
2019-10-19 02:53:29 +00:00
|
|
|
getter.clone(),
|
2019-10-08 14:34:08 +00:00
|
|
|
None,
|
|
|
|
EnumSet::empty(),
|
|
|
|
);
|
|
|
|
object.write(context.gc_context).force_set_virtual(
|
|
|
|
"virtual_hidden",
|
|
|
|
getter,
|
|
|
|
None,
|
2019-10-08 18:35:23 +00:00
|
|
|
DontEnum,
|
2019-10-08 14:34:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let keys = object.read().get_keys();
|
|
|
|
assert_eq!(keys.len(), 2);
|
|
|
|
assert_eq!(keys.contains(&"stored".to_string()), true);
|
|
|
|
assert_eq!(keys.contains(&"stored_hidden".to_string()), false);
|
|
|
|
assert_eq!(keys.contains(&"virtual".to_string()), true);
|
|
|
|
assert_eq!(keys.contains(&"virtual_hidden".to_string()), false);
|
|
|
|
})
|
|
|
|
}
|
2019-09-27 13:46:53 +00:00
|
|
|
}
|