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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
use rust_jsc_sys::{
    JSContextRef, JSObjectRef, JSValueCreateJSONString, JSValueGetType, JSValueIsArray, JSValueIsBoolean, JSValueIsDate, JSValueIsEqual, JSValueIsInstanceOfConstructor, JSValueIsNull, JSValueIsNumber, JSValueIsObject, JSValueIsObjectOfClass, JSValueIsStrictEqual, JSValueIsString, JSValueIsSymbol, JSValueIsUndefined, JSValueMakeBoolean, JSValueMakeFromJSONString, JSValueMakeNull, JSValueMakeNumber, JSValueMakeString, JSValueMakeSymbol, JSValueMakeUndefined, JSValueProtect, JSValueRef, JSValueToBoolean, JSValueToNumber, JSValueToObject, JSValueToStringCopy, JSValueUnprotect
};

use crate::{
    JSClass, JSContext, JSError, JSObject, JSResult, JSString, JSValue, JSValueType,
};

impl JSValue {
    /// Creates a new `JSValue` object.
    pub fn new(inner: JSValueRef, ctx: JSContextRef) -> Self {
        Self { inner, ctx }
    }

    /// Creates a JavaScript boolean value.
    ///
    /// # Arguments
    /// * `value` - The boolean value.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::boolean(&ctx, true);
    /// assert!(value.is_boolean());
    /// ```
    ///
    /// # Returns
    /// A JavaScript boolean value.
    pub fn boolean(ctx: &JSContext, value: bool) -> JSValue {
        let inner = unsafe { JSValueMakeBoolean(ctx.inner, value) };
        Self::new(inner, ctx.inner)
    }

    /// Creates a JavaScript undefined value.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::undefined(&ctx);
    /// assert!(value.is_undefined());
    /// ```
    ///
    /// # Returns
    /// A JavaScript undefined value.
    pub fn undefined(ctx: &JSContext) -> JSValue {
        let inner = unsafe { JSValueMakeUndefined(ctx.inner) };
        Self::new(inner, ctx.inner)
    }

    /// Creates a JavaScript null value.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::null(&ctx);
    /// assert!(value.is_null());
    /// ```
    ///
    /// # Returns
    /// A JavaScript null value.
    pub fn null(ctx: &JSContext) -> JSValue {
        let inner = unsafe { JSValueMakeNull(ctx.inner) };
        Self::new(inner, ctx.inner)
    }

    /// Creates a JavaScript number value from a double-precision floating-point number.
    ///
    /// # Arguments
    /// * `value` - The number to use as the value.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::number(&ctx, 42.0);
    /// assert!(value.is_number());
    /// ```
    ///
    /// # Returns
    /// A JavaScript number value.
    pub fn number(ctx: &JSContext, value: f64) -> JSValue {
        let inner = unsafe { JSValueMakeNumber(ctx.inner, value) };
        Self::new(inner, ctx.inner)
    }

    /// Creates a JavaScript string value from a string.
    ///
    /// # Arguments
    /// * `value` - The string to use as the value.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::string(&ctx, "Hello, World!");
    /// assert!(value.is_string());
    /// ```
    ///
    /// # Returns
    /// A JavaScript string value.
    pub fn string(ctx: &JSContext, value: impl Into<JSString>) -> JSValue {
        let inner = unsafe { JSValueMakeString(ctx.inner, value.into().inner) };
        Self::new(inner, ctx.inner)
    }

    /// Creates a JavaScript symbol value.
    ///
    /// # Arguments
    /// * `description` - A description of the symbol.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::symbol(&ctx, "symbol");
    /// assert!(value.is_symbol());
    /// ```
    ///
    /// # Returns
    /// A JavaScript symbol value.
    pub fn symbol(ctx: &JSContext, description: impl Into<JSString>) -> JSValue {
        let inner = unsafe { JSValueMakeSymbol(ctx.inner, description.into().inner) };
        Self::new(inner, ctx.inner)
    }

