ruffle/core/src/avm1/globals.rs

220 lines
6.4 KiB
Rust
Raw Normal View History

2019-09-20 19:11:33 +00:00
use crate::avm1::fscommand;
use crate::avm1::{Avm1, Object, UpdateContext, Value};
use crate::backend::navigator::NavigationMethod;
use enumset::EnumSet;
2019-09-17 03:37:11 +00:00
use gc_arena::{GcCell, MutationContext};
use rand::Rng;
mod math;
2019-09-20 19:11:33 +00:00
#[allow(non_snake_case, unused_must_use)] //can't use errors yet
2019-09-01 22:40:32 +00:00
pub fn getURL<'a, 'gc>(
avm: &mut Avm1<'gc>,
context: &mut UpdateContext<'a, 'gc, '_>,
2019-09-01 22:40:32 +00:00
_this: GcCell<'gc, Object<'gc>>,
args: &[Value<'gc>],
) -> Option<Value<'gc>> {
//TODO: Error behavior if no arguments are present
if let Some(url_val) = args.get(0) {
let url = url_val.clone().into_string();
2019-09-20 19:11:33 +00:00
if let Some(fscommand) = fscommand::parse(&url) {
fscommand::handle(fscommand, avm, context);
return Some(Value::Undefined);
2019-09-20 19:11:33 +00:00
}
2019-09-26 18:45:45 +00:00
let window = args.get(1).map(|v| v.clone().into_string());
let method = match args.get(2) {
Some(Value::String(s)) if s == "GET" => Some(NavigationMethod::GET),
Some(Value::String(s)) if s == "POST" => Some(NavigationMethod::POST),
2019-09-17 03:37:11 +00:00
_ => None,
};
2019-10-08 14:34:08 +00:00
let vars_method = method.map(|m| (m, avm.locals_into_form_values(context)));
2019-09-17 03:37:11 +00:00
context.navigator.navigate_to_url(url, window, vars_method);
2019-09-01 22:40:32 +00:00
}
Some(Value::Undefined)
2019-09-01 22:40:32 +00:00
}
pub fn random<'gc>(
_avm: &mut Avm1<'gc>,
action_context: &mut UpdateContext<'_, 'gc, '_>,
_this: GcCell<'gc, Object<'gc>>,
args: &[Value<'gc>],
) -> Option<Value<'gc>> {
match args.get(0) {
Some(Value::Number(max)) => Some(Value::Number(
action_context.rng.gen_range(0.0f64, max).floor(),
)),
_ => Some(Value::Undefined), //TODO: Shouldn't this be an error condition?
}
}
2019-10-10 12:28:14 +00:00
pub fn boolean<'gc>(
avm: &mut Avm1<'gc>,
_action_context: &mut UpdateContext<'_, 'gc, '_>,
2019-10-10 12:28:14 +00:00
_this: GcCell<'gc, Object<'gc>>,
args: &[Value<'gc>],
) -> Option<Value<'gc>> {
2019-10-10 12:28:14 +00:00
if let Some(val) = args.get(0) {
Some(Value::Bool(val.as_bool(avm.current_swf_version())))
2019-10-10 12:28:14 +00:00
} else {
Some(Value::Bool(false))
2019-10-10 12:28:14 +00:00
}
}
pub fn number<'gc>(
_avm: &mut Avm1<'gc>,
_action_context: &mut UpdateContext<'_, 'gc, '_>,
_this: GcCell<'gc, Object<'gc>>,
args: &[Value<'gc>],
) -> Option<Value<'gc>> {
if let Some(val) = args.get(0) {
Some(Value::Number(val.as_number()))
} else {
Some(Value::Number(0.0))
}
}
pub fn is_nan<'gc>(
_avm: &mut Avm1<'gc>,
_action_context: &mut UpdateContext<'_, 'gc, '_>,
_this: GcCell<'gc, Object<'gc>>,
args: &[Value<'gc>],
) -> Option<Value<'gc>> {
if let Some(val) = args.get(0) {
Some(Value::Bool(val.as_number().is_nan()))
} else {
Some(Value::Bool(true))
}
}
pub fn create_globals<'gc>(gc_context: MutationContext<'gc, '_>) -> Object<'gc> {
let mut globals = Object::object(gc_context);
globals.force_set_function("isNaN", is_nan, gc_context, EnumSet::empty());
2019-10-10 12:28:14 +00:00
globals.force_set_function("Boolean", boolean, gc_context, EnumSet::empty());
globals.force_set("Math", math::create(gc_context), EnumSet::empty());
globals.force_set_function("getURL", getURL, gc_context, EnumSet::empty());
globals.force_set_function("Number", number, gc_context, EnumSet::empty());
globals.force_set_function("random", random, gc_context, EnumSet::empty());
globals.force_set("NaN", Value::Number(std::f64::NAN), EnumSet::empty());
globals.force_set(
"Infinity",
Value::Number(std::f64::INFINITY),
EnumSet::empty(),
);
globals
2019-09-17 03:37:11 +00:00
}
2019-10-10 12:28:14 +00:00
#[cfg(test)]
#[allow(clippy::unreadable_literal)]
2019-10-10 12:28:14 +00:00
mod tests {
use super::*;
use crate::avm1::test_utils::with_avm;
2019-10-10 12:28:14 +00:00
use crate::avm1::Error;
macro_rules! test_std {
2019-10-21 10:44:21 +00:00
( $test: ident, $fun: expr, $version: expr, $([$($arg: expr),*] => $out: expr),* ) => {
2019-10-10 12:28:14 +00:00
#[test]
fn $test() -> Result<(), Error> {
with_avm($version, |avm, context, this| {
$(
2019-10-21 10:44:21 +00:00
#[allow(unused_mut)]
let mut args: Vec<Value> = Vec::new();
$(
args.push($arg.into());
)*
assert_eq!($fun(avm, context, this, &args), Some($out.into()));
2019-10-10 12:28:14 +00:00
)*
Ok(())
})
}
};
}
test_std!(boolean_function, boolean, 19,
[true] => true,
[false] => false,
[10.0] => true,
[-10.0] => true,
[0.0] => false,
[std::f64::INFINITY] => true,
[std::f64::NAN] => false,
[""] => false,
["Hello"] => true,
[" "] => true,
["0"] => true,
["1"] => true,
[] => false
2019-10-10 12:28:14 +00:00
);
test_std!(boolean_function_swf6, boolean, 6,
[true] => true,
[false] => false,
[10.0] => true,
[-10.0] => true,
[0.0] => false,
[std::f64::INFINITY] => true,
[std::f64::NAN] => false,
[""] => false,
["Hello"] => false,
[" "] => false,
["0"] => false,
["1"] => true,
[] => false
2019-10-10 12:28:14 +00:00
);
test_std!(is_nan_function, is_nan, 19,
[true] => false,
[false] => false,
[10.0] => false,
[-10.0] => false,
[0.0] => false,
[std::f64::INFINITY] => false,
[std::f64::NAN] => true,
[""] => false,
["Hello"] => true,
[" "] => true,
[" 5 "] => true,
["0"] => false,
["1"] => false,
["Infinity"] => true,
["100a"] => true,
["0x10"] => false,
["0xhello"] => true,
["0x1999999981ffffff"] => false,
["0xUIXUIDFKHJDF012345678"] => true,
["123e-1"] => false,
[] => true
);
test_std!(number_function, number, 19,
[true] => 1.0,
[false] => 0.0,
[10.0] => 10.0,
[-10.0] => -10.0,
[0.0] => 0.0,
[std::f64::INFINITY] => std::f64::INFINITY,
[std::f64::NAN] => std::f64::NAN,
[""] => 0.0,
["Hello"] => std::f64::NAN,
[" "] => std::f64::NAN,
[" 5 "] => std::f64::NAN,
["0"] => 0.0,
["1"] => 1.0,
["Infinity"] => std::f64::NAN,
["100a"] => std::f64::NAN,
["0x10"] => 16.0,
["0xhello"] => std::f64::NAN,
["123e-1"] => 12.3,
["0x1999999981ffffff"] => -2113929217.0,
["0xUIXUIDFKHJDF012345678"] => std::f64::NAN,
[] => 0.0
);
2019-10-10 12:28:14 +00:00
}