diff --git a/src/js.rs b/src/js.rs index 079d65af..e3b0f217 100644 --- a/src/js.rs +++ b/src/js.rs @@ -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; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index da37a2da..8e89f3e8 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -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() +} \ No newline at end of file