-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
doc: cleanup and add references to CPP_STYLE_GUIDE.md #23650
Conversation
CPP_STYLE_GUIDE.md
Outdated
not inside of nested calls. | ||
|
||
Using C++ `throw` is not allowed. | ||
Node.js is built without C++ exception semantics, so code using `thorw` or, even |
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.
thorw
->throw
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.
ack
CPP_STYLE_GUIDE.md
Outdated
```cpp | ||
std::unique_ptr<Foo> FooFactory(); | ||
void FooConsumer(std::unique_ptr<Foo> ptr); | ||
``` | ||
Since `std::unique_ptr` has only move semantics, passing one by value trasfers |
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.
trasfers
->transfers
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.
ack
CPP_STYLE_GUIDE.md
Outdated
@@ -316,13 +311,19 @@ exports.foo = function(str) { | |||
|
|||
#### Avoid throwing JavaScript errors in nested C++ methods | |||
|
|||
When you have to throw the errors from C++, try to do it at the top level and | |||
When you need to return an error from C++, try to do it at the top level and |
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 referred specifically to throwing exceptions, though.
(s/error/exception/ would be a good change here, that’s on me.)
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.
ack
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 you clear this up for me? we can't c++ throw
, we need to isolate()->ThrowException()
right?
CPP_STYLE_GUIDE.md
Outdated
- Use `static_cast` for casting whenever it works | ||
- `reinterpret_cast` is okay if `static_cast` is not appropriate | ||
- Use `static_cast<T>` if casting is required, and its valid | ||
- Use `reinterpret_cast` only when its essential |
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.
typo: it is (twice)
(and maybe s/essential/necessary/)
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.
ack x 3
CPP_STYLE_GUIDE.md
Outdated
|
||
Never use `std::auto_ptr`. Instead, use `std::unique_ptr`. | ||
Avoid `std::auto_ptr`. It was deprecated in C++11 and removed from C++17. |
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 seemed sensible in previous form? There’s no reason to use it in our code, and that it’s deprecated/removed is not really of concern if you already know not to use it
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 didn't like the phrasing "Never use", also I do believe an explanation why is nice, since it's very short.
Maybe:
Don't use `std::auto_ptr`.<br/>
It was deprecated in C++11 if favor of `std::unique_ptr`, and later removed from
C++17. `std::unique_ptr` has move semantics instead the of unusual copy
semantics of `std::auto_ptr`.
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.
Actually we should be able to lint for that. I'll see if I can figure out how to.
CPP_STYLE_GUIDE.md
Outdated
```cpp | ||
std::unique_ptr<Foo> FooFactory(); | ||
void FooConsumer(std::unique_ptr<Foo> ptr); | ||
``` | ||
Since `std::unique_ptr` has only move semantics, passing one by value trasfers |
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.
nit: blank lines around code block
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.
ack
CPP_STYLE_GUIDE.md
Outdated
@@ -192,40 +192,35 @@ class FancyContainer { | |||
|
|||
### Use `nullptr` instead of `NULL` or `0` | |||
|
|||
What it says in the title. | |||
Further reading at [ES.47]. |
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’d really like it if we could make these links more expressive instead of just having the item numbers in the text, e.g. Further reading in the [C++ Core Guidelines][ES.47]
?
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.
ack
CPP_STYLE_GUIDE.md
Outdated
|
||
Prefer to use `std::unique_ptr` to make ownership | ||
transfer explicit. For example: | ||
[R.20]: Use `std::unique_ptr` or `std::shared_ptr` to represent ownership |
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.
Currently, this and next lines are rendered as one line.
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.
ack
CPP_STYLE_GUIDE.md
Outdated
Prefer to use `std::unique_ptr` to make ownership | ||
transfer explicit. For example: | ||
[R.20]: Use `std::unique_ptr` or `std::shared_ptr` to represent ownership<br/> | ||
[R.21]: Prefer `unique_ptr` over `shared_ptr` unless you need to share ownership |
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 want a list of things, you can juse start lines with *
instead of manually inserting line breaks
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.
ack x 3
CPP_STYLE_GUIDE.md
Outdated
Since `std::unique_ptr` has only move semantics, passing one by value transfers | ||
ownership to the callee and invalidates the instance the caller hold. | ||
|
||
Don't use `std::auto_ptr`.<br/> |
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.
No need for a line break, this makes sense as one paragraph.
CPP_STYLE_GUIDE.md
Outdated
|
||
Further reading:<br/> | ||
[ES.48]: Avoid casts<br/> | ||
[ES.49]: If you must use a cast, use a named cast |
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.
ditto here
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!
CPP_STYLE_GUIDE.md
Outdated
@@ -316,13 +316,19 @@ exports.foo = function(str) { | |||
|
|||
#### Avoid throwing JavaScript errors in nested C++ methods | |||
|
|||
When you have to throw the errors from C++, try to do it at the top level and | |||
When you need to throw an exception from C++, try to do it at the top level and |
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 somehow reads a bit ambiguous to me now that it's exception
, can you change it to JavaScript exception
?
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.
Ack
CPP_STYLE_GUIDE.md
Outdated
Since `std::unique_ptr` has only move semantics, passing one by value transfers | ||
ownership to the callee and invalidates the instance the caller hold. | ||
|
||
Don't use `std::auto_ptr`. It was deprecated in C++11 if favor of |
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.
Do we need to explain this here? Can we link to some docs somewhere?
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.
Ack (ish). PTAL
CPP_STYLE_GUIDE.md
Outdated
When you need to throw a JavaScript exception from C++ (i.e. | ||
`isolate()->ThrowException()`) prefer to do it as close to the return to JS as | ||
possible, and not inside of nested C++ calls. Since this changes the `isolate`'s | ||
state doing closest to where it is consumed, reduces the chance of side effects. |
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.
@joyeecheung, @addaleax PTAL
I'm trying to grok this, so I wrote it down.
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.
s/isolate/Isolate/, and the comma should be removed
I don’t think it matters that it’s an Isolate
thing, though. That’s implementation details, and you can just say “JS execution state” or something like that.
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.
Ack
CPP_STYLE_GUIDE.md
Outdated
|
||
Using C++ `throw` is not allowed. | ||
Node.js is built without C++ exception semantics, so code using `throw` or, even |
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.
s/exception semantics/exceptions/?
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 went with exception handling
and added a reference (GCC state 7% runtime cost which I think is interesting).
CPP_STYLE_GUIDE.md
Outdated
When you need to throw a JavaScript exception from C++ (i.e. | ||
`isolate()->ThrowException()`) prefer to do it as close to the return to JS as | ||
possible, and not inside of nested C++ calls. Since this changes the `isolate`'s | ||
state doing closest to where it is consumed, reduces the chance of side effects. |
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.
s/isolate/Isolate/, and the comma should be removed
I don’t think it matters that it’s an Isolate
thing, though. That’s implementation details, and you can just say “JS execution state” or something like that.
41872c5
to
5764fdb
Compare
PR-URL: nodejs#23650 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
5764fdb
to
cf3f8dd
Compare
PR-URL: #23650 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: #23650 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: #23650 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: #23650 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: #23650 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes