Files
OxideJS-New/crates/oxide_vm/src/bindings/bind_bigint.rs
T

62 lines
2.9 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 crate::bindings::{
apply_binding_table, bind_global_value, bind_well_known_data_property, configure_native_constructor, BindEnv,
};
use oxide_types::object::{JsObject, PropAttributes};
use oxide_types::value::JsValue;
/// 把 BigInt 构造器与原型方法绑定到 global。
///
/// 构造器/原型对象由 `BuiltinWorld` 创建(含 name/prototype/constructor 互指),
/// 这里只配置 native 实现并挂方法。`BigInt(value)` 当函数调用时返回 BigInt 值;
/// `new BigInt()` 抛 TypeError(BigInt 不可 new)。
///
/// `realm_id` 是目标所属 realm 的编号:`@@toStringTag` 符号键按 (realm 编号,
/// 局部下标) 编码,realm 编号为 0 时与旧编码逐字节一致。
pub fn bind_bigint(env: BindEnv<'_>, global: &mut JsObject) {
let world = env.world();
let ctor_ptr = world.bigint_constructor.as_ptr() as *mut JsObject;
let ctor = unsafe { &mut *ctor_ptr };
let proto_ptr = world.bigint_proto.as_ptr() as *mut JsObject;
let proto = unsafe { &mut *proto_ptr };
configure_native_constructor(ctor, oxide_builtins::bigint::bigint_constructor::<crate::vm::Vm> as *const (), 1);
// BigInt.length = 1,属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }。
let length_si = env.string_forge().intern("length").0;
let length_shape = env.shape_forge().make_shape(ctor.shape_id(), length_si);
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));
// 静态方法:asIntN / asUintN(length 均为 2)。
apply_binding_table(
env,
ctor,
&[
("asIntN", oxide_builtins::bigint::bigint_as_int_n::<crate::vm::Vm> as *const (), 2),
("asUintN", oxide_builtins::bigint::bigint_as_uint_n::<crate::vm::Vm> as *const (), 2),
],
);
apply_binding_table(
env,
proto,
&[
("toString", oxide_builtins::bigint::bigint_to_string::<crate::vm::Vm> as *const (), 0),
(
"toLocaleString",
oxide_builtins::bigint::bigint_to_locale_string::<crate::vm::Vm> as *const (),
0,
),
("valueOf", oxide_builtins::bigint::bigint_value_of::<crate::vm::Vm> as *const (), 0),
],
);
// BigInt.prototype[@@toStringTag] = "BigInt",属性 { writable:false, enumerable:false, configurable:true }。
let tag_si = env.string_forge().intern("BigInt").0;
let tag_value = JsValue::perm_string(env.string_forge().string_ptr(tag_si));
bind_well_known_data_property(env.core, proto, 9, tag_value, PropAttributes::new(false, false, true), env.realm_id);
bind_global_value(env.core, global, "BigInt", JsValue::from_js_object(ctor_ptr));
}