mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-24 02:31:13 +08:00
style: 自动格式化
This commit is contained in:
@@ -93,7 +93,13 @@ impl KernelCore {
|
||||
let shape_forge = Arc::new(ShapeForge::new());
|
||||
let code_forge = Arc::new(CodeForge::new());
|
||||
let prop_forge = Arc::new(PropForge::new());
|
||||
Arc::new(Self { config, string_forge, shape_forge, code_forge, prop_forge })
|
||||
Arc::new(Self {
|
||||
config,
|
||||
string_forge,
|
||||
shape_forge,
|
||||
code_forge,
|
||||
prop_forge,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn string_forge(&self) -> &Arc<StringForge> {
|
||||
@@ -148,7 +154,10 @@ impl KernelSession {
|
||||
global_obj.set_shape_id(inf_shape);
|
||||
global_obj.ensure_hash_props().push(JsValue::float(f64::INFINITY));
|
||||
|
||||
Self { builtin_world, global_object: P::new(global_obj) }
|
||||
Self {
|
||||
builtin_world,
|
||||
global_object: P::new(global_obj),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn builtin_world(&self) -> &Arc<BuiltinWorld> {
|
||||
@@ -160,7 +169,6 @@ impl KernelSession {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -189,11 +189,26 @@ impl JsObject {
|
||||
pub const OBJ_TYPE_NUMBER_OBJ: u8 = 4;
|
||||
pub const OBJ_TYPE_STRING_OBJ: u8 = 5;
|
||||
|
||||
#[inline] pub fn is_date_obj(&self) -> bool { self.type_tag == Self::OBJ_TYPE_DATE }
|
||||
#[inline] pub fn is_regexp_obj(&self) -> bool { self.type_tag == Self::OBJ_TYPE_REGEXP }
|
||||
#[inline] pub fn is_boolean_obj(&self) -> bool { self.type_tag == Self::OBJ_TYPE_BOOLEAN_OBJ }
|
||||
#[inline] pub fn is_number_obj(&self) -> bool { self.type_tag == Self::OBJ_TYPE_NUMBER_OBJ }
|
||||
#[inline] pub fn is_string_obj(&self) -> bool { self.type_tag == Self::OBJ_TYPE_STRING_OBJ }
|
||||
#[inline]
|
||||
pub fn is_date_obj(&self) -> bool {
|
||||
self.type_tag == Self::OBJ_TYPE_DATE
|
||||
}
|
||||
#[inline]
|
||||
pub fn is_regexp_obj(&self) -> bool {
|
||||
self.type_tag == Self::OBJ_TYPE_REGEXP
|
||||
}
|
||||
#[inline]
|
||||
pub fn is_boolean_obj(&self) -> bool {
|
||||
self.type_tag == Self::OBJ_TYPE_BOOLEAN_OBJ
|
||||
}
|
||||
#[inline]
|
||||
pub fn is_number_obj(&self) -> bool {
|
||||
self.type_tag == Self::OBJ_TYPE_NUMBER_OBJ
|
||||
}
|
||||
#[inline]
|
||||
pub fn is_string_obj(&self) -> bool {
|
||||
self.type_tag == Self::OBJ_TYPE_STRING_OBJ
|
||||
}
|
||||
|
||||
pub fn new_empty(shape_id: ShapeId, proto: JsValue) -> Self {
|
||||
Self {
|
||||
|
||||
@@ -13,7 +13,11 @@ pub fn boolean_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
} else if arg.is_bool() {
|
||||
arg.as_bool()
|
||||
} else if arg.is_string() {
|
||||
let s = vm.kernel_core().string_forge().lookup(arg.as_string_index()).unwrap_or_default();
|
||||
let s = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(arg.as_string_index())
|
||||
.unwrap_or_default();
|
||||
!s.is_empty()
|
||||
} else if arg.is_object() {
|
||||
true
|
||||
|
||||
@@ -10,7 +10,11 @@ use crate::vm::Vm;
|
||||
|
||||
fn get_timestamp(obj: &JsObject) -> f64 {
|
||||
let v = obj.get_prop_at(0);
|
||||
if v.is_double() { v.as_double() } else { f64::NAN }
|
||||
if v.is_double() {
|
||||
v.as_double()
|
||||
} else {
|
||||
f64::NAN
|
||||
}
|
||||
}
|
||||
|
||||
fn set_timestamp(obj: &mut JsObject, ms: f64) {
|
||||
@@ -137,7 +141,11 @@ pub fn date_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
} else {
|
||||
let val = vm.reg(args[1]);
|
||||
if val.is_string() {
|
||||
let s = vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default();
|
||||
let s = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default();
|
||||
let formats = ["%Y-%m-%dT%H:%M:%S%.fZ", "%Y-%m-%dT%H:%M:%S%.f"];
|
||||
let mut ts = f64::NAN;
|
||||
for fmt in &formats {
|
||||
@@ -209,7 +217,11 @@ pub fn date_parse(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
if !val.is_string() {
|
||||
return NativeResult::Ok(JsValue::float(f64::NAN));
|
||||
}
|
||||
let s = vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default();
|
||||
let s = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default();
|
||||
let formats = ["%Y-%m-%dT%H:%M:%S%.fZ", "%Y-%m-%dT%H:%M:%S%.f"];
|
||||
let mut ts = f64::NAN;
|
||||
for fmt in &formats {
|
||||
|
||||
@@ -178,7 +178,11 @@ mod tests {
|
||||
});
|
||||
|
||||
let stack = error_stack_getter(&mut vm, &[0]).unwrap();
|
||||
let stack_str = vm.kernel_core().string_forge().lookup(stack.as_string_index()).unwrap_or_default();
|
||||
let stack_str = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(stack.as_string_index())
|
||||
.unwrap_or_default();
|
||||
assert!(stack_str.contains("foo"), "expected function name in stack: {stack_str}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,10 @@ fn get_regexp_ptr(vm: &Vm, args: &[u8]) -> Result<*mut JsObject, JsValue> {
|
||||
}
|
||||
if !unsafe { &*ptr }.is_regexp_obj() {
|
||||
return Err(JsValue::string(
|
||||
vm.kernel_core().string_forge().intern("TypeError: RegExp.prototype method called on non-RegExp object").0,
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.intern("TypeError: RegExp.prototype method called on non-RegExp object")
|
||||
.0,
|
||||
0,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -10,11 +10,27 @@ fn stub_error(vm: &mut Vm, name: &str) -> NativeResult {
|
||||
}
|
||||
|
||||
// Architecturally deferred — see 13.5-CONTEXT.md for rationale
|
||||
pub fn proxy_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult { stub_error(vm, "Proxy") }
|
||||
pub fn bigint_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult { stub_error(vm, "BigInt") }
|
||||
pub fn weakmap_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult { stub_error(vm, "WeakMap") }
|
||||
pub fn weakset_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult { stub_error(vm, "WeakSet") }
|
||||
pub fn weakref_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult { stub_error(vm, "WeakRef") }
|
||||
pub fn finalization_registry_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult { stub_error(vm, "FinalizationRegistry") }
|
||||
pub fn shared_array_buffer_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult { stub_error(vm, "SharedArrayBuffer") }
|
||||
pub fn atomics_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult { stub_error(vm, "Atomics") }
|
||||
pub fn proxy_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
stub_error(vm, "Proxy")
|
||||
}
|
||||
pub fn bigint_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
stub_error(vm, "BigInt")
|
||||
}
|
||||
pub fn weakmap_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
stub_error(vm, "WeakMap")
|
||||
}
|
||||
pub fn weakset_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
stub_error(vm, "WeakSet")
|
||||
}
|
||||
pub fn weakref_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
stub_error(vm, "WeakRef")
|
||||
}
|
||||
pub fn finalization_registry_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
stub_error(vm, "FinalizationRegistry")
|
||||
}
|
||||
pub fn shared_array_buffer_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
stub_error(vm, "SharedArrayBuffer")
|
||||
}
|
||||
pub fn atomics_stub(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
stub_error(vm, "Atomics")
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@ pub fn symbol_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let sp = vm.session().builtin_world().symbol_proto.as_ptr() as *mut JsObject;
|
||||
if std::ptr::eq(proto_ptr, sp) {
|
||||
return NativeResult::Err(JsValue::string(
|
||||
vm.kernel_core().string_forge().intern("TypeError: Symbol is not a constructor").0,
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.intern("TypeError: Symbol is not a constructor")
|
||||
.0,
|
||||
0,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -294,11 +294,20 @@ pub fn to_object(val: JsValue, vm: &mut Vm) -> Result<JsValue, &'static str> {
|
||||
return Err("TypeError: Cannot convert null or undefined to object");
|
||||
}
|
||||
let (proto_ptr, type_tag) = if val.is_string() {
|
||||
(vm.session().builtin_world().string_proto.as_ptr() as *mut JsObject, JsObject::OBJ_TYPE_STRING_OBJ)
|
||||
(
|
||||
vm.session().builtin_world().string_proto.as_ptr() as *mut JsObject,
|
||||
JsObject::OBJ_TYPE_STRING_OBJ,
|
||||
)
|
||||
} else if val.is_int() || val.is_double() {
|
||||
(vm.session().builtin_world().number_proto.as_ptr() as *mut JsObject, JsObject::OBJ_TYPE_NUMBER_OBJ)
|
||||
(
|
||||
vm.session().builtin_world().number_proto.as_ptr() as *mut JsObject,
|
||||
JsObject::OBJ_TYPE_NUMBER_OBJ,
|
||||
)
|
||||
} else if val.is_bool() {
|
||||
(vm.session().builtin_world().boolean_proto.as_ptr() as *mut JsObject, JsObject::OBJ_TYPE_BOOLEAN_OBJ)
|
||||
(
|
||||
vm.session().builtin_world().boolean_proto.as_ptr() as *mut JsObject,
|
||||
JsObject::OBJ_TYPE_BOOLEAN_OBJ,
|
||||
)
|
||||
} else {
|
||||
(&*vm.object_prototype as *const JsObject as *mut JsObject, JsObject::OBJ_TYPE_PLAIN)
|
||||
};
|
||||
|
||||
@@ -97,8 +97,6 @@ pub struct CallFrame {
|
||||
pub continuation: FrameContinuation,
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub struct ForInIter<'bump> {
|
||||
pub keys: bumpalo::collections::Vec<'bump, JsValue>,
|
||||
pub index: usize,
|
||||
@@ -305,7 +303,11 @@ impl Vm {
|
||||
let mut proto = obj.proto();
|
||||
while proto.is_object() {
|
||||
let proto_obj = unsafe { &*proto.as_js_object_ptr() };
|
||||
if let Some(pos) = self.kernel_core.shape_forge().lookup_position(proto_obj.shape_id(), prop_name_si) {
|
||||
if let Some(pos) = self
|
||||
.kernel_core
|
||||
.shape_forge()
|
||||
.lookup_position(proto_obj.shape_id(), prop_name_si)
|
||||
{
|
||||
let val = proto_obj.get_prop_at(pos);
|
||||
if !val.is_undefined() || proto_obj.prop_vec_len() > pos as usize {
|
||||
return Some(val);
|
||||
|
||||
@@ -172,7 +172,10 @@ impl Vm {
|
||||
let reg = (seg & 0x7F) as u8;
|
||||
let val = self.regs[reg as usize];
|
||||
let s = if val.is_string() {
|
||||
self.kernel_core.string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
self.kernel_core
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
format!("{}", val)
|
||||
};
|
||||
@@ -182,7 +185,11 @@ impl Vm {
|
||||
if const_idx < self.constants.len() {
|
||||
let val = self.constants[const_idx];
|
||||
if val.is_string() {
|
||||
let s = self.kernel_core.string_forge().lookup(val.as_string_index()).unwrap_or_default();
|
||||
let s = self
|
||||
.kernel_core
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default();
|
||||
result.push_str(&s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,10 @@ fn eval_val(source: &str) -> (Vm, Result<JsValue, String>) {
|
||||
|
||||
fn to_str(vm: &Vm, val: JsValue) -> String {
|
||||
if val.is_string() {
|
||||
vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
format!("{}", val)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,10 @@ fn eval(vm: &mut Vm, source: &str) -> Result<JsValue, String> {
|
||||
}
|
||||
|
||||
fn str_val(vm: &Vm, val: JsValue) -> String {
|
||||
vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
// -- direct native fn tests --
|
||||
|
||||
@@ -9,7 +9,10 @@ fn eval(vm: &mut Vm, source: &str) -> Result<oxide_types::value::JsValue, String
|
||||
}
|
||||
|
||||
fn str_val(vm: &Vm, val: oxide_types::value::JsValue) -> String {
|
||||
vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
// -- static methods --
|
||||
|
||||
@@ -34,7 +34,11 @@ fn function_bind_creates_wrapper() {
|
||||
fn function_constructor_is_global() {
|
||||
let mut vm = Vm::new();
|
||||
let result = eval(&mut vm, "typeof Function").unwrap();
|
||||
let rendered = vm.kernel_core().string_forge().lookup(result.as_string_index()).unwrap_or_default();
|
||||
let rendered = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(result.as_string_index())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(rendered, "function");
|
||||
}
|
||||
|
||||
@@ -82,7 +86,11 @@ fn function_to_string_includes_function() {
|
||||
let mut vm = Vm::new();
|
||||
let result = eval(&mut vm, "Math.max.toString()").unwrap();
|
||||
assert!(result.is_string());
|
||||
let rendered = vm.kernel_core().string_forge().lookup(result.as_string_index()).unwrap_or_default();
|
||||
let rendered = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(result.as_string_index())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(rendered, "function max() { [native code] }");
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,10 @@ fn eval(vm: &mut Vm, source: &str) -> Result<JsValue, String> {
|
||||
}
|
||||
|
||||
fn str_val(vm: &Vm, val: JsValue) -> String {
|
||||
vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
// -- direct native fn tests --
|
||||
|
||||
@@ -76,7 +76,11 @@ fn number_constructor() {
|
||||
fn to_string_of_number() {
|
||||
let mut vm = Vm::new();
|
||||
let result = eval(&mut vm, "var n = 42; n.toString()").unwrap();
|
||||
let s = vm.kernel_core().string_forge().lookup(result.as_string_index()).unwrap_or_default();
|
||||
let s = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(result.as_string_index())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(s, "42");
|
||||
}
|
||||
|
||||
@@ -84,7 +88,11 @@ fn to_string_of_number() {
|
||||
fn to_fixed_of_number() {
|
||||
let mut vm = Vm::new();
|
||||
let result = eval(&mut vm, "var n = 3.14159; n.toFixed(2)").unwrap();
|
||||
let s = vm.kernel_core().string_forge().lookup(result.as_string_index()).unwrap_or_default();
|
||||
let s = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(result.as_string_index())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(s, "3.14");
|
||||
}
|
||||
|
||||
@@ -113,7 +121,11 @@ fn number_epsilon_is_positive() {
|
||||
fn number_to_exponential() {
|
||||
let mut vm = Vm::new();
|
||||
let result = eval(&mut vm, "var n = 123.456; n.toExponential(2)").unwrap();
|
||||
let s = vm.kernel_core().string_forge().lookup(result.as_string_index()).unwrap_or_default();
|
||||
let s = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(result.as_string_index())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(s, "1.23e2");
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,11 @@ fn object_assign_copies() {
|
||||
fn object_assign_boxes_primitive_target() {
|
||||
let (_vm, result) = eval("var result = Object.assign('a'); result.valueOf()").unwrap();
|
||||
assert!(result.is_string());
|
||||
let rendered = _vm.kernel_core().string_forge().lookup(result.as_string_index()).unwrap_or_default();
|
||||
let rendered = _vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(result.as_string_index())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(rendered, "a");
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,10 @@ fn eval(vm: &mut Vm, source: &str) -> Result<JsValue, String> {
|
||||
}
|
||||
|
||||
fn to_str(vm: &Vm, val: JsValue) -> String {
|
||||
vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
// --- RegExp constructor ---
|
||||
|
||||
@@ -10,7 +10,10 @@ fn eval(vm: &mut Vm, source: &str) -> Result<JsValue, String> {
|
||||
}
|
||||
|
||||
fn to_str(vm: &Vm, val: JsValue) -> String {
|
||||
vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -10,7 +10,10 @@ fn eval(vm: &mut Vm, source: &str) -> Result<JsValue, String> {
|
||||
}
|
||||
|
||||
fn to_str(vm: &Vm, val: JsValue) -> String {
|
||||
vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
// --- Symbol constructor ---
|
||||
|
||||
@@ -204,6 +204,10 @@ fn function_default_prototype_accepts_member_assignment() {
|
||||
"function Test262Error(message) { this.message = message || ''; } Test262Error.prototype.toString = function () { return this.message; }; new Test262Error('ok').toString()",
|
||||
)
|
||||
.unwrap();
|
||||
let rendered = vm.kernel_core().string_forge().lookup(result.as_string_index()).unwrap_or_default();
|
||||
let rendered = vm
|
||||
.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(result.as_string_index())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(rendered, "ok");
|
||||
}
|
||||
|
||||
@@ -10,7 +10,10 @@ fn eval(vm: &mut Vm, source: &str) -> Result<JsValue, String> {
|
||||
}
|
||||
|
||||
fn string_value(vm: &Vm, val: JsValue) -> String {
|
||||
vm.kernel_core().string_forge().lookup(val.as_string_index()).unwrap_or_default()
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
// -- JSON.parse --
|
||||
|
||||
Reference in New Issue
Block a user