-
Notifications
You must be signed in to change notification settings - Fork 30k
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
url: add ToObject method to native URL class #12056
Conversation
Provides a factory method to convert a native URL class into a JS URL object. ```c++ Environment* env = ... URL url("http://example.org/a/b/c?query#fragment"); MaybeLocal<Value> val = url.ToObject(env); ```
lib/internal/url.js
Outdated
const url = new NativeURL(ctx); | ||
if (!url[searchParams]) { // invoked from URL constructor | ||
url[searchParams] = new URLSearchParams(); | ||
url[searchParams][context] = this; |
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.
Isn’t this === undefined
?
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.
Additionally, since this function is never "invoked from URL constructor" but !url[searchParams]
is always true, you might want to remove the conditional part.
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.
heh... yeah, just missed changing this one ;-)
lib/internal/bootstrap_node.js
Outdated
@@ -54,6 +54,8 @@ | |||
|
|||
_process.setupRawDebug(); | |||
|
|||
NativeModule.require('internal/url'); |
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.
You could add a comment that mentions the side effect you’re using this for, e.g.
// Make sure setURLConstructor() is called before the native
// URL::ToObject() method is used.
NativeModule.require('internal/url');
lib/internal/url.js
Outdated
function NativeURL(ctx) { | ||
this[context] = ctx; | ||
} | ||
util.inherits(NativeURL, URL); |
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 think this would lead to observable differences between URLs constructed with URL
vs NativeURL
. How about a plain NativeURL.prototype = URL.prototype
?
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.
Yep, that works too :-)
src/node_url.cc
Outdated
FatalException(isolate, try_catch); | ||
} | ||
|
||
return ret; |
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.
If you’re crashing for empty MaybeLocals anyway, you might as well make this method return Local<Value>
and pass ret.ToLocalChecked()
?
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 wasn't entirely sure what to do here, to be honest. I wanted to get it out so that @bmeck could play with it then tweak it from there. Your suggestion works for me tho :-)
src/node_url.h
Outdated
@@ -619,6 +626,8 @@ class URL { | |||
return ret; | |||
} | |||
|
|||
MaybeLocal<Value> ToObject(Environment* env); |
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 can be a const
method
src/node_url.cc
Outdated
if (context_.port > -1) | ||
argv[ARG_PORT] = Integer::New(isolate, context_.port); | ||
if (context_.flags & URL_FLAGS_HAS_PATH) | ||
argv[ARG_PATH] = Copy(env, context_.path); |
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.
Can this series of C++-to-JS conversion be somehow consolidated with the code in Parse
?
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.
A common inline utility method could likely be used for both. I was already considering it :-)
src/node_url.cc
Outdated
|
||
const Local<Value> undef = Undefined(isolate); | ||
if (context_.flags & URL_FLAGS_FAILED) | ||
return undef; |
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.
Either return a Local<Value>
as @addaleax suggested, or make this return MaybeLocal<Value>()
.
lib/internal/bootstrap_node.js
Outdated
@@ -54,6 +54,8 @@ | |||
|
|||
_process.setupRawDebug(); | |||
|
|||
NativeModule.require('internal/url'); |
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'm a bit afraid of the increase in memory usage. Is there a need to make some requires in internal/url
lazy?
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.
There shouldn't be. The requires that are there are minimal (util
will have already been loaded, os
is small, etc). I'm not convinced this should be a concern.
src/node_url.h
Outdated
using v8::MaybeLocal; | ||
using v8::Value; | ||
|
||
|
||
#define BIT_AT(a, i) \ |
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.
Aside from this PR, maybe we would want to move these private macros and inlined functions into the .cc
file itself?
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.
Likely. It's been on my todo list but just haven't gotten to it yet.
lib/internal/url.js
Outdated
const url = new NativeURL(ctx); | ||
if (!url[searchParams]) { // invoked from URL constructor | ||
url[searchParams] = new URLSearchParams(); | ||
url[searchParams][context] = this; |
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.
Additionally, since this function is never "invoked from URL constructor" but !url[searchParams]
is always true, you might want to remove the conditional part.
Updated! PTAL! I do not consider this to be ready to land yet, btw. I'd like feedback from @bmeck on whether this will work for what he needs before landing. Marking as in progress so it's not landed prematurely. |
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 PR LGTM in its current state
src/node_url.h
Outdated
@@ -626,7 +626,7 @@ class URL { | |||
return ret; | |||
} | |||
|
|||
MaybeLocal<Value> ToObject(Environment* env); | |||
const Local<Value> ToObject(Environment* env) const; |
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.
Specifying const
on a return type dosn’t really do anything, btw ;)
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.
Bad habits die hard. I appreciate the reminder. Eventually I'll remember ;)
Provides a factory method to convert a native URL class into a JS URL object. ```c++ Environment* env = ... URL url("http://example.org/a/b/c?query#fragment"); MaybeLocal<Value> val = url.ToObject(env); ``` PR-URL: #12056 Reviewed-By: Anna Henningsen <anna@addaleax.net>
Landed in 7139b93 |
cc @jasnell |
Removing semver-minor since this API is not exposed to users. |
Provides a factory method to convert a native URL class into a JS URL object.
(Including a test for this is a bit difficult at the current time.)
/cc @bmeck
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
url