Skip to content

Commit a804a0e

Browse files
committed
Merge branch 'rollup'
2 parents 9e01e67 + 99ee74d commit a804a0e

File tree

3 files changed

+234
-72
lines changed

3 files changed

+234
-72
lines changed

src/js.rs

Lines changed: 113 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -67,134 +67,127 @@ extern {
6767
pub fn eval(js_source_text: &str) -> Result<JsValue, JsValue>;
6868
}
6969

70-
// Object.
70+
// Array
7171
#[wasm_bindgen]
7272
extern {
73-
pub type Object;
73+
pub type Array;
7474

75-
/// The Object constructor creates an object wrapper.
75+
/// The copyWithin() method shallow copies part of an array to another location in the same
76+
/// array and returns it, without modifying its size.
7677
///
77-
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
78-
#[wasm_bindgen(constructor)]
79-
pub fn new() -> Object;
78+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
79+
#[wasm_bindgen(method, js_name = copyWithin)]
80+
pub fn copy_within(this: &Array, target: i32, start: i32, end: i32) -> Array;
8081

81-
/// The `hasOwnProperty()` method returns a boolean indicating whether the
82-
/// object has the specified property as its own property (as opposed to
83-
/// inheriting it).
82+
///The concat() method is used to merge two or more arrays. This method
83+
/// does not change the existing arrays, but instead returns a new array.
8484
///
85-
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
86-
#[wasm_bindgen(method, js_name = hasOwnProperty)]
87-
pub fn has_own_property(this: &Object, property: &JsValue) -> bool;
85+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
86+
#[wasm_bindgen(method)]
87+
pub fn concat(this: &Array, array: &Array) -> Array;
8888

89-
/// The toString() method returns a string representing the object.
89+
/// The fill() method fills all the elements of an array from a start index to an
90+
/// end index with a static value. The end index is not included.
9091
///
91-
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
92-
#[wasm_bindgen(method, js_name = toString)]
93-
pub fn to_string(this: &Object) -> String;
94-
95-
/// The isPrototypeOf() method checks if an object exists in another
96-
/// object's prototype chain.
97-
///
98-
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf
99-
#[wasm_bindgen(method, js_name = isPrototypeOf)]
100-
pub fn is_prototype_of(this: &Object, value: &JsValue) -> bool;
101-
102-
/// The propertyIsEnumerable() method returns a Boolean indicating
103-
/// whether the specified property is enumerable.
104-
///
105-
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
106-
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
107-
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
108-
}
92+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
93+
#[wasm_bindgen(method)]
94+
pub fn fill(this: &Array, value: JsValue, start: u32, end: u32) -> Array;
10995

110-
// Array
111-
#[wasm_bindgen]
112-
extern {
113-
pub type Array;
96+
/// The length property of an object which is an instance of type Array sets or returns the number of elements in that array.
97+
/// The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
98+
///
99+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length
100+
#[wasm_bindgen(method, getter, structural)]
101+
pub fn length(this: &Array) -> u32;
114102

115-
/// 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.
103+
/// The indexOf() method returns the first index at which a given element can be
104+
/// found in the array, or -1 if it is not present.
116105
///
117106
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
118107
#[wasm_bindgen(method, js_name = indexOf)]
119108
pub fn index_of(this: &Array, value: JsValue, from_index: i32) -> i32;
120109

121-
/// 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.
122-
/// The array is searched backwards, starting at fromIndex.
110+
/// The includes() method determines whether an array includes a certain element,
111+
/// returning true or false as appropriate.
123112
///
124-
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
125-
#[wasm_bindgen(method, js_name = lastIndexOf)]
126-
pub fn last_index_of(this: &Array, value: JsValue, from_index: i32) -> i32;
113+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
114+
#[wasm_bindgen(method)]
115+
pub fn includes(this: &Array, value: JsValue, from_index: i32) -> bool;
127116

128-
/// The join() method joins all elements of an array (or an array-like object) into a string and returns this string.
117+
/// The join() method joins all elements of an array (or an array-like object)
118+
/// into a string and returns this string.
129119
///
130120
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
131121
#[wasm_bindgen(method)]
132122
pub fn join(this: &Array, delimiter: &str) -> String;
133123

134-
/// The slice() method returns a shallow copy of a portion of an array into a new array
135-
/// object selected from begin to end (end not included).
136-
/// The original array will not be modified.
137-
///
138-
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
139-
#[wasm_bindgen(method)]
140-
pub fn slice(this: &Array, start: u32, end: u32) -> Array;
141-
142-
/// The fill() method fills all the elements of an array from a start index to an end index with a static value.
143-
/// The end index is not included.
124+
/// The lastIndexOf() method returns the last index at which a given element can
125+
/// be found in the array, or -1 if it is not present. The array is searched
126+
/// backwards, starting at fromIndex.
144127
///
145-
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
146-
#[wasm_bindgen(method)]
147-
pub fn fill(this: &Array, value: JsValue, start: u32, end: u32) -> Array;
148-
149-
/// The copyWithin() method shallow copies part of an array to another location in the same array and returns it, without modifying its size.
150-
///
151-
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
152-
#[wasm_bindgen(method, js_name = copyWithin)]
153-
pub fn copy_within(this: &Array, target: i32, start: i32, end: i32) -> Array;
128+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
129+
#[wasm_bindgen(method, js_name = lastIndexOf)]
130+
pub fn last_index_of(this: &Array, value: JsValue, from_index: i32) -> i32;
154131

155-
/// The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
132+
/// The pop() method removes the last element from an array and returns that element.
133+
/// This method changes the length of the array.
156134
///
157135
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
158136
#[wasm_bindgen(method)]
159137
pub fn pop(this: &Array) -> JsValue;
160138

161-
/// The push() method adds one or more elements to the end of an array and returns the new length of the array.
139+
/// The push() method adds one or more elements to the end of an array and returns
140+
/// the new length of the array.
162141
///
163142
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
164143
#[wasm_bindgen(method)]
165144
pub fn push(this: &Array, value: JsValue) -> u32;
166145

167146
/// The reverse() method reverses an array in place.
168147
/// The first array element becomes the last, and the last array element becomes the first.
169-
///
148+
///
170149
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
171150
#[wasm_bindgen(method)]
172151
pub fn reverse(this: &Array) -> Array;
173152

153+
/// The slice() method returns a shallow copy of a portion of an array into a new array
154+
/// object selected from begin to end (end not included).
155+
/// The original array will not be modified.
156+
///
157+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
158+
#[wasm_bindgen(method)]
159+
pub fn slice(this: &Array, start: u32, end: u32) -> Array;
160+
174161
/// The shift() method removes the first element from an array and returns that removed element.
175162
/// This method changes the length of the array.
176163
///
177164
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
178165
#[wasm_bindgen(method)]
179166
pub fn shift(this: &Array) -> JsValue;
180167

181-
/// The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
182-
///
183-
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
168+
/// The sort() method sorts the elements of an array in place and returns
169+
/// the array. The sort is not necessarily stable. The default sort
170+
/// order is according to string Unicode code points.
171+
///
172+
/// The time and space complexity of the sort cannot be guaranteed as it
173+
/// is implementation dependent.
174+
///
175+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
184176
#[wasm_bindgen(method)]
185-
pub fn unshift(this: &Array, value: JsValue) -> u32;
177+
pub fn sort(this: &Array) -> Array;
186178

187179
/// The toString() method returns a string representing the specified array and its elements.
188180
///
189181
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
190182
#[wasm_bindgen(method, js_name = toString)]
191183
pub fn to_string(this: &Array) -> String;
192184

193-
/// The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
185+
/// The unshift() method adds one or more elements to the beginning of an array
186+
/// and returns the new length of the array.
194187
///
195-
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
188+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
196189
#[wasm_bindgen(method)]
197-
pub fn includes(this: &Array, value: JsValue, from_index: i32) -> bool;
190+
pub fn unshift(this: &Array, value: JsValue) -> u32;
198191
}
199192

200193
// Array Iterator
@@ -213,4 +206,52 @@ extern {
213206
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries
214207
#[wasm_bindgen(method)]
215208
pub fn entries(this: &Array) -> ArrayIterator;
216-
}
209+
}
210+
211+
// Object.
212+
#[wasm_bindgen]
213+
extern {
214+
pub type Object;
215+
216+
/// The Object constructor creates an object wrapper.
217+
///
218+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
219+
#[wasm_bindgen(constructor)]
220+
pub fn new() -> Object;
221+
222+
/// The `hasOwnProperty()` method returns a boolean indicating whether the
223+
/// object has the specified property as its own property (as opposed to
224+
/// inheriting it).
225+
///
226+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
227+
#[wasm_bindgen(method, js_name = hasOwnProperty)]
228+
pub fn has_own_property(this: &Object, property: &JsValue) -> bool;
229+
230+
/// The toLocaleString() method returns a string representing the object.
231+
/// This method is meant to be overridden by derived objects for locale-specific
232+
/// purposes.
233+
///
234+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString
235+
#[wasm_bindgen(method, js_name = toLocaleString)]
236+
pub fn to_locale_string(this: &Object) -> String;
237+
238+
/// The toString() method returns a string representing the object.
239+
///
240+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
241+
#[wasm_bindgen(method, js_name = toString)]
242+
pub fn to_string(this: &Object) -> String;
243+
244+
/// The isPrototypeOf() method checks if an object exists in another
245+
/// object's prototype chain.
246+
///
247+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf
248+
#[wasm_bindgen(method, js_name = isPrototypeOf)]
249+
pub fn is_prototype_of(this: &Object, value: &JsValue) -> bool;
250+
251+
/// The propertyIsEnumerable() method returns a Boolean indicating
252+
/// whether the specified property is enumerable.
253+
///
254+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
255+
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
256+
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
257+
}

