Skip to content
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

Simplify "named intercept" #1585

Merged
merged 9 commits into from
Jan 30, 2024
6 changes: 3 additions & 3 deletions src/workerd/api/worker-rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ constexpr size_t MAX_JS_RPC_MESSAGE_SIZE = 1u << 20;

// A WorkerRpc object forwards JS method calls to the remote Worker/Durable Object over RPC.
// Since methods are not known until runtime, WorkerRpc doesn't define any JS methods.
// Instead, we use JSG_NAMED_INTERCEPT to intercept property accesses of names that are not known at
// compile time.
// Instead, we use JSG_WILDCARD_PROPERTY to intercept property accesses of names that are not known
// at compile time.
//
// WorkerRpc only supports method calls. You cannot, for instance, access a property of a
// Durable Object over RPC.
Expand Down Expand Up @@ -69,7 +69,7 @@ class WorkerRpc : public Fetcher {
// change log.
JSG_RESOURCE_TYPE(WorkerRpc, CompatibilityFlags::Reader flags) {
if (flags.getWorkerdExperimental()) {
JSG_NAMED_INTERCEPT(getRpcMethod);
JSG_WILDCARD_PROPERTY(getRpcMethod);
}
JSG_INHERIT(Fetcher);
}
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/jsg/jsg-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ struct InterceptContext: public ContextGlobalObject {

int getBar() { return 123; }

// JSG_NAMED_INTERCEPT implementation
// JSG_WILDCARD_PROPERTY implementation
kj::Maybe<kj::StringPtr> testGetNamed(jsg::Lock& js, kj::StringPtr name) {
if (name == "foo") {
return "bar"_kj;
Expand All @@ -384,7 +384,7 @@ struct InterceptContext: public ContextGlobalObject {

JSG_RESOURCE_TYPE(ProxyImpl) {
JSG_READONLY_PROTOTYPE_PROPERTY(bar, getBar);
JSG_NAMED_INTERCEPT(testGetNamed);
JSG_WILDCARD_PROPERTY(testGetNamed);
}
};

Expand Down
21 changes: 17 additions & 4 deletions src/workerd/jsg/jsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,24 @@ using HasGetTemplateOverload = decltype(
wrapper.initReflection(this, __VA_ARGS__); \
}

// Configures the resource type to implement named property interception.
// @see the definition of jsg::NamedIntercept in resource.h for more information.
#define JSG_NAMED_INTERCEPT(method) \
// Declares a wildcart property getter. If a property is requested that isn't already present on
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: s/wildcart/wildcard/

// the object or its prototypes, the wildcard property getter will be given a chance to return the
// property.
//
// Example:
//
// struct MyType {
// // Get the value of the named dynamic property. Returns none if the property doesn't exist.
// // `SomeType` can be any type that JSG is able to convert to JavaScript.
// kj::Maybe<SomeType> getWildcard(jsg::Lock& js, kj::StringPtr name);
//
// JSG_RESOURCE_TYPE(MyType) {
// JSG_WILDCARD_PROPERTY(getWildcard);
// }
// };
#define JSG_WILDCARD_PROPERTY(method) \
do { \
registry.template registerNamedIntercept<Self, decltype(&Self::method), &Self::method>(); \
registry.template registerWildcardProperty<Self, decltype(&Self::method), &Self::method>(); \
} while (false)

// Use inside a JSG_RESOURCE_TYPE block to declare that this type should be considered a "root" for
Expand Down
12 changes: 6 additions & 6 deletions src/workerd/jsg/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,19 +604,19 @@ class DynamicResourceTypeMap {
};

// ======================================================================================
// NamedIntercept implementation
// WildcardProperty implementation

class JsValue;

template <typename TypeWrapper, typename T, typename GetNamedMethod, GetNamedMethod getNamedMethod>
struct NamedInterceptorCallbacks;
struct WildcardPropertyCallbacks;

template <typename TypeWrapper, typename T, typename U, typename Ret,
kj::Maybe<Ret> (U::*getNamedMethod)(jsg::Lock&, kj::StringPtr)>
struct NamedInterceptorCallbacks<
struct WildcardPropertyCallbacks<
TypeWrapper, T, kj::Maybe<Ret> (U::*)(jsg::Lock&, kj::StringPtr), getNamedMethod>
: public v8::NamedPropertyHandlerConfiguration {
NamedInterceptorCallbacks() : v8::NamedPropertyHandlerConfiguration(
WildcardPropertyCallbacks() : v8::NamedPropertyHandlerConfiguration(
getter,
nullptr,
nullptr,
Expand Down Expand Up @@ -681,9 +681,9 @@ struct ResourceTypeBuilder {
}

template <typename Type, typename GetNamedMethod, GetNamedMethod getNamedMethod>
inline void registerNamedIntercept() {
inline void registerWildcardProperty() {
prototype->SetHandler(
NamedInterceptorCallbacks<TypeWrapper, Type, GetNamedMethod, getNamedMethod> {});
WildcardPropertyCallbacks<TypeWrapper, Type, GetNamedMethod, getNamedMethod> {});
}

template<typename Type>
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/jsg/rtti.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ struct BuildRtti<Configuration, const T&> {
// count all members in the structure
struct MemberCounter {
template <typename Type, typename GetNamedMethod, GetNamedMethod getNamedMethod>
inline void registerNamedIntercept() { /* not a member */}
inline void registerWildcardProperty() { /* not a member */}

template<const char* name, typename Method, Method method>
inline void registerMethod() { ++members; }
Expand Down Expand Up @@ -818,7 +818,7 @@ struct MembersBuilder {
}

template <typename Type, typename GetNamedMethod, GetNamedMethod getNamedMethod>
inline void registerNamedIntercept() {
inline void registerWildcardProperty() {
// Nothing to do in this case.
}
};
Expand Down
Loading