Skip to content

Commit 38bccd1

Browse files
Add in support for output option type
1 parent 616c31e commit 38bccd1

File tree

4 files changed

+103
-14
lines changed

4 files changed

+103
-14
lines changed

crates/backend/src/codegen.rs

+4
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ impl ToTokens for ast::ImportType {
517517
fn none() -> Self::Abi { 0 }
518518
}
519519

520+
impl<'a> ::wasm_bindgen::convert::OptionIntoWasmAbi for &'a #name {
521+
fn none() -> Self::Abi { 0 }
522+
}
523+
520524
impl ::wasm_bindgen::convert::FromWasmAbi for #name {
521525
type Abi = <::wasm_bindgen::JsValue as
522526
::wasm_bindgen::convert::FromWasmAbi>::Abi;

crates/backend/src/defined.rs

+75-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ pub enum ImportedTypeKind {
1010
Reference,
1111
}
1212

13+
impl<T> ImportedTypes for Option<T>
14+
where
15+
T: ImportedTypes,
16+
{
17+
fn imported_types<F>(&self, f: &mut F)
18+
where
19+
F: FnMut(&Ident, ImportedTypeKind),
20+
{
21+
if let Some(inner) = self {
22+
inner.imported_types(f);
23+
}
24+
}
25+
}
26+
1327
/// Iterate over definitions of and references to imported types in the AST.
1428
pub trait ImportedTypes {
1529
fn imported_types<F>(&self, f: &mut F)
@@ -147,19 +161,74 @@ impl ImportedTypes for syn::TypePath {
147161
where
148162
F: FnMut(&Ident, ImportedTypeKind),
149163
{
150-
if self.qself.is_some()
151-
|| self.path.leading_colon.is_some()
152-
|| self.path.segments.len() != 1
153-
{
154-
return;
164+
self.qself.imported_types(f);
165+
self.path.imported_types(f);
166+
}
167+
}
168+
169+
impl ImportedTypes for syn::QSelf {
170+
fn imported_types<F>(&self, _: &mut F)
171+
where
172+
F: FnMut(&Ident, ImportedTypeKind),
173+
{
174+
// TODO
175+
}
176+
}
177+
178+
impl ImportedTypes for syn::Path {
179+
fn imported_types<F>(&self, f: &mut F)
180+
where
181+
F: FnMut(&Ident, ImportedTypeKind),
182+
{
183+
for seg in self.segments.iter() {
184+
seg.arguments.imported_types(f);
155185
}
156186
f(
157-
&self.path.segments.last().unwrap().value().ident,
187+
&self.segments.last().unwrap().value().ident,
158188
ImportedTypeKind::Reference,
159189
);
160190
}
161191
}
162192

193+
impl ImportedTypes for syn::PathArguments {
194+
fn imported_types<F>(&self, f: &mut F)
195+
where
196+
F: FnMut(&Ident, ImportedTypeKind),
197+
{
198+
match self {
199+
syn::PathArguments::AngleBracketed(data) => {
200+
for arg in data.args.iter() {
201+
arg.imported_types(f);
202+
}
203+
}
204+
//TOCHECK
205+
syn::PathArguments::Parenthesized(data) => {
206+
for input in data.inputs.iter() {
207+
input.imported_types(f);
208+
}
209+
// TODO do we need to handle output here?
210+
// https://docs.rs/syn/0.14.0/syn/struct.ParenthesizedGenericArguments.html
211+
}
212+
syn::PathArguments::None => {}
213+
}
214+
}
215+
}
216+
217+
impl ImportedTypes for syn::GenericArgument {
218+
fn imported_types<F>(&self, f: &mut F)
219+
where
220+
F: FnMut(&Ident, ImportedTypeKind),
221+
{
222+
match self {
223+
syn::GenericArgument::Lifetime(_) => {}
224+
syn::GenericArgument::Type(ty) => ty.imported_types(f),
225+
syn::GenericArgument::Binding(_) => {}, // TODO
226+
syn::GenericArgument::Const(_) => {}, // TODO
227+
}
228+
}
229+
}
230+
231+
163232
impl ImportedTypes for ast::ImportFunction {
164233
fn imported_types<F>(&self, f: &mut F)
165234
where

crates/web-sys/tests/all/element.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ fn element() {
1313
1414
#[wasm_bindgen]
1515
pub fn test_element(element: &web_sys::Element) {
16-
assert_eq!(element.local_name(), "div", "Shouldn't have a div local name");
16+
/* Tests needed for:
17+
namespace_uri
18+
*/
19+
assert_eq!(element.prefix(), None, "Shouldn't have a prefix");
20+
assert_eq!(element.local_name(), "div", "Should have a div local name");
1721
assert_eq!(element.tag_name(), "div", "Should be a div tag");
1822
assert!(!element.has_attribute("id"), "Shouldn't have an id");
1923
element.set_id("beep");
@@ -27,7 +31,11 @@ fn element() {
2731
assert_eq!(element.class_name(), "", "Shouldn't have a class name");
2832
element.set_class_name("test thing");
2933
assert_eq!(element.class_name(), "test thing", "Should have a class name");
34+
assert_eq!(element.get_attribute("class").unwrap(), "test thing", "Should have a class name");
3035
assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed");
36+
/* Tests needed for:
37+
get_attribute_ns
38+
*/
3139
3240
/*TODO should we enable toggle_attribute tests? (Firefox Nightly + Chrome canary only)
3341
// TODO toggle_attribute should permit a single argument when optional arguments are supported
@@ -44,13 +52,19 @@ fn element() {
4452
// TODO check get_attribute here when supported
4553
assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed");
4654
assert!(!element.has_attribute("title"), "Should not have a title");
55+
/* Tests needed for:
56+
set_attribute_ns
57+
*/
4758
4859
assert!(!element.has_attributes(), "Should not have any attributes");
4960
assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly");
5061
assert!(element.has_attributes(), "Should have attributes");
5162
assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed");
52-
53-
// closest
63+
/* Tests needed for:
64+
remove_attribute_ns
65+
has_attribure_ns
66+
closest
67+
*/
5468
5569
assert_eq!(element.matches(".this-is-a-thing").unwrap(), false, "Should not match selector");
5670
assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), false, "Should not match selector");

crates/webidl/src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn compile_ast(mut ast: backend::ast::Program) -> String {
8282
let mut defined = BTreeSet::from_iter(
8383
vec![
8484
"str", "char", "bool", "JsValue", "u8", "i8", "u16", "i16", "u32", "i32", "u64", "i64",
85-
"usize", "isize", "f32", "f64", "Result", "String", "Vec",
85+
"usize", "isize", "f32", "f64", "Result", "String", "Vec", "Option",
8686
].into_iter()
8787
.map(|id| proc_macro2::Ident::new(id, proc_macro2::Span::call_site())),
8888
);
@@ -533,6 +533,7 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::Iterable {
533533
return Ok(());
534534
}
535535

536+
/* TODO
536537
let throws = util::throws(&self.extended_attributes);
537538
let return_value = webidl::ast::ReturnType::NonVoid(self.value_type.clone());
538539
let args = [];
@@ -543,22 +544,23 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::Iterable {
543544
&return_value,
544545
self_name,
545546
false,
546-
true, // Should be false
547+
false, // Should be false
547548
)
548549
.map(wrap_import_function)
549550
.map(|import| program.imports.push(import));
550551
551552
first_pass
552553
.create_basic_method(
553554
&args,
554-
Some(&"item".to_string()),
555-
&return_value,
555+
Some(&"keys".to_string()),
556+
&return_value, // Should be a number
556557
self_name,
557558
false,
558-
true, // Should be false
559+
false, // Should be false
559560
)
560561
.map(wrap_import_function)
561562
.map(|import| program.imports.push(import));
563+
*/
562564

563565
Ok(())
564566
}

0 commit comments

Comments
 (0)