mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
feat: PlainMonthDay/PlainYearMonth toLocaleString(默认形串)与 valueOf(TypeError)绑定
This commit is contained in:
@@ -7862,6 +7862,17 @@ pub fn plain_month_day_to_json<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResu
|
||||
plain_month_day_default_string(vm, args)
|
||||
}
|
||||
|
||||
/// `Temporal.PlainMonthDay.prototype.toLocaleString()`:locale/options 无引擎行为,
|
||||
/// 恒输出默认形串。
|
||||
pub fn plain_month_day_to_locale_string<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
||||
plain_month_day_default_string(vm, args)
|
||||
}
|
||||
|
||||
/// `Temporal.PlainMonthDay.prototype.valueOf()`:Temporal 对象无值表示,恒 TypeError。
|
||||
pub fn plain_month_day_value_of<H: VmHost>(vm: &mut H, _args: &[u8]) -> NativeResult {
|
||||
NativeResult::Err(crate::error::create_type_error(vm, "Temporal.PlainMonthDay has no valueOf"))
|
||||
}
|
||||
|
||||
/// PlainYearMonth 默认形串(`±YYYY-MM`),含 receiver 校验,不读 options。
|
||||
fn plain_year_month_default_string<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
||||
let ptr = native_try!(receiver_obj(vm, args));
|
||||
@@ -7900,6 +7911,17 @@ pub fn plain_year_month_to_json<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeRes
|
||||
plain_year_month_default_string(vm, args)
|
||||
}
|
||||
|
||||
/// `Temporal.PlainYearMonth.prototype.toLocaleString()`:locale/options 无引擎行为,
|
||||
/// 恒输出默认形串。
|
||||
pub fn plain_year_month_to_locale_string<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
||||
plain_year_month_default_string(vm, args)
|
||||
}
|
||||
|
||||
/// `Temporal.PlainYearMonth.prototype.valueOf()`:Temporal 对象无值表示,恒 TypeError。
|
||||
pub fn plain_year_month_value_of<H: VmHost>(vm: &mut H, _args: &[u8]) -> NativeResult {
|
||||
NativeResult::Err(crate::error::create_type_error(vm, "Temporal.PlainYearMonth has no valueOf"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -1005,6 +1005,16 @@ pub fn bind_temporal(core: &Arc<KernelCore>, session: &KernelSession, global: &m
|
||||
oxide_builtins::temporal::plain_month_day_to_json::<crate::vm::Vm> as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"toLocaleString",
|
||||
oxide_builtins::temporal::plain_month_day_to_locale_string::<crate::vm::Vm> as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"valueOf",
|
||||
oxide_builtins::temporal::plain_month_day_value_of::<crate::vm::Vm> as *const (),
|
||||
0,
|
||||
),
|
||||
],
|
||||
);
|
||||
bind_well_known_data_property(
|
||||
@@ -1088,6 +1098,16 @@ pub fn bind_temporal(core: &Arc<KernelCore>, session: &KernelSession, global: &m
|
||||
oxide_builtins::temporal::plain_year_month_to_json::<crate::vm::Vm> as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"toLocaleString",
|
||||
oxide_builtins::temporal::plain_year_month_to_locale_string::<crate::vm::Vm> as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"valueOf",
|
||||
oxide_builtins::temporal::plain_year_month_value_of::<crate::vm::Vm> as *const (),
|
||||
0,
|
||||
),
|
||||
],
|
||||
);
|
||||
bind_well_known_data_property(
|
||||
|
||||
@@ -2797,3 +2797,32 @@ fn plain_month_day_and_year_month_branding() {
|
||||
.unwrap();
|
||||
assert_eq!(str_val(&vm, r), "TypeError|TypeError|TypeError|TypeError");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plain_month_day_and_year_month_to_locale_string_value_of_to_string_tag() {
|
||||
let mut vm = Vm::new();
|
||||
// toLocaleString 忽略 locale/options,恒默认形串;valueOf 恒 TypeError
|
||||
// (`<` 走 ToPrimitive→valueOf 同抛);toStringTag 经 Object.prototype.toString 读回。
|
||||
let r = eval(
|
||||
&mut vm,
|
||||
"(() => {
|
||||
const pmd = new Temporal.PlainMonthDay(5, 2);
|
||||
const pym = new Temporal.PlainYearMonth(2021, 2);
|
||||
const kind = (fn) => { try { fn(); return 'no'; } catch (e) { return e.constructor.name; } };
|
||||
return [
|
||||
pmd.toLocaleString('en-US', {calendar: 'iso8601'}),
|
||||
pym.toLocaleString(),
|
||||
kind(() => pmd.valueOf()),
|
||||
kind(() => pym.valueOf()),
|
||||
kind(() => pmd < pym),
|
||||
Object.prototype.toString.call(pmd),
|
||||
Object.prototype.toString.call(pym),
|
||||
].join('|');
|
||||
})()",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
str_val(&vm, r),
|
||||
"05-02|2021-02|TypeError|TypeError|TypeError|[object Temporal.PlainMonthDay]|[object Temporal.PlainYearMonth]"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user