Skip to content

Commit

Permalink
make it compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistonight committed Jan 3, 2024
1 parent 5a2c918 commit ba05e92
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 92 deletions.
33 changes: 0 additions & 33 deletions compiler-base/src/env/env_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, T>(budget: u32, iter: I) -> Vec<T>
where
I: IntoIterator,
I::Item: Future<Output = T> + 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()
}
24 changes: 5 additions & 19 deletions compiler-base/src/env/env_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<I, T>(budget: u32, iter: I) -> Vec<T>
where
I: IntoIterator,
I::Item: Future<Output = T>,
{
let mut results = Vec::new();
for future in iter {
yield_budget(budget).await;
results.push(future.await);
}
results
}
4 changes: 2 additions & 2 deletions compiler-core/src/comp/comp_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,7 +58,7 @@ pub struct LineContext<'c, 'p> {
pub errors: Vec<CompError>,
}

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();
Expand Down
13 changes: 9 additions & 4 deletions compiler-core/src/comp/comp_section.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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,
};
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)]
Expand Down Expand Up @@ -43,7 +43,7 @@ impl CompSection {
// }
}

impl<'p> CompilerInternal<'p> {
impl<'p> Compiler<'p> {
pub fn compile_preface(&self, value: RouteBlobRef<'p>) -> Result<DocRichText, RouteBlobError> {
let value = value.checked()?;
let text = value.coerce_into_string();
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 0 additions & 34 deletions compiler-core/src/comp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompileContext<'p>> for CompilerInternal<'p> {
fn as_ref(&self) -> &CompileContext<'p> {
&self.ctx
}
}

impl<'p> AsMut<CompileContext<'p>> 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> {
Expand Down

0 comments on commit ba05e92

Please sign in to comment.