mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-25 03:01:12 +08:00
feat: String.raw/groupBy/withResolvers/f16round/__defineGetter__ 低垂果实批量实现
This commit is contained in:
@@ -38,5 +38,13 @@ pub fn bind_map(core: &Arc<KernelCore>, session: &KernelSession, global: &mut Js
|
||||
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 ());
|
||||
|
||||
// Map.groupBy 静态方法。
|
||||
apply_binding_table(
|
||||
session.builtin_world(),
|
||||
ctor,
|
||||
core,
|
||||
&[("groupBy", oxide_builtins::map::map_group_by::<crate::vm::Vm> as *const (), 2)],
|
||||
);
|
||||
|
||||
bind_constructor!(core, global, "Map", ctor_ptr, oxide_builtins::map::map_constructor::<crate::vm::Vm>, 1, hash: true);
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ pub fn bind_math(core: &Arc<KernelCore>, session: &KernelSession, global: &mut J
|
||||
("atanh", oxide_builtins::math::math_atanh::<crate::vm::Vm>, 1),
|
||||
("clz32", oxide_builtins::math::math_clz32::<crate::vm::Vm>, 1),
|
||||
("expm1", oxide_builtins::math::math_expm1::<crate::vm::Vm>, 1),
|
||||
("f16round", oxide_builtins::math::math_f16round::<crate::vm::Vm>, 1),
|
||||
("fround", oxide_builtins::math::math_fround::<crate::vm::Vm>, 1),
|
||||
("log1p", oxide_builtins::math::math_log1p::<crate::vm::Vm>, 1),
|
||||
);
|
||||
|
||||
@@ -34,6 +34,7 @@ pub fn bind_object(core: &Arc<KernelCore>, session: &KernelSession, global: &mut
|
||||
has_own_property: oxide_builtins::object::object_proto_has_own_property::<crate::vm::Vm> as *const (),
|
||||
property_is_enumerable: oxide_builtins::object::object_proto_property_is_enumerable::<crate::vm::Vm>
|
||||
as *const (),
|
||||
group_by: oxide_builtins::object::object_group_by::<crate::vm::Vm> as *const (),
|
||||
};
|
||||
session
|
||||
.builtin_world()
|
||||
@@ -52,10 +53,15 @@ pub fn bind_object(core: &Arc<KernelCore>, session: &KernelSession, global: &mut
|
||||
0,
|
||||
),
|
||||
("valueOf", oxide_builtins::object::object_proto_value_of::<crate::vm::Vm> as *const (), 0),
|
||||
(
|
||||
"__defineGetter__",
|
||||
oxide_builtins::object::object_proto_define_getter::<crate::vm::Vm> as *const (),
|
||||
2,
|
||||
),
|
||||
],
|
||||
);
|
||||
// 内置原型方法不可枚举(否则会泄漏进 for-in)。
|
||||
for name in ["toString", "valueOf"] {
|
||||
for name in ["toString", "valueOf", "__defineGetter__"] {
|
||||
let si = core.perm_interner().intern(name).0;
|
||||
if let Some(pos) = core.shape_forge().lookup_position(object_proto.shape_id(), si) {
|
||||
object_proto.set_data_meta(pos, PropAttributes::new(true, false, true));
|
||||
|
||||
@@ -42,6 +42,7 @@ pub fn bind_string(core: &Arc<KernelCore>, session: &KernelSession, global: &mut
|
||||
from_code_point: oxide_builtins::string::string_from_code_point::<crate::vm::Vm> as *const (),
|
||||
is_well_formed: oxide_builtins::string::string_is_well_formed::<crate::vm::Vm> as *const (),
|
||||
to_well_formed: oxide_builtins::string::string_to_well_formed::<crate::vm::Vm> as *const (),
|
||||
from_raw: oxide_builtins::string::string_raw::<crate::vm::Vm> as *const (),
|
||||
};
|
||||
session.builtin_world().bind_string_methods(
|
||||
&string_methods,
|
||||
|
||||
@@ -764,6 +764,7 @@ impl Vm {
|
||||
("race", promise_static_race as *const (), 1),
|
||||
("allSettled", promise_static_all_settled as *const (), 1),
|
||||
("any", promise_static_any as *const (), 1),
|
||||
("withResolvers", promise_static_with_resolvers as *const (), 0),
|
||||
);
|
||||
|
||||
// 固定地址后互相接线:proto.constructor ↔ ctor.prototype。
|
||||
@@ -1284,6 +1285,37 @@ fn promise_static_reject(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
}
|
||||
|
||||
/// `Promise.withResolvers()`:按 `this` 构造器建能力(非构造器抛 TypeError),
|
||||
/// 返回 `{promise, resolve, reject}` 普通对象(proto 为 `%Object.prototype%`)。
|
||||
///
|
||||
/// # 步骤
|
||||
/// 1. 取 `this` 为构造器 C,经 `new_promise_capability_with_ctor` 建能力
|
||||
/// 2. 创建普通对象,依次写入 promise / resolve / reject 三个数据属性
|
||||
///
|
||||
/// # 边界与前提
|
||||
/// - C 非构造器(普通值 / arrow / 非构造 native)时抛 TypeError
|
||||
/// - 返回值属性为默认数据描述符(writable/enumerable/configurable 均 true)
|
||||
fn promise_static_with_resolvers(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let ctor = vm.reg(if args.is_empty() { 0 } else { args[0] });
|
||||
let (promise, resolve, reject) = match vm.new_promise_capability_with_ctor(ctor) {
|
||||
Ok(t) => t,
|
||||
Err(err) => return NativeResult::Err(err),
|
||||
};
|
||||
let object_proto =
|
||||
JsValue::from_js_object(vm.session.builtin_world().object_proto.as_ptr() as *mut JsObject);
|
||||
let ptr = vm.alloc_object(JsObject::new_empty(EMPTY_SHAPE_ID, object_proto));
|
||||
let obj = unsafe { &mut *ptr };
|
||||
let sf = vm.kernel_core.perm_interner().as_ref();
|
||||
let sh = vm.kernel_core.shape_forge().as_ref();
|
||||
for (name, val) in [("promise", promise), ("resolve", resolve), ("reject", reject)] {
|
||||
let si = sf.intern(name).0;
|
||||
let shape = sh.make_shape(obj.shape_id(), si);
|
||||
obj.set_shape_id(shape);
|
||||
obj.push_prop(val);
|
||||
}
|
||||
NativeResult::Ok(JsValue::from_js_object(ptr))
|
||||
}
|
||||
|
||||
/// `Promise.all(iterable)`:全部元素结算后以结果数组完成,任一拒绝则拒绝。
|
||||
fn promise_static_all(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let ctor = vm.reg(if args.is_empty() { 0 } else { args[0] });
|
||||
|
||||
@@ -2161,6 +2161,11 @@ impl oxide_runtime_api::VmHost for Vm {
|
||||
self.property_key_si(val)
|
||||
.unwrap_or_else(|_| self.kernel_core.perm_interner().intern("").0)
|
||||
}
|
||||
fn to_property_key_si(&mut self, val: JsValue) -> Result<u32, String> {
|
||||
// ToPropertyKey 完整路径:转换异常(对象 ToPrimitive 抛错 / Symbol 处理)
|
||||
// 原样返回,供需传播异常的 builtins(groupBy / __defineGetter__ 等)使用。
|
||||
self.property_key_si(val)
|
||||
}
|
||||
fn string_key_si(&mut self, s: &str) -> u32 {
|
||||
if let Some(i) = canonical_index_of(s) {
|
||||
make_int_key(i)
|
||||
|
||||
Reference in New Issue
Block a user