    /// Creates a JavaScript value from a JSON serialized string.
    ///
    /// # Arguments
    /// * `string` - The JSON serialized string.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::from_json(&ctx, r#"{"key": "value"}"#);
    /// assert!(value.is_object());
    /// ```
    ///
    /// # Returns
    /// A JavaScript value, or null if the input is not valid JSON.
    pub fn from_json(ctx: &JSContext, string: impl Into<JSString>) -> JSValue {
        let string = string.into();
        let inner = unsafe { JSValueMakeFromJSONString(ctx.inner, string.inner) };
        Self::new(inner, ctx.inner)
    }

    /// Creates a JavaScript string containing the JSON serialized representation of a JS value.
    ///
    /// # Arguments
    /// * `indent` - The number of spaces to indent when nesting.  If 0, the resulting JSON will not contains newlines. The size of the indent is clamped to 10 spaces.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = ctx.evaluate_script("({ key: 'value' })", None).unwrap();
    /// assert!(value.as_json_string(2).is_ok());
    /// ```
    ///
    /// # Returns
    /// A JSString with the result of serialization, or JSError if an exception occurs.
    pub fn as_json_string(&self, indent: u32) -> JSResult<JSString> {
        let mut exception: JSValueRef = std::ptr::null_mut();
        let string = unsafe {
            JSValueCreateJSONString(self.ctx, self.inner, indent, &mut exception)
        };

        if !exception.is_null() {
            let value = JSValue::new(exception, self.ctx);
            return Err(JSError::from(value));
        }

        Ok(string.into())
    }

    /// Converts a JavaScript value to a js string and returns the resulting js string.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::string(&ctx, "Hello, World!");
    /// assert!(value.as_string().is_ok());
    /// ```
    ///
    /// # Returns
    /// A JavaScript string.
    pub fn as_string(&self) -> JSResult<JSString> {
        let mut exception: JSValueRef = std::ptr::null_mut();
        let string = unsafe { JSValueToStringCopy(self.ctx, self.inner, &mut exception) };

        if !exception.is_null() {
            let value = JSValue::new(exception, self.ctx);
            return Err(JSError::from(value));
        }

        Ok(string.into())
    }

    /// Converts a JavaScript value to an object and returns the resulting object.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = ctx.evaluate_script("({ key: 'value' })", None).unwrap();
    /// assert!(value.as_object().is_ok());
    /// ```
    ///
    /// # Returns
    /// A JavaScript object.
    pub fn as_object(&self) -> JSResult<JSObject> {
        let mut exception: JSValueRef = std::ptr::null_mut();
        let object = unsafe { JSValueToObject(self.ctx, self.inner, &mut exception) };

        if !exception.is_null() {
            let value = JSValue::new(exception, self.ctx);
            return Err(JSError::from(value));
        }

        Ok(JSObject::from_ref(object, self.ctx))
    }

    /// Converts a JavaScript value to boolean and returns the resulting boolean.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::boolean(&ctx, true);
    /// assert_eq!(value.as_boolean(), true);
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn as_boolean(&self) -> bool {
        unsafe { JSValueToBoolean(self.ctx, self.inner) }
    }

    /// Converts a JavaScript value to number and returns the resulting number.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::number(&ctx, 42.0);
    /// assert_eq!(value.as_number().unwrap(), 42.0);
    /// ```
    ///
    /// # Returns
    /// A number value.
    pub fn as_number(&self) -> JSResult<f64> {
        let mut exception: JSValueRef = std::ptr::null_mut();
        let number = unsafe { JSValueToNumber(self.ctx, self.inner, &mut exception) };

        if !exception.is_null() {
            let value = JSValue::new(exception, self.ctx);
            return Err(JSError::from(value));
        }

        Ok(number)
    }

