Repository navigation
Add #[jsg_callable] to Rust JSG - #8201
Conversation
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.
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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_signaturechecks the callable's return type and argument list, and that the remaining members are left as they are.structure_without_callable_has_nonechecks 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
…llable # Conflicts: # src/rust/AGENTS.md
There was a problem hiding this comment.
✅ 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_callbackand<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. Withcustom_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
callablesignature.
⚡ 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
…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.
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++:
new,always with the instance as
this;callablesignature.The method takes
&selforthis: jsg::Rc<Self>and otherwise has theshape 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.