Skip to main content

mozjs/
conversions.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! Conversions of Rust values to and from `JSVal`.
6//!
7//! | IDL type                | Type                             |
8//! |-------------------------|----------------------------------|
9//! | any                     | `JSVal`                          |
10//! | boolean                 | `bool`                           |
11//! | byte                    | `i8`                             |
12//! | octet                   | `u8`                             |
13//! | short                   | `i16`                            |
14//! | unsigned short          | `u16`                            |
15//! | long                    | `i32`                            |
16//! | unsigned long           | `u32`                            |
17//! | long long               | `i64`                            |
18//! | unsigned long long      | `u64`                            |
19//! | unrestricted float      | `f32`                            |
20//! | float                   | `Finite<f32>`                    |
21//! | unrestricted double     | `f64`                            |
22//! | double                  | `Finite<f64>`                    |
23//! | USVString               | `String`                         |
24//! | object                  | `*mut JSObject`                  |
25//! | symbol                  | `*mut Symbol`                    |
26//! | nullable types          | `Option<T>`                      |
27//! | sequences               | `Vec<T>`                         |
28
29#![deny(missing_docs)]
30
31use crate::error::throw_type_error_safe;
32use crate::jsapi::Heap;
33use crate::jsapi::JS;
34use crate::jsapi::{JSContext, JSObject, JSString};
35use crate::jsapi::{JS_DeprecatedStringHasLatin1Chars, JSPROP_ENUMERATE};
36use crate::jsval::{BooleanValue, DoubleValue, Int32Value, NullValue, UInt32Value, UndefinedValue};
37use crate::jsval::{JSVal, ObjectOrNullValue, ObjectValue, StringValue, SymbolValue};
38use crate::rooted;
39use crate::rust::for_of;
40use crate::rust::maybe_wrap_value;
41use crate::rust::wrappers2::{
42    AssertSameCompartment, JS_DefineElement, JS_GetLatin1StringCharsAndLength,
43    JS_GetTwoByteStringCharsAndLength, JS_NewStringCopyUTF8N, NewArrayObject1,
44};
45use crate::rust::ForOfIterationFailure;
46use crate::rust::{maybe_wrap_object_or_null_value, maybe_wrap_object_value, ToString};
47use crate::rust::{HandleValue, MutableHandleValue};
48use crate::rust::{ToBoolean, ToInt32, ToInt64, ToNumber, ToUint16, ToUint32, ToUint64};
49use libc;
50use log::debug;
51use num_traits::PrimInt;
52use std::borrow::Cow;
53use std::ffi::CStr;
54use std::ops::ControlFlow;
55use std::ptr::NonNull;
56use std::rc::Rc;
57use std::{ptr, slice};
58
59trait As<O>: Copy {
60    fn cast(self) -> O;
61}
62
63macro_rules! impl_as {
64    ($I:ty, $O:ty) => {
65        impl As<$O> for $I {
66            fn cast(self) -> $O {
67                self as $O
68            }
69        }
70    };
71}
72
73impl_as!(f64, u8);
74impl_as!(f64, u16);
75impl_as!(f64, u32);
76impl_as!(f64, u64);
77impl_as!(f64, i8);
78impl_as!(f64, i16);
79impl_as!(f64, i32);
80impl_as!(f64, i64);
81
82impl_as!(u8, f64);
83impl_as!(u16, f64);
84impl_as!(u32, f64);
85impl_as!(u64, f64);
86impl_as!(i8, f64);
87impl_as!(i16, f64);
88impl_as!(i32, f64);
89impl_as!(i64, f64);
90
91impl_as!(i32, i8);
92impl_as!(i32, u8);
93impl_as!(i32, i16);
94impl_as!(u16, u16);
95impl_as!(i32, i32);
96impl_as!(u32, u32);
97impl_as!(i64, i64);
98impl_as!(u64, u64);
99
100/// Similar to num_traits, but we use need to be able to customize values
101pub trait Number {
102    /// Zero value of this type
103    const ZERO: Self;
104    /// Smallest finite number this type can represent
105    const MIN: Self;
106    /// Largest finite number this type can represent
107    const MAX: Self;
108}
109
110macro_rules! impl_num {
111    ($N:ty, $zero:expr, $min:expr, $max:expr) => {
112        impl Number for $N {
113            const ZERO: $N = $zero;
114            const MIN: $N = $min;
115            const MAX: $N = $max;
116        }
117    };
118}
119
120// lower upper bound per: https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
121impl_num!(u8, 0, u8::MIN, u8::MAX);
122impl_num!(u16, 0, u16::MIN, u16::MAX);
123impl_num!(u32, 0, u32::MIN, u32::MAX);
124impl_num!(u64, 0, 0, (1 << 53) - 1);
125
126impl_num!(i8, 0, i8::MIN, i8::MAX);
127impl_num!(i16, 0, i16::MIN, i16::MAX);
128impl_num!(i32, 0, i32::MIN, i32::MAX);
129impl_num!(i64, 0, -(1 << 53) + 1, (1 << 53) - 1);
130
131impl_num!(f32, 0.0, f32::MIN, f32::MAX);
132impl_num!(f64, 0.0, f64::MIN, f64::MAX);
133
134/// A trait to convert Rust types to `JSVal`s.
135pub trait ToJSValConvertible {
136    /// Convert `self` to a `JSVal`. JSAPI failure causes a panic.
137    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, rval: MutableHandleValue);
138}
139
140/// An enum to better support enums through FromJSValConvertible::from_jsval.
141#[derive(PartialEq, Eq, Clone, Debug)]
142pub enum ConversionResult<T> {
143    /// Everything went fine.
144    Success(T),
145    /// Conversion failed, without a pending exception.
146    Failure(Cow<'static, CStr>),
147}
148
149impl<T> ConversionResult<T> {
150    /// Returns Some(value) if it is `ConversionResult::Success`.
151    pub fn get_success_value(&self) -> Option<&T> {
152        match *self {
153            ConversionResult::Success(ref v) => Some(v),
154            _ => None,
155        }
156    }
157}
158
159/// A trait to convert `JSVal`s to Rust types.
160pub trait FromJSValConvertible: Sized {
161    /// Optional configurable behaviour switch; use () for no configuration.
162    type Config;
163
164    /// Convert `val` to type `Self`.
165    /// Optional configuration of type `T` can be passed as the `option`
166    /// argument.
167    /// If it returns `Err(())`, a JSAPI exception is pending.
168    /// If it returns `Ok(Failure(reason))`, there is no pending JSAPI exception.
169    fn safe_from_jsval(
170        cx: &mut crate::context::JSContext,
171        val: HandleValue,
172        option: Self::Config,
173    ) -> Result<ConversionResult<Self>, ()>;
174}
175
176/// A trait to convert `JSVal`s to Rust types inside of Rc wrappers.
177pub trait FromJSValConvertibleRc: Sized {
178    /// Convert `val` to type `Self`.
179    /// If it returns `Err(())`, a JSAPI exception is pending.
180    /// If it returns `Ok(Failure(reason))`, there is no pending JSAPI exception.
181    fn safe_from_jsval(
182        cx: &mut crate::context::JSContext,
183        val: HandleValue,
184    ) -> Result<ConversionResult<Rc<Self>>, ()>;
185}
186
187impl<T: FromJSValConvertibleRc> FromJSValConvertible for Rc<T> {
188    type Config = ();
189
190    fn safe_from_jsval(
191        cx: &mut crate::context::JSContext,
192        val: HandleValue,
193        _option: (),
194    ) -> Result<ConversionResult<Rc<T>>, ()> {
195        <T as FromJSValConvertibleRc>::safe_from_jsval(cx, val)
196    }
197}
198
199/// Behavior for converting out-of-range integers.
200#[derive(PartialEq, Eq, Clone)]
201pub enum ConversionBehavior {
202    /// Wrap into the integer's range.
203    Default,
204    /// Throw an exception.
205    EnforceRange,
206    /// Clamp into the integer's range.
207    Clamp,
208}
209
210/// Try to cast the number to a smaller type, but
211/// if it doesn't fit, it will return an error.
212// https://searchfox.org/mozilla-esr128/rev/1aa97f9d67f7a7231e62af283eaa02a6b31380e1/dom/bindings/PrimitiveConversions.h#166
213fn enforce_range<D>(cx: &mut crate::context::JSContext, d: f64) -> Result<ConversionResult<D>, ()>
214where
215    D: Number + As<f64>,
216    f64: As<D>,
217{
218    if d.is_infinite() {
219        throw_type_error_safe(cx, c"value out of range in an EnforceRange argument");
220        return Err(());
221    }
222
223    let rounded = d.signum() * d.abs().floor();
224    if D::MIN.cast() <= rounded && rounded <= D::MAX.cast() {
225        Ok(ConversionResult::Success(rounded.cast()))
226    } else {
227        throw_type_error_safe(cx, c"value out of range in an EnforceRange argument");
228        Err(())
229    }
230}
231
232/// WebIDL ConvertToInt (Clamp) conversion.
233/// Spec: <https://webidl.spec.whatwg.org/#abstract-opdef-converttoint>
234///
235/// This function is ported from Gecko’s
236/// [`PrimitiveConversionTraits_Clamp`](https://searchfox.org/firefox-main/rev/aee7c0f24f488cd7f5a835803b48dd0c0cb2fd5f/dom/bindings/PrimitiveConversions.h#226).
237///
238/// # Warning
239/// This function must only be used when the target type `D` represents an
240/// integer WebIDL type. Using it with non-integer types would be incorrect.
241fn clamp_to<D>(d: f64) -> D
242where
243    D: Number + PrimInt + As<f64>,
244    f64: As<D>,
245{
246    // NaN maps to zero.
247    if d.is_nan() {
248        return D::ZERO;
249    }
250
251    if d >= D::MAX.cast() {
252        return D::MAX;
253    }
254    if d <= D::MIN.cast() {
255        return D::MIN;
256    }
257
258    debug_assert!(d.is_finite());
259
260    // Banker's rounding (round ties towards even).
261    // We move away from 0 by 0.5 and then truncate. That gets us the right
262    // answer for any starting value except plus or minus N.5. With a starting
263    // value of that form, we now have plus or minus N+1. If N is odd, this is
264    // the correct result. If N is even, plus or minus N is the correct result.
265    let to_truncate = if d < 0.0 { d - 0.5 } else { d + 0.5 };
266
267    let mut truncated: D = to_truncate.cast();
268
269    if truncated.cast() == to_truncate {
270        // It was a tie (since moving away from 0 by 0.5 gave us the exact integer
271        // we want). Since we rounded away from 0, we either already have an even
272        // number or we have an odd number but the number we want is one closer to
273        // 0. So just unconditionally masking out the ones bit should do the trick
274        // to get us the value we want.
275        truncated = truncated & !D::one();
276    }
277
278    truncated
279}
280
281// https://heycam.github.io/webidl/#es-void
282impl ToJSValConvertible for () {
283    #[inline]
284    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
285        rval.set(UndefinedValue());
286    }
287}
288
289impl FromJSValConvertible for JSVal {
290    type Config = ();
291
292    fn safe_from_jsval(
293        _cx: &mut crate::context::JSContext,
294        value: HandleValue,
295        _option: (),
296    ) -> Result<ConversionResult<JSVal>, ()> {
297        Ok(ConversionResult::Success(value.get()))
298    }
299}
300
301impl ToJSValConvertible for JSVal {
302    #[inline]
303    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
304        rval.set(*self);
305        maybe_wrap_value(cx, rval);
306    }
307}
308
309impl<'a> ToJSValConvertible for HandleValue<'a> {
310    #[inline]
311    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
312        rval.set(self.get());
313        maybe_wrap_value(cx, rval);
314    }
315}
316
317impl ToJSValConvertible for Heap<JSVal> {
318    #[inline]
319    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
320        rval.set(self.get());
321        maybe_wrap_value(cx, rval);
322    }
323}
324
325#[inline]
326fn convert_int_from_jsval<T, M>(
327    cx: &mut crate::context::JSContext,
328    value: HandleValue,
329    option: ConversionBehavior,
330    convert_fn: unsafe fn(*mut JSContext, HandleValue) -> Result<M, ()>,
331) -> Result<ConversionResult<T>, ()>
332where
333    T: Number + As<f64> + PrimInt,
334    M: Number + As<T>,
335    f64: As<T>,
336{
337    match option {
338        ConversionBehavior::Default => Ok(ConversionResult::Success(unsafe {
339            convert_fn(cx.raw_cx(), value)?.cast()
340        })),
341        ConversionBehavior::EnforceRange => {
342            let number = unsafe { ToNumber(cx.raw_cx(), value) }?;
343            enforce_range(cx, number)
344        }
345        ConversionBehavior::Clamp => Ok(ConversionResult::Success(clamp_to(unsafe {
346            ToNumber(cx.raw_cx(), value)
347        }?))),
348    }
349}
350
351// https://heycam.github.io/webidl/#es-boolean
352impl ToJSValConvertible for bool {
353    #[inline]
354    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
355        rval.set(BooleanValue(*self));
356    }
357}
358
359// https://heycam.github.io/webidl/#es-boolean
360impl FromJSValConvertible for bool {
361    type Config = ();
362
363    fn safe_from_jsval(
364        _cx: &mut crate::context::JSContext,
365        val: HandleValue,
366        _option: (),
367    ) -> Result<ConversionResult<bool>, ()> {
368        unsafe { Ok(ToBoolean(val)).map(ConversionResult::Success) }
369    }
370}
371
372// https://heycam.github.io/webidl/#es-byte
373impl ToJSValConvertible for i8 {
374    #[inline]
375    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
376        rval.set(Int32Value(*self as i32));
377    }
378}
379
380// https://heycam.github.io/webidl/#es-byte
381impl FromJSValConvertible for i8 {
382    type Config = ConversionBehavior;
383
384    fn safe_from_jsval(
385        cx: &mut crate::context::JSContext,
386        val: HandleValue,
387        option: ConversionBehavior,
388    ) -> Result<ConversionResult<i8>, ()> {
389        convert_int_from_jsval(cx, val, option, ToInt32)
390    }
391}
392
393// https://heycam.github.io/webidl/#es-octet
394impl ToJSValConvertible for u8 {
395    #[inline]
396    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
397        rval.set(Int32Value(*self as i32));
398    }
399}
400
401// https://heycam.github.io/webidl/#es-octet
402impl FromJSValConvertible for u8 {
403    type Config = ConversionBehavior;
404
405    fn safe_from_jsval(
406        cx: &mut crate::context::JSContext,
407        val: HandleValue,
408        option: ConversionBehavior,
409    ) -> Result<ConversionResult<u8>, ()> {
410        convert_int_from_jsval(cx, val, option, ToInt32)
411    }
412}
413
414// https://heycam.github.io/webidl/#es-short
415impl ToJSValConvertible for i16 {
416    #[inline]
417    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
418        rval.set(Int32Value(*self as i32));
419    }
420}
421
422// https://heycam.github.io/webidl/#es-short
423impl FromJSValConvertible for i16 {
424    type Config = ConversionBehavior;
425
426    fn safe_from_jsval(
427        cx: &mut crate::context::JSContext,
428        val: HandleValue,
429        option: ConversionBehavior,
430    ) -> Result<ConversionResult<i16>, ()> {
431        convert_int_from_jsval(cx, val, option, ToInt32)
432    }
433}
434
435// https://heycam.github.io/webidl/#es-unsigned-short
436impl ToJSValConvertible for u16 {
437    #[inline]
438    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
439        rval.set(Int32Value(*self as i32));
440    }
441}
442
443// https://heycam.github.io/webidl/#es-unsigned-short
444impl FromJSValConvertible for u16 {
445    type Config = ConversionBehavior;
446
447    fn safe_from_jsval(
448        cx: &mut crate::context::JSContext,
449        val: HandleValue,
450        option: ConversionBehavior,
451    ) -> Result<ConversionResult<u16>, ()> {
452        convert_int_from_jsval(cx, val, option, ToUint16)
453    }
454}
455
456// https://heycam.github.io/webidl/#es-long
457impl ToJSValConvertible for i32 {
458    #[inline]
459    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
460        rval.set(Int32Value(*self));
461    }
462}
463
464// https://heycam.github.io/webidl/#es-long
465impl FromJSValConvertible for i32 {
466    type Config = ConversionBehavior;
467
468    fn safe_from_jsval(
469        cx: &mut crate::context::JSContext,
470        val: HandleValue,
471        option: ConversionBehavior,
472    ) -> Result<ConversionResult<i32>, ()> {
473        convert_int_from_jsval(cx, val, option, ToInt32)
474    }
475}
476
477// https://heycam.github.io/webidl/#es-unsigned-long
478impl ToJSValConvertible for u32 {
479    #[inline]
480    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
481        rval.set(UInt32Value(*self));
482    }
483}
484
485// https://heycam.github.io/webidl/#es-unsigned-long
486impl FromJSValConvertible for u32 {
487    type Config = ConversionBehavior;
488
489    fn safe_from_jsval(
490        cx: &mut crate::context::JSContext,
491        val: HandleValue,
492        option: ConversionBehavior,
493    ) -> Result<ConversionResult<u32>, ()> {
494        convert_int_from_jsval(cx, val, option, ToUint32)
495    }
496}
497
498// https://heycam.github.io/webidl/#es-long-long
499impl ToJSValConvertible for i64 {
500    #[inline]
501    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
502        rval.set(DoubleValue(*self as f64));
503    }
504}
505
506// https://heycam.github.io/webidl/#es-long-long
507impl FromJSValConvertible for i64 {
508    type Config = ConversionBehavior;
509
510    fn safe_from_jsval(
511        cx: &mut crate::context::JSContext,
512        val: HandleValue,
513        option: ConversionBehavior,
514    ) -> Result<ConversionResult<i64>, ()> {
515        convert_int_from_jsval(cx, val, option, ToInt64)
516    }
517}
518
519// https://heycam.github.io/webidl/#es-unsigned-long-long
520impl ToJSValConvertible for u64 {
521    #[inline]
522    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
523        rval.set(DoubleValue(*self as f64));
524    }
525}
526
527// https://heycam.github.io/webidl/#es-unsigned-long-long
528impl FromJSValConvertible for u64 {
529    type Config = ConversionBehavior;
530
531    fn safe_from_jsval(
532        cx: &mut crate::context::JSContext,
533        val: HandleValue,
534        option: ConversionBehavior,
535    ) -> Result<ConversionResult<u64>, ()> {
536        convert_int_from_jsval(cx, val, option, ToUint64)
537    }
538}
539
540// https://heycam.github.io/webidl/#es-float
541impl ToJSValConvertible for f32 {
542    #[inline]
543    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
544        rval.set(DoubleValue(*self as f64));
545    }
546}
547
548// https://heycam.github.io/webidl/#es-float
549impl FromJSValConvertible for f32 {
550    type Config = ();
551
552    fn safe_from_jsval(
553        cx: &mut crate::context::JSContext,
554        val: HandleValue,
555        _option: (),
556    ) -> Result<ConversionResult<f32>, ()> {
557        let result = unsafe { ToNumber(cx.raw_cx(), val) };
558        result.map(|f| f as f32).map(ConversionResult::Success)
559    }
560}
561
562// https://heycam.github.io/webidl/#es-double
563impl ToJSValConvertible for f64 {
564    #[inline]
565    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
566        rval.set(DoubleValue(*self))
567    }
568}
569
570// https://heycam.github.io/webidl/#es-double
571impl FromJSValConvertible for f64 {
572    type Config = ();
573
574    fn safe_from_jsval(
575        cx: &mut crate::context::JSContext,
576        val: HandleValue,
577        _option: (),
578    ) -> Result<ConversionResult<f64>, ()> {
579        unsafe { ToNumber(cx.raw_cx(), val).map(ConversionResult::Success) }
580    }
581}
582
583/// Converts a `JSString`, encoded in "Latin1" (i.e. U+0000-U+00FF encoded as 0x00-0xFF) into a
584/// `String`.
585///
586/// ### Safety
587/// `s` must points to a valid `JSString`
588pub unsafe fn latin1_to_string(cx: &crate::context::JSContext, s: NonNull<JSString>) -> String {
589    assert!(unsafe { JS_DeprecatedStringHasLatin1Chars(s.as_ptr()) });
590
591    let mut length = 0;
592    let chars = unsafe {
593        let chars = JS_GetLatin1StringCharsAndLength(cx, s.as_ptr(), &mut length);
594        assert!(!chars.is_null());
595
596        slice::from_raw_parts(chars, length as usize)
597    };
598    // The `encoding.rs` documentation for `convert_latin1_to_utf8` states that:
599    // > The length of the destination buffer must be at least the length of the source
600    // > buffer times two.
601    let mut v = vec![0; chars.len() * 2];
602    let real_size = encoding_rs::mem::convert_latin1_to_utf8(chars, v.as_mut_slice());
603
604    v.truncate(real_size);
605
606    // Safety: convert_latin1_to_utf8 converts the raw bytes to utf8 and the
607    // buffer is the size specified in the documentation, so this should be safe.
608    unsafe { String::from_utf8_unchecked(v) }
609}
610
611/// Converts a `JSString` into a `String`, regardless of used encoding.
612///
613/// ### Safety
614/// `jsstr` must points to a valid `JSString`
615pub unsafe fn jsstr_to_string(cx: &crate::context::JSContext, jsstr: NonNull<JSString>) -> String {
616    if unsafe { JS_DeprecatedStringHasLatin1Chars(jsstr.as_ptr()) } {
617        return latin1_to_string(cx, jsstr);
618    }
619
620    let mut length = 0;
621    let chars = unsafe { JS_GetTwoByteStringCharsAndLength(cx, jsstr.as_ptr(), &mut length) };
622    assert!(!chars.is_null());
623    let char_vec = unsafe { slice::from_raw_parts(chars, length as usize) };
624    String::from_utf16_lossy(char_vec)
625}
626
627/// Converts a `JSString`, encoded in "Latin1" (i.e. U+0000-U+00FF encoded as 0x00-0xFF) into a
628/// `String`.
629///
630/// Use [`latin1_to_string`] if possible as this function will be eventually removed.
631#[deprecated(note = "Use latin1_to_string instead")]
632pub unsafe fn unsafe_latin1_to_string(cx: *mut JSContext, s: NonNull<JSString>) -> String {
633    // while this can break direct invariants of JSContext
634    // it is ok in the current usage of this function and it avoids duplicating the code
635    let cx = crate::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
636    latin1_to_string(&cx, s)
637}
638
639/// Converts a `JSString` into a `String`, regardless of used encoding.
640///
641/// Use [`jsstr_to_string`] if possible as this function will be eventually removed.
642#[deprecated(note = "Use jsstr_to_string instead")]
643pub unsafe fn unsafe_jsstr_to_string(cx: *mut JSContext, jsstr: NonNull<JSString>) -> String {
644    // while this can break direct invariants of JSContext
645    // it is ok in the current usage of this function and it avoids duplicating the code
646    let cx = crate::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
647    jsstr_to_string(&cx, jsstr)
648}
649
650// https://heycam.github.io/webidl/#es-USVString
651impl ToJSValConvertible for str {
652    #[inline]
653    #[deny(unsafe_op_in_unsafe_fn)]
654    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
655        // Spidermonkey will automatically only copy latin1
656        // or similar if the given encoding can be small enough.
657        // So there is no need to distinguish between ascii only or similar.
658        let s = Utf8Chars::from(self);
659        let jsstr = unsafe { JS_NewStringCopyUTF8N(cx, &*s as *const _) };
660        if jsstr.is_null() {
661            panic!("JS String copy routine failed");
662        }
663        unsafe {
664            rval.set(StringValue(&*jsstr));
665        }
666    }
667}
668
669// https://heycam.github.io/webidl/#es-USVString
670impl ToJSValConvertible for String {
671    #[inline]
672    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, rval: MutableHandleValue) {
673        (**self).safe_to_jsval(cx, rval);
674    }
675}
676
677// https://heycam.github.io/webidl/#es-USVString
678impl FromJSValConvertible for String {
679    type Config = ();
680
681    fn safe_from_jsval(
682        cx: &mut crate::context::JSContext,
683        value: HandleValue,
684        _config: Self::Config,
685    ) -> Result<ConversionResult<String>, ()> {
686        let jsstr = unsafe { ToString(cx, value) };
687        let Some(jsstr) = NonNull::new(jsstr) else {
688            debug!("ToString failed");
689            return Err(());
690        };
691        Ok(unsafe { jsstr_to_string(cx, jsstr) }).map(ConversionResult::Success)
692    }
693}
694
695impl<T: ToJSValConvertible> ToJSValConvertible for Option<T> {
696    #[inline]
697    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
698        match self {
699            &Some(ref value) => value.safe_to_jsval(cx, rval),
700            &None => rval.set(NullValue()),
701        }
702    }
703}
704
705impl<T: FromJSValConvertible> FromJSValConvertible for Option<T> {
706    type Config = T::Config;
707
708    fn safe_from_jsval(
709        cx: &mut crate::context::JSContext,
710        value: HandleValue,
711        option: T::Config,
712    ) -> Result<ConversionResult<Option<T>>, ()> {
713        if value.get().is_null_or_undefined() {
714            Ok(ConversionResult::Success(None))
715        } else {
716            Ok(
717                match FromJSValConvertible::safe_from_jsval(cx, value, option)? {
718                    ConversionResult::Success(v) => ConversionResult::Success(Some(v)),
719                    ConversionResult::Failure(v) => ConversionResult::Failure(v),
720                },
721            )
722        }
723    }
724}
725
726impl<T: ToJSValConvertible> ToJSValConvertible for &'_ T {
727    #[inline]
728    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, rval: MutableHandleValue) {
729        (**self).safe_to_jsval(cx, rval)
730    }
731}
732
733impl<T: ToJSValConvertible> ToJSValConvertible for Box<T> {
734    #[inline]
735    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, rval: MutableHandleValue) {
736        (**self).safe_to_jsval(cx, rval)
737    }
738}
739
740impl<T: ToJSValConvertible> ToJSValConvertible for Rc<T> {
741    #[inline]
742    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, rval: MutableHandleValue) {
743        (**self).safe_to_jsval(cx, rval)
744    }
745}
746
747// https://heycam.github.io/webidl/#es-sequence
748impl<T: ToJSValConvertible> ToJSValConvertible for [T] {
749    #[inline]
750    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
751        rooted!(&in(cx) let js_array = unsafe { NewArrayObject1(cx, self.len() as libc::size_t) });
752        assert!(!js_array.handle().is_null());
753
754        rooted!(&in(cx) let mut val = UndefinedValue());
755        for (index, obj) in self.iter().enumerate() {
756            obj.safe_to_jsval(cx, val.handle_mut());
757
758            assert!(unsafe {
759                JS_DefineElement(
760                    cx,
761                    js_array.handle(),
762                    index as u32,
763                    val.handle(),
764                    JSPROP_ENUMERATE as u32,
765                )
766            });
767        }
768
769        rval.set(ObjectValue(js_array.handle().get()));
770    }
771}
772
773// https://heycam.github.io/webidl/#es-sequence
774impl<T: ToJSValConvertible> ToJSValConvertible for Vec<T> {
775    #[inline]
776    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, rval: MutableHandleValue) {
777        <[_]>::safe_to_jsval(self, cx, rval)
778    }
779}
780
781impl<C: Clone, T: FromJSValConvertible<Config = C>> FromJSValConvertible for Vec<T> {
782    type Config = C;
783
784    fn safe_from_jsval(
785        cx: &mut crate::context::JSContext,
786        value: HandleValue,
787        option: C,
788    ) -> Result<ConversionResult<Vec<T>>, ()> {
789        if !value.is_object() {
790            return Ok(ConversionResult::Failure(c"Value is not an object".into()));
791        }
792
793        let mut return_value = vec![];
794        let result = for_of(unsafe { cx.raw_cx() }, value, |iterator_element| {
795            let conversion_result = T::safe_from_jsval(cx, iterator_element, option.clone())
796                .map_err(|_| ForOfIterationFailure::JSFailed)?;
797            return_value.push(match conversion_result {
798                ConversionResult::Success(value) => value,
799                ConversionResult::Failure(error) => {
800                    return Err(ForOfIterationFailure::Other(error));
801                }
802            });
803
804            Ok(ControlFlow::Continue(()))
805        });
806
807        match result {
808            Ok(_) => Ok(ConversionResult::Success(return_value)),
809            Err(ForOfIterationFailure::ValueIsNotIterable) => {
810                Ok(ConversionResult::Failure(c"Value is not iterable".into()))
811            }
812            Err(ForOfIterationFailure::JSFailed) => Err(()),
813            Err(ForOfIterationFailure::Other(error)) => {
814                throw_type_error_safe(cx, error.as_ref());
815                Err(())
816            }
817        }
818    }
819}
820
821// https://heycam.github.io/webidl/#es-object
822impl ToJSValConvertible for *mut JSObject {
823    #[inline]
824    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
825        rval.set(ObjectOrNullValue(*self));
826        maybe_wrap_object_or_null_value(cx, rval);
827    }
828}
829
830// https://heycam.github.io/webidl/#es-object
831impl ToJSValConvertible for ptr::NonNull<JSObject> {
832    #[inline]
833    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
834        rval.set(ObjectValue(self.as_ptr()));
835        unsafe { maybe_wrap_object_value(cx, rval) };
836    }
837}
838
839// https://heycam.github.io/webidl/#es-object
840impl ToJSValConvertible for Heap<*mut JSObject> {
841    #[inline]
842    fn safe_to_jsval(&self, cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
843        rval.set(ObjectOrNullValue(self.get()));
844        maybe_wrap_object_or_null_value(cx, rval);
845    }
846}
847
848// https://heycam.github.io/webidl/#es-object
849impl FromJSValConvertible for *mut JSObject {
850    type Config = ();
851
852    #[inline]
853    fn safe_from_jsval(
854        cx: &mut crate::context::JSContext,
855        value: HandleValue,
856        _option: (),
857    ) -> Result<ConversionResult<*mut JSObject>, ()> {
858        if !value.is_object() {
859            throw_type_error_safe(cx, c"value is not an object");
860            return Err(());
861        }
862
863        unsafe { AssertSameCompartment(cx, value.to_object()) };
864
865        Ok(ConversionResult::Success(value.to_object()))
866    }
867}
868
869impl ToJSValConvertible for *mut JS::Symbol {
870    #[inline]
871    fn safe_to_jsval(&self, _cx: &mut crate::context::JSContext, mut rval: MutableHandleValue) {
872        unsafe { rval.set(SymbolValue(&**self)) };
873    }
874}
875
876impl FromJSValConvertible for *mut JS::Symbol {
877    type Config = ();
878
879    #[inline]
880    fn safe_from_jsval(
881        cx: &mut crate::context::JSContext,
882        value: HandleValue,
883        _option: (),
884    ) -> Result<ConversionResult<*mut JS::Symbol>, ()> {
885        if !value.is_symbol() {
886            throw_type_error_safe(cx, c"value is not a symbol");
887            return Err(());
888        }
889
890        Ok(ConversionResult::Success(value.to_symbol()))
891    }
892}
893
894/// A wrapper type over [`crate::jsapi::UTF8Chars`]. This is created to help transferring
895/// a rust string to mozjs. The inner [`crate::jsapi::UTF8Chars`] can be accessed via the
896/// [`std::ops::Deref`] trait.
897pub struct Utf8Chars<'a> {
898    lt_marker: std::marker::PhantomData<&'a ()>,
899    inner: crate::jsapi::UTF8Chars,
900}
901
902impl<'a> std::ops::Deref for Utf8Chars<'a> {
903    type Target = crate::jsapi::UTF8Chars;
904
905    fn deref(&self) -> &Self::Target {
906        &self.inner
907    }
908}
909
910impl<'a> From<&'a str> for Utf8Chars<'a> {
911    #[allow(unsafe_code)]
912    fn from(value: &'a str) -> Self {
913        use std::marker::PhantomData;
914
915        use crate::jsapi::mozilla::{Range, RangedPtr};
916        use crate::jsapi::UTF8Chars;
917
918        let range = value.as_bytes().as_ptr_range();
919        let range_start = range.start as *mut _;
920        let range_end = range.end as *mut _;
921        let start = RangedPtr {
922            _phantom_0: PhantomData,
923            mPtr: range_start,
924            #[cfg(feature = "debugmozjs")]
925            mRangeStart: range_start,
926            #[cfg(feature = "debugmozjs")]
927            mRangeEnd: range_end,
928        };
929        let end = RangedPtr {
930            _phantom_0: PhantomData,
931            mPtr: range_end,
932            #[cfg(feature = "debugmozjs")]
933            mRangeStart: range_start,
934            #[cfg(feature = "debugmozjs")]
935            mRangeEnd: range_end,
936        };
937        let base = Range {
938            _phantom_0: PhantomData,
939            mStart: start,
940            mEnd: end,
941        };
942        let inner = UTF8Chars { _base: base };
943        Self {
944            lt_marker: PhantomData,
945            inner,
946        }
947    }
948}