mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-27 12:11:12 +08:00
style: 自动格式化
This commit is contained in:
@@ -10,9 +10,7 @@ use oxide_runtime_api::{NativeResult, ProtoKind, ToPrimitiveHint, VmHost};
|
|||||||
const WEEKDAY_NAMES: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
const WEEKDAY_NAMES: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||||
|
|
||||||
/// 月名(1-based 下标)。
|
/// 月名(1-based 下标)。
|
||||||
const MONTH_NAMES: [&str; 12] = [
|
const MONTH_NAMES: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
|
||||||
];
|
|
||||||
|
|
||||||
/// 4 位年格式化:非负无符号零填充(年 0 写作 `0000`),负年 `-` 前缀加 4 位。
|
/// 4 位年格式化:非负无符号零填充(年 0 写作 `0000`),负年 `-` 前缀加 4 位。
|
||||||
fn format_year_4(y: i64) -> String {
|
fn format_year_4(y: i64) -> String {
|
||||||
@@ -550,11 +548,7 @@ fn parse_iso_strict(s: &str) -> Option<f64> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 组合:naive 时刻按解释方式折算 UTC。
|
// 组合:naive 时刻按解释方式折算 UTC。
|
||||||
let naive = civil_day_count(y, m, d) * MS_PER_DAY
|
let naive = civil_day_count(y, m, d) * MS_PER_DAY + h * 3_600_000 + mi * 60_000 + sec * 1_000 + ms;
|
||||||
+ h * 3_600_000
|
|
||||||
+ mi * 60_000
|
|
||||||
+ sec * 1_000
|
|
||||||
+ ms;
|
|
||||||
let ts = match offset_min {
|
let ts = match offset_min {
|
||||||
Some(off) => naive - off * 60_000,
|
Some(off) => naive - off * 60_000,
|
||||||
None => local_offset_sample(naive)?,
|
None => local_offset_sample(naive)?,
|
||||||
@@ -634,11 +628,7 @@ fn parse_engine_date_string(s: &str) -> Option<f64> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_signed_year(s: &str) -> Option<i64> {
|
fn parse_signed_year(s: &str) -> Option<i64> {
|
||||||
let (neg, rest) = if let Some(r) = s.strip_prefix('-') {
|
let (neg, rest) = if let Some(r) = s.strip_prefix('-') { (true, r) } else { (false, s) };
|
||||||
(true, r)
|
|
||||||
} else {
|
|
||||||
(false, s)
|
|
||||||
};
|
|
||||||
if rest.is_empty() || !rest.bytes().all(|c| c.is_ascii_digit()) {
|
if rest.is_empty() || !rest.bytes().all(|c| c.is_ascii_digit()) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@@ -654,7 +644,11 @@ fn parse_hms(s: &str) -> Option<(i64, i64, i64)> {
|
|||||||
if parts.len() != 3 {
|
if parts.len() != 3 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some((two_digits(parts[0].as_bytes())?, two_digits(parts[1].as_bytes())?, two_digits(parts[2].as_bytes())?))
|
Some((
|
||||||
|
two_digits(parts[0].as_bytes())?,
|
||||||
|
two_digits(parts[1].as_bytes())?,
|
||||||
|
two_digits(parts[2].as_bytes())?,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 日期字符串解析总入口:先严格 ISO,失败后按序回退实现自定义格式
|
/// 日期字符串解析总入口:先严格 ISO,失败后按序回退实现自定义格式
|
||||||
@@ -772,11 +766,7 @@ pub fn date_constructor<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
|||||||
parse_date_string(&s)
|
parse_date_string(&s)
|
||||||
} else if val.is_int() || val.is_double() {
|
} else if val.is_int() || val.is_double() {
|
||||||
// 数值参数先截断再 TimeClip(Date 值为整毫秒)。
|
// 数值参数先截断再 TimeClip(Date 值为整毫秒)。
|
||||||
let n = if val.is_int() {
|
let n = if val.is_int() { val.as_int() as f64 } else { val.as_double() };
|
||||||
val.as_int() as f64
|
|
||||||
} else {
|
|
||||||
val.as_double()
|
|
||||||
};
|
|
||||||
time_clip(n.trunc())
|
time_clip(n.trunc())
|
||||||
} else if val.is_object() && {
|
} else if val.is_object() && {
|
||||||
let ptr = val.as_js_object_ptr();
|
let ptr = val.as_js_object_ptr();
|
||||||
@@ -812,7 +802,10 @@ pub fn date_constructor<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
|||||||
if let Some(exc) = vm.take_uncaught_value() {
|
if let Some(exc) = vm.take_uncaught_value() {
|
||||||
return NativeResult::Err(exc);
|
return NativeResult::Err(exc);
|
||||||
}
|
}
|
||||||
return NativeResult::Err(crate::error::create_type_error(vm, "Cannot convert value to a number"));
|
return NativeResult::Err(crate::error::create_type_error(
|
||||||
|
vm,
|
||||||
|
"Cannot convert value to a number",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1202,15 +1195,7 @@ pub fn date_set_seconds<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
|||||||
}
|
}
|
||||||
finish_local_setter(
|
finish_local_setter(
|
||||||
obj,
|
obj,
|
||||||
make_local_timestamp(
|
make_local_timestamp(p.year as f64, p.month0 as f64, p.day as f64, p.hour as f64, p.minute as f64, sec, ms_v),
|
||||||
p.year as f64,
|
|
||||||
p.month0 as f64,
|
|
||||||
p.day as f64,
|
|
||||||
p.hour as f64,
|
|
||||||
p.minute as f64,
|
|
||||||
sec,
|
|
||||||
ms_v,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
/// `Date.prototype.setMilliseconds(ms)`:设置本地毫秒,返回新时间戳。
|
/// `Date.prototype.setMilliseconds(ms)`:设置本地毫秒,返回新时间戳。
|
||||||
@@ -1632,11 +1617,7 @@ pub fn date_to_json<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
|||||||
// 有限性检查仅对 Number 结果生效:tv 为非有限 Number 返回 null;
|
// 有限性检查仅对 Number 结果生效:tv 为非有限 Number 返回 null;
|
||||||
// 字符串 / Symbol 等其余原始值不拦截,直落到 Invoke。
|
// 字符串 / Symbol 等其余原始值不拦截,直落到 Invoke。
|
||||||
if tv.is_int() || tv.is_double() {
|
if tv.is_int() || tv.is_double() {
|
||||||
let n = if tv.is_int() {
|
let n = if tv.is_int() { tv.as_int() as f64 } else { tv.as_double() };
|
||||||
tv.as_int() as f64
|
|
||||||
} else {
|
|
||||||
tv.as_double()
|
|
||||||
};
|
|
||||||
if !n.is_finite() {
|
if !n.is_finite() {
|
||||||
return NativeResult::Ok(JsValue::null());
|
return NativeResult::Ok(JsValue::null());
|
||||||
}
|
}
|
||||||
@@ -1704,13 +1685,7 @@ pub fn date_to_time_string<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
|||||||
let Some(p) = local_components(ms) else {
|
let Some(p) = local_components(ms) else {
|
||||||
return NativeResult::Ok(vm.new_string("Invalid Date"));
|
return NativeResult::Ok(vm.new_string("Invalid Date"));
|
||||||
};
|
};
|
||||||
let s = format!(
|
let s = format!("{:02}:{:02}:{:02} {}", p.hour, p.minute, p.second, format_tz_ms(p.offset_ms));
|
||||||
"{:02}:{:02}:{:02} {}",
|
|
||||||
p.hour,
|
|
||||||
p.minute,
|
|
||||||
p.second,
|
|
||||||
format_tz_ms(p.offset_ms)
|
|
||||||
);
|
|
||||||
NativeResult::Ok(vm.new_string_owned(s))
|
NativeResult::Ok(vm.new_string_owned(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -311,9 +311,7 @@ pub trait VmHost {
|
|||||||
|
|
||||||
// 查找 / 强制转换
|
// 查找 / 强制转换
|
||||||
fn lookup_str(&self, val: JsValue) -> Option<String>;
|
fn lookup_str(&self, val: JsValue) -> Option<String>;
|
||||||
fn coerce_primitive_bounded(
|
fn coerce_primitive_bounded(&mut self, value: JsValue, hint: crate::ToPrimitiveHint) -> Result<JsValue, String>;
|
||||||
&mut self, value: JsValue, hint: crate::ToPrimitiveHint,
|
|
||||||
) -> Result<JsValue, String>;
|
|
||||||
fn coerce_number_bounded(&mut self, value: JsValue) -> Result<f64, String>;
|
fn coerce_number_bounded(&mut self, value: JsValue) -> Result<f64, String>;
|
||||||
|
|
||||||
// 调用基础设施
|
// 调用基础设施
|
||||||
|
|||||||
@@ -280,7 +280,11 @@ impl Vm {
|
|||||||
parts.push(rhs);
|
parts.push(rhs);
|
||||||
for ® in rest {
|
for ® in rest {
|
||||||
let v = self.regs[reg];
|
let v = self.regs[reg];
|
||||||
let prim = if v.is_object() { self.coerce_primitive_bounded(v, coercion::ToPrimitiveHint::Number)? } else { v };
|
let prim = if v.is_object() {
|
||||||
|
self.coerce_primitive_bounded(v, coercion::ToPrimitiveHint::Number)?
|
||||||
|
} else {
|
||||||
|
v
|
||||||
|
};
|
||||||
// 字符串模式对全部操作数做 ToString,Symbol 按 §7.1.17 抛 TypeError。
|
// 字符串模式对全部操作数做 ToString,Symbol 按 §7.1.17 抛 TypeError。
|
||||||
if prim.is_symbol() {
|
if prim.is_symbol() {
|
||||||
self.raise_type_error("Cannot convert a Symbol value to a string")?;
|
self.raise_type_error("Cannot convert a Symbol value to a string")?;
|
||||||
|
|||||||
@@ -74,7 +74,11 @@ impl Vm {
|
|||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
let method_names = if hint.prefer_string() { ["toString", "valueOf"] } else { ["valueOf", "toString"] };
|
let method_names = if hint.prefer_string() {
|
||||||
|
["toString", "valueOf"]
|
||||||
|
} else {
|
||||||
|
["valueOf", "toString"]
|
||||||
|
};
|
||||||
|
|
||||||
for method_name in method_names {
|
for method_name in method_names {
|
||||||
let method_si = self.kernel_core.perm_interner().intern(method_name).0;
|
let method_si = self.kernel_core.perm_interner().intern(method_name).0;
|
||||||
|
|||||||
Reference in New Issue
Block a user