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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! SM-backed promise types.
//!
//! `JSPromise` and `JSInternalPromise` are newtypes over `*mut JSObject`.
//! In SpiderMonkey, a Promise is a JSObject with internal slots.
use ::std::ffi::c_void;
use ::std::ptr::NonNull;
use mozjs::jsapi::*;
use mozjs::rooted;
use crate::js_value::JSValue;
use crate::strong::Strong;
// ─── JSPromise ──────────────────────────────────────────────────────────────
/// SpiderMonkey-backed JSPromise — newtype over `*mut JSObject`.
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct JSPromise(pub(crate) NonNull<JSObject>);
impl JSPromise {
/// Create a JSPromise from a raw JSObject pointer.
///
/// # Safety
/// `obj` must be a valid, non-null JSObject that is a Promise.
pub unsafe fn from_object(obj: *mut JSObject) -> Option<Self> {
NonNull::new(obj).map(JSPromise)
}
/// Get the underlying JSObject pointer.
pub fn as_object(&self) -> *mut JSObject {
self.0.as_ptr()
}
/// Opaque reference for FFI.
pub fn opaque_ref(&self) -> *const c_void {
self.0.as_ptr() as *const c_void
}
/// Opaque mutable reference for FFI.
pub fn opaque_mut(&mut self) -> *mut c_void {
self.0.as_ptr() as *mut c_void
}
/// Get the promise state (pending/fulfilled/rejected).
///
/// # Safety
/// `cx` must be a valid JSContext.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn state(&self, cx: *mut JSContext) -> PromiseResult {
let obj = self.as_object();
// 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);
if JS::IsPromiseObject(obj_root.handle().into()) {
let state = JS::GetPromiseState(obj_root.handle().into());
match state {
PromiseState::Pending => PromiseResult::Pending,
PromiseState::Fulfilled => PromiseResult::Fulfilled,
PromiseState::Rejected => PromiseResult::Rejected,
#[allow(unreachable_patterns)]
_ => PromiseResult::Pending,
}
} else {
PromiseResult::Pending
}
}
/// Resolve the promise with a value.
///
/// # Safety
/// `cx` must be a valid JSContext.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn resolve(&self, cx: *mut JSContext, value: JS::Handle<Value>) {
let obj = self.as_object();
// 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::ResolvePromise(cx, obj_root.handle().into(), value);
}
/// Reject the promise with a value.
///
/// # Safety
/// `cx` must be a valid JSContext.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn reject(&self, cx: *mut JSContext, value: JS::Handle<Value>) {
let obj = self.as_object();
// 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::RejectPromise(cx, obj_root.handle().into(), value);
}
}
// ─── JSInternalPromise ──────────────────────────────────────────────────────
/// SpiderMonkey-backed JSInternalPromise — newtype over `*mut JSObject`.
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct JSInternalPromise(pub(crate) NonNull<JSObject>);
impl JSInternalPromise {
/// Create from a raw JSObject pointer.
pub unsafe fn from_object(obj: *mut JSObject) -> Option<Self> {
NonNull::new(obj).map(JSInternalPromise)
}
/// Get the underlying JSObject pointer.
pub fn as_object(&self) -> *mut JSObject {
self.0.as_ptr()
}
/// Opaque reference for FFI.
pub fn opaque_ref(&self) -> *const c_void {
self.0.as_ptr() as *const c_void
}
/// Opaque mutable reference for FFI.
pub fn opaque_mut(&mut self) -> *mut c_void {
self.0.as_ptr() as *mut c_void
}
}
// ─── AnyPromise ─────────────────────────────────────────────────────────────
/// Union of JSPromise and JSInternalPromise.
#[derive(Debug, Clone, Copy)]
pub enum AnyPromise {
Normal(JSPromise),
Internal(JSInternalPromise),
}
impl AnyPromise {
/// Get the underlying JSObject pointer.
pub fn as_object(&self) -> *mut JSObject {
match self {
AnyPromise::Normal(p) => p.as_object(),
AnyPromise::Internal(p) => p.as_object(),
}
}
/// Convert to a PromiseResult by checking the promise state.
///
/// # Safety
/// `cx` must be a valid JSContext.
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe fn to_result(&self, cx: *mut JSContext) -> PromiseResult {
let obj = self.as_object();
// 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);
if JS::IsPromiseObject(obj_root.handle().into()) {
let state = JS::GetPromiseState(obj_root.handle().into());
match state {
PromiseState::Pending => PromiseResult::Pending,
PromiseState::Fulfilled => PromiseResult::Fulfilled,
PromiseState::Rejected => PromiseResult::Rejected,
#[allow(unreachable_patterns)]
_ => PromiseResult::Pending,
}
} else {
PromiseResult::Pending
}
}
}
// ─── JSPromiseStrong ────────────────────────────────────────────────────────
/// GC-rooted strong reference to a JSPromise.
pub type JSPromiseStrong = Strong<JSPromise>;
// ─── PromiseResult ──────────────────────────────────────────────────────────
/// Promise result state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PromiseResult {
Pending,
Fulfilled,
Rejected,
}
// ─── Promise submodule ──────────────────────────────────────────────────────
/// Promise module re-export.
pub mod js_promise {
pub use super::{AnyPromise, JSInternalPromise, JSPromise, PromiseResult};
/// Promise unwrap mode.
#[derive(Debug, Clone, Copy)]
pub enum UnwrapMode {
MarkHandled,
DontMarkHandled,
}
/// Promise status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
Pending,
Fulfilled,
Rejected,
}
}