ul-next 0.5.4

Ultralight Rust bindings
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use core::fmt;
use std::ops::Deref;

use super::{JSContext, JSObject, JSString, JSTypedArray, JSTypedArrayType};

/// A trait for converting a type into a [`JSValue`].
///
/// Types implementing this trait are [`JSValue`], we are wrapping them in other types
/// to provide specific functionality.
pub trait AsJSValue<'a>: Deref<Target = JSValue<'a>> + AsRef<JSValue<'a>> {
    fn into_value(self) -> JSValue<'a>;
    fn as_value(&self) -> &JSValue<'a>;
}

/// An enum identifying the type of a [`JSValue`].
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum JSType {
    Undefined = ul_sys::JSType_kJSTypeUndefined,
    Null = ul_sys::JSType_kJSTypeNull,
    Boolean = ul_sys::JSType_kJSTypeBoolean,
    Number = ul_sys::JSType_kJSTypeNumber,
    String = ul_sys::JSType_kJSTypeString,
    Object = ul_sys::JSType_kJSTypeObject,
    Symbol = ul_sys::JSType_kJSTypeSymbol,
}

/// A JavaScript value.
///
/// This is the basic type for managing values in JavaScriptCore. It can represent
/// any JavaScript value, including `undefined`, `null`, booleans, numbers, strings,
/// symbols, objects, and arrays.
pub struct JSValue<'a> {
    pub(crate) internal: ul_sys::JSValueRef,
    pub(crate) ctx: &'a JSContext,
}

impl<'a> JSValue<'a> {
    pub(crate) fn from_raw(ctx: &'a JSContext, value: ul_sys::JSValueRef) -> Self {
        assert!(!value.is_null());

        Self {
            internal: value,
            ctx,
        }
    }

    pub(crate) fn copy_from_raw(ctx: &'a JSContext, value: ul_sys::JSValueRef) -> Self {
        assert!(!value.is_null());

        unsafe {
            ctx.lib.ultralight().JSValueProtect(ctx.internal, value);
        }

        Self::from_raw(ctx, value)
    }

    pub(crate) fn into_raw(self) -> ul_sys::JSValueRef {
        // add protection so that the `Drop` impl doesn't free while we need it
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueProtect(self.ctx.internal, self.internal);
        }

        self.internal
    }
}

impl<'a> JSValue<'a> {
    /// Creates a Javascript `undefined` value.
    pub fn new_undefined(ctx: &'a JSContext) -> Self {
        let value = unsafe { ctx.lib.ultralight().JSValueMakeUndefined(ctx.internal) };

        Self {
            internal: value,
            ctx,
        }
    }

    /// Creates a Javascript `null` value.
    pub fn new_null(ctx: &'a JSContext) -> Self {
        let value = unsafe { ctx.lib.ultralight().JSValueMakeNull(ctx.internal) };

        Self {
            internal: value,
            ctx,
        }
    }

    /// Creates a Javascript boolean value.
    pub fn new_boolean(ctx: &'a JSContext, value: bool) -> Self {
        let value = unsafe { ctx.lib.ultralight().JSValueMakeBoolean(ctx.internal, value) };

        Self {
            internal: value,
            ctx,
        }
    }

    /// Creates a Javascript numeric value.
    pub fn new_number(ctx: &'a JSContext, value: f64) -> Self {
        let value = unsafe { ctx.lib.ultralight().JSValueMakeNumber(ctx.internal, value) };

        Self {
            internal: value,
            ctx,
        }
    }

    /// Creates a unique Javascript symbol object.
    ///
    /// `description` is a string that describes the symbol.
    pub fn new_symbol(ctx: &'a JSContext, description: &str) -> Self {
        let value = JSString::new(ctx.lib.clone(), description);

        let value = unsafe {
            ctx.lib
                .ultralight()
                .JSValueMakeSymbol(ctx.internal, value.internal)
        };

        Self {
            internal: value,
            ctx,
        }
    }

    /// Converts a Javascript string object to a Javascript value.
    pub fn from_jsstring(ctx: &'a JSContext, value: JSString) -> Self {
        let value = unsafe {
            ctx.lib
                .ultralight()
                .JSValueMakeString(ctx.internal, value.internal)
        };

        Self {
            internal: value,
            ctx,
        }
    }

    /// Creates a Javascript string value from a Rust string.
    ///
    /// If you have already `JSString` object, use [`JSValue::from_jsstring`] instead.
    pub fn new_string(ctx: &'a JSContext, value: &str) -> Self {
        let value = JSString::new(ctx.lib.clone(), value);

        let value = unsafe {
            ctx.lib
                .ultralight()
                .JSValueMakeString(ctx.internal, value.internal)
        };

        Self {
            internal: value,
            ctx,
        }
    }

    /// Creates a JavaScript value from a JSON formatted string.
    ///
    /// Returns [`None`] if the JSON string is invalid.
    pub fn new_from_json(ctx: &'a JSContext, value: &str) -> Option<Self> {
        let value = JSString::new(ctx.lib.clone(), value);

        let value = unsafe {
            ctx.lib
                .ultralight()
                .JSValueMakeFromJSONString(ctx.internal, value.internal)
        };

        if value.is_null() {
            None
        } else {
            Some(Self {
                internal: value,
                ctx,
            })
        }
    }
}

