Skip to content

Add #[jsg_callable] to Rust JSG - #8201

Merged
bartlomieju merged 4 commits into
mainfrom
biwanczuk/rust-jsg-callable
Nov 5, 2026
Merged

bartlomieju merged 4 commits into
mainfrom
biwanczuk/rust-jsg-callable

Conversation

@bartlomieju

Copy link
Copy Markdown
Collaborator

C++ JSG's JSG_CALLABLE makes a resource's instances callable as functions.
Its consumers are JS RPC (JsRpcStub, JsRpcProperty, JsRpcPromise), the
ctx.exports loopback stubs, and the experimental SqlStorage::Statement.
The RPC and loopback types combine the call with a wildcard property or
with inherited native methods, so neither a plain JS function nor a Proxy
can stand in for them: a Proxy has no internal fields, so methods checked
by a v8::Signature reject it. Those types need the call handler on the
same instance template, which is what JSG_CALLABLE installs.

#[jsg_callable] on a method of a #[jsg_resource] impl registers
Member::Callable, and create_resource_template() installs its callback
with SetCallAsFunctionHandler on the instance template, as C++
registerCallable() does. As in C++:

  • the method runs whether the instance is called with or without new,
    always with the instance as this;
  • argument errors name the method "(called as function)";
  • typeof is "function", but the object keeps the type's prototype;
  • RTTI describes the method as the structure's callable signature.

The method takes &self or this: jsg::Rc<Self> and otherwise has the
shape of a #[jsg_method]. A call handler has no v8::Signature, so the
callback resolves its receiver with the marker check, as accessors do. A
type is callable at most one way: a second #[jsg_callable] is a compile
error with automatic registration, and custom_members may choose between
several by compatibility flag, as LoopbackServiceStub does with
enable_version_api. The callback and factory are
_callable_callback and _callable_member, so a method can be
both a #[jsg_method] and the callable, as in the C++ JSG_CALLABLE test.

