1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! JS object types and property operation helpers.
//!
//! Provides wrappers for common JS object property operations.
use ::std::ffi::CString;
use ::std::marker::PhantomData;
use ::std::ptr::NonNull;
use mozjs::jsapi::*;
use mozjs::jsval::{JSVal, UndefinedValue};
use mozjs::rooted;
use crate::js_value::JSValue;
/// Opaque JS object type (JSC API compatibility).
///
/// In JSC, `JSObject` is a concrete type. In SM, `*mut mozjs::jsapi::JSObject`
/// is the actual type used. This ZST exists purely for JSC API compatibility
/// at the type-name level; actual object references use raw pointers to
/// `mozjs::jsapi::JSObject`.
pub struct JSObject {
_private: (),
}
/// Opaque JS function type (JSC API compatibility).
pub struct JSFunction {
_private: (),
}
/// Opaque JS string type (JSC API compatibility).
pub struct JSString {
_private: (),
}
/// Opaque JS array iterator type (JSC API compatibility).
pub struct JSArrayIterator {
_private: (),
}
/// Opaque JS array type (JSC API compatibility).
pub struct JSArray {
_private: (),
}
/// Opaque JS BigInt type (JSC API compatibility).
pub struct JSBigInt {
_private: (),
}
// ─── Property operation helpers ────────────────────────────────────────────
/// Get a property from a JS object by name.
///
/// # Safety
/// `cx` must be a valid JSContext. `obj` must be a valid JSObject.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn get_property(
cx: *mut JSContext,
obj: *mut mozjs::jsapi::JSObject,
name: &str,
) -> JSValue {
let c_name = CString::new(name).unwrap_or_default();
// BCE-20260619-012: root obj before passing as Handle to JS API.
let cx_ref = &mut mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
rooted!(&in(cx_ref) let obj_root = obj);
let mut val = UndefinedValue();
JS_GetProperty(
cx,
obj_root.handle().into(),
c_name.as_ptr(),
MutableHandle::<Value> {
_phantom_0: PhantomData,
ptr: &mut val,
},
);
JSValue::from_raw(cx, val)
}
/// Set a property on a JS object by name.
///
/// # Safety
/// `cx` must be a valid JSContext. `obj` must be a valid JSObject.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn set_property(
cx: *mut JSContext,
obj: *mut mozjs::jsapi::JSObject,
name: &str,
value: JSVal,
) {
let c_name = CString::new(name).unwrap_or_default();
// BCE-20260619-012: root obj and value (may be Object/String JSVal) before passing as Handle.
let cx_ref = &mut mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
rooted!(&in(cx_ref) let obj_root = obj);
rooted!(&in(cx_ref) let value_root = value);
JS_SetProperty(
cx,
obj_root.handle().into(),
c_name.as_ptr(),
value_root.handle().into(),
);
}
/// Define a property on a JS object with specific attributes.
///
/// # Safety
/// `cx` must be a valid JSContext. `obj` must be a valid JSObject.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn define_property(
cx: *mut JSContext,
obj: *mut mozjs::jsapi::JSObject,
name: &str,
value: JSVal,
attrs: u32,
) -> bool {
let c_name = CString::new(name).unwrap_or_default();
// BCE-20260619-012: root obj and value (may be Object/String JSVal) before passing as Handle.
let cx_ref = &mut mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
rooted!(&in(cx_ref) let obj_root = obj);
rooted!(&in(cx_ref) let value_root = value);
JS_DefineProperty(
cx,
obj_root.handle().into(),
c_name.as_ptr(),
value_root.handle().into(),
attrs,
)
}
/// Check if a JS object has a property.
///
/// # Safety
/// `cx` must be a valid JSContext. `obj` must be a valid JSObject.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn has_property(
cx: *mut JSContext,
obj: *mut mozjs::jsapi::JSObject,
name: &str,
) -> bool {
let c_name = CString::new(name).unwrap_or_default();
// BCE-20260619-012: root obj before passing as Handle to JS API.
let cx_ref = &mut mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
rooted!(&in(cx_ref) let obj_root = obj);
let mut found = false;
JS_HasProperty(cx, obj_root.handle().into(), c_name.as_ptr(), &mut found);
found
}
/// Delete a property from a JS object.
///
/// # Safety
/// `cx` must be a valid JSContext. `obj` must be a valid JSObject.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn delete_property(
cx: *mut JSContext,
obj: *mut mozjs::jsapi::JSObject,
name: &str,
) -> bool {
let c_name = CString::new(name).unwrap_or_default();
// BCE-20260619-012: root obj before passing as Handle to JS API.
let cx_ref = &mut mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
rooted!(&in(cx_ref) let obj_root = obj);
JS_DeleteProperty1(cx, obj_root.handle().into(), c_name.as_ptr())
}