-
Notifications
You must be signed in to change notification settings - Fork 293
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
Adds JSG_NAMED_INTERCEPT #1288
Adds JSG_NAMED_INTERCEPT #1288
Conversation
I think |
ac94f6b
to
63e79db
Compare
This comment was marked as resolved.
This comment was marked as resolved.
ba7c284
to
e8bf535
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really cool! Left some questions/suggestions, but good to go in general.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this James, very much appreciated!
I was working on #1311 (comment) and noticed that if I Ex.
Should be able to catch a JSG exception thrown during name interception, right? Any ideas what might be wrong? |
This mechanism uses v8's underlying named property handler mechanism to allow a JSG_RESOURCE_TYPE to expose dynamic named properties (much like a Proxy). Currently this only supports read-only properties. The intent of this is to support the dynamic interface of a JS RPC proxy.
e8bf535
to
a3d7ead
Compare
Yeah, I've updated with a fix for that. Please give it a try again :-) |
Works now, thanks! |
Ok, I'll get an internal CI run going now and if that looks good I'll get this merged. I'll likely hold off actually pulling this into the internal repo immediately tho. |
int getBar() { return 123; } | ||
|
||
// NamedIntercept implementation | ||
kj::Maybe<jsg::JsValue> getNamed(jsg::Lock& js, kj::StringPtr name) override { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell Could we allow getNamed()
to return an arbitrary type, which then gets JSG-wrapped?
(Most implementations would return jsg::Optional<Something>
so that they can return none for properties that don't exist...)
// would return kj::none). | ||
// | ||
// struct ProxyImpl: public jsg::Object, | ||
// public jsg::NamedIntercept { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell It surprises me that this is based on inheritance, and the use of multiple inheritance makes me nervous since if you don't declare jsg::Object
as the first superclass, stuff will break.
Is this NamedIntercept
base class actually necessary? It looks like the implementation is templated such that the calls are always made on the derived class anyway...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually ran into this problem the other day, declared the jsg::Object
(in my case, Fetcher
) after NamedIntercept
and got segfaults for a while.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this
NamedIntercept
base class actually necessary?
Probably not. I put this together fairly quickly so that @MellowYarker could make progress, fully expecting that it could be refined. I think we ought to be able to drop the base class and I want to see if I can make the three methods that need to be implemented here a bit more ergonomic.
Just noticed that if I define a private method on my DO and try to call it from the client side, I get Is there any way we can have private names also be intercepted? We want to reject calls to private methods over RPC, but I think it would be better if the script got a clear exception stating we don't allow calling private methods over RPC rather than the message above. |
This mechanism uses v8's underlying named property handler mechanism to allow a JSG_RESOURCE_TYPE to expose dynamic named properties (much like a Proxy). Currently this only supports read-only properties. The intent of this is to support the dynamic interface of a JS RPC proxy.
This is a first pass at the JSG api for this. I'm sure there is lots of room for improvement but I wanted to get something out that would allow the folks on the JS RPC stuff to make progress.