feat: Function 构造器(动态编译字符串生成函数)

This commit is contained in:
2026-08-08 03:07:26 +08:00
parent 0b68baa802
commit d186c4d9f2
6 changed files with 252 additions and 4 deletions
+16 -1
View File
@@ -2,10 +2,11 @@ use std::sync::Arc;
use oxide_kernel::builtin::FunctionMethods;
use oxide_kernel::kernel::{KernelCore, KernelSession};
use oxide_types::object::JsObject;
use oxide_types::object::{JsObject, PropAttributes};
use oxide_types::value::JsValue;
use super::bind_global_value;
use super::configure_native_constructor;
/// 把 Function 构造器与原型方法绑定到 global。
pub fn bind_function(core: &Arc<KernelCore>, session: &KernelSession, global: &mut JsObject) {
@@ -22,5 +23,19 @@ pub fn bind_function(core: &Arc<KernelCore>, session: &KernelSession, global: &m
);
let function_ctor = session.builtin_world().function_constructor.as_ptr() as *mut JsObject;
configure_native_constructor(
unsafe { &mut *function_ctor },
oxide_builtins::function::function_constructor::<crate::vm::Vm> as *const (),
1,
);
// 设置 Function.length = 1(configure_native_constructor 不写 length 属性),
// 属性为不可写不可枚举可配置。
let length_si = core.perm_interner().intern("length").0;
let length_shape = core.shape_forge().make_shape(unsafe { &*function_ctor }.shape_id(), length_si);
let ctor = unsafe { &mut *function_ctor };
ctor.set_shape_id(length_shape);
ctor.ensure_hash_props().push(JsValue::int(1));
let length_pos = ctor.hash_props_vec().map_or(0, |v| v.len() as u32).saturating_sub(1);
ctor.set_data_meta(length_pos, PropAttributes::new(false, false, true));
bind_global_value(core, global, "Function", JsValue::from_js_object(function_ctor));
}