Skip to content

Commit

Permalink
tools,src: forbid usage of v8::Persistent
Browse files Browse the repository at this point in the history
`v8::Persistent` comes with the surprising catch that it requires
manual cleanup. `v8::Global` doesn’t, making it easier to use,
and additionally provides move semantics. New code should always
use `v8::Global`.

PR-URL: nodejs#31018
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • Loading branch information
addaleax committed Dec 24, 2019
1 parent 62436c2 commit e23bf8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/node_object_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class ObjectWrap {
}


// NOLINTNEXTLINE(runtime/v8_persistent)
inline v8::Persistent<v8::Object>& persistent() {
return handle_;
}
Expand Down Expand Up @@ -122,6 +123,7 @@ class ObjectWrap {
delete wrap;
}

// NOLINTNEXTLINE(runtime/v8_persistent)
v8::Persistent<v8::Object> handle_;
};

Expand Down
26 changes: 26 additions & 0 deletions tools/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
'runtime/string',
'runtime/threadsafe_fn',
'runtime/vlog',
'runtime/v8_persistent',
'whitespace/blank_line',
'whitespace/braces',
'whitespace/comma',
Expand Down Expand Up @@ -627,6 +628,8 @@

_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')

_V8_PERSISTENT_PATTERN = re.compile(r'\bv8::Persistent\b')

_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]'
r'(?<!(sizeof|return))'
r'\s\*[a-zA-Z_][0-9a-zA-Z_]*')
Expand Down Expand Up @@ -4547,6 +4550,28 @@ def CheckNullTokens(filename, clean_lines, linenum, error):
error(filename, linenum, 'readability/null_usage', 2,
'Use nullptr instead of NULL')

def CheckV8PersistentTokens(filename, clean_lines, linenum, error):
"""Check v8::Persistent usage.
Args:
filename: The name of the current file.
clean_lines: A CleansedLines instance containing the file.
linenum: The number of the line to check.
error: The function to call with any errors found.
"""
line = clean_lines.elided[linenum]

# Avoid preprocessor lines
if Match(r'^\s*#', line):
return

if line.find('/*') >= 0 or line.find('*/') >= 0:
return

for match in _V8_PERSISTENT_PATTERN.finditer(line):
error(filename, linenum, 'runtime/v8_persistent', 2,
'Use v8::Global instead of v8::Persistent')

def CheckLeftLeaningPointer(filename, clean_lines, linenum, error):
"""Check for left-leaning pointer placement.
Expand Down Expand Up @@ -4723,6 +4748,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
CheckCheck(filename, clean_lines, linenum, error)
CheckAltTokens(filename, clean_lines, linenum, error)
CheckNullTokens(filename, clean_lines, linenum, error)
CheckV8PersistentTokens(filename, clean_lines, linenum, error)
CheckLeftLeaningPointer(filename, clean_lines, linenum, error)
classinfo = nesting_state.InnermostClass()
if classinfo:
Expand Down

0 comments on commit e23bf8f

Please sign in to comment.