Boa 0.5.1

Boa is a Javascript lexer, parser and Just-in-Time compiler written in Rust. Currently, it has support for some of the language.
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
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
use crate::builtins::{
    function::{Function, NativeFunction, NativeFunctionData},
    object::{InternalState, InternalStateCell, Object, ObjectKind, INSTANCE_PROTOTYPE, PROTOTYPE},
    property::Property,
};
use gc::{Gc, GcCell};
use gc_derive::{Finalize, Trace};
use serde_json::{map::Map, Number as JSONNumber, Value as JSONValue};
use std::{
    any::Any,
    collections::HashSet,
    f64::NAN,
    fmt::{self, Display},
    ops::{Add, BitAnd, BitOr, BitXor, Deref, DerefMut, Div, Mul, Not, Rem, Shl, Shr, Sub},
    str::FromStr,
};

#[must_use]
/// The result of a Javascript expression is represented like this so it can succeed (`Ok`) or fail (`Err`)
pub type ResultValue = Result<Value, Value>;
/// A Garbage-collected Javascript value as represented in the interpreter
pub type Value = Gc<ValueData>;

pub fn undefined() -> Value {
    Gc::new(ValueData::Undefined)
}

/// A Javascript value
#[derive(Trace, Finalize, Debug, Clone)]
pub enum ValueData {
    /// `null` - A null value, for when a value doesn't exist
    Null,
    /// `undefined` - An undefined value, for when a field or index doesn't exist
    Undefined,
    /// `boolean` - A `true` / `false` value, for if a certain criteria is met
    Boolean(bool),
    /// `String` - A UTF-8 string, such as `"Hello, world"`
    String(String),
    /// `Number` - A 64-bit floating point number, such as `3.1415`
    Number(f64),
    /// `Number` - A 32-bit integer, such as `42`
    Integer(i32),
    /// `Object` - An object, such as `Math`, represented by a binary tree of string keys to Javascript values
    Object(GcCell<Object>),
    /// `Function` - A runnable block of code, such as `Math.sqrt`, which can take some variables and return a useful value or act upon an object
    Function(Box<GcCell<Function>>),
    /// `Symbol` - A Symbol Type - Internally Symbols are similar to objects, except there are no properties, only internal slots
    Symbol(GcCell<Object>),
}

impl ValueData {
    /// Returns a new empty object
    pub fn new_obj(global: Option<&Value>) -> Value {
        match global {
            Some(glob) => {
                let obj_proto = glob.get_field_slice("Object").get_field_slice(PROTOTYPE);

                let obj = Object::create(obj_proto);
                Gc::new(ValueData::Object(GcCell::new(obj)))
            }
            None => {
                let obj = Object::default();
                Gc::new(ValueData::Object(GcCell::new(obj)))
            }
        }
    }

    /// Similar to `new_obj`, but you can pass a prototype to create from,
    /// plus a kind
    pub fn new_obj_from_prototype(proto: Value, kind: ObjectKind) -> Value {
        let mut obj = Object::default();
        obj.kind = kind;

        obj.internal_slots
            .insert(INSTANCE_PROTOTYPE.to_string(), proto);

        Gc::new(ValueData::Object(GcCell::new(obj)))
    }

    /// This will tell us if we can exten an object or not, not properly implemented yet, for now always returns true
    /// For scalar types it should be false, for objects check the private field for extensibilaty. By default true
    /// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal would turn extensible to false/>
    /// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze would also turn extensible to false/>
    pub fn is_extensible(&self) -> bool {
        true
    }

    /// Returns true if the value is an object
    pub fn is_object(&self) -> bool {
        match *self {
            ValueData::Object(_) => true,
            _ => false,
        }
    }

    /// Returns true if the value is a symbol
    pub fn is_symbol(&self) -> bool {
        match *self {
            ValueData::Symbol(_) => true,
            _ => false,
        }
    }

    /// Returns true if the value is a function
    pub fn is_function(&self) -> bool {
        match *self {
            ValueData::Function(_) => true,
            ValueData::Object(ref o) => o.deref().borrow().get_internal_slot("call").is_function(),
            _ => false,
        }
    }

    /// Returns true if the value is undefined
    pub fn is_undefined(&self) -> bool {
        match *self {
            ValueData::Undefined => true,
            _ => false,
        }
    }

    /// Returns true if the value is null
    pub fn is_null(&self) -> bool {
        match *self {
            ValueData::Null => true,
            _ => false,
        }
    }

    /// Returns true if the value is null or undefined
    pub fn is_null_or_undefined(&self) -> bool {
        match *self {
            ValueData::Null | ValueData::Undefined => true,
            _ => false,
        }
    }

    /// Returns true if the value is a 64-bit floating-point number
    pub fn is_double(&self) -> bool {
        match *self {
            ValueData::Number(_) => true,
            _ => false,
        }
    }

