Skip to content

(feat) generate wasm-bindgen wrappers for #[rpc] methods in impl … #721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ilijaljubicic
Copy link

@ilijaljubicic ilijaljubicic commented Apr 3, 2025

Improvements

  • Introduced #[rpc] attribute macro for impl blocks
  • Automatically generates:
    • #[wasm_bindgen] struct with env: worker::Env
    • #[wasm_bindgen(constructor)] new(env: Env)
    • #[wasm_bindgen(js_name = "is_rpc")] method for JS discovery
  • Replaces #[rpc] on methods with #[wasm_bindgen]
  • Requires unique method names across all RPC exports
  • Updated macro docs and runtime constraints

Allows using Env inside RPC methods. Open space for further improvements for the rpc functionality.

Related: #720

RPC methods can be exported using a custom #[rpc] attribute macro.
RPC methods must be defined inside an impl block annotated with #[rpc], and individual methods must also be marked with #[rpc].

When the macro is expanded, it generates:

  • A #[wasm_bindgen]-annotated struct with env: worker::Env
  • A constructor function: #[wasm_bindgen(constructor)] pub fn new(env: Env)
  • A method #[wasm_bindgen(js_name = "__is_rpc__")] fn is_rpc(&self) -> bool for RPC auto-discovery
  • All methods marked with #[rpc] converted into #[wasm_bindgen]-annotated exports

Example

#[rpc]
impl Rpc {
    #[rpc]
    pub async fn add(&self, a: u32, b: u32) -> u32 {
        // self.env available 
        a + b
    }
}

Expands to:

#[wasm_bindgen]
pub struct Rpc {
    env: worker::Env,
}

#[wasm_bindgen]
impl Rpc {
    #[wasm_bindgen(js_name = "__is_rpc__")]
    pub fn is_rpc(&self) -> bool {
        true
    }
    #[wasm_bindgen(constructor)]
    pub fn new(env: worker::Env) -> Self {
        Self { env }
    }

    #[wasm_bindgen]
    pub async fn add(&self, a: u32, b: u32) -> u32 {
        a + b
    }
}

Used in in rpc client workers:

export default {
  async fetch(request, env) {
    const result = await env.RPC_SERVICE.add(1,2);
    return new Response(JSON.stringify(result, null, 2));
  }
}

…blocks

- Introduced #[rpc] attribute macro for impl blocks
- Automatically generates:
  - #[wasm_bindgen] struct with `env: worker::Env`
  - #[wasm_bindgen(constructor)] `new(env: Env)`
  - #[wasm_bindgen(js_name = "__is_rpc__")] method for JS discovery
- Replaces #[rpc] on methods with #[wasm_bindgen]
- Requires unique method names across all RPC exports
- Updated macro docs and runtime constraints
- Enables using Env inside rpc methods.
@ilijaljubicic ilijaljubicic changed the title generate wasm-bindgen wrappers for #[rpc] methods in impl … (feat) generate wasm-bindgen wrappers for #[rpc] methods in impl … Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant