From 8a17d88c7784273f7a0a696507459223ca0db6dc Mon Sep 17 00:00:00 2001 From: relrelb Date: Wed, 16 Mar 2022 16:09:26 +0200 Subject: [PATCH] core: Remove `NavigatorBackend::time_since_launch` `core` already depends on the `instant` crate which abstracts `std::instant::Instant` and polyfills it on Web. Use it to replace `NavigatorBackend::time_since_launch` in order to make `NavigatorBackend` a little smaller and more simple. --- core/src/avm1/activation.rs | 5 ++++- core/src/avm1/object/script_object.rs | 1 + core/src/avm1/test_utils.rs | 1 + core/src/avm2/globals/flash/utils.rs | 6 +++++- core/src/backend/navigator.rs | 9 --------- core/src/context.rs | 4 ++++ core/src/player.rs | 5 +++++ desktop/src/navigator.rs | 10 ---------- web/Cargo.toml | 2 +- web/src/navigator.rs | 17 ++--------------- 10 files changed, 23 insertions(+), 37 deletions(-) diff --git a/core/src/avm1/activation.rs b/core/src/avm1/activation.rs index f16b71f6d..407a2265b 100644 --- a/core/src/avm1/activation.rs +++ b/core/src/avm1/activation.rs @@ -17,6 +17,7 @@ use crate::vminterface::Instantiator; use crate::{avm_error, avm_warn}; use gc_arena::{Gc, GcCell, MutationContext}; use indexmap::IndexMap; +use instant::Instant; use rand::Rng; use smallvec::SmallVec; use std::borrow::Cow; @@ -1114,7 +1115,9 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> { *self.context.time_offset += 1; } - let time = self.context.navigator.time_since_launch().as_millis() as u32; + let time = Instant::now() + .duration_since(self.context.start_time) + .as_millis() as u32; let result = time.wrapping_add(*self.context.time_offset); self.context.avm1.push(result.into()); Ok(FrameControl::Continue) diff --git a/core/src/avm1/object/script_object.rs b/core/src/avm1/object/script_object.rs index 1d3497937..5d84843b2 100644 --- a/core/src/avm1/object/script_object.rs +++ b/core/src/avm1/object/script_object.rs @@ -618,6 +618,7 @@ mod tests { avm1: &mut avm1, avm2: &mut avm2, external_interface: &mut Default::default(), + start_time: Instant::now(), update_start: Instant::now(), max_execution_duration: Duration::from_secs(15), focus_tracker: FocusTracker::new(gc_context), diff --git a/core/src/avm1/test_utils.rs b/core/src/avm1/test_utils.rs index d7a4a09e2..675852ad4 100644 --- a/core/src/avm1/test_utils.rs +++ b/core/src/avm1/test_utils.rs @@ -74,6 +74,7 @@ where avm1: &mut avm1, avm2: &mut avm2, external_interface: &mut Default::default(), + start_time: Instant::now(), update_start: Instant::now(), max_execution_duration: Duration::from_secs(15), focus_tracker: FocusTracker::new(gc_context), diff --git a/core/src/avm2/globals/flash/utils.rs b/core/src/avm2/globals/flash/utils.rs index fad1d0209..fea713e3a 100644 --- a/core/src/avm2/globals/flash/utils.rs +++ b/core/src/avm2/globals/flash/utils.rs @@ -3,6 +3,7 @@ use crate::avm2::object::TObject; use crate::avm2::QName; use crate::avm2::{Activation, Error, Object, Value}; +use instant::Instant; pub mod bytearray; pub mod compression_algorithm; @@ -20,7 +21,10 @@ pub fn get_timer<'gc>( _this: Option>, _args: &[Value<'gc>], ) -> Result, Error> { - Ok((activation.context.navigator.time_since_launch().as_millis() as u32).into()) + Ok((Instant::now() + .duration_since(activation.context.start_time) + .as_millis() as u32) + .into()) } /// Implements `flash.utils.getQualifiedClassName` diff --git a/core/src/backend/navigator.rs b/core/src/backend/navigator.rs index c111a66a9..32da3c3e1 100644 --- a/core/src/backend/navigator.rs +++ b/core/src/backend/navigator.rs @@ -12,7 +12,6 @@ use std::pin::Pin; use std::ptr::null; use std::sync::mpsc::{channel, Receiver, Sender}; use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker}; -use std::time::Duration; use swf::avm1::types::SendVarsMethod; use url::{ParseError, Url}; @@ -178,10 +177,6 @@ pub trait NavigatorBackend { /// Fetch data at a given URL and return it some time in the future. fn fetch(&self, url: &str, request_options: RequestOptions) -> OwnedFuture, Error>; - /// Get the amount of time since the SWF was launched. - /// Used by the `getTimer` ActionScript call. - fn time_since_launch(&mut self) -> Duration; - /// Arrange for a future to be run at some point in the... well, future. /// /// This function must be called to ensure a future is actually computed. @@ -369,10 +364,6 @@ impl NavigatorBackend for NullNavigatorBackend { Box::pin(async move { fs::read(path).map_err(Error::NetworkError) }) } - fn time_since_launch(&mut self) -> Duration { - Duration::from_millis(0) - } - fn spawn_future(&mut self, future: OwnedFuture<(), Error>) { if let Some(channel) = self.channel.as_ref() { channel.send(future).unwrap(); diff --git a/core/src/context.rs b/core/src/context.rs index 819da5ec6..4f1bd3833 100644 --- a/core/src/context.rs +++ b/core/src/context.rs @@ -146,6 +146,9 @@ pub struct UpdateContext<'a, 'gc, 'gc_context> { /// External interface for (for example) JavaScript <-> ActionScript interaction pub external_interface: &'a mut ExternalInterface<'gc>, + /// The instant at which the SWF was launched. + pub start_time: Instant, + /// The instant at which the current update started. pub update_start: Instant, @@ -315,6 +318,7 @@ impl<'a, 'gc, 'gc_context> UpdateContext<'a, 'gc, 'gc_context> { avm1: self.avm1, avm2: self.avm2, external_interface: self.external_interface, + start_time: self.start_time, update_start: self.update_start, max_execution_duration: self.max_execution_duration, focus_tracker: self.focus_tracker, diff --git a/core/src/player.rs b/core/src/player.rs index 6f167f896..50b471db6 100644 --- a/core/src/player.rs +++ b/core/src/player.rs @@ -216,6 +216,9 @@ pub struct Player { /// Time remaining until the next timer will fire. time_til_next_timer: Option, + /// The instant at which the SWF was launched. + start_time: Instant, + /// The maximum amount of time that can be called before a `Error::ExecutionTimeout` /// is raised. This defaults to 15 seconds but can be changed. max_execution_duration: Duration, @@ -309,6 +312,7 @@ impl Player { instance_counter: 0, time_til_next_timer: None, storage, + start_time: Instant::now(), max_execution_duration: Duration::from_secs(max_execution_duration), current_frame: None, }; @@ -1622,6 +1626,7 @@ impl Player { avm1, avm2, external_interface, + start_time: self.start_time, update_start: Instant::now(), max_execution_duration: self.max_execution_duration, focus_tracker, diff --git a/desktop/src/navigator.rs b/desktop/src/navigator.rs index 344d0c909..6ee714f02 100644 --- a/desktop/src/navigator.rs +++ b/desktop/src/navigator.rs @@ -11,7 +11,6 @@ use std::borrow::Cow; use std::fs; use std::rc::Rc; use std::sync::mpsc::Sender; -use std::time::{Duration, Instant}; use url::Url; use winit::event_loop::EventLoopProxy; @@ -27,9 +26,6 @@ pub struct ExternalNavigatorBackend { /// The url to use for all relative fetches. movie_url: Url, - /// The time that the SWF was launched. - start_time: Instant, - // Client to use for network requests client: Option>, @@ -37,7 +33,6 @@ pub struct ExternalNavigatorBackend { } impl ExternalNavigatorBackend { - #[allow(dead_code)] /// Construct a navigator backend with fetch and async capability. pub fn new( movie_url: Url, @@ -58,7 +53,6 @@ impl ExternalNavigatorBackend { event_loop, client, movie_url, - start_time: Instant::now(), upgrade_to_https, } } @@ -165,10 +159,6 @@ impl NavigatorBackend for ExternalNavigatorBackend { } } - fn time_since_launch(&mut self) -> Duration { - Instant::now().duration_since(self.start_time) - } - fn spawn_future(&mut self, future: OwnedFuture<(), Error>) { self.channel.send(future).expect("working channel send"); diff --git a/web/Cargo.toml b/web/Cargo.toml index d486c4259..0ce7950d0 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -58,6 +58,6 @@ features = [ "AddEventListenerOptions", "AudioBuffer", "AudioBufferSourceNode", "AudioParam", "AudioProcessingEvent", "AudioContext", "AudioDestinationNode", "AudioNode", "CanvasRenderingContext2d", "ChannelMergerNode", "ChannelSplitterNode", "CssStyleDeclaration", "Document", "Element", "Event", "EventTarget", "GainNode", "Gpu", "HtmlCanvasElement", "HtmlElement", "HtmlImageElement", "MouseEvent", - "Navigator", "Node", "Performance", "PointerEvent", "ScriptProcessorNode", "UiEvent", "Window", "Location", "HtmlFormElement", + "Navigator", "Node", "PointerEvent", "ScriptProcessorNode", "UiEvent", "Window", "Location", "HtmlFormElement", "KeyboardEvent", "Path2d", "CanvasGradient", "CanvasPattern", "SvgMatrix", "SvgsvgElement", "Response", "Request", "RequestInit", "Blob", "BlobPropertyBag", "Storage", "WheelEvent", "ImageData"] diff --git a/web/src/navigator.rs b/web/src/navigator.rs index 867119e15..74c0aca71 100644 --- a/web/src/navigator.rs +++ b/web/src/navigator.rs @@ -6,17 +6,12 @@ use ruffle_core::backend::navigator::{ use ruffle_core::indexmap::IndexMap; use ruffle_core::loader::Error; use std::borrow::Cow; -use std::time::Duration; use url::Url; use wasm_bindgen::JsCast; use wasm_bindgen_futures::{spawn_local, JsFuture}; -use web_sys::{ - window, Blob, BlobPropertyBag, Document, Performance, Request, RequestInit, Response, -}; +use web_sys::{window, Blob, BlobPropertyBag, Document, Request, RequestInit, Response}; pub struct WebNavigatorBackend { - performance: Performance, - start_time: f64, allow_script_access: bool, upgrade_to_https: bool, base_url: Option, @@ -29,7 +24,6 @@ impl WebNavigatorBackend { mut base_url: Option, ) -> Self { let window = web_sys::window().expect("window()"); - let performance = window.performance().expect("window.performance()"); // Upgrade to HTTPS takes effect if the current page is hosted on HTTPS. let upgrade_to_https = @@ -60,9 +54,7 @@ impl WebNavigatorBackend { } } - WebNavigatorBackend { - start_time: performance.now(), - performance, + Self { allow_script_access, upgrade_to_https, base_url, @@ -165,11 +157,6 @@ impl NavigatorBackend for WebNavigatorBackend { } } - fn time_since_launch(&mut self) -> Duration { - let dt = self.performance.now() - self.start_time; - Duration::from_millis(dt as u64) - } - fn fetch(&self, url: &str, options: RequestOptions) -> OwnedFuture, Error> { let url = if let Ok(parsed_url) = Url::parse(url) { self.pre_process_url(parsed_url).to_string()