    /// Returns true if the value is a number
    pub fn is_num(&self) -> bool {
        self.is_double()
    }

    /// Returns true if the value is a string
    pub fn is_string(&self) -> bool {
        match *self {
            ValueData::String(_) => true,
            _ => false,
        }
    }

    /// Returns true if the value is a boolean
    pub fn is_boolean(&self) -> bool {
        match *self {
            ValueData::Boolean(_) => true,
            _ => false,
        }
    }

    /// Returns true if the value is true
    /// [toBoolean](https://tc39.github.io/ecma262/#sec-toboolean)
    pub fn is_true(&self) -> bool {
        match *self {
            ValueData::Object(_) => true,
            ValueData::String(ref s) if !s.is_empty() => true,
            ValueData::Number(n) if n != 0.0 && !n.is_nan() => true,
            ValueData::Integer(n) if n != 0 => true,
            ValueData::Boolean(v) => v,
            _ => false,
        }
    }

    /// Converts the value into a 64-bit floating point number
    pub fn to_num(&self) -> f64 {
        match *self {
            ValueData::Object(_)
            | ValueData::Symbol(_)
            | ValueData::Undefined
            | ValueData::Function(_) => NAN,
            ValueData::String(ref str) => match FromStr::from_str(str) {
                Ok(num) => num,
                Err(_) => NAN,
            },
            ValueData::Number(num) => num,
            ValueData::Boolean(true) => 1.0,
            ValueData::Boolean(false) | ValueData::Null => 0.0,
            ValueData::Integer(num) => f64::from(num),
        }
    }

    /// Converts the value into a 32-bit integer
    pub fn to_int(&self) -> i32 {
        match *self {
            ValueData::Object(_)
            | ValueData::Undefined
            | ValueData::Symbol(_)
            | ValueData::Null
            | ValueData::Boolean(false)
            | ValueData::Function(_) => 0,
            ValueData::String(ref str) => match FromStr::from_str(str) {
                Ok(num) => num,
                Err(_) => 0,
            },
            ValueData::Number(num) => num as i32,
            ValueData::Boolean(true) => 1,
            ValueData::Integer(num) => num,
        }
    }

    /// remove_prop removes a property from a Value object.
    /// It will return a boolean based on if the value was removed, if there was no value to remove false is returned
    pub fn remove_prop(&self, field: &str) {
        match *self {
            ValueData::Object(ref obj) => obj.borrow_mut().deref_mut().properties.remove(field),
            // Accesing .object on borrow() seems to automatically dereference it, so we don't need the *
            ValueData::Function(ref func) => match func.borrow_mut().deref_mut() {
                Function::NativeFunc(ref mut func) => func.object.properties.remove(field),
                Function::RegularFunc(ref mut func) => func.object.properties.remove(field),
            },
            _ => None,
        };
    }

    /// Resolve the property in the object
    /// Returns a copy of the Property
    pub fn get_prop(&self, field: &str) -> Option<Property> {
        // Spidermonkey has its own GetLengthProperty: https://searchfox.org/mozilla-central/source/js/src/vm/Interpreter-inl.h#154
        // This is only for primitive strings, String() objects have their lengths calculated in string.rs
        if self.is_string() && field == "length" {
            if let ValueData::String(ref s) = *self {
                return Some(Property::default().value(to_value(s.len() as i32)));
            }
        }

        let obj: Object = match *self {
            ValueData::Object(ref obj) => {
                let hash = obj.clone();
                // TODO: This will break, we should return a GcCellRefMut instead
                // into_inner will consume the wrapped value and remove it from the hashmap
                hash.into_inner()
            }
            // Accesing .object on borrow() seems to automatically dereference it, so we don't need the *
            ValueData::Function(ref func) => match func.clone().into_inner() {
                Function::NativeFunc(ref func) => func.object.clone(),
                Function::RegularFunc(ref func) => func.object.clone(),
            },
            ValueData::Symbol(ref obj) => {
                let hash = obj.clone();
                hash.into_inner()
            }
            _ => return None,
        };

        match obj.properties.get(field) {
            Some(val) => Some(val.clone()),
            None => match obj.internal_slots.get(&INSTANCE_PROTOTYPE.to_string()) {
                Some(value) => value.get_prop(field),
                None => None,
            },
        }
    }

