core: Add `Value::get` shortcut that resolves Objects or returns Undefined

This commit is contained in:
Nathan Adams 2020-05-31 22:47:03 +02:00 committed by Mike Welsh
parent d0ad1b57fe
commit 58df56e4a4
3 changed files with 27 additions and 15 deletions

View File

@ -13,7 +13,14 @@ pub fn value_to_matrix<'gc>(
avm: &mut Avm1<'gc>, avm: &mut Avm1<'gc>,
context: &mut UpdateContext<'_, 'gc, '_>, context: &mut UpdateContext<'_, 'gc, '_>,
) -> Result<Matrix, Error> { ) -> Result<Matrix, Error> {
object_to_matrix(value.as_object()?, avm, context) let a = value.get("a", avm, context)?.as_number(avm, context)? as f32;
let b = value.get("b", avm, context)?.as_number(avm, context)? as f32;
let c = value.get("c", avm, context)?.as_number(avm, context)? as f32;
let d = value.get("d", avm, context)?.as_number(avm, context)? as f32;
let tx = Twips::from_pixels(value.get("tx", avm, context)?.as_number(avm, context)?);
let ty = Twips::from_pixels(value.get("ty", avm, context)?.as_number(avm, context)?);
Ok(Matrix { a, b, c, d, tx, ty })
} }
pub fn gradient_object_to_matrix<'gc>( pub fn gradient_object_to_matrix<'gc>(

View File

@ -34,11 +34,9 @@ pub fn value_to_point<'gc>(
avm: &mut Avm1<'gc>, avm: &mut Avm1<'gc>,
context: &mut UpdateContext<'_, 'gc, '_>, context: &mut UpdateContext<'_, 'gc, '_>,
) -> Result<(f64, f64), Error> { ) -> Result<(f64, f64), Error> {
if let Value::Object(object) = value { let x = value.get("x", avm, context)?.as_number(avm, context)?;
object_to_point(object, avm, context) let y = value.get("y", avm, context)?.as_number(avm, context)?;
} else { Ok((x, y))
Ok((NAN, NAN))
}
} }
pub fn object_to_point<'gc>( pub fn object_to_point<'gc>(
@ -98,7 +96,7 @@ fn equals<'gc>(
this: Object<'gc>, this: Object<'gc>,
args: &[Value<'gc>], args: &[Value<'gc>],
) -> Result<ReturnValue<'gc>, Error> { ) -> Result<ReturnValue<'gc>, Error> {
if let Some(Value::Object(other)) = args.get(0) { if let Some(other) = args.get(0) {
let this_x = this.get("x", avm, context)?; let this_x = this.get("x", avm, context)?;
let this_y = this.get("y", avm, context)?; let this_y = this.get("y", avm, context)?;
let other_x = other.get("x", avm, context)?; let other_x = other.get("x", avm, context)?;
@ -155,14 +153,8 @@ fn distance<'gc>(
let a = args.get(0).unwrap_or(&Value::Undefined); let a = args.get(0).unwrap_or(&Value::Undefined);
let b = args.get(1).unwrap_or(&Value::Undefined); let b = args.get(1).unwrap_or(&Value::Undefined);
let delta = a let delta = a.call_method("subtract", &[b.to_owned()], avm, context)?;
.call_method("subtract", &[b.to_owned()], avm, context)?; Ok(delta.get("length", avm, context)?.into())
if let Value::Object(object) = delta {
let length = object.get("length", avm, context)?;
Ok(length.into())
} else {
Ok(Value::Undefined.into())
}
} }
fn polar<'gc>( fn polar<'gc>(

View File

@ -556,6 +556,19 @@ impl<'gc> Value<'gc> {
} }
} }
pub fn get(
&self,
name: &str,
avm: &mut Avm1<'gc>,
context: &mut UpdateContext<'_, 'gc, '_>,
) -> Result<Value<'gc>, Error> {
if let Value::Object(object) = self {
object.get(name, avm, context)
} else {
Ok(Value::Undefined)
}
}
pub fn call( pub fn call(
&self, &self,
avm: &mut Avm1<'gc>, avm: &mut Avm1<'gc>,