    /// Checks if the value is undefined.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::undefined(&ctx);
    /// assert!(value.is_undefined());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_undefined(&self) -> bool {
        unsafe { JSValueIsUndefined(self.ctx, self.inner) }
    }

    /// Checks if the value is null.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::null(&ctx);
    /// assert!(value.is_null());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_null(&self) -> bool {
        unsafe { JSValueIsNull(self.ctx, self.inner) }
    }

    /// Checks if the value is a boolean.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::boolean(&ctx, true);
    /// assert!(value.is_boolean());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_boolean(&self) -> bool {
        unsafe { JSValueIsBoolean(self.ctx, self.inner) }
    }

    /// Checks if the value is a number.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::number(&ctx, 42.0);
    /// assert!(value.is_number());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_number(&self) -> bool {
        unsafe { JSValueIsNumber(self.ctx, self.inner) }
    }

    /// Checks if the value is a string.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::string(&ctx, "Hello, World!");
    /// assert!(value.is_string());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_string(&self) -> bool {
        unsafe { JSValueIsString(self.ctx, self.inner) }
    }

    /// Checks if the value is an object.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = ctx.evaluate_script("({ key: 'value' })", None).unwrap();
    /// assert!(value.is_object());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_object(&self) -> bool {
        unsafe { JSValueIsObject(self.ctx, self.inner) }
    }

    /// Checks if the value is a symbol.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::symbol(&ctx, "symbol");
    /// assert!(value.is_symbol());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_symbol(&self) -> bool {
        unsafe { JSValueIsSymbol(self.ctx, self.inner) }
    }

    /// Checks if the value is an array.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = ctx.evaluate_script("[1, 2, 3]", None).unwrap();
    /// assert!(value.is_array());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_array(&self) -> bool {
        unsafe { JSValueIsArray(self.ctx, self.inner) }
    }

    /// Checks if the value is a date.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = ctx.evaluate_script("new Date()", None).unwrap();
    /// assert!(value.is_date());
    /// ```
    ///
    /// # Returns
    /// A boolean value.
    pub fn is_date(&self) -> bool {
        unsafe { JSValueIsDate(self.ctx, self.inner) }
    }

    /// Tests whether a JavaScript value is an object constructed by a given constructor,
    /// as compared by the JS instanceof operator.
    ///
    /// # Arguments
    /// * `constructor` - The constructor to test against.
    ///
    /// # Returns
    /// true if value is an object constructed by constructor,
    /// as compared by the JS instanceof operator, otherwise false.
    pub fn is_instance_of(&self, constructor: &JSObject) -> JSResult<bool> {
        let mut exception: JSValueRef = std::ptr::null_mut();
        let result = unsafe {
            JSValueIsInstanceOfConstructor(
                self.ctx,
                self.inner,
                constructor.inner,
                &mut exception,
            )
        };

        if !exception.is_null() {
            let value = JSValue::new(exception, self.ctx);
            return Err(JSError::from(value));
        }

        Ok(result)
    }

    /// Tests whether an object is an instance of a particular class.
    ///
    /// # Arguments
    /// * `class` - The class to test against.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let class = JSClass::builder("Test").build().unwrap();
    /// let value = class.object::<i32>(&ctx, Some(Box::new(42)));
    /// assert!(value.is_object_of_class(&class).unwrap());
    /// ```
    ///
    /// # Returns
    /// true if the object is an instance of class, otherwise false.
    pub fn is_object_of_class(&self, class: &JSClass) -> JSResult<bool> {
        return Ok(unsafe { JSValueIsObjectOfClass(self.ctx, self.inner, class.inner) });
    }

    /// Tests whether two JavaScript values are equal, as compared by the JS == operator.
    ///
    /// # Arguments
    /// * `other` - The value to compare.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value1 = JSValue::number(&ctx, 42.0);
    /// let value2 = JSValue::number(&ctx, 42.0);
    /// assert!(value1.is_equal(&value2).unwrap());
    /// ```
    ///
    /// # Returns
    /// true if the values are equal, otherwise false.
    pub fn is_equal(&self, other: &JSValue) -> JSResult<bool> {
        let mut exception: JSValueRef = std::ptr::null_mut();
        let result =
            unsafe { JSValueIsEqual(self.ctx, self.inner, other.inner, &mut exception) };

        if !exception.is_null() {
            let value = JSValue::new(exception, self.ctx);
            return Err(JSError::from(value));
        }

        Ok(result)
    }

    /// Protects a JavaScript value from garbage collection.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::number(&ctx, 42.0);
    /// value.protect();
    /// ```
    ///
    /// # Note
    /// You must call `unprotect` when you no longer need the value.
    /// Use this method when you want to store a JSValue in a global or on the heap,
    /// where the garbage collector will not be able to discover your reference to it.\n
    /// A value may be protected multiple times and must be unprotected an equal number of times
    /// before becoming eligible for garbage collection.
    pub fn protect(&self) {
        unsafe { JSValueProtect(self.ctx, self.inner) };
    }

    /// Unprotects a JavaScript value from garbage collection.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::number(&ctx, 42.0);
    /// value.protect();
    /// value.unprotect();
    /// ```
    ///
    /// # Note
    /// A value may be protected multiple times and must be unprotected an\n
    /// equal number of times before becoming eligible for garbage collection.
    pub fn unprotect(&self) {
        unsafe { JSValueUnprotect(self.ctx, self.inner) };
    }

    /// Returns the type of a JavaScript value.
    ///
    /// # Examples
    /// ```
    /// use rust_jsc::*;
    ///
    /// let ctx = JSContext::new();
    /// let value = JSValue::number(&ctx, 42.0);
    /// assert_eq!(value.get_type(), JSValueType::Number);
    /// ```
    ///
    /// # Returns
    /// The type of the JavaScript value.
    pub fn get_type(&self) -> JSValueType {
        let type_ = unsafe { JSValueGetType(self.ctx, self.inner) };
        JSValueType::from_js_type(type_)
    }
}

