@@ -45,9 +45,8 @@ extern {
45
45
/// previously created by `encodeURI` or by a similar routine.
46
46
///
47
47
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
48
- #[ cfg( feature = "std" ) ]
49
48
#[ 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 > ;
51
50
52
51
/// The `encodeURI()` function encodes a Uniform Resource Identifier (URI)
53
52
/// by replacing each instance of certain characters by one, two, three, or
@@ -56,9 +55,8 @@ extern {
56
55
/// "surrogate" characters).
57
56
///
58
57
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
59
- #[ cfg( feature = "std" ) ]
60
58
#[ wasm_bindgen( js_name = encodeURI) ]
61
- pub fn encode_uri ( decoded : & str ) -> String ;
59
+ pub fn encode_uri ( decoded : & str ) -> JsString ;
62
60
63
61
/// The `eval()` function evaluates JavaScript code represented as a string.
64
62
///
@@ -119,7 +117,7 @@ extern {
119
117
///
120
118
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
121
119
#[ wasm_bindgen( method) ]
122
- pub fn join ( this : & Array , delimiter : & str ) -> String ;
120
+ pub fn join ( this : & Array , delimiter : & str ) -> JsString ;
123
121
124
122
/// The lastIndexOf() method returns the last index at which a given element can
125
123
/// be found in the array, or -1 if it is not present. The array is searched
@@ -180,7 +178,7 @@ extern {
180
178
///
181
179
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
182
180
#[ wasm_bindgen( method, js_name = toString) ]
183
- pub fn to_string ( this : & Array ) -> String ;
181
+ pub fn to_string ( this : & Array ) -> JsString ;
184
182
185
183
/// The unshift() method adds one or more elements to the beginning of an array
186
184
/// and returns the new length of the array.
@@ -224,7 +222,7 @@ extern {
224
222
///
225
223
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
226
224
#[ wasm_bindgen( method, getter, structural) ]
227
- pub fn name ( this : & JsFunction ) -> String ;
225
+ pub fn name ( this : & JsFunction ) -> JsString ;
228
226
}
229
227
230
228
// Number.
@@ -237,14 +235,14 @@ extern {
237
235
///
238
236
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
239
237
#[ 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 ;
241
239
242
240
/// The toPrecision() method returns a string representing the Number
243
241
/// object to the specified precision.
244
242
///
245
243
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
246
244
#[ 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 > ;
248
246
249
247
/// The toFixed() method returns a string representing the Number
250
248
/// object using fixed-point notation.
@@ -265,7 +263,7 @@ extern {
265
263
///
266
264
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
267
265
#[ 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 > ;
269
267
270
268
/// The valueOf() method returns the wrapped primitive value of
271
269
/// a Number object.
@@ -300,13 +298,13 @@ extern {
300
298
///
301
299
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString
302
300
#[ wasm_bindgen( method, js_name = toLocaleString) ]
303
- pub fn to_locale_string ( this : & Object ) -> String ;
301
+ pub fn to_locale_string ( this : & Object ) -> JsString ;
304
302
305
303
/// The toString() method returns a string representing the object.
306
304
///
307
305
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
308
306
#[ wasm_bindgen( method, js_name = toString) ]
309
- pub fn to_string ( this : & Object ) -> String ;
307
+ pub fn to_string ( this : & Object ) -> JsString ;
310
308
311
309
/// The isPrototypeOf() method checks if an object exists in another
312
310
/// object's prototype chain.
@@ -324,16 +322,16 @@ extern {
324
322
325
323
/// The valueOf() method returns the primitive value of the
326
324
/// specified object.
327
- ///
325
+ ///
328
326
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
329
327
#[ wasm_bindgen( method, js_name = valueOf) ]
330
328
pub fn value_of ( this : & Object ) -> Object ;
331
329
}
332
330
333
- // String
331
+ // JsString
334
332
#[ wasm_bindgen]
335
333
extern {
336
- #[ wasm_bindgen( js_name = String ) ]
334
+ #[ wasm_bindgen( js_name = JsString ) ]
337
335
pub type JsString ;
338
336
339
337
/// The slice() method extracts a section of a string and returns it as a
@@ -343,3 +341,31 @@ extern {
343
341
#[ wasm_bindgen( method, js_class = "String" ) ]
344
342
pub fn slice ( this : & JsString , start : u32 , end : u32 ) -> JsString ;
345
343
}
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
+ }
0 commit comments