    /// update_prop will overwrite individual [Property] fields, unlike
    /// Set_prop, which will overwrite prop with a new Property
    /// Mostly used internally for now
    pub fn update_prop(
        &self,
        field: &str,
        value: Option<Value>,
        enumerable: Option<bool>,
        writable: Option<bool>,
        configurable: Option<bool>,
    ) {
        let obj: Option<Object> = match self {
            ValueData::Object(ref obj) => Some(obj.borrow_mut().deref_mut().clone()),
            // Accesing .object on borrow() seems to automatically dereference it, so we don't need the *
            ValueData::Function(ref func) => match func.borrow_mut().deref_mut() {
                Function::NativeFunc(ref mut func) => Some(func.object.clone()),
                Function::RegularFunc(ref mut func) => Some(func.object.clone()),
            },
            _ => None,
        };

        if let Some(mut obj_data) = obj {
            // Use value, or walk up the prototype chain
            if let Some(ref mut prop) = obj_data.properties.get_mut(field) {
                prop.value = value;
                prop.enumerable = enumerable;
                prop.writable = writable;
                prop.configurable = configurable;
            }
        }
    }

    /// Resolve the property in the object
    /// Returns a copy of the Property
    pub fn get_internal_slot(&self, field: &str) -> Value {
        let obj: Object = match *self {
            ValueData::Object(ref obj) => {
                let hash = obj.clone();
                hash.into_inner()
            }
            ValueData::Symbol(ref obj) => {
                let hash = obj.clone();
                hash.into_inner()
            }
            _ => return Gc::new(ValueData::Undefined),
        };

        match obj.internal_slots.get(field) {
            Some(val) => val.clone(),
            None => Gc::new(ValueData::Undefined),
        }
    }

    /// Resolve the property in the object and get its value, or undefined if this is not an object or the field doesn't exist
    /// get_field recieves a Property from get_prop(). It should then return the [[Get]] result value if that's set, otherwise fall back to [[Value]]
    /// TODO: this function should use the get Value if its set
    pub fn get_field(&self, field: Value) -> Value {
        match *field {
            // Our field will either be a String or a Symbol
            ValueData::String(ref s) => {
                match self.get_prop(s) {
                    Some(prop) => {
                        // If the Property has [[Get]] set to a function, we should run that and return the Value
                        let prop_getter = match prop.get {
                            Some(_) => None,
                            None => None,
                        };

                        // If the getter is populated, use that. If not use [[Value]] instead
                        match prop_getter {
                            Some(val) => val,
                            None => {
                                let val = prop
                                    .value
                                    .as_ref()
                                    .expect("Could not get property as reference");
                                val.clone()
                            }
                        }
                    }
                    None => Gc::new(ValueData::Undefined),
                }
            }
            ValueData::Symbol(_) => unimplemented!(),
            _ => Gc::new(ValueData::Undefined),
        }
    }

    /// Check whether an object has an internal state set.
    pub fn has_internal_state(&self) -> bool {
        if let ValueData::Object(ref obj) = *self {
            obj.borrow().state.is_some()
        } else {
            false
        }
    }

    /// Get the internal state of an object.
    pub fn get_internal_state(&self) -> Option<InternalStateCell> {
        if let ValueData::Object(ref obj) = *self {
            obj.borrow()
                .state
                .as_ref()
                .map(|state| state.deref().clone())
        } else {
            None
        }
    }

    /// Run a function with a reference to the internal state.
    ///
    /// # Panics
    ///
    /// This will panic if this value doesn't have an internal state or if the internal state doesn't
    /// have the concrete type `S`.
    pub fn with_internal_state_ref<S: Any + InternalState, R, F: FnOnce(&S) -> R>(
        &self,
        f: F,
    ) -> R {
        if let ValueData::Object(ref obj) = *self {
            let o = obj.borrow();
            let state = o
                .state
                .as_ref()
                .expect("no state")
                .downcast_ref()
                .expect("wrong state type");
            f(state)
        } else {
            panic!("not an object");
        }
    }

    /// Run a function with a mutable reference to the internal state.
    ///
    /// # Panics
    ///
    /// This will panic if this value doesn't have an internal state or if the internal state doesn't
    /// have the concrete type `S`.
    pub fn with_internal_state_mut<S: Any + InternalState, R, F: FnOnce(&mut S) -> R>(
        &self,
        f: F,
    ) -> R {
        if let ValueData::Object(ref obj) = *self {
            let mut o = obj.borrow_mut();
            let state = o
                .state
                .as_mut()
                .expect("no state")
                .downcast_mut()
                .expect("wrong state type");
            f(state)
        } else {
            panic!("not an object");
        }
    }

    /// Check to see if the Value has the field, mainly used by environment records
    pub fn has_field(&self, field: &str) -> bool {
        self.get_prop(field).is_some()
    }

    /// Resolve the property in the object and get its value, or undefined if this is not an object or the field doesn't exist
    pub fn get_field_slice(&self, field: &str) -> Value {
        // get_field used to accept strings, but now Symbols accept it needs to accept a value
        // So this function will now need to Box strings back into values (at least for now)
        let f = Gc::new(ValueData::String(field.to_string()));
        self.get_field(f)
    }

    /// Set the field in the value
    /// Field could be a Symbol, so we need to accept a Value (not a string)
    pub fn set_field(&self, field: Value, val: Value) -> Value {
        match *self {
            ValueData::Object(ref obj) => {
                if obj.borrow().kind == ObjectKind::Array {
                    if let Ok(num) = field.to_string().parse::<usize>() {
                        if num > 0 {
                            let len: i32 = from_value(self.get_field_slice("length"))
                                .expect("Could not convert argument to i32");
                            if len < (num + 1) as i32 {
                                self.set_field_slice("length", to_value(num + 1));
                            }
                        }
                    }
                }

                // Symbols get saved into a different bucket to general properties
                if field.is_symbol() {
                    obj.borrow_mut().set(field.clone(), val.clone());
                } else {
                    obj.borrow_mut()
                        .set(to_value(field.to_string()), val.clone());
                }
            }
            ValueData::Function(ref func) => {
                match *func.borrow_mut().deref_mut() {
                    Function::NativeFunc(ref mut f) => f
                        .object
                        .properties
                        .insert(field.to_string(), Property::default().value(val.clone())),
                    Function::RegularFunc(ref mut f) => f
                        .object
                        .properties
                        .insert(field.to_string(), Property::default().value(val.clone())),
                };
            }
            _ => (),
        }
        val
    }

    /// Set the field in the value
    pub fn set_field_slice<'a>(&self, field: &'a str, val: Value) -> Value {
        // set_field used to accept strings, but now Symbols accept it needs to accept a value
        // So this function will now need to Box strings back into values (at least for now)
        let f = Gc::new(ValueData::String(field.to_string()));
        self.set_field(f, val)
    }

    /// Set the private field in the value
    pub fn set_internal_slot(&self, field: &str, val: Value) -> Value {
        if let ValueData::Object(ref obj) = *self {
            obj.borrow_mut()
                .internal_slots
                .insert(field.to_string(), val.clone());
        }
        val
    }

    /// Set the kind of an object
    pub fn set_kind(&self, kind: ObjectKind) -> ObjectKind {
        if let ValueData::Object(ref obj) = *self {
            obj.borrow_mut().kind = kind.clone();
        }
        kind
    }

    /// Set the property in the value
    pub fn set_prop(&self, field: String, prop: Property) -> Property {
        match *self {
            ValueData::Object(ref obj) => {
                obj.borrow_mut().properties.insert(field, prop.clone());
            }
            ValueData::Function(ref func) => {
                match *func.borrow_mut().deref_mut() {
                    Function::NativeFunc(ref mut f) => {
                        f.object.properties.insert(field, prop.clone())
                    }
                    Function::RegularFunc(ref mut f) => {
                        f.object.properties.insert(field, prop.clone())
                    }
                };
            }
            _ => (),
        }
        prop
    }

    /// Set the property in the value
    pub fn set_prop_slice<'t>(&self, field: &'t str, prop: Property) -> Property {
        self.set_prop(field.to_string(), prop)
    }

    /// Set internal state of an Object. Discards the previous state if it was set.
    pub fn set_internal_state<T: Any + InternalState>(&self, state: T) {
        if let ValueData::Object(ref obj) = *self {
            obj.borrow_mut()
                .state
                .replace(Box::new(InternalStateCell::new(state)));
        }
    }

    /// Convert from a JSON value to a JS value
    pub fn from_json(json: JSONValue) -> Self {
        match json {
            JSONValue::Number(v) => {
                ValueData::Number(v.as_f64().expect("Could not convert value to f64"))
            }
            JSONValue::String(v) => ValueData::String(v),
            JSONValue::Bool(v) => ValueData::Boolean(v),
            JSONValue::Array(vs) => {
                let mut new_obj = Object::default();
                for (idx, json) in vs.iter().enumerate() {
                    new_obj.properties.insert(
                        idx.to_string(),
                        Property::default().value(to_value(json.clone())),
                    );
                }
                new_obj.properties.insert(
                    "length".to_string(),
                    Property::default().value(to_value(vs.len() as i32)),
                );
                ValueData::Object(GcCell::new(new_obj))
            }
            JSONValue::Object(obj) => {
                let mut new_obj = Object::default();
                for (key, json) in obj.iter() {
                    new_obj.properties.insert(
                        key.clone(),
                        Property::default().value(to_value(json.clone())),
                    );
                }

                ValueData::Object(GcCell::new(new_obj))
            }
            JSONValue::Null => ValueData::Null,
        }
    }

    pub fn to_json(&self) -> JSONValue {
        match *self {
            ValueData::Null
            | ValueData::Symbol(_)
            | ValueData::Undefined
            | ValueData::Function(_) => JSONValue::Null,
            ValueData::Boolean(b) => JSONValue::Bool(b),
            ValueData::Object(ref obj) => {
                let mut new_obj = Map::new();
                for (k, v) in obj.borrow().internal_slots.iter() {
                    if k != INSTANCE_PROTOTYPE {
                        new_obj.insert(k.clone(), v.to_json());
                    }
                }
                JSONValue::Object(new_obj)
            }
            ValueData::String(ref str) => JSONValue::String(str.clone()),
            ValueData::Number(num) => JSONValue::Number(
                JSONNumber::from_f64(num).expect("Could not convert to JSONNumber"),
            ),
            ValueData::Integer(val) => JSONValue::Number(JSONNumber::from(val)),
        }
    }

    /// Get the type of the value
    /// https://tc39.es/ecma262/#sec-typeof-operator
    pub fn get_type(&self) -> &'static str {
        match *self {
            ValueData::Number(_) | ValueData::Integer(_) => "number",
            ValueData::String(_) => "string",
            ValueData::Boolean(_) => "boolean",
            ValueData::Symbol(_) => "symbol",
            ValueData::Null => "null",
            ValueData::Undefined => "undefined",
            ValueData::Function(_) => "function",
            ValueData::Object(ref o) => {
                if o.deref().borrow().get_internal_slot("call").is_null() {
                    "object"
                } else {
                    "function"
                }
            }
        }
    }

    pub fn as_num_to_power(&self, other: ValueData) -> ValueData {
        ValueData::Number(self.to_num().powf(other.to_num()))
    }
}

