From 2cfffc65d7cd8e6c0da84d0949fa919416ae7b62 Mon Sep 17 00:00:00 2001 From: kzvi Date: Fri, 22 Jun 2018 12:12:43 -0700 Subject: [PATCH 1/2] add js_class attribute for defining what class an imported method is for --- crates/backend/src/ast.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/backend/src/ast.rs b/crates/backend/src/ast.rs index eea4d9d6..02a482e3 100644 --- a/crates/backend/src/ast.rs +++ b/crates/backend/src/ast.rs @@ -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, } @@ -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 { @@ -977,6 +989,7 @@ pub enum BindgenAttr { Structural, Readonly, JsName(Ident), + JsClass(String), } impl syn::synom::Synom for BindgenAttr { @@ -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 } )); } From 5ae6ee7aec9a7ae9e821ae5fd62cf3dcf7011421 Mon Sep 17 00:00:00 2001 From: kzvi Date: Fri, 22 Jun 2018 17:51:44 -0700 Subject: [PATCH 2/2] add JsString binding to src/js.rs and tests --- src/js.rs | 14 ++++++++++++++ tests/all/js_globals/JsString.rs | 32 ++++++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 47 insertions(+) create mode 100755 tests/all/js_globals/JsString.rs diff --git a/src/js.rs b/src/js.rs index 56a4dd2c..c3fa8e62 100644 --- a/src/js.rs +++ b/src/js.rs @@ -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; +} \ No newline at end of file diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs new file mode 100755 index 00000000..112f2d2b --- /dev/null +++ b/tests/all/js_globals/JsString.rs @@ -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() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index d9c58b5c..b03c3ae4 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -5,6 +5,7 @@ use super::project; mod Object; mod Array; mod ArrayIterator; +mod JsString; #[test] #[cfg(feature = "std")]