Skip to content

Commit b1cbb56

Browse files
authored
Merge branch 'master' into master
2 parents 9e07c49 + 0f5badf commit b1cbb56

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed

src/js.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,40 @@ extern {
227227
pub fn name(this: &JsFunction) -> String;
228228
}
229229

230+
// Number.
231+
#[wasm_bindgen]
232+
extern {
233+
pub type Number;
234+
235+
/// The toLocaleString() method returns a string with a language sensitive
236+
/// representation of this number.
237+
///
238+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
239+
#[wasm_bindgen(method, js_name = toLocaleString)]
240+
pub fn to_locale_string(this: &Number, locale: String) -> String;
241+
242+
/// The toPrecision() method returns a string representing the Number
243+
/// object to the specified precision.
244+
///
245+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
246+
#[wasm_bindgen(catch, method, js_name = toPrecision)]
247+
pub fn to_precision(this: &Number, precision: u8) -> Result<String, JsValue>;
248+
249+
/// The toString() method returns a string representing the
250+
/// specified Number object.
251+
///
252+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
253+
#[wasm_bindgen(catch, method, js_name = toString)]
254+
pub fn to_string(this: &Number, radix: u8) -> Result<String, JsValue>;
255+
256+
/// The valueOf() method returns the wrapped primitive value of
257+
/// a Number object.
258+
///
259+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/valueOf
260+
#[wasm_bindgen(method, js_name = valueOf)]
261+
pub fn value_of(this: &Number) -> Number;
262+
}
263+
230264
// Object.
231265
#[wasm_bindgen]
232266
extern {
@@ -273,6 +307,13 @@ extern {
273307
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
274308
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
275309
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
310+
311+
/// The valueOf() method returns the primitive value of the
312+
/// specified object.
313+
///
314+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
315+
#[wasm_bindgen(method, js_name = valueOf)]
316+
pub fn value_of(this: &Object) -> Object;
276317
}
277318

278319
// String
@@ -287,4 +328,4 @@ extern {
287328
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
288329
#[wasm_bindgen(method, js_class = "String")]
289330
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
290-
}
331+
}

tests/all/js_globals/Number.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#![allow(non_snake_case)]
2+
3+
use super::project;
4+
5+
6+
#[test]
7+
fn to_locale_string() {
8+
project()
9+
.file("src/lib.rs", r#"
10+
#![feature(proc_macro, wasm_custom_section)]
11+
12+
extern crate wasm_bindgen;
13+
use wasm_bindgen::prelude::*;
14+
use wasm_bindgen::js;
15+
16+
#[wasm_bindgen]
17+
pub fn to_locale_string(this: &js::Number, locale: String) -> String {
18+
this.to_locale_string(locale)
19+
}
20+
"#)
21+
.file("test.ts", r#"
22+
import * as assert from "assert";
23+
import * as wasm from "./out";
24+
25+
export function test() {
26+
let number = 1234.45;
27+
assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45");
28+
assert.equal(wasm.to_locale_string(number, "en-US"), "1,234.45");
29+
assert.equal(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45");
30+
}
31+
"#)
32+
.test()
33+
}
34+
35+
#[test]
36+
fn to_precision() {
37+
project()
38+
.file("src/lib.rs", r#"
39+
#![feature(proc_macro, wasm_custom_section)]
40+
41+
extern crate wasm_bindgen;
42+
use wasm_bindgen::prelude::*;
43+
use wasm_bindgen::js;
44+
45+
#[wasm_bindgen]
46+
pub fn to_precision(this: &js::Number, precision: u8) -> String {
47+
let result = this.to_precision(precision);
48+
let result = match result {
49+
Ok(num) => num,
50+
Err(_err) => "RangeError".to_string()
51+
};
52+
result
53+
}
54+
"#)
55+
.file("test.ts", r#"
56+
import * as assert from "assert";
57+
import * as wasm from "./out";
58+
59+
export function test() {
60+
assert.equal(wasm.to_precision(0.1, 3), "0.100");
61+
assert.equal(wasm.to_precision(10, 101), "RangeError");
62+
}
63+
"#)
64+
.test()
65+
}
66+
67+
#[test]
68+
fn to_string() {
69+
project()
70+
.file("src/lib.rs", r#"
71+
#![feature(proc_macro, wasm_custom_section)]
72+
73+
extern crate wasm_bindgen;
74+
use wasm_bindgen::prelude::*;
75+
use wasm_bindgen::js;
76+
77+
#[wasm_bindgen]
78+
pub fn to_string(this: &js::Number, radix: u8) -> String {
79+
let result = this.to_string(radix);
80+
let result = match result {
81+
Ok(num) => num,
82+
Err(_err) => "RangeError".to_string()
83+
};
84+
result
85+
}
86+
"#)
87+
.file("test.ts", r#"
88+
import * as assert from "assert";
89+
import * as wasm from "./out";
90+
91+
export function test() {
92+
let number = 42;
93+
assert.equal(wasm.to_string(number, 10), "42");
94+
assert.equal(wasm.to_string(233, 16), "e9");
95+
assert.equal(wasm.to_string(number, 100), "RangeError");
96+
}
97+
"#)
98+
.test()
99+
}
100+
101+
#[test]
102+
fn value_of() {
103+
project()
104+
.file("src/lib.rs", r#"
105+
#![feature(proc_macro, wasm_custom_section)]
106+
107+
extern crate wasm_bindgen;
108+
use wasm_bindgen::prelude::*;
109+
use wasm_bindgen::js;
110+
111+
#[wasm_bindgen]
112+
pub fn js_value_of(this: &js::Number) -> js::Number {
113+
this.value_of()
114+
}
115+
"#)
116+
.file("test.ts", r#"
117+
import * as assert from "assert";
118+
import * as wasm from "./out";
119+
120+
export function test() {
121+
let number = 42;
122+
assert.equal(wasm.js_value_of(number), 42);
123+
assert.equal(typeof wasm.js_value_of(number), "number");
124+
}
125+
"#)
126+
.test()
127+
}

tests/all/js_globals/Object.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,31 @@ fn to_locale_string() {
184184
"#)
185185
.test()
186186
}
187+
188+
#[test]
189+
fn value_of() {
190+
project()
191+
.file("src/lib.rs", r#"
192+
#![feature(proc_macro, wasm_custom_section)]
193+
194+
extern crate wasm_bindgen;
195+
use wasm_bindgen::prelude::*;
196+
use wasm_bindgen::js;
197+
198+
#[wasm_bindgen]
199+
pub fn value_of(obj: &js::Object) -> js::Object {
200+
obj.value_of()
201+
}
202+
"#)
203+
.file("test.ts", r#"
204+
import * as assert from "assert";
205+
import * as wasm from "./out";
206+
207+
export function test() {
208+
const obj = { foo: 42 };
209+
assert.strictEqual(wasm.value_of(obj), obj);
210+
assert.notStrictEqual(wasm.value_of(obj), { foo: 42 });
211+
}
212+
"#)
213+
.test()
214+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod Array;
77
mod ArrayIterator;
88
mod JsFunction;
99
mod JsString;
10+
mod Number;
1011

1112
#[test]
1213
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)