@@ -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,21 +235,21 @@ 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 toString() method returns a string representing the
250
248
/// specified Number object.
251
249
///
252
250
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
253
251
#[ 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 > ;
255
253
256
254
/// The valueOf() method returns the wrapped primitive value of
257
255
/// a Number object.
@@ -286,13 +284,13 @@ extern {
286
284
///
287
285
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString
288
286
#[ wasm_bindgen( method, js_name = toLocaleString) ]
289
- pub fn to_locale_string ( this : & Object ) -> String ;
287
+ pub fn to_locale_string ( this : & Object ) -> JsString ;
290
288
291
289
/// The toString() method returns a string representing the object.
292
290
///
293
291
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
294
292
#[ wasm_bindgen( method, js_name = toString) ]
295
- pub fn to_string ( this : & Object ) -> String ;
293
+ pub fn to_string ( this : & Object ) -> JsString ;
296
294
297
295
/// The isPrototypeOf() method checks if an object exists in another
298
296
/// object's prototype chain.
@@ -310,16 +308,16 @@ extern {
310
308
311
309
/// The valueOf() method returns the primitive value of the
312
310
/// specified object.
313
- ///
311
+ ///
314
312
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
315
313
#[ wasm_bindgen( method, js_name = valueOf) ]
316
314
pub fn value_of ( this : & Object ) -> Object ;
317
315
}
318
316
319
- // String
317
+ // JsString
320
318
#[ wasm_bindgen]
321
319
extern {
322
- #[ wasm_bindgen( js_name = String ) ]
320
+ #[ wasm_bindgen( js_name = JsString ) ]
323
321
pub type JsString ;
324
322
325
323
/// The slice() method extracts a section of a string and returns it as a
@@ -329,3 +327,31 @@ extern {
329
327
#[ wasm_bindgen( method, js_class = "String" ) ]
330
328
pub fn slice ( this : & JsString , start : u32 , end : u32 ) -> JsString ;
331
329
}
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
+ }
0 commit comments