ruffle/core/src/avm1/globals.rs

72 lines
2.2 KiB
Rust

use crate::avm1::fscommand;
use crate::avm1::{ActionContext, Avm1, Object, Value};
use crate::backend::navigator::NavigationMethod;
use enumset::EnumSet;
use gc_arena::{GcCell, MutationContext};
use rand::Rng;
mod math;
#[allow(non_snake_case, unused_must_use)] //can't use errors yet
pub fn getURL<'a, 'gc>(
avm: &mut Avm1<'gc>,
context: &mut ActionContext<'a, 'gc, '_>,
_this: GcCell<'gc, Object<'gc>>,
args: &[Value<'gc>],
) -> 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();
if let Some(fscommand) = fscommand::parse(&url) {
fscommand::handle(fscommand, avm, context);
return Value::Undefined;
}
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),
_ => None,
};
let vars_method = method.map(|m| (m, avm.locals_into_form_values()));
context.navigator.navigate_to_url(url, window, vars_method);
}
Value::Undefined
}
pub fn random<'gc>(
_avm: &mut Avm1<'gc>,
action_context: &mut ActionContext<'_, 'gc, '_>,
_this: GcCell<'gc, Object<'gc>>,
args: &[Value<'gc>],
) -> Value<'gc> {
match args.get(0) {
Some(Value::Number(max)) => {
Value::Number(action_context.rng.gen_range(0.0f64, max).floor())
}
_ => Value::Undefined, //TODO: Shouldn't this be an error condition?
}
}
pub fn create_globals<'gc>(gc_context: MutationContext<'gc, '_>) -> Object<'gc> {
let mut globals = Object::object(gc_context);
globals.force_set(
"Math",
Value::Object(math::create(gc_context)),
EnumSet::empty(),
);
globals.force_set_function("getURL", getURL, 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
}