mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-14 08:27:50 +00:00
Merge pull request #1467 from RReverser/tuple-structs
Generate bindings for indexed struct properties
This commit is contained in:
commit
cd7aa717c5
@ -227,7 +227,7 @@ pub struct Struct {
|
|||||||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct StructField {
|
pub struct StructField {
|
||||||
pub name: Ident,
|
pub name: syn::Member,
|
||||||
pub struct_name: Ident,
|
pub struct_name: Ident,
|
||||||
pub readonly: bool,
|
pub readonly: bool,
|
||||||
pub ty: syn::Type,
|
pub ty: syn::Type,
|
||||||
|
@ -332,7 +332,10 @@ fn shared_struct<'a>(s: &'a ast::Struct, intern: &'a Interner) -> Struct<'a> {
|
|||||||
|
|
||||||
fn shared_struct_field<'a>(s: &'a ast::StructField, intern: &'a Interner) -> StructField<'a> {
|
fn shared_struct_field<'a>(s: &'a ast::StructField, intern: &'a Interner) -> StructField<'a> {
|
||||||
StructField {
|
StructField {
|
||||||
name: intern.intern(&s.name),
|
name: match &s.name {
|
||||||
|
syn::Member::Named(ident) => intern.intern(ident),
|
||||||
|
syn::Member::Unnamed(index) => intern.intern_str(&index.index.to_string()),
|
||||||
|
},
|
||||||
readonly: s.readonly,
|
readonly: s.readonly,
|
||||||
comments: s.comments.iter().map(|s| &**s).collect(),
|
comments: s.comments.iter().map(|s| &**s).collect(),
|
||||||
}
|
}
|
||||||
|
@ -309,40 +309,37 @@ impl<'a> ConvertToAst<BindgenAttrs> for &'a mut syn::ItemStruct {
|
|||||||
.js_name()
|
.js_name()
|
||||||
.map(|s| s.0.to_string())
|
.map(|s| s.0.to_string())
|
||||||
.unwrap_or(self.ident.to_string());
|
.unwrap_or(self.ident.to_string());
|
||||||
if let syn::Fields::Named(names) = &mut self.fields {
|
for (i, field) in self.fields.iter_mut().enumerate() {
|
||||||
for field in names.named.iter_mut() {
|
match field.vis {
|
||||||
match field.vis {
|
syn::Visibility::Public(..) => {}
|
||||||
syn::Visibility::Public(..) => {}
|
_ => continue,
|
||||||
_ => continue,
|
|
||||||
}
|
|
||||||
let name = match &field.ident {
|
|
||||||
Some(n) => n,
|
|
||||||
None => continue,
|
|
||||||
};
|
|
||||||
|
|
||||||
let attrs = BindgenAttrs::find(&mut field.attrs)?;
|
|
||||||
assert_not_variadic(&attrs)?;
|
|
||||||
if attrs.skip().is_some() {
|
|
||||||
attrs.check_used()?;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let comments = extract_doc_comments(&field.attrs);
|
|
||||||
let name_str = name.to_string();
|
|
||||||
let getter = shared::struct_field_get(&js_name, &name_str);
|
|
||||||
let setter = shared::struct_field_set(&js_name, &name_str);
|
|
||||||
|
|
||||||
fields.push(ast::StructField {
|
|
||||||
name: name.clone(),
|
|
||||||
struct_name: self.ident.clone(),
|
|
||||||
readonly: attrs.readonly().is_some(),
|
|
||||||
ty: field.ty.clone(),
|
|
||||||
getter: Ident::new(&getter, Span::call_site()),
|
|
||||||
setter: Ident::new(&setter, Span::call_site()),
|
|
||||||
comments,
|
|
||||||
});
|
|
||||||
attrs.check_used()?;
|
|
||||||
}
|
}
|
||||||
|
let (name_str, member) = match &field.ident {
|
||||||
|
Some(ident) => (ident.to_string(), syn::Member::Named(ident.clone())),
|
||||||
|
None => (i.to_string(), syn::Member::Unnamed(i.into())),
|
||||||
|
};
|
||||||
|
|
||||||
|
let attrs = BindgenAttrs::find(&mut field.attrs)?;
|
||||||
|
assert_not_variadic(&attrs)?;
|
||||||
|
if attrs.skip().is_some() {
|
||||||
|
attrs.check_used()?;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let comments = extract_doc_comments(&field.attrs);
|
||||||
|
let getter = shared::struct_field_get(&js_name, &name_str);
|
||||||
|
let setter = shared::struct_field_set(&js_name, &name_str);
|
||||||
|
|
||||||
|
fields.push(ast::StructField {
|
||||||
|
name: member,
|
||||||
|
struct_name: self.ident.clone(),
|
||||||
|
readonly: attrs.readonly().is_some(),
|
||||||
|
ty: field.ty.clone(),
|
||||||
|
getter: Ident::new(&getter, Span::call_site()),
|
||||||
|
setter: Ident::new(&setter, Span::call_site()),
|
||||||
|
comments,
|
||||||
|
});
|
||||||
|
attrs.check_used()?;
|
||||||
}
|
}
|
||||||
let comments: Vec<String> = extract_doc_comments(&self.attrs);
|
let comments: Vec<String> = extract_doc_comments(&self.attrs);
|
||||||
attrs.check_used()?;
|
attrs.check_used()?;
|
||||||
|
@ -1,14 +1,22 @@
|
|||||||
import {
|
import {
|
||||||
ExportedRustType,
|
ExportedNamedStruct,
|
||||||
exported_type_by_value,
|
named_struct_by_value,
|
||||||
exported_type_by_shared_ref,
|
named_struct_by_shared_ref,
|
||||||
exported_type_by_exclusive_ref,
|
named_struct_by_exclusive_ref,
|
||||||
return_exported_type,
|
return_named_struct,
|
||||||
|
|
||||||
|
ExportedTupleStruct,
|
||||||
|
return_tuple_struct
|
||||||
} from './guide_supported_types_examples';
|
} from './guide_supported_types_examples';
|
||||||
|
|
||||||
let rustThing = return_exported_type();
|
let namedStruct = return_named_struct(42);
|
||||||
console.log(rustThing instanceof ExportedRustType); // true
|
console.log(namedStruct instanceof ExportedNamedStruct); // true
|
||||||
|
console.log(namedStruct.inner); // 42
|
||||||
|
|
||||||
exported_type_by_value(rustThing);
|
named_struct_by_value(namedStruct);
|
||||||
exported_type_by_shared_ref(rustThing);
|
named_struct_by_shared_ref(namedStruct);
|
||||||
exported_type_by_exclusive_ref(rustThing);
|
named_struct_by_exclusive_ref(namedStruct);
|
||||||
|
|
||||||
|
let tupleStruct = return_tuple_struct(10, 20);
|
||||||
|
console.log(tupleStruct instanceof ExportedTupleStruct); // true
|
||||||
|
console.log(tupleStruct[0], tupleStruct[1]); // 10, 20
|
||||||
|
@ -1,20 +1,28 @@
|
|||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub struct ExportedRustType {
|
pub struct ExportedNamedStruct {
|
||||||
inner: u32,
|
pub inner: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn exported_type_by_value(x: ExportedRustType) {}
|
pub fn named_struct_by_value(x: ExportedNamedStruct) {}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn exported_type_by_shared_ref(x: &ExportedRustType) {}
|
pub fn named_struct_by_shared_ref(x: &ExportedNamedStruct) {}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn exported_type_by_exclusive_ref(x: &mut ExportedRustType) {}
|
pub fn named_struct_by_exclusive_ref(x: &mut ExportedNamedStruct) {}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn return_exported_type() -> ExportedRustType {
|
pub fn return_named_struct(inner: u32) -> ExportedNamedStruct {
|
||||||
unimplemented!()
|
ExportedNamedStruct { inner }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct ExportedTupleStruct(pub u32, pub u32);
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn return_tuple_struct(x: u32, y: u32) -> ExportedTupleStruct {
|
||||||
|
ExportedTupleStruct(x, y)
|
||||||
}
|
}
|
||||||
|
@ -137,6 +137,7 @@ exports.js_js_rename = () => {
|
|||||||
|
|
||||||
exports.js_access_fields = () => {
|
exports.js_access_fields = () => {
|
||||||
assert.ok((new wasm.AccessFieldFoo()).bar instanceof wasm.AccessFieldBar);
|
assert.ok((new wasm.AccessFieldFoo()).bar instanceof wasm.AccessFieldBar);
|
||||||
|
assert.ok((new wasm.AccessField0())[0] instanceof wasm.AccessFieldBar);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.js_renamed_export = () => {
|
exports.js_renamed_export = () => {
|
||||||
|
@ -369,6 +369,9 @@ pub struct AccessFieldFoo {
|
|||||||
pub bar: AccessFieldBar,
|
pub bar: AccessFieldBar,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct AccessField0(pub AccessFieldBar);
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct AccessFieldBar {
|
pub struct AccessFieldBar {
|
||||||
@ -385,6 +388,14 @@ impl AccessFieldFoo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl AccessField0 {
|
||||||
|
#[wasm_bindgen(constructor)]
|
||||||
|
pub fn new() -> AccessField0 {
|
||||||
|
AccessField0(AccessFieldBar { _value: 2 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn access_fields() {
|
fn access_fields() {
|
||||||
js_access_fields();
|
js_access_fields();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user