mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-24 13:42:14 +00:00
add binding for slice
This commit is contained in:
parent
0b80888c0d
commit
e8bb0c2f98
@ -118,4 +118,12 @@ extern {
|
|||||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
|
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
|
||||||
#[wasm_bindgen(method)]
|
#[wasm_bindgen(method)]
|
||||||
pub fn join(this: &Array, delimiter: &str) -> String;
|
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;
|
||||||
}
|
}
|
@ -109,3 +109,33 @@ fn join() {
|
|||||||
"#)
|
"#)
|
||||||
.test()
|
.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()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user