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
205
206
207
use crate::{
    function::{Opt, Rest, This},
    qjs, Ctx, Error, FromJs, Result, Value,
};
use std::{ops::Range, slice};

/// The input to rust callback functions containing its arguments.
pub struct Input<'js> {
    ctx: Ctx<'js>,
    this: qjs::JSValue,
    args: &'js [qjs::JSValue],
}

impl<'js> Input<'js> {
    #[inline]
    pub(crate) unsafe fn new_raw(
        ctx: *mut qjs::JSContext,
        this: qjs::JSValue,
        argc: qjs::c_int,
        argv: *mut qjs::JSValue,
    ) -> Self {
        let ctx = Ctx::from_ptr(ctx);
        let args = slice::from_raw_parts(argv, argc as _);
        Self { ctx, this, args }
    }

    /// Returns the input accessor for actually acquiring arguments
    #[inline]
    pub fn access(&self) -> InputAccessor<'_, 'js> {
        InputAccessor {
            input: self,
            arg: 0,
        }
    }

    /// Returns the number of arguments
    #[inline]
    pub fn len(&self) -> usize {
        self.args.len()
    }

    /// Returns whether there are no arguments
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.args.is_empty()
    }
}

/// struct for accessing function arguments
pub struct InputAccessor<'i, 'js> {
    input: &'i Input<'js>,
    arg: usize,
}

impl<'i, 'js> InputAccessor<'i, 'js> {
    /// Get context
    #[inline]
    pub fn ctx(&self) -> Ctx<'js> {
        self.input.ctx
    }

    /// Get value of `this`
    #[inline]
    pub fn this<T>(&self) -> Result<T>
    where
        T: FromJs<'js>,
    {
        let value = unsafe { Value::from_js_value_const(self.input.ctx, self.input.this) };
        T::from_js(self.input.ctx, value)
    }

    /// Get count of rest arguments
    #[inline]
    pub fn len(&self) -> usize {
        self.input.args.len() - self.arg
    }

    /// Get whether there are no more arguments
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get next argument
    #[inline]
    pub fn arg<T>(&mut self) -> Result<T>
    where
        T: FromJs<'js>,
    {
        if self.arg < self.input.args.len() {
            let value = self.input.args[self.arg];
            self.arg += 1;
            let value = unsafe { Value::from_js_value_const(self.input.ctx, value) };
            T::from_js(self.input.ctx, value)
        } else {
            Err(Error::new_from_js_message(
                "uninitialized",
                "value",
                "out of range",
            ))
        }
    }

    /// Get rest arguments
    #[inline]
    pub fn args<T>(&mut self) -> Result<Vec<T>>
    where
        T: FromJs<'js>,
    {
        self.input.args[self.arg..]
            .iter()
            .map(|value| {
                let value = unsafe { Value::from_js_value_const(self.input.ctx, *value) };
                T::from_js(self.input.ctx, value)
            })
            .collect::<Result<Vec<_>>>()
    }

    /// Get something
    #[inline]
    pub fn get<T>(&mut self) -> Result<T>
    where
        T: FromInput<'js>,
    {
        T::from_input(self)
    }
}

pub trait FromInput<'js>: Sized {
    /// Get possible number of arguments
    fn num_args() -> Range<usize>;

    /// Get it from input
    fn from_input<'i>(accessor: &mut InputAccessor<'i, 'js>) -> Result<Self>;
}

/// Get context from input
impl<'js> FromInput<'js> for Ctx<'js> {
    fn num_args() -> Range<usize> {
        0..0
    }

    fn from_input<'i>(accessor: &mut InputAccessor<'i, 'js>) -> Result<Self> {
        Ok(accessor.ctx())
        //Ok(Ctx::from_ptr(accessor.ctx().ctx))
    }
}

/// Get the `this` from input
impl<'js, T> FromInput<'js> for This<T>
where
    T: FromJs<'js>,
{
    fn num_args() -> Range<usize> {
        0..0
    }

    fn from_input<'i>(accessor: &mut InputAccessor<'i, 'js>) -> Result<Self> {
        accessor.this().map(Self)
    }
}

/// Get the next optional argument from input
impl<'js, T> FromInput<'js> for Opt<T>
where
    T: FromJs<'js>,
{
    fn num_args() -> Range<usize> {
        0..1
    }

    fn from_input<'i>(accessor: &mut InputAccessor<'i, 'js>) -> Result<Self> {
        if !accessor.is_empty() {
            accessor.arg().map(Self)
        } else {
            Ok(Self(None))
        }
    }
}

/// Get the rest arguments from input
impl<'js, T> FromInput<'js> for Rest<T>
where
    T: FromJs<'js>,
{
    fn num_args() -> Range<usize> {
        0..usize::MAX
    }

    fn from_input<'i>(accessor: &mut InputAccessor<'i, 'js>) -> Result<Self> {
        accessor.args().map(Self)
    }
}

/// Get the next argument from input
impl<'js, T> FromInput<'js> for T
where
    T: FromJs<'js>,
{
    fn num_args() -> Range<usize> {
        1..1
    }

    fn from_input<'i>(accessor: &mut InputAccessor<'i, 'js>) -> Result<Self> {
        accessor.arg()
    }
}