fix: symbol 作属性键独立编码,不再塌缩为空串
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor;
|
||||
use crate::bindings::{apply_binding_table, bind_accessor_getter, bind_method_alias, configure_native_constructor};
|
||||
use crate::bindings::{
|
||||
apply_binding_table, bind_accessor_getter, bind_well_known_method_alias, configure_native_constructor,
|
||||
};
|
||||
use oxide_kernel::kernel::{KernelCore, KernelSession};
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
@@ -32,8 +34,8 @@ pub fn bind_map(core: &Arc<KernelCore>, session: &KernelSession, global: &mut Js
|
||||
],
|
||||
);
|
||||
|
||||
// Map 的 @@iterator 是 entries 的同一函数对象。
|
||||
bind_method_alias(core, proto, "entries", "@@iterator");
|
||||
// Map 的 @@iterator(Symbol.iterator)是 entries 的同一函数对象。
|
||||
bind_well_known_method_alias(core, proto, "entries", 0);
|
||||
bind_accessor_getter(core, session, proto, "size", oxide_builtins::map::map_size::<crate::vm::Vm> as *const ());
|
||||
|
||||
bind_constructor!(core, global, "Map", ctor_ptr, oxide_builtins::map::map_constructor::<crate::vm::Vm>, 1, hash: true);
|
||||
|
||||
@@ -24,6 +24,7 @@ pub fn bind_object(core: &Arc<KernelCore>, session: &KernelSession, global: &mut
|
||||
is_sealed: oxide_builtins::object::object_is_sealed::<crate::vm::Vm> as *const (),
|
||||
is_extensible: oxide_builtins::object::object_is_extensible::<crate::vm::Vm> as *const (),
|
||||
get_own_property_names: oxide_builtins::object::object_get_own_property_names::<crate::vm::Vm> as *const (),
|
||||
get_own_property_symbols: oxide_builtins::object::object_get_own_property_symbols::<crate::vm::Vm> as *const (),
|
||||
define_properties: oxide_builtins::object::object_define_properties::<crate::vm::Vm> as *const (),
|
||||
from_entries: oxide_builtins::object::object_from_entries::<crate::vm::Vm> as *const (),
|
||||
get_prototype_of: oxide_builtins::object::object_get_prototype_of::<crate::vm::Vm> as *const (),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bindings::{apply_binding_table, configure_native_constructor};
|
||||
use crate::bindings::{apply_binding_table, bind_well_known_method, configure_native_constructor};
|
||||
use oxide_kernel::kernel::{KernelCore, KernelSession};
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
@@ -23,21 +23,56 @@ pub fn bind_regexp(core: &Arc<KernelCore>, session: &KernelSession, global: &mut
|
||||
("exec", oxide_builtins::regexp::regexp_exec::<crate::vm::Vm> as *const (), 1),
|
||||
("test", oxide_builtins::regexp::regexp_test::<crate::vm::Vm> as *const (), 1),
|
||||
("toString", oxide_builtins::regexp::regexp_to_string::<crate::vm::Vm> as *const (), 0),
|
||||
("@@match", oxide_builtins::regexp::regexp_symbol_match::<crate::vm::Vm> as *const (), 1),
|
||||
(
|
||||
"@@replace",
|
||||
oxide_builtins::regexp::regexp_symbol_replace::<crate::vm::Vm> as *const (),
|
||||
2,
|
||||
),
|
||||
("@@search", oxide_builtins::regexp::regexp_symbol_search::<crate::vm::Vm> as *const (), 1),
|
||||
("@@split", oxide_builtins::regexp::regexp_symbol_split::<crate::vm::Vm> as *const (), 2),
|
||||
(
|
||||
"@@matchAll",
|
||||
oxide_builtins::regexp::regexp_symbol_match_all::<crate::vm::Vm> as *const (),
|
||||
1,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
// Symbol.match 等 well-known symbol 方法按 Symbol 键安装,供 `re[Symbol.match]` 等读取。
|
||||
let world = session.builtin_world();
|
||||
bind_well_known_method(
|
||||
world,
|
||||
core,
|
||||
proto,
|
||||
1,
|
||||
"@@match",
|
||||
oxide_builtins::regexp::regexp_symbol_match::<crate::vm::Vm> as *const (),
|
||||
1,
|
||||
);
|
||||
bind_well_known_method(
|
||||
world,
|
||||
core,
|
||||
proto,
|
||||
2,
|
||||
"@@replace",
|
||||
oxide_builtins::regexp::regexp_symbol_replace::<crate::vm::Vm> as *const (),
|
||||
2,
|
||||
);
|
||||
bind_well_known_method(
|
||||
world,
|
||||
core,
|
||||
proto,
|
||||
3,
|
||||
"@@search",
|
||||
oxide_builtins::regexp::regexp_symbol_search::<crate::vm::Vm> as *const (),
|
||||
1,
|
||||
);
|
||||
bind_well_known_method(
|
||||
world,
|
||||
core,
|
||||
proto,
|
||||
4,
|
||||
"@@split",
|
||||
oxide_builtins::regexp::regexp_symbol_split::<crate::vm::Vm> as *const (),
|
||||
2,
|
||||
);
|
||||
bind_well_known_method(
|
||||
world,
|
||||
core,
|
||||
proto,
|
||||
7,
|
||||
"@@matchAll",
|
||||
oxide_builtins::regexp::regexp_symbol_match_all::<crate::vm::Vm> as *const (),
|
||||
1,
|
||||
);
|
||||
|
||||
bind_constructor!(core, global, "RegExp", ctor_ptr, oxide_builtins::regexp::regexp_constructor::<crate::vm::Vm>, 2, hash: true);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor;
|
||||
use crate::bindings::{apply_binding_table, bind_accessor_getter, bind_method_alias, configure_native_constructor};
|
||||
use crate::bindings::{
|
||||
apply_binding_table, bind_accessor_getter, bind_method_alias, bind_well_known_method_alias,
|
||||
configure_native_constructor,
|
||||
};
|
||||
use oxide_kernel::kernel::{KernelCore, KernelSession};
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
@@ -45,9 +48,9 @@ pub fn bind_set(core: &Arc<KernelCore>, session: &KernelSession, global: &mut Js
|
||||
],
|
||||
);
|
||||
|
||||
// keys 与 @@iterator 是 values 的同一函数对象。
|
||||
// keys 与 @@iterator(Symbol.iterator)是 values 的同一函数对象。
|
||||
bind_method_alias(core, proto, "values", "keys");
|
||||
bind_method_alias(core, proto, "values", "@@iterator");
|
||||
bind_well_known_method_alias(core, proto, "values", 0);
|
||||
bind_accessor_getter(core, session, proto, "size", oxide_builtins::set::set_size::<crate::vm::Vm> as *const ());
|
||||
|
||||
bind_constructor!(core, global, "Set", ctor_ptr, oxide_builtins::set::set_constructor::<crate::vm::Vm>, 1, hash: true);
|
||||
|
||||
@@ -2,7 +2,8 @@ use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor;
|
||||
use crate::bindings::{
|
||||
apply_binding_table, bind_accessor_getter, bind_global_value, bind_method_alias, configure_native_constructor,
|
||||
apply_binding_table, bind_accessor_getter, bind_global_value, bind_method_alias, bind_well_known_method_alias,
|
||||
configure_native_constructor,
|
||||
};
|
||||
use oxide_kernel::kernel::{KernelCore, KernelSession};
|
||||
use oxide_types::object::{JsObject, PropAttributes};
|
||||
@@ -195,7 +196,8 @@ pub fn bind_typed_array(core: &Arc<KernelCore>, session: &KernelSession, global:
|
||||
("with", oxide_builtins::typed_array::typed_array_with::<crate::vm::Vm> as *const (), 2),
|
||||
],
|
||||
);
|
||||
bind_method_alias(core, shared_proto, "values", "@@iterator");
|
||||
bind_method_alias(core, shared_proto, "values", "keys");
|
||||
bind_well_known_method_alias(core, shared_proto, "values", 0);
|
||||
|
||||
// 原型访问器:视图属性(buffer/byteOffset/byteLength/length)读内部数据槽,
|
||||
// @@toStringTag 返回具体类型名,供 Object.prototype.toString 区分类型。
|
||||
|
||||
@@ -176,6 +176,48 @@ pub(crate) fn bind_method_alias(core: &Arc<KernelCore>, proto: &mut JsObject, so
|
||||
proto.bump_generation();
|
||||
}
|
||||
|
||||
/// 在原型上按 well-known symbol 键绑定方法(键不字符串 intern,读键经
|
||||
/// `property_key_si` 的 well-known 分支映射到同一键)。
|
||||
pub(crate) fn bind_well_known_method(
|
||||
world: &BuiltinWorld, core: &Arc<KernelCore>, target: &mut JsObject, well_known_id: u32, method_name: &str,
|
||||
func: *const (), nargs: u8,
|
||||
) {
|
||||
let shape_forge = core.shape_forge().as_ref();
|
||||
let string_forge = core.perm_interner().as_ref();
|
||||
// SAFETY: func 是转成 *const () 的 NativeFn 函数项指针。
|
||||
let fn_ptr = unsafe { oxide_types::object::NativeFnPtr::from_raw(func) };
|
||||
let key = oxide_types::private_key::make_well_known_symbol_key(well_known_id);
|
||||
let _ = oxide_kernel::builtin::BuiltinWorld::bind_method_key_static(
|
||||
target,
|
||||
shape_forge,
|
||||
string_forge,
|
||||
key,
|
||||
method_name,
|
||||
fn_ptr,
|
||||
nargs,
|
||||
world.fn_proto_val(),
|
||||
);
|
||||
}
|
||||
|
||||
/// 把原型上 `source` 属性已绑定的函数值复制到 well-known symbol 键名下(共享同一函数对象)。
|
||||
pub(crate) fn bind_well_known_method_alias(
|
||||
core: &Arc<KernelCore>, proto: &mut JsObject, source: &str, well_known_id: u32,
|
||||
) {
|
||||
let shape_forge = core.shape_forge().as_ref();
|
||||
let string_forge = core.perm_interner().as_ref();
|
||||
let src_si = string_forge.intern(source).0;
|
||||
let Some(pos) = shape_forge.lookup_position(proto.shape_id(), src_si) else {
|
||||
return;
|
||||
};
|
||||
let value = proto.get_prop_at(pos);
|
||||
let key = oxide_types::private_key::make_well_known_symbol_key(well_known_id);
|
||||
let new_shape = shape_forge.make_shape(proto.shape_id(), key);
|
||||
proto.set_shape_id(new_shape);
|
||||
let alias_pos = proto.push_prop(value);
|
||||
proto.set_data_meta(alias_pos, PropAttributes::new(true, false, true));
|
||||
proto.bump_generation();
|
||||
}
|
||||
|
||||
pub(crate) fn bind_global_value(core: &Arc<KernelCore>, global: &mut JsObject, name: &str, value: JsValue) {
|
||||
let si = core.perm_interner().intern(name).0;
|
||||
let shape = core.shape_forge().make_shape(global.shape_id(), si);
|
||||
|
||||
Reference in New Issue
Block a user