This commit is contained in:
fengchuanhn@gmail.com
2026-05-16 12:35:04 +08:00
parent e119327ac2
commit c0ddaceb43
10 changed files with 477 additions and 138 deletions

View File

@@ -1,5 +1,6 @@
//! QuickJS 流水线命令:脚本由后端 `/api/v1/quickjs-scripts` 下发,
//! 运行时注入 `__native` 后 eval并调用 `globalThis.__quickjsMain(params)`。
//! 调试命令 `run_quickjs_script_source` 则将源码当作异步函数体直接执行,并注入变量 `params`。
//!
//! 前端用法示例:
//!
@@ -80,6 +81,45 @@ pub async fn run_quickjs_script(
Ok(result)
}
/// 调试:直接执行传入的 QuickJS 源码(异步函数体),可使用 `__native`、`params`、`return`,无需 `__quickjsMain`。
#[tauri::command]
pub async fn run_quickjs_script_source(
app: AppHandle,
window: Window,
script_source: String,
params: Value,
) -> Result<Value, String> {
let (tx, mut rx) = mpsc::unbounded_channel::<PipelineEvent>();
let app_for_emit = app.clone();
let win_label = window.label().to_string();
const EMIT_LABEL: &str = "<debug-source>";
let pump = tokio::spawn(async move {
while let Some(ev) = rx.recv().await {
let payload: FrontendEvent = match ev {
PipelineEvent::Progress(s) => FrontendEvent::Progress {
script_name: EMIT_LABEL.to_string(),
status: s,
},
PipelineEvent::Log { level, msg, fields } => FrontendEvent::Log {
script_name: EMIT_LABEL.to_string(),
level,
msg,
fields,
},
};
let _ = app_for_emit.emit_to(&win_label, EVENT_NAME, payload);
}
});
let result = js_runtime::run_script_source(script_source, params, tx)
.await
.map_err(|e| e.to_string())?;
pump.await.ok();
Ok(result)
}
/// 列出后端 `scripts/quickjs` 目录中的 `.js` 文件名。
#[tauri::command]
pub async fn list_quickjs_scripts() -> Result<Vec<String>, String> {