GeezerVison is showing github.com — src: update std::vector<v8::Local<T>> to use v8::LocalVector<T> · nodejs/node@4e7ae97
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/README.md +20Lines changed: 20 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -151,6 +151,25 @@ is done executing. `Local` handles can only be allocated on the C++ stack. | 151 | 151 | Most of the V8 API uses `Local` handles to work with JavaScript values or return | 152 | 152 | them from functions. | 153 | 153 |
| | 154 | +Additionally, according to [V8 public API documentation][`v8::Local<T>`], local handles | | 155 | +(`v8::Local<T>`) should **never** be allocated on the heap. | | 156 | + | | 157 | +This disallows heap-allocated data structures containing instances of `v8::Local` | | 158 | + | | 159 | +For example: | | 160 | + | | 161 | +```cpp | | 162 | +// Don't do this | | 163 | +std::vector<v8::Local<v8::Value>> v1; | | 164 | +``` | | 165 | + | | 166 | +Instead, it is recommended to use `v8::LocalVector<T>` provided by V8 | | 167 | +for such scenarios: | | 168 | + | | 169 | +```cpp | | 170 | +v8::LocalVector<v8::Value> v1(isolate); | | 171 | +``` | | 172 | +
| 154 | 173 | Whenever a `Local` handle is created, a `v8::HandleScope` or | 155 | 174 | `v8::EscapableHandleScope` object must exist on the stack. The `Local` is then | 156 | 175 | added to that scope and deleted along with it. | @@ -1409,6 +1428,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) { | 1409 | 1428 | [`v8.h` in Code Search]: https://cs.chromium.org/chromium/src/v8/include/v8.h | 1410 | 1429 | [`v8.h` in Node.js]: https://github.com/nodejs/node/blob/HEAD/deps/v8/include/v8.h | 1411 | 1430 | [`v8.h` in V8]: https://github.com/v8/v8/blob/HEAD/include/v8.h | | 1431 | +[`v8::Local<T>`]: https://v8.github.io/api/head/classv8_1_1Local.html | 1412 | 1432 | [`vm` module]: https://nodejs.org/api/vm.html | 1413 | 1433 | [binding function]: #binding-functions | 1414 | 1434 | [cleanup hooks]: #cleanup-hooks | |
Collapse file Copy file name to clipboardExpand all lines: src/node_contextify.cc +16-14Lines changed: 16 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1166,7 +1166,7 @@ Maybe<void> StoreCodeCacheResult( | 1166 | 1166 | MaybeLocal<Function> CompileFunction(Local<Context> context, | 1167 | 1167 | Local<String> filename, | 1168 | 1168 | Local<String> content, | 1169 | | - std::vector<Local<String>>* parameters) { | | 1169 | + LocalVector<String>* parameters) { | 1170 | 1170 | ScriptOrigin script_origin(filename, 0, 0, true); | 1171 | 1171 | ScriptCompiler::Source script_source(content, script_origin); | 1172 | 1172 |
| @@ -1483,7 +1483,7 @@ void ContextifyContext::CompileFunction( | 1483 | 1483 | Context::Scope scope(parsing_context); | 1484 | 1484 |
| 1485 | 1485 | // Read context extensions from buffer | 1486 | | - std::vector<Local<Object>> context_extensions; | | 1486 | + LocalVector<Object> context_extensions(isolate); | 1487 | 1487 | if (!context_extensions_buf.IsEmpty()) { | 1488 | 1488 | for (uint32_t n = 0; n < context_extensions_buf->Length(); n++) { | 1489 | 1489 | Local<Value> val; | @@ -1494,7 +1494,7 @@ void ContextifyContext::CompileFunction( | 1494 | 1494 | } | 1495 | 1495 |
| 1496 | 1496 | // Read params from params buffer | 1497 | | - std::vector<Local<String>> params; | | 1497 | + LocalVector<String> params(isolate); | 1498 | 1498 | if (!params_buf.IsEmpty()) { | 1499 | 1499 | for (uint32_t n = 0; n < params_buf->Length(); n++) { | 1500 | 1500 | Local<Value> val; | @@ -1526,22 +1526,24 @@ void ContextifyContext::CompileFunction( | 1526 | 1526 | args.GetReturnValue().Set(result); | 1527 | 1527 | } | 1528 | 1528 |
| 1529 | | -static std::vector<Local<String>> GetCJSParameters(IsolateData* data) { | 1530 | | - return { | 1531 | | - data->exports_string(), | 1532 | | - data->require_string(), | 1533 | | - data->module_string(), | 1534 | | - data->__filename_string(), | 1535 | | - data->__dirname_string(), | 1536 | | - }; | | 1529 | +static LocalVector<String> GetCJSParameters(IsolateData* data) { | | 1530 | + LocalVector<String> result(data->isolate(), | | 1531 | + { | | 1532 | + data->exports_string(), | | 1533 | + data->require_string(), | | 1534 | + data->module_string(), | | 1535 | + data->__filename_string(), | | 1536 | + data->__dirname_string(), | | 1537 | + }); | | 1538 | + return result; | 1537 | 1539 | } | 1538 | 1540 |
| 1539 | 1541 | Local<Object> ContextifyContext::CompileFunctionAndCacheResult( | 1540 | 1542 | Environment* env, | 1541 | 1543 | Local<Context> parsing_context, | 1542 | 1544 | ScriptCompiler::Source* source, | 1543 | | - std::vector<Local<String>> params, | 1544 | | - std::vector<Local<Object>> context_extensions, | | 1545 | + LocalVector<String> params, | | 1546 | + LocalVector<Object> context_extensions, | 1545 | 1547 | ScriptCompiler::CompileOptions options, | 1546 | 1548 | bool produce_cached_data, | 1547 | 1549 | Local<Symbol> id_symbol, | @@ -1677,7 +1679,7 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader( | 1677 | 1679 | options = ScriptCompiler::kConsumeCodeCache; | 1678 | 1680 | } | 1679 | 1681 |
| 1680 | | - std::vector<Local<String>> params; | | 1682 | + LocalVector<String> params(isolate); | 1681 | 1683 | if (is_cjs_scope) { | 1682 | 1684 | params = GetCJSParameters(env->isolate_data()); | 1683 | 1685 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/node_contextify.h +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -149,8 +149,8 @@ class ContextifyContext final : CPPGC_MIXIN(ContextifyContext) { | 149 | 149 | Environment* env, | 150 | 150 | v8::Local<v8::Context> parsing_context, | 151 | 151 | v8::ScriptCompiler::Source* source, | 152 | | - std::vector<v8::Local<v8::String>> params, | 153 | | - std::vector<v8::Local<v8::Object>> context_extensions, | | 152 | + v8::LocalVector<v8::String> params, | | 153 | + v8::LocalVector<v8::Object> context_extensions, | 154 | 154 | v8::ScriptCompiler::CompileOptions options, | 155 | 155 | bool produce_cached_data, | 156 | 156 | v8::Local<v8::Symbol> id_symbol, | @@ -244,7 +244,7 @@ v8::MaybeLocal<v8::Function> CompileFunction( | 244 | 244 | v8::Local<v8::Context> context, | 245 | 245 | v8::Local<v8::String> filename, | 246 | 246 | v8::Local<v8::String> content, | 247 | | - std::vector<v8::Local<v8::String>>* parameters); | | 247 | + v8::LocalVector<v8::String>* parameters); | 248 | 248 |
| 249 | 249 | } // namespace contextify | 250 | 250 | } // namespace node | |
Collapse file Copy file name to clipboardExpand all lines: src/node_sea.cc +10-7Lines changed: 10 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ using v8::FunctionCallbackInfo; | 36 | 36 | using v8::HandleScope; | 37 | 37 | using v8::Isolate; | 38 | 38 | using v8::Local; | | 39 | +using v8::LocalVector; | 39 | 40 | using v8::MaybeLocal; | 40 | 41 | using v8::NewStringType; | 41 | 42 | using v8::Object; | @@ -450,13 +451,15 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path, | 450 | 451 | return std::nullopt; | 451 | 452 | } | 452 | 453 |
| 453 | | - std::vector<Local<String>> parameters = { | 454 | | - FIXED_ONE_BYTE_STRING(isolate, "exports"), | 455 | | - FIXED_ONE_BYTE_STRING(isolate, "require"), | 456 | | - FIXED_ONE_BYTE_STRING(isolate, "module"), | 457 | | - FIXED_ONE_BYTE_STRING(isolate, "__filename"), | 458 | | - FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | 459 | | - }; | | 454 | + LocalVector<String> parameters( | | 455 | + isolate, | | 456 | + { | | 457 | + FIXED_ONE_BYTE_STRING(isolate, "exports"), | | 458 | + FIXED_ONE_BYTE_STRING(isolate, "require"), | | 459 | + FIXED_ONE_BYTE_STRING(isolate, "module"), | | 460 | + FIXED_ONE_BYTE_STRING(isolate, "__filename"), | | 461 | + FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | | 462 | + }); | 460 | 463 |
| 461 | 464 | // TODO(RaisinTen): Using the V8 code cache prevents us from using `import()` | 462 | 465 | // in the SEA code. Support it. | |
Collapse file Copy file name to clipboardExpand all lines: src/node_snapshotable.cc +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,7 @@ using v8::FunctionCallbackInfo; | 41 | 41 | using v8::HandleScope; | 42 | 42 | using v8::Isolate; | 43 | 43 | using v8::Local; | | 44 | +using v8::LocalVector; | 44 | 45 | using v8::Object; | 45 | 46 | using v8::ObjectTemplate; | 46 | 47 | using v8::SnapshotCreator; | @@ -1480,11 +1481,13 @@ void CompileSerializeMain(const FunctionCallbackInfo<Value>& args) { | 1480 | 1481 | Local<Context> context = isolate->GetCurrentContext(); | 1481 | 1482 | // TODO(joyeecheung): do we need all of these? Maybe we would want a less | 1482 | 1483 | // internal version of them. | 1483 | | - std::vector<Local<String>> parameters = { | 1484 | | - FIXED_ONE_BYTE_STRING(isolate, "require"), | 1485 | | - FIXED_ONE_BYTE_STRING(isolate, "__filename"), | 1486 | | - FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | 1487 | | - }; | | 1484 | + LocalVector<String> parameters( | | 1485 | + isolate, | | 1486 | + { | | 1487 | + FIXED_ONE_BYTE_STRING(isolate, "require"), | | 1488 | + FIXED_ONE_BYTE_STRING(isolate, "__filename"), | | 1489 | + FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | | 1490 | + }); | 1488 | 1491 | Local<Function> fn; | 1489 | 1492 | if (contextify::CompileFunction(context, filename, source, ¶meters) | 1490 | 1493 | .ToLocal(&fn)) { | |
|