tests/all/js_globals/Array.rs

100644100755
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@ fn index_of() {
4040
.test()
4141
}
4242

43+
#[test]
44+
fn sort() {
45+
project()
46+
.file("src/lib.rs", r#"
47+
#![feature(proc_macro, wasm_custom_section)]
48+
49+
extern crate wasm_bindgen;
50+
use wasm_bindgen::prelude::*;
51+
use wasm_bindgen::js;
52+
53+
#[wasm_bindgen]
54+
pub fn sort_array(this: &js::Array) -> js::Array {
55+
this.sort()
56+
}
57+
58+
"#)
59+
.file("test.ts", r#"
60+
import * as assert from "assert";
61+
import * as wasm from "./out";
62+
63+
export function test() {
64+
let numbers = [3, 1, 6, 2];
65+
let sorted = wasm.sort_array(numbers);
66+
67+
assert.deepStrictEqual(sorted, [1, 2, 3, 6])
68+
}
69+
"#)
70+
.test()
71+
}
72+
4373
#[test]
4474
fn last_index_of() {
4575
project()
@@ -422,3 +452,67 @@ fn includes() {
422452
"#)
423453
.test()
424454
}
455+
456+
#[test]
457+
fn concat() {
458+
project()
459+
.file("src/lib.rs", r#"
460+
#![feature(proc_macro, wasm_custom_section)]
461+
462+
extern crate wasm_bindgen;
463+
use wasm_bindgen::prelude::*;
464+
use wasm_bindgen::js;
465+
466+
#[wasm_bindgen]
467+
pub fn array_concat(this: &js::Array, arr: &js::Array) -> js::Array {
468+
this.concat(arr)
469+
}
470+
471+
"#)
472+
.file("test.ts", r#"
473+
import * as assert from "assert";
474+
import * as wasm from "./out";
475+
476+
export function test() {
477+
let arr1 = [1, 2, 3];
478+
let arr2 = [4, 5, 6];
479+
480+
let new_array = wasm.array_concat(arr1, arr2)
481+
assert.deepStrictEqual(new_array, [1, 2, 3, 4, 5, 6]);
482+
}
483+
"#)
484+
.test()
485+
}
486+
487+
#[test]
488+
fn length() {
489+
project()
490+
.file("src/lib.rs", r#"
491+
#![feature(proc_macro, wasm_custom_section)]
492+
493+
extern crate wasm_bindgen;
494+
use wasm_bindgen::prelude::*;
495+
use wasm_bindgen::js;
496+
497+
#[wasm_bindgen]
498+
pub fn array_length(this: &js::Array) -> u32 {
499+
this.length()
500+
}
501+
502+
"#)
503+
.file("test.ts", r#"
504+
import * as assert from "assert";
505+
import * as wasm from "./out";
506+
507+
export function test() {
508+
let characters = [8, 5, 4, 3, 1, 2]
509+
let charactersLength = wasm.array_length(characters);
510+
assert.equal(charactersLength, 6);
511+
512+
var empty : number[] = [];
513+
let emptyLength = wasm.array_length(empty);
514+
assert.equal(emptyLength, 0);
515+
}
516+
"#)
517+
.test()
518+
}

tests/all/js_globals/Object.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,30 @@ fn property_is_enumerable() {
157157
"#)
158158
.test()
159159
}
160+
161+
#[test]
162+
fn to_locale_string() {
163+
project()
164+
.file("src/lib.rs", r#"
165+
#![feature(proc_macro, wasm_custom_section)]
166+
167+
extern crate wasm_bindgen;
168+
use wasm_bindgen::prelude::*;
169+
use wasm_bindgen::js;
170+
171+
#[wasm_bindgen]
172+
pub fn to_locale_string() -> String {
173+
let object = js::Object::new();
174+
object.to_locale_string()
175+
}
176+
"#)
177+
.file("test.ts", r#"
178+
import * as assert from "assert";
179+
import * as wasm from "./out";
180+
181+
export function test() {
182+
assert.equal(wasm.to_locale_string(), "[object Object]");
183+
}
184+
"#)
185+
.test()
186+
}

0 commit comments

Comments
 (0)