Merge branch 'rollup'

This commit is contained in:
Nick Fitzgerald 2018-06-22 10:57:27 -07:00
commit a804a0e634
3 changed files with 272 additions and 110 deletions

261
src/js.rs
View File

@ -67,6 +67,147 @@ extern {
pub fn eval(js_source_text: &str) -> Result<JsValue, JsValue>;
}
// Array
#[wasm_bindgen]
extern {
pub type Array;
/// The copyWithin() method shallow copies part of an array to another location in the same
/// array and returns it, without modifying its size.
///
/// http://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.
///
/// 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 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.
///
/// http://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 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.
///
/// 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;
/// 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.
///
/// http://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 includes() method determines whether an array includes a certain element,
/// returning true or false as appropriate.
///
/// http://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 join() method joins all elements of an array (or an array-like object)
/// into a string and returns this string.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
#[wasm_bindgen(method)]
pub fn join(this: &Array, delimiter: &str) -> String;
/// 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.
///
/// http://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 pop() method removes the last element from an array and returns that element.
/// This method changes the length of the array.
///
/// http://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.
///
/// http://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 reverse() method reverses an array in place.
/// The first array element becomes the last, and the last array element becomes the first.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
#[wasm_bindgen(method)]
pub fn reverse(this: &Array) -> Array;
/// 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.
///
/// http://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 shift() method removes the first element from an array and returns that removed element.
/// This method changes the length of the array.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
#[wasm_bindgen(method)]
pub fn shift(this: &Array) -> JsValue;
/// 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.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
#[wasm_bindgen(method)]
pub fn sort(this: &Array) -> Array;
/// The toString() method returns a string representing the specified array and its elements.
///
/// http://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) -> String;
/// The unshift() method adds one or more elements to the beginning of an array
/// and returns the new length of the array.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
#[wasm_bindgen(method)]
pub fn unshift(this: &Array, value: JsValue) -> u32;
}
// Array Iterator
#[wasm_bindgen]
extern {
pub type ArrayIterator;
/// The keys() method returns a new Array Iterator object that contains the keys for each index in the array.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys
#[wasm_bindgen(method)]
pub fn keys(this: &Array) -> ArrayIterator;
/// The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries
#[wasm_bindgen(method)]
pub fn entries(this: &Array) -> ArrayIterator;
}
// Object.
#[wasm_bindgen]
extern {
@ -86,6 +227,14 @@ extern {
#[wasm_bindgen(method, js_name = hasOwnProperty)]
pub fn has_own_property(this: &Object, property: &JsValue) -> bool;
/// The toLocaleString() method returns a string representing the object.
/// This method is meant to be overridden by derived objects for locale-specific
/// purposes.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString
#[wasm_bindgen(method, js_name = toLocaleString)]
pub fn to_locale_string(this: &Object) -> String;
/// The toString() method returns a string representing the object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
@ -94,123 +243,15 @@ extern {
/// The isPrototypeOf() method checks if an object exists in another
/// object's prototype chain.
///
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf
#[wasm_bindgen(method, js_name = isPrototypeOf)]
pub fn is_prototype_of(this: &Object, value: &JsValue) -> bool;
/// The propertyIsEnumerable() method returns a Boolean indicating
/// whether the specified property is enumerable.
///
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
}
// Array
#[wasm_bindgen]
extern {
pub type Array;
/// 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.
///
/// http://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 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.
///
/// http://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 join() method joins all elements of an array (or an array-like object) into a string and returns this string.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
#[wasm_bindgen(method)]
pub fn join(this: &Array, delimiter: &str) -> String;
/// 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.
///
/// http://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 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.
///
/// http://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 copyWithin() method shallow copies part of an array to another location in the same array and returns it, without modifying its size.
///
/// http://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 pop() method removes the last element from an array and returns that element. This method changes the length of the array.
///
/// http://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.
///
/// http://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 reverse() method reverses an array in place.
/// The first array element becomes the last, and the last array element becomes the first.
///
/// http://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.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
#[wasm_bindgen(method)]
pub fn shift(this: &Array) -> JsValue;
/// The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
#[wasm_bindgen(method)]
pub fn unshift(this: &Array, value: JsValue) -> u32;
/// The toString() method returns a string representing the specified array and its elements.
///
/// http://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) -> String;
/// The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
///
/// http://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;
}
// Array Iterator
#[wasm_bindgen]
extern {
pub type ArrayIterator;
/// The keys() method returns a new Array Iterator object that contains the keys for each index in the array.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys
#[wasm_bindgen(method)]
pub fn keys(this: &Array) -> ArrayIterator;
/// The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries
#[wasm_bindgen(method)]
pub fn entries(this: &Array) -> ArrayIterator;
}

94
tests/all/js_globals/Array.rs Normal file → Executable file
View File

@ -40,6 +40,36 @@ fn index_of() {
.test()
}
#[test]
fn sort() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn sort_array(this: &js::Array) -> js::Array {
this.sort()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let numbers = [3, 1, 6, 2];
let sorted = wasm.sort_array(numbers);
assert.deepStrictEqual(sorted, [1, 2, 3, 6])
}
"#)
.test()
}
#[test]
fn last_index_of() {
project()
@ -422,3 +452,67 @@ fn includes() {
"#)
.test()
}
#[test]
fn concat() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn array_concat(this: &js::Array, arr: &js::Array) -> js::Array {
this.concat(arr)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let new_array = wasm.array_concat(arr1, arr2)
assert.deepStrictEqual(new_array, [1, 2, 3, 4, 5, 6]);
}
"#)
.test()
}
#[test]
fn length() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn array_length(this: &js::Array) -> u32 {
this.length()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let characters = [8, 5, 4, 3, 1, 2]
let charactersLength = wasm.array_length(characters);
assert.equal(charactersLength, 6);
var empty : number[] = [];
let emptyLength = wasm.array_length(empty);
assert.equal(emptyLength, 0);
}
"#)
.test()
}

View File

@ -157,3 +157,30 @@ fn property_is_enumerable() {
"#)
.test()
}
#[test]
fn to_locale_string() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn to_locale_string() -> String {
let object = js::Object::new();
object.to_locale_string()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
assert.equal(wasm.to_locale_string(), "[object Object]");
}
"#)
.test()
}