From a7f8e071fec208beb20304d436275bd3f9bf85cb Mon Sep 17 00:00:00 2001 From: Jonathan Sundqvist Date: Sat, 23 Jun 2018 14:43:43 +0200 Subject: [PATCH 1/4] Add the binding valueOf to number --- src/js.rs | 16 +++++++++++++++- tests/all/js_globals/Number.rs | 32 ++++++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/all/js_globals/Number.rs diff --git a/src/js.rs b/src/js.rs index c3fa8e62..cff0402c 100644 --- a/src/js.rs +++ b/src/js.rs @@ -208,6 +208,20 @@ extern { pub fn entries(this: &Array) -> ArrayIterator; } +// Number. +#[wasm_bindgen] +extern { + pub type Number; + + /// The valueOf() method returns the wrapped primitive value of + /// a Number object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/valueOf + #[wasm_bindgen(method, js_name = valueOf)] + pub fn value_of(this: &Number) -> Number; + +} + // Object. #[wasm_bindgen] extern { @@ -268,4 +282,4 @@ extern { /// 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/Number.rs b/tests/all/js_globals/Number.rs new file mode 100644 index 00000000..a78051ea --- /dev/null +++ b/tests/all/js_globals/Number.rs @@ -0,0 +1,32 @@ +#![allow(non_snake_case)] + +use super::project; + + +#[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 js_value_of(this: &js::Number) -> js::Number { + this.value_of() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let number = 42; + assert.equal(wasm.js_value_of(number), 42); + assert.equal(typeof wasm.js_value_of(number), "number"); + } + "#) + .test() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index b03c3ae4..25bacbc6 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -6,6 +6,7 @@ mod Object; mod Array; mod ArrayIterator; mod JsString; +mod Number; #[test] #[cfg(feature = "std")] From bf56d5815b77d18dc56be65714d399823d40c1f2 Mon Sep 17 00:00:00 2001 From: Jonathan Sundqvist Date: Sat, 23 Jun 2018 17:38:13 +0200 Subject: [PATCH 2/4] 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() From f636f7b28ddef9677720cb875d7ff06cf5fe6f08 Mon Sep 17 00:00:00 2001 From: Jonathan Sundqvist Date: Sat, 23 Jun 2018 18:18:58 +0200 Subject: [PATCH 3/4] Add toPrecision to Number --- src/js.rs | 7 +++++++ tests/all/js_globals/Number.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/js.rs b/src/js.rs index 3d4414aa..4acf60f0 100644 --- a/src/js.rs +++ b/src/js.rs @@ -213,6 +213,13 @@ extern { extern { pub type Number; + /// The toPrecision() method returns a string representing the Number + /// object to the specified precision. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision + #[wasm_bindgen(catch, method, js_name = toPrecision)] + pub fn to_precision(this: &Number, precision: u8) -> Result; + /// The toString() method returns a string representing the /// specified Number object. /// diff --git a/tests/all/js_globals/Number.rs b/tests/all/js_globals/Number.rs index 96fe745a..fa215fc6 100644 --- a/tests/all/js_globals/Number.rs +++ b/tests/all/js_globals/Number.rs @@ -2,6 +2,37 @@ use super::project; +#[test] +fn to_precision() { + 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_precision(this: &js::Number, precision: u8) -> String { + let result = this.to_precision(precision); + 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() { + assert.equal(wasm.to_precision(0.1, 3), "0.100"); + assert.equal(wasm.to_precision(10, 101), "RangeError"); + } + "#) + .test() +} #[test] fn to_string() { From 6b5974d1bda6a8a7fe878f1a3f7b8bae97a9fc92 Mon Sep 17 00:00:00 2001 From: Jonathan Sundqvist Date: Sat, 23 Jun 2018 19:03:55 +0200 Subject: [PATCH 4/4] Add toLocaleString to Number --- src/js.rs | 7 +++++++ tests/all/js_globals/Number.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index 4acf60f0..44e41147 100644 --- a/src/js.rs +++ b/src/js.rs @@ -213,6 +213,13 @@ extern { extern { pub type Number; + /// The toLocaleString() method returns a string with a language sensitive + /// representation of this number. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString + #[wasm_bindgen(method, js_name = toLocaleString)] + pub fn to_locale_string(this: &Number, locale: String) -> String; + /// The toPrecision() method returns a string representing the Number /// object to the specified precision. /// diff --git a/tests/all/js_globals/Number.rs b/tests/all/js_globals/Number.rs index fa215fc6..da308342 100644 --- a/tests/all/js_globals/Number.rs +++ b/tests/all/js_globals/Number.rs @@ -2,6 +2,36 @@ use super::project; + +#[test] +fn to_locale_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_locale_string(this: &js::Number, locale: String) -> String { + this.to_locale_string(locale) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let number = 1234.45; + assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45"); + assert.equal(wasm.to_locale_string(number, "en-US"), "1,234.45"); + assert.equal(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45"); + } + "#) + .test() +} + #[test] fn to_precision() { project()