Skip to content

Commit

Permalink
Make avm11::Value Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian17 authored and Herschel committed Jan 23, 2021
1 parent 2065812 commit 0a64c34
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 52 deletions.
8 changes: 4 additions & 4 deletions core/src/avm1/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
target.as_display_object()
} else {
let start = self.target_clip_or_root()?;
self.resolve_target_display_object(start, target.clone(), true)?
self.resolve_target_display_object(start, target, true)?
}
} else {
Some(self.target_clip_or_root()?)
Expand Down Expand Up @@ -1792,7 +1792,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
SwfValue::Register(v) => self.current_register(*v),
SwfValue::ConstantPool(i) => {
if let Some(value) = self.constant_pool().read().get(*i as usize) {
value.clone()
*value
} else {
avm_warn!(
self,
Expand All @@ -1811,7 +1811,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {

fn action_push_duplicate(&mut self) -> Result<FrameControl<'gc>, Error<'gc>> {
let val = self.context.avm1.pop();
self.context.avm1.push(val.clone());
self.context.avm1.push(val);
self.context.avm1.push(val);
Ok(FrameControl::Continue)
}
Expand Down Expand Up @@ -2028,7 +2028,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
fn action_store_register(&mut self, register: u8) -> Result<FrameControl<'gc>, Error<'gc>> {
// The value must remain on the stack.
let val = self.context.avm1.pop();
self.context.avm1.push(val.clone());
self.context.avm1.push(val);
self.set_current_register(register, val);

Ok(FrameControl::Continue)
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm1/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl<'gc> Executable<'gc> {
for i in 0..args.len() {
arguments.set_array_element(
i,
args.get(i).unwrap().clone(),
*args.get(i).unwrap(),
activation.context.gc_context,
);
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/avm1/globals/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ pub fn concat<'gc>(
}

if !added {
array.set_array_element(length, arg.clone(), activation.context.gc_context);
array.set_array_element(length, *arg, activation.context.gc_context);
length += 1;
}
}
Expand Down Expand Up @@ -792,8 +792,8 @@ fn sort_compare_fields<'a, 'gc: 'a>(
use crate::avm1::object::value_object::ValueObject;
move |activation, a, b| {
for (field_name, compare_fn) in field_names.iter().zip(compare_fns.iter_mut()) {
let a_object = ValueObject::boxed(activation, a.clone());
let b_object = ValueObject::boxed(activation, b.clone());
let a_object = ValueObject::boxed(activation, *a);
let b_object = ValueObject::boxed(activation, *b);
let a_prop = a_object.get(field_name, activation).unwrap();
let b_prop = b_object.get(field_name, activation).unwrap();

Expand All @@ -816,7 +816,7 @@ fn sort_compare_custom<'gc>(
compare_fn: &Value<'gc>,
) -> Ordering {
// TODO: Handle errors.
let args = [a.clone(), b.clone()];
let args = [*a, *b];
let ret = compare_fn
.call("[Compare]", activation, this, None, &args)
.unwrap_or(Value::Undefined);
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm1/globals/load_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn on_data<'gc>(
let success = match args.get(0) {
None | Some(Value::Undefined) | Some(Value::Null) => false,
Some(val) => {
this.call_method(&"decode", &[val.clone()], activation)?;
this.call_method(&"decode", &[*val], activation)?;
this.set("loaded", true.into(), activation)?;
true
}
Expand Down
14 changes: 7 additions & 7 deletions core/src/avm1/globals/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,22 @@ fn constructor<'gc>(
apply_matrix_to_object(Matrix::identity(), this, activation)?;
} else {
if let Some(a) = args.get(0) {
this.set("a", a.clone(), activation)?;
this.set("a", *a, activation)?;
}
if let Some(b) = args.get(1) {
this.set("b", b.clone(), activation)?;
this.set("b", *b, activation)?;
}
if let Some(c) = args.get(2) {
this.set("c", c.clone(), activation)?;
this.set("c", *c, activation)?;
}
if let Some(d) = args.get(3) {
this.set("d", d.clone(), activation)?;
this.set("d", *d, activation)?;
}
if let Some(tx) = args.get(4) {
this.set("tx", tx.clone(), activation)?;
this.set("tx", *tx, activation)?;
}
if let Some(ty) = args.get(5) {
this.set("ty", ty.clone(), activation)?;
this.set("ty", *ty, activation)?;
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ fn concat<'gc>(
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let mut matrix = object_to_matrix(this, activation)?;
let other = value_to_matrix(args.get(0).unwrap_or(&Value::Undefined).clone(), activation)?;
let other = value_to_matrix(*args.get(0).unwrap_or(&Value::Undefined), activation)?;
matrix = other * matrix;
apply_matrix_to_object(matrix, this, activation)?;

Expand Down
2 changes: 1 addition & 1 deletion core/src/avm1/globals/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn hit_test<'gc>(
} else if args.len() == 1 {
let other = activation.resolve_target_display_object(
movie_clip.into(),
args.get(0).unwrap().clone(),
*args.get(0).unwrap(),
false,
)?;
if let Some(other) = other {
Expand Down
8 changes: 4 additions & 4 deletions core/src/avm1/globals/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ fn set_left<'gc>(
let new_left = args.get(0).unwrap_or(&Value::Undefined).to_owned();
let old_left = this.get("x", activation)?.coerce_to_f64(activation)?;
let width = this.get("width", activation)?.coerce_to_f64(activation)?;
this.set("x", new_left.clone(), activation)?;
this.set("x", new_left, activation)?;
this.set(
"width",
Value::Number(width + (old_left - new_left.coerce_to_f64(activation)?)),
Expand All @@ -515,7 +515,7 @@ fn set_top<'gc>(
let new_top = args.get(0).unwrap_or(&Value::Undefined).to_owned();
let old_top = this.get("y", activation)?.coerce_to_f64(activation)?;
let height = this.get("height", activation)?.coerce_to_f64(activation)?;
this.set("y", new_top.clone(), activation)?;
this.set("y", new_top, activation)?;
this.set(
"height",
Value::Number(height + (old_top - new_top.coerce_to_f64(activation)?)),
Expand Down Expand Up @@ -632,8 +632,8 @@ fn set_top_left<'gc>(
let old_top = this.get("y", activation)?.coerce_to_f64(activation)?;
let height = this.get("height", activation)?.coerce_to_f64(activation)?;

this.set("x", new_left.clone(), activation)?;
this.set("y", new_top.clone(), activation)?;
this.set("x", new_left, activation)?;
this.set("y", new_top, activation)?;
this.set(
"width",
Value::Number(width + (old_left - new_left.coerce_to_f64(activation)?)),
Expand Down
24 changes: 9 additions & 15 deletions core/src/avm1/object/script_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'gc> Watcher<'gc> {
)),
old_value,
new_value,
self.user_data.clone(),
self.user_data,
];
if let Some(executable) = self.callback.as_executable() {
executable.exec(
Expand Down Expand Up @@ -295,14 +295,14 @@ impl<'gc> ScriptObject<'gc> {

if let Some(this_proto) = proto {
worked = true;
if let Some(rval) = this_proto.call_setter(name, value.clone(), activation) {
if let Some(rval) = this_proto.call_setter(name, value, activation) {
if let Some(exec) = rval.as_executable() {
let _ = exec.exec(
"[Setter]",
activation,
this,
Some(this_proto),
&[value.clone()],
&[value],
ExecutionReason::Special,
rval,
);
Expand All @@ -324,14 +324,8 @@ impl<'gc> ScriptObject<'gc> {
let mut return_value = Ok(());
if let Some(watcher) = watcher {
let old_value = self.get(name, activation)?;
value = match watcher.call(
activation,
name,
old_value,
value.clone(),
this,
base_proto,
) {
value = match watcher.call(activation, name, old_value, value, this, base_proto)
{
Ok(value) => value,
Err(Error::ThrownValue(error)) => {
return_value = Err(Error::ThrownValue(error));
Expand All @@ -347,10 +341,10 @@ impl<'gc> ScriptObject<'gc> {
.values
.entry(name, activation.is_case_sensitive())
{
Entry::Occupied(mut entry) => entry.get_mut().set(value.clone()),
Entry::Occupied(mut entry) => entry.get_mut().set(value),
Entry::Vacant(entry) => {
entry.insert(Property::Stored {
value: value.clone(),
value,
attributes: Attribute::empty(),
});

Expand Down Expand Up @@ -803,14 +797,14 @@ impl<'gc> TObject<'gc> for ScriptObject<'gc> {
value: Value<'gc>,
gc_context: MutationContext<'gc, '_>,
) -> usize {
self.sync_native_property(&index.to_string(), gc_context, Some(value.clone()), true);
self.sync_native_property(&index.to_string(), gc_context, Some(value), true);
let mut adjust_length = false;
let length = match &mut self.0.write(gc_context).array {
ArrayStorage::Vector(vector) => {
if index >= vector.len() {
vector.resize(index + 1, Value::Undefined);
}
vector[index] = value.clone();
vector[index] = value;
adjust_length = true;
vector.len()
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm1/object/value_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<'gc> ValueObject<'gc> {

/// Retrieve the boxed value.
pub fn unbox(self) -> Value<'gc> {
self.0.read().value.clone()
self.0.read().value
}

/// Change the value in the box.
Expand Down
19 changes: 5 additions & 14 deletions core/src/avm1/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::ecma_conversions::{
use std::borrow::Cow;
use std::f64::NAN;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum Value<'gc> {
Undefined,
Expand Down Expand Up @@ -639,26 +639,17 @@ mod test {
let a = Value::Number(1.0);
let b = Value::Number(2.0);

assert_eq!(
b.abstract_lt(a.clone(), activation).unwrap(),
Value::Bool(false)
);
assert_eq!(b.abstract_lt(a, activation).unwrap(), Value::Bool(false));

let nan = Value::Number(NAN);
assert_eq!(
nan.abstract_lt(a.clone(), activation).unwrap(),
Value::Undefined
);
assert_eq!(nan.abstract_lt(a, activation).unwrap(), Value::Undefined);

let inf = Value::Number(INFINITY);
assert_eq!(
inf.abstract_lt(a.clone(), activation).unwrap(),
Value::Bool(false)
);
assert_eq!(inf.abstract_lt(a, activation).unwrap(), Value::Bool(false));

let neg_inf = Value::Number(NEG_INFINITY);
assert_eq!(
neg_inf.abstract_lt(a.clone(), activation).unwrap(),
neg_inf.abstract_lt(a, activation).unwrap(),
Value::Bool(true)
);

Expand Down

0 comments on commit 0a64c34

Please sign in to comment.