Add the binding of to_string to Number

This commit is contained in:
Jonathan Sundqvist 2018-06-23 17:38:13 +02:00
parent a7f8e071fe
commit bf56d5815b
2 changed files with 41 additions and 0 deletions

View File

@ -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<String, JsValue>;
/// The valueOf() method returns the wrapped primitive value of
/// a Number object.
///

View File

@ -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()