feat: render interactive manuscript from Endor through generic graphics

This commit is contained in:
2026-11-12 10:52:28 +08:00
parent d29a1be329
commit 2715c1ce2c
71 changed files with 3000 additions and 78 deletions
+77 -4
View File
@@ -13,7 +13,7 @@ use std::{
ops::{Deref, DerefMut},
panic::{AssertUnwindSafe, catch_unwind},
sync::{
Arc, RwLock,
Arc, OnceLock, RwLock,
mpsc::{Receiver, SyncSender, TrySendError, sync_channel},
},
sync::{
@@ -51,8 +51,40 @@ pub struct RuntimeControl {
sender: SyncSender<()>,
requested: Arc<AtomicBool>,
completion: ShutdownCompletion,
native_sender: Arc<OnceLock<SyncSender<BuildRequest>>>,
namespace: Arc<RwLock<NamespaceState>>,
}
impl RuntimeControl {
/// Only the native input relay calls this blocking acknowledgement path.
/// Window threads still use a bounded, nonblocking Input sender.
pub(crate) fn dispatch_native(&self, input: crate::events::Input) -> Result<()> {
anyhow::ensure!(!self.is_exiting(), "application is exiting");
let (done, completed) = sync_channel(1);
let job = {
let state = self
.namespace
.read()
.map_err(|_| anyhow::anyhow!("namespace lock is poisoned"))?;
BuildRequest {
request: RequestReply::Native(done),
kind: RequestKind::Event,
source: String::new(),
namespace: state.name.clone(),
submission: state.submission,
native: Some(input),
}
};
self.native_sender
.get()
.ok_or_else(|| anyhow::anyhow!("native input queue is unavailable"))?
.send(job)
.map_err(|_| anyhow::anyhow!("native input worker stopped"))?;
completed
.recv()
.map_err(|_| anyhow::anyhow!("native input acknowledgement stopped"))?
.map_err(anyhow::Error::msg)
}
/// 只发出一次退出请求,唤醒 worker 执行清理,并通过完成句柄通知原生循环。
pub fn request_shutdown(&self) -> bool {
if self.requested.swap(true, Ordering::AcqRel) {
@@ -98,7 +130,8 @@ enum RequestKind {
/// 保留原 HTTP 请求与提交身份的执行工作,完成或失败后由 worker 回应。
struct BuildRequest {
request: ObservedRequest,
request: RequestReply,
native: Option<crate::events::Input>,
kind: RequestKind,
source: String,
namespace: String,
@@ -115,6 +148,8 @@ pub fn start(
sender,
requested: Arc::new(AtomicBool::new(false)),
completion: Arc::new((Mutex::new(None), Condvar::new())),
native_sender: Arc::new(OnceLock::new()),
namespace: namespace.clone(),
};
let shutdown = Shutdown {
receiver,
@@ -135,6 +170,7 @@ pub fn start(
activity.clone(),
notify,
Some(shutdown),
Some(control.native_sender.clone()),
)?;
Ok((activity, control))
}
@@ -153,6 +189,7 @@ fn start_server(
Arc::new(Mutex::new(ActivityLog::default())),
Arc::new(|| {}),
None,
None,
)
}
@@ -164,8 +201,14 @@ fn start_server_observed(
activity: Activity,
observer_notify: Notify,
shutdown: Option<Shutdown>,
native_port: Option<Arc<OnceLock<SyncSender<BuildRequest>>>>,
) -> Result<thread::JoinHandle<()>> {
let (sender, receiver) = sync_channel::<BuildRequest>(8);
if let Some(native_port) = native_port {
native_port
.set(sender.clone())
.map_err(|_| anyhow::anyhow!("native input queue already attached"))?;
}
let worker_namespace = namespace.clone();
let worker_notify = notify.clone();
let exiting = shutdown
@@ -240,7 +283,9 @@ fn start_server_observed(
Ok(job) => {
match worker.begin_request(&job, &worker_namespace, &worker_notify) {
RequestProgress::Pending(input) => {
if matches!(&input, PendingRequest::BeforeInput) {
if job.native.is_none()
&& matches!(&input, PendingRequest::BeforeInput)
{
eprintln!(
"Tessera worker: submission {} waits for active refresh",
job.submission
@@ -299,6 +344,33 @@ fn start_server_observed(
// Capture only allowlisted headers: credentials and cookies never enter the inspector.
/// HTTP 请求的观察包装,在读取正文和响应时维护 Network 工具记录。
/// HTTP and in-process native jobs share one serial execution/refresh path.
/// Native envelopes cannot be created by an HTTP header or request body.
enum RequestReply {
Http(ObservedRequest),
Native(SyncSender<std::result::Result<(), String>>),
}
impl RequestReply {
fn respond(self, response: Response<std::io::Cursor<Vec<u8>>>) -> std::io::Result<()> {
match self {
Self::Http(request) => request.respond(response),
Self::Native(done) => {
let status = response.status_code().0;
let result = if status == 200 || status == 409 {
Ok(())
} else {
Err(String::from_utf8_lossy(response.into_reader().get_ref())
.chars()
.take(2048)
.collect())
};
let _ = done.send(result);
Ok(())
}
}
}
}
struct ObservedRequest {
inner: Request,
activity: Activity,
@@ -572,7 +644,8 @@ fn submit(
}
};
let job = BuildRequest {
request,
request: RequestReply::Http(request),
native: None,
kind,
source,
namespace: addressed.into(),
+255
View File
@@ -845,3 +845,258 @@ fn canvas_in_world_ui_uses_ray_uv_coordinates_for_real_http_camera_events() {
assert_eq!(value("canvas.camera.zoom"), 2.);
assert!(namespace.read().unwrap().document.frame.fault.is_none());
}
#[test]
fn native_pointer_kinds_cross_real_http_route_with_local_coordinates_and_strict_versions() {
let server = Arc::new(Server::http("127.0.0.1:0").unwrap());
let namespace = Arc::new(RwLock::new(NamespaceState {
viewport: Some(crate::layout::Size {
width: 500.,
height: 400.,
}),
..Default::default()
}));
let api = start_server(server.clone(), namespace.clone(), Arc::new(|| {})).unwrap();
let client = TestServer {
address: server.server_addr().to_ip().unwrap(),
server,
api: Some(api),
};
let source = r#"
class Tracker {
x:Number
y:Number
held:Number
function move(self:Tracker,e:Event):Unit {self.x=e.x self.y=e.y}
function down(self:Tracker,e:Event):Unit {self.held=1}
function up(self:Tracker,e:Event):Unit {self.held=0}
}
tracker=Tracker(0,0,0)
target=RectBlock(200,100,0)
a=target.on("pointermove",tracker.move)
b=target.on("pointerdown",tracker.down)
c=target.on("pointerup",tracker.up)
d=target.on("pointerleave",tracker.up)
e=target.on("pointercancel",tracker.up)
page=Page()
page.render(Column(20,0,target))
"#;
let put = client.request("PUT", "/v1/namespaces/app.main/code", source);
assert_eq!(put.0, 200, "{}", put.2);
let input = |kind: &str| {
let current = namespace.read().unwrap();
let frame = &current.document.frame;
let mount = frame.mounts.iter().find(|m| m.pointer_events).unwrap();
crate::events::Input {
window: 1,
session: frame.session,
layout_version: frame.layout_version,
mount: mount.id,
kind: kind.into(),
point: Some([mount.x + 35., mount.y + 27.]),
press_point: None,
delta: None,
tab_drop: None,
screen_position: None,
}
};
let body = serde_json::to_string(&input("pointermove")).unwrap();
let response = client.request("POST", "/v1/namespaces/app.main/events", &body);
assert_eq!(response.0, 200, "{}", response.2);
// Tracker state alone need not publish a new picture. Expire the frame
// through a real visual mutation before asserting strict stale rejection.
assert_eq!(
client
.request("POST", "/v1/namespaces/app.main/repl", "target.width=201")
.0,
200
);
assert_eq!(
client
.request("POST", "/v1/namespaces/app.main/events", &body)
.0,
409
);
for (expr, expected) in [("tracker.x", "35"), ("tracker.y", "27")] {
let r = client.request("POST", "/v1/namespaces/app.main/repl", expr);
assert_eq!(r.0, 200);
assert_eq!(
serde_json::from_str::<serde_json::Value>(&r.2).unwrap()["value"],
expected
);
}
for (kind, expected) in [
("pointerdown", "1"),
("pointerup", "0"),
("pointerdown", "1"),
("pointerleave", "0"),
("pointerdown", "1"),
("pointercancel", "0"),
] {
let response = client.request(
"POST",
"/v1/namespaces/app.main/events",
&serde_json::to_string(&input(kind)).unwrap(),
);
assert_eq!(response.0, 200, "{}", response.2);
let r = client.request("POST", "/v1/namespaces/app.main/repl", "tracker.held");
assert_eq!(
serde_json::from_str::<serde_json::Value>(&r.2).unwrap()["value"],
expected
);
}
let mut invalid = input("pointermove");
invalid.point = None;
assert_eq!(
client
.request(
"POST",
"/v1/namespaces/app.main/events",
&serde_json::to_string(&invalid).unwrap()
)
.0,
400
);
assert!(namespace.read().unwrap().document.frame.fault.is_none());
}
#[test]
fn native_envelopes_rebase_after_animation_but_reject_replaced_sessions() {
let server = Arc::new(Server::http("127.0.0.1:0").unwrap());
let namespace = Arc::new(RwLock::new(NamespaceState {
viewport: Some(crate::layout::Size {
width: 500.,
height: 400.,
}),
..Default::default()
}));
let native = Arc::new(OnceLock::new());
let api = start_server_observed(
server.clone(),
namespace.clone(),
Arc::new(|| {}),
Arc::new(Mutex::new(ActivityLog::default())),
Arc::new(|| {}),
None,
Some(native.clone()),
)
.unwrap();
let client = TestServer {
address: server.server_addr().to_ip().unwrap(),
server,
api: Some(api),
};
let source = r#"
class Tracker {
x:Number
held:Number
function move(self:Tracker,e:Event):Unit {self.x=e.x}
function down(self:Tracker,e:Event):Unit {self.held=1}
function up(self:Tracker,e:Event):Unit {self.held=0}
}
tracker=Tracker(0,0)
target=RectBlock(200,100,0)
a=target.on("pointermove",tracker.move)
b=target.on("pointerdown",tracker.down)
c=target.on("pointerup",tracker.up)
page=Page()
page.render(Column(20,0,target))
"#;
assert_eq!(
client
.request("PUT", "/v1/namespaces/app.main/code", source)
.0,
200
);
let old = {
let state = namespace.read().unwrap();
let f = &state.document.frame;
let m = f.mounts.iter().find(|m| m.pointer_events).unwrap();
crate::events::Input {
window: 1,
session: f.session,
layout_version: f.layout_version,
mount: m.id,
kind: "pointermove".into(),
point: Some([m.x + 37., m.y + 20.]),
press_point: None,
delta: None,
tab_drop: None,
screen_position: None,
}
};
let send_native = |input: crate::events::Input| {
let state = namespace.read().unwrap();
let epoch = state.submission;
let name = state.name.clone();
drop(state);
let (done, complete) = sync_channel(1);
native
.get()
.unwrap()
.send(BuildRequest {
request: RequestReply::Native(done),
native: Some(input),
kind: RequestKind::Event,
source: String::new(),
namespace: name,
submission: epoch,
})
.unwrap_or_else(|_| panic!("native worker stopped"));
complete
.recv_timeout(Duration::from_secs(5))
.unwrap()
.unwrap();
};
// Every native gesture deliberately carries the old picture version; each
// intervening visual mutation simulates an animation publishing a frame.
for (i, kind) in ["pointermove", "pointerdown", "pointerup"]
.into_iter()
.enumerate()
{
assert_eq!(
client
.request(
"POST",
"/v1/namespaces/app.main/repl",
&format!("target.width={}", 201 + i)
)
.0,
200
);
let mut sample = old.clone();
sample.kind = kind.into();
send_native(sample);
}
for (code, value) in [("tracker.x", "37"), ("tracker.held", "0")] {
let r = client.request("POST", "/v1/namespaces/app.main/repl", code);
assert_eq!(
serde_json::from_str::<serde_json::Value>(&r.2).unwrap()["value"],
value
);
}
assert_eq!(
client
.request(
"POST",
"/v1/namespaces/app.main/events",
&serde_json::to_string(&old).unwrap()
)
.0,
409
);
assert_eq!(
client
.request("PUT", "/v1/namespaces/app.main/code", source)
.0,
200
);
let mut stale = old;
stale.kind = "pointerdown".into();
send_native(stale);
let r = client.request("POST", "/v1/namespaces/app.main/repl", "tracker.held");
assert_eq!(
serde_json::from_str::<serde_json::Value>(&r.2).unwrap()["value"],
"0"
);
}
+26 -7
View File
@@ -337,19 +337,28 @@ impl Worker {
return RequestProgress::Pending(PendingRequest::BeforeInput);
}
let event = if job.kind == RequestKind::Event {
let input: crate::events::Input = match serde_json::from_str(&job.source) {
Ok(input) => input,
Err(error) => {
return RequestProgress::Complete(error_response(
StatusCode(400),
error.to_string(),
));
let mut input: crate::events::Input = if let Some(input) = &job.native {
input.clone()
} else {
match serde_json::from_str(&job.source) {
Ok(input) => input,
Err(error) => {
return RequestProgress::Complete(error_response(
StatusCode(400),
error.to_string(),
));
}
}
};
if !input.kind.starts_with("developer-open:")
&& !matches!(
input.kind.as_str(),
"click"
| "pointermove"
| "pointerdown"
| "pointerup"
| "pointerleave"
| "pointercancel"
| "focus"
| "scroll"
| "page-pan"
@@ -383,6 +392,16 @@ impl Worker {
));
}
};
// Rebase trusted captured native gestures only after BeforeInput
// has drained the prior refresh. External HTTP remains strict.
if job.native.is_some()
&& !crate::events::refresh_continuous_input(&mut input, &current)
{
return RequestProgress::Complete(error_response(
StatusCode(409),
"native target was replaced or detached",
));
}
let frame = if input.window == crate::window::MAIN_WINDOW {
&current.document.frame
} else {
+43 -5
View File
@@ -185,6 +185,32 @@ fn compile_plan_with_globals(
}
}
let mut compiled = Vec::with_capacity(plan.roots().len());
fn assigned(statement: &Stmt, globals: &mut BTreeSet<String>) {
match &statement.kind {
StmtKind::Binding(b) => {
globals.insert(b.name.text.clone());
}
StmtKind::Assignment(a) => {
globals.insert(a.name.text.clone());
}
StmtKind::If(b) => {
for s in &b.body.statements {
assigned(s, globals);
}
if let Some(other) = &b.otherwise {
for s in &other.statements {
assigned(s, globals);
}
}
}
StmtKind::While(b) => {
for s in &b.body.statements {
assigned(s, globals);
}
}
_ => {}
}
}
for root in plan.roots() {
let options = compiler::CompileOptions {
globals: globals.iter().cloned().collect(),
@@ -195,11 +221,7 @@ fn compile_plan_with_globals(
);
if let AstRoot::Interactive(input) = root {
for statement in &input.statements {
if let StmtKind::Binding(binding) = &statement.kind {
globals.insert(binding.name.text.clone());
} else if let StmtKind::Assignment(assignment) = &statement.kind {
globals.insert(assignment.name.text.clone());
}
assigned(statement, &mut globals);
}
}
}
@@ -1387,6 +1409,19 @@ impl Architecture {
/// 将相应语法结构投影为带源码范围的 CodeNode,供 Sources 展示和定位。
fn statement(&mut self, statement: &Stmt) -> CodeNode {
match &statement.kind {
StmtKind::If(branch) => {
let mut children = vec![self.expression(&branch.condition)];
children.extend(branch.body.statements.iter().map(|s| self.statement(s)));
if let Some(body) = &branch.otherwise {
children.extend(body.statements.iter().map(|s| self.statement(s)));
}
self.node("if".into(), "if", statement.span, children)
}
StmtKind::While(loop_) => {
let mut children = vec![self.expression(&loop_.condition)];
children.extend(loop_.body.statements.iter().map(|s| self.statement(s)));
self.node("while".into(), "while", statement.span, children)
}
StmtKind::MemberAssignment(assignment) => {
let object = self.expression(&assignment.object);
let value = self.expression(&assignment.value);
@@ -1458,3 +1493,6 @@ impl Architecture {
#[cfg(test)]
mod tests;
#[cfg(test)]
mod manuscript_tests;
+251
View File
@@ -0,0 +1,251 @@
//! Integration acceptance through the actual Endor VM and published frames.
use super::*;
use crate::document::RenderCommand;
#[test]
fn illustrated_manuscript_is_driven_by_real_endor_source_and_pointer_events() {
let source = include_str!("../../../examples/illustrated-manuscript.endor");
let (mut session, mut document) = ReplSession::load(source, "app.main", 1, 735).unwrap();
fn evaluate(session: &mut ReplSession, document: &mut RenderDocument, source: &str) -> String {
let result = session.execute(source, "app.main").unwrap();
if let Some(next) = result.document {
*document = next;
}
result.value
}
evaluate(&mut session, &mut document, "view.timer.cancel()");
document.validate().unwrap();
assert!(
document
.frame
.commands
.iter()
.any(|c| matches!(c, RenderCommand::Image { .. }))
);
assert!(document.frame.mounts.iter().any(|m| m.pointer_events));
let before = evaluate(&mut session, &mut document, "dragon.segments.at(0).x");
let target = document
.frame
.mounts
.iter()
.find(|m| m.pointer_events)
.unwrap();
let mut input = crate::events::Input {
window: 1,
session: 735,
layout_version: document.frame.layout_version,
mount: target.id,
kind: "pointermove".into(),
point: Some([
target.x + target.width * 0.65,
target.y + target.height * 0.55,
]),
press_point: None,
delta: None,
tab_drop: None,
screen_position: None,
};
document = session
.dispatch(&input)
.unwrap()
.document
.unwrap_or(document);
for _ in 0..8 {
document = session
.execute("dragon.step()", "app.main")
.unwrap()
.document
.unwrap_or(document);
}
let after = evaluate(&mut session, &mut document, "dragon.segments.at(0).x");
assert_ne!(before, after);
input.layout_version = document.frame.layout_version;
input.kind = "pointerdown".into();
document = session
.dispatch(&input)
.unwrap()
.document
.unwrap_or(document);
for _ in 0..3 {
document = session
.execute("dragon.step()", "app.main")
.unwrap()
.document
.unwrap_or(document);
}
assert_ne!(
evaluate(&mut session, &mut document, "dragon.flames.count"),
"0"
);
let max_size = evaluate(&mut session, &mut document, "dragon.flames.at(0).size < 20");
assert_eq!(max_size, "true");
input.layout_version = document.frame.layout_version;
input.kind = "pointercancel".into();
session.dispatch(&input).unwrap();
assert_eq!(evaluate(&mut session, &mut document, "dragon.pressed"), "0");
for _ in 0..16 {
document = session
.execute("dragon.step()", "app.main")
.unwrap()
.document
.unwrap_or(document);
}
assert_eq!(
evaluate(&mut session, &mut document, "dragon.flames.count"),
"0"
);
let list = document.display_list(1.).unwrap();
let mut pixels = vec![0; 1024 * 800];
crate::software_renderer::SoftwareRenderer::render(&list, &mut pixels, 1024, 800).unwrap();
assert!(pixels.contains(&0xf4eee0));
}
fn execute(session: &mut ReplSession, document: &mut RenderDocument, source: &str) -> String {
let result = session.execute(source, "app.main").unwrap();
if let Some(next) = result.document {
*document = next;
}
result.value
}
fn write_png(document: &RenderDocument, w: u32, h: u32, scale: f32, name: &str) {
let list = document.display_list(scale).unwrap();
let mut pixels = vec![0; w as usize * h as usize];
crate::software_renderer::SoftwareRenderer::render(&list, &mut pixels, w, h).unwrap();
assert!(pixels.iter().filter(|&&p| p == 0xf4eee0).count() > 1000);
if let Ok(dir) = std::env::var("TESSERA_MANUSCRIPT_SNAPSHOTS") {
std::fs::create_dir_all(&dir).unwrap();
let file =
std::fs::File::create(std::path::Path::new(&dir).join(format!("endor-{name}.png")))
.unwrap();
let mut encoder = png::Encoder::new(file, w, h);
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
let rgb: Vec<_> = pixels
.iter()
.flat_map(|p| [(p >> 16) as u8, (p >> 8) as u8, *p as u8])
.collect();
encoder
.write_header()
.unwrap()
.write_image_data(&rgb)
.unwrap();
}
}
#[test]
fn manuscript_script_snapshots_and_owned_animation_lifecycle() {
let source = format!(
"{}\nfunction advanceTest():Unit {{dragon.lastMove=math.now() dragon.step()}}",
include_str!("../../../examples/illustrated-manuscript.endor")
);
for (width, height, scale) in [(1000., 900., 1.), (420., 760., 2.)] {
let (mut session, mut document) = ReplSession::load_with_viewport(
&source,
"app.main",
1,
736,
Some(crate::layout::Size { width, height }),
)
.unwrap();
// The actual owner-scoped Task calls script tick, not a native demo loop.
let before = execute(&mut session, &mut document, "dragon.time");
std::thread::sleep(std::time::Duration::from_millis(85));
if let Some(next) = session.poll_tasks().unwrap() {
document = next;
}
let after = execute(&mut session, &mut document, "dragon.time");
assert_ne!(before, after);
execute(&mut session, &mut document, "view.timer.cancel()");
let (w, h) = ((width * scale) as u32, (height * scale) as u32);
write_png(
&document,
w,
h,
scale,
if width > 500. { "rest" } else { "narrow-rest" },
);
let mount = document
.frame
.mounts
.iter()
.find(|m| m.pointer_events)
.unwrap()
.clone();
let point = [mount.x + mount.width * 0.58, mount.y + mount.height * 0.50];
let mut input = crate::events::Input {
window: 1,
session: 736,
layout_version: document.frame.layout_version,
mount: mount.id,
kind: "pointermove".into(),
point: Some(point),
press_point: None,
delta: None,
tab_drop: None,
screen_position: None,
};
document = session
.dispatch(&input)
.unwrap()
.document
.unwrap_or(document);
for _ in 0..12 {
execute(&mut session, &mut document, "advanceTest()");
}
write_png(
&document,
w,
h,
scale,
if width > 500. { "crossing" } else { "narrow" },
);
input.layout_version = document.frame.layout_version;
input.kind = "pointerdown".into();
document = session
.dispatch(&input)
.unwrap()
.document
.unwrap_or(document);
// Hold long enough to reach the steady-state particle cap and exercise
// the worst paint workload, then release/cancel through the event path.
let start = std::time::Instant::now();
for _ in 0..16 {
execute(&mut session, &mut document, "advanceTest()");
}
eprintln!(
"Endor manuscript width={width}: 16 steady-fire VM refreshes {:?}",
start.elapsed()
);
assert_eq!(
execute(&mut session, &mut document, "dragon.flames.count <= 12"),
"true"
);
execute(
&mut session,
&mut document,
"function verifyFire(): Bool { art=dragon.artwork(0,0,1) c=art.at(art.count-1) p=dragon.flames.at(dragon.flames.count-1) return math.abs(c.angle+p.art.axis-math.atan2(p.vy,p.vx))<0.0001 }",
);
assert_eq!(execute(&mut session, &mut document, "verifyFire()"), "true");
execute(
&mut session,
&mut document,
"function verifyFireSize(): Bool { art=dragon.artwork(0,0,1) c=art.at(art.count-1) return math.max(c.width,c.height)<=20 }",
);
assert_eq!(
execute(&mut session, &mut document, "verifyFireSize()"),
"true"
);
write_png(
&document,
w,
h,
scale,
if width > 500. { "fire" } else { "narrow-fire" },
);
execute(&mut session, &mut document, "page.close()");
let closed = execute(&mut session, &mut document, "dragon.time");
std::thread::sleep(std::time::Duration::from_millis(85));
session.poll_tasks().unwrap();
assert_eq!(execute(&mut session, &mut document, "dragon.time"), closed);
}
}
+22
View File
@@ -102,6 +102,28 @@ fn shift_input(input: &mut InteractiveInput, offset: usize) {
fn shift_statement(statement: &mut Stmt, offset: usize) {
shift_span(&mut statement.span, offset);
match &mut statement.kind {
StmtKind::If(branch) => {
shift_span(&mut branch.span, offset);
shift_expression(&mut branch.condition, offset);
shift_span(&mut branch.body.span, offset);
for s in &mut branch.body.statements {
shift_statement(s, offset);
}
if let Some(body) = &mut branch.otherwise {
shift_span(&mut body.span, offset);
for s in &mut body.statements {
shift_statement(s, offset);
}
}
}
StmtKind::While(loop_) => {
shift_span(&mut loop_.span, offset);
shift_expression(&mut loop_.condition, offset);
shift_span(&mut loop_.body.span, offset);
for s in &mut loop_.body.statements {
shift_statement(s, offset);
}
}
StmtKind::Binding(binding) => {
shift_span(&mut binding.span, offset);
shift_span(&mut binding.name.span, offset);
+158 -51
View File
@@ -1,18 +1,13 @@
//! 原生窗口输入到执行 worker 的纯数据传输。Input 只含窗口/会话/版本/Mount 身份及有限坐标。
//! 连续相机和滚动输入出队时可更新帧版本,但不改变捕获目标和所属 VM;离散点击仍保留严格的过期帧语义。
//! 队列有界且当前逐项经本地 HTTP 发送;这不意味着输入已经按显示帧合并,也不是无损无限队列。
//! 生产端只合并相同目标的相邻绝对指针采样;有序动作是边界。relay 直接进入串行 worker,后台等待确认。
//!
//! Native input transport. Frame/mount identities and optional logical pointer positions cross to the VM worker.
use serde::{Deserialize, Serialize};
use std::{
io::{Read, Write},
net::TcpStream,
sync::{
Arc, RwLock,
mpsc::{SyncSender, sync_channel},
},
collections::VecDeque,
sync::{Arc, Condvar, Mutex, RwLock},
thread,
time::Duration,
};
use winit::event_loop::EventLoopProxy;
@@ -142,6 +137,23 @@ impl Input {
_ => Err("scroll requires point and finite delta, with no press_point"),
};
}
if matches!(
self.kind.as_str(),
"pointermove" | "pointerdown" | "pointerup" | "pointerleave" | "pointercancel"
) {
return if self.mount != 0
&& self.press_point.is_none()
&& self.delta.is_none()
&& self.screen_position.is_none()
&& self
.point
.is_some_and(|p| p.iter().all(|v| v.is_finite() && v.abs() <= 1_000_000.))
{
Ok(())
} else {
Err("pointer event requires finite point and target")
};
}
if self.delta.is_some() {
return Err("delta is only accepted for scroll input");
}
@@ -161,20 +173,80 @@ impl Input {
}
}
#[derive(Clone)]
/// 原生输入的有界发送端,后台线程串行发往本地 API;所有窗口共享权威发布状态。
pub struct Client(SyncSender<Input>);
/// Bounded producer-side coalescing preserves action barriers while the relay
/// waits for a VM acknowledgement. Motion cannot fill the queue by itself.
struct Queue {
items: VecDeque<Input>,
senders: usize,
closed: bool,
}
struct Inbox {
queue: Mutex<Queue>,
ready: Condvar,
}
pub struct Client(Arc<Inbox>);
impl Clone for Client {
fn clone(&self) -> Self {
self.0.queue.lock().unwrap().senders += 1;
Self(self.0.clone())
}
}
impl Drop for Client {
fn drop(&mut self) {
if let Ok(mut q) = self.0.queue.lock() {
q.senders -= 1;
if q.senders == 0 {
q.closed = true;
q.items.clear();
self.0.ready.notify_all();
}
}
}
}
fn enqueue(queue: &mut VecDeque<Input>, input: Input) -> bool {
if input.kind == "pointermove"
&& let Some(last) = queue.back_mut()
&& last.kind == "pointermove"
&& (last.window, last.session, last.mount) == (input.window, input.session, input.mount)
{
*last = input;
return true;
}
if queue.len() >= 64 {
return false;
}
queue.push_back(input);
true
}
impl Inbox {
fn receive(&self) -> Option<Input> {
let mut q = self.queue.lock().ok()?;
while q.items.is_empty() && !q.closed {
q = self.ready.wait(q).ok()?;
}
if q.closed { None } else { q.items.pop_front() }
}
}
impl Client {
/// 构造本类型并初始化其状态;资源建立和默认字段以函数体为准。
pub fn new(
proxy: EventLoopProxy<crate::app::AppEvent>,
namespace: Arc<RwLock<crate::document::NamespaceState>>,
control: crate::api::RuntimeControl,
) -> anyhow::Result<Self> {
let (sender, receiver) = sync_channel::<Input>(64);
let inbox = Arc::new(Inbox {
queue: Mutex::new(Queue {
items: VecDeque::new(),
senders: 1,
closed: false,
}),
ready: Condvar::new(),
});
let receiver = inbox.clone();
thread::Builder::new()
.name("tessera-native-events".into())
.spawn(move || {
for mut input in receiver {
while let Some(mut input) = receiver.receive() {
if let Ok(current) = namespace.read() {
if !refresh_continuous_input(&mut input, &current) {
continue;
@@ -182,18 +254,28 @@ impl Client {
} else {
continue;
}
if let Err(error) = send(&input) {
if let Err(error) = control.dispatch_native(input) {
let _ = proxy.send_event(crate::app::AppEvent::RuntimeFailed {
message: error.to_string(),
});
}
}
})?;
Ok(Self(sender))
Ok(Self(inbox))
}
/// 通过有界原生输入客户端或本地 HTTP 发送纯数据事件;过期版本按既定协议处理。
pub fn send(&self, input: Input) -> bool {
self.0.try_send(input).is_ok()
let Ok(mut q) = self.0.queue.lock() else {
return false;
};
if q.closed {
return false;
}
let queued = enqueue(&mut q.items, input);
if queued {
self.0.ready.notify_one();
}
queued
}
}
@@ -201,10 +283,23 @@ impl Client {
// the version at dequeue, never session, window or target. HTTP callers and
// discrete actions still receive strict stale-frame rejection.
/// 连续手势出队时只更新有效目标的帧版本;保留原 session、窗口和 Mount,离散事件不自动重试。
fn refresh_continuous_input(input: &mut Input, current: &crate::document::NamespaceState) -> bool {
pub(crate) fn refresh_continuous_input(
input: &mut Input,
current: &crate::document::NamespaceState,
) -> bool {
if !matches!(
input.kind.as_str(),
"viewport-pan" | "viewport-zoom" | "scene-pan" | "scene-orbit" | "scene-zoom" | "scroll"
"viewport-pan"
| "viewport-zoom"
| "scene-pan"
| "scene-orbit"
| "scene-zoom"
| "scroll"
| "pointermove"
| "pointerdown"
| "pointerup"
| "pointerleave"
| "pointercancel"
) {
return true;
}
@@ -229,6 +324,8 @@ fn refresh_continuous_input(input: &mut Input, current: &crate::document::Namesp
mount.canvas_viewport
} else if input.kind.starts_with("scene-") {
mount.scene3d_viewport && mount.camera_controls
} else if input.kind.starts_with("pointer") {
mount.pointer_events
} else {
mount.scroll.is_some()
}
@@ -240,39 +337,6 @@ fn refresh_continuous_input(input: &mut Input, current: &crate::document::Namesp
true
}
/// 通过有界原生输入客户端或本地 HTTP 发送纯数据事件;过期版本按既定协议处理。
fn send(input: &Input) -> anyhow::Result<()> {
let body = serde_json::to_string(input)?;
let mut stream =
TcpStream::connect_timeout(&crate::api::API_ADDRESS.parse()?, Duration::from_secs(2))?;
stream.set_read_timeout(Some(Duration::from_secs(10)))?;
stream.set_write_timeout(Some(Duration::from_secs(2)))?;
write!(
stream,
"POST /v1/namespaces/app.main/events HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
body.len(),
body
)?;
let mut response = String::new();
stream.take(32768).read_to_string(&mut response)?;
let status = response.split_whitespace().nth(1).unwrap_or("");
// An old frame's event is deliberately discarded, with no automatic retry.
if status == "409" {
return Ok(());
}
if status != "200" {
let body = response
.split_once("\r\n\r\n")
.map(|(_, body)| body)
.unwrap_or(&response);
anyhow::bail!(
"Native event failed: {}",
body.chars().take(1024).collect::<String>()
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
@@ -353,3 +417,46 @@ mod tests {
assert!(input.validate().is_err());
}
}
#[cfg(test)]
mod coalescing_tests {
use super::*;
#[test]
fn producer_side_motion_coalescing_keeps_action_and_target_boundaries() {
let template: Input = serde_json::from_str(
r#"{"session":1,"layout_version":1,"mount":2,"kind":"pointermove","point":[0,0]}"#,
)
.unwrap();
let mut q = VecDeque::new();
for i in 0..1000 {
let mut sample = template.clone();
sample.point = Some([i as f32, 0.]);
assert!(enqueue(&mut q, sample));
}
assert_eq!(q.len(), 1);
assert_eq!(q[0].point.unwrap()[0], 999.);
let mut down = template.clone();
down.kind = "pointerdown".into();
enqueue(&mut q, down);
let mut moved = template.clone();
moved.point = Some([1200., 0.]);
enqueue(&mut q, moved);
let mut up = template.clone();
up.kind = "pointerup".into();
enqueue(&mut q, up);
let mut other = template;
other.window = 2;
enqueue(&mut q, other);
assert_eq!(
q.iter().map(|i| i.kind.as_str()).collect::<Vec<_>>(),
[
"pointermove",
"pointerdown",
"pointermove",
"pointerup",
"pointermove"
]
);
assert_eq!(q.back().unwrap().window, 2);
}
}