avm1: Move register get/setting from avm1 to StackFrame

This commit is contained in:
Nathan Adams 2020-06-26 18:16:14 +02:00
parent 63e66c29eb
commit cdfd58d619
2 changed files with 39 additions and 48 deletions

View File

@ -894,52 +894,6 @@ impl<'gc> Avm1<'gc> {
value value
} }
/// Retrieve a given register value.
///
/// If a given register does not exist, this function yields
/// Value::Undefined, which is also a valid register value.
pub fn current_register(&self, id: u8) -> Value<'gc> {
if self
.current_stack_frame()
.map(|sf| sf.read().has_local_register(id))
.unwrap_or(false)
{
self.current_stack_frame()
.unwrap()
.read()
.local_register(id)
.unwrap_or(Value::Undefined)
} else {
self.registers
.get(id as usize)
.cloned()
.unwrap_or(Value::Undefined)
}
}
/// Set a register to a given value.
///
/// If a given register does not exist, this function does nothing.
pub fn set_current_register(
&mut self,
id: u8,
value: Value<'gc>,
context: &mut UpdateContext<'_, 'gc, '_>,
) {
if self
.current_stack_frame()
.map(|sf| sf.read().has_local_register(id))
.unwrap_or(false)
{
self.current_stack_frame()
.unwrap()
.write(context.gc_context)
.set_local_register(id, value, context.gc_context);
} else if let Some(v) = self.registers.get_mut(id as usize) {
*v = value;
}
}
/// Obtain the value of `_root`. /// Obtain the value of `_root`.
pub fn root_object(&self, _context: &mut UpdateContext<'_, 'gc, '_>) -> Value<'gc> { pub fn root_object(&self, _context: &mut UpdateContext<'_, 'gc, '_>) -> Value<'gc> {
self.base_clip().root().object() self.base_clip().root().object()

View File

@ -1534,7 +1534,7 @@ impl<'a, 'gc: 'a> StackFrame<'a, 'gc> {
SwfValue::Float(v) => f64::from(*v).into(), SwfValue::Float(v) => f64::from(*v).into(),
SwfValue::Double(v) => (*v).into(), SwfValue::Double(v) => (*v).into(),
SwfValue::Str(v) => (*v).to_string().into(), SwfValue::Str(v) => (*v).to_string().into(),
SwfValue::Register(v) => self.avm.current_register(*v), SwfValue::Register(v) => self.current_register(*v),
SwfValue::ConstantPool(i) => { SwfValue::ConstantPool(i) => {
if let Some(value) = self if let Some(value) = self
.activation .activation
@ -1834,7 +1834,7 @@ impl<'a, 'gc: 'a> StackFrame<'a, 'gc> {
// The value must remain on the stack. // The value must remain on the stack.
let val = self.avm.pop(); let val = self.avm.pop();
self.avm.push(val.clone()); self.avm.push(val.clone());
self.avm.set_current_register(register, val, context); self.set_current_register(register, val, context);
Ok(FrameControl::Continue) Ok(FrameControl::Continue)
} }
@ -2090,4 +2090,41 @@ impl<'a, 'gc: 'a> StackFrame<'a, 'gc> {
self.avm.run_activation(context, new_activation)?; self.avm.run_activation(context, new_activation)?;
Ok(FrameControl::Continue) Ok(FrameControl::Continue)
} }
/// Retrieve a given register value.
///
/// If a given register does not exist, this function yields
/// Value::Undefined, which is also a valid register value.
pub fn current_register(&self, id: u8) -> Value<'gc> {
if self.activation.read().has_local_register(id) {
self.activation
.read()
.local_register(id)
.unwrap_or(Value::Undefined)
} else {
self.avm
.registers
.get(id as usize)
.cloned()
.unwrap_or(Value::Undefined)
}
}
/// Set a register to a given value.
///
/// If a given register does not exist, this function does nothing.
pub fn set_current_register(
&mut self,
id: u8,
value: Value<'gc>,
context: &mut UpdateContext<'_, 'gc, '_>,
) {
if self.activation.read().has_local_register(id) {
self.activation
.write(context.gc_context)
.set_local_register(id, value, context.gc_context);
} else if let Some(v) = self.avm.registers.get_mut(id as usize) {
*v = value;
}
}
} }