feat(foreign): raw stdin, per-cell inputs and interrupt
Persistent Python/Node kernels receive raw program stdin per eval; foreign.session accepts an inputs table and view exposes per-cell stdin. Add session interrupt host API and view control.
This commit is contained in:
1 parent
c85f1a2829
commit
064522a338
9 files changed
+86
-2
No files matched your search
+17
-1
@@ -217,6 +217,7 @@ pub enum HostFn {
|
||||
ForeignSessionNames = 208,
|
||||
ForeignSessionClose = 209,
|
||||
ForeignSessionList = 210,
|
||||
ForeignSessionInterrupt = 211,
|
||||
}
|
||||
|
||||
|
||||
@@ -424,6 +425,7 @@ impl HostFn {
|
||||
208 => Self::ForeignSessionNames,
|
||||
209 => Self::ForeignSessionClose,
|
||||
210 => Self::ForeignSessionList,
|
||||
211 => Self::ForeignSessionInterrupt,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -515,6 +517,7 @@ impl HostFn {
|
||||
"host_foreign_session_names" | "foreign_session_names" => Self::ForeignSessionNames,
|
||||
"host_foreign_session_close" | "foreign_session_close" => Self::ForeignSessionClose,
|
||||
"host_foreign_session_list" | "foreign_session_list" => Self::ForeignSessionList,
|
||||
"host_foreign_session_interrupt" | "foreign_session_interrupt" => Self::ForeignSessionInterrupt,
|
||||
"host_foreign_langs" => Self::ForeignLangs,
|
||||
"host_dotenv_load" | "load_dotenv" => Self::DotenvLoad,
|
||||
"host_http_request" | "http_request" => Self::HttpRequest,
|
||||
@@ -726,6 +729,7 @@ impl HostFn {
|
||||
Self::ForeignSessionNames => "host_foreign_session_names",
|
||||
Self::ForeignSessionClose => "host_foreign_session_close",
|
||||
Self::ForeignSessionList => "host_foreign_session_list",
|
||||
Self::ForeignSessionInterrupt => "host_foreign_session_interrupt",
|
||||
Self::ForeignLangs => "host_foreign_langs",
|
||||
Self::DotenvLoad => "host_dotenv_load",
|
||||
Self::HttpRequest => "host_http_request",
|
||||
@@ -903,7 +907,8 @@ impl HostFn {
|
||||
Self::ForeignSessionGet => &["id", "name"],
|
||||
Self::ForeignSessionNames => &["id"],
|
||||
Self::ForeignSessionClose => &["id"],
|
||||
Self::ForeignSessionList => &[],
|
||||
Self::ForeignSessionList => &["id"],
|
||||
Self::ForeignSessionInterrupt => &["id"],
|
||||
Self::ForeignLangs => &[],
|
||||
Self::PluginLoad => &["path"],
|
||||
Self::PluginUnload | Self::PluginList => &[],
|
||||
@@ -1581,6 +1586,17 @@ pub fn call_host(
|
||||
Err("foreign unavailable in browser wasm".into())
|
||||
}
|
||||
}
|
||||
HostFn::ForeignSessionInterrupt => {
|
||||
#[cfg(feature = "exec-host")]
|
||||
{
|
||||
crate::host::foreign_session::interrupt(ctx, require(bound, "id")?)
|
||||
}
|
||||
#[cfg(not(feature = "exec-host"))]
|
||||
{
|
||||
let _ = (ctx, bound);
|
||||
Err("foreign unavailable in browser wasm".into())
|
||||
}
|
||||
}
|
||||
HostFn::ForeignSessionList => {
|
||||
#[cfg(feature = "exec-host")]
|
||||
{
|
||||
|
||||
@@ -807,6 +807,20 @@ impl DocumentSession {
|
||||
]))
|
||||
}
|
||||
|
||||
pub fn interrupt(&mut self) -> Result<Value, String> {
|
||||
let ids: Vec<String> = self.kernel_sessions.keys().cloned().collect();
|
||||
let mut out = Vec::new();
|
||||
for name in ids {
|
||||
if let Some(id) = self.kernel_sessions.get(&name) {
|
||||
match foreign_session::interrupt(&mut self.host, &Value::Text(id.clone())) {
|
||||
Ok(_) => out.push(Value::Text(name)),
|
||||
Err(e) => out.push(Value::Text(format!("{name}: {e}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Value::List(out))
|
||||
}
|
||||
|
||||
pub fn restart_kernel(&mut self, name: &str) -> Result<Value, String> {
|
||||
if let Some(id) = self.kernel_sessions.remove(name) {
|
||||
let _ = foreign_session::close(&mut self.host, &Value::Text(id));
|
||||
|
||||
@@ -652,6 +652,28 @@ fn kill_session(ctx: &mut HostContext, id: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Interrupt the current evaluation without destroying the kernel. Python
|
||||
/// reports KeyboardInterrupt through the normal result frame.
|
||||
pub fn interrupt(ctx: &mut HostContext, id: &Value) -> Result<Value, String> {
|
||||
let id = as_text(id, "id")?.to_string();
|
||||
let session = ctx
|
||||
.foreign_sessions
|
||||
.get_mut(&id)
|
||||
.ok_or_else(|| format!("foreign session `{id}` not found"))?;
|
||||
#[cfg(unix)]
|
||||
unsafe {
|
||||
libc::kill(-(session.child.id() as i32), libc::SIGINT);
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let _ = Command::new("taskkill")
|
||||
.args(["/PID", &session.child.id().to_string(), "/T", "/F"])
|
||||
.output();
|
||||
session.alive = false;
|
||||
}
|
||||
Ok(Value::Bool(true))
|
||||
}
|
||||
|
||||
pub fn set(
|
||||
ctx: &mut HostContext,
|
||||
id: &Value,
|
||||
|
||||
Reference in new issue
Block a user