impl JSValue<'_> {
    /// Returns a JavaScript value's type.
    pub fn get_type(&self) -> JSType {
        let ty = unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueGetType(self.ctx.internal, self.internal)
        };

        match ty {
            ul_sys::JSType_kJSTypeUndefined => JSType::Undefined,
            ul_sys::JSType_kJSTypeNull => JSType::Null,
            ul_sys::JSType_kJSTypeBoolean => JSType::Boolean,
            ul_sys::JSType_kJSTypeNumber => JSType::Number,
            ul_sys::JSType_kJSTypeString => JSType::String,
            ul_sys::JSType_kJSTypeObject => JSType::Object,
            ul_sys::JSType_kJSTypeSymbol => JSType::Symbol,
            _ => panic!("Unknown JSValue type: {}", ty),
        }
    }

    /// Returns `true` if the value is `undefined`.
    pub fn is_undefined(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsUndefined(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value is `null`.
    pub fn is_null(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsNull(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value is a JavaScript date object.
    pub fn is_date(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsDate(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value is a JavaScript array.
    pub fn is_array(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsArray(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value's type is the symbol type
    pub fn is_symbol(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsSymbol(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value is an object.
    pub fn is_object(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsObject(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value is a string.
    pub fn is_string(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsString(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value is a number.
    pub fn is_number(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsNumber(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value is a boolean.
    pub fn is_boolean(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueIsBoolean(self.ctx.internal, self.internal)
        }
    }

    /// Returns `true` if the value is a Typed Array.
    pub fn is_typed_array(&self) -> bool {
        // Note: we are creating the object `JSTypedArray` here to check if the value is a typed
        // array, i.e. it shouldn't be used to call any other `JSTypedArray` methods.
        let typed_array = JSTypedArray {
            value: Self {
                internal: self.internal,
                ctx: self.ctx,
            },
        };

        match typed_array.ty().ok() {
            None | Some(JSTypedArrayType::None) => false,
            Some(_) => true,
        }
    }
}

impl<'a> JSValue<'a> {
    /// Converts a JavaScript value to object.
    ///
    /// Returns an [`Err`] if an exception is thrown.
    pub fn as_object(&self) -> Result<JSObject<'a>, JSValue<'a>> {
        let mut exception = std::ptr::null();

        let result = unsafe {
            self.ctx.lib.ultralight().JSValueToObject(
                self.ctx.internal,
                self.internal,
                &mut exception,
            )
        };

        if !exception.is_null() {
            Err(JSValue::from_raw(self.ctx, exception))
        } else if result.is_null() {
            Err(JSValue::new_string(
                self.ctx,
                "Failed to convert value to object",
            ))
        } else {
            Ok(JSObject {
                value: JSValue::from_raw(self.ctx, result),
            })
        }
    }

    /// Converts a JavaScript value to string.
    ///
    /// Returns an [`Err`] if an exception is thrown.
    pub fn as_string(&self) -> Result<JSString, JSValue<'a>> {
        let mut exception = std::ptr::null();

        let result = unsafe {
            self.ctx.lib.ultralight().JSValueToStringCopy(
                self.ctx.internal,
                self.internal,
                &mut exception,
            )
        };

        if !exception.is_null() {
            Err(JSValue::from_raw(self.ctx, exception))
        } else if result.is_null() {
            Err(JSValue::new_string(
                self.ctx,
                "Failed to convert value to string",
            ))
        } else {
            Ok(JSString::copy_from_raw(self.ctx.lib.clone(), result))
        }
    }

    /// Converts a JavaScript value to number.
    ///
    /// Returns an [`Err`] if an exception is thrown.
    pub fn as_number(&self) -> Result<f64, JSValue<'a>> {
        let mut exception = std::ptr::null();

        let result = unsafe {
            self.ctx.lib.ultralight().JSValueToNumber(
                self.ctx.internal,
                self.internal,
                &mut exception,
            )
        };

        if !exception.is_null() {
            Err(JSValue::from_raw(self.ctx, exception))
        } else {
            Ok(result)
        }
    }

    /// Converts a JavaScript value to boolean.
    pub fn as_boolean(&self) -> bool {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueToBoolean(self.ctx.internal, self.internal)
        }
    }

    /// Converts a JavaScript value to a typed array.
    ///
    /// Returns an [`Err`] if the value is not a typed array, or if an exception is thrown.
    pub fn as_typed_array(&self) -> Result<JSTypedArray<'a>, JSValue<'a>> {
        if self.is_typed_array() {
            let object = self.as_object()?;

            Ok(JSTypedArray {
                value: object.value,
            })
        } else {
            Err(JSValue::new_string(self.ctx, "Value is not a typed array"))
        }
    }

    /// Converts a JavaScript value to JSON serialized representation of a JS value.
    ///
    /// Returns an [`Err`] if an exception is thrown.
    pub fn to_json_string(&self) -> Result<JSString, JSValue<'a>> {
        let mut exception = std::ptr::null();

        let result = unsafe {
            self.ctx.lib.ultralight().JSValueCreateJSONString(
                self.ctx.internal,
                self.internal,
                0,
                &mut exception,
            )
        };

        if !exception.is_null() {
            Err(JSValue::from_raw(self.ctx, exception))
        } else if result.is_null() {
            Err(JSValue::new_string(
                self.ctx,
                "Failed to convert value to JSON string",
            ))
        } else {
            Ok(JSString::from_raw(self.ctx.lib.clone(), result))
        }
    }
}

impl Clone for JSValue<'_> {
    fn clone(&self) -> Self {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueProtect(self.ctx.internal, self.internal)
        };

        Self {
            internal: self.internal,
            ctx: self.ctx,
        }
    }
}

impl fmt::Debug for JSValue<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JSValue")
            .field("type", &self.get_type())
            .field("repr", &self.to_json_string())
            .finish()
    }
}

impl Drop for JSValue<'_> {
    fn drop(&mut self) {
        unsafe {
            self.ctx
                .lib
                .ultralight()
                .JSValueUnprotect(self.ctx.internal, self.internal);
        }
    }
}