mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-25 03:01:12 +08:00
refactor: 符号属性键核心推导改经 realm 维度编码
This commit is contained in:
@@ -4,26 +4,19 @@
|
||||
use oxide_runtime_api as coercion;
|
||||
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::private_key::{
|
||||
int_key_value, is_int_key, make_int_key, make_symbol_key, make_well_known_symbol_key, INT_KEY_COUNT,
|
||||
WELL_KNOWN_SYMBOL_COUNT,
|
||||
};
|
||||
use oxide_types::private_key::{encode_symbol_key, int_key_value, is_int_key, make_int_key, INT_KEY_COUNT};
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
use super::{canonical_index_of, canonical_index_units, Vm, MAX_PROTO_CHAIN_DEPTH};
|
||||
use crate::vm_trace;
|
||||
|
||||
/// 把符号原语的下标编码为属性键:well-known 下标走保留键槽,用户下标加偏移,
|
||||
/// 两段键区间不重叠。
|
||||
fn symbol_value_key(idx: u32) -> u32 {
|
||||
if idx < WELL_KNOWN_SYMBOL_COUNT {
|
||||
make_well_known_symbol_key(idx)
|
||||
} else {
|
||||
make_symbol_key(idx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Vm {
|
||||
/// 把符号原语的局部下标编码为属性键:经 realm 感知编码(realm 编号占高位、
|
||||
/// 局部下标占低 20 位),realm 0 路径与旧编码逐字节一致。
|
||||
fn symbol_value_key(&self, idx: u32) -> u32 {
|
||||
encode_symbol_key(self.realm_id(), idx)
|
||||
}
|
||||
|
||||
/// 把 `JsValue` 转为属性键 si(字符串 intern id,`u32`)。
|
||||
///
|
||||
/// 非负小整数(含整值 double)直接编码到整数键区间,免 to_string + intern;
|
||||
@@ -55,16 +48,16 @@ impl Vm {
|
||||
return Ok(self.string_key_units(&s.units()));
|
||||
}
|
||||
// Symbol 值直接编码为 Symbol 键(不进字符串 interner,键相互独立);
|
||||
// well-known 下标占保留槽,用户符号下标须加偏移,两段键区间不重叠。
|
||||
// realm 编号占高位、局部下标占低 20 位,realm 0 与旧编码逐字节一致。
|
||||
if val.is_symbol() {
|
||||
return Ok(symbol_value_key(val.as_symbol_index()));
|
||||
return Ok(self.symbol_value_key(val.as_symbol_local_index()));
|
||||
}
|
||||
if val.is_object() {
|
||||
// ToPropertyKey:对象经 ToPrimitive(string hint),结果为 Symbol 时直接作键;
|
||||
// 其余字符串按单元序列推导键(避免 lossy 文本桥接破坏孤立 surrogate 键)。
|
||||
let prim = coercion::to_primitive(val, coercion::ToPrimitiveHint::String, self)?;
|
||||
if prim.is_symbol() {
|
||||
return Ok(symbol_value_key(prim.as_symbol_index()));
|
||||
return Ok(self.symbol_value_key(prim.as_symbol_local_index()));
|
||||
}
|
||||
let units = oxide_runtime_api::to_units_full(prim, self)?;
|
||||
return Ok(self.string_key_units(&units));
|
||||
|
||||
@@ -90,27 +90,25 @@ impl GcState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Symbol 的 intern 状态。
|
||||
/// 每 realm 一份的 Symbol intern 状态。
|
||||
///
|
||||
/// 符号下标 `0..WELL_KNOWN_SYMBOL_COUNT` 保留给 well-known symbol(描述取内建
|
||||
/// 名表),用户 `Symbol()`/`Symbol.for()` 的下标自此区间之后递增,
|
||||
/// `symbol_descriptions` 只存用户符号描述。
|
||||
/// 符号下标是 realm 内局部下标:`0..WELL_KNOWN_SYMBOL_COUNT` 保留给
|
||||
/// well-known symbol(描述取内建名表),用户 `Symbol()`/`Symbol.for()` 的
|
||||
/// 局部下标自此区间之后递增,`symbol_descriptions` 只存用户符号描述。
|
||||
/// 局部下标由描述槽数自然递增,每 realm 独立计数。
|
||||
pub(crate) struct SymbolState {
|
||||
pub(crate) symbol_counter: u32,
|
||||
pub(crate) symbol_descriptions: Vec<Option<String>>,
|
||||
pub(crate) symbol_registry: HashMap<String, u32>,
|
||||
}
|
||||
|
||||
impl SymbolState {
|
||||
pub(crate) fn reset(&mut self) {
|
||||
self.symbol_counter = 0;
|
||||
self.symbol_descriptions.clear();
|
||||
self.symbol_registry.clear();
|
||||
}
|
||||
|
||||
pub(crate) fn intern(&mut self, description: Option<String>) -> u32 {
|
||||
self.symbol_counter = self.symbol_counter.wrapping_add(1);
|
||||
// 用户符号下标顺延到 well-known 保留区间之后,与 well-known 下标空间不重叠。
|
||||
// 用户符号局部下标顺延到 well-known 保留区间之后,与 well-known 下标空间不重叠。
|
||||
let idx = WELL_KNOWN_SYMBOL_COUNT + self.symbol_descriptions.len() as u32;
|
||||
self.symbol_descriptions.push(description);
|
||||
idx
|
||||
|
||||
@@ -96,7 +96,6 @@ impl Vm {
|
||||
gc_pressure_mode: gc_pressure_mode_from_env(),
|
||||
}),
|
||||
symbols: RefCell::new(SymbolState {
|
||||
symbol_counter: 0,
|
||||
symbol_descriptions: Vec::new(),
|
||||
symbol_registry: std::collections::HashMap::new(),
|
||||
}),
|
||||
@@ -246,7 +245,6 @@ impl Vm {
|
||||
gc_pressure_mode: gc_pressure_mode_from_env(),
|
||||
}),
|
||||
symbols: RefCell::new(SymbolState {
|
||||
symbol_counter: 0,
|
||||
symbol_descriptions: Vec::new(),
|
||||
symbol_registry: std::collections::HashMap::new(),
|
||||
}),
|
||||
|
||||
@@ -1112,7 +1112,6 @@ fn full_reset_clears_symbol_state() {
|
||||
|
||||
vm.full_reset();
|
||||
|
||||
assert_eq!(vm.realm.symbols.borrow().symbol_counter, 0);
|
||||
assert!(vm.realm.symbols.borrow().symbol_descriptions.is_empty());
|
||||
assert!(vm.realm.symbols.borrow().symbol_registry.is_empty());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user