mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-13 04:51:23 +00:00
Implement support for js_class
on exported types
Allow defining types which have different names in Rust than they have in JS! (just like can be done with imported types) Closes #1010
This commit is contained in:
@ -29,8 +29,10 @@ pub struct Program {
|
||||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
||||
#[derive(Clone)]
|
||||
pub struct Export {
|
||||
/// The javascript class name.
|
||||
pub class: Option<Ident>,
|
||||
/// The struct name, in Rust, this is attached to
|
||||
pub rust_class: Option<Ident>,
|
||||
/// The class name in JS this is attached to
|
||||
pub js_class: Option<String>,
|
||||
/// The type of `self` (either `self`, `&self`, or `&mut self`)
|
||||
pub method_self: Option<MethodSelf>,
|
||||
/// Whether or not this export is flagged as a constructor, returning an
|
||||
@ -176,7 +178,8 @@ pub struct Function {
|
||||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
||||
#[derive(Clone)]
|
||||
pub struct Struct {
|
||||
pub name: Ident,
|
||||
pub rust_name: Ident,
|
||||
pub js_name: String,
|
||||
pub fields: Vec<StructField>,
|
||||
pub comments: Vec<String>,
|
||||
}
|
||||
@ -264,9 +267,9 @@ impl Export {
|
||||
/// name and class name, if the function belongs to a javascript class.
|
||||
pub(crate) fn rust_symbol(&self) -> Ident {
|
||||
let mut generated_name = String::from("__wasm_bindgen_generated");
|
||||
if let Some(class) = &self.class {
|
||||
if let Some(class) = &self.js_class {
|
||||
generated_name.push_str("_");
|
||||
generated_name.push_str(&class.to_string());
|
||||
generated_name.push_str(class);
|
||||
}
|
||||
generated_name.push_str("_");
|
||||
generated_name.push_str(&self.function.name.to_string());
|
||||
@ -278,8 +281,8 @@ impl Export {
|
||||
/// "high level" form before calling the actual function.
|
||||
pub(crate) fn export_name(&self) -> String {
|
||||
let fn_name = self.function.name.to_string();
|
||||
match &self.class {
|
||||
Some(class) => shared::struct_function_export_name(&class.to_string(), &fn_name),
|
||||
match &self.js_class {
|
||||
Some(class) => shared::struct_function_export_name(class, &fn_name),
|
||||
None => shared::free_function_export_name(&fn_name),
|
||||
}
|
||||
}
|
||||
|
@ -118,8 +118,8 @@ impl TryToTokens for ast::Program {
|
||||
|
||||
impl ToTokens for ast::Struct {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let name = &self.name;
|
||||
let name_str = name.to_string();
|
||||
let name = &self.rust_name;
|
||||
let name_str = self.js_name.to_string();
|
||||
let name_len = name_str.len() as u32;
|
||||
let name_chars = name_str.chars().map(|c| c as u32);
|
||||
let new_fn = Ident::new(&shared::new_function(&name_str), Span::call_site());
|
||||
@ -328,7 +328,7 @@ impl TryToTokens for ast::Export {
|
||||
let name = &self.rust_name;
|
||||
let receiver = match self.method_self {
|
||||
Some(ast::MethodSelf::ByValue) => {
|
||||
let class = self.class.as_ref().unwrap();
|
||||
let class = self.rust_class.as_ref().unwrap();
|
||||
arg_conversions.push(quote! {
|
||||
let me = unsafe {
|
||||
<#class as ::wasm_bindgen::convert::FromWasmAbi>::from_abi(
|
||||
@ -340,7 +340,7 @@ impl TryToTokens for ast::Export {
|
||||
quote! { me.#name }
|
||||
}
|
||||
Some(ast::MethodSelf::RefMutable) => {
|
||||
let class = self.class.as_ref().unwrap();
|
||||
let class = self.rust_class.as_ref().unwrap();
|
||||
arg_conversions.push(quote! {
|
||||
let mut me = unsafe {
|
||||
<#class as ::wasm_bindgen::convert::RefMutFromWasmAbi>
|
||||
@ -354,7 +354,7 @@ impl TryToTokens for ast::Export {
|
||||
quote! { me.#name }
|
||||
}
|
||||
Some(ast::MethodSelf::RefShared) => {
|
||||
let class = self.class.as_ref().unwrap();
|
||||
let class = self.rust_class.as_ref().unwrap();
|
||||
arg_conversions.push(quote! {
|
||||
let me = unsafe {
|
||||
<#class as ::wasm_bindgen::convert::RefFromWasmAbi>
|
||||
@ -367,7 +367,7 @@ impl TryToTokens for ast::Export {
|
||||
});
|
||||
quote! { me.#name }
|
||||
}
|
||||
None => match &self.class {
|
||||
None => match &self.rust_class {
|
||||
Some(class) => quote! { #class::#name },
|
||||
None => quote! { #name },
|
||||
},
|
||||
|
@ -58,7 +58,7 @@ fn shared_export<'a>(export: &'a ast::Export, intern: &'a Interner) -> Export<'a
|
||||
None => (false, false),
|
||||
};
|
||||
Export {
|
||||
class: export.class.as_ref().map(|s| intern.intern(s)),
|
||||
class: export.js_class.as_ref().map(|s| &**s),
|
||||
method,
|
||||
consumed,
|
||||
is_constructor: export.is_constructor,
|
||||
@ -187,7 +187,7 @@ fn shared_import_enum<'a>(_i: &'a ast::ImportEnum, _intern: &'a Interner)
|
||||
|
||||
fn shared_struct<'a>(s: &'a ast::Struct, intern: &'a Interner) -> Struct<'a> {
|
||||
Struct {
|
||||
name: intern.intern(&s.name),
|
||||
name: &s.js_name,
|
||||
fields: s.fields.iter().map(|s| shared_struct_field(s, intern)).collect(),
|
||||
comments: s.comments.iter().map(|s| &**s).collect(),
|
||||
}
|
||||
|
Reference in New Issue
Block a user