impl Default for ValueData {
    fn default() -> Self {
        ValueData::Undefined
    }
}

/// A helper macro for printing objects
/// Can be used to print both properties and internal slots
/// All of the overloads take:
/// - The object to be printed
/// - The function with which to print
/// - The indentation for the current level (for nested objects)
/// - A HashSet with the addresses of the already printed objects for the current branch
///      (used to avoid infinite loops when there are cyclic deps)
macro_rules! print_obj_value {
    (all of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr) => {
        {
            let mut internals = print_obj_value!(internals of $obj, $display_fn, $indent, $encounters);
            let mut props = print_obj_value!(props of $obj, $display_fn, $indent, $encounters, true);

            props.reserve(internals.len());

            props.append(&mut internals);

            props
        }
    };
    (internals of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr) => {
        print_obj_value!(impl internal_slots, $obj, |(key, val)| {
            format!(
                "{}{}: {}",
                String::from_utf8(vec![b' '; $indent])
                                .expect("Could not create indentation string"),
                key,
                $display_fn(&val, $encounters, $indent.wrapping_add(4), true)
            )
        })
    };
    (props of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr, $print_internals:expr) => {
        print_obj_value!(impl properties, $obj, |(key, val)| {
            let v = &val
                .value
                .as_ref()
                .expect("Could not get the property's value");

            format!(
                "{}{}: {}",
                String::from_utf8(vec![b' '; $indent])
                                .expect("Could not create indentation string"),
                key,
                $display_fn(v, $encounters, $indent.wrapping_add(4), $print_internals)
            )
        })
    };

    // A private overload of the macro
    // DO NOT use directly
    (impl $field:ident, $v:expr, $f:expr) => {
        $v
            .borrow()
            .$field
            .iter()
            .map($f)
            .collect::<Vec<String>>()
    };
}

pub(crate) fn log_string_from(x: &ValueData, print_internals: bool) -> String {
    match x {
        // We don't want to print private (compiler) or prototype properties
        ValueData::Object(ref v) => {
            // Can use the private "type" field of an Object to match on
            // which type of Object it represents for special printing
            match v.borrow().kind {
                ObjectKind::String => from_value(
                    v.borrow()
                        .internal_slots
                        .get("StringData")
                        .expect("Cannot get primitive value from String")
                        .clone(),
                )
                .expect("Cannot clone primitive value from String"),
                ObjectKind::Boolean => {
                    let bool_data = v.borrow().get_internal_slot("BooleanData").to_string();

                    format!("Boolean {{ {} }}", bool_data)
                }
                ObjectKind::Array => {
                    let len: i32 = from_value(
                        v.borrow()
                            .properties
                            .get("length")
                            .unwrap()
                            .value
                            .clone()
                            .expect("Could not borrow value"),
                    )
                    .expect("Could not convert JS value to i32");

                    if len == 0 {
                        return String::from("[]");
                    }

                    let arr = (0..len)
                        .map(|i| {
                            // Introduce recursive call to stringify any objects
                            // which are part of the Array
                            log_string_from(
                                &v.borrow()
                                    .properties
                                    .get(&i.to_string())
                                    .unwrap()
                                    .value
                                    .clone()
                                    .expect("Could not borrow value"),
                                print_internals,
                            )
                        })
                        .collect::<Vec<String>>()
                        .join(", ");

                    format!("[ {} ]", arr)
                }
                _ => display_obj(&x, print_internals),
            }
        }
        ValueData::Symbol(ref sym) => {
            let desc: Value = sym.borrow().get_internal_slot("Description");
            match *desc {
                ValueData::String(ref st) => format!("Symbol(\"{}\")", st.to_string()),
                _ => String::from("Symbol()"),
            }
        }

        _ => format!("{}", x),
    }
}

