Rewrite ext/web around tables and class methods.

Drop bag/scaffold glue and rebuild the web plugin for compose, SQLite CRUD, and listen, with call_lib_path support and a §4 English site sample.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-11 15:06:39 +08:00
1 parent 8041a2274c
commit 9599303c4c
38 files changed
+2772 -4658

No files matched your search

+51 -2
View File
@@ -1,6 +1,6 @@
//! Native plugin loader (C ABI v1/v2). Shared libs are not linked into marqdo.
use std::cell::Cell;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_void};
@@ -54,6 +54,24 @@ struct MarqdoHostApi {
thread_local! {
/// Set around plugin calls so `host_query` can read entry source / call site.
static CURRENT_HOST: Cell<*const HostContext> = const { Cell::new(std::ptr::null()) };
/// Optional `lib.member` resolver for `host_query("call_lib_path")` (site entry module).
static LIB_PATH_CALL: RefCell<Option<LibPathCall>> = const { RefCell::new(None) };
}
/// Hook: resolve bare `lib.member` (and longer paths) in the site module's import tree.
pub struct LibPathCall {
pub call: fn(*mut (), &str) -> Result<Value, String>,
pub data: *mut (),
}
/// Run `body` with a temporary lib-path call hook (nested-safe).
pub fn with_lib_path_call<R>(hook: LibPathCall, body: impl FnOnce() -> R) -> R {
LIB_PATH_CALL.with(|slot| {
let prev = slot.replace(Some(hook));
let out = body();
slot.replace(prev);
out
})
}
struct RegisterBuf {
@@ -205,7 +223,7 @@ unsafe extern "C" fn host_register(
unsafe extern "C" fn host_query(
_userdata: *mut c_void,
name: *const c_char,
_args_json: *const c_char,
args_json: *const c_char,
out_json: *mut *mut c_char,
err_msg: *mut *mut c_char,
) -> c_int {
@@ -239,6 +257,15 @@ unsafe extern "C" fn host_query(
}
let ctx = &*ctx_ptr;
let args_owned: Option<String> = if args_json.is_null() {
None
} else {
CStr::from_ptr(args_json)
.to_str()
.ok()
.map(|s| s.to_string())
};
let result = match name {
"module_source" => agent_rt::module_source(ctx).and_then(|v| value_to_json(&v)),
"call_site" => agent_rt::call_site(ctx, Some(ctx.current_line)).and_then(|v| value_to_json(&v)),
@@ -246,6 +273,28 @@ unsafe extern "C" fn host_query(
"cwd" => Ok(serde_json::Value::String(
ctx.cwd.to_string_lossy().into_owned(),
)),
"call_lib_path" => (|| {
let raw = args_owned.as_deref().unwrap_or("{}");
let args: serde_json::Value = serde_json::from_str(if raw.trim().is_empty() {
"{}"
} else {
raw
})
.map_err(|e| format!("call_lib_path args: {e}"))?;
let path = args
.get("path")
.and_then(|v| v.as_str())
.ok_or_else(|| "call_lib_path requires `path`".to_string())?
.to_string();
LIB_PATH_CALL.with(|slot| {
let hook = slot.borrow();
let hook = hook
.as_ref()
.ok_or_else(|| "call_lib_path: no site lib resolver".to_string())?;
let v = (hook.call)(hook.data, &path)?;
value_to_json(&v)
})
})(),
other => Err(format!("host_query: unknown `{other}`")),
};
+70 -14
View File
@@ -28,6 +28,8 @@ pub struct Interpreter {
input: InputFeed,
host: HostContext,
debug: Option<Arc<DebugController>>,
/// Entry module pointer for `host_query("call_lib_path")` while plugins run.
site_module: Option<*const Module>,
}
struct Env {
@@ -50,7 +52,7 @@ impl Env {
}
}
enum EvArg {
pub(crate) enum EvArg {
Positional(Value),
Named(String, Value),
}
@@ -66,6 +68,7 @@ impl Interpreter {
input: InputFeed::new(false, Vec::new()),
host: HostContext::for_run(path, Default::default(), Vec::new()),
debug: None,
site_module: None,
}
}
@@ -79,6 +82,7 @@ impl Interpreter {
input: InputFeed::new(true, Vec::new()),
host: HostContext::for_capture(path, Default::default()),
debug: None,
site_module: None,
}
}
@@ -140,19 +144,70 @@ impl Interpreter {
}
pub fn run_module(&mut self, module: &Module) -> Result<Value> {
if let Some(main) = find_top(module, "main") {
return self.run_function(module, main, Env::new(), &[]);
}
// Document-as-entry: sole top-level `#` object with no params (e.g. `# Hello World`).
let entries: Vec<&Function> = module
.functions
.iter()
.filter(|f| f.is_object() && f.params.is_empty())
let prev = self.site_module;
self.site_module = Some(module as *const Module);
let result = (|| {
if let Some(main) = find_top(module, "main") {
return self.run_function(module, main, Env::new(), &[]);
}
// Document-as-entry: sole top-level `#` object with no params (e.g. `# Hello World`).
let entries: Vec<&Function> = module
.functions
.iter()
.filter(|f| f.is_object() && f.params.is_empty())
.collect();
if entries.len() == 1 {
return self.run_function(module, entries[0], Env::new(), &[]);
}
bail!("no `# main` object to run");
})();
self.site_module = prev;
result
}
/// Resolve `lib.member` on the site (entry) module — used by ABI `call_lib_path`.
pub(crate) fn call_site_lib_path(&mut self, path: &str) -> std::result::Result<Value, String> {
let site = self
.site_module
.ok_or_else(|| "call_lib_path: no site module".to_string())?;
let site = unsafe { &*site };
let parts: Vec<String> = path
.split('.')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
if entries.len() == 1 {
return self.run_function(module, entries[0], Env::new(), &[]);
if parts.len() < 2 {
return Err(format!(
"call_lib_path: need `lib.member`, got `{path}`"
));
}
bail!("no `# main` object to run");
self.eval_path_call(site, &parts, &[])
.map_err(|e| e.to_string())
}
fn call_plugin_registered(
&mut self,
name: &str,
bound: &HashMap<String, Value>,
) -> std::result::Result<Value, String> {
struct HookData {
interp: *mut Interpreter,
}
fn trampoline(data: *mut (), path: &str) -> std::result::Result<Value, String> {
let data = unsafe { &mut *(data as *mut HookData) };
let interp = unsafe { &mut *data.interp };
interp.call_site_lib_path(path)
}
let mut data = HookData {
interp: self as *mut Interpreter,
};
let hook = crate::host::plugin::LibPathCall {
call: trampoline,
data: &mut data as *mut HookData as *mut (),
};
crate::host::plugin::with_lib_path_call(hook, || {
crate::host::plugin::call_registered(&self.host, name, bound)
})
}
fn run_function(
@@ -636,7 +691,8 @@ impl Interpreter {
if let Some(reg) = self.host.plugins.get(&call.callee).cloned() {
let bound = bind_args(&reg.params, &ev_args, false).map_err(|m| self.err(m))?;
return crate::host::plugin::call_registered(&self.host, &call.callee, &bound)
return self
.call_plugin_registered(&call.callee, &bound)
.map_err(|m| self.err(m));
}
@@ -662,7 +718,7 @@ impl Interpreter {
}
/// Resolve bare path `lib.member` / `lib.Type.member` (definition-tree addressing).
fn eval_path_call(
pub(crate) fn eval_path_call(
&mut self,
module: &Module,
path: &[String],