Skip to content

Commit 233b352

Browse files
committed
Merge branch 'master' into number-fixed-exponential
2 parents f5e050d + 243f73e commit 233b352

File tree

6 files changed

+57
-30
lines changed

6 files changed

+57
-30
lines changed

src/js.rs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ extern {
4545
/// previously created by `encodeURI` or by a similar routine.
4646
///
4747
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
48-
#[cfg(feature = "std")]
4948
#[wasm_bindgen(catch, js_name = decodeURI)]
50-
pub fn decode_uri(encoded: &str) -> Result<String, JsValue>;
49+
pub fn decode_uri(encoded: &str) -> Result<JsString, JsValue>;
5150

5251
/// The `encodeURI()` function encodes a Uniform Resource Identifier (URI)
5352
/// by replacing each instance of certain characters by one, two, three, or
@@ -56,9 +55,8 @@ extern {
5655
/// "surrogate" characters).
5756
///
5857
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
59-
#[cfg(feature = "std")]
6058
#[wasm_bindgen(js_name = encodeURI)]
61-
pub fn encode_uri(decoded: &str) -> String;
59+
pub fn encode_uri(decoded: &str) -> JsString;
6260

6361
/// The `eval()` function evaluates JavaScript code represented as a string.
6462
///
@@ -119,7 +117,7 @@ extern {
119117
///
120118
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
121119
#[wasm_bindgen(method)]
122-
pub fn join(this: &Array, delimiter: &str) -> String;
120+
pub fn join(this: &Array, delimiter: &str) -> JsString;
123121

124122
/// The lastIndexOf() method returns the last index at which a given element can
125123
/// be found in the array, or -1 if it is not present. The array is searched
@@ -180,7 +178,7 @@ extern {
180178
///
181179
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
182180
#[wasm_bindgen(method, js_name = toString)]
183-
pub fn to_string(this: &Array) -> String;
181+
pub fn to_string(this: &Array) -> JsString;
184182

185183
/// The unshift() method adds one or more elements to the beginning of an array
186184
/// and returns the new length of the array.
@@ -224,7 +222,7 @@ extern {
224222
///
225223
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
226224
#[wasm_bindgen(method, getter, structural)]
227-
pub fn name(this: &JsFunction) -> String;
225+
pub fn name(this: &JsFunction) -> JsString;
228226
}
229227

230228
// Number.
@@ -237,14 +235,14 @@ extern {
237235
///
238236
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
239237
#[wasm_bindgen(method, js_name = toLocaleString)]
240-
pub fn to_locale_string(this: &Number, locale: String) -> String;
238+
pub fn to_locale_string(this: &Number, locale: &str) -> JsString;
241239

242240
/// The toPrecision() method returns a string representing the Number
243241
/// object to the specified precision.
244242
///
245243
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
246244
#[wasm_bindgen(catch, method, js_name = toPrecision)]
247-
pub fn to_precision(this: &Number, precision: u8) -> Result<String, JsValue>;
245+
pub fn to_precision(this: &Number, precision: u8) -> Result<JsString, JsValue>;
248246

249247
/// The toFixed() method returns a string representing the Number
250248
/// object using fixed-point notation.
@@ -265,7 +263,7 @@ extern {
265263
///
266264
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
267265
#[wasm_bindgen(catch, method, js_name = toString)]
268-
pub fn to_string(this: &Number, radix: u8) -> Result<String, JsValue>;
266+
pub fn to_string(this: &Number, radix: u8) -> Result<JsString, JsValue>;
269267

270268
/// The valueOf() method returns the wrapped primitive value of
271269
/// a Number object.
@@ -300,13 +298,13 @@ extern {
300298
///
301299
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString
302300
#[wasm_bindgen(method, js_name = toLocaleString)]
303-
pub fn to_locale_string(this: &Object) -> String;
301+
pub fn to_locale_string(this: &Object) -> JsString;
304302

305303
/// The toString() method returns a string representing the object.
306304
///
307305
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
308306
#[wasm_bindgen(method, js_name = toString)]
309-
pub fn to_string(this: &Object) -> String;
307+
pub fn to_string(this: &Object) -> JsString;
310308

311309
/// The isPrototypeOf() method checks if an object exists in another
312310
/// object's prototype chain.
@@ -324,16 +322,16 @@ extern {
324322

325323
/// The valueOf() method returns the primitive value of the
326324
/// specified object.
327-
///
325+
///
328326
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
329327
#[wasm_bindgen(method, js_name = valueOf)]
330328
pub fn value_of(this: &Object) -> Object;
331329
}
332330

333-
// String
331+
// JsString
334332
#[wasm_bindgen]
335333
extern {
336-
#[wasm_bindgen(js_name = String)]
334+
#[wasm_bindgen(js_name = JsString)]
337335
pub type JsString;
338336

339337
/// The slice() method extracts a section of a string and returns it as a
@@ -343,3 +341,31 @@ extern {
343341
#[wasm_bindgen(method, js_class = "String")]
344342
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
345343
}
344+
345+
impl<'a> From<&'a str> for JsString {
346+
fn from(s: &'a str) -> Self {
347+
JsString {
348+
obj: JsValue::from_str(s),
349+
}
350+
}
351+
}
352+
353+
if_std! {
354+
impl From<String> for JsString {
355+
fn from(s: String) -> Self {
356+
From::from(&*s)
357+
}
358+
}
359+
360+
impl<'a> From<&'a JsString> for String {
361+
fn from(s: &'a JsString) -> Self {
362+
s.obj.as_string().unwrap()
363+
}
364+
}
365+
366+
impl From<JsString> for String {
367+
fn from(s: JsString) -> Self {
368+
From::from(&s)
369+
}
370+
}
371+
}

tests/all/js_globals/Array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn join() {
119119
use wasm_bindgen::js;
120120
121121
#[wasm_bindgen]
122-
pub fn join_array(this: &js::Array, delimiter: &str) -> String {
122+
pub fn join_array(this: &js::Array, delimiter: &str) -> js::JsString {
123123
this.join(delimiter)
124124
}
125125
@@ -398,7 +398,7 @@ fn to_string() {
398398
use wasm_bindgen::js;
399399
400400
#[wasm_bindgen]
401-
pub fn array_to_string(this: &js::Array) -> String {
401+
pub fn array_to_string(this: &js::Array) -> js::JsString {
402402
this.to_string()
403403
}
404404

tests/all/js_globals/JsFunction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn name() {
5353
use wasm_bindgen::js;
5454
5555
#[wasm_bindgen]
56-
pub fn fn_name(this: &js::JsFunction) -> String {
56+
pub fn fn_name(this: &js::JsFunction) -> js::JsString {
5757
this.name()
5858
}
5959
"#)

tests/all/js_globals/Number.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn to_locale_string() {
1414
use wasm_bindgen::js;
1515
1616
#[wasm_bindgen]
17-
pub fn to_locale_string(this: &js::Number, locale: String) -> String {
17+
pub fn to_locale_string(this: &js::Number, locale: &str) -> js::JsString {
1818
this.to_locale_string(locale)
1919
}
2020
"#)
@@ -24,9 +24,10 @@ fn to_locale_string() {
2424
2525
export function test() {
2626
let number = 1234.45;
27-
assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45");
2827
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");
28+
// TODO: these tests seems to be system dependent, disable for now
29+
// assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45");
30+
// assert.equal(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45");
3031
}
3132
"#)
3233
.test()
@@ -43,11 +44,11 @@ fn to_precision() {
4344
use wasm_bindgen::js;
4445
4546
#[wasm_bindgen]
46-
pub fn to_precision(this: &js::Number, precision: u8) -> String {
47+
pub fn to_precision(this: &js::Number, precision: u8) -> js::JsString {
4748
let result = this.to_precision(precision);
4849
let result = match result {
4950
Ok(num) => num,
50-
Err(_err) => "RangeError".to_string()
51+
Err(_err) => "RangeError".into()
5152
};
5253
result
5354
}
@@ -75,11 +76,11 @@ fn to_string() {
7576
use wasm_bindgen::js;
7677
7778
#[wasm_bindgen]
78-
pub fn to_string(this: &js::Number, radix: u8) -> String {
79+
pub fn to_string(this: &js::Number, radix: u8) -> js::JsString {
7980
let result = this.to_string(radix);
8081
let result = match result {
8182
Ok(num) => num,
82-
Err(_err) => "RangeError".to_string()
83+
Err(_err) => "RangeError".into()
8384
};
8485
result
8586
}

tests/all/js_globals/Object.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ fn to_string() {
7070
use wasm_bindgen::js;
7171
7272
#[wasm_bindgen]
73-
pub fn to_string(obj: &js::Object) -> String {
73+
pub fn to_string(obj: &js::Object) -> js::JsString {
7474
obj.to_string()
7575
}
7676
7777
#[wasm_bindgen]
7878
pub fn test() {
7979
let object = js::Object::new();
80-
assert_eq!(object.to_string(), "[object Object]");
80+
assert_eq!(String::from(object.to_string()), "[object Object]");
8181
}
8282
"#)
8383
.file("test.ts", r#"
@@ -169,7 +169,7 @@ fn to_locale_string() {
169169
use wasm_bindgen::js;
170170
171171
#[wasm_bindgen]
172-
pub fn to_locale_string() -> String {
172+
pub fn to_locale_string() -> js::JsString {
173173
let object = js::Object::new();
174174
object.to_locale_string()
175175
}

tests/all/js_globals/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn decode_uri() {
2525
let x = js::decode_uri("https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B")
2626
.ok()
2727
.expect("should decode URI OK");
28-
assert_eq!(x, "https://mozilla.org/?x=шеллы");
28+
assert_eq!(String::from(x), "https://mozilla.org/?x=шеллы");
2929
3030
assert!(js::decode_uri("%E0%A4%A").is_err());
3131
}
@@ -47,7 +47,7 @@ fn encode_uri() {
4747
#[wasm_bindgen]
4848
pub fn test() {
4949
let x = js::encode_uri("ABC abc 123");
50-
assert_eq!(x, "ABC%20abc%20123");
50+
assert_eq!(String::from(x), "ABC%20abc%20123");
5151
}
5252
"#)
5353
.test();

0 commit comments

Comments
 (0)