fix: 绑定 Function 和 Object prototype harness helper
This commit is contained in:
@@ -2,8 +2,12 @@ use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::builtin::FunctionMethods;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
pub fn bind_function(kernel: &Arc<OxideKernel>) {
|
||||
use super::bind_global_value;
|
||||
|
||||
pub fn bind_function(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let function_methods = FunctionMethods {
|
||||
call: crate::builtins::function::function_call as *const (),
|
||||
apply: crate::builtins::function::function_apply as *const (),
|
||||
@@ -15,4 +19,7 @@ pub fn bind_function(kernel: &Arc<OxideKernel>) {
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
);
|
||||
|
||||
let function_ctor = kernel.builtin_world().function_constructor.as_ptr() as *mut JsObject;
|
||||
bind_global_value(kernel, global, "Function", JsValue::from_js_object(function_ctor));
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ pub fn bind_object(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
has_own: crate::builtins::object::object_has_own as *const (),
|
||||
entries: crate::builtins::object::object_entries as *const (),
|
||||
values: crate::builtins::object::object_values as *const (),
|
||||
has_own_property: crate::builtins::object::object_proto_has_own_property as *const (),
|
||||
property_is_enumerable: crate::builtins::object::object_proto_property_is_enumerable as *const (),
|
||||
};
|
||||
kernel
|
||||
.builtin_world()
|
||||
|
||||
@@ -87,7 +87,7 @@ pub fn init_kernel_builtins(kernel: &Arc<OxideKernel>) {
|
||||
bind_set::bind_set(kernel, global);
|
||||
bind_map::bind_map(kernel, global);
|
||||
bind_boolean::bind_boolean(kernel, global);
|
||||
bind_function::bind_function(kernel);
|
||||
bind_function::bind_function(kernel, global);
|
||||
bind_regexp::bind_regexp(kernel, global);
|
||||
bind_symbol::bind_symbol(kernel, global);
|
||||
|
||||
|
||||
@@ -447,12 +447,49 @@ pub fn object_has_own(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
if args.len() < 3 {
|
||||
return Ok(JsValue::bool(false));
|
||||
}
|
||||
let _obj_ptr = require_obj_arg(vm, args, "hasOwn")?;
|
||||
let key_val = vm.reg(args[2]);
|
||||
if !key_val.is_string() {
|
||||
let obj_ptr = require_obj_arg(vm, args, "hasOwn")?;
|
||||
let key_si = vm.property_key_si(vm.reg(args[2]));
|
||||
let obj = unsafe { &*obj_ptr };
|
||||
Ok(JsValue::bool(vm.get_own_property_slot(obj, key_si).is_some()))
|
||||
}
|
||||
|
||||
pub fn object_proto_has_own_property(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
if args.len() < 2 {
|
||||
return Ok(JsValue::bool(false));
|
||||
}
|
||||
Ok(JsValue::bool(false))
|
||||
let this_val = vm.reg(args[0]);
|
||||
if !this_val.is_object() || this_val.as_js_object_ptr().is_null() {
|
||||
return Err(crate::builtins::error::create_type_error(
|
||||
vm,
|
||||
"Object.prototype.hasOwnProperty called on non-object",
|
||||
));
|
||||
}
|
||||
let key_si = vm.property_key_si(vm.reg(args[1]));
|
||||
let obj = unsafe { &*this_val.as_js_object_ptr() };
|
||||
Ok(JsValue::bool(vm.get_own_property_slot(obj, key_si).is_some()))
|
||||
}
|
||||
|
||||
pub fn object_proto_property_is_enumerable(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
if args.len() < 2 {
|
||||
return Ok(JsValue::bool(false));
|
||||
}
|
||||
let this_val = vm.reg(args[0]);
|
||||
if !this_val.is_object() || this_val.as_js_object_ptr().is_null() {
|
||||
return Err(crate::builtins::error::create_type_error(
|
||||
vm,
|
||||
"Object.prototype.propertyIsEnumerable called on non-object",
|
||||
));
|
||||
}
|
||||
let key_si = vm.property_key_si(vm.reg(args[1]));
|
||||
let obj = unsafe { &*this_val.as_js_object_ptr() };
|
||||
let Some(pos) = vm.get_own_property_slot(obj, key_si) else {
|
||||
return Ok(JsValue::bool(false));
|
||||
};
|
||||
let enumerable = obj
|
||||
.prop_meta_at(pos)
|
||||
.map(|meta| meta.attributes.enumerable())
|
||||
.unwrap_or(PropAttributes::DEFAULT_DATA.enumerable());
|
||||
Ok(JsValue::bool(enumerable))
|
||||
}
|
||||
|
||||
pub fn object_entries(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
|
||||
Reference in New Issue
Block a user