2019-09-20 19:11:33 +00:00
|
|
|
use crate::avm1::fscommand;
|
2019-09-26 18:45:45 +00:00
|
|
|
use crate::avm1::{ActionContext, Avm1, Object, Value};
|
2019-09-14 02:53:42 +00:00
|
|
|
use crate::backend::navigator::NavigationMethod;
|
2019-10-08 12:21:07 +00:00
|
|
|
use enumset::EnumSet;
|
2019-09-17 03:37:11 +00:00
|
|
|
use gc_arena::{GcCell, MutationContext};
|
2019-09-02 03:03:50 +00:00
|
|
|
use rand::Rng;
|
2019-09-02 18:45:19 +00:00
|
|
|
|
|
|
|
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>(
|
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> {
|
2019-09-14 03:03:24 +00:00
|
|
|
//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 Value::Undefined;
|
|
|
|
}
|
2019-09-26 18:45:45 +00:00
|
|
|
|
2019-09-14 03:03:24 +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-09-14 03:03:24 +00:00
|
|
|
};
|
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
|
|
|
|
2019-09-14 03:03:24 +00:00
|
|
|
context.navigator.navigate_to_url(url, window, vars_method);
|
2019-09-01 22:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-09-17 03:37:11 +00:00
|
|
|
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 03:03:50 +00:00
|
|
|
}
|
2019-09-02 00:23:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 12:28:14 +00:00
|
|
|
pub fn boolean<'gc>(
|
|
|
|
avm: &mut Avm1<'gc>,
|
|
|
|
_action_context: &mut ActionContext<'_, 'gc, '_>,
|
|
|
|
_this: GcCell<'gc, Object<'gc>>,
|
|
|
|
args: &[Value<'gc>],
|
|
|
|
) -> Value<'gc> {
|
|
|
|
if let Some(val) = args.get(0) {
|
|
|
|
Value::Bool(val.as_bool(avm.current_swf_version()))
|
|
|
|
} else {
|
|
|
|
Value::Bool(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:11:02 +00:00
|
|
|
pub fn number<'gc>(
|
|
|
|
_avm: &mut Avm1<'gc>,
|
|
|
|
_action_context: &mut ActionContext<'_, 'gc, '_>,
|
|
|
|
_this: GcCell<'gc, Object<'gc>>,
|
|
|
|
args: &[Value<'gc>],
|
|
|
|
) -> Value<'gc> {
|
|
|
|
if let Some(val) = args.get(0) {
|
|
|
|
Value::Number(val.as_number())
|
|
|
|
} else {
|
|
|
|
Value::Number(0.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_nan<'gc>(
|
|
|
|
_avm: &mut Avm1<'gc>,
|
|
|
|
_action_context: &mut ActionContext<'_, 'gc, '_>,
|
|
|
|
_this: GcCell<'gc, Object<'gc>>,
|
|
|
|
args: &[Value<'gc>],
|
|
|
|
) -> Value<'gc> {
|
|
|
|
if let Some(val) = args.get(0) {
|
|
|
|
Value::Bool(val.as_number().is_nan())
|
|
|
|
} else {
|
|
|
|
Value::Bool(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-11 14:11:02 +00:00
|
|
|
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());
|
2019-10-08 12:21:07 +00:00
|
|
|
globals.force_set(
|
|
|
|
"Math",
|
|
|
|
Value::Object(math::create(gc_context)),
|
|
|
|
EnumSet::empty(),
|
|
|
|
);
|
|
|
|
globals.force_set_function("getURL", getURL, gc_context, EnumSet::empty());
|
2019-10-11 14:11:02 +00:00
|
|
|
globals.force_set_function("Number", number, gc_context, EnumSet::empty());
|
2019-10-08 12:21:07 +00:00
|
|
|
globals.force_set_function("random", random, gc_context, EnumSet::empty());
|
2019-10-11 14:11:02 +00:00
|
|
|
|
2019-10-08 12:21:07 +00:00
|
|
|
globals.force_set("NaN", Value::Number(std::f64::NAN), EnumSet::empty());
|
|
|
|
globals.force_set(
|
|
|
|
"Infinity",
|
|
|
|
Value::Number(std::f64::INFINITY),
|
|
|
|
EnumSet::empty(),
|
|
|
|
);
|
2019-09-02 18:45:19 +00:00
|
|
|
|
|
|
|
globals
|
2019-09-17 03:37:11 +00:00
|
|
|
}
|
2019-10-10 12:28:14 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::avm1::activation::Activation;
|
|
|
|
use crate::avm1::Error;
|
|
|
|
use crate::backend::audio::NullAudioBackend;
|
|
|
|
use crate::backend::navigator::NullNavigatorBackend;
|
|
|
|
use crate::display_object::DisplayObject;
|
|
|
|
use crate::movie_clip::MovieClip;
|
|
|
|
use gc_arena::rootless_arena;
|
|
|
|
use rand::{rngs::SmallRng, SeedableRng};
|
|
|
|
|
|
|
|
macro_rules! test_std {
|
|
|
|
( $test: ident, $fun: expr, $version: expr, $($args: expr => $out: expr),* ) => {
|
|
|
|
#[test]
|
|
|
|
fn $test() -> Result<(), Error> {
|
|
|
|
with_avm($version, |avm, context, this| {
|
|
|
|
|
|
|
|
$(
|
|
|
|
assert_eq!($fun(avm, context, this, $args), $out);
|
|
|
|
)*
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_avm<F, R>(swf_version: u8, test: F) -> R
|
|
|
|
where
|
|
|
|
F: for<'a, 'gc> FnOnce(
|
|
|
|
&mut Avm1<'gc>,
|
|
|
|
&mut ActionContext<'a, 'gc, '_>,
|
|
|
|
GcCell<'gc, Object<'gc>>,
|
|
|
|
) -> R,
|
|
|
|
{
|
|
|
|
rootless_arena(|gc_context| {
|
|
|
|
let mut avm = Avm1::new(gc_context);
|
|
|
|
let movie_clip: Box<dyn DisplayObject> = Box::new(MovieClip::new(gc_context));
|
|
|
|
let root = GcCell::allocate(gc_context, movie_clip);
|
|
|
|
let mut context = ActionContext {
|
|
|
|
gc_context,
|
|
|
|
global_time: 0,
|
|
|
|
root,
|
|
|
|
start_clip: root,
|
|
|
|
active_clip: root,
|
|
|
|
target_clip: Some(root),
|
|
|
|
target_path: Value::Undefined,
|
|
|
|
rng: &mut SmallRng::from_seed([0u8; 16]),
|
|
|
|
audio: &mut NullAudioBackend::new(),
|
|
|
|
navigator: &mut NullNavigatorBackend::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let globals = avm.global_object_cell();
|
|
|
|
avm.insert_stack_frame(Activation::from_nothing(swf_version, globals, gc_context));
|
|
|
|
|
|
|
|
let this = root.read().object().as_object().unwrap().to_owned();
|
|
|
|
|
|
|
|
test(&mut avm, &mut context, this)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
test_std!(boolean_function, boolean, 19,
|
|
|
|
&[Value::Bool(true)] => Value::Bool(true),
|
|
|
|
&[Value::Bool(false)] => Value::Bool(false),
|
|
|
|
&[Value::Number(10.0)] => Value::Bool(true),
|
|
|
|
&[Value::Number(-10.0)] => Value::Bool(true),
|
|
|
|
&[Value::Number(0.0)] => Value::Bool(false),
|
|
|
|
&[Value::Number(std::f64::INFINITY)] => Value::Bool(true),
|
|
|
|
&[Value::Number(std::f64::NAN)] => Value::Bool(false),
|
|
|
|
&[Value::String("".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String("Hello".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String(" ".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String("0".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String("1".to_string())] => Value::Bool(true),
|
|
|
|
&[] => Value::Bool(false)
|
|
|
|
);
|
|
|
|
|
|
|
|
test_std!(boolean_function_swf6, boolean, 6,
|
|
|
|
&[Value::Bool(true)] => Value::Bool(true),
|
|
|
|
&[Value::Bool(false)] => Value::Bool(false),
|
|
|
|
&[Value::Number(10.0)] => Value::Bool(true),
|
|
|
|
&[Value::Number(-10.0)] => Value::Bool(true),
|
|
|
|
&[Value::Number(0.0)] => Value::Bool(false),
|
|
|
|
&[Value::Number(std::f64::INFINITY)] => Value::Bool(true),
|
|
|
|
&[Value::Number(std::f64::NAN)] => Value::Bool(false),
|
|
|
|
&[Value::String("".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String("Hello".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String(" ".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String("0".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String("1".to_string())] => Value::Bool(true),
|
|
|
|
&[] => Value::Bool(false)
|
|
|
|
);
|
|
|
|
|
2019-10-11 14:11:02 +00:00
|
|
|
test_std!(is_nan_function, is_nan, 19,
|
|
|
|
&[Value::Bool(true)] => Value::Bool(false),
|
|
|
|
&[Value::Bool(false)] => Value::Bool(false),
|
|
|
|
&[Value::Number(10.0)] => Value::Bool(false),
|
|
|
|
&[Value::Number(-10.0)] => Value::Bool(false),
|
|
|
|
&[Value::Number(0.0)] => Value::Bool(false),
|
|
|
|
&[Value::Number(std::f64::INFINITY)] => Value::Bool(false),
|
|
|
|
&[Value::Number(std::f64::NAN)] => Value::Bool(true),
|
|
|
|
&[Value::String("".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String("Hello".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String(" ".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String(" 5 ".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String("0".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String("1".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String("Infinity".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String("100a".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String("0x10".to_string())] => Value::Bool(false),
|
|
|
|
&[Value::String("0xhello".to_string())] => Value::Bool(true),
|
|
|
|
&[Value::String("123e-1".to_string())] => Value::Bool(false),
|
|
|
|
&[] => Value::Bool(true)
|
|
|
|
);
|
|
|
|
|
|
|
|
test_std!(number_function, number, 19,
|
|
|
|
&[Value::Bool(true)] => Value::Number(1.0),
|
|
|
|
&[Value::Bool(false)] => Value::Number(0.0),
|
|
|
|
&[Value::Number(10.0)] => Value::Number(10.0),
|
|
|
|
&[Value::Number(-10.0)] => Value::Number(-10.0),
|
|
|
|
&[Value::Number(0.0)] => Value::Number(0.0),
|
|
|
|
&[Value::Number(std::f64::INFINITY)] => Value::Number(std::f64::INFINITY),
|
|
|
|
&[Value::Number(std::f64::NAN)] => Value::Number(std::f64::NAN),
|
|
|
|
&[Value::String("".to_string())] => Value::Number(0.0),
|
|
|
|
&[Value::String("Hello".to_string())] => Value::Number(std::f64::NAN),
|
|
|
|
&[Value::String(" ".to_string())] => Value::Number(std::f64::NAN),
|
|
|
|
&[Value::String(" 5 ".to_string())] => Value::Number(std::f64::NAN),
|
|
|
|
&[Value::String("0".to_string())] => Value::Number(0.0),
|
|
|
|
&[Value::String("1".to_string())] => Value::Number(1.0),
|
|
|
|
&[Value::String("Infinity".to_string())] => Value::Number(std::f64::NAN),
|
|
|
|
&[Value::String("100a".to_string())] => Value::Number(std::f64::NAN),
|
|
|
|
&[Value::String("0x10".to_string())] => Value::Number(16.0),
|
|
|
|
&[Value::String("0xhello".to_string())] => Value::Number(std::f64::NAN),
|
|
|
|
&[Value::String("123e-1".to_string())] => Value::Number(12.3),
|
|
|
|
&[] => Value::Number(0.0)
|
|
|
|
);
|
|
|
|
|
2019-10-10 12:28:14 +00:00
|
|
|
}
|