mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 11:02:11 +03:00
add JsString binding to src/js.rs and tests
This commit is contained in:
parent
2cfffc65d7
commit
5ae6ee7aec
14
src/js.rs
14
src/js.rs
@ -255,3 +255,17 @@ extern {
|
||||
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
|
||||
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
|
||||
}
|
||||
|
||||
// String
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
#[wasm_bindgen(js_name = String)]
|
||||
pub type JsString;
|
||||
|
||||
/// The slice() method extracts a section of a string and returns it as a
|
||||
/// new string, without modifying the original string.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
|
||||
#[wasm_bindgen(method, js_class = "String")]
|
||||
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
|
||||
}
|
32
tests/all/js_globals/JsString.rs
Executable file
32
tests/all/js_globals/JsString.rs
Executable file
@ -0,0 +1,32 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use project;
|
||||
|
||||
#[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::JsString, start: u32, end: u32) -> js::JsString {
|
||||
this.slice(start, end)
|
||||
}
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let characters = "acxn18";
|
||||
let subset = wasm.create_slice(characters, 1, 3);
|
||||
|
||||
assert.equal(subset, "cx");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
@ -5,6 +5,7 @@ use super::project;
|
||||
mod Object;
|
||||
mod Array;
|
||||
mod ArrayIterator;
|
||||
mod JsString;
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
|
Loading…
Reference in New Issue
Block a user