跳转到内容

Host 对象生命周期

Host objects 由 Rust 拥有。Vela 存储的是指向它们的 handles,不是对象本身。

CallArgs::with_host_refCallArgs::with_host_mut 会为一次调用绑定 Rust 值。VM 看到的是 HostRef handle 和 call-local adapter binding。

runtime.call(
"main",
CallArgs::new().with_host_mut("player", &mut player),
CallOptions::unbounded(),
)?;

调用返回后,direct binding 就结束了。持久状态仍在 Rust 里,不在脚本 heap 里。

extern state binding 保存 Rust 拥有的 persistent host object。对象必须是 Send,因为 runtime 可以被移动到 worker 线程。

let runtime = Runtime::builder(engine, program)?
.bind_extern_state("main::player", player)?
.build()?;

VM state 不同:records、arrays、maps、sets、enums 和 scalars 由 Runtime 的脚本 heap root 并追踪。

HostRef 包含 generation。如果对象被移除或替换后 slot 被复用,adapter 可以 拒绝 stale handle,而不是静默写入错误对象。拒绝是 runtime diagnostic,不是 best-effort fallback。