/// This is equivalent to `===` in JavaScript.
impl PartialEq for JSValue {
    fn eq(&self, other: &JSValue) -> bool {
        unsafe { JSValueIsStrictEqual(self.ctx, self.inner, other.inner) }
    }
}

impl From<JSValue> for JSValueRef {
    fn from(val: JSValue) -> Self {
        val.inner
    }
}

impl From<JSValue> for JSObjectRef {
    fn from(value: JSValue) -> Self {
        value.inner as *mut _
    }
}

#[cfg(test)]
mod tests {
    use crate::{JSObject, JSValue};

    #[test]
    fn test_boolean() {
        let ctx = crate::JSContext::new();
        let value = JSValue::boolean(&ctx, true);
        assert!(value.is_boolean());
    }

    #[test]
    fn test_undefined() {
        let ctx = crate::JSContext::new();
        let value = JSValue::undefined(&ctx);
        assert!(value.is_undefined());
    }

    #[test]
    fn test_null() {
        let ctx = crate::JSContext::new();
        let value = JSValue::null(&ctx);
        assert!(value.is_null());
    }

    #[test]
    fn test_number() {
        let ctx = crate::JSContext::new();
        let value = JSValue::number(&ctx, 42.0);
        assert!(value.is_number());
    }

    #[test]
    fn test_string() {
        let ctx = crate::JSContext::new();
        let value = JSValue::string(&ctx, "Hello, World!");
        assert!(value.is_string());
    }

    #[test]
    fn test_symbol() {
        let ctx = crate::JSContext::new();
        let value = JSValue::symbol(&ctx, "symbol");
        assert!(value.is_symbol());
    }

