@@ -17,10 +17,10 @@ use crate::{
17
17
builtins:: {
18
18
object:: { ObjectData , PROTOTYPE } ,
19
19
property:: { Attribute , Property } ,
20
- value:: { same_value_zero, ResultValue , Value } ,
20
+ value:: { same_value_zero, Value } ,
21
21
} ,
22
22
exec:: Interpreter ,
23
- BoaProfiler ,
23
+ BoaProfiler , Result ,
24
24
} ;
25
25
use std:: {
26
26
borrow:: Borrow ,
@@ -39,7 +39,7 @@ impl Array {
39
39
pub ( crate ) const LENGTH : usize = 1 ;
40
40
41
41
/// Creates a new `Array` instance.
42
- pub ( crate ) fn new_array ( interpreter : & Interpreter ) -> ResultValue {
42
+ pub ( crate ) fn new_array ( interpreter : & Interpreter ) -> Result < Value > {
43
43
let array = Value :: new_object ( Some (
44
44
& interpreter
45
45
. realm ( )
@@ -65,7 +65,7 @@ impl Array {
65
65
///
66
66
/// `array_obj` can be any array with prototype already set (it will be wiped and
67
67
/// recreated from `array_contents`)
68
- pub ( crate ) fn construct_array ( array_obj : & Value , array_contents : & [ Value ] ) -> ResultValue {
68
+ pub ( crate ) fn construct_array ( array_obj : & Value , array_contents : & [ Value ] ) -> Result < Value > {
69
69
let array_obj_ptr = array_obj. clone ( ) ;
70
70
71
71
// Wipe existing contents of the array object
@@ -89,7 +89,7 @@ impl Array {
89
89
90
90
/// Utility function which takes an existing array object and puts additional
91
91
/// values on the end, correctly rewriting the length
92
- pub ( crate ) fn add_to_array_object ( array_ptr : & Value , add_values : & [ Value ] ) -> ResultValue {
92
+ pub ( crate ) fn add_to_array_object ( array_ptr : & Value , add_values : & [ Value ] ) -> Result < Value > {
93
93
let orig_length = array_ptr. get_field ( "length" ) . as_number ( ) . unwrap ( ) as i32 ;
94
94
95
95
for ( n, value) in add_values. iter ( ) . enumerate ( ) {
@@ -106,7 +106,7 @@ impl Array {
106
106
}
107
107
108
108
/// Create a new array
109
- pub ( crate ) fn make_array ( this : & Value , args : & [ Value ] , ctx : & mut Interpreter ) -> ResultValue {
109
+ pub ( crate ) fn make_array ( this : & Value , args : & [ Value ] , ctx : & mut Interpreter ) -> Result < Value > {
110
110
// Make a new Object which will internally represent the Array (mapping
111
111
// between indices and values): this creates an Object with no prototype
112
112
@@ -166,7 +166,7 @@ impl Array {
166
166
_this : & Value ,
167
167
args : & [ Value ] ,
168
168
_interpreter : & mut Interpreter ,
169
- ) -> ResultValue {
169
+ ) -> Result < Value > {
170
170
match args. get ( 0 ) . and_then ( |x| x. as_object ( ) ) {
171
171
Some ( object) => Ok ( Value :: from ( object. is_array ( ) ) ) ,
172
172
None => Ok ( Value :: from ( false ) ) ,
@@ -185,7 +185,7 @@ impl Array {
185
185
///
186
186
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.concat
187
187
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
188
- pub ( crate ) fn concat ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> ResultValue {
188
+ pub ( crate ) fn concat ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> Result < Value > {
189
189
if args. is_empty ( ) {
190
190
// If concat is called with no arguments, it returns the original array
191
191
return Ok ( this. clone ( ) ) ;
@@ -222,7 +222,7 @@ impl Array {
222
222
///
223
223
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.push
224
224
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
225
- pub ( crate ) fn push ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> ResultValue {
225
+ pub ( crate ) fn push ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> Result < Value > {
226
226
let new_array = Self :: add_to_array_object ( this, args) ?;
227
227
Ok ( new_array. get_field ( "length" ) )
228
228
}
@@ -237,8 +237,9 @@ impl Array {
237
237
///
238
238
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.pop
239
239
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
240
- pub ( crate ) fn pop ( this : & Value , _: & [ Value ] , _: & mut Interpreter ) -> ResultValue {
240
+ pub ( crate ) fn pop ( this : & Value , _: & [ Value ] , _: & mut Interpreter ) -> Result < Value > {
241
241
let curr_length = this. get_field ( "length" ) . as_number ( ) . unwrap ( ) as i32 ;
242
+
242
243
if curr_length < 1 {
243
244
return Ok ( Value :: undefined ( ) ) ;
244
245
}
@@ -263,7 +264,7 @@ impl Array {
263
264
this : & Value ,
264
265
args : & [ Value ] ,
265
266
interpreter : & mut Interpreter ,
266
- ) -> ResultValue {
267
+ ) -> Result < Value > {
267
268
if args. is_empty ( ) {
268
269
return Err ( Value :: from ( "Missing argument for Array.prototype.forEach" ) ) ;
269
270
}
@@ -295,7 +296,7 @@ impl Array {
295
296
///
296
297
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.join
297
298
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
298
- pub ( crate ) fn join ( this : & Value , args : & [ Value ] , ctx : & mut Interpreter ) -> ResultValue {
299
+ pub ( crate ) fn join ( this : & Value , args : & [ Value ] , ctx : & mut Interpreter ) -> Result < Value > {
299
300
let separator = if args. is_empty ( ) {
300
301
String :: from ( "," )
301
302
} else {
@@ -328,7 +329,7 @@ impl Array {
328
329
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.tostring
329
330
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
330
331
#[ allow( clippy:: wrong_self_convention) ]
331
- pub ( crate ) fn to_string ( this : & Value , _args : & [ Value ] , ctx : & mut Interpreter ) -> ResultValue {
332
+ pub ( crate ) fn to_string ( this : & Value , _args : & [ Value ] , ctx : & mut Interpreter ) -> Result < Value > {
332
333
let method_name = "join" ;
333
334
let mut arguments = vec ! [ Value :: from( "," ) ] ;
334
335
// 2.
@@ -368,8 +369,9 @@ impl Array {
368
369
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.reverse
369
370
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
370
371
#[ allow( clippy:: else_if_without_else) ]
371
- pub ( crate ) fn reverse ( this : & Value , _: & [ Value ] , _: & mut Interpreter ) -> ResultValue {
372
+ pub ( crate ) fn reverse ( this : & Value , _: & [ Value ] , _: & mut Interpreter ) -> Result < Value > {
372
373
let len = this. get_field ( "length" ) . as_number ( ) . unwrap ( ) as i32 ;
374
+
373
375
let middle: i32 = len. wrapping_div ( 2 ) ;
374
376
375
377
for lower in 0 ..middle {
@@ -406,7 +408,7 @@ impl Array {
406
408
///
407
409
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.shift
408
410
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
409
- pub ( crate ) fn shift ( this : & Value , _: & [ Value ] , _: & mut Interpreter ) -> ResultValue {
411
+ pub ( crate ) fn shift ( this : & Value , _: & [ Value ] , _: & mut Interpreter ) -> Result < Value > {
410
412
let len = this. get_field ( "length" ) . as_number ( ) . unwrap ( ) as i32 ;
411
413
412
414
if len == 0 {
@@ -448,8 +450,9 @@ impl Array {
448
450
///
449
451
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.unshift
450
452
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
451
- pub ( crate ) fn unshift ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> ResultValue {
453
+ pub ( crate ) fn unshift ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> Result < Value > {
452
454
let len = this. get_field ( "length" ) . as_number ( ) . unwrap ( ) as i32 ;
455
+
453
456
let arg_c: i32 = args. len ( ) as i32 ;
454
457
455
458
if arg_c > 0 {
@@ -496,7 +499,7 @@ impl Array {
496
499
this : & Value ,
497
500
args : & [ Value ] ,
498
501
interpreter : & mut Interpreter ,
499
- ) -> ResultValue {
502
+ ) -> Result < Value > {
500
503
if args. is_empty ( ) {
501
504
return Err ( Value :: from (
502
505
"missing callback when calling function Array.prototype.every" ,
@@ -538,7 +541,11 @@ impl Array {
538
541
///
539
542
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.map
540
543
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
541
- pub ( crate ) fn map ( this : & Value , args : & [ Value ] , interpreter : & mut Interpreter ) -> ResultValue {
544
+ pub ( crate ) fn map (
545
+ this : & Value ,
546
+ args : & [ Value ] ,
547
+ interpreter : & mut Interpreter ,
548
+ ) -> Result < Value > {
542
549
if args. is_empty ( ) {
543
550
return Err ( Value :: from (
544
551
"missing argument 0 when calling function Array.prototype.map" ,
@@ -585,7 +592,7 @@ impl Array {
585
592
///
586
593
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.indexof
587
594
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
588
- pub ( crate ) fn index_of ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> ResultValue {
595
+ pub ( crate ) fn index_of ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> Result < Value > {
589
596
// If no arguments, return -1. Not described in spec, but is what chrome does.
590
597
if args. is_empty ( ) {
591
598
return Ok ( Value :: from ( -1 ) ) ;
@@ -638,7 +645,11 @@ impl Array {
638
645
///
639
646
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.lastindexof
640
647
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
641
- pub ( crate ) fn last_index_of ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> ResultValue {
648
+ pub ( crate ) fn last_index_of (
649
+ this : & Value ,
650
+ args : & [ Value ] ,
651
+ _: & mut Interpreter ,
652
+ ) -> Result < Value > {
642
653
// If no arguments, return -1. Not described in spec, but is what chrome does.
643
654
if args. is_empty ( ) {
644
655
return Ok ( Value :: from ( -1 ) ) ;
@@ -685,7 +696,11 @@ impl Array {
685
696
///
686
697
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.find
687
698
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
688
- pub ( crate ) fn find ( this : & Value , args : & [ Value ] , interpreter : & mut Interpreter ) -> ResultValue {
699
+ pub ( crate ) fn find (
700
+ this : & Value ,
701
+ args : & [ Value ] ,
702
+ interpreter : & mut Interpreter ,
703
+ ) -> Result < Value > {
689
704
if args. is_empty ( ) {
690
705
return Err ( Value :: from (
691
706
"missing callback when calling function Array.prototype.find" ,
@@ -721,7 +736,7 @@ impl Array {
721
736
this : & Value ,
722
737
args : & [ Value ] ,
723
738
interpreter : & mut Interpreter ,
724
- ) -> ResultValue {
739
+ ) -> Result < Value > {
725
740
if args. is_empty ( ) {
726
741
return Err ( Value :: from (
727
742
"Missing argument for Array.prototype.findIndex" ,
@@ -759,8 +774,9 @@ impl Array {
759
774
///
760
775
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.fill
761
776
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
762
- pub ( crate ) fn fill ( this : & Value , args : & [ Value ] , ctx : & mut Interpreter ) -> ResultValue {
777
+ pub ( crate ) fn fill ( this : & Value , args : & [ Value ] , ctx : & mut Interpreter ) -> Result < Value > {
763
778
let len: i32 = this. get_field ( "length" ) . as_number ( ) . unwrap ( ) as i32 ;
779
+
764
780
let default_value = Value :: undefined ( ) ;
765
781
let value = args. get ( 0 ) . unwrap_or ( & default_value) ;
766
782
let relative_start = args. get ( 1 ) . unwrap_or ( & default_value) . to_number ( ctx) ? as i32 ;
@@ -798,7 +814,11 @@ impl Array {
798
814
///
799
815
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.includes
800
816
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
801
- pub ( crate ) fn includes_value ( this : & Value , args : & [ Value ] , _: & mut Interpreter ) -> ResultValue {
817
+ pub ( crate ) fn includes_value (
818
+ this : & Value ,
819
+ args : & [ Value ] ,
820
+ _: & mut Interpreter ,
821
+ ) -> Result < Value > {
802
822
let search_element = args. get ( 0 ) . cloned ( ) . unwrap_or_else ( Value :: undefined) ;
803
823
804
824
let length = this. get_field ( "length" ) . as_number ( ) . unwrap ( ) as i32 ;
@@ -832,7 +852,7 @@ impl Array {
832
852
this : & Value ,
833
853
args : & [ Value ] ,
834
854
interpreter : & mut Interpreter ,
835
- ) -> ResultValue {
855
+ ) -> Result < Value > {
836
856
let new_array = Self :: new_array ( interpreter) ?;
837
857
let len = this. get_field ( "length" ) . as_number ( ) . unwrap ( ) as i32 ;
838
858
@@ -881,7 +901,7 @@ impl Array {
881
901
this : & Value ,
882
902
args : & [ Value ] ,
883
903
interpreter : & mut Interpreter ,
884
- ) -> ResultValue {
904
+ ) -> Result < Value > {
885
905
if args. is_empty ( ) {
886
906
return Err ( Value :: from (
887
907
"missing argument 0 when calling function Array.prototype.filter" ,
@@ -931,7 +951,11 @@ impl Array {
931
951
///
932
952
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype.some
933
953
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
934
- pub ( crate ) fn some ( this : & Value , args : & [ Value ] , interpreter : & mut Interpreter ) -> ResultValue {
954
+ pub ( crate ) fn some (
955
+ this : & Value ,
956
+ args : & [ Value ] ,
957
+ interpreter : & mut Interpreter ,
958
+ ) -> Result < Value > {
935
959
if args. is_empty ( ) {
936
960
return Err ( Value :: from (
937
961
"missing callback when calling function Array.prototype.some" ,
@@ -978,7 +1002,7 @@ impl Array {
978
1002
this : & Value ,
979
1003
args : & [ Value ] ,
980
1004
interpreter : & mut Interpreter ,
981
- ) -> ResultValue {
1005
+ ) -> Result < Value > {
982
1006
let this = this. to_object ( interpreter) ?;
983
1007
let callback = match args. get ( 0 ) {
984
1008
Some ( value) if value. is_function ( ) => value,
@@ -1045,7 +1069,7 @@ impl Array {
1045
1069
this : & Value ,
1046
1070
args : & [ Value ] ,
1047
1071
interpreter : & mut Interpreter ,
1048
- ) -> ResultValue {
1072
+ ) -> Result < Value > {
1049
1073
let this = this. to_object ( interpreter) ?;
1050
1074
let callback = match args. get ( 0 ) {
1051
1075
Some ( value) if value. is_function ( ) => value,
0 commit comments