fix: 脏重建方法 wrapper/构造器/global 槽跨轮复用迁移——FnWrapperKey 四分件键(加 label 消解同名原型 next/return/throw 碰撞)登记表继承止 S2 每轮 wrapper 无界累积,global 槽原位更新堵住旧构造器滞留 UAF 与每轮 +3 槽无界增长

This commit is contained in:
2026-09-11 18:06:19 +00:00
parent 4095ad2c1d
commit b17119ce16
12 changed files with 805 additions and 329 deletions
@@ -2,7 +2,7 @@ use std::sync::Arc;
use oxide_kernel::kernel::{KernelCore, KernelSession};
use oxide_kernel::shape_forge::EMPTY_SHAPE_ID;
use oxide_types::object::{JsObject, PropAttributes};
use oxide_types::object::{JsObject, NativeFnPtr, PropAttributes};
use oxide_types::value::JsValue;
use crate::async_disposable::dispose_async;
@@ -91,35 +91,56 @@ pub fn bind_async_disposable_stack(core: &Arc<KernelCore>, session: &KernelSessi
let ctor_val = if ctor_val.is_object() {
ctor_val
} else {
// Box 自建:[[Prototype]] 指向 Function.prototype,prototype/name 描述符按规范。
let function_proto = world.function_proto.as_ptr() as *mut JsObject;
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(function_proto)));
ctor.set_function(true);
configure_native_constructor(
&mut ctor,
oxide_builtins::disposable_stack::async_disposable_stack_constructor::<crate::vm::Vm> as *const (),
0,
);
let sf = core.perm_interner().as_ref();
let sh = core.shape_forge().as_ref();
let si_prototype = sf.intern("prototype").0;
let si_name = sf.intern("name").0;
let si_length = sf.intern("length").0;
let ctor_shape1 = sh.make_shape(EMPTY_SHAPE_ID, si_prototype);
let ctor_shape2 = sh.make_shape(ctor_shape1, si_name);
let ctor_shape3 = sh.make_shape(ctor_shape2, si_length);
ctor.set_shape_id(ctor_shape3);
ctor.ensure_hash_props()
.push(JsValue::from_js_object(world.async_disposable_stack_proto.as_ptr() as *mut JsObject));
ctor.ensure_hash_props()
.push(JsValue::perm_string(sf.string_ptr(sf.intern("AsyncDisposableStack").0)));
ctor.ensure_hash_props().push(JsValue::int(0));
ctor.set_data_meta(0u32, PropAttributes::new(false, false, false));
ctor.set_data_meta(1u32, PropAttributes::new(false, false, true));
ctor.set_data_meta(2u32, PropAttributes::new(false, false, true));
let ctor_ptr = Box::into_raw(ctor);
// 登记进 world 释放表:session 收尾统一释放构造器本体与属性区。
world.track_leaked_object(ctor_ptr);
let name_si = sf.intern("AsyncDisposableStack").0;
// 选择性重建复用:非 P 目标(家族 0)按键查找前轮旧构造器迁移,避免每轮
// 新建导致登记表无界累积;槽键取 global 槽名,该命名空间内全局唯一。
let ctor_fn =
oxide_builtins::disposable_stack::async_disposable_stack_constructor::<crate::vm::Vm> as *const ();
// SAFETY: ctor_fn 是转成 *const () 的 NativeFn 函数项指针。
let ctor_fn_ptr = unsafe { NativeFnPtr::from_raw(ctor_fn) };
let reuse_key = oxide_kernel::builtin::FnWrapperKey::new(0, 0, name_si, name_si);
let ctor_ptr = match world.find_fn_wrapper(reuse_key, ctor_fn_ptr, 0) {
Some(ptr) => {
// 迁移内引用:prototype 槽改指新 AsyncDisposableStack.prototype
// (旧原型已被重建替换释放)。
let ctor = unsafe { &mut *ptr };
let si_prototype = sf.intern("prototype").0;
if let Some(pos) = sh.lookup_position(ctor.shape_id(), si_prototype) {
ctor.set_prop_at(
pos,
JsValue::from_js_object(world.async_disposable_stack_proto.as_ptr() as *mut JsObject),
);
}
ptr
}
None => {
// Box 自建:[[Prototype]] 指向 Function.prototype,prototype/name 描述符按规范。
let function_proto = world.function_proto.as_ptr() as *mut JsObject;
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(function_proto)));
ctor.set_function(true);
configure_native_constructor(&mut ctor, ctor_fn, 0);
let si_prototype = sf.intern("prototype").0;
let si_name = sf.intern("name").0;
let si_length = sf.intern("length").0;
let ctor_shape1 = sh.make_shape(EMPTY_SHAPE_ID, si_prototype);
let ctor_shape2 = sh.make_shape(ctor_shape1, si_name);
let ctor_shape3 = sh.make_shape(ctor_shape2, si_length);
ctor.set_shape_id(ctor_shape3);
ctor.ensure_hash_props()
.push(JsValue::from_js_object(world.async_disposable_stack_proto.as_ptr() as *mut JsObject));
ctor.ensure_hash_props().push(JsValue::perm_string(sf.string_ptr(name_si)));
ctor.ensure_hash_props().push(JsValue::int(0));
ctor.set_data_meta(0u32, PropAttributes::new(false, false, false));
ctor.set_data_meta(1u32, PropAttributes::new(false, false, true));
ctor.set_data_meta(2u32, PropAttributes::new(false, false, true));
let ctor_ptr = Box::into_raw(ctor);
// 登记进 world 释放表(带复用键):session 收尾统一释放构造器本体与属性区。
world.track_fn_wrapper(ctor_ptr, reuse_key);
ctor_ptr
}
};
JsValue::from_js_object(ctor_ptr)
};
@@ -2,7 +2,7 @@ use std::sync::Arc;
use oxide_kernel::kernel::{KernelCore, KernelSession};
use oxide_kernel::shape_forge::EMPTY_SHAPE_ID;
use oxide_types::object::{JsObject, PropAttributes};
use oxide_types::object::{JsObject, NativeFnPtr, PropAttributes};
use oxide_types::value::JsValue;
use crate::bindings::{
@@ -109,35 +109,55 @@ pub fn bind_disposable_stack(core: &Arc<KernelCore>, session: &KernelSession, gl
let ctor_val = if ctor_val.is_object() {
ctor_val
} else {
// Box 自建:[[Prototype]] 指向 Function.prototype,prototype/name 描述符按规范。
let function_proto = world.function_proto.as_ptr() as *mut JsObject;
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(function_proto)));
ctor.set_function(true);
configure_native_constructor(
&mut ctor,
oxide_builtins::disposable_stack::disposable_stack_constructor::<crate::vm::Vm> as *const (),
0,
);
let sf = core.perm_interner().as_ref();
let sh = core.shape_forge().as_ref();
let si_prototype = sf.intern("prototype").0;
let si_name = sf.intern("name").0;
let si_length = sf.intern("length").0;
let ctor_shape1 = sh.make_shape(EMPTY_SHAPE_ID, si_prototype);
let ctor_shape2 = sh.make_shape(ctor_shape1, si_name);
let ctor_shape3 = sh.make_shape(ctor_shape2, si_length);
ctor.set_shape_id(ctor_shape3);
ctor.ensure_hash_props()
.push(JsValue::from_js_object(world.disposable_stack_proto.as_ptr() as *mut JsObject));
ctor.ensure_hash_props()
.push(JsValue::perm_string(sf.string_ptr(sf.intern("DisposableStack").0)));
ctor.ensure_hash_props().push(JsValue::int(0));
ctor.set_data_meta(0u32, PropAttributes::new(false, false, false));
ctor.set_data_meta(1u32, PropAttributes::new(false, false, true));
ctor.set_data_meta(2u32, PropAttributes::new(false, false, true));
let ctor_ptr = Box::into_raw(ctor);
// 登记进 world 释放表:session 收尾统一释放构造器本体与属性区。
world.track_leaked_object(ctor_ptr);
let name_si = sf.intern("DisposableStack").0;
// 选择性重建复用:非 P 目标(家族 0)按键查找前轮旧构造器迁移,避免每轮
// 新建导致登记表无界累积;槽键取 global 槽名,该命名空间内全局唯一。
let ctor_fn = oxide_builtins::disposable_stack::disposable_stack_constructor::<crate::vm::Vm> as *const ();
// SAFETY: ctor_fn 是转成 *const () 的 NativeFn 函数项指针。
let ctor_fn_ptr = unsafe { NativeFnPtr::from_raw(ctor_fn) };
let reuse_key = oxide_kernel::builtin::FnWrapperKey::new(0, 0, name_si, name_si);
let ctor_ptr = match world.find_fn_wrapper(reuse_key, ctor_fn_ptr, 0) {
Some(ptr) => {
// 迁移内引用:prototype 槽改指新 DisposableStack.prototype
// (旧原型已被重建替换释放)。
let ctor = unsafe { &mut *ptr };
let si_prototype = sf.intern("prototype").0;
if let Some(pos) = sh.lookup_position(ctor.shape_id(), si_prototype) {
ctor.set_prop_at(
pos,
JsValue::from_js_object(world.disposable_stack_proto.as_ptr() as *mut JsObject),
);
}
ptr
}
None => {
// Box 自建:[[Prototype]] 指向 Function.prototype,prototype/name 描述符按规范。
let function_proto = world.function_proto.as_ptr() as *mut JsObject;
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(function_proto)));
ctor.set_function(true);
configure_native_constructor(&mut ctor, ctor_fn, 0);
let si_prototype = sf.intern("prototype").0;
let si_name = sf.intern("name").0;
let si_length = sf.intern("length").0;
let ctor_shape1 = sh.make_shape(EMPTY_SHAPE_ID, si_prototype);
let ctor_shape2 = sh.make_shape(ctor_shape1, si_name);
let ctor_shape3 = sh.make_shape(ctor_shape2, si_length);
ctor.set_shape_id(ctor_shape3);
ctor.ensure_hash_props()
.push(JsValue::from_js_object(world.disposable_stack_proto.as_ptr() as *mut JsObject));
ctor.ensure_hash_props().push(JsValue::perm_string(sf.string_ptr(name_si)));
ctor.ensure_hash_props().push(JsValue::int(0));
ctor.set_data_meta(0u32, PropAttributes::new(false, false, false));
ctor.set_data_meta(1u32, PropAttributes::new(false, false, true));
ctor.set_data_meta(2u32, PropAttributes::new(false, false, true));
let ctor_ptr = Box::into_raw(ctor);
// 登记进 world 释放表(带复用键):session 收尾统一释放构造器本体与属性区。
world.track_fn_wrapper(ctor_ptr, reuse_key);
ctor_ptr
}
};
JsValue::from_js_object(ctor_ptr)
};
+86 -50
View File
@@ -12,55 +12,86 @@ fn bind_error_subtype_constructor(
) {
let sf = core.perm_interner().as_ref();
let sh = core.shape_forge().as_ref();
// 子类型构造器 [[Prototype]] 指向 Error 构造器(规范:NativeError 构造器继承 Error
// 构造器,而非直接继承 Function.prototype;instanceof 走 @@hasInstance 不受影响)。
let error_ctor_ptr = session.builtin_world().error_constructor.as_ptr() as *mut JsObject;
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(error_ctor_ptr)));
ctor.set_function(true);
// Error 子类型须标记可构造:new TypeError(...) 走 NEW_EXPRESSION 校验。
ctor.type_tag = JsObject::OBJ_TYPE_CONSTRUCTOR;
// SAFETY: ctor_fn 是调用方转成 *const () 的 NativeFn 函数项指针。
ctor.set_native_fn(Some(unsafe { NativeFnPtr::from_raw(ctor_fn) }));
ctor.set_native_arg_count(arg_count);
let si_prototype = sf.intern("prototype").0;
let si_name = sf.intern("name").0;
let si_length = sf.intern("length").0;
let world = session.builtin_world();
let name_si = sf.intern(name).0;
// SAFETY: ctor_fn 是调用方转成 *const () 的 NativeFn 函数项指针。
let ctor_fn_ptr = unsafe { NativeFnPtr::from_raw(ctor_fn) };
// 选择性重建复用:非 P 目标(家族 0)按键查找前轮旧构造器迁移,避免每轮
// 新建导致登记表无界累积;槽键取 global 槽名,该命名空间内全局唯一。
let reuse_key = oxide_kernel::builtin::FnWrapperKey::new(0, 0, name_si, name_si);
let si_prototype = sf.intern("prototype").0;
let ctor_ptr = match world.find_fn_wrapper(reuse_key, ctor_fn_ptr, arg_count) {
Some(ptr) => {
// 迁移内引用:prototype 槽改指新子类型原型(旧原型已被重建替换释放)。
let ctor = unsafe { &mut *ptr };
if let Some(pos) = sh.lookup_position(ctor.shape_id(), si_prototype) {
ctor.set_prop_at(pos, JsValue::from_js_object(proto_ptr));
}
ptr
}
None => {
// 子类型构造器 [[Prototype]] 指向 Error 构造器(规范:NativeError
// 构造器继承 Error 构造器,而非直接继承 Function.prototype;instanceof
// 走 @@hasInstance 不受影响)。
let error_ctor_ptr = world.error_constructor.as_ptr() as *mut JsObject;
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(error_ctor_ptr)));
ctor.set_function(true);
// Error 子类型须标记可构造:new TypeError(...) 走 NEW_EXPRESSION 校验。
ctor.type_tag = JsObject::OBJ_TYPE_CONSTRUCTOR;
ctor.set_native_fn(Some(ctor_fn_ptr));
ctor.set_native_arg_count(arg_count);
let ctor_shape1 = sh.make_shape(EMPTY_SHAPE_ID, si_prototype);
let ctor_shape2 = sh.make_shape(ctor_shape1, si_name);
let ctor_shape3 = sh.make_shape(ctor_shape2, si_length);
ctor.set_shape_id(ctor_shape3);
ctor.ensure_hash_props().push(JsValue::from_js_object(proto_ptr));
ctor.ensure_hash_props().push(JsValue::perm_string(sf.string_ptr(name_si)));
ctor.ensure_hash_props().push(JsValue::int(arg_count as i32));
// 构造器 prototype 槽按规范 { writable:false, enumerable:false, configurable:false }。
ctor.set_data_meta(0u32, PropAttributes::new(false, false, false));
// 构造器 name 槽按规范 { writable:false, enumerable:false, configurable:true }。
ctor.set_data_meta(1u32, PropAttributes::new(false, false, true));
// 构造器 length 按规范为不可写不可枚举(Function.length 属性描述符约定)。
ctor.set_data_meta(2u32, PropAttributes::new(false, false, true));
let si_name = sf.intern("name").0;
let si_length = sf.intern("length").0;
let ctor_ptr = Box::into_raw(ctor);
// 登记进 world 释放表:session 收尾统一释放构造器本体与属性区。
session.builtin_world().track_leaked_object(ctor_ptr);
let ctor_shape1 = sh.make_shape(EMPTY_SHAPE_ID, si_prototype);
let ctor_shape2 = sh.make_shape(ctor_shape1, si_name);
let ctor_shape3 = sh.make_shape(ctor_shape2, si_length);
ctor.set_shape_id(ctor_shape3);
ctor.ensure_hash_props().push(JsValue::from_js_object(proto_ptr));
ctor.ensure_hash_props().push(JsValue::perm_string(sf.string_ptr(name_si)));
ctor.ensure_hash_props().push(JsValue::int(arg_count as i32));
// 构造器 prototype 槽按规范 { writable:false, enumerable:false, configurable:false }。
ctor.set_data_meta(0u32, PropAttributes::new(false, false, false));
// 构造器 name 槽按规范 { writable:false, enumerable:false, configurable:true }。
ctor.set_data_meta(1u32, PropAttributes::new(false, false, true));
// 构造器 length 按规范为不可写不可枚举(Function.length 属性描述符约定)。
ctor.set_data_meta(2u32, PropAttributes::new(false, false, true));
let ctor_ptr = Box::into_raw(ctor);
// 登记进 world 释放表(带复用键):session 收尾统一释放构造器本体与属性区。
world.track_fn_wrapper(ctor_ptr, reuse_key);
ctor_ptr
}
};
let ctor_val = JsValue::from_js_object(ctor_ptr);
// 原型上的 constructor 既有槽原位更新,无槽时开新槽(重建原型无既有槽);
// 非枚举数据属性(与 Error.prototype.constructor 一致)。
let proto = unsafe { &mut *proto_ptr };
let proto_ctor_shape = sh.make_shape(proto.shape_id(), sf.intern("constructor").0);
proto.set_shape_id(proto_ctor_shape);
proto.ensure_hash_props().push(JsValue::from_js_object(ctor_ptr));
// 原型上的 constructor 按规范为非枚举数据属性(与 Error.prototype.constructor 一致)。
let ctor_pos = proto.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
proto.set_data_meta(ctor_pos, PropAttributes::new(true, false, true));
let si_constructor = sf.intern("constructor").0;
if let Some(pos) = sh.lookup_position(proto.shape_id(), si_constructor) {
proto.set_prop_at(pos, ctor_val);
} else {
let proto_ctor_shape = sh.make_shape(proto.shape_id(), si_constructor);
proto.set_shape_id(proto_ctor_shape);
proto.ensure_hash_props().push(ctor_val);
let ctor_pos = proto.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
proto.set_data_meta(ctor_pos, PropAttributes::new(true, false, true));
}
let global_shape = sh.make_shape(global.shape_id(), name_si);
global.set_shape_id(global_shape);
global.ensure_hash_props().push(JsValue::from_js_object(ctor_ptr));
// 全局子类型构造器槽位非枚举(规范全局构造器描述符约定)。
let global_pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(global_pos, PropAttributes::new(true, false, true));
global.bump_generation();
// 全局子类型构造器槽位既有槽原位更新(旧家族构造器指针不得滞留在属性 vec),
// 无槽时开新槽;描述符非枚举(规范全局构造器描述符约定)。
if let Some(pos) = sh.lookup_position(global.shape_id(), name_si) {
global.set_prop_at(pos, ctor_val);
} else {
let global_shape = sh.make_shape(global.shape_id(), name_si);
global.set_shape_id(global_shape);
global.ensure_hash_props().push(ctor_val);
let global_pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(global_pos, PropAttributes::new(true, false, true));
global.bump_generation();
}
}
/// 把 Error 及各子类型(TypeError/ReferenceError/...)构造器与原型方法绑定到 global。
@@ -83,15 +114,20 @@ pub fn bind_error(core: &Arc<KernelCore>, session: &KernelSession, global: &mut
core.shape_forge().as_ref(),
);
// 全局 Error 槽位既有槽原位更新(旧家族构造器指针不得滞留在属性 vec),
// 无槽时开新槽;描述符非枚举(规范 { writable:true, enumerable:false, configurable:true })。
let si_err = core.perm_interner().intern("Error").0;
let err_shape = core.shape_forge().make_shape(global.shape_id(), si_err);
let err_val = JsValue::from_js_object(session.builtin_world().error_constructor.as_ptr() as *mut JsObject);
global.set_shape_id(err_shape);
global.ensure_hash_props().push(err_val);
// 全局 Error 构造器槽位非枚举(规范 { writable:true, enumerable:false, configurable:true })。
let err_pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(err_pos, PropAttributes::new(true, false, true));
global.bump_generation();
if let Some(pos) = core.shape_forge().lookup_position(global.shape_id(), si_err) {
global.set_prop_at(pos, err_val);
} else {
let err_shape = core.shape_forge().make_shape(global.shape_id(), si_err);
global.set_shape_id(err_shape);
global.ensure_hash_props().push(err_val);
let err_pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(err_pos, PropAttributes::new(true, false, true));
global.bump_generation();
}
bind_error_subtype_constructor(
core,
+12 -7
View File
@@ -77,13 +77,18 @@ pub fn bind_math(core: &Arc<KernelCore>, session: &KernelSession, global: &mut J
math.set_data_meta(pos, PropAttributes::new(false, false, false));
}
// 全局 Math 槽位既有槽原位更新(旧对象指针不得滞留在属性 vec),无槽时
// 开新槽;描述符非枚举(规范全局命名空间对象属性均 enumerable:false)。
let si_m = core.perm_interner().intern("Math").0;
let m_shape = core.shape_forge().make_shape(global.shape_id(), si_m);
let m_val = JsValue::from_js_object(session.builtin_world().math_object.as_ptr() as *mut JsObject);
global.set_shape_id(m_shape);
global.ensure_hash_props().push(m_val);
// 全局 Math 槽位非枚举(规范全局命名空间对象属性均 enumerable:false)。
let pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(pos, PropAttributes::new(true, false, true));
global.bump_generation();
if let Some(pos) = core.shape_forge().lookup_position(global.shape_id(), si_m) {
global.set_prop_at(pos, m_val);
} else {
let m_shape = core.shape_forge().make_shape(global.shape_id(), si_m);
global.set_shape_id(m_shape);
global.ensure_hash_props().push(m_val);
let pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(pos, PropAttributes::new(true, false, true));
global.bump_generation();
}
}
+12 -7
View File
@@ -73,13 +73,18 @@ pub fn bind_string(core: &Arc<KernelCore>, session: &KernelSession, global: &mut
0,
);
// 全局 String 槽位既有槽原位更新(旧家族构造器指针不得滞留在属性 vec),
// 无槽时开新槽;描述符非枚举(规范 { writable:true, enumerable:false, configurable:true })。
let si_str = core.perm_interner().intern("String").0;
let str_shape = core.shape_forge().make_shape(global.shape_id(), si_str);
let str_val = JsValue::from_js_object(session.builtin_world().string_constructor.as_ptr() as *mut JsObject);
global.set_shape_id(str_shape);
global.ensure_hash_props().push(str_val);
// 全局构造器槽位非枚举(规范 { writable:true, enumerable:false, configurable:true })。
let pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(pos, PropAttributes::new(true, false, true));
global.bump_generation();
if let Some(pos) = core.shape_forge().lookup_position(global.shape_id(), si_str) {
global.set_prop_at(pos, str_val);
} else {
let str_shape = core.shape_forge().make_shape(global.shape_id(), si_str);
global.set_shape_id(str_shape);
global.ensure_hash_props().push(str_val);
let pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(pos, PropAttributes::new(true, false, true));
global.bump_generation();
}
}
+143 -100
View File
@@ -71,19 +71,26 @@ macro_rules! bind_constructor {
}};
($core:expr, $global:expr, $name:literal, $ctor_ptr:expr, $ctor_fn:path, $nargs:literal, hash: $hash:literal) => {{
let si = $core.perm_interner().intern($name).0;
let shape = $core.shape_forge().make_shape($global.shape_id(), si);
let val = $crate::JsValue::from_js_object($ctor_ptr);
$global.set_shape_id(shape);
if $hash {
$global.ensure_hash_props().push(val);
$global.bump_generation();
if let Some(pos) = $core.shape_forge().lookup_position($global.shape_id(), si) {
// 既有槽(重建后保留的 global):原位更新槽值,不追加新槽——旧家族
// 构造器指针不得滞留属性 vec,shape 链不得继续增长。
$global.set_prop_at(pos, val);
} else {
$global.push_prop(val);
let shape = $core.shape_forge().make_shape($global.shape_id(), si);
$global.set_shape_id(shape);
if $hash {
$global.ensure_hash_props().push(val);
} else {
$global.push_prop(val);
}
// 全局构造器槽位:规范描述符 { writable:true, enumerable:false,
// configurable:true },不设 meta 时默认全枚举会泄漏进
// Object.keys(globalThis) / for-in。
let global_pos = $global.prop_vec_len().saturating_sub(1) as u32;
$global.set_data_meta(global_pos, oxide_types::object::PropAttributes::new(true, false, true));
$global.bump_generation();
}
// 全局构造器槽位:规范描述符 { writable:true, enumerable:false, configurable:true },
// 不设 meta 时默认全枚举会泄漏进 Object.keys(globalThis) / for-in。
let global_pos = $global.prop_vec_len().saturating_sub(1) as u32;
$global.set_data_meta(global_pos, oxide_types::object::PropAttributes::new(true, false, true));
let ctor = unsafe { &mut *$ctor_ptr };
let ptr: *const () = ($ctor_fn as fn(&mut $crate::vm::Vm, &[u8]) -> oxide_runtime_api::NativeResult) as *const ();
ctor.set_native_fn(Some(unsafe { oxide_types::object::NativeFnPtr::from_raw(ptr) }));
@@ -137,36 +144,48 @@ pub(crate) fn bind_accessor_getter(
let shape_forge = core.shape_forge().as_ref();
let string_forge = core.perm_interner().as_ref();
let world = session.builtin_world();
let fn_proto_val = JsValue::from_js_object(world.function_proto.as_ptr() as *mut JsObject);
let getter_name = format!("get {name}");
let mut getter = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, fn_proto_val));
getter.set_function(true);
// SAFETY: getter_fn 是转成 *const () 的 NativeFn 函数项指针。
getter.set_native_fn(Some(unsafe { oxide_types::object::NativeFnPtr::from_raw(getter_fn) }));
getter.set_native_arg_count(0);
let si_name = string_forge.intern("name").0;
let name_shape = shape_forge.make_shape(getter.shape_id(), si_name);
getter.set_shape_id(name_shape);
getter
.ensure_hash_props()
.push(JsValue::perm_string(string_forge.string_ptr(string_forge.intern(&getter_name).0)));
let name_pos = getter.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
getter.set_data_meta(name_pos, PropAttributes::new(false, false, true));
let si_length = string_forge.intern("length").0;
let length_shape = shape_forge.make_shape(getter.shape_id(), si_length);
getter.set_shape_id(length_shape);
getter.ensure_hash_props().push(JsValue::int(0));
let length_pos = getter.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
getter.set_data_meta(length_pos, PropAttributes::new(false, false, true));
let getter_ptr = Box::into_raw(getter);
world.track_leaked_object(getter_ptr);
let getter_val = JsValue::from_js_object(getter_ptr);
let si = string_forge.intern(name).0;
let getter_name = format!("get {name}");
let si_label = string_forge.intern(&getter_name).0;
// SAFETY: getter_fn 是转成 *const () 的 NativeFn 函数项指针。
let getter_fn_ptr = unsafe { oxide_types::object::NativeFnPtr::from_raw(getter_fn) };
// 选择性重建复用:同家族同槽旧 getter 迁移到新 proto 的访问器槽(proto
// 槽已由重建收尾重指),不再新建对象。
let reuse_key =
oxide_kernel::builtin::FnWrapperKey::new(world.wrapper_family_of(proto as *const JsObject), 0, si, si_label);
let getter_ptr = match world.find_fn_wrapper(reuse_key, getter_fn_ptr, 0) {
Some(ptr) => ptr,
None => {
let fn_proto_val = JsValue::from_js_object(world.function_proto.as_ptr() as *mut JsObject);
let mut getter = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, fn_proto_val));
getter.set_function(true);
getter.set_native_fn(Some(getter_fn_ptr));
getter.set_native_arg_count(0);
let si_name = string_forge.intern("name").0;
let name_shape = shape_forge.make_shape(getter.shape_id(), si_name);
getter.set_shape_id(name_shape);
getter
.ensure_hash_props()
.push(JsValue::perm_string(string_forge.string_ptr(si_label)));
let name_pos = getter.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
getter.set_data_meta(name_pos, PropAttributes::new(false, false, true));
let si_length = string_forge.intern("length").0;
let length_shape = shape_forge.make_shape(getter.shape_id(), si_length);
getter.set_shape_id(length_shape);
getter.ensure_hash_props().push(JsValue::int(0));
let length_pos = getter.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
getter.set_data_meta(length_pos, PropAttributes::new(false, false, true));
let getter_ptr = Box::into_raw(getter);
world.track_fn_wrapper(getter_ptr, reuse_key);
getter_ptr
}
};
let getter_val = JsValue::from_js_object(getter_ptr);
let new_shape = shape_forge.make_shape(proto.shape_id(), si);
proto.set_shape_id(new_shape);
let pos = proto.push_prop(JsValue::undefined());
@@ -193,51 +212,50 @@ pub(crate) fn bind_accessor_getset(
let shape_forge = core.shape_forge().as_ref();
let string_forge = core.perm_interner().as_ref();
let world = session.builtin_world();
let fn_proto_val = JsValue::from_js_object(world.function_proto.as_ptr() as *mut JsObject);
let mut getter = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, fn_proto_val));
getter.set_function(true);
// SAFETY: getter_fn 是转成 *const () 的 NativeFn 函数项指针。
getter.set_native_fn(Some(unsafe { oxide_types::object::NativeFnPtr::from_raw(getter_fn) }));
getter.set_native_arg_count(0);
// 给函数对象开 name/length 槽位(描述符均 { writable:false, enumerable:false, configurable:true })。
let si_name = string_forge.intern("name").0;
let name_shape = shape_forge.make_shape(getter.shape_id(), si_name);
getter.set_shape_id(name_shape);
getter
.ensure_hash_props()
.push(JsValue::perm_string(string_forge.string_ptr(string_forge.intern(getter_name).0)));
let name_pos = getter.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
getter.set_data_meta(name_pos, PropAttributes::new(false, false, true));
let family = world.wrapper_family_of(proto as *const JsObject);
let si_length = string_forge.intern("length").0;
let length_shape = shape_forge.make_shape(getter.shape_id(), si_length);
getter.set_shape_id(length_shape);
getter.ensure_hash_props().push(JsValue::int(0));
let length_pos = getter.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
getter.set_data_meta(length_pos, PropAttributes::new(false, false, true));
let getter_ptr = Box::into_raw(getter);
world.track_leaked_object(getter_ptr);
let getter_val = JsValue::from_js_object(getter_ptr);
let si_name = string_forge.intern("name").0;
let mut setter = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, fn_proto_val));
setter.set_function(true);
// SAFETY: setter_fn 是转成 *const () 的 NativeFn 函数项指针。
setter.set_native_fn(Some(unsafe { oxide_types::object::NativeFnPtr::from_raw(setter_fn) }));
setter.set_native_arg_count(1);
let name_shape = shape_forge.make_shape(setter.shape_id(), si_name);
setter.set_shape_id(name_shape);
setter
.ensure_hash_props()
.push(JsValue::perm_string(string_forge.string_ptr(string_forge.intern(setter_name).0)));
let name_pos = setter.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
setter.set_data_meta(name_pos, PropAttributes::new(false, false, true));
let length_shape = shape_forge.make_shape(setter.shape_id(), si_length);
setter.set_shape_id(length_shape);
setter.ensure_hash_props().push(JsValue::int(1));
let length_pos = setter.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
setter.set_data_meta(length_pos, PropAttributes::new(false, false, true));
let setter_ptr = Box::into_raw(setter);
world.track_leaked_object(setter_ptr);
// 选择性重建复用:同家族同槽旧 getter/setter 迁移到新 proto 的访问器槽
// (proto 槽已由重建收尾重指),不再新建对象。
let build = |si_label: u32,
fn_ptr_native: oxide_types::object::NativeFnPtr,
arg_count: u8,
length_val: i32|
-> *mut JsObject {
let reuse_key = oxide_kernel::builtin::FnWrapperKey::new(family, 0, key, si_label);
if let Some(ptr) = world.find_fn_wrapper(reuse_key, fn_ptr_native, arg_count) {
return ptr;
}
let fn_proto_val = JsValue::from_js_object(world.function_proto.as_ptr() as *mut JsObject);
let mut func = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, fn_proto_val));
func.set_function(true);
func.set_native_fn(Some(fn_ptr_native));
func.set_native_arg_count(arg_count);
// 给函数对象开 name/length 槽位(描述符均 { writable:false, enumerable:false, configurable:true })。
let name_shape = shape_forge.make_shape(func.shape_id(), si_name);
func.set_shape_id(name_shape);
func.ensure_hash_props()
.push(JsValue::perm_string(string_forge.string_ptr(si_label)));
let name_pos = func.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
func.set_data_meta(name_pos, PropAttributes::new(false, false, true));
let length_shape = shape_forge.make_shape(func.shape_id(), si_length);
func.set_shape_id(length_shape);
func.ensure_hash_props().push(JsValue::int(length_val));
let length_pos = func.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
func.set_data_meta(length_pos, PropAttributes::new(false, false, true));
let func_ptr = Box::into_raw(func);
world.track_fn_wrapper(func_ptr, reuse_key);
func_ptr
};
// SAFETY: getter_fn / setter_fn 是转成 *const () 的 NativeFn 函数项指针。
let getter_fn_ptr = unsafe { oxide_types::object::NativeFnPtr::from_raw(getter_fn) };
let setter_fn_ptr = unsafe { oxide_types::object::NativeFnPtr::from_raw(setter_fn) };
let si_getter = string_forge.intern(getter_name).0;
let si_setter = string_forge.intern(setter_name).0;
let getter_ptr = build(si_getter, getter_fn_ptr, 0, 0);
let setter_ptr = build(si_setter, setter_fn_ptr, 1, 1);
let getter_val = JsValue::from_js_object(getter_ptr);
let setter_val = JsValue::from_js_object(setter_ptr);
// 访问器属性槽:enumerable=false、configurable=true(get/set 函数对象已持有)。
@@ -573,13 +591,20 @@ fn sync_iterator_function_prototype(core: &Arc<KernelCore>, session: &KernelSess
iterator.set_prop_at(proto_pos, new_proto);
}
/// 在目标对象(global 或命名空间对象)上绑定一个具名槽:既有槽(选择性
/// 重建后保留的目标)原位更新槽值,无槽时开新槽。内置全局值统一非枚举:
/// 描述符 { writable:true, enumerable:false, configurable:true },与构造器/
/// 命名空间对象(Math/JSON/Reflect/Temporal 等)规范一致。
pub(crate) fn bind_global_value(core: &Arc<KernelCore>, global: &mut JsObject, name: &str, value: JsValue) {
let si = core.perm_interner().intern(name).0;
if let Some(pos) = core.shape_forge().lookup_position(global.shape_id(), si) {
// 原位更新:旧对象指针不得滞留在属性 vec,shape 链不得继续增长。
global.set_prop_at(pos, value);
return;
}
let shape = core.shape_forge().make_shape(global.shape_id(), si);
global.set_shape_id(shape);
global.ensure_hash_props().push(value);
// 内置全局值统一非枚举:描述符 { writable:true, enumerable:false, configurable:true },
// 与构造器/命名空间对象(Math/JSON/Reflect/Temporal 等)规范一致。
let pos = global.prop_vec_len().saturating_sub(1) as u32;
global.set_data_meta(pos, PropAttributes::new(true, false, true));
global.bump_generation();
@@ -604,28 +629,46 @@ fn bind_error_subtype_global(
// fallback 自建路径与 bind_error 的 Box 自建路径保持同一形态:[[Prototype]] 指向
// Error 构造器(NativeError 构造器继承 Error 构造器),prototype/name 描述符按规范。
let error_ctor_ptr = session.builtin_world().error_constructor.as_ptr() as *mut JsObject;
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(error_ctor_ptr)));
ctor.set_function(true);
configure_native_constructor(&mut ctor, ctor_fn, arg_count);
// 家族重建后新原型无既有槽:按复用键查找前轮旧构造器迁移,避免每轮新建。
let world = session.builtin_world();
let sf = core.perm_interner().as_ref();
let sh = core.shape_forge().as_ref();
let si_prototype = sf.intern("prototype").0;
let si_name = sf.intern("name").0;
let name_si = sf.intern(name).0;
let ctor_shape1 = sh.make_shape(EMPTY_SHAPE_ID, si_prototype);
let ctor_shape2 = sh.make_shape(ctor_shape1, si_name);
ctor.set_shape_id(ctor_shape2);
ctor.ensure_hash_props()
.push(JsValue::from_js_object(proto.as_ptr() as *mut JsObject));
ctor.ensure_hash_props().push(JsValue::perm_string(sf.string_ptr(name_si)));
// prototype/name 描述符与 Box 自建路径一致:{f,f,f} / {f,f,t}。
ctor.set_data_meta(0u32, PropAttributes::new(false, false, false));
ctor.set_data_meta(1u32, PropAttributes::new(false, false, true));
// SAFETY: ctor_fn 是转成 *const () 的 NativeFn 函数项指针。
let ctor_fn_ptr = unsafe { oxide_types::object::NativeFnPtr::from_raw(ctor_fn) };
let reuse_key = oxide_kernel::builtin::FnWrapperKey::new(0, 0, name_si, name_si);
let ctor_ptr = match world.find_fn_wrapper(reuse_key, ctor_fn_ptr, arg_count) {
Some(ptr) => {
// 迁移内引用:prototype 槽改指新子类型原型(旧原型已被重建替换)。
let ctor = unsafe { &mut *ptr };
if let Some(pos) = sh.lookup_position(ctor.shape_id(), si_prototype) {
ctor.set_prop_at(pos, JsValue::from_js_object(proto.as_ptr() as *mut JsObject));
}
ptr
}
None => {
let error_ctor_ptr = world.error_constructor.as_ptr() as *mut JsObject;
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(error_ctor_ptr)));
ctor.set_function(true);
configure_native_constructor(&mut ctor, ctor_fn, arg_count);
let ctor_ptr = Box::into_raw(ctor);
session.builtin_world().track_leaked_object(ctor_ptr);
let si_name = sf.intern("name").0;
let ctor_shape1 = sh.make_shape(EMPTY_SHAPE_ID, si_prototype);
let ctor_shape2 = sh.make_shape(ctor_shape1, si_name);
ctor.set_shape_id(ctor_shape2);
ctor.ensure_hash_props()
.push(JsValue::from_js_object(proto.as_ptr() as *mut JsObject));
ctor.ensure_hash_props().push(JsValue::perm_string(sf.string_ptr(name_si)));
// prototype/name 描述符与 Box 自建路径一致:{f,f,f} / {f,f,t}。
ctor.set_data_meta(0u32, PropAttributes::new(false, false, false));
ctor.set_data_meta(1u32, PropAttributes::new(false, false, true));
let ctor_ptr = Box::into_raw(ctor);
world.track_fn_wrapper(ctor_ptr, reuse_key);
ctor_ptr
}
};
bind_existing_global(core, global, name, JsValue::from_js_object(ctor_ptr));
}