2019-09-03 00:44:24 +00:00
|
|
|
use crate::avm1::{Object, Value, ActionContext, Avm1};
|
2019-09-14 02:53:42 +00:00
|
|
|
use crate::backend::navigator::NavigationMethod;
|
2019-09-01 22:40:32 +00:00
|
|
|
use gc_arena::{MutationContext, GcCell};
|
2019-09-02 03:03:50 +00:00
|
|
|
use rand::Rng;
|
2019-09-02 18:45:19 +00:00
|
|
|
|
|
|
|
mod math;
|
|
|
|
|
2019-09-01 22:40:32 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn getURL<'a, 'gc>(
|
2019-09-14 02:53:42 +00:00
|
|
|
avm: &mut Avm1<'gc>,
|
2019-09-01 22:40:32 +00:00
|
|
|
context: &mut ActionContext<'a, 'gc, '_>,
|
|
|
|
_this: GcCell<'gc, Object<'gc>>,
|
|
|
|
args: &[Value<'gc>],
|
|
|
|
) -> Value<'gc> {
|
|
|
|
match args.get(0) {
|
|
|
|
Some(url_val) => {
|
|
|
|
let url = url_val.clone().into_string();
|
|
|
|
let window = args.get(1).map(|v| v.clone().into_string());
|
2019-09-14 02:53:42 +00:00
|
|
|
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);
|
2019-09-01 22:40:32 +00:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
//TODO: Does AVM1 error out?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Undefined
|
|
|
|
}
|
|
|
|
|
2019-09-02 00:23:55 +00:00
|
|
|
pub fn random<'gc>(
|
2019-09-03 00:44:24 +00:00
|
|
|
_avm: &mut Avm1<'gc>,
|
2019-09-02 03:03:50 +00:00
|
|
|
action_context: &mut ActionContext<'_, 'gc, '_>,
|
2019-09-02 00:23:55 +00:00
|
|
|
_this: GcCell<'gc, Object<'gc>>,
|
|
|
|
args: &[Value<'gc>],
|
|
|
|
) -> Value<'gc> {
|
2019-09-02 03:03:50 +00:00
|
|
|
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?
|
|
|
|
}
|
2019-09-02 00:23:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 18:45:19 +00:00
|
|
|
pub fn create_globals<'gc>(gc_context: MutationContext<'gc, '_>) -> Object<'gc> {
|
|
|
|
let mut globals = Object::object(gc_context);
|
|
|
|
|
2019-09-02 20:19:09 +00:00
|
|
|
globals.set_object("Math", math::create(gc_context));
|
2019-09-01 22:40:32 +00:00
|
|
|
globals.set_function("getURL", getURL, gc_context);
|
2019-09-02 00:23:55 +00:00
|
|
|
globals.set_function("random", random, gc_context);
|
2019-09-02 18:45:19 +00:00
|
|
|
|
|
|
|
globals
|
2019-09-02 00:23:55 +00:00
|
|
|
}
|