Skip to content

Commit 243f73e

Browse files
authored
Merge pull request #302 from ohanar/JsString
js globals: return JsString rather than String
2 parents 4c7b267 + 21f49d0 commit 243f73e

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,21 +235,21 @@ 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 toString() method returns a string representing the
250248
/// specified Number object.
251249
///
252250
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
253251
#[wasm_bindgen(catch, method, js_name = toString)]
254-
pub fn to_string(this: &Number, radix: u8) -> Result<String, JsValue>;
252+
pub fn to_string(this: &Number, radix: u8) -> Result<JsString, JsValue>;
255253

256254
/// The valueOf() method returns the wrapped primitive value of
257255
/// a Number object.
@@ -286,13 +284,13 @@ extern {
286284
///
287285
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString
288286
#[wasm_bindgen(method, js_name = toLocaleString)]
289-
pub fn to_locale_string(this: &Object) -> String;
287+
pub fn to_locale_string(this: &Object) -> JsString;
290288

291289
/// The toString() method returns a string representing the object.
292290
///
293291
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
294292
#[wasm_bindgen(method, js_name = toString)]
295-
pub fn to_string(this: &Object) -> String;
293+
pub fn to_string(this: &Object) -> JsString;
296294

297295
/// The isPrototypeOf() method checks if an object exists in another
298296
/// object's prototype chain.
@@ -310,16 +308,16 @@ extern {
310308

311309
/// The valueOf() method returns the primitive value of the
312310
/// specified object.
313-
///
311+
///
314312
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
315313
#[wasm_bindgen(method, js_name = valueOf)]
316314
pub fn value_of(this: &Object) -> Object;
317315
}
318316

319-
// String
317+
// JsString
320318
#[wasm_bindgen]
321319
extern {
322-
#[wasm_bindgen(js_name = String)]
320+
#[wasm_bindgen(js_name = JsString)]
323321
pub type JsString;
324322

325323
/// The slice() method extracts a section of a string and returns it as a
@@ -329,3 +327,31 @@ extern {
329327
#[wasm_bindgen(method, js_class = "String")]
330328
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
331329
}
330+
331+
impl<'a> From<&'a str> for JsString {
332+
fn from(s: &'a str) -> Self {
333+
JsString {
334+
obj: JsValue::from_str(s),
335+
}
336+
}
337+
}
338+
339+
if_std! {
340+
impl From<String> for JsString {
341+
fn from(s: String) -> Self {
342+
From::from(&*s)
343+
}
344+
}
345+
346+
impl<'a> From<&'a JsString> for String {
347+
fn from(s: &'a JsString) -> Self {
348+
s.obj.as_string().unwrap()
349+
}
350+
}
351+
352+
impl From<JsString> for String {
353+
fn from(s: JsString) -> Self {
354+
From::from(&s)
355+
}
356+
}
357+
}

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)