171 lines
6.6 KiB
Rust
171 lines
6.6 KiB
Rust
//! 不依赖 Tessera 的 Nexus 宿主契约回归;需在获授权的构建环境编译/执行。
|
|
//! 验证宿主注册、半完成安装清理、脚本跨片名称和原文位置,而不是复制私有实现断言。
|
|
use anyhow::{Result, bail};
|
|
use endor_core::{ObjectPayload, ObjectRef, Value, Vm};
|
|
use endorphin_nexus_runtime as nexus;
|
|
use frontend::{
|
|
EndorReader,
|
|
ast::{Spanned, visitor::Visitor},
|
|
};
|
|
use nexus::{HostRegistration, VmSession, budget::AstBudget, program::compile_plan};
|
|
use std::{
|
|
any::Any,
|
|
sync::{
|
|
Arc,
|
|
atomic::{AtomicUsize, Ordering},
|
|
},
|
|
};
|
|
|
|
#[derive(Debug)]
|
|
/// 由假宿主分配的资源,析构计数在 VM 外可观察,不携带 UI 类型或托管出边。
|
|
struct Capability(Arc<AtomicUsize>);
|
|
impl Drop for Capability {
|
|
/// 最后强边移除时真正释放资源;根归还与 Rust 句柄复制不应增加此计数。
|
|
fn drop(&mut self) {
|
|
self.0.fetch_add(1, Ordering::SeqCst);
|
|
}
|
|
}
|
|
impl ObjectPayload for Capability {
|
|
/// Aster 载荷下转型入口,独立于宿主的名义类型注册。
|
|
fn as_any(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
/// 为错误/诊断提供类别,不让测试依赖 Tessera 的类型名。
|
|
fn kind_name(&self) -> &'static str {
|
|
"test-capability"
|
|
}
|
|
}
|
|
|
|
/// 只依赖 Nexus/Aster 的假宿主;可在分配并登记根后故意失败,观察安装失败的收尾。
|
|
struct Host {
|
|
drops: Arc<AtomicUsize>,
|
|
fail: bool,
|
|
}
|
|
impl HostRegistration for Host {
|
|
type Installed = ObjectRef;
|
|
/// Nexus 调用注册接口,宿主向实际 globals 安装能力,返回借用身份供测试检查。
|
|
fn register(
|
|
&self,
|
|
vm: &mut Vm,
|
|
roots: &mut Vec<ObjectRef>,
|
|
globals: ObjectRef,
|
|
) -> Result<ObjectRef> {
|
|
// 先登记拥有根,后续字典安装或故意失败都不能丢掉清理责任。
|
|
let capability = vm.allocate(Capability(self.drops.clone()))?;
|
|
roots.push(capability);
|
|
// 错误发生在能力根创建之后,检验会话能清理半完成注册。
|
|
if self.fail {
|
|
bail!("host registration failed");
|
|
}
|
|
vm.dict_set(
|
|
globals,
|
|
Value::Text("capability".into()),
|
|
Value::Object(capability),
|
|
)?;
|
|
Ok(capability)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
/// 宿主通过接口注册后,全局真实持有同一能力;close 释放根和字典边,重复 close 无副作用。
|
|
fn host_registration_uses_the_owned_vm_and_cleans_its_resources() {
|
|
// 计数器不在 VM 内,资源销毁后仍可用于观察。
|
|
let drops = Arc::new(AtomicUsize::new(0));
|
|
// 空会话不暗中安装 ui 或任何宿主对象。
|
|
let mut runtime = VmSession::new().unwrap();
|
|
assert_eq!(runtime.vm().object_count(), 0);
|
|
// 主环境拥有根由 Nexus 会话登记,宿主只收到借用句柄。
|
|
let globals = runtime.create_globals().unwrap();
|
|
// Installed 身份借用会话根,不能再把它当成独立拥有结果释放。
|
|
let capability = runtime
|
|
.register_host(&Host {
|
|
drops: drops.clone(),
|
|
fail: false,
|
|
})
|
|
.unwrap();
|
|
assert_eq!(
|
|
runtime
|
|
.vm()
|
|
.dict_get(globals, &Value::Text("capability".into()))
|
|
.unwrap(),
|
|
Some(Value::Object(capability))
|
|
);
|
|
assert_eq!(runtime.vm().ref_count(capability).unwrap(), 2);
|
|
runtime.close().unwrap();
|
|
runtime.close().unwrap();
|
|
assert_eq!(runtime.vm().object_count(), 0);
|
|
assert_eq!(drops.load(Ordering::SeqCst), 1);
|
|
}
|
|
|
|
#[test]
|
|
/// 缺环境时不进入宿主;重复环境初始化不分配;宿主部分失败留下的根仍由会话 close 清理。
|
|
fn failed_registration_and_repeated_initialization_preserve_cleanup() {
|
|
// 假宿主每次实际分配一个可计数资源。
|
|
let drops = Arc::new(AtomicUsize::new(0));
|
|
// 没有 globals 的会话应在调用宿主之前拒绝注册。
|
|
let mut runtime = VmSession::new().unwrap();
|
|
assert!(
|
|
runtime
|
|
.register_host(&Host {
|
|
drops: drops.clone(),
|
|
fail: false
|
|
})
|
|
.is_err()
|
|
);
|
|
assert_eq!(runtime.vm().object_count(), 0);
|
|
runtime.create_globals().unwrap();
|
|
assert!(runtime.create_globals().is_err());
|
|
assert_eq!(runtime.vm().object_count(), 1);
|
|
assert!(
|
|
runtime
|
|
.register_host(&Host {
|
|
drops: drops.clone(),
|
|
fail: true
|
|
})
|
|
.is_err()
|
|
);
|
|
runtime.close().unwrap();
|
|
assert_eq!(runtime.vm().object_count(), 0);
|
|
assert_eq!(drops.load(Ordering::SeqCst), 1);
|
|
}
|
|
|
|
#[test]
|
|
/// 脚本根保留原文跨度并按源序编译,后段读取前段建立的真实全局,不需要 UI 注册。
|
|
fn script_compilation_preserves_spans_and_shared_globals_without_ui() {
|
|
// 两个顶层交互片段,第二片段依赖前面的赋值名称。
|
|
let source = EndorReader::from_text("headless", "value = 2\nvalue + 3");
|
|
// Nexus 组织输入,词法/语法与 AST 节点继续由 Aster 提供。
|
|
let plan = nexus::source::parse(&source).unwrap();
|
|
assert_eq!(plan.roots().len(), 2);
|
|
assert!(plan.roots()[1].span().start >= "value = 2\n".len());
|
|
// 所有片段共享预算访问器,退出子树不重置累计节点数。
|
|
let mut budget = AstBudget::default();
|
|
for root in plan.roots() {
|
|
budget.visit_ast_root(root).unwrap();
|
|
}
|
|
assert!(budget.nodes() > 0);
|
|
// 空宿主名称集仍允许后续片段引用前段赋值,实际值由 VM 执行建立。
|
|
let templates = compile_plan(&source, &plan, Default::default()).unwrap();
|
|
// 一份 owning VM 和 globals 贯穿各片段,执行后归还每次返回的根。
|
|
let mut runtime = VmSession::new().unwrap();
|
|
let globals = runtime.create_globals().unwrap();
|
|
for (index, template) in templates.iter().enumerate() {
|
|
// 加载的代码根交给运行时清理,既不跨 VM 复用也不留下测试外根。
|
|
let code = template.load(runtime.vm_mut()).unwrap();
|
|
runtime.keep_root(code);
|
|
// 第一段赋值为 Nil,第二段求值应得到精确整数五。
|
|
let value = runtime
|
|
.vm_mut()
|
|
.execute_with_limits(code, globals, nexus::DEFAULT_LIMITS)
|
|
.unwrap();
|
|
if index == 1 {
|
|
assert_eq!(value, Value::Number(5_i64.into()));
|
|
}
|
|
runtime.vm_mut().release_value(value).unwrap();
|
|
}
|
|
runtime.close().unwrap();
|
|
assert_eq!(runtime.vm().object_count(), 0);
|
|
assert!(nexus::evaluate("ui").is_err());
|
|
assert_eq!(nexus::evaluate("0.1 + 0.2 == 0.3").unwrap(), "true");
|
|
}
|