-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lib.rs
5852 lines (5097 loc) · 268 KB
/
lib.rs
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
//! Bindings to JavaScript's standard, built-in objects, including their methods
//! and properties.
//!
//! This does *not* include any Web, Node, or any other JS environment
//! APIs. Only the things that are guaranteed to exist in the global scope by
//! the ECMAScript standard.
//!
//! <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects>
//!
//! ## A Note About `camelCase`, `snake_case`, and Naming Conventions
//!
//! JavaScript's global objects use `camelCase` naming conventions for functions
//! and methods, but Rust style is to use `snake_case`. These bindings expose
//! the Rust style `snake_case` name. Additionally, acronyms within a method
//! name are all lower case, where as in JavaScript they are all upper case. For
//! example, `decodeURI` in JavaScript is exposed as `decode_uri` in these
//! bindings.
#![doc(html_root_url = "https://docs.rs/js-sys/0.2")]
use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub};
use std::cmp::Ordering;
use std::convert::{self, Infallible};
use std::f64;
use std::fmt;
use std::iter::{self, Product, Sum};
use std::mem;
use std::str;
use std::str::FromStr;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
// When adding new imports:
//
// * Keep imports in alphabetical order.
//
// * Rename imports with `js_name = ...` according to the note about `camelCase`
// and `snake_case` in the module's documentation above.
//
// * Include the one sentence summary of the import from the MDN link in the
// module's documentation above, and the MDN link itself.
//
// * If a function or method can throw an exception, make it catchable by adding
// `#[wasm_bindgen(catch)]`.
//
// * Add a new `#[test]` into the appropriate file in the
// `crates/js-sys/tests/wasm/` directory. If the imported function or method
// can throw an exception, make sure to also add test coverage for that case.
//
// * Arguments that are `JsValue`s or imported JavaScript types should be taken
// by reference.
macro_rules! forward_deref_unop {
(impl $imp:ident, $method:ident for $t:ty) => {
impl $imp for $t {
type Output = <&'static $t as $imp>::Output;
#[inline]
fn $method(self) -> Self::Output {
$imp::$method(&self)
}
}
};
}
macro_rules! forward_deref_binop {
(impl $imp:ident, $method:ident for $t:ty) => {
impl<'a> $imp<$t> for &'a $t {
type Output = <&'static $t as $imp<&'static $t>>::Output;
#[inline]
fn $method(self, other: $t) -> Self::Output {
$imp::$method(self, &other)
}
}
impl $imp<&$t> for $t {
type Output = <&'static $t as $imp<&'static $t>>::Output;
#[inline]
fn $method(self, other: &$t) -> Self::Output {
$imp::$method(&self, other)
}
}
impl $imp<$t> for $t {
type Output = <&'static $t as $imp<&'static $t>>::Output;
#[inline]
fn $method(self, other: $t) -> Self::Output {
$imp::$method(&self, &other)
}
}
};
}
macro_rules! forward_js_unop {
(impl $imp:ident, $method:ident for $t:ty) => {
impl $imp for &$t {
type Output = $t;
#[inline]
fn $method(self) -> Self::Output {
$imp::$method(JsValue::as_ref(self)).unchecked_into()
}
}
forward_deref_unop!(impl $imp, $method for $t);
};
}
macro_rules! forward_js_binop {
(impl $imp:ident, $method:ident for $t:ty) => {
impl $imp<&$t> for &$t {
type Output = $t;
#[inline]
fn $method(self, other: &$t) -> Self::Output {
$imp::$method(JsValue::as_ref(self), JsValue::as_ref(other)).unchecked_into()
}
}
forward_deref_binop!(impl $imp, $method for $t);
};
}
macro_rules! sum_product {
($($a:ident)*) => ($(
impl Sum for $a {
#[inline]
fn sum<I: iter::Iterator<Item=Self>>(iter: I) -> Self {
iter.fold(
$a::from(0),
|a, b| a + b,
)
}
}
impl Product for $a {
#[inline]
fn product<I: iter::Iterator<Item=Self>>(iter: I) -> Self {
iter.fold(
$a::from(1),
|a, b| a * b,
)
}
}
impl<'a> Sum<&'a $a> for $a {
fn sum<I: iter::Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold(
$a::from(0),
|a, b| a + b,
)
}
}
impl<'a> Product<&'a $a> for $a {
#[inline]
fn product<I: iter::Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold(
$a::from(1),
|a, b| a * b,
)
}
}
)*)
}
macro_rules! partialord_ord {
($t:ident) => {
impl PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
#[inline]
fn lt(&self, other: &Self) -> bool {
JsValue::as_ref(self).lt(JsValue::as_ref(other))
}
#[inline]
fn le(&self, other: &Self) -> bool {
JsValue::as_ref(self).le(JsValue::as_ref(other))
}
#[inline]
fn ge(&self, other: &Self) -> bool {
JsValue::as_ref(self).ge(JsValue::as_ref(other))
}
#[inline]
fn gt(&self, other: &Self) -> bool {
JsValue::as_ref(self).gt(JsValue::as_ref(other))
}
}
impl Ord for $t {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
if self == other {
Ordering::Equal
} else if self.lt(other) {
Ordering::Less
} else {
Ordering::Greater
}
}
}
};
}
#[wasm_bindgen]
extern "C" {
/// The `decodeURI()` function decodes a Uniform Resource Identifier (URI)
/// previously created by `encodeURI` or by a similar routine.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI)
#[wasm_bindgen(catch, js_name = decodeURI)]
pub fn decode_uri(encoded: &str) -> Result<JsString, JsValue>;
/// The `decodeURIComponent()` function decodes a Uniform Resource Identifier (URI) component
/// previously created by `encodeURIComponent` or by a similar routine.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent)
#[wasm_bindgen(catch, js_name = decodeURIComponent)]
pub fn decode_uri_component(encoded: &str) -> Result<JsString, JsValue>;
/// The `encodeURI()` function encodes a Uniform Resource Identifier (URI)
/// by replacing each instance of certain characters by one, two, three, or
/// four escape sequences representing the UTF-8 encoding of the character
/// (will only be four escape sequences for characters composed of two
/// "surrogate" characters).
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI)
#[wasm_bindgen(js_name = encodeURI)]
pub fn encode_uri(decoded: &str) -> JsString;
/// The `encodeURIComponent()` function encodes a Uniform Resource Identifier (URI) component
/// by replacing each instance of certain characters by one, two, three, or four escape sequences
/// representing the UTF-8 encoding of the character
/// (will only be four escape sequences for characters composed of two "surrogate" characters).
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent)
#[wasm_bindgen(js_name = encodeURIComponent)]
pub fn encode_uri_component(decoded: &str) -> JsString;
/// The `eval()` function evaluates JavaScript code represented as a string.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)
#[wasm_bindgen(catch)]
pub fn eval(js_source_text: &str) -> Result<JsValue, JsValue>;
/// The global `isFinite()` function determines whether the passed value is a finite number.
/// If needed, the parameter is first converted to a number.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite)
#[wasm_bindgen(js_name = isFinite)]
pub fn is_finite(value: &JsValue) -> bool;
/// The `parseInt()` function parses a string argument and returns an integer
/// of the specified radix (the base in mathematical numeral systems), or NaN on error.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt)
#[wasm_bindgen(js_name = parseInt)]
pub fn parse_int(text: &str, radix: u8) -> f64;
/// The `parseFloat()` function parses an argument and returns a floating point number,
/// or NaN on error.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat)
#[wasm_bindgen(js_name = parseFloat)]
pub fn parse_float(text: &str) -> f64;
/// The `escape()` function computes a new string in which certain characters have been
/// replaced by a hexadecimal escape sequence.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape)
#[wasm_bindgen]
pub fn escape(string: &str) -> JsString;
/// The `unescape()` function computes a new string in which hexadecimal escape
/// sequences are replaced with the character that it represents. The escape sequences might
/// be introduced by a function like `escape`. Usually, `decodeURI` or `decodeURIComponent`
/// are preferred over `unescape`.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape)
#[wasm_bindgen]
pub fn unescape(string: &str) -> JsString;
}
// Array
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object, is_type_of = Array::is_array, typescript_type = "Array<any>")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub type Array;
/// Creates a new empty array.
#[wasm_bindgen(constructor)]
pub fn new() -> Array;
/// Creates a new array with the specified length (elements are initialized to `undefined`).
#[wasm_bindgen(constructor)]
pub fn new_with_length(len: u32) -> Array;
/// Retrieves the element at the index, counting from the end if negative
/// (returns `undefined` if the index is out of range).
#[wasm_bindgen(method)]
pub fn at(this: &Array, index: i32) -> JsValue;
/// Retrieves the element at the index (returns `undefined` if the index is out of range).
#[wasm_bindgen(method, structural, indexing_getter)]
pub fn get(this: &Array, index: u32) -> JsValue;
/// Sets the element at the index (auto-enlarges the array if the index is out of range).
#[wasm_bindgen(method, structural, indexing_setter)]
pub fn set(this: &Array, index: u32, value: JsValue);
/// Deletes the element at the index (does nothing if the index is out of range).
///
/// The element at the index is set to `undefined`.
///
/// This does not resize the array, the array will still be the same length.
#[wasm_bindgen(method, structural, indexing_deleter)]
pub fn delete(this: &Array, index: u32);
/// The `Array.from()` method creates a new, shallow-copied `Array` instance
/// from an array-like or iterable object.
#[wasm_bindgen(static_method_of = Array)]
pub fn from(val: &JsValue) -> Array;
/// The `copyWithin()` method shallow copies part of an array to another
/// location in the same array and returns it, without modifying its size.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin)
#[wasm_bindgen(method, js_name = copyWithin)]
pub fn copy_within(this: &Array, target: i32, start: i32, end: i32) -> Array;
/// The `concat()` method is used to merge two or more arrays. This method
/// does not change the existing arrays, but instead returns a new array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat)
#[wasm_bindgen(method)]
pub fn concat(this: &Array, array: &Array) -> Array;
/// The `every()` method tests whether all elements in the array pass the test
/// implemented by the provided function.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
#[wasm_bindgen(method)]
pub fn every(this: &Array, predicate: &mut dyn FnMut(JsValue, u32, Array) -> bool) -> bool;
/// The `fill()` method fills all the elements of an array from a start index
/// to an end index with a static value. The end index is not included.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill)
#[wasm_bindgen(method)]
pub fn fill(this: &Array, value: &JsValue, start: u32, end: u32) -> Array;
/// The `filter()` method creates a new array with all elements that pass the
/// test implemented by the provided function.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
#[wasm_bindgen(method)]
pub fn filter(this: &Array, predicate: &mut dyn FnMut(JsValue, u32, Array) -> bool) -> Array;
/// The `find()` method returns the value of the first element in the array that satisfies
/// the provided testing function. Otherwise `undefined` is returned.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
#[wasm_bindgen(method)]
pub fn find(this: &Array, predicate: &mut dyn FnMut(JsValue, u32, Array) -> bool) -> JsValue;
/// The `findIndex()` method returns the index of the first element in the array that
/// satisfies the provided testing function. Otherwise -1 is returned.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)
#[wasm_bindgen(method, js_name = findIndex)]
pub fn find_index(this: &Array, predicate: &mut dyn FnMut(JsValue, u32, Array) -> bool) -> i32;
/// The `flat()` method creates a new array with all sub-array elements concatenated into it
/// recursively up to the specified depth.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)
#[wasm_bindgen(method)]
pub fn flat(this: &Array, depth: i32) -> Array;
/// The `flatMap()` method first maps each element using a mapping function, then flattens
/// the result into a new array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)
#[wasm_bindgen(method, js_name = flatMap)]
pub fn flat_map(
this: &Array,
callback: &mut dyn FnMut(JsValue, u32, Array) -> Vec<JsValue>,
) -> Array;
/// The `forEach()` method executes a provided function once for each array element.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
#[wasm_bindgen(method, js_name = forEach)]
pub fn for_each(this: &Array, callback: &mut dyn FnMut(JsValue, u32, Array));
/// The `includes()` method determines whether an array includes a certain
/// element, returning true or false as appropriate.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)
#[wasm_bindgen(method)]
pub fn includes(this: &Array, value: &JsValue, from_index: i32) -> bool;
/// The `indexOf()` method returns the first index at which a given element
/// can be found in the array, or -1 if it is not present.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
#[wasm_bindgen(method, js_name = indexOf)]
pub fn index_of(this: &Array, value: &JsValue, from_index: i32) -> i32;
/// The `Array.isArray()` method determines whether the passed value is an Array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
#[wasm_bindgen(static_method_of = Array, js_name = isArray)]
pub fn is_array(value: &JsValue) -> bool;
/// The `join()` method joins all elements of an array (or an array-like object)
/// into a string and returns this string.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)
#[wasm_bindgen(method)]
pub fn join(this: &Array, delimiter: &str) -> JsString;
/// The `lastIndexOf()` method returns the last index at which a given element
/// can be found in the array, or -1 if it is not present. The array is
/// searched backwards, starting at fromIndex.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf)
#[wasm_bindgen(method, js_name = lastIndexOf)]
pub fn last_index_of(this: &Array, value: &JsValue, from_index: i32) -> i32;
/// The length property of an object which is an instance of type Array
/// sets or returns the number of elements in that array. The value is an
/// unsigned, 32-bit integer that is always numerically greater than the
/// highest index in the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length)
#[wasm_bindgen(method, getter, structural)]
pub fn length(this: &Array) -> u32;
/// `map()` calls a provided callback function once for each element in an array,
/// in order, and constructs a new array from the results. callback is invoked
/// only for indexes of the array which have assigned values, including undefined.
/// It is not called for missing elements of the array (that is, indexes that have
/// never been set, which have been deleted or which have never been assigned a value).
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
#[wasm_bindgen(method)]
pub fn map(this: &Array, predicate: &mut dyn FnMut(JsValue, u32, Array) -> JsValue) -> Array;
/// The `Array.of()` method creates a new Array instance with a variable
/// number of arguments, regardless of number or type of the arguments.
///
/// The difference between `Array.of()` and the `Array` constructor is in the
/// handling of integer arguments: `Array.of(7)` creates an array with a single
/// element, `7`, whereas `Array(7)` creates an empty array with a `length`
/// property of `7` (Note: this implies an array of 7 empty slots, not slots
/// with actual undefined values).
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)
///
/// # Notes
///
/// There are a few bindings to `of` in `js-sys`: `of1`, `of2`, etc...
/// with different arities.
#[wasm_bindgen(static_method_of = Array, js_name = of)]
pub fn of1(a: &JsValue) -> Array;
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)
#[wasm_bindgen(static_method_of = Array, js_name = of)]
pub fn of2(a: &JsValue, b: &JsValue) -> Array;
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)
#[wasm_bindgen(static_method_of = Array, js_name = of)]
pub fn of3(a: &JsValue, b: &JsValue, c: &JsValue) -> Array;
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)
#[wasm_bindgen(static_method_of = Array, js_name = of)]
pub fn of4(a: &JsValue, b: &JsValue, c: &JsValue, d: &JsValue) -> Array;
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)
#[wasm_bindgen(static_method_of = Array, js_name = of)]
pub fn of5(a: &JsValue, b: &JsValue, c: &JsValue, d: &JsValue, e: &JsValue) -> Array;
/// The `pop()` method removes the last element from an array and returns that
/// element. This method changes the length of the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop)
#[wasm_bindgen(method)]
pub fn pop(this: &Array) -> JsValue;
/// The `push()` method adds one or more elements to the end of an array and
/// returns the new length of the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)
#[wasm_bindgen(method)]
pub fn push(this: &Array, value: &JsValue) -> u32;
/// The `reduce()` method applies a function against an accumulator and each element in
/// the array (from left to right) to reduce it to a single value.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)
#[wasm_bindgen(method)]
pub fn reduce(
this: &Array,
predicate: &mut dyn FnMut(JsValue, JsValue, u32, Array) -> JsValue,
initial_value: &JsValue,
) -> JsValue;
/// The `reduceRight()` method applies a function against an accumulator and each value
/// of the array (from right-to-left) to reduce it to a single value.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight)
#[wasm_bindgen(method, js_name = reduceRight)]
pub fn reduce_right(
this: &Array,
predicate: &mut dyn FnMut(JsValue, JsValue, u32, Array) -> JsValue,
initial_value: &JsValue,
) -> JsValue;
/// The `reverse()` method reverses an array in place. The first array
/// element becomes the last, and the last array element becomes the first.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)
#[wasm_bindgen(method)]
pub fn reverse(this: &Array) -> Array;
/// The `shift()` method removes the first element from an array and returns
/// that removed element. This method changes the length of the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift)
#[wasm_bindgen(method)]
pub fn shift(this: &Array) -> JsValue;
/// The `slice()` method returns a shallow copy of a portion of an array into
/// a new array object selected from begin to end (end not included).
/// The original array will not be modified.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)
#[wasm_bindgen(method)]
pub fn slice(this: &Array, start: u32, end: u32) -> Array;
/// The `some()` method tests whether at least one element in the array passes the test implemented
/// by the provided function.
/// Note: This method returns false for any condition put on an empty array.
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)
#[wasm_bindgen(method)]
pub fn some(this: &Array, predicate: &mut dyn FnMut(JsValue) -> bool) -> bool;
/// The `sort()` method sorts the elements of an array in place and returns
/// the array. The sort is not necessarily stable. The default sort
/// order is according to string Unicode code points.
///
/// The time and space complexity of the sort cannot be guaranteed as it
/// is implementation dependent.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
#[wasm_bindgen(method)]
pub fn sort(this: &Array) -> Array;
/// The `splice()` method changes the contents of an array by removing existing elements and/or
/// adding new elements.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)
#[wasm_bindgen(method)]
pub fn splice(this: &Array, start: u32, delete_count: u32, item: &JsValue) -> Array;
/// The `toLocaleString()` method returns a string representing the elements of the array.
/// The elements are converted to Strings using their toLocaleString methods and these
/// Strings are separated by a locale-specific String (such as a comma “,”).
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toLocaleString)
#[wasm_bindgen(method, js_name = toLocaleString)]
pub fn to_locale_string(this: &Array, locales: &JsValue, options: &JsValue) -> JsString;
/// The `toString()` method returns a string representing the specified array
/// and its elements.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString)
#[wasm_bindgen(method, js_name = toString)]
pub fn to_string(this: &Array) -> JsString;
/// The `unshift()` method adds one or more elements to the beginning of an
/// array and returns the new length of the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift)
#[wasm_bindgen(method)]
pub fn unshift(this: &Array, value: &JsValue) -> u32;
}
/// Iterator returned by `Array::iter`
#[derive(Debug, Clone)]
pub struct ArrayIter<'a> {
range: std::ops::Range<u32>,
array: &'a Array,
}
impl<'a> std::iter::Iterator for ArrayIter<'a> {
type Item = JsValue;
fn next(&mut self) -> Option<Self::Item> {
let index = self.range.next()?;
Some(self.array.get(index))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}
}
impl<'a> std::iter::DoubleEndedIterator for ArrayIter<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
let index = self.range.next_back()?;
Some(self.array.get(index))
}
}
impl<'a> std::iter::FusedIterator for ArrayIter<'a> {}
impl<'a> std::iter::ExactSizeIterator for ArrayIter<'a> {}
impl Array {
/// Returns an iterator over the values of the JS array.
pub fn iter(&self) -> ArrayIter<'_> {
ArrayIter {
range: 0..self.length(),
array: self,
}
}
/// Converts the JS array into a new Vec.
pub fn to_vec(&self) -> Vec<JsValue> {
let len = self.length();
let mut output = Vec::with_capacity(len as usize);
for i in 0..len {
output.push(self.get(i));
}
output
}
}
// TODO pre-initialize the Array with the correct length using TrustedLen
impl<A> std::iter::FromIterator<A> for Array
where
A: AsRef<JsValue>,
{
fn from_iter<T>(iter: T) -> Array
where
T: IntoIterator<Item = A>,
{
let mut out = Array::new();
out.extend(iter);
out
}
}
impl<A> std::iter::Extend<A> for Array
where
A: AsRef<JsValue>,
{
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = A>,
{
for value in iter {
self.push(value.as_ref());
}
}
}
impl Default for Array {
fn default() -> Self {
Self::new()
}
}
// ArrayBuffer
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object, typescript_type = "ArrayBuffer")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub type ArrayBuffer;
/// The `ArrayBuffer` object is used to represent a generic,
/// fixed-length raw binary data buffer. You cannot directly
/// manipulate the contents of an `ArrayBuffer`; instead, you
/// create one of the typed array objects or a `DataView` object
/// which represents the buffer in a specific format, and use that
/// to read and write the contents of the buffer.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
#[wasm_bindgen(constructor)]
pub fn new(length: u32) -> ArrayBuffer;
/// The byteLength property of an object which is an instance of type ArrayBuffer
/// it's an accessor property whose set accessor function is undefined,
/// meaning that you can only read this property.
/// The value is established when the array is constructed and cannot be changed.
/// This property returns 0 if this ArrayBuffer has been detached.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/byteLength)
#[wasm_bindgen(method, getter, js_name = byteLength)]
pub fn byte_length(this: &ArrayBuffer) -> u32;
/// The `isView()` method returns true if arg is one of the `ArrayBuffer`
/// views, such as typed array objects or a DataView; false otherwise.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView)
#[wasm_bindgen(static_method_of = ArrayBuffer, js_name = isView)]
pub fn is_view(value: &JsValue) -> bool;
/// The `slice()` method returns a new `ArrayBuffer` whose contents
/// are a copy of this `ArrayBuffer`'s bytes from begin, inclusive,
/// up to end, exclusive.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice)
#[wasm_bindgen(method)]
pub fn slice(this: &ArrayBuffer, begin: u32) -> ArrayBuffer;
/// Like `slice()` but with the `end` argument.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice)
#[wasm_bindgen(method, js_name = slice)]
pub fn slice_with_end(this: &ArrayBuffer, begin: u32, end: u32) -> ArrayBuffer;
}
// SharedArrayBuffer
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object, typescript_type = "SharedArrayBuffer")]
#[derive(Clone, Debug)]
pub type SharedArrayBuffer;
/// The `SharedArrayBuffer` object is used to represent a generic,
/// fixed-length raw binary data buffer, similar to the `ArrayBuffer`
/// object, but in a way that they can be used to create views
/// on shared memory. Unlike an `ArrayBuffer`, a `SharedArrayBuffer`
/// cannot become detached.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer)
#[wasm_bindgen(constructor)]
pub fn new(length: u32) -> SharedArrayBuffer;
/// The byteLength accessor property represents the length of
/// an `SharedArrayBuffer` in bytes. This is established when
/// the `SharedArrayBuffer` is constructed and cannot be changed.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/byteLength)
#[wasm_bindgen(method, getter, js_name = byteLength)]
pub fn byte_length(this: &SharedArrayBuffer) -> u32;
/// The `slice()` method returns a new `SharedArrayBuffer` whose contents
/// are a copy of this `SharedArrayBuffer`'s bytes from begin, inclusive,
/// up to end, exclusive.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/slice)
#[wasm_bindgen(method)]
pub fn slice(this: &SharedArrayBuffer, begin: u32) -> SharedArrayBuffer;
/// Like `slice()` but with the `end` argument.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/slice)
#[wasm_bindgen(method, js_name = slice)]
pub fn slice_with_end(this: &SharedArrayBuffer, begin: u32, end: u32) -> SharedArrayBuffer;
}
// Array Iterator
#[wasm_bindgen]
extern "C" {
/// The `keys()` method returns a new Array Iterator object that contains the
/// keys for each index in the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys)
#[wasm_bindgen(method)]
pub fn keys(this: &Array) -> Iterator;
/// The `entries()` method returns a new Array Iterator object that contains
/// the key/value pairs for each index in the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries)
#[wasm_bindgen(method)]
pub fn entries(this: &Array) -> Iterator;
/// The `values()` method returns a new Array Iterator object that
/// contains the values for each index in the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values)
#[wasm_bindgen(method)]
pub fn values(this: &Array) -> Iterator;
}
/// The `Atomics` object provides atomic operations as static methods.
/// They are used with `SharedArrayBuffer` objects.
///
/// The Atomic operations are installed on an `Atomics` module. Unlike
/// the other global objects, `Atomics` is not a constructor. You cannot
/// use it with a new operator or invoke the `Atomics` object as a
/// function. All properties and methods of `Atomics` are static
/// (as is the case with the Math object, for example).
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics)
#[allow(non_snake_case)]
pub mod Atomics {
use super::*;
#[wasm_bindgen]
extern "C" {
/// The static `Atomics.add()` method adds a given value at a given
/// position in the array and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/add)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn add(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;
/// The static `Atomics.and()` method computes a bitwise AND with a given
/// value at a given position in the array, and returns the old value
/// at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/and)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn and(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;
/// The static `Atomics.compareExchange()` method exchanges a given
/// replacement value at a given position in the array, if a given expected
/// value equals the old value. It returns the old value at that position
/// whether it was equal to the expected value or not.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/compareExchange)
#[wasm_bindgen(js_namespace = Atomics, catch, js_name = compareExchange)]
pub fn compare_exchange(
typed_array: &JsValue,
index: u32,
expected_value: i32,
replacement_value: i32,
) -> Result<i32, JsValue>;
/// The static `Atomics.exchange()` method stores a given value at a given
/// position in the array and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/exchange)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn exchange(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;
/// The static `Atomics.isLockFree()` method is used to determine
/// whether to use locks or atomic operations. It returns true,
/// if the given size is one of the `BYTES_PER_ELEMENT` property
/// of integer `TypedArray` types.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/isLockFree)
#[wasm_bindgen(js_namespace = Atomics, js_name = isLockFree)]
pub fn is_lock_free(size: u32) -> bool;
/// The static `Atomics.load()` method returns a value at a given
/// position in the array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/load)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn load(typed_array: &JsValue, index: u32) -> Result<i32, JsValue>;
/// The static `Atomics.notify()` method notifies up some agents that
/// are sleeping in the wait queue.
/// Note: This operation works with a shared `Int32Array` only.
/// If `count` is not provided, notifies all the agents in the queue.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/notify)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn notify(typed_array: &Int32Array, index: u32) -> Result<u32, JsValue>;
/// Notifies up to `count` agents in the wait queue.
#[wasm_bindgen(js_namespace = Atomics, catch, js_name = notify)]
pub fn notify_with_count(
typed_array: &Int32Array,
index: u32,
count: u32,
) -> Result<u32, JsValue>;
/// The static `Atomics.or()` method computes a bitwise OR with a given value
/// at a given position in the array, and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/or)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn or(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;
/// The static `Atomics.store()` method stores a given value at the given
/// position in the array and returns that value.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/store)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn store(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;
/// The static `Atomics.sub()` method substracts a given value at a
/// given position in the array and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/sub)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn sub(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;
/// The static `Atomics.wait()` method verifies that a given
/// position in an `Int32Array` still contains a given value
/// and if so sleeps, awaiting a wakeup or a timeout.
/// It returns a string which is either "ok", "not-equal", or "timed-out".
/// Note: This operation only works with a shared `Int32Array`
/// and may not be allowed on the main thread.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn wait(typed_array: &Int32Array, index: u32, value: i32) -> Result<JsString, JsValue>;
/// Like `wait()`, but with timeout
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait)
#[wasm_bindgen(js_namespace = Atomics, catch, js_name = wait)]
pub fn wait_with_timeout(
typed_array: &Int32Array,
index: u32,
value: i32,
timeout: f64,
) -> Result<JsString, JsValue>;
/// The static `Atomics.xor()` method computes a bitwise XOR
/// with a given value at a given position in the array,
/// and returns the old value at that position.
/// This atomic operation guarantees that no other write happens
/// until the modified value is written back.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/xor)
#[wasm_bindgen(js_namespace = Atomics, catch)]
pub fn xor(typed_array: &JsValue, index: u32, value: i32) -> Result<i32, JsValue>;
}
}
// BigInt
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object, is_type_of = |v| v.is_bigint(), typescript_type = "bigint")]
#[derive(Clone, PartialEq, Eq)]
pub type BigInt;
#[wasm_bindgen(catch, js_name = BigInt)]
fn new_bigint(value: &JsValue) -> Result<BigInt, Error>;
#[wasm_bindgen(js_name = BigInt)]
fn new_bigint_unchecked(value: &JsValue) -> BigInt;
/// Clamps a BigInt value to a signed integer value, and returns that value.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asIntN)
#[wasm_bindgen(static_method_of = BigInt, js_name = asIntN)]
pub fn as_int_n(bits: f64, bigint: &BigInt) -> BigInt;
/// Clamps a BigInt value to an unsigned integer value, and returns that value.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asUintN)
#[wasm_bindgen(static_method_of = BigInt, js_name = asUintN)]
pub fn as_uint_n(bits: f64, bigint: &BigInt) -> BigInt;
/// Returns a string with a language-sensitive representation of this BigInt value. Overrides the [`Object.prototype.toLocaleString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString) method.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString)
#[wasm_bindgen(method, js_name = toLocaleString)]
pub fn to_locale_string(this: &BigInt, locales: &JsValue, options: &JsValue) -> JsString;
/// Returns a string representing this BigInt value in the specified radix (base). Overrides the [`Object.prototype.toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString) method.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString)
#[wasm_bindgen(catch, method, js_name = toString)]
pub fn to_string(this: &BigInt, radix: u8) -> Result<JsString, RangeError>;
#[wasm_bindgen(method, js_name = toString)]
fn to_string_unchecked(this: &BigInt, radix: u8) -> String;
/// Returns this BigInt value. Overrides the [`Object.prototype.valueOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf) method.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/valueOf)
#[wasm_bindgen(method, js_name = valueOf)]