refactor: 统一调用调度并安全化 IC 缓存

This commit is contained in:
2026-06-10 16:17:35 +08:00
parent af7c591414
commit 4aaa62812a
27 changed files with 1284 additions and 847 deletions
+5 -3
View File
@@ -3,7 +3,7 @@ use std::sync::Arc;
use oxide_kernel::builtin::ErrorMethods;
use oxide_kernel::kernel::OxideKernel;
use oxide_kernel::shape_forge::EMPTY_SHAPE_ID;
use oxide_types::object::JsObject;
use oxide_types::object::{JsObject, NativeFnPtr};
use oxide_types::value::JsValue;
fn bind_error_subtype_constructor(
@@ -15,7 +15,8 @@ fn bind_error_subtype_constructor(
let mut ctor = Box::new(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(function_proto_ptr)));
ctor.set_function(true);
ctor.set_native_fn(Some(ctor_fn));
// SAFETY: ctor_fn is a NativeFn fn-item pointer cast to *const () by callers.
ctor.set_native_fn(Some(unsafe { NativeFnPtr::from_raw(ctor_fn) }));
ctor.set_native_arg_count(1);
let si_prototype = sf.intern("prototype").0;
@@ -112,6 +113,7 @@ pub fn bind_error(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
{
let err_ctor_ptr = kernel.builtin_world().error_constructor.as_ptr() as *mut JsObject;
let err_ctor = unsafe { &mut *err_ctor_ptr };
err_ctor.set_native_fn(Some(crate::builtins::error::error_constructor as *const ()));
// SAFETY: error_constructor is a NativeFn fn-item.
err_ctor.set_native_fn(Some(unsafe { NativeFnPtr::from_raw(crate::builtins::error::error_constructor as *const ()) }));
}
}
+3 -2
View File
@@ -3,7 +3,7 @@ use std::sync::Arc;
use oxide_kernel::bind_methods;
use oxide_kernel::builtin::NumberMethods;
use oxide_kernel::kernel::OxideKernel;
use oxide_types::object::JsObject;
use oxide_types::object::{JsObject, NativeFnPtr};
use oxide_types::value::JsValue;
pub fn bind_number(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
@@ -36,7 +36,8 @@ pub fn bind_number(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
{
let num_ctor_ptr = kernel.builtin_world().number_constructor.as_ptr() as *mut JsObject;
let num_ctor = unsafe { &mut *num_ctor_ptr };
num_ctor.set_native_fn(Some(crate::builtins::number::number_constructor as *const ()));
// SAFETY: number_constructor is a NativeFn fn-item.
num_ctor.set_native_fn(Some(unsafe { NativeFnPtr::from_raw(crate::builtins::number::number_constructor as *const ()) }));
num_ctor.set_native_arg_count(1);
for (name, value) in [
+10 -5
View File
@@ -17,7 +17,7 @@ use std::sync::Arc;
use oxide_kernel::builtin::BuiltinWorld;
use oxide_kernel::kernel::OxideKernel;
use oxide_types::object::JsObject;
use oxide_types::object::{JsObject, NativeFnPtr};
use oxide_types::value::JsValue;
#[macro_export]
@@ -29,7 +29,8 @@ macro_rules! bind_constructor {
$global.set_shape_id(shape);
$global.push_prop(val);
let ctor = unsafe { &mut *$ctor_ptr };
ctor.set_native_fn(Some($ctor_fn as *const ()));
// SAFETY: $ctor_fn is a fn-item expression; casting to *const () preserves validity.
ctor.set_native_fn(Some(unsafe { oxide_types::object::NativeFnPtr::from_raw($ctor_fn as *const ()) }));
ctor.set_native_arg_count($nargs);
}};
}
@@ -44,13 +45,15 @@ macro_rules! bind_constructor_hash {
$global.ensure_hash_props().push(Box::new(val));
$global.bump_generation();
let ctor = unsafe { &mut *$ctor_ptr };
ctor.set_native_fn(Some($ctor_fn as *const ()));
// SAFETY: $ctor_fn is a fn-item expression; casting to *const () preserves validity.
ctor.set_native_fn(Some(unsafe { oxide_types::object::NativeFnPtr::from_raw($ctor_fn as *const ()) }));
ctor.set_native_arg_count($nargs);
}};
}
pub(crate) fn configure_native_constructor(ctor: &mut JsObject, native_fn: *const (), arg_count: u8) {
ctor.set_native_fn(Some(native_fn));
// SAFETY: native_fn is always a valid NativeFn fn-item pointer cast to *const () by callers.
ctor.set_native_fn(Some(unsafe { NativeFnPtr::from_raw(native_fn) }));
ctor.set_native_arg_count(arg_count);
}
@@ -60,7 +63,9 @@ pub(crate) fn apply_binding_table(
let shape_forge = kernel.shape_forge().as_ref();
let string_forge = kernel.string_forge().as_ref();
for (name, func, nargs) in bindings {
let _ = world.bind_method(target, shape_forge, string_forge, name, *func, *nargs);
// SAFETY: all entries in the binding table are NativeFn fn-item pointers cast to *const ().
let fn_ptr = unsafe { oxide_types::object::NativeFnPtr::from_raw(*func) };
let _ = world.bind_method(target, shape_forge, string_forge, name, fn_ptr, *nargs);
}
}