Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/api/src/backend/js/entities/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use js_sys::{Reflect, Uint8Array, WebAssembly};
use tracing::{debug, warn};
use wasm_bindgen::{JsValue, prelude::*};
use wasmer_types::{
CompileError, DeserializeError, ExportType, ExportsIterator, ExternType, FunctionType,
GlobalType, ImportType, ImportsIterator, MemoryType, ModuleInfo, Mutability, Pages,
SerializeError, TableType, Type,
CompilationProgressCallback, CompileError, DeserializeError, ExportType, ExportsIterator,
ExternType, FunctionType, GlobalType, ImportType, ImportsIterator, MemoryType, ModuleInfo,
Mutability, Pages, SerializeError, TableType, Type,
};

use crate::{
Expand Down Expand Up @@ -67,6 +67,14 @@ impl Module {
unsafe { Self::from_binary_unchecked(_engine, binary) }
}

pub(crate) fn from_binary_with_progress(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should only be needed on the sys crate. Remove this

engine: &impl AsEngineRef,
binary: &[u8],
_callback: CompilationProgressCallback,
) -> Result<Self, CompileError> {
Self::from_binary(engine, binary)
}

pub(crate) unsafe fn from_binary_unchecked(
_engine: &impl AsEngineRef,
binary: &[u8],
Expand Down
12 changes: 10 additions & 2 deletions lib/api/src/backend/jsc/entities/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use bytes::Bytes;
use rusty_jsc::{JSObject, JSValue};
use tracing::warn;
use wasmer_types::{
CompileError, DeserializeError, ExportType, ExportsIterator, ImportType, ImportsIterator,
ModuleInfo, SerializeError,
CompilationProgressCallback, CompileError, DeserializeError, ExportType, ExportsIterator,
ImportType, ImportsIterator, ModuleInfo, SerializeError,
};

use crate::{
Expand Down Expand Up @@ -43,6 +43,14 @@ impl Module {
unsafe { Self::from_binary_unchecked(_engine, binary) }
}

pub(crate) fn from_binary_with_progress(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should only be needed on the sys crate. Remove this

engine: &impl AsEngineRef,
binary: &[u8],
_callback: CompilationProgressCallback,
) -> Result<Self, CompileError> {
Self::from_binary(engine, binary)
}

pub(crate) unsafe fn from_binary_unchecked(
engine: &impl AsEngineRef,
binary: &[u8],
Expand Down
4 changes: 3 additions & 1 deletion lib/api/src/backend/sys/entities/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{path::Path, sync::Arc};

use shared_buffer::OwnedBuffer;
pub use wasmer_compiler::{Artifact, BaseTunables, Engine, EngineBuilder, Tunables};
use wasmer_types::{DeserializeError, Features, HashAlgorithm, target::Target};
use wasmer_types::{
CompilationProgressCallback, DeserializeError, Features, HashAlgorithm, target::Target,
};

use crate::{BackendEngine, BackendModule};

Expand Down
48 changes: 44 additions & 4 deletions lib/api/src/backend/sys/entities/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::sync::Arc;
use bytes::Bytes;
use wasmer_compiler::{Artifact, ArtifactCreate, Engine};
use wasmer_types::{
CompileError, DeserializeError, ExportType, ExportsIterator, ImportType, ImportsIterator,
ModuleInfo, SerializeError,
CompilationProgressCallback, CompileError, DeserializeError, ExportType, ExportsIterator,
ImportType, ImportsIterator, ModuleInfo, SerializeError,
};

use crate::{
Expand Down Expand Up @@ -45,12 +45,38 @@ impl Module {
unsafe { Self::from_binary_unchecked(engine, binary) }
}

pub(crate) fn from_binary_with_progress(
engine: &impl AsEngineRef,
binary: &[u8],
callback: CompilationProgressCallback,
) -> Result<Self, CompileError> {
Self::validate(engine, binary)?;
unsafe { Self::from_binary_unchecked_with_progress(engine, binary, Some(callback)) }
}

pub(crate) unsafe fn from_binary_unchecked(
engine: &impl AsEngineRef,
binary: &[u8],
) -> Result<Self, CompileError> {
let module = Self::compile(engine, binary)?;
Ok(module)
Self::compile(engine, binary)
}

pub(crate) unsafe fn from_binary_unchecked_with_progress(
engine: &impl AsEngineRef,
binary: &[u8],
callback: Option<CompilationProgressCallback>,
) -> Result<Self, CompileError> {
#[cfg(feature = "compiler")]
{
let module = Self::compile_with_progress(engine, binary, callback)?;
Ok(module)
}

#[cfg(not(feature = "compiler"))]
{
let _ = callback;
Self::compile(engine, binary)
}
}

#[cfg(feature = "compiler")]
Expand All @@ -72,6 +98,20 @@ impl Module {
Ok(Self::from_artifact(artifact))
}

#[cfg(feature = "compiler")]
fn compile_with_progress(
engine: &impl AsEngineRef,
binary: &[u8],
callback: Option<CompilationProgressCallback>,
) -> Result<Self, CompileError> {
let artifact = engine
.as_engine_ref()
.engine()
.as_sys()
.compile_with_progress(binary, callback)?;
Ok(Self::from_artifact(artifact))
}

#[cfg(not(feature = "compiler"))]
fn compile(_engine: &impl AsEngineRef, _binary: &[u8]) -> Result<Self, CompileError> {
Err(CompileError::UnsupportedTarget(
Expand Down
14 changes: 11 additions & 3 deletions lib/api/src/backend/v8/entities/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::{

use bytes::Bytes;
use wasmer_types::{
CompileError, DeserializeError, ExportType, ExportsIterator, ExternType, FunctionType,
GlobalType, ImportType, ImportsIterator, MemoryType, ModuleInfo, Mutability, Pages,
SerializeError, TableType, Type,
CompilationProgressCallback, CompileError, DeserializeError, ExportType, ExportsIterator,
ExternType, FunctionType, GlobalType, ImportType, ImportsIterator, MemoryType, ModuleInfo,
Mutability, Pages, SerializeError, TableType, Type,
};

#[derive(Debug)]
Expand Down Expand Up @@ -152,6 +152,14 @@ impl Module {
unsafe { Self::from_binary_unchecked(engine, binary) }
}

pub(crate) fn from_binary_with_progress(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should only be needed on the sys crate. Remove this

engine: &impl AsEngineRef,
binary: &[u8],
_callback: CompilationProgressCallback,
) -> Result<Self, CompileError> {
Self::from_binary(engine, binary)
}

#[allow(clippy::arc_with_non_send_sync)]
#[tracing::instrument(skip(engine, binary))]
pub(crate) unsafe fn from_binary_unchecked(
Expand Down
14 changes: 11 additions & 3 deletions lib/api/src/backend/wamr/entities/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::{AsEngineRef, BackendModule, IntoBytes, backend::wamr::bindings::*};

use bytes::Bytes;
use wasmer_types::{
CompileError, DeserializeError, ExportType, ExportsIterator, ExternType, FunctionType,
GlobalType, ImportType, ImportsIterator, MemoryType, ModuleInfo, Mutability, Pages,
SerializeError, TableType, Type,
CompilationProgressCallback, CompileError, DeserializeError, ExportType, ExportsIterator,
ExternType, FunctionType, GlobalType, ImportType, ImportsIterator, MemoryType, ModuleInfo,
Mutability, Pages, SerializeError, TableType, Type,
};
pub(crate) struct ModuleHandle {
pub(crate) inner: *mut wasm_module_t,
Expand Down Expand Up @@ -75,6 +75,14 @@ impl Module {
unsafe { Self::from_binary_unchecked(_engine, binary) }
}

pub(crate) fn from_binary_with_progress(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should only be needed on the sys crate. Remove this

engine: &impl AsEngineRef,
binary: &[u8],
_callback: CompilationProgressCallback,
) -> Result<Self, CompileError> {
Self::from_binary(engine, binary)
}

pub(crate) unsafe fn from_binary_unchecked(
engine: &impl AsEngineRef,
binary: &[u8],
Expand Down
14 changes: 11 additions & 3 deletions lib/api/src/backend/wasmi/entities/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use crate::{

use bytes::Bytes;
use wasmer_types::{
CompileError, DeserializeError, ExportType, ExportsIterator, ExternType, FunctionType,
GlobalType, ImportType, ImportsIterator, MemoryType, ModuleInfo, Mutability, Pages,
SerializeError, TableType, Type,
CompilationProgressCallback, CompileError, DeserializeError, ExportType, ExportsIterator,
ExternType, FunctionType, GlobalType, ImportType, ImportsIterator, MemoryType, ModuleInfo,
Mutability, Pages, SerializeError, TableType, Type,
};
pub(crate) struct ModuleHandle {
pub(crate) inner: *mut wasm_module_t,
Expand Down Expand Up @@ -74,6 +74,14 @@ impl Module {
unsafe { Self::from_binary_unchecked(_engine, binary) }
}

pub(crate) fn from_binary_with_progress(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should only be needed on the sys crate. Remove this

engine: &impl AsEngineRef,
binary: &[u8],
_callback: CompilationProgressCallback,
) -> Result<Self, CompileError> {
Self::from_binary(engine, binary)
}

#[allow(clippy::arc_with_non_send_sync)]
pub(crate) unsafe fn from_binary_unchecked(
engine: &impl AsEngineRef,
Expand Down
3 changes: 3 additions & 0 deletions lib/api/src/entities/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use crate::{BackendKind, IntoBytes, Store};
/// Create temporary handles to engines.
mod engine_ref;

mod progress_ext;
pub use progress_ext::ProgressEngineExt;

/// The actual (private) definition of the engines.
mod inner;
pub(crate) use inner::BackendEngine;
Expand Down
35 changes: 35 additions & 0 deletions lib/api/src/entities/engine/progress_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use wasmer_types::CompilationProgressCallback;

/// Provides progress-related extensions to the `Engine` trait.
pub trait ProgressEngineExt {
/// Compile a module from bytes with a progress callback.
///
/// The callback is invoked with progress updates during the compilation process.
/// The callback also may return an error to abort the compilation.
///
/// Signature of the callback function: `Fn(CompilationProgress) -> Result<(), UserAbort> + Send + Sync + 'static`
///
/// # Aborting compilation
///
/// The callback has to return a `Result<(), UserAbort>`.
///
/// If the callback returns an error, the compilation will fail with a `CompileError::Aborted`.
///
/// See [`CompilationProgressCallback::new`] for more details.
fn new_module_with_progress(
&self,
bytes: &[u8],
on_progress: CompilationProgressCallback,
) -> Result<crate::Module, wasmer_types::CompileError>;
}

impl ProgressEngineExt for crate::Engine {
Copy link
Member

@syrusakbary syrusakbary Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This impl and work should not live here. Should live only on the sys implementation that already exists (NativeEngineExt):

https://github.com/wasmerio/wasmer/blob/main/lib/api/src/backend/sys/entities/engine.rs#L53

/// See [`ProgressEngineExt::new_module_with_progress`].
fn new_module_with_progress(
&self,
bytes: &[u8],
on_progress: CompilationProgressCallback,
) -> Result<crate::Module, wasmer_types::CompileError> {
crate::BackendModule::new_with_progress(self, bytes, on_progress).map(crate::Module)
}
}
80 changes: 78 additions & 2 deletions lib/api/src/entities/module/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use thiserror::Error;
#[cfg(feature = "wat")]
use wasmer_types::WasmError;
use wasmer_types::{
CompileError, DeserializeError, ExportType, ExportsIterator, ImportType, ImportsIterator,
ModuleInfo, SerializeError,
CompilationProgressCallback, CompileError, DeserializeError, ExportType, ExportsIterator,
ImportType, ImportsIterator, ModuleInfo, SerializeError,
};

use crate::{
Expand Down Expand Up @@ -43,6 +43,21 @@ impl BackendModule {
Self::from_binary(engine, bytes.as_ref())
}

#[inline]
pub fn new_with_progress(
engine: &impl AsEngineRef,
bytes: impl AsRef<[u8]>,
callback: CompilationProgressCallback,
) -> Result<Self, CompileError> {
#[cfg(feature = "wat")]
let bytes = wat::parse_bytes(bytes.as_ref()).map_err(|e| {
CompileError::Wasm(WasmError::Generic(format!(
"Error when converting wat: {e}",
)))
})?;
Self::from_binary_with_progress(engine, bytes.as_ref(), callback)
}
Comment on lines +46 to +59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not live here


/// Creates a new WebAssembly module from a file path.
#[inline]
pub fn from_file(
Expand Down Expand Up @@ -100,6 +115,67 @@ impl BackendModule {
}
}

#[inline]
pub fn from_binary_with_progress(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete me

engine: &impl AsEngineRef,
binary: &[u8],
callback: CompilationProgressCallback,
) -> Result<Self, CompileError> {
match engine.as_engine_ref().inner.be {
#[cfg(feature = "sys")]
crate::BackendEngine::Sys(_) => Ok(Self::Sys(
crate::backend::sys::entities::module::Module::from_binary_with_progress(
engine,
binary,
callback.clone(),
)?,
)),

#[cfg(feature = "wamr")]
crate::BackendEngine::Wamr(_) => Ok(Self::Wamr(
crate::backend::wamr::entities::module::Module::from_binary_with_progress(
engine,
binary,
callback.clone(),
)?,
)),

#[cfg(feature = "wasmi")]
crate::BackendEngine::Wasmi(_) => Ok(Self::Wasmi(
crate::backend::wasmi::entities::module::Module::from_binary_with_progress(
engine,
binary,
callback.clone(),
)?,
)),

#[cfg(feature = "v8")]
crate::BackendEngine::V8(_) => Ok(Self::V8(
crate::backend::v8::entities::module::Module::from_binary_with_progress(
engine,
binary,
callback.clone(),
)?,
)),

#[cfg(feature = "js")]
crate::BackendEngine::Js(_) => Ok(Self::Js(
crate::backend::js::entities::module::Module::from_binary_with_progress(
engine,
binary,
callback.clone(),
)?,
)),

#[cfg(feature = "jsc")]
crate::BackendEngine::Jsc(_) => Ok(Self::Jsc(
crate::backend::jsc::entities::module::Module::from_binary_with_progress(
engine, binary, callback,
)?,
)),
}
}

/// Creates a new WebAssembly module from a Wasm binary,
/// skipping any kind of validation on the WebAssembly file.
///
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/entities/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use thiserror::Error;
#[cfg(feature = "wat")]
use wasmer_types::WasmError;
use wasmer_types::{
CompileError, DeserializeError, ExportType, ExportsIterator, ImportType, ImportsIterator,
ModuleInfo, SerializeError,
CompilationProgress, CompilationProgressCallback, CompileError, DeserializeError, ExportType,
ExportsIterator, ImportType, ImportsIterator, ModuleInfo, SerializeError, UserAbort,
};

use crate::{AsEngineRef, macros::backend::match_rt, utils::IntoBytes};
Expand Down
Loading
Loading