Skip to content

Commit 5ae6ee7

Browse files
committed
add JsString binding to src/js.rs and tests
1 parent 2cfffc6 commit 5ae6ee7

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/js.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,17 @@ extern {
255255
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
256256
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
257257
}
258+
259+
// String
260+
#[wasm_bindgen]
261+
extern {
262+
#[wasm_bindgen(js_name = String)]
263+
pub type JsString;
264+
265+
/// The slice() method extracts a section of a string and returns it as a
266+
/// new string, without modifying the original string.
267+
///
268+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
269+
#[wasm_bindgen(method, js_class = "String")]
270+
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
271+
}

tests/all/js_globals/JsString.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![allow(non_snake_case)]
2+
3+
use project;
4+
5+
#[test]
6+
fn slice() {
7+
project()
8+
.file("src/lib.rs", r#"
9+
#![feature(proc_macro, wasm_custom_section)]
10+
11+
extern crate wasm_bindgen;
12+
use wasm_bindgen::prelude::*;
13+
use wasm_bindgen::js;
14+
15+
#[wasm_bindgen]
16+
pub fn create_slice(this: &js::JsString, start: u32, end: u32) -> js::JsString {
17+
this.slice(start, end)
18+
}
19+
"#)
20+
.file("test.ts", r#"
21+
import * as assert from "assert";
22+
import * as wasm from "./out";
23+
24+
export function test() {
25+
let characters = "acxn18";
26+
let subset = wasm.create_slice(characters, 1, 3);
27+
28+
assert.equal(subset, "cx");
29+
}
30+
"#)
31+
.test()
32+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::project;
55
mod Object;
66
mod Array;
77
mod ArrayIterator;
8+
mod JsString;
89

910
#[test]
1011
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)