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

src: refactor callback #defines into C++ templates #18133

Closed
wants to merge 2 commits into from

Conversation

addaleax
Copy link
Member

Use template helpers instead of #defines to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the Parser methods take.

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

src/node_http_parser.cc

Use template helpers instead of `#define`s to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the `Parser` methods take.
@addaleax addaleax added c++ Issues and PRs that require attention from people who are familiar with C++. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding. labels Jan 13, 2018
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding. labels Jan 13, 2018
Copy link
Contributor

@maclover7 maclover7 left a comment

Choose a reason for hiding this comment

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

LGTM as long as CI passes (I'm assuming there's no weird cases here)


// These are helper functions for filling `http_parser_settings`, which turn
// a member function of Parser into a C-style HTTP parser callback.
template <int (Parser::* Member)()>
Copy link
Member

Choose a reason for hiding this comment

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

It's possible to make the template more generic and avoid code duplication like this:

template <typename Parser, Parser> struct Proxy;
template <typename Parser, typename ...Args, int (Parser::*Member)(Args...)>
struct Proxy<int (Parser::*)(Args...), Member> {
  static int Raw(http_parser* p, Args ... args) {
    Parser* parser = ContainerOf(&Parser::parser_, p);
    return (parser->*Member)(std::forward<Args>(args)...);
  }
};

typedef int(Parser::*Call)();
typedef int(Parser::*DataCall)(const char* at, size_t length);

const struct http_parser_settings Parser::settings = {
  Proxy<Call, &Parser::on_message_begin>::Raw,
  Proxy<DataCall, &Parser::on_url>::Raw,
  ...
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Wow! I tried going into that direction but couldn’t get it to compile 😄 Would you prefer your approach? I feel like the current version is already a lot to digest, but your approach is kind of cool

Copy link
Member

@joyeecheung joyeecheung Jan 13, 2018

Choose a reason for hiding this comment

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

I personally prefer DRYing code even it involves a bit more template magic (I mean, that's the point of using templates right XD)..but it's totally fine that you keep it this way.

Copy link
Member Author

Choose a reason for hiding this comment

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

@joyeecheung Okay, done! Kinda still can’t believe it works. 😄

@joyeecheung
Copy link
Member

@addaleax addaleax added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jan 18, 2018
@addaleax
Copy link
Member Author

Landed in 6283077

@addaleax addaleax closed this Jan 18, 2018
@addaleax addaleax deleted the http-parser-refactor branch January 18, 2018 21:56
addaleax added a commit that referenced this pull request Jan 18, 2018
Use template helpers instead of `#define`s to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the `Parser` methods take.

PR-URL: #18133
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
evanlucas pushed a commit that referenced this pull request Jan 30, 2018
Use template helpers instead of `#define`s to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the `Parser` methods take.

PR-URL: #18133
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
evanlucas pushed a commit that referenced this pull request Jan 30, 2018
Use template helpers instead of `#define`s to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the `Parser` methods take.

PR-URL: #18133
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
@MylesBorins
Copy link
Contributor

Should this be backported to v6.x-staging or v8.x-staging? If yes please follow the guide and raise a backport PR, if not let me know or add the dont-land-on label.

MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
Use template helpers instead of `#define`s to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the `Parser` methods take.

PR-URL: nodejs#18133
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 23, 2018
Use template helpers instead of `#define`s to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the `Parser` methods take.

PR-URL: #18133
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jun 14, 2018
Use template helpers instead of `#define`s to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the `Parser` methods take.

PR-URL: #18133
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Jul 9, 2018
rvagg pushed a commit that referenced this pull request Aug 16, 2018
Use template helpers instead of `#define`s to generate
the raw C callbacks that are passed to the HTTP parser library.

A nice effect of this is that it is more obvious what
parameters the `Parser` methods take.

PR-URL: #18133
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants