From ba05e9292c382ea623386a64f44aef90d153eb37 Mon Sep 17 00:00:00 2001 From: Pistonight Date: Wed, 3 Jan 2024 13:44:10 -0800 Subject: [PATCH] make it compile --- compiler-base/src/env/env_native.rs | 33 ------------------------- compiler-base/src/env/env_wasm.rs | 24 ++++-------------- compiler-core/src/comp/comp_line.rs | 4 +-- compiler-core/src/comp/comp_section.rs | 13 +++++++--- compiler-core/src/comp/mod.rs | 34 -------------------------- 5 files changed, 16 insertions(+), 92 deletions(-) diff --git a/compiler-base/src/env/env_native.rs b/compiler-base/src/env/env_native.rs index 79fa097d..ef9661c5 100644 --- a/compiler-base/src/env/env_native.rs +++ b/compiler-base/src/env/env_native.rs @@ -49,36 +49,3 @@ pub async fn yield_budget(limit: u32) { /// Wait for multiple futures to complete pub use tokio::join as join_futures; - -pub async fn iter_futures(budget: u32, iter: I) -> Vec -where - I: IntoIterator, - I::Item: Future + Send + 'static, - T: Send + 'static -{ - let mut set = JoinSet::new(); - let mut results = Vec::new(); - for (i, future) in iter.into_iter().enumerate() { - set.spawn(async move { (i, future.await) }); - results.push(None); - } - let mut joined: usize = 0; - while let Some(result) = set.join_next().await { - joined += 1; - match result { - Ok((i, result)) => { - *results.get_mut(i).unwrap() = Some(result); - } - Err(e) => { - if e.is_panic() { - panic!("Panic in async task: {:?}", e); - } - } - } - yield_budget(budget).await; - } - if joined != results.len() { - panic!("Not all futures joined"); - } - results.into_iter().map(|r| r.unwrap()).collect() -} diff --git a/compiler-base/src/env/env_wasm.rs b/compiler-base/src/env/env_wasm.rs index adf5d927..b3dd54f2 100644 --- a/compiler-base/src/env/env_wasm.rs +++ b/compiler-base/src/env/env_wasm.rs @@ -48,8 +48,11 @@ pub async fn yield_budget(limit: u32) { use wasm_bindgen_futures::JsFuture; use web_sys::WorkerGlobalScope; - let global_self: WorkerGlobalScope = - Reflect::get(&global(), &JsValue::from("self"))?.dyn_into()?; + let self_value = JsValue::from("self"); + let global_obj = global(); + let global_self: WorkerGlobalScope = unsafe { + Reflect::get(&global_obj, &self_value) + }?.dyn_into()?; let promise = Promise::new(&mut |resolve, _| { let _ = global_self.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, 0); @@ -76,20 +79,3 @@ macro_rules! join_futures { }; } pub use join_futures; - -/// Wait for multiple futures to complete and collect their results -/// in the same order -/// -/// On WASM, futures are simply executed sequentially -pub async fn iter_futures(budget: u32, iter: I) -> Vec -where - I: IntoIterator, - I::Item: Future, -{ - let mut results = Vec::new(); - for future in iter { - yield_budget(budget).await; - results.push(future.await); - } - results -} diff --git a/compiler-core/src/comp/comp_line.rs b/compiler-core/src/comp/comp_line.rs index d0c5f45f..1e09989f 100644 --- a/compiler-core/src/comp/comp_line.rs +++ b/compiler-core/src/comp/comp_line.rs @@ -9,7 +9,7 @@ use crate::pack::{Compiler, PackError}; use crate::prep::GameCoord; use crate::util::StringMap; -use super::{CompError, CompMarker, CompMovement, CompResult, DocNote, CompilerInternal}; +use super::{CompError, CompMarker, CompMovement, CompResult, DocNote}; #[derive(PartialEq, Default, Serialize, Deserialize, Debug, Clone)] #[derive_wasm] pub struct CompLine { @@ -58,7 +58,7 @@ pub struct LineContext<'c, 'p> { pub errors: Vec, } -impl<'p> CompilerInternal<'p> { +impl<'p> Compiler<'p> { /// Parse the line (parallel pass) pub fn parse_line(&self, value: RouteBlobRef<'p>) -> CompLine { let mut ctx = self.create_line_context(); diff --git a/compiler-core/src/comp/comp_section.rs b/compiler-core/src/comp/comp_section.rs index 2fe64da8..cd74ec79 100644 --- a/compiler-core/src/comp/comp_section.rs +++ b/compiler-core/src/comp/comp_section.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::env::{iter_futures, yield_budget}; +use crate::env::{yield_budget}; use crate::json::{ Cast, Coerce, RouteBlobArrayIterResult, RouteBlobError, RouteBlobRef, RouteBlobSingleKeyObjectResult, @@ -8,7 +8,7 @@ use crate::json::{ use crate::lang::{self, DocDiagnostic, DocRichText, IntoDiagnostic}; use crate::pack::PackError; -use super::{CompError, CompLine, CompResult, Compiler, CompilerInternal}; +use super::{CompError, CompLine, CompResult, Compiler}; /// Compiled Section #[derive(Default, Serialize, Deserialize, Debug, Clone)] @@ -43,7 +43,7 @@ impl CompSection { // } } -impl<'p> CompilerInternal<'p> { +impl<'p> Compiler<'p> { pub fn compile_preface(&self, value: RouteBlobRef<'p>) -> Result { let value = value.checked()?; let text = value.coerce_into_string(); @@ -104,7 +104,12 @@ impl<'p> CompilerInternal<'p> { } }; - let lines = iter_futures(64, array.map(|line| async { self.parse_line(line) })).await; + let mut lines = vec![]; + for line in array { + yield_budget(64).await; + lines.push(self.parse_line(line)); + } + let section = CompSection { name: name.to_owned(), lines, diff --git a/compiler-core/src/comp/mod.rs b/compiler-core/src/comp/mod.rs index 2f32d491..3c135a21 100644 --- a/compiler-core/src/comp/mod.rs +++ b/compiler-core/src/comp/mod.rs @@ -51,40 +51,6 @@ macro_rules! validate_not_array_or_object { } pub(crate) use validate_not_array_or_object; -/// The internal data of the compiler held during compilation -/// -/// This is necessary because [`Compiler`] holds reference to plugins, -/// which cannot be sent between threads -pub struct CompilerInternal<'p> { - pub ctx: CompileContext<'p>, -} - -impl<'p> Deref for CompilerInternal<'p> { - type Target = CompileContext<'p>; - - fn deref(&self) -> &Self::Target { - &self.ctx - } -} - -impl<'p> DerefMut for CompilerInternal<'p> { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.ctx - } -} - -impl<'p> AsRef> for CompilerInternal<'p> { - fn as_ref(&self) -> &CompileContext<'p> { - &self.ctx - } -} - -impl<'p> AsMut> for CompilerInternal<'p> { - fn as_mut(&mut self) -> &mut CompileContext<'p> { - &mut self.ctx - } -} - static DEFAULT_SETTING: Setting = Setting::const_default(); impl<'p> Compiler<'p> {