add JsString binding to src/js.rs and tests

This commit is contained in:
kzvi 2018-06-22 17:51:44 -07:00
parent 2cfffc65d7
commit 5ae6ee7aec
3 changed files with 47 additions and 0 deletions

View File

@ -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;
}

View 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()
}

View File

@ -5,6 +5,7 @@ use super::project;
mod Object;
mod Array;
mod ArrayIterator;
mod JsString;
#[test]
#[cfg(feature = "std")]