2019-09-20 19:11:33 +00:00
|
|
|
use crate::avm1::fscommand;
|
2019-10-23 18:15:46 +00:00
|
|
|
use crate::avm1::function::Executable;
|
2019-10-21 22:37:04 +00:00
|
|
|
use crate::avm1::return_value::ReturnValue;
|
2019-10-25 03:21:35 +00:00
|
|
|
use crate::avm1::{Avm1, Error, Object, ObjectCell, ScriptObject, UpdateContext, 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-10-25 03:21:35 +00:00
|
|
|
use gc_arena::MutationContext;
|
2019-09-02 03:03:50 +00:00
|
|
|
use rand::Rng;
|
2019-11-22 22:32:57 +00:00
|
|
|
use std::f64;
|
2019-09-02 18:45:19 +00:00
|
|
|
|
2019-10-17 02:31:41 +00:00
|
|
|
mod function;
|
2019-09-02 18:45:19 +00:00
|
|
|
mod math;
|
2019-10-17 02:31:41 +00:00
|
|
|
mod movie_clip;
|
|
|
|
mod object;
|
2019-09-02 18:45:19 +00:00
|
|
|
|
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-10-27 18:58:30 +00:00
|
|
|
context: &mut UpdateContext<'a, 'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
_this: ObjectCell<'gc>,
|
2019-09-01 22:40:32 +00:00
|
|
|
args: &[Value<'gc>],
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
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);
|
2019-10-31 00:25:52 +00:00
|
|
|
return Ok(Value::Undefined.into());
|
2019-09-20 19:11:33 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-10-31 00:25:52 +00:00
|
|
|
Ok(Value::Undefined.into())
|
2019-09-01 22:40:32 +00:00
|
|
|
}
|
|
|
|
|
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-10-27 18:58:30 +00:00
|
|
|
action_context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
_this: ObjectCell<'gc>,
|
2019-09-02 00:23:55 +00:00
|
|
|
args: &[Value<'gc>],
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
2019-09-02 03:03:50 +00:00
|
|
|
match args.get(0) {
|
2019-11-05 02:47:21 +00:00
|
|
|
Some(Value::Number(max)) => Ok(action_context.rng.gen_range(0.0f64, max).floor().into()),
|
2019-10-31 00:25:52 +00:00
|
|
|
_ => Ok(Value::Undefined.into()), //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>,
|
2019-10-27 18:58:30 +00:00
|
|
|
_action_context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
_this: ObjectCell<'gc>,
|
2019-10-10 12:28:14 +00:00
|
|
|
args: &[Value<'gc>],
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
2019-10-10 12:28:14 +00:00
|
|
|
if let Some(val) = args.get(0) {
|
2019-11-05 02:47:21 +00:00
|
|
|
Ok(val.as_bool(avm.current_swf_version()).into())
|
2019-10-10 12:28:14 +00:00
|
|
|
} else {
|
2019-11-05 02:47:21 +00:00
|
|
|
Ok(false.into())
|
2019-10-10 12:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:11:02 +00:00
|
|
|
pub fn number<'gc>(
|
2019-10-27 00:35:22 +00:00
|
|
|
avm: &mut Avm1<'gc>,
|
|
|
|
action_context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
_this: ObjectCell<'gc>,
|
2019-10-11 14:11:02 +00:00
|
|
|
args: &[Value<'gc>],
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
2019-10-11 14:11:02 +00:00
|
|
|
if let Some(val) = args.get(0) {
|
2019-10-27 00:35:22 +00:00
|
|
|
Ok(val.as_number(avm, action_context)?.into())
|
2019-10-11 14:11:02 +00:00
|
|
|
} else {
|
2019-11-05 02:47:21 +00:00
|
|
|
Ok(0.0.into())
|
2019-10-11 14:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_nan<'gc>(
|
2019-10-27 00:35:22 +00:00
|
|
|
avm: &mut Avm1<'gc>,
|
|
|
|
action_context: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
_this: ObjectCell<'gc>,
|
2019-10-11 14:11:02 +00:00
|
|
|
args: &[Value<'gc>],
|
2019-10-26 03:21:14 +00:00
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
2019-10-11 14:11:02 +00:00
|
|
|
if let Some(val) = args.get(0) {
|
2019-10-27 00:35:22 +00:00
|
|
|
Ok(val.as_number(avm, action_context)?.is_nan().into())
|
2019-10-11 14:11:02 +00:00
|
|
|
} else {
|
2019-11-05 02:47:21 +00:00
|
|
|
Ok(true.into())
|
2019-10-11 14:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 22:32:57 +00:00
|
|
|
pub fn get_infinity<'gc>(
|
|
|
|
avm: &mut Avm1<'gc>,
|
|
|
|
_action_context: &mut UpdateContext<'_, 'gc, '_>,
|
|
|
|
_this: ObjectCell<'gc>,
|
|
|
|
_args: &[Value<'gc>],
|
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
|
|
|
if avm.current_swf_version() > 4 {
|
|
|
|
Ok(f64::INFINITY.into())
|
|
|
|
} else {
|
|
|
|
Ok(Value::Undefined.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_nan<'gc>(
|
|
|
|
avm: &mut Avm1<'gc>,
|
|
|
|
_action_context: &mut UpdateContext<'_, 'gc, '_>,
|
|
|
|
_this: ObjectCell<'gc>,
|
|
|
|
_args: &[Value<'gc>],
|
|
|
|
) -> Result<ReturnValue<'gc>, Error> {
|
|
|
|
if avm.current_swf_version() > 4 {
|
|
|
|
Ok(f64::NAN.into())
|
|
|
|
} else {
|
|
|
|
Ok(Value::Undefined.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 02:31:41 +00:00
|
|
|
/// This structure represents all system builtins that are used regardless of
|
|
|
|
/// whatever the hell happens to `_global`. These are, of course,
|
|
|
|
/// user-modifiable.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SystemPrototypes<'gc> {
|
2019-10-25 03:21:35 +00:00
|
|
|
pub object: ObjectCell<'gc>,
|
|
|
|
pub function: ObjectCell<'gc>,
|
|
|
|
pub movie_clip: ObjectCell<'gc>,
|
2019-10-17 02:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<'gc> gc_arena::Collect for SystemPrototypes<'gc> {
|
|
|
|
#[inline]
|
|
|
|
fn trace(&self, cc: gc_arena::CollectionContext) {
|
|
|
|
self.object.trace(cc);
|
|
|
|
self.function.trace(cc);
|
|
|
|
self.movie_clip.trace(cc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize default global scope and builtins for an AVM1 instance.
|
|
|
|
pub fn create_globals<'gc>(
|
|
|
|
gc_context: MutationContext<'gc, '_>,
|
2019-10-25 03:21:35 +00:00
|
|
|
) -> (SystemPrototypes<'gc>, Box<dyn Object<'gc> + 'gc>) {
|
|
|
|
let object_proto = ScriptObject::object_cell(gc_context, None);
|
2019-10-17 02:31:41 +00:00
|
|
|
let function_proto = function::create_proto(gc_context, object_proto);
|
|
|
|
|
|
|
|
object::fill_proto(gc_context, object_proto, function_proto);
|
2019-09-02 18:45:19 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
let movie_clip_proto: ObjectCell<'gc> =
|
|
|
|
movie_clip::create_proto(gc_context, object_proto, function_proto);
|
2019-10-11 14:11:02 +00:00
|
|
|
|
2019-10-23 18:15:46 +00:00
|
|
|
//TODO: These need to be constructors and should also set `.prototype` on each one
|
2019-10-25 03:21:35 +00:00
|
|
|
let object = ScriptObject::function(
|
2019-10-17 02:31:41 +00:00
|
|
|
gc_context,
|
2019-10-23 18:15:46 +00:00
|
|
|
Executable::Native(object::constructor),
|
|
|
|
Some(function_proto),
|
|
|
|
Some(object_proto),
|
2019-10-17 02:31:41 +00:00
|
|
|
);
|
2019-10-23 18:15:46 +00:00
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
let function = ScriptObject::function(
|
2019-10-17 02:31:41 +00:00
|
|
|
gc_context,
|
2019-10-23 18:15:46 +00:00
|
|
|
Executable::Native(function::constructor),
|
|
|
|
Some(function_proto),
|
|
|
|
Some(function_proto),
|
2019-10-17 02:31:41 +00:00
|
|
|
);
|
2019-10-25 03:21:35 +00:00
|
|
|
let movie_clip = ScriptObject::function(
|
2019-10-17 02:31:41 +00:00
|
|
|
gc_context,
|
2019-10-23 18:15:46 +00:00
|
|
|
Executable::Native(movie_clip::constructor),
|
|
|
|
Some(function_proto),
|
|
|
|
Some(movie_clip_proto),
|
2019-10-17 02:31:41 +00:00
|
|
|
);
|
|
|
|
|
2019-10-25 03:21:35 +00:00
|
|
|
let mut globals = ScriptObject::object(gc_context, Some(object_proto));
|
2019-10-25 23:30:05 +00:00
|
|
|
globals.define_value("Object", object.into(), EnumSet::empty());
|
|
|
|
globals.define_value("Function", function.into(), EnumSet::empty());
|
|
|
|
globals.define_value("MovieClip", movie_clip.into(), EnumSet::empty());
|
2019-10-17 02:31:41 +00:00
|
|
|
globals.force_set_function(
|
|
|
|
"Number",
|
|
|
|
number,
|
|
|
|
gc_context,
|
|
|
|
EnumSet::empty(),
|
|
|
|
Some(function_proto),
|
|
|
|
);
|
|
|
|
globals.force_set_function(
|
|
|
|
"Boolean",
|
|
|
|
boolean,
|
|
|
|
gc_context,
|
|
|
|
EnumSet::empty(),
|
|
|
|
Some(function_proto),
|
|
|
|
);
|
2019-10-25 23:30:05 +00:00
|
|
|
globals.define_value(
|
2019-10-17 02:31:41 +00:00
|
|
|
"Math",
|
|
|
|
Value::Object(math::create(
|
|
|
|
gc_context,
|
|
|
|
Some(object_proto),
|
|
|
|
Some(function_proto),
|
|
|
|
)),
|
|
|
|
EnumSet::empty(),
|
|
|
|
);
|
|
|
|
globals.force_set_function(
|
|
|
|
"isNaN",
|
|
|
|
is_nan,
|
|
|
|
gc_context,
|
|
|
|
EnumSet::empty(),
|
|
|
|
Some(function_proto),
|
|
|
|
);
|
|
|
|
globals.force_set_function(
|
|
|
|
"getURL",
|
|
|
|
getURL,
|
|
|
|
gc_context,
|
|
|
|
EnumSet::empty(),
|
|
|
|
Some(function_proto),
|
|
|
|
);
|
|
|
|
globals.force_set_function(
|
|
|
|
"random",
|
|
|
|
random,
|
|
|
|
gc_context,
|
|
|
|
EnumSet::empty(),
|
|
|
|
Some(function_proto),
|
|
|
|
);
|
2019-11-22 22:32:57 +00:00
|
|
|
globals.add_property("NaN", Executable::Native(get_nan), None, EnumSet::empty());
|
|
|
|
globals.add_property(
|
2019-10-08 12:21:07 +00:00
|
|
|
"Infinity",
|
2019-11-22 22:32:57 +00:00
|
|
|
Executable::Native(get_infinity),
|
|
|
|
None,
|
2019-10-08 12:21:07 +00:00
|
|
|
EnumSet::empty(),
|
|
|
|
);
|
2019-09-02 18:45:19 +00:00
|
|
|
|
2019-10-17 02:31:41 +00:00
|
|
|
(
|
|
|
|
SystemPrototypes {
|
|
|
|
object: object_proto,
|
|
|
|
function: function_proto,
|
|
|
|
movie_clip: movie_clip_proto,
|
|
|
|
},
|
2019-10-25 03:21:35 +00:00
|
|
|
Box::new(globals),
|
2019-10-17 02:31:41 +00:00
|
|
|
)
|
2019-09-17 03:37:11 +00:00
|
|
|
}
|
2019-10-10 12:28:14 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-10-14 15:30:17 +00:00
|
|
|
#[allow(clippy::unreadable_literal)]
|
2019-10-10 12:28:14 +00:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-10-14 21:31:30 +00:00
|
|
|
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());
|
|
|
|
)*
|
2019-10-26 03:21:14 +00:00
|
|
|
assert_eq!($fun(avm, context, this, &args).unwrap(), ReturnValue::Immediate($out.into()));
|
2019-10-10 12:28:14 +00:00
|
|
|
)*
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test_std!(boolean_function, boolean, 19,
|
2019-10-21 15:22:03 +00:00
|
|
|
[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,
|
2019-10-21 15:22:03 +00:00
|
|
|
[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
|
|
|
);
|
|
|
|
|
2019-10-11 14:11:02 +00:00
|
|
|
test_std!(is_nan_function, is_nan, 19,
|
2019-10-21 15:22:03 +00:00
|
|
|
[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
|
2019-10-11 14:11:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
test_std!(number_function, number, 19,
|
2019-10-21 15:22:03 +00:00
|
|
|
[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-11 14:11:02 +00:00
|
|
|
);
|
2019-10-10 12:28:14 +00:00
|
|
|
}
|