From d155136f0eddf747b48b8218c48f62237fad0c55 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:49:34 -0400 Subject: [PATCH] add binding for fill --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index e3b0f217..fc614e4c 100644 --- a/src/js.rs +++ b/src/js.rs @@ -126,4 +126,11 @@ extern { /// 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; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 8e89f3e8..f83f56e2 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -138,4 +138,34 @@ fn slice() { } "#) .test() +} + +#[test] +fn fill() { + 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 fill_with(this: &js::Array, value: JsValue, start: u32, end: u32) -> js::Array { + this.fill(value, 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.fill_with(characters, 0, 0, 3); + + assert.equal(subset[0], 0); + assert.equal(subset[4], 1); + } + "#) + .test() } \ No newline at end of file