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
180 changes: 175 additions & 5 deletions crates/celox-napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,140 @@ impl NativeSimulatorHandle {
}
}

/// A runtime event of a simulation: `$display`, `$write`, a report of a
/// severity task, `$finish`, `$stop`, or events the design's event buffer
/// overwrote before they were drained.
#[cfg(not(target_arch = "wasm32"))]
#[napi(object)]
pub struct NapiRuntimeEvent {
/// `"display"`, `"write"`, `"report"`, `"finish"`, `"stop"` or
/// `"missed"`.
pub kind: String,
pub message: Option<Uint8Array>,
/// `"info"`, `"warning"`, `"error"` or `"fatal"` for a report.
pub severity: Option<String>,
pub time: Option<f64>,
pub scope: Option<String>,
pub file: Option<String>,
pub line: Option<u32>,
pub assertion: Option<NapiAssertionSite>,
/// The number of missed events.
pub count: Option<f64>,
}

/// The assertion whose action block issued a report.
#[cfg(not(target_arch = "wasm32"))]
#[napi(object)]
pub struct NapiAssertionSite {
pub label: Option<String>,
/// `"assert"`, `"assume"` or `"cover"`.
pub directive: String,
/// `"none"`, `"observed"` (`#0`) or `"final"`.
pub deferral: String,
/// `"pass"` or `"fail"`.
pub outcome: String,
}

#[cfg(not(target_arch = "wasm32"))]
fn severity_name(severity: celox::Severity) -> &'static str {
match severity {
celox::Severity::Info => "info",
celox::Severity::Warning => "warning",
celox::Severity::Error => "error",
celox::Severity::Fatal => "fatal",
}
}

#[cfg(not(target_arch = "wasm32"))]
fn parse_severity(name: &str) -> Result<celox::Severity> {
Ok(match name {
"info" => celox::Severity::Info,
"warning" => celox::Severity::Warning,
"error" => celox::Severity::Error,
"fatal" => celox::Severity::Fatal,
_ => {
return Err(Error::from_reason(format!(
"unknown severity `{name}`: expected info, warning, error or fatal"
)));
}
})
}

#[cfg(not(target_arch = "wasm32"))]
fn napi_runtime_event(event: celox::RuntimeEvent) -> NapiRuntimeEvent {
let mut out = NapiRuntimeEvent {
kind: String::new(),
message: None,
severity: None,
time: None,
scope: None,
file: None,
line: None,
assertion: None,
count: None,
};
match event {
celox::RuntimeEvent::Display { message } => {
out.kind = "display".into();
out.message = Some(message.into());
}
celox::RuntimeEvent::Write { message } => {
out.kind = "write".into();
out.message = Some(message.into());
}
celox::RuntimeEvent::Report(report) => {
out.kind = "report".into();
out.severity = Some(severity_name(report.severity).into());
out.message = Some(report.message.into());
out.time = report.time.map(|time| time as f64);
out.scope = report.scope;
if let Some(location) = report.location {
out.file = Some(location.file);
out.line = Some(location.line as u32);
}
out.assertion = report.assertion.map(|assertion| NapiAssertionSite {
label: assertion.label,
directive: match assertion.directive {
celox::AssertionDirective::Assert => "assert",
celox::AssertionDirective::Assume => "assume",
celox::AssertionDirective::Cover => "cover",
}
.into(),
deferral: match assertion.deferral {
celox::AssertionDeferral::None => "none",
celox::AssertionDeferral::Observed => "observed",
celox::AssertionDeferral::Final => "final",
}
.into(),
outcome: match assertion.outcome {
celox::AssertionOutcome::Pass => "pass",
celox::AssertionOutcome::Fail => "fail",
}
.into(),
});
}
celox::RuntimeEvent::Finish => out.kind = "finish".into(),
celox::RuntimeEvent::Stop => out.kind = "stop".into(),
celox::RuntimeEvent::Missed { count } => {
out.kind = "missed".into();
out.count = Some(count as f64);
}
}
out
}

/// The error of a run that stopped at a report of the `failOn` severity.
#[cfg(not(target_arch = "wasm32"))]
fn check_failure(sim: &celox::Simulation) -> Result<()> {
match sim.failure() {
Some(report) => Err(Error::from_reason(format!(
"failOn: {}",
String::from_utf8_lossy(&report.render())
))),
None => Ok(()),
}
}

/// Low-level handle wrapping a `celox::Simulation`.
#[cfg(not(target_arch = "wasm32"))]
#[napi]
Expand Down Expand Up @@ -2082,7 +2216,8 @@ impl NativeSimulationHandle {
.as_mut()
.ok_or_else(|| Error::from_reason("Simulation has been disposed"))?;
sim.run_until(end_time as u64)
.map_err(|e| Error::from_reason(format!("{}", e)))
.map_err(|e| Error::from_reason(format!("{}", e)))?;
check_failure(sim)
}

/// Advance until `end_time` taking at most `max_steps` steps. Returns
Expand All @@ -2093,8 +2228,40 @@ impl NativeSimulationHandle {
.sim
.as_mut()
.ok_or_else(|| Error::from_reason("Simulation has been disposed"))?;
sim.run_until_bounded(end_time as u64, max_steps as u64)
.map_err(|e| Error::from_reason(format!("{}", e)))
let reached = sim
.run_until_bounded(end_time as u64, max_steps as u64)
.map_err(|e| Error::from_reason(format!("{}", e)))?;
check_failure(sim)?;
Ok(reached)
}

/// Take the runtime events the design emitted since the last call, in
/// emission order.
#[napi]
pub fn drain_runtime_events(&mut self) -> Result<Vec<NapiRuntimeEvent>> {
let sim = self
.sim
.as_mut()
.ok_or_else(|| Error::from_reason("Simulation has been disposed"))?;
Ok(sim
.drain_runtime_events()
.into_iter()
.map(napi_runtime_event)
.collect())
}

/// Make runs and steps fail after the time step of a report of at least
/// `severity` (`"info"`, `"warning"`, `"error"` or `"fatal"`); `null`
/// fails only at `$fatal`.
#[napi]
pub fn set_fail_on(&mut self, severity: Option<String>) -> Result<()> {
let severity = severity.as_deref().map(parse_severity).transpose()?;
let sim = self
.sim
.as_mut()
.ok_or_else(|| Error::from_reason("Simulation has been disposed"))?;
sim.set_fail_on(severity);
Ok(())
}

/// Report that the host wrote the state through the shared memory: the
Expand All @@ -2117,9 +2284,12 @@ impl NativeSimulationHandle {
.sim
.as_mut()
.ok_or_else(|| Error::from_reason("Simulation has been disposed"))?;
sim.step()
let time = sim
.step()
.map(|opt| opt.map(|t| t as f64))
.map_err(|e| Error::from_reason(format!("{}", e)))
.map_err(|e| Error::from_reason(format!("{}", e)))?;
check_failure(sim)?;
Ok(time)
}

/// Returns the current simulation time.
Expand Down
5 changes: 3 additions & 2 deletions crates/celox-runtime/src/simulation/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ impl<B: SimBackend> SimulationState<B> {
Ok(())
}

/// The next time with scheduled events or processes to run.
pub(super) fn next_work_time(&self) -> Option<u64> {
/// The next time with scheduled events or processes to run, apart
/// from host writes to settle.
pub fn next_work_time(&self) -> Option<u64> {
if self.finished {
return None;
}
Expand Down
Loading
Loading