File tree 4 files changed +68
-1
lines changed
4 files changed +68
-1
lines changed Original file line number Diff line number Diff line change @@ -404,9 +404,11 @@ impl Program {
404
404
} ;
405
405
let class_name = extract_path_ident ( class_name)
406
406
. expect ( "first argument of method must be a bare type" ) ;
407
+ let class_name = wasm. opts . js_class ( ) . map ( Into :: into)
408
+ . unwrap_or_else ( || class_name. to_string ( ) ) ;
407
409
408
410
ImportFunctionKind :: Method {
409
- class : class_name. to_string ( ) ,
411
+ class : class_name,
410
412
ty : class. clone ( ) ,
411
413
kind : MethodKind :: Normal ,
412
414
}
@@ -946,6 +948,16 @@ impl BindgenAttrs {
946
948
} )
947
949
. next ( )
948
950
}
951
+
952
+ fn js_class ( & self ) -> Option < & str > {
953
+ self . attrs
954
+ . iter ( )
955
+ . filter_map ( |a| match a {
956
+ BindgenAttr :: JsClass ( s) => Some ( & s[ ..] ) ,
957
+ _ => None ,
958
+ } )
959
+ . next ( )
960
+ }
949
961
}
950
962
951
963
impl syn:: synom:: Synom for BindgenAttrs {
@@ -976,6 +988,7 @@ pub enum BindgenAttr {
976
988
Structural ,
977
989
Readonly ,
978
990
JsName ( Ident ) ,
991
+ JsClass ( String ) ,
979
992
}
980
993
981
994
impl syn:: synom:: Synom for BindgenAttr {
@@ -1037,6 +1050,13 @@ impl syn::synom::Synom for BindgenAttr {
1037
1050
ns: call!( term2ident) >>
1038
1051
( ns)
1039
1052
) => { BindgenAttr :: JsName }
1053
+ |
1054
+ do_parse!(
1055
+ call!( term, "js_class" ) >>
1056
+ punct!( =) >>
1057
+ s: syn!( syn:: LitStr ) >>
1058
+ ( s. value( ) )
1059
+ ) => { BindgenAttr :: JsClass }
1040
1060
) ) ;
1041
1061
}
1042
1062
Original file line number Diff line number Diff line change @@ -255,3 +255,17 @@ extern {
255
255
#[ wasm_bindgen( method, js_name = propertyIsEnumerable) ]
256
256
pub fn property_is_enumerable ( this : & Object , property : & JsValue ) -> bool ;
257
257
}
258
+
259
+ // String
260
+ #[ wasm_bindgen]
261
+ extern {
262
+ #[ wasm_bindgen( js_name = String ) ]
263
+ pub type JsString ;
264
+
265
+ /// The slice() method extracts a section of a string and returns it as a
266
+ /// new string, without modifying the original string.
267
+ ///
268
+ /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
269
+ #[ wasm_bindgen( method, js_class = "String" ) ]
270
+ pub fn slice ( this : & JsString , start : u32 , end : u32 ) -> JsString ;
271
+ }
Original file line number Diff line number Diff line change
1
+ #![ allow( non_snake_case) ]
2
+
3
+ use project;
4
+
5
+ #[ test]
6
+ fn slice ( ) {
7
+ project ( )
8
+ . file ( "src/lib.rs" , r#"
9
+ #![feature(proc_macro, wasm_custom_section)]
10
+
11
+ extern crate wasm_bindgen;
12
+ use wasm_bindgen::prelude::*;
13
+ use wasm_bindgen::js;
14
+
15
+ #[wasm_bindgen]
16
+ pub fn create_slice(this: &js::JsString, start: u32, end: u32) -> js::JsString {
17
+ this.slice(start, end)
18
+ }
19
+ "# )
20
+ . file ( "test.ts" , r#"
21
+ import * as assert from "assert";
22
+ import * as wasm from "./out";
23
+
24
+ export function test() {
25
+ let characters = "acxn18";
26
+ let subset = wasm.create_slice(characters, 1, 3);
27
+
28
+ assert.equal(subset, "cx");
29
+ }
30
+ "# )
31
+ . test ( )
32
+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ use super::project;
5
5
mod Object ;
6
6
mod Array ;
7
7
mod ArrayIterator ;
8
+ mod JsString ;
8
9
9
10
#[ test]
10
11
#[ cfg( feature = "std" ) ]
You can’t perform that action at this time.
0 commit comments