feat(rfc-cm): baseline P0-P3 compute machine, stdlib and resource slices

Implements the first vertical slices of RFC-CM-0001/0002 on top of a113d89:

- P1: opaque Memory regions (64 KiB pages, budgets, grow-only), byte
  read/write, Memory-index place assignment, wire/host-boundary rejection.
- CM-A04/A05: value-semantics List/Map/Text/Table primitives and the
  recoverable Result map convention.
- CM-A06: opaque Resource handles, resource.open/read/write/close, stdout
  write, granted project-file read and buffered write, strict fail-closed.
- CM-A07 Compat-A: explicit .mq.md format/utf8/io chain matching builtin
  print for the covered value types; builtin remains default.
- P2 fixtures: allocators, list/tree/graph/hash, integer codecs, UTF-8
  encode/decode, CSV/JSON subsets, table filter/sort/join, bytecode VM.
- ADRs 0022-0027, spec updates, evidence reports and 68 semantic goldens.

Gate: cargo fmt --check, clippy -D warnings, 407 tests passed.
This commit is contained in:
cflmy committed 2026-11-20 21:16:54 +08:00
1 parent a113d89496
commit 92bfbf2568
127 files changed
+11351 -141

No files matched your search

+28
View File
@@ -181,6 +181,34 @@ fn analysis_v2_identity_is_location_independent() {
assert_eq!(analysis["project"]["identity_kind"], "manifest");
}
#[test]
fn analysis_v2_projects_memory_place_targets_additively() {
let dir = TempDir::new("analysis-place");
let source = "## main\n\n**内存 = memory.new(1, 2)**\n\n**`内存[0] = 7`**\n\n*内存[0]*\n";
write(dir.path(), "index.mq.md", source);
let target = dir.path().join("analysis.json");
let output = cli(
dir.path(),
&["check", "index.mq.md", "--emit", target.to_str().unwrap()],
);
assert_eq!(
output.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let analysis: serde_json::Value =
serde_json::from_slice(&std::fs::read(&target).unwrap()).unwrap();
let body = analysis["modules"][0]["functions"][0]["body"]
.as_array()
.expect("body is an array");
assert_eq!(body[0]["target"], "内存");
assert_eq!(body[1]["target"]["kind"], "index");
assert_eq!(body[1]["target"]["base"]["kind"], "variable");
assert_eq!(body[1]["target"]["index"]["kind"], "literal");
assert_eq!(body[1]["target"]["index"]["type"], "int");
}
#[test]
fn analysis_v2_projects_addressable_formula_and_code_resources() {
let dir = TempDir::new("analysis-resources");
+43
View File
@@ -24,6 +24,49 @@ fn stderr(output: &Output) -> String {
String::from_utf8_lossy(&output.stderr).to_string()
}
#[test]
fn mqmd_stdlib_print_output_matches_builtin_print() {
let output = cli(&["run", "examples/stdlib/demo.mq.md"]);
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
let stdout = String::from_utf8_lossy(&output.stdout);
let lines: Vec<&str> = stdout.lines().collect();
assert_eq!(lines.len(), 4, "{stdout:?}");
assert_eq!(lines[0], lines[1], "list formatting differs from 打印");
assert_eq!(lines[2], lines[3], "table formatting differs from 打印");
assert_eq!(
lines[0],
r#"["文本", 2, 1.5, [3, true], {a: 1, b: "x"}, None]"#
);
assert_eq!(lines[2], "Table[名称 | 分数]{甲 | 87; 乙 | 92}");
}
#[test]
fn addressable_memory_example_runs_through_the_cli() {
let output = cli(&["run", "examples/memory-buffer.mq.md", "--json"]);
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
let report: serde_json::Value =
serde_json::from_slice(&output.stdout).expect("run --json emits JSON");
assert_eq!(report["success"], true);
assert_eq!(report["value"]["type"], "int");
assert_eq!(report["value"]["decimal"], "141");
}
#[test]
fn cli_memory_budget_override_fails_closed() {
let output = cli(&[
"run",
"examples/memory-buffer.mq.md",
"--max-memory",
"0",
"--json",
]);
assert_eq!(output.status.code(), Some(1), "{}", stderr(&output));
let report: serde_json::Value =
serde_json::from_slice(&output.stdout).expect("run --json emits JSON");
assert_eq!(report["success"], false);
assert_eq!(report["error"]["code"], "resource.exhausted");
}
#[test]
fn multi_version_packages_run_without_an_implicit_override() {
let output = cli(&["run", "examples/packages-multi-version/index.mq.md"]);
+235
View File
@@ -0,0 +1,235 @@
//! End-to-end resource stream tests through the CLI.
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
struct TempDir(PathBuf);
impl TempDir {
fn new(tag: &str) -> Self {
let path =
std::env::temp_dir().join(format!("marqdo-resource-test-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).unwrap();
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn cli(dir: &Path, args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_marqdo"))
.args(args)
.current_dir(dir)
.output()
.expect("run marqdo")
}
fn write_file(path: &Path, contents: &str) {
std::fs::write(path, contents).unwrap();
}
const READER: &str = r#"## main
**内存 = memory.new(1, 1)**
**流 = resource.open("file", "data.txt", ["read"])**
**第一次 = resource.read(流, 内存, 0, 2)**
**第二次 = resource.read(流, 内存, 2, 4)**
**第三次 = resource.read(流, 内存, 6, 1)**
**关闭 = resource.close(流)**
*[第一次.value.count, 第一次.value.eof, 第二次.value.count, 第二次.value.eof, 第三次.value.count, 第三次.value.eof, 内存[0], 内存[1], 内存[2], 内存[3], 内存[4], 内存[5], 关闭.ok]*
"#;
#[test]
fn project_file_stream_reads_chunks_into_memory() {
let dir = TempDir::new("file-read");
write_file(&dir.path().join("data.txt"), "abcdef");
write_file(&dir.path().join("index.mq.md"), READER);
let output = cli(
dir.path(),
&["run", "index.mq.md", "--allow-read", "data.txt", "--json"],
);
assert_eq!(
output.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json output");
assert_eq!(report["success"], true);
let values: Vec<&serde_json::Value> = report["value"]["items"]
.as_array()
.expect("list result")
.iter()
.collect();
assert_eq!(values[0]["decimal"], "2");
assert_eq!(values[1]["value"], false);
assert_eq!(values[2]["decimal"], "4");
assert_eq!(values[3]["value"], true);
assert_eq!(values[4]["decimal"], "0");
assert_eq!(values[5]["value"], true);
for (index, expected) in ["97", "98", "99", "100", "101", "102"]
.into_iter()
.enumerate()
{
assert_eq!(values[6 + index]["decimal"], expected);
}
assert_eq!(values[12]["value"], true);
}
const WRITER: &str = r#"## main
**内存 = memory.new(1, 1)**
**`内存[0] = 65`**
**`内存[1] = 66`**
**`内存[2] = 67`**
**写 = resource.open("file", "out.bin", ["write"])**
**写入 = resource.write(写, 内存, 0, 3)**
**关闭写 = resource.close(写)**
**读 = resource.open("file", "out.bin", ["read"])**
**读出 = resource.read(读, 内存, 0, 3)**
**关闭读 = resource.close(读)**
*[写入.value.count, 关闭写.ok, 读出.value.count, 读出.value.eof, 内存[0], 内存[1], 内存[2], 关闭读.ok]*
"#;
#[test]
fn project_file_stream_writes_on_close_then_reads_back() {
let dir = TempDir::new("file-write");
write_file(&dir.path().join("index.mq.md"), WRITER);
let output = cli(
dir.path(),
&[
"run",
"index.mq.md",
"--allow-write",
"out.bin",
"--allow-read",
"out.bin",
"--json",
],
);
assert_eq!(
output.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json output");
assert_eq!(report["success"], true);
let values = report["value"]["items"].as_array().expect("list result");
assert_eq!(values[0]["decimal"], "3");
assert_eq!(values[1]["value"], true);
assert_eq!(values[2]["decimal"], "3");
assert_eq!(values[3]["value"], true);
assert_eq!(values[4]["decimal"], "65");
assert_eq!(values[5]["decimal"], "66");
assert_eq!(values[6]["decimal"], "67");
assert_eq!(values[7]["value"], true);
assert_eq!(std::fs::read(dir.path().join("out.bin")).unwrap(), b"ABC");
}
const COPY: &str = r#"## main
**输入 = resource.open("file", "data.txt", ["read"])**
**输出 = resource.open("file", "out.bin", ["write"])**
**内存 = memory.new(1, 1)**
**总数 = 0**
**继续 = true**
- `继续`
**块 = resource.read(输入, 内存, 0, 16)**
1. `块.ok`
> resource.write(输出, 内存, 0, 块.value.count)
**总数 = 总数 + 块.value.count**
**继续 = not 块.value.eof**
2. `true`
**继续 = false**
**关闭读 = resource.close(输入)**
**关闭写 = resource.close(输出)**
*[总数, 关闭读.ok, 关闭写.ok]*
"#;
#[test]
fn file_copy_is_written_in_mqmd_through_chunked_streams() {
let dir = TempDir::new("file-copy");
write_file(&dir.path().join("data.txt"), "abcdef");
write_file(&dir.path().join("index.mq.md"), COPY);
let output = cli(
dir.path(),
&[
"run",
"index.mq.md",
"--allow-read",
"data.txt",
"--allow-write",
"out.bin",
"--json",
],
);
assert_eq!(
output.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json output");
assert_eq!(report["success"], true);
let values = report["value"]["items"].as_array().expect("list result");
assert_eq!(values[0]["decimal"], "6");
assert_eq!(values[1]["value"], true);
assert_eq!(values[2]["value"], true);
assert_eq!(
std::fs::read(dir.path().join("out.bin")).unwrap(),
b"abcdef"
);
}
#[test]
fn file_stream_requires_a_read_grant() {
let dir = TempDir::new("file-denied");
write_file(&dir.path().join("data.txt"), "abcdef");
write_file(&dir.path().join("index.mq.md"), READER);
let output = cli(dir.path(), &["run", "index.mq.md", "--json"]);
assert_eq!(output.status.code(), Some(1));
let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json output");
assert_eq!(report["success"], false);
assert_eq!(report["error"]["code"], "permission.denied");
}