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

url: add ToObject method to native URL class #12056

Closed
wants to merge 2 commits into from

Conversation

@jasnell
Copy link
Member

commented Mar 27, 2017

Provides a factory method to convert a native URL class into a JS URL object.

Environment* env = ...

URL url("http://example.org/a/b/c?query#fragment");

MaybeLocal<Value> val = url.ToObject(env);

(Including a test for this is a bit difficult at the current time.)

/cc @bmeck

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

url

url: add ToObject method to native URL class
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);
```
const url = new NativeURL(ctx);
if (!url[searchParams]) { // invoked from URL constructor
url[searchParams] = new URLSearchParams();
url[searchParams][context] = this;

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 27, 2017

Member

Isn’t this === undefined?

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Mar 27, 2017

Member

Additionally, since this function is never "invoked from URL constructor" but !url[searchParams] is always true, you might want to remove the conditional part.

This comment has been minimized.

Copy link
@jasnell

jasnell Mar 27, 2017

Author Member

heh... yeah, just missed changing this one ;-)

@@ -54,6 +54,8 @@

_process.setupRawDebug();

NativeModule.require('internal/url');

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 27, 2017

Member

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');
function NativeURL(ctx) {
this[context] = ctx;
}
util.inherits(NativeURL, URL);

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 27, 2017

Member

I think this would lead to observable differences between URLs constructed with URL vs NativeURL. How about a plain NativeURL.prototype = URL.prototype?

This comment has been minimized.

Copy link
@jasnell

jasnell Mar 27, 2017

Author Member

Yep, that works too :-)

FatalException(isolate, try_catch);
}

return ret;

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 27, 2017

Member

If you’re crashing for empty MaybeLocals anyway, you might as well make this method return Local<Value> and pass ret.ToLocalChecked()?

This comment has been minimized.

Copy link
@jasnell

jasnell Mar 27, 2017

Author Member

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 :-)

@@ -619,6 +626,8 @@ class URL {
return ret;
}

MaybeLocal<Value> ToObject(Environment* env);

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 27, 2017

Member

This can be a const method

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);

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Mar 27, 2017

Member

Can this series of C++-to-JS conversion be somehow consolidated with the code in Parse?

This comment has been minimized.

Copy link
@jasnell

jasnell Mar 27, 2017

Author Member

A common inline utility method could likely be used for both. I was already considering it :-)


const Local<Value> undef = Undefined(isolate);
if (context_.flags & URL_FLAGS_FAILED)
return undef;

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Mar 27, 2017

Member

Either return a Local<Value> as @addaleax suggested, or make this return MaybeLocal<Value>().

@@ -54,6 +54,8 @@

_process.setupRawDebug();

NativeModule.require('internal/url');

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Mar 27, 2017

Member

I'm a bit afraid of the increase in memory usage. Is there a need to make some requires in internal/url lazy?

This comment has been minimized.

Copy link
@jasnell

jasnell Mar 27, 2017

Author Member

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.

using v8::MaybeLocal;
using v8::Value;


#define BIT_AT(a, i) \

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Mar 27, 2017

Member

Aside from this PR, maybe we would want to move these private macros and inlined functions into the .cc file itself?

This comment has been minimized.

Copy link
@jasnell

jasnell Mar 27, 2017

Author Member

Likely. It's been on my todo list but just haven't gotten to it yet.

const url = new NativeURL(ctx);
if (!url[searchParams]) { // invoked from URL constructor
url[searchParams] = new URLSearchParams();
url[searchParams][context] = this;

This comment has been minimized.

Copy link
@TimothyGu

TimothyGu Mar 27, 2017

Member

Additionally, since this function is never "invoked from URL constructor" but !url[searchParams] is always true, you might want to remove the conditional part.

@jasnell

This comment has been minimized.

Copy link
Member Author

commented Mar 27, 2017

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.

@addaleax
Copy link
Member

left a comment

This PR LGTM in its current state

@@ -626,7 +626,7 @@ class URL {
return ret;
}

MaybeLocal<Value> ToObject(Environment* env);
const Local<Value> ToObject(Environment* env) const;

This comment has been minimized.

Copy link
@addaleax

addaleax Mar 27, 2017

Member

Specifying const on a return type dosn’t really do anything, btw ;)

This comment has been minimized.

Copy link
@jasnell

jasnell Mar 27, 2017

Author Member

Bad habits die hard. I appreciate the reminder. Eventually I'll remember ;)

@jasnell

This comment has been minimized.

Copy link
Member Author

commented Mar 27, 2017

jasnell added a commit that referenced this pull request Mar 30, 2017
url: add ToObject method to native URL class
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>
@jasnell

This comment has been minimized.

Copy link
Member Author

commented Mar 30, 2017

Landed in 7139b93

@italoacasas

This comment has been minimized.

Copy link
Member

commented Apr 10, 2017

@TimothyGu

This comment has been minimized.

Copy link
Member

commented Apr 19, 2017

Removing semver-minor since this API is not exposed to users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.