/// A helper function for specifically printing object values
fn display_obj(v: &ValueData, print_internals: bool) -> String {
    // A simple helper for getting the address of a value
    // TODO: Find a more general place for this, as it can be used in other situations as well
    fn address_of<T>(t: &T) -> usize {
        let my_ptr: *const T = t;
        my_ptr as usize
    }

    // We keep track of which objects we have encountered by keeping their
    // in-memory address in this set
    let mut encounters = HashSet::new();

    fn display_obj_internal(
        data: &ValueData,
        encounters: &mut HashSet<usize>,
        indent: usize,
        print_internals: bool,
    ) -> String {
        match *data {
            ValueData::Object(ref v) => {
                // The in-memory address of the current object
                let addr = address_of(v.borrow().deref());

                // We need not continue if this object has already been
                // printed up the current chain
                if encounters.contains(&addr) {
                    return String::from("[Cycle]");
                }

                // Mark the current object as encountered
                encounters.insert(addr);

                let result = if print_internals {
                    print_obj_value!(all of v, display_obj_internal, indent, encounters).join(",\n")
                } else {
                    print_obj_value!(props of v, display_obj_internal, indent, encounters, print_internals)
                        .join(",\n")
                };

                // If the current object is referenced in a different branch,
                // it will not cause an infinte printing loop, so it is safe to be printed again
                encounters.remove(&addr);

                let closing_indent = String::from_utf8(vec![b' '; indent.wrapping_sub(4)])
                    .expect("Could not create the closing brace's indentation string");

                format!("{{\n{}\n{}}}", result, closing_indent)
            }

            // Every other type of data is printed as is
            _ => format!("{}", data),
        }
    }

    display_obj_internal(v, &mut encounters, 4, print_internals)
}

impl Display for ValueData {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ValueData::Null => write!(f, "null"),
            ValueData::Undefined => write!(f, "undefined"),
            ValueData::Boolean(v) => write!(f, "{}", v),
            ValueData::Symbol(ref v) => match *v.borrow().get_internal_slot("Description") {
                // If a description exists use it
                ValueData::String(ref v) => write!(f, "{}", format!("Symbol({})", v)),
                _ => write!(f, "Symbol()"),
            },
            ValueData::String(ref v) => write!(f, "{}", v),
            ValueData::Number(v) => write!(
                f,
                "{}",
                match v {
                    _ if v.is_nan() => "NaN".to_string(),
                    _ if v.is_infinite() && v.is_sign_negative() => "-Infinity".to_string(),
                    _ if v.is_infinite() => "Infinity".to_string(),
                    _ => v.to_string(),
                }
            ),
            ValueData::Object(_) => write!(f, "{}", log_string_from(self, true)),
            ValueData::Integer(v) => write!(f, "{}", v),
            ValueData::Function(ref v) => match *v.borrow() {
                Function::NativeFunc(_) => write!(f, "function() {{ [native code] }}"),
                Function::RegularFunc(ref rf) => {
                    write!(f, "function(")?;
                    let last_index = rf.args.len() - 1;
                    for (index, arg) in rf.args.iter().enumerate() {
                        write!(f, "{}", arg)?;
                        if index != last_index {
                            write!(f, ", ")?;
                        }
                    }
                    write!(f, "){}", rf.expr)
                }
            },
        }
    }
}

impl PartialEq for ValueData {
    fn eq(&self, other: &Self) -> bool {
        match (self.clone(), other.clone()) {
            // TODO: fix this
            // _ if self.ptr.to_inner() == &other.ptr.to_inner() => true,
            _ if self.is_null_or_undefined() && other.is_null_or_undefined() => true,
            (ValueData::String(_), _) | (_, ValueData::String(_)) => {
                self.to_string() == other.to_string()
            }
            (ValueData::Boolean(a), ValueData::Boolean(b)) if a == b => true,
            (ValueData::Number(a), ValueData::Number(b))
                if a == b && !a.is_nan() && !b.is_nan() =>
            {
                true
            }
            (ValueData::Number(a), _) if a == other.to_num() => true,
            (_, ValueData::Number(a)) if a == self.to_num() => true,
            (ValueData::Integer(a), ValueData::Integer(b)) if a == b => true,
            _ => false,
        }
    }
}

