Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preallocate AVM1 constant pool values #2800

Merged
merged 2 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/avm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub struct Avm1<'gc> {

/// The constant pool to use for new activations from code sources that
/// don't close over the constant pool they were defined with.
constant_pool: GcCell<'gc, Vec<String>>,
constant_pool: GcCell<'gc, Vec<Value<'gc>>>,

/// The global object.
globals: Object<'gc>,
Expand Down
21 changes: 12 additions & 9 deletions core/src/avm1/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub struct Activation<'a, 'gc: 'a, 'gc_context: 'a> {
scope: GcCell<'gc, Scope<'gc>>,

/// The currently in use constant pool.
constant_pool: GcCell<'gc, Vec<String>>,
constant_pool: GcCell<'gc, Vec<Value<'gc>>>,

/// The immutable value of `this`.
this: Object<'gc>,
Expand Down Expand Up @@ -252,7 +252,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
id: ActivationIdentifier<'a>,
swf_version: u8,
scope: GcCell<'gc, Scope<'gc>>,
constant_pool: GcCell<'gc, Vec<String>>,
constant_pool: GcCell<'gc, Vec<Value<'gc>>>,
base_clip: DisplayObject<'gc>,
this: Object<'gc>,
callee: Option<Object<'gc>>,
Expand Down Expand Up @@ -896,7 +896,10 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
self.context.gc_context,
constant_pool
.iter()
.map(|s| (*s).to_string_lossy(self.encoding()))
.map(|s| {
AvmString::new(self.context.gc_context, s.to_string_lossy(self.encoding()))
.into()
})
.collect(),
);
self.set_constant_pool(self.context.avm1.constant_pool);
Expand Down Expand Up @@ -1297,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 @@ -1789,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) {
AvmString::new(self.context.gc_context, value).into()
*value
} else {
avm_warn!(
self,
Expand All @@ -1808,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 @@ -2025,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 Expand Up @@ -2977,11 +2980,11 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
}
}

pub fn constant_pool(&self) -> GcCell<'gc, Vec<String>> {
pub fn constant_pool(&self) -> GcCell<'gc, Vec<Value<'gc>>> {
self.constant_pool
}

pub fn set_constant_pool(&mut self, constant_pool: GcCell<'gc, Vec<String>>) {
pub fn set_constant_pool(&mut self, constant_pool: GcCell<'gc, Vec<Value<'gc>>>) {
self.constant_pool = constant_pool;
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/avm1/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct Avm1Function<'gc> {
scope: GcCell<'gc, Scope<'gc>>,

/// The constant pool the function executes with.
constant_pool: GcCell<'gc, Vec<String>>,
constant_pool: GcCell<'gc, Vec<Value<'gc>>>,

/// The base movie clip that the function was defined on.
/// This is the movie clip that contains the bytecode.
Expand All @@ -95,7 +95,7 @@ impl<'gc> Avm1Function<'gc> {
name: &str,
params: &[&'_ SwfStr],
scope: GcCell<'gc, Scope<'gc>>,
constant_pool: GcCell<'gc, Vec<String>>,
constant_pool: GcCell<'gc, Vec<Value<'gc>>>,
base_clip: DisplayObject<'gc>,
) -> Self {
let name = if name.is_empty() {
Expand Down Expand Up @@ -139,7 +139,7 @@ impl<'gc> Avm1Function<'gc> {
actions: SwfSlice,
swf_function: &swf::avm1::types::Function,
scope: GcCell<'gc, Scope<'gc>>,
constant_pool: GcCell<'gc, Vec<String>>,
constant_pool: GcCell<'gc, Vec<Value<'gc>>>,
base_clip: DisplayObject<'gc>,
) -> Self {
let name = if swf_function.name.is_empty() {
Expand Down 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