From bf56d5815b77d18dc56be65714d399823d40c1f2 Mon Sep 17 00:00:00 2001 From: Jonathan Sundqvist Date: Sat, 23 Jun 2018 17:38:13 +0200 Subject: [PATCH] Add the binding of to_string to Number --- src/js.rs | 7 +++++++ tests/all/js_globals/Number.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/js.rs b/src/js.rs index cff0402c..3d4414aa 100644 --- a/src/js.rs +++ b/src/js.rs @@ -213,6 +213,13 @@ extern { extern { pub type Number; + /// The toString() method returns a string representing the + /// specified Number object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString + #[wasm_bindgen(catch, method, js_name = toString)] + pub fn to_string(this: &Number, radix: u8) -> Result; + /// The valueOf() method returns the wrapped primitive value of /// a Number object. /// diff --git a/tests/all/js_globals/Number.rs b/tests/all/js_globals/Number.rs index a78051ea..96fe745a 100644 --- a/tests/all/js_globals/Number.rs +++ b/tests/all/js_globals/Number.rs @@ -3,6 +3,40 @@ use super::project; +#[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 to_string(this: &js::Number, radix: u8) -> String { + let result = this.to_string(radix); + let result = match result { + Ok(num) => num, + Err(_err) => "RangeError".to_string() + }; + result + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let number = 42; + assert.equal(wasm.to_string(number, 10), "42"); + assert.equal(wasm.to_string(233, 16), "e9"); + assert.equal(wasm.to_string(number, 100), "RangeError"); + } + "#) + .test() +} + #[test] fn value_of() { project()