impl Add for ValueData {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        match (self, other) {
            (ValueData::String(ref s), ref o) => {
                ValueData::String(format!("{}{}", s.clone(), &o.to_string()))
            }
            (ref s, ValueData::String(ref o)) => {
                ValueData::String(format!("{}{}", s.to_string(), o))
            }
            (ref s, ref o) => ValueData::Number(s.to_num() + o.to_num()),
        }
    }
}
impl Sub for ValueData {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        ValueData::Number(self.to_num() - other.to_num())
    }
}
impl Mul for ValueData {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        ValueData::Number(self.to_num() * other.to_num())
    }
}
impl Div for ValueData {
    type Output = Self;
    fn div(self, other: Self) -> Self {
        ValueData::Number(self.to_num() / other.to_num())
    }
}
impl Rem for ValueData {
    type Output = Self;
    fn rem(self, other: Self) -> Self {
        ValueData::Number(self.to_num() % other.to_num())
    }
}
impl BitAnd for ValueData {
    type Output = Self;
    fn bitand(self, other: Self) -> Self {
        ValueData::Integer(self.to_int() & other.to_int())
    }
}
impl BitOr for ValueData {
    type Output = Self;
    fn bitor(self, other: Self) -> Self {
        ValueData::Integer(self.to_int() | other.to_int())
    }
}
impl BitXor for ValueData {
    type Output = Self;
    fn bitxor(self, other: Self) -> Self {
        ValueData::Integer(self.to_int() ^ other.to_int())
    }
}
impl Shl for ValueData {
    type Output = Self;
    fn shl(self, other: Self) -> Self {
        ValueData::Integer(self.to_int() << other.to_int())
    }
}
impl Shr for ValueData {
    type Output = Self;
    fn shr(self, other: Self) -> Self {
        ValueData::Integer(self.to_int() >> other.to_int())
    }
}
impl Not for ValueData {
    type Output = Self;
    fn not(self) -> Self {
        ValueData::Boolean(!self.is_true())
    }
}

/// Conversion to Javascript values from Rust values
pub trait ToValue {
    /// Convert this value to a Rust value
    fn to_value(&self) -> Value;
}
/// Conversion to Rust values from Javascript values
pub trait FromValue {
    /// Convert this value to a Javascript value
    fn from_value(value: Value) -> Result<Self, &'static str>
    where
        Self: Sized;
}

impl ToValue for Value {
    fn to_value(&self) -> Value {
        self.clone()
    }
}

impl FromValue for Value {
    fn from_value(value: Value) -> Result<Self, &'static str> {
        Ok(value)
    }
}

impl ToValue for String {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::String(self.clone()))
    }
}

impl FromValue for String {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        Ok(v.to_string())
    }
}

impl<'s> ToValue for &'s str {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::String(
            String::from_str(*self).expect("Could not convert string to self to String"),
        ))
    }
}

impl ToValue for char {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::String(self.to_string()))
    }
}
impl FromValue for char {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        Ok(v.to_string()
            .chars()
            .next()
            .expect("Could not get next char"))
    }
}

impl ToValue for f64 {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::Number(*self))
    }
}
impl FromValue for f64 {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        Ok(v.to_num())
    }
}

impl ToValue for i32 {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::Integer(*self))
    }
}
impl FromValue for i32 {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        Ok(v.to_int())
    }
}

impl ToValue for usize {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::Integer(*self as i32))
    }
}
impl FromValue for usize {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        Ok(v.to_int() as usize)
    }
}

impl ToValue for bool {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::Boolean(*self))
    }
}
impl FromValue for bool {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        Ok(v.is_true())
    }
}

impl<'s, T: ToValue> ToValue for &'s [T] {
    fn to_value(&self) -> Value {
        let mut arr = Object::default();
        for (i, item) in self.iter().enumerate() {
            arr.properties
                .insert(i.to_string(), Property::default().value(item.to_value()));
        }
        to_value(arr)
    }
}
impl<T: ToValue> ToValue for Vec<T> {
    fn to_value(&self) -> Value {
        let mut arr = Object::default();
        for (i, item) in self.iter().enumerate() {
            arr.properties
                .insert(i.to_string(), Property::default().value(item.to_value()));
        }
        to_value(arr)
    }
}

impl<T: FromValue> FromValue for Vec<T> {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        let len = v.get_field_slice("length").to_int();
        let mut vec = Self::with_capacity(len as usize);
        for i in 0..len {
            vec.push(from_value(v.get_field_slice(&i.to_string()))?)
        }
        Ok(vec)
    }
}

impl ToValue for Object {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::Object(GcCell::new(self.clone())))
    }
}

impl FromValue for Object {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        match *v {
            ValueData::Object(ref obj) => Ok(obj.clone().into_inner()),
            ValueData::Function(ref func) => Ok(match *func.borrow().deref() {
                Function::NativeFunc(ref data) => data.object.clone(),
                Function::RegularFunc(ref data) => data.object.clone(),
            }),
            _ => Err("Value is not a valid object"),
        }
    }
}

