add binding for slice

This commit is contained in:
Matt Long 2018-06-20 17:46:10 -04:00
parent 0b80888c0d
commit e8bb0c2f98
2 changed files with 38 additions and 0 deletions

View File

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

View File

@ -109,3 +109,33 @@ fn join() {
"#)
.test()
}
#[test]
fn slice() {
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 create_slice(this: &js::Array, start: u32, end: u32) -> js::Array {
this.slice(start, end)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let characters = ["a", "c", "x", "n", 1, "8"];
let subset = wasm.create_slice(characters, 1, 3);
assert.equal(subset[0], "c");
assert.equal(subset[1], "x");
}
"#)
.test()
}