diff --git a/src/js.rs b/src/js.rs index 6e4500608..b623daf88 100644 --- a/src/js.rs +++ b/src/js.rs @@ -644,6 +644,12 @@ extern { #[wasm_bindgen(method, js_class = "String")] pub fn substr(this: &JsString, start: i32, length: i32) -> JsString; + /// The toString() method returns a string representing the specified object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString + #[wasm_bindgen(method, js_class = "String", js_name = toString)] + pub fn to_string(this: &JsString) -> JsString; + /// The trim() method removes whitespace from both ends of a string. /// Whitespace in this context is all the whitespace characters /// (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.). @@ -679,6 +685,12 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart #[wasm_bindgen(method, js_class = "String", js_name = trimLeft)] pub fn trim_left(this: &JsString) -> JsString; + + /// The valueOf() method returns the primitive value of a String object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/valueOf + #[wasm_bindgen(method, js_class = "String", js_name = valueOf)] + pub fn value_of(this: &JsString) -> JsString; } impl<'a> From<&'a str> for JsString { diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs index 190adf9e6..07579b7c7 100644 --- a/tests/all/js_globals/JsString.rs +++ b/tests/all/js_globals/JsString.rs @@ -360,6 +360,33 @@ fn substr() { .test() } +#[test] +fn to_string() { + 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 string_to_string(this: &js::JsString) -> js::JsString { + this.to_string() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let greeting = 'Hello world!'; + assert.equal(wasm.string_to_string(greeting), 'Hello world!'); + } + "#) + .test() +} + #[test] fn trim() { project() @@ -454,4 +481,31 @@ fn trim_start_and_trim_left() { } "#) .test() +} + +#[test] +fn value_of() { + 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 string_value_of(this: &js::JsString) -> js::JsString { + this.value_of() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let greeting = new String('Hello world!'); + assert.equal(wasm.string_value_of(greeting), 'Hello world!'); + } + "#) + .test() } \ No newline at end of file