impl ToValue for JSONValue {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::from_json(self.clone()))
    }
}

impl FromValue for JSONValue {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        Ok(v.to_json())
    }
}

impl ToValue for () {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::Null)
    }
}
impl FromValue for () {
    fn from_value(_: Value) -> Result<(), &'static str> {
        Ok(())
    }
}

impl<T: ToValue> ToValue for Option<T> {
    fn to_value(&self) -> Value {
        match *self {
            Some(ref v) => v.to_value(),
            None => Gc::new(ValueData::Null),
        }
    }
}
impl<T: FromValue> FromValue for Option<T> {
    fn from_value(value: Value) -> Result<Self, &'static str> {
        Ok(if value.is_null_or_undefined() {
            None
        } else {
            Some(FromValue::from_value(value)?)
        })
    }
}

impl ToValue for NativeFunctionData {
    fn to_value(&self) -> Value {
        Gc::new(ValueData::Function(Box::new(GcCell::new(
            Function::NativeFunc(NativeFunction::new(*self)),
        ))))
    }
}
impl FromValue for NativeFunctionData {
    fn from_value(v: Value) -> Result<Self, &'static str> {
        match *v {
            ValueData::Function(ref func) => match *func.borrow() {
                Function::NativeFunc(ref data) => Ok(data.data),
                _ => Err("Value is not a native function"),
            },
            _ => Err("Value is not a function"),
        }
    }
}

/// A utility function that just calls `FromValue::from_value`
pub fn from_value<A: FromValue>(v: Value) -> Result<A, &'static str> {
    FromValue::from_value(v)
}

/// A utility function that just calls `ToValue::to_value`
pub fn to_value<A: ToValue>(v: A) -> Value {
    v.to_value()
}

/// The internal comparison abstract operation SameValue(x, y),
/// where x and y are ECMAScript language values, produces true or false.
/// Such a comparison is performed as follows:
///
/// https://tc39.es/ecma262/#sec-samevalue
/// strict mode currently compares the pointers
pub fn same_value(x: &Value, y: &Value, strict: bool) -> bool {
    if strict {
        // Do both Values point to the same underlying valueData?
        let x_ptr = Gc::into_raw(x.clone());
        let y_ptr = Gc::into_raw(y.clone());
        return x_ptr == y_ptr;
    }

    if x.get_type() != y.get_type() {
        return false;
    }

    if x.get_type() == "number" {
        let native_x: f64 = from_value(x.clone()).expect("failed to get value");
        let native_y: f64 = from_value(y.clone()).expect("failed to get value");
        return native_x.abs() - native_y.abs() == 0.0;
    }

    same_value_non_number(x, y)
}

pub fn same_value_non_number(x: &Value, y: &Value) -> bool {
    debug_assert!(x.get_type() == y.get_type());
    match x.get_type() {
        "undefined" => true,
        "null" => true,
        "string" => {
            if x.to_string() == y.to_string() {
                return true;
            }
            false
        }
        "boolean" => {
            from_value::<bool>(x.clone()).expect("failed to get value")
                == from_value::<bool>(y.clone()).expect("failed to get value")
        }
        "object" => *x == *y,
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn check_is_object() {
        let val = ValueData::new_obj(None);
        assert_eq!(val.is_object(), true);
    }

    #[test]
    fn check_string_to_value() {
        let s = String::from("Hello");
        let v = s.to_value();
        assert_eq!(v.is_string(), true);
        assert_eq!(v.is_null(), false);
    }

    #[test]
    fn check_undefined() {
        let u = ValueData::Undefined;
        assert_eq!(u.get_type(), "undefined");
        assert_eq!(u.to_string(), "undefined");
    }

    #[test]
    fn check_get_set_field() {
        let obj = ValueData::new_obj(None);
        // Create string and convert it to a Value
        let s = String::from("bar").to_value();
        obj.set_field_slice("foo", s);
        assert_eq!(obj.get_field_slice("foo").to_string(), "bar");
    }

    #[test]
    fn check_integer_is_true() {
        assert_eq!(1.to_value().is_true(), true);
        assert_eq!(0.to_value().is_true(), false);
        assert_eq!((-1).to_value().is_true(), true);
    }

    #[test]
    fn check_number_is_true() {
        assert_eq!(1.0.to_value().is_true(), true);
        assert_eq!(0.1.to_value().is_true(), true);
        assert_eq!(0.0.to_value().is_true(), false);
        assert_eq!((-0.0).to_value().is_true(), false);
        assert_eq!((-1.0).to_value().is_true(), true);
        assert_eq!(NAN.to_value().is_true(), false);
    }
}