Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 13 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pecos-qis-selene = { version = "0.1.1", path = "crates/pecos-qis-selene" }
pecos-qis-core = { version = "0.1.1", path = "crates/pecos-qis-core" }
pecos-hugr-qis = { version = "0.1.1", path = "crates/pecos-hugr-qis" }
pecos-rslib = { version = "0.1.1", path = "python/pecos-rslib/rust" }
pecos-wasm = { version = "0.1.1", path = "crates/pecos-wasm" }
pecos-build-utils = { version = "0.1.1", path = "crates/pecos-build-utils" }
pecos-llvm-utils = { version = "0.1.1", path = "crates/pecos-llvm-utils" }

Expand Down
5 changes: 2 additions & 3 deletions crates/pecos-phir-json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ description = "PHIR-JSON (PECOS High-level Intermediate Representation JSON form
default = ["v0_1", "wasm"]
v0_1 = []
all-versions = ["v0_1"]
wasm = ["wasmtime", "wat", "parking_lot"]
wasm = ["pecos-wasm/wasm", "wat"]

[dependencies]
log.workspace = true
Expand All @@ -25,9 +25,8 @@ pecos-core.workspace = true
pecos-engines.workspace = true
pecos-phir.workspace = true
pecos-programs.workspace = true
wasmtime = { workspace = true, optional = true }
pecos-wasm = { workspace = true, optional = true }
wat = { workspace = true, optional = true }
parking_lot = { workspace = true, optional = true }

[dev-dependencies]
# Testing
Expand Down
93 changes: 6 additions & 87 deletions crates/pecos-phir-json/src/v0_1/foreign_objects.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,7 @@
use pecos_core::errors::PecosError;
use std::any::Any;
use std::fmt::Debug;
// Re-export from pecos-wasm crate
#[cfg(feature = "wasm")]
pub use pecos_wasm::{DummyForeignObject, ForeignObject};

/// Trait for foreign object implementations
pub trait ForeignObject: Debug + Send + Sync {
/// Clone the foreign object
fn clone_box(&self) -> Box<dyn ForeignObject>;
/// Initialize object before running a series of simulations
///
/// # Errors
/// Returns an error if initialization fails.
fn init(&mut self) -> Result<(), PecosError>;

/// Create new instance/internal state
///
/// # Errors
/// Returns an error if instance creation fails.
fn new_instance(&mut self) -> Result<(), PecosError>;

/// Get a list of function names available from the object
fn get_funcs(&self) -> Vec<String>;

/// Execute a function given a list of arguments
///
/// # Errors
/// Returns an error if the function does not exist or execution fails.
fn exec(&mut self, func_name: &str, args: &[i64]) -> Result<Vec<i64>, PecosError>;

/// Cleanup resources
fn teardown(&mut self) {}

/// Get as Any for downcasting
fn as_any(&self) -> &dyn Any;

/// Get as Any for downcasting (mutable)
fn as_any_mut(&mut self) -> &mut dyn Any;
}

/// Dummy foreign object for when no foreign object is needed
#[derive(Debug, Clone)]
pub struct DummyForeignObject {}

impl DummyForeignObject {
/// Create a new dummy foreign object
#[must_use]
pub fn new() -> Self {
Self {}
}
}

impl Default for DummyForeignObject {
fn default() -> Self {
Self::new()
}
}

impl ForeignObject for DummyForeignObject {
fn clone_box(&self) -> Box<dyn ForeignObject> {
Box::new(Self::default())
}

fn init(&mut self) -> Result<(), PecosError> {
Ok(())
}

fn new_instance(&mut self) -> Result<(), PecosError> {
Ok(())
}

fn get_funcs(&self) -> Vec<String> {
vec![]
}

fn exec(&mut self, func_name: &str, _args: &[i64]) -> Result<Vec<i64>, PecosError> {
Err(PecosError::Input(format!(
"Dummy foreign object cannot execute function: {func_name}"
)))
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
// For when wasm feature is disabled, provide minimal trait
#[cfg(not(feature = "wasm"))]
pub use pecos_wasm::{DummyForeignObject, ForeignObject};
Loading
Loading