Skip to content

add js_class attribute for defining what class an imported method is for #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,11 @@ impl Program {
};
let class_name = extract_path_ident(class_name)
.expect("first argument of method must be a bare type");
let class_name = wasm.opts.js_class().map(Into::into)
.unwrap_or_else(|| class_name.to_string());

ImportFunctionKind::Method {
class: class_name.to_string(),
class: class_name,
ty: class.clone(),
kind: MethodKind::Normal,
}
Expand Down Expand Up @@ -947,6 +949,16 @@ impl BindgenAttrs {
})
.next()
}

fn js_class(&self) -> Option<&str> {
self.attrs
.iter()
.filter_map(|a| match a {
BindgenAttr::JsClass(s) => Some(&s[..]),
_ => None,
})
.next()
}
}

impl syn::synom::Synom for BindgenAttrs {
Expand Down Expand Up @@ -977,6 +989,7 @@ pub enum BindgenAttr {
Structural,
Readonly,
JsName(Ident),
JsClass(String),
}

impl syn::synom::Synom for BindgenAttr {
Expand Down Expand Up @@ -1038,6 +1051,13 @@ impl syn::synom::Synom for BindgenAttr {
ns: call!(term2ident) >>
(ns)
)=> { BindgenAttr::JsName }
|
do_parse!(
call!(term, "js_class") >>
punct!(=) >>
s: syn!(syn::LitStr) >>
(s.value())
)=> { BindgenAttr::JsClass }
));
}

Expand Down
14 changes: 14 additions & 0 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,17 @@ extern {
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
}

// String
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_name = String)]
pub type JsString;

/// The slice() method extracts a section of a string and returns it as a
/// new string, without modifying the original string.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
#[wasm_bindgen(method, js_class = "String")]
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
}
32 changes: 32 additions & 0 deletions tests/all/js_globals/JsString.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![allow(non_snake_case)]

use project;

#[test]
fn slice() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section)]

extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;

#[wasm_bindgen]
pub fn create_slice(this: &js::JsString, start: u32, end: u32) -> js::JsString {
this.slice(start, end)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";

export function test() {
let characters = "acxn18";
let subset = wasm.create_slice(characters, 1, 3);

assert.equal(subset, "cx");
}
"#)
.test()
}
1 change: 1 addition & 0 deletions tests/all/js_globals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::project;
mod Object;
mod Array;
mod ArrayIterator;
mod JsString;

#[test]
#[cfg(feature = "std")]
Expand Down