    #[test]
    fn test_from_json() {
        let ctx = crate::JSContext::new();
        let value = JSValue::from_json(&ctx, r#"{"key": "value"}"#);
        assert!(value.is_object());
    }

    #[test]
    fn test_as_json_string() {
        let ctx = crate::JSContext::new();
        let value = ctx.evaluate_script("({ key: 'value' })", None).unwrap();
        assert!(value.as_json_string(2).is_ok());
    }

    #[test]
    fn test_as_string() {
        let ctx = crate::JSContext::new();
        let value = JSValue::string(&ctx, "Hello, World!");
        assert!(value.as_string().is_ok());
    }

    #[test]
    fn test_as_object() {
        let ctx = crate::JSContext::new();
        let value: JSValue = JSObject::new(&ctx).into();
        assert!(value.as_object().is_ok());
    }

    #[test]
    fn test_as_boolean() {
        let ctx = crate::JSContext::new();
        let value = JSValue::boolean(&ctx, true);
        assert_eq!(value.as_boolean(), true);
    }

    #[test]
    fn test_as_number() {
        let ctx = crate::JSContext::new();
        let value = JSValue::number(&ctx, 42.0);
        assert_eq!(value.as_number().unwrap(), 42.0);
    }

    #[test]
    fn test_is_undefined() {
        let ctx = crate::JSContext::new();
        let value = JSValue::undefined(&ctx);
        assert!(value.is_undefined());
    }

    #[test]
    fn test_is_null() {
        let ctx = crate::JSContext::new();
        let value = JSValue::null(&ctx);
        assert!(value.is_null());
    }

    #[test]
    fn test_is_boolean() {
        let ctx = crate::JSContext::new();
        let value = JSValue::boolean(&ctx, true);
        assert!(value.is_boolean());
    }

    #[test]
    fn test_is_number() {
        let ctx = crate::JSContext::new();
        let value = JSValue::number(&ctx, 42.0);
        assert!(value.is_number());
    }

    #[test]
    fn test_is_string() {
        let ctx = crate::JSContext::new();
        let value = JSValue::string(&ctx, "Hello, World!");
        assert!(value.is_string());
    }

    #[test]
    fn test_is_object() {
        let ctx = crate::JSContext::new();
        let value: JSValue = ctx.evaluate_script("({ key: 'value' })", None).unwrap();
        assert!(value.is_object());
    }

    #[test]
    fn test_is_symbol() {
        let ctx = crate::JSContext::new();
        let value = JSValue::symbol(&ctx, "symbol");
        assert!(value.is_symbol());
    }

    #[test]
    fn test_is_array() {
        let ctx = crate::JSContext::new();
        let value: JSValue = ctx.evaluate_script("[1, 2, 3]", None).unwrap();
        assert!(value.is_array());
    }

    #[test]
    fn test_is_date() {
        let ctx = crate::JSContext::new();
        let value: JSValue = ctx.evaluate_script("new Date()", None).unwrap();
        assert!(value.is_date());
    }

    // #[test]
    // fn test_is_instance_of() {
    //     let ctx = crate::JSContext::new();
    //     let value = JSValue::object(&ctx);
    //     let constructor = ctx.evaluate_script("Object").unwrap();
    //     assert!(value.is_instance_of(&constructor).is_ok());
    // }

    #[test]
    fn test_is_object_of_class() {
        let ctx = crate::JSContext::new();
        let class = crate::JSClass::builder("Test").build().unwrap();
        let value = class.object::<i32>(&ctx, Some(Box::new(42)));
        assert!(value.is_object_of_class(&class).unwrap());
    }

    #[test]
    fn test_is_equal() {
        let ctx = crate::JSContext::new();
        let value1 = JSValue::number(&ctx, 42.0);
        let value2 = JSValue::number(&ctx, 42.0);
        assert!(value1.is_equal(&value2).unwrap());
    }

    #[test]
    fn test_protect() {
        let ctx = crate::JSContext::new();
        let value = JSValue::number(&ctx, 42.0);
        value.protect();
    }

    #[test]
    fn test_unprotect() {
        let ctx = crate::JSContext::new();
        let value = JSValue::number(&ctx, 42.0);
        value.protect();
        value.unprotect();
    }

    #[test]
    fn test_get_type() {
        let ctx = crate::JSContext::new();
        let value = JSValue::number(&ctx, 42.0);
        assert_eq!(value.get_type(), crate::JSValueType::Number);
    }

    #[test]
    fn test_partial_eq() {
        let ctx = crate::JSContext::new();
        let value1 = JSValue::number(&ctx, 42.0);
        let value2 = JSValue::number(&ctx, 42.0);
        assert_eq!(value1, value2);
    }
}