To let the attributes stack in either order, the callback-generating
macros (#[jsg_method], #[jsg_property], #[jsg_inspect_property]) now keep
the method's other attributes instead of re-emitting only its signature
and body. They used to drop doc comments and attributes such as the
#[expect]s on two methods in jsg-test's fast_api.rs, which now apply.

This adds no API visible to Workers; no Rust resource is callable yet.

C++ JSG's JSG_CALLABLE makes a resource's instances callable as functions.
Its consumers are JS RPC (JsRpcStub, JsRpcProperty, JsRpcPromise), the
ctx.exports loopback stubs, and the experimental SqlStorage::Statement.
The RPC and loopback types combine the call with a wildcard property or
with inherited native methods, so neither a plain JS function nor a Proxy
can stand in for them: a Proxy has no internal fields, so methods checked
by a v8::Signature reject it. Those types need the call handler on the
same instance template, which is what JSG_CALLABLE installs.

#[jsg_callable] on a method of a #[jsg_resource] impl registers
Member::Callable, and create_resource_template() installs its callback
with SetCallAsFunctionHandler on the instance template, as C++
registerCallable() does. As in C++:

- the method runs whether the instance is called with or without `new`,
  always with the instance as `this`;
- argument errors name the method "(called as function)";
- typeof is "function", but the object keeps the type's prototype;
- RTTI describes the method as the structure's `callable` signature.

The method takes `&self` or `this: jsg::Rc<Self>` and otherwise has the
shape of a #[jsg_method]. A call handler has no v8::Signature, so the
callback resolves its receiver with the marker check, as accessors do. A
type is callable at most one way: a second #[jsg_callable] is a compile
error with automatic registration, and custom_members may choose between
several by compatibility flag, as LoopbackServiceStub does with
enable_version_api. The callback and factory are
<name>_callable_callback and <name>_callable_member, so a method can be
both a #[jsg_method] and the callable, as in the C++ JSG_CALLABLE test.

To let the attributes stack in either order, the callback-generating
macros (#[jsg_method], #[jsg_property], #[jsg_inspect_property]) now keep
the method's other attributes instead of re-emitting only its signature
and body. They used to drop doc comments and attributes such as the
#[expect]s on two methods in jsg-test's fast_api.rs, which now apply.

This adds no API visible to Workers; no Rust resource is callable yet.
@bartlomieju
bartlomieju requested review from a team as code owners November 3, 2026 22:25

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 33656ea0.

This review, of 9371d329

✅ Approved — I found no issues: the callable registration, receiver check, RTTI output and macro attribute handling are consistent with each other and with C++ JSG_CALLABLE, and tests cover them.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The call handler checks its receiver with the marker check, so if this is not an instance of the type the call is rejected instead of casting it. The callback pointer comes from Rust code generated by the macros and is registered as an external reference. The new C++ shim code uses no C++ JSG calls.


📖 AGENTS.md: ✅ LGTM

The src/rust/AGENTS.md update is consistent with the change.


📋 Code Quality: ✅ LGTM

The Member::Callable registration, the marker receiver check, the RTTI callable output and the attribute-preserving macro changes are consistent with each other. Unit, compile-fail, RTTI and runtime tests cover them.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The README updates describe the new attribute accurately.


Reviewed commit: 9371d329

@codecov-commenter

codecov-commenter commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 41.66667% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.87%. Comparing base (b9b44d8) to head (c0781bd).

Files with missing lines Patch % Lines
src/rust/jsg/ffi.c++ 0.00% 4 Missing and 1 partial ⚠️
src/rust/jsg/resource.rs 16.66% 5 Missing ⚠️
src/rust/jsg/rtti.rs 69.23% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8201      +/-   ##
==========================================
- Coverage   68.88%   68.87%   -0.01%     
==========================================
  Files         745      745              
  Lines      195047   195068      +21     
  Branches    30288    30289       +1     
==========================================
+ Hits       134350   134355       +5     
- Misses      42925    42938      +13     
- Partials    17772    17775       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Emitting a method with its own block span made clippy lint the method's
signature, so needless_pass_by_ref_mut, unused_self and similar lints fired
on every jsg_method whose signature JavaScript dictates. Wrap the body in
braces spanned at the macro call again, as before the methods kept their
other attributes, and fix the two lints in the callable tests.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of fb738751.

This review, of 33656ea0

✅ Approved — No issues found. The attribute-preserving macro expansion and the new callable tests are consistent with C++ JSG_CALLABLE.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The call handler resolves its receiver with the marker check. Its callback pointer comes from code the macros generate and is registered as an external reference. At most one callable is registered per type: a second #[jsg_callable] fails to compile, and registration asserts it. Workers get no new API.


📖 AGENTS.md: ✅ LGTM

The src/rust/AGENTS.md update matches the change.


📋 Code Quality: ✅ LGTM

reemit_method, generate_callbacks and jsg_callable in jsg-macros/lib.rs work together correctly. Each method keeps its other attributes, and the body is wrapped in braces spanned at the macro call again. When a signature is rejected, the macro still emits the method, the error and the callback stubs, so registration doesn't produce extra errors. The <name>_callable_callback and <name>_callable_member names match what resource.rs collects. The tests in callable.rs use helpers that exist and cover the documented behaviour.


⚡ Performance: ✅ LGTM

The change only adds template setup that runs once per type, so it raises no performance concerns.


📝 Documentation: ✅ LGTM

The README updates describe #[jsg_callable] accurately.


Reviewed commit: 33656ea0

…llable

# Conflicts:
#	src/rust/jsg/rtti-test.rs

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of c0781bda.

This review, of fb738751

✅ Approved — No issues found. The only change since the last review adds RTTI tests that match the callable signature handling in rtti.rs.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The new RTTI tests only read serialized RTTI output, so they add no attack surface. The call handler gets its callback pointer from the descriptor the macros generate, and that pointer is registered as an external reference.


📖 AGENTS.md: ✅ LGTM

The src/rust/AGENTS.md update matches the change.


📋 Code Quality: ✅ LGTM

The merge commit adds two RTTI tests to src/rust/jsg/rtti-test.rs. Both match rtti.rs:

  • callable_is_the_structures_callable_signature checks the callable's return type and argument list, and that the remaining members are left as they are.
  • structure_without_callable_has_none checks that a structure without a callable reports none.

⚡ Performance: ✅ LGTM

The only new code since the last review is test code. The callable support itself only adds template setup that runs once per type.


📝 Documentation: ✅ LGTM

The README updates describe #[jsg_callable] accurately.


Reviewed commit: fb738751

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found. The only change since the last approved review is the merge resolution in src/rust/AGENTS.md, and it is consistent with the change.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The call handler's callback pointer comes from the descriptor the macros generate, never from JS input, and it is registered as an external reference. A type can be callable at most one way: a compile-time check covers automatic registration and a runtime assert covers custom_members.


📖 AGENTS.md: ✅ LGTM

After the merge resolution, the src/rust/AGENTS.md update still matches the change.


📋 Code Quality: ✅ LGTM

  • Registration: #[jsg_callable] generates <name>_callable_callback and <name>_callable_member, so a method can be both a #[jsg_method] and the callable without a name clash.
  • More than one callable: with automatic registration, a second #[jsg_callable] is a compile error. With custom_members, a runtime assert rejects registering more than one.
  • Receiver check: the call handler uses the marker receiver check, so a wrong receiver throws "Illegal invocation" instead of panicking.
  • Attributes: methods now keep their other attributes, so the attributes can stack in either order.
  • RTTI: the callable is written as the structure's callable signature.

⚡ Performance: ✅ LGTM

The new code runs once per type, when the template is set up, so the runtime hot path is unaffected.


📝 Documentation: ✅ LGTM

The README updates describe #[jsg_callable] accurately.


Reviewed commit: c0781bda

@bartlomieju
bartlomieju merged commit e6cb60a into main Nov 5, 2026
23 of 24 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/rust-jsg-callable branch November 5, 2026 10:40
bartlomieju added a commit that referenced this pull request Nov 5, 2026
…spose

Conflicts with #[jsg_callable] (#8201) were parallel additions, resolved
by keeping both: the Member, descriptor and RTTI variants, the macros'
registrations and docs.

Two fixes the merge needs to compile:

- #8213 added an early return to generate_callbacks() written for the
  TokenStream it returned before; it returns a Result here.
- #8201 made member attributes re-emit the function with its other
  attributes, so that a later #[jsg_callable] expands. The disposer
  relied on the first attribute dropping them, and a #[jsg_dispose] after
  a #[jsg_method] (or after an invalid #[jsg_property] or
  #[jsg_inspect_property]) generated a second <name>_callback. Those
  attributes now drop just the disposer attributes, and a disposer
  attribute generates no callback when another member attribute, or the
  other disposer attribute, is on the function.
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.

4 participants