refactor: 退役内置盒克隆重写族死函数与孤儿辅助

This commit is contained in:
2026-10-02 20:35:48 +00:00
parent 43acaacd08
commit b76f2214f9
8 changed files with 17 additions and 358 deletions
-73
View File
@@ -235,79 +235,6 @@ pub fn module_ns_cell_edges(obj: &JsObject) -> Vec<*mut Cell> {
out
}
/// 原地重写条目表内的 JsValue 边(对象搬移/epoch 晋升后的引用重定位)。
///
/// # 副作用
/// - 改写 `Value` 条目的值与共享 cell 内的值;cell 指针本身稳定不变。
pub fn rewrite_module_ns_native<F>(obj: &mut JsObject, mut rewrite: F)
where
F: FnMut(JsValue) -> JsValue,
{
let table = ns_table_ptr(obj);
if table.is_null() {
return;
}
// SAFETY: table 归本 ns 对象持有,原地改写其 JsValue 边。
let table_ref = unsafe { &mut *table };
for entry in table_ref.entries.iter_mut() {
match &mut entry.state {
ModuleNsState::Value { value, .. } => *value = rewrite(*value),
ModuleNsState::Cell(cell) => {
if !cell.is_null() {
// SAFETY: cell 生命周期见 `module_ns_export`;共享 cell 内值一并改写。
let cell_ref = unsafe { &mut **cell };
cell_ref.value = rewrite(cell_ref.value);
}
}
}
}
}
/// 深拷贝条目表到 `dst`,并用 `rewrite` 改写其中的对象引用。
///
/// # 注意事项
/// - `clone_for_session_epoch` 只浅拷贝 `native_data` 指针;本函数必须在新对象上
/// 重建独立 Box,否则原件与克隆共享同一表会在释放时双放。
/// - `Cell` 指针本身跨搬移稳定,只改写其内部值。
pub fn clone_module_ns_native_with_rewrite<F>(src: &JsObject, dst: &mut JsObject, mut rewrite: F)
where
F: FnMut(JsValue) -> JsValue,
{
if !src.is_module_namespace() {
return;
}
let table = src.native_data() as *const ModuleNsTable;
if table.is_null() {
dst.set_native_data(std::ptr::null_mut());
return;
}
// SAFETY: src.native_data 指向 src 持有的 Box<ModuleNsTable>。
let table_ref = unsafe { &*table };
let mut entries = Vec::with_capacity(table_ref.entries.len());
for entry in &table_ref.entries {
let state = match &entry.state {
ModuleNsState::Value { value, initialized } => ModuleNsState::Value {
value: if value.is_object() { rewrite(*value) } else { *value },
initialized: *initialized,
},
ModuleNsState::Cell(cell) => {
if !cell.is_null() {
// SAFETY: cell 生命周期见 `module_ns_export`;晋升/搬移后改写其内值。
let cell_ref = unsafe { &mut **cell };
cell_ref.value = rewrite(cell_ref.value);
}
ModuleNsState::Cell(*cell)
}
};
entries.push(ModuleNsEntry {
name_si: entry.name_si,
origin: entry.origin,
state,
});
}
dst.set_native_data(Box::into_raw(Box::new(ModuleNsTable { entries })) as *mut u8);
}
/// 只读核算条目表的 native 数据字节(不释放),与 `drop_module_ns_native` 的
/// capacity 口径一致,供 GC 账目核算。
pub fn module_ns_native_size(obj: &JsObject) -> u64 {