add binding for reverse

This commit is contained in:
Matt Long 2018-06-20 18:00:58 -04:00
parent 4611d7bdba
commit d705cd8bbf
2 changed files with 37 additions and 0 deletions

View File

@ -151,4 +151,11 @@ extern {
/// 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;
}

View File

@ -263,4 +263,34 @@ fn push() {
}
"#)
.test()
}
#[test]
fn reverse() {
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 reverse_array(this: &js::Array) -> js::Array {
this.reverse()
}
"#)
.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 reversed = wasm.reverse_array(characters);
assert.equal(reversed[0], 2);
assert.equal(reversed[5], 8);
}
"#)
.test()
}