This repository has been archived on 2026-09-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
OxideJS/crates/oxide_vm/src/bindings/bind_function.rs
T
pjh123 c230edeb57 feat: Function.prototype[Symbol.hasInstance] 绑定与 bound 函数构造语义
@@hasInstance 按 OrdinaryHasInstance 实现(非可调用 this 返回 false、
prototype 非对象抛 TypeError、bound 链递归到最内层 target);
new boundFn() 解包 [[BoundTargetFunction]] 链后转发构造:绑定实参前置、
新对象原型取 target.prototype、new.target 替换为最内层 target。
2026-08-15 22:40:45 +08:00

43 lines
2.0 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use std::sync::Arc;
use oxide_kernel::builtin::FunctionMethods;
use oxide_kernel::kernel::{KernelCore, KernelSession};
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) {
let function_methods = FunctionMethods {
call: oxide_builtins::function::function_call::<crate::vm::Vm> as *const (),
apply: oxide_builtins::function::function_apply::<crate::vm::Vm> as *const (),
bind: oxide_builtins::function::function_bind::<crate::vm::Vm> as *const (),
to_string: oxide_builtins::function::function_to_string::<crate::vm::Vm> as *const (),
has_instance: oxide_builtins::function::function_symbol_has_instance::<crate::vm::Vm> as *const (),
};
session.builtin_world().bind_function_methods(
&function_methods,
core.perm_interner().as_ref(),
core.shape_forge().as_ref(),
);
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));
}