Skip to main content

quickjs_jit_stdlib/llrt/llrt_utils/
option.rs

1// Vendored from LLRT d9fd873e793c (libs/llrt_utils/src/option.rs); module paths and
2// feature names adapted by scripts/import-stdlib.py.
3use rquickjs::{
4    class::{Trace, Tracer},
5    function::{FromParam, ParamRequirement, ParamsAccessor},
6    Ctx, FromJs, IntoJs, JsLifetime, Result, Type, Value,
7};
8
9/// Helper type for treating an undefined value as None, without treating null as None
10#[derive(Clone)]
11pub struct Undefined<T>(pub Option<T>);
12
13impl<'js, T: FromJs<'js>> FromJs<'js> for Undefined<T> {
14    fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
15        if value.type_of() == Type::Undefined {
16            Ok(Self(None))
17        } else {
18            Ok(Self(Some(FromJs::from_js(ctx, value)?)))
19        }
20    }
21}
22
23impl<'js, T: IntoJs<'js>> IntoJs<'js> for Undefined<T> {
24    fn into_js(self, ctx: &Ctx<'js>) -> Result<Value<'js>> {
25        match self.0 {
26            None => Ok(Value::new_undefined(ctx.clone())),
27            Some(val) => val.into_js(ctx),
28        }
29    }
30}
31
32impl<T> Default for Undefined<T> {
33    fn default() -> Self {
34        Self(None)
35    }
36}
37
38unsafe impl<'js, T: JsLifetime<'js>> JsLifetime<'js> for Undefined<T> {
39    type Changed<'to> = Undefined<T::Changed<'to>>;
40}
41
42impl<'js, T: Trace<'js>> Trace<'js> for Undefined<T> {
43    fn trace<'a>(&self, tracer: Tracer<'a, 'js>) {
44        self.0.trace(tracer)
45    }
46}
47
48/// Helper type for converting an None into null instead of undefined.
49/// Needed while rquickjs::function::Null has no IntoJs implementation
50#[derive(Clone)]
51pub struct Null<T>(pub Option<T>);
52
53impl<'js, T: FromJs<'js>> FromJs<'js> for Null<T> {
54    fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
55        if value.type_of() == Type::Null {
56            Ok(Self(None))
57        } else {
58            Ok(Self(Some(FromJs::from_js(ctx, value)?)))
59        }
60    }
61}
62
63impl<'js, T: IntoJs<'js>> IntoJs<'js> for Null<T> {
64    fn into_js(self, ctx: &Ctx<'js>) -> Result<Value<'js>> {
65        match self.0 {
66            None => Ok(Value::new_null(ctx.clone())),
67            Some(val) => val.into_js(ctx),
68        }
69    }
70}
71
72unsafe impl<'js, T: JsLifetime<'js>> JsLifetime<'js> for Null<T> {
73    type Changed<'to> = Null<T::Changed<'to>>;
74}
75
76impl<'js, T: Trace<'js>> Trace<'js> for Null<T> {
77    fn trace<'a>(&self, tracer: Tracer<'a, 'js>) {
78        self.0.trace(tracer)
79    }
80}
81
82/// Helper type for accepting no value, or null, but considering undefined as a value
83pub struct NullableOpt<T>(pub Option<T>);
84
85impl<'js, T: FromJs<'js>> FromParam<'js> for NullableOpt<T> {
86    fn param_requirement() -> ParamRequirement {
87        ParamRequirement::optional()
88    }
89
90    fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<Self> {
91        if !params.is_empty() {
92            let arg = params.arg();
93            if arg.is_null() {
94                Ok(NullableOpt(None))
95            } else {
96                let ctx = params.ctx().clone();
97                Ok(NullableOpt(Some(T::from_js(&ctx, arg)?)))
98            }
99        } else {
100            Ok(NullableOpt(None))
101        }
102    }
103}