feat(foreign): structured invoke, presets, probe and config

F0/F1: safe execution core (exec gate, bounded IO, process-group timeout), structured invoke records, run options, 15 language presets, register/unset/probe/config, view stderr/exit/ms, and node/bash gold tests.
This commit is contained in:
cflmy committed 2026-10-18 01:26:53 +08:00
1 parent c21a28c3bc
commit e32ff8f6b3
21 files changed
+2301 -282

No files matched your search

+73
View File
@@ -3317,6 +3317,79 @@ fn lib_foreign_zh() {
assert_out("tests/lib/外联-python.mq.md", "中文外联");
}
fn node_available() -> bool {
Command::new("node")
.args(["-e", "process.exit(0)"])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
#[test]
fn lib_foreign_invoke() {
if !node_available() {
eprintln!("skip lib_foreign_invoke: no node on PATH");
return;
}
assert_out("tests/lib/foreign-invoke.mq.md", "False\n3\nboom\nFalse");
}
#[test]
fn lib_foreign_json() {
if !node_available() {
eprintln!("skip lib_foreign_json: no node on PATH");
return;
}
assert_out("tests/lib/foreign-json.mq.md", "True\n4\n10");
}
#[test]
fn lib_foreign_env_timeout() {
if !node_available() {
eprintln!("skip lib_foreign_env_timeout: no node on PATH");
return;
}
assert_out(
"tests/lib/foreign-env-timeout.mq.md",
"fast|node\nFalse\nTrue",
);
}
#[test]
fn lib_foreign_probe_register() {
if !node_available() {
eprintln!("skip lib_foreign_probe_register: no node on PATH");
return;
}
assert_out(
"tests/lib/foreign-probe-register.mq.md",
"True\nnode\ntiny",
);
}
#[test]
fn lib_foreign_config() {
if !node_available() {
eprintln!("skip lib_foreign_config: no node on PATH");
return;
}
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let output = Command::new(env!("CARGO_BIN_EXE_marqdo"))
.args(["run", "tests/lib/foreign-config.mq.md"])
.env(
"MARQDO_FOREIGN_CONFIG",
root.join("tests/lib/fixtures/foreign.toml"),
)
.current_dir(&root)
.output()
.expect("run foreign-config");
let code = output.status.code().unwrap_or(1);
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(code, 0, "foreign-config stderr={stderr}");
assert_eq!(stdout.trim_end(), "1\ncfg", "stdout={stdout}");
}
#[test]
fn bytecode_lib_math_formula() {
assert_out_backend(
+2
View File
@@ -0,0 +1,2 @@
[tinycfg]
template = ["node", "-e", "{inline}"]
+12
View File
@@ -0,0 +1,12 @@
---
title: foreign config load
import foreign:lib/foreign.mq.md
---
# main
**载入 = > foreign.load_config**
> print text=`载入`
**运行 = > foreign.invoke lang="tinycfg" source="console.log('cfg')"**
> print text=[stdout](`运行`)
+32
View File
@@ -0,0 +1,32 @@
---
title: foreign env cwd timeout
import foreign:lib/foreign.mq.md
---
# main
`环境` =
| MODE | NOTE |
|------|------|
| fast | test |
`环境检查` =
```node
console.log(process.env.MODE + "|" + process.env.MARQDO_FOREIGN_LANG);
```
**检查 = > foreign.invoke code=`环境检查` env=`环境`**
**检查输出 = > trim [stdout](`检查`)**
> print text=`检查输出`
`慢` =
```node
setTimeout(() => console.log("late"), 5000);
```
**超时 = > foreign.invoke code=`慢` timeout=0.5**
> print text=[ok](`超时`)
> print text=[timed_out](`超时`)
+19
View File
@@ -0,0 +1,19 @@
---
title: foreign invoke record
import foreign:lib/foreign.mq.md
---
# main
`失败` =
```node
process.stderr.write("boom");
process.exit(3);
```
**记录 = > foreign.invoke code=`失败`**
> print text=[ok](`记录`)
> print text=[code](`记录`)
> print text=[stderr](`记录`)
> print text=[truncated](`记录`)
+32
View File
@@ -0,0 +1,32 @@
---
title: foreign invoke JSON input
import foreign:lib/foreign.mq.md
---
# main
`数值表` =
| 数值 |
|------|
| 1 |
| 2 |
| 3 |
| 4 |
`统计` =
```node
let s = "";
process.stdin.on("data", d => { s += d; });
process.stdin.on("end", () => {
const xs = JSON.parse(s);
console.log(JSON.stringify({ n: xs.length, sum: xs.reduce((a, b) => a + b, 0) }));
});
```
**记录 = > foreign.invoke code=`统计` input=`数值表` output="json"**
**统计结果 = [result](`记录`)**
> print text=[ok](`记录`)
> print text=[n](`统计结果`)
> print text=[sum](`统计结果`)
+24
View File
@@ -0,0 +1,24 @@
---
title: foreign probe and register
import foreign:lib/foreign.mq.md
---
# main
**探测 = > foreign.probe lang="node"**
> print text=[ok](`探测`)
> print text=[lang](`探测`)
`模板` =
| 步骤 |
|------|
| node |
| "-e" |
| {inline} |
**> foreign.register lang="tiny" template=`模板`**
**运行 = > foreign.invoke lang="tiny" source="console.log('tiny')"**
**运行输出 = > trim [stdout](`运行`)**
> print text=`运行输出`
**> foreign.unset lang="tiny"**