merge: resolve manuscript conflicts with latest main comments
This commit is contained in:
@@ -33,7 +33,7 @@ fn object(value: Value) -> Result<ObjectRef, VmError> {
|
||||
Err(invalid("paint must return managed objects"))
|
||||
}
|
||||
}
|
||||
/// 把当前源值或文本选择解码成此调用需要的文本表示。
|
||||
/// 读取指定命令字段并要求其值为 Text;不隐式字符串化其他类型。
|
||||
fn text(store: &ObjectStore, object: ObjectRef, name: &str) -> Result<String, VmError> {
|
||||
if let Value::Text(value) = store.get_attr(object, name)? {
|
||||
Ok(value)
|
||||
@@ -46,6 +46,7 @@ fn number(store: &ObjectStore, object: ObjectRef, name: &str) -> Result<f32, VmE
|
||||
let Value::Number(value) = store.get_attr(object, name)? else {
|
||||
return Err(invalid("paint field must be a Number"));
|
||||
};
|
||||
// x/y 允许负数以表达相对原点的绘制;其余长度必须非负。所有数值都必须有限且在尺寸预算内。
|
||||
let value = value
|
||||
.to_string()
|
||||
.parse::<f32>()
|
||||
@@ -73,12 +74,14 @@ fn decode(
|
||||
command: ObjectRef,
|
||||
expected: ObjectRef,
|
||||
) -> Result<RenderCommand, VmError> {
|
||||
// 使用当前 desktop 注册的 PaintCommand 名义类型;字段形状相似的普通对象也不能冒充命令。
|
||||
if !store.is_instance(command, expected)? {
|
||||
return Err(invalid("paint commands must be PaintCommand instances"));
|
||||
}
|
||||
let x = number(store, command, "x")?;
|
||||
let y = number(store, command, "y")?;
|
||||
let kind = text(store, command, "kind")?;
|
||||
// 场景和图片命令持有独立资源,无需读取通用颜色字段。
|
||||
let color = if matches!(kind.as_str(), "scene3d" | "image") {
|
||||
String::new()
|
||||
} else {
|
||||
@@ -87,6 +90,7 @@ fn decode(
|
||||
let result = match kind.as_str() {
|
||||
"image" => crate::bridge::graphics::decode_image(store, command)?,
|
||||
"scene3d" => {
|
||||
// 这里仅接受宿主生成的 PaintView payload,克隆原生 Arc,不把活动的 VM 场景图传到绘制线程。
|
||||
let snapshot = object(store.get_attr(command, "scene")?)?;
|
||||
RenderCommand::Scene3D {
|
||||
x,
|
||||
@@ -103,6 +107,7 @@ fn decode(
|
||||
let width = number(store, command, "width")?;
|
||||
let height = number(store, command, "height")?;
|
||||
let radius = number(store, command, "radius")?;
|
||||
// 单项数值合法还不足以保证右/下边界合法;额外检查原点与尺寸相加后的范围。
|
||||
if x + width > MAX_DIMENSION as f32 || y + height > MAX_DIMENSION as f32 {
|
||||
return Err(invalid("paint rectangle exceeds bounds"));
|
||||
}
|
||||
@@ -129,9 +134,11 @@ fn decode(
|
||||
}
|
||||
"text" => {
|
||||
let size = number(store, command, "size")?;
|
||||
// size 是离散文字等级而非任意像素字号,只接受 1 到 8 的整数。
|
||||
if size.fract() != 0.0 || !(1.0..=8.0).contains(&size) {
|
||||
return Err(invalid("paint text size must be an integer from 1 to 8"));
|
||||
}
|
||||
// 两个换行边界必须同时出现或同时缺失;只缺一个时传播属性错误,防止半定义文本框。
|
||||
let wrap_bounds = match (
|
||||
store.get_attr(command, "wrapWidth"),
|
||||
store.get_attr(command, "wrapHeight"),
|
||||
@@ -154,6 +161,7 @@ fn decode(
|
||||
}
|
||||
_ => return Err(invalid("unsupported paint command kind")),
|
||||
};
|
||||
// 变体字段解码后仍走统一命令校验,覆盖颜色格式、文本以及组合几何合法性。
|
||||
result
|
||||
.validate()
|
||||
.map_err(|_| invalid("invalid paint command geometry, text or color"))?;
|
||||
@@ -163,7 +171,9 @@ fn decode(
|
||||
#[derive(Debug, Default)]
|
||||
/// 按出现 ID 收集已检查的源绘制覆盖,随后与原生默认命令合并。
|
||||
struct PaintOutput {
|
||||
// 按出现 ID 收集覆盖;同一个业务 Block 在多个 Mount 中可产生不同绘制结果。
|
||||
commands: BTreeMap<u64, Vec<RenderCommand>>,
|
||||
// 全轮源命令累计数,不能靠拆分多个源 DisplayList 绕过 10000 条总预算。
|
||||
count: usize,
|
||||
}
|
||||
type Output = Arc<Mutex<PaintOutput>>;
|
||||
@@ -175,12 +185,12 @@ struct Collect {
|
||||
output: Output,
|
||||
}
|
||||
impl NativeCallable for Collect {
|
||||
/// 声明宿主可调用对象接受的参数个数,供 VM 在执行前检查。
|
||||
/// 接收当前出现位置 paint 的唯一 DisplayList 结果。
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(1))
|
||||
}
|
||||
|
||||
/// VM 调入本宿主能力的入口;按所属操作解码参数并返回托管值或后续调用。
|
||||
/// 验证返回名义类型并完整解码所有 PaintCommand,检查全轮命令预算后才把该出现位置结果写入输出表。
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let [Value::Object(result)] = args else {
|
||||
return Err(invalid("paint must return a DisplayList"));
|
||||
@@ -191,12 +201,14 @@ impl NativeCallable for Collect {
|
||||
)? {
|
||||
return Err(invalid("paint must return a DisplayList"));
|
||||
}
|
||||
// 先验证外层 DisplayList,再取其受控 ObjectList;所有成员解码成功后才写入 output。
|
||||
let commands = object(cx.store().get_attr(*result, "commands")?)?;
|
||||
let expected = named_type(cx.store(), self.desktop, "PaintCommand")?;
|
||||
let commands = object_list::items(cx.store(), commands)?
|
||||
.iter()
|
||||
.map(|command| decode(cx.store(), *command, expected))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
// 仅收集结果时持锁,不在调用源 paint 期间持有结果锁;失败不会插入半个命令列表。
|
||||
let mut output = self
|
||||
.output
|
||||
.lock()
|
||||
@@ -217,13 +229,14 @@ struct Prepare {
|
||||
output: Output,
|
||||
}
|
||||
impl NativeCallable for Prepare {
|
||||
/// 声明宿主可调用对象接受的参数个数,供 VM 在执行前检查。
|
||||
/// 零参数驱动入口,待绘制 Mount 和输出容器已保存在适配器。
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(0))
|
||||
}
|
||||
|
||||
/// VM 调入本宿主能力的入口;按所属操作解码参数并返回托管值或后续调用。
|
||||
/// 建立共享游标与固定外层字节码循环,让 VM 每轮只准备一个出现位置的 paint 调用。
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
|
||||
// 构造一个共享游标驱动;每轮只准备当前出现,避免一次保留所有出现的临时实参。
|
||||
let advance = cx.allocate(ContextNativeFunction::new(
|
||||
"next paint occurrence",
|
||||
Advance {
|
||||
@@ -251,12 +264,13 @@ struct Advance {
|
||||
output: Output,
|
||||
}
|
||||
impl NativeCallable for Advance {
|
||||
/// 声明宿主可调用对象接受的参数个数,供 VM 在执行前检查。
|
||||
/// 由外层循环无参调用,下一出现位置由内部游标决定。
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(0))
|
||||
}
|
||||
/// VM 调入本宿主能力的入口;按所属操作解码参数并返回托管值或后续调用。
|
||||
/// 短锁内取并推进游标,随后验证/准备当前 Mount 的方法与几何;用临时调用链收集结果,完成后才进入下一位置。
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
|
||||
// 锁只保护取游标与递增;离开此块后释放锁,再做 VM 分配和源调用。
|
||||
let occurrence = {
|
||||
let mut next = self
|
||||
.next
|
||||
@@ -266,6 +280,7 @@ impl NativeCallable for Advance {
|
||||
*next += usize::from(occurrence.is_some());
|
||||
occurrence
|
||||
};
|
||||
// false 是 repeat_call 的停止信号;有出现时即使跳过原生方法也返回 true 继续遍历。
|
||||
let Some((id, weak)) = occurrence else {
|
||||
return Ok(NativeResult::Return(Value::Bool(false)));
|
||||
};
|
||||
@@ -284,9 +299,11 @@ impl NativeCallable for Advance {
|
||||
.store()
|
||||
.lookup_method(definition, "paint")?
|
||||
.ok_or(invalid("missing paint method"))?;
|
||||
// 原生默认 paint 留给 finish 生成;仅执行实际覆盖的源方法,避免重复绘制同一出现。
|
||||
if crate::native_visual::is_intrinsic(cx.store(), function, "paint", self.desktop)? {
|
||||
return Ok(NativeResult::Return(Value::Bool(true)));
|
||||
}
|
||||
// 把已提交 Mount 几何封装为本轮临时 Bounds;新分配参数可初始化,业务模型仍处于只读域。
|
||||
let bounds = cx.new_object()?;
|
||||
attach_desktop_type(cx, bounds, "Bounds", self.desktop)?;
|
||||
for (name, value) in [
|
||||
@@ -297,6 +314,7 @@ impl NativeCallable for Advance {
|
||||
] {
|
||||
cx.set_attr(bounds, name, Value::Number((value as f64).into()))?;
|
||||
}
|
||||
// 收集器只捕获桌面根和当前出现 ID;输出是原生共享容器,不增加业务图的强引用。
|
||||
let collector = cx.allocate(ContextNativeFunction::new(
|
||||
"collect source paint",
|
||||
Collect {
|
||||
@@ -306,6 +324,7 @@ impl NativeCallable for Advance {
|
||||
},
|
||||
vec![self.desktop],
|
||||
))?;
|
||||
// 辅助帧先执行 paint(block, mount, bounds),收集其结果,再返回 true 推进外层游标。
|
||||
let function = crate::vm_sequence::collect_and_continue(
|
||||
cx,
|
||||
collector,
|
||||
@@ -327,7 +346,9 @@ impl NativeCallable for Advance {
|
||||
|
||||
/// 已经完成本阶段、但尚未由桥接层发布的派生结果,包含帧/缓存或真实读取依赖。
|
||||
pub struct Draft {
|
||||
// 已经合并命令的原生帧;Draft 仍需桥接层提交,完成计算不等于发布成功。
|
||||
pub frame: Frame,
|
||||
// 保留源绘制的业务读取依赖,供下次写入后判定是否必须重绘。
|
||||
pub reads: Vec<endor_core::ReadDependency>,
|
||||
pub slices: usize,
|
||||
}
|
||||
@@ -343,6 +364,7 @@ pub enum Progress {
|
||||
/// 拥有未完成源调用及临时根的工作句柄;丢弃 Rust 值不能替代 VM 内的取消。
|
||||
pub struct Work {
|
||||
desktop: ObjectRef,
|
||||
// 跨切片保留的入口根;完成或取消都必须在所属 VM 中显式释放。
|
||||
prepare: ObjectRef,
|
||||
output: Output,
|
||||
root: Option<Arc<UiNode>>,
|
||||
@@ -353,6 +375,7 @@ pub struct Work {
|
||||
impl Work {
|
||||
/// 继续当前绘制续体,保留其先前收集的命令和所属窗口上下文。
|
||||
pub fn resume(mut self, vm: &mut Vm, slice: usize) -> Result<Progress> {
|
||||
// 恢复保留原执行预算,slice 只决定本次让出的指令配额,不能重置整个绘制上限。
|
||||
self.slices += 1;
|
||||
let progress = vm.resume_read_only_call(slice);
|
||||
self.accept(vm, progress)
|
||||
@@ -360,6 +383,7 @@ impl Work {
|
||||
|
||||
/// 关闭未完成绘制续体并释放其临时根,不发布半完成命令列表。
|
||||
pub fn cancel(self, vm: &mut Vm) -> Result<()> {
|
||||
// 先执行取消和 release 两个操作,再依次传播错误;取消失败也仍尝试释放入口根。
|
||||
let cancelled = vm.cancel_read_only_call();
|
||||
let released = vm.release(self.prepare);
|
||||
cancelled?;
|
||||
@@ -374,6 +398,7 @@ impl Work {
|
||||
progress: Result<endor_core::ExecutionProgress, VmError>,
|
||||
) -> Result<Progress> {
|
||||
match progress {
|
||||
// 挂起时工作句柄继续持有 frame/output/prepare,不能发布部分输出。
|
||||
Ok(endor_core::ExecutionProgress::Pending { .. }) => {
|
||||
Ok(Progress::Pending(Box::new(self)))
|
||||
}
|
||||
@@ -382,11 +407,13 @@ impl Work {
|
||||
dependencies,
|
||||
..
|
||||
}) => {
|
||||
// VM 完成后先释放入口与驱动返回值,再读取纯原生收集结果;返回值本身不作为绘制命令。
|
||||
vm.release(self.prepare)?;
|
||||
vm.release_value(value)?;
|
||||
finish(vm, self, dependencies)
|
||||
}
|
||||
Err(error) => {
|
||||
// 趁 VM 诊断信息仍可用先生成错误,再取消只读执行并清理临时根。
|
||||
let diagnostic = crate::endor_host::runtime_error(vm, error);
|
||||
self.cancel(vm)?;
|
||||
Err(diagnostic)
|
||||
@@ -404,6 +431,7 @@ pub fn start(
|
||||
frame: Frame,
|
||||
slice: usize,
|
||||
) -> Result<Progress> {
|
||||
// 禁止零切片和嵌套只读/挂起调用;同一 VM 同时只能由一个续体驱动。
|
||||
anyhow::ensure!(slice > 0, "paint slice must be positive");
|
||||
anyhow::ensure!(
|
||||
!vm.has_suspended_call() && !vm.object_store().is_read_only(),
|
||||
@@ -445,7 +473,9 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
.output
|
||||
.lock()
|
||||
.map_err(|_| anyhow::anyhow!("paint output poisoned"))?;
|
||||
// 是否有源覆盖以 map 是否为空判断;源方法返回空命令列表也算覆盖,会抑制该出现的默认绘制。
|
||||
let native_only = output.commands.is_empty();
|
||||
// 没有根时没有节点投影可合并,直接交回已有帧并清空绘制读取,不展开默认命令。
|
||||
if work.root.is_none() {
|
||||
return Ok(Progress::Complete(Box::new(Draft {
|
||||
frame: work.frame,
|
||||
@@ -456,6 +486,7 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
let root = work.root.as_deref();
|
||||
let desktop = work.desktop;
|
||||
let frame = &mut work.frame;
|
||||
// 建立 Block ID 到不可变节点的索引;最终合并顺序仍以 frame.mounts 为准,不能按 map 排序。
|
||||
let mut nodes = HashMap::new();
|
||||
let mut pending: Vec<_> = root.into_iter().collect();
|
||||
while let Some(node) = pending.pop() {
|
||||
@@ -467,7 +498,9 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
let mut views = Vec::new();
|
||||
let mut owners = Vec::new();
|
||||
for mount in &frame.mounts {
|
||||
// 记录本出现追加前的长度,用差值为新增命令同步扩展 clip/view/owner 三张平行表。
|
||||
let before = commands.len();
|
||||
// 有源覆盖则使用其整个结果(包括空列表);否则用节点和 Mount 几何生成原生默认命令。
|
||||
if let Some(custom) = output.commands.get(&mount.id) {
|
||||
commands.extend(custom.iter().cloned());
|
||||
} else {
|
||||
@@ -487,6 +520,7 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
clips.extend(std::iter::repeat_n(mount.clip, commands.len() - before));
|
||||
views.extend(std::iter::repeat_n(mount.view, commands.len() - before));
|
||||
owners.extend(std::iter::repeat_n(mount.id, commands.len() - before));
|
||||
// 混合原生与源命令后再检查总预算,前面的源输出预算不能覆盖这里新增的默认命令。
|
||||
if commands.len() > 10_000 {
|
||||
bail!("paint command budget exceeded");
|
||||
}
|
||||
@@ -495,8 +529,11 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
frame.command_clips = clips;
|
||||
frame.command_views = views;
|
||||
frame.command_mounts = owners;
|
||||
// 统一命令和投影元数据就绪后,再把世界 UI 面板装配进对应 3D 场景。
|
||||
crate::scene3d::panel::compose(frame)?;
|
||||
let registry = object(vm.get_attr(desktop, "types")?)?;
|
||||
// 排除宿主桌面、类型注册表、Mount 以及已经死亡的临时身份,只保留业务读取。
|
||||
// 纯原生帧不需要保留这次驱动的读取记录。
|
||||
let reads = reads
|
||||
.into_iter()
|
||||
.filter(|dependency| {
|
||||
|
||||
Reference in New Issue
Block a user