Skip to content

Commit

Permalink
docs: export method
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Dec 30, 2024
1 parent 95b7db5 commit 1509e7d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
40 changes: 40 additions & 0 deletions wasm/examples/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// the method will be renamed `retrieveCas` in the generated bindings
#[wasm::export]
pub fn retrieve_cas() {
}

// the method will be still called `update` in the generated bindings
#[wasm::export]
pub fn update() {
}

#[wasm::export]
pub struct Session {
instance_url: String,
php_sess_id: String,
}

#[wasm::export]
impl Session {
#[wasm_bindgen(constructor)]
pub fn new(instance_url: String, php_sess_id: String) -> Self {
Self {
instance_url,
php_sess_id,
}
}

#[wasm_bindgen(getter)]
pub fn instance_url(&self) -> String {
self.instance_url.clone()
}

#[wasm_bindgen(getter)]
pub fn php_sess_id(&self) -> String {
self.php_sess_id.clone()
}
}

fn main() {
println!("Hello, world!");
}
27 changes: 26 additions & 1 deletion wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use quote::quote;
/// If applied to a function, it will also rename the
/// name to camel case using `js_name`.
///
/// # Usages
/// ## Usages
///
/// ```rust
/// // The method will be called `retrieveCas` in the generated bindings.
Expand All @@ -28,6 +28,31 @@ use quote::quote;
/// impl Session {
/// // ...
/// }
/// ```
///
/// ## Notes
///
/// In `impl`s, you can use the `#[wasm_bindgen]` attribute
/// wherever you want since the one on the `impl` itself
/// defines it directly.
///
/// ```rust
/// #[wasm::export]
/// impl Session {
/// #[wasm_bindgen(constructor)]
/// pub fn new () -> Self {
/// // ...
/// }
///
/// #[wasm_bindgen(getter = instanceUrl)]
/// pub fn instance_url(&self) -> String {
/// // ...
/// }
/// }
/// ```
///
/// Sadly, we can't override those to make `wasm::getter` and
/// `wasm::constructor` exist, but anyway, it's not a big deal.
///
#[proc_macro_attribute]
pub fn export(_args: TokenStream, input: TokenStream) -> TokenStream {
Expand Down

0 comments on commit 1509e7d

Please sign in to comment.