Skip to content

Commit d79f982

Browse files
authored
Merge pull request #295 from kzvi/js-class-attr
add js_class attribute for defining what class an imported method is for
2 parents 5f12064 + 5ae6ee7 commit d79f982

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

crates/backend/src/ast.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,11 @@ impl Program {
404404
};
405405
let class_name = extract_path_ident(class_name)
406406
.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());
407409

408410
ImportFunctionKind::Method {
409-
class: class_name.to_string(),
411+
class: class_name,
410412
ty: class.clone(),
411413
kind: MethodKind::Normal,
412414
}
@@ -946,6 +948,16 @@ impl BindgenAttrs {
946948
})
947949
.next()
948950
}
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+
}
949961
}
950962

951963
impl syn::synom::Synom for BindgenAttrs {
@@ -976,6 +988,7 @@ pub enum BindgenAttr {
976988
Structural,
977989
Readonly,
978990
JsName(Ident),
991+
JsClass(String),
979992
}
980993

981994
impl syn::synom::Synom for BindgenAttr {
@@ -1037,6 +1050,13 @@ impl syn::synom::Synom for BindgenAttr {
10371050
ns: call!(term2ident) >>
10381051
(ns)
10391052
)=> { BindgenAttr::JsName }
1053+
|
1054+
do_parse!(
1055+
call!(term, "js_class") >>
1056+
punct!(=) >>
1057+
s: syn!(syn::LitStr) >>
1058+
(s.value())
1059+
)=> { BindgenAttr::JsClass }
10401060
));
10411061
}
10421062

src/js.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,17 @@ extern {
255255
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
256256
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
257257
}
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+
}

tests/all/js_globals/JsString.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::project;
55
mod Object;
66
mod Array;
77
mod ArrayIterator;
8+
mod JsString;
89

910
#[test]
1011
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)