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
use super::FromJs;
use crate::{Array, Ctx, Error, Function, Object, Result, String, Value};
use std::string::String as StdString;

impl<'js> FromJs<'js> for Value<'js> {
    fn from_js(_: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        Ok(value)
    }
}

impl<'js> FromJs<'js> for StdString {
    fn from_js(_ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        match value {
            Value::String(x) => Ok(x.to_string()?),
            x => Err(Error::FromJsConversion {
                from: x.type_name(),
                to: "string",
                message: None,
            }),
        }
    }
}

impl<'js> FromJs<'js> for i32 {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        let type_name = value.type_name();
        ctx.coerce_i32(value).map_err(|e| {
            if e.is_exception() {
                Error::FromJsConversion {
                    from: type_name,
                    to: "i32",
                    message: Some(format!("{}", e)),
                }
            } else {
                e
            }
        })
    }
}

impl<'js> FromJs<'js> for u64 {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        let type_name = value.type_name();
        ctx.coerce_u64(value).map_err(|e| {
            if e.is_exception() {
                Error::FromJsConversion {
                    from: type_name,
                    to: "u32",
                    message: Some(format!("{}", e)),
                }
            } else {
                e
            }
        })
    }
}

impl<'js> FromJs<'js> for f64 {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        let type_name = value.type_name();
        ctx.coerce_f64(value).map_err(|e| {
            if e.is_exception() {
                Error::FromJsConversion {
                    from: type_name,
                    to: "f64",
                    message: Some(format!("{}", e)),
                }
            } else {
                e
            }
        })
    }
}

impl<'js> FromJs<'js> for bool {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        let type_name = value.type_name();
        ctx.coerce_bool(value).map_err(|e| {
            if e.is_exception() {
                Error::FromJsConversion {
                    from: type_name,
                    to: "bool",
                    message: Some(format!("{}", e)),
                }
            } else {
                e
            }
        })
    }
}

impl<'js> FromJs<'js> for String<'js> {
    fn from_js(_: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        match value {
            Value::String(x) => Ok(x),
            x => Err(Error::FromJsConversion {
                from: x.type_name(),
                to: "string",
                message: None,
            }),
        }
    }
}

impl<'js> FromJs<'js> for Object<'js> {
    fn from_js(_: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        match value {
            Value::Object(x) => Ok(x),
            Value::Array(x) => Ok(x.into_object()),
            Value::Function(x) => Ok(x.into_object()),
            x => Err(Error::FromJsConversion {
                from: x.type_name(),
                to: "object",
                message: None,
            }),
        }
    }
}

impl<'js> FromJs<'js> for Array<'js> {
    fn from_js(_: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        match value {
            Value::Array(x) => Ok(x),
            x => Err(Error::FromJsConversion {
                from: x.type_name(),
                to: "array",
                message: None,
            }),
        }
    }
}

impl<'js> FromJs<'js> for Function<'js> {
    fn from_js(_: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        match value {
            Value::Function(x) => Ok(x),
            x => Err(Error::FromJsConversion {
                from: x.type_name(),
                to: "function",
                message: None,
            }),
        }
    }
}

impl<'js> FromJs<'js> for () {
    fn from_js(_: Ctx<'js>, _: Value<'js>) -> Result<Self> {
        Ok(())
    }
}

impl<'js, T: FromJs<'js>> FromJs<'js> for Vec<T> {
    fn from_js(_: Ctx<'js>, v: Value<'js>) -> Result<Self> {
        let x = match v {
            Value::Array(x) => x,
            x => {
                return Err(Error::FromJsConversion {
                    from: x.type_name(),
                    to: "vector",
                    message: None,
                });
            }
        };
        let len = x.len();
        let mut res = Vec::new();
        for i in 0..len {
            res.push(x.get(i as u32)?);
        }
        Ok(res)
    }
}