From 5ae6ee7aec9a7ae9e821ae5fd62cf3dcf7011421 Mon Sep 17 00:00:00 2001 From: kzvi Date: Fri, 22 Jun 2018 17:51:44 -0700 Subject: [PATCH] add JsString binding to src/js.rs and tests --- src/js.rs | 14 ++++++++++++++ tests/all/js_globals/JsString.rs | 32 ++++++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 47 insertions(+) create mode 100755 tests/all/js_globals/JsString.rs diff --git a/src/js.rs b/src/js.rs index 56a4dd2c..c3fa8e62 100644 --- a/src/js.rs +++ b/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; +} \ No newline at end of file diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs new file mode 100755 index 00000000..112f2d2b --- /dev/null +++ b/tests/all/js_globals/JsString.rs @@ -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() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index d9c58b5c..b03c3ae4 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -5,6 +5,7 @@ use super::project; mod Object; mod Array; mod ArrayIterator; +mod JsString; #[test] #[cfg(feature = "std")]