| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d1c6f86 commit 0f2cbc1
5 files changed
| 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. | |
@@ -1176,6 +1195,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) { | |||
| 1176 | 1195 | [`v8.h` in Code Search]: https://cs.chromium.org/chromium/src/v8/include/v8.h | |
| 1177 | 1196 | [`v8.h` in Node.js]: https://github.com/nodejs/node/blob/HEAD/deps/v8/include/v8.h | |
| 1178 | 1197 | [`v8.h` in V8]: https://github.com/v8/v8/blob/HEAD/include/v8.h | |
| 1198 | + [`v8::Local<T>`]: https://v8.github.io/api/head/classv8_1_1Local.html | ||
| 1179 | 1199 | [`vm` module]: https://nodejs.org/api/vm.html | |
| 1180 | 1200 | [binding function]: #binding-functions | |
| 1181 | 1201 | [cleanup hooks]: #cleanup-hooks | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1165,7 +1165,7 @@ Maybe<void> StoreCodeCacheResult( | |||
| 1165 | 1165 | MaybeLocal<Function> CompileFunction(Local<Context> context, | |
| 1166 | 1166 | Local<String> filename, | |
| 1167 | 1167 | Local<String> content, | |
| 1168 | - std::vector<Local<String>>* parameters) { | ||
| 1168 | + LocalVector<String>* parameters) { | ||
| 1169 | 1169 | ScriptOrigin script_origin(filename, 0, 0, true); | |
| 1170 | 1170 | ScriptCompiler::Source script_source(content, script_origin); | |
| 1171 | 1171 | ||
@@ -1474,7 +1474,7 @@ void ContextifyContext::CompileFunction( | |||
| 1474 | 1474 | Context::Scope scope(parsing_context); | |
| 1475 | 1475 | ||
| 1476 | 1476 | // Read context extensions from buffer | |
| 1477 | - std::vector<Local<Object>> context_extensions; | ||
| 1477 | + LocalVector<Object> context_extensions(isolate); | ||
| 1478 | 1478 | if (!context_extensions_buf.IsEmpty()) { | |
| 1479 | 1479 | for (uint32_t n = 0; n < context_extensions_buf->Length(); n++) { | |
| 1480 | 1480 | Local<Value> val; | |
@@ -1485,7 +1485,7 @@ void ContextifyContext::CompileFunction( | |||
| 1485 | 1485 | } | |
| 1486 | 1486 | ||
| 1487 | 1487 | // Read params from params buffer | |
| 1488 | - std::vector<Local<String>> params; | ||
| 1488 | + LocalVector<String> params(isolate); | ||
| 1489 | 1489 | if (!params_buf.IsEmpty()) { | |
| 1490 | 1490 | for (uint32_t n = 0; n < params_buf->Length(); n++) { | |
| 1491 | 1491 | Local<Value> val; | |
@@ -1517,22 +1517,24 @@ void ContextifyContext::CompileFunction( | |||
| 1517 | 1517 | args.GetReturnValue().Set(result); | |
| 1518 | 1518 | } | |
| 1519 | 1519 | ||
| 1520 | - static std::vector<Local<String>> GetCJSParameters(IsolateData* data) { | ||
| 1521 | - return { | ||
| 1522 | - data->exports_string(), | ||
| 1523 | - data->require_string(), | ||
| 1524 | - data->module_string(), | ||
| 1525 | - data->__filename_string(), | ||
| 1526 | - data->__dirname_string(), | ||
| 1527 | - }; | ||
| 1520 | + static LocalVector<String> GetCJSParameters(IsolateData* data) { | ||
| 1521 | + LocalVector<String> result(data->isolate(), | ||
| 1522 | + { | ||
| 1523 | + data->exports_string(), | ||
| 1524 | + data->require_string(), | ||
| 1525 | + data->module_string(), | ||
| 1526 | + data->__filename_string(), | ||
| 1527 | + data->__dirname_string(), | ||
| 1528 | + }); | ||
| 1529 | + return result; | ||
| 1528 | 1530 | } | |
| 1529 | 1531 | ||
| 1530 | 1532 | Local<Object> ContextifyContext::CompileFunctionAndCacheResult( | |
| 1531 | 1533 | Environment* env, | |
| 1532 | 1534 | Local<Context> parsing_context, | |
| 1533 | 1535 | ScriptCompiler::Source* source, | |
| 1534 | - std::vector<Local<String>> params, | ||
| 1535 | - std::vector<Local<Object>> context_extensions, | ||
| 1536 | + LocalVector<String> params, | ||
| 1537 | + LocalVector<Object> context_extensions, | ||
| 1536 | 1538 | ScriptCompiler::CompileOptions options, | |
| 1537 | 1539 | bool produce_cached_data, | |
| 1538 | 1540 | Local<Symbol> id_symbol, | |
@@ -1668,7 +1670,7 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader( | |||
| 1668 | 1670 | options = ScriptCompiler::kConsumeCodeCache; | |
| 1669 | 1671 | } | |
| 1670 | 1672 | ||
| 1671 | - std::vector<Local<String>> params; | ||
| 1673 | + LocalVector<String> params(isolate); | ||
| 1672 | 1674 | if (is_cjs_scope) { | |
| 1673 | 1675 | params = GetCJSParameters(env->isolate_data()); | |
| 1674 | 1676 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -92,8 +92,8 @@ class ContextifyContext : public BaseObject { | |||
| 92 | 92 | Environment* env, | |
| 93 | 93 | v8::Local<v8::Context> parsing_context, | |
| 94 | 94 | v8::ScriptCompiler::Source* source, | |
| 95 | - std::vector<v8::Local<v8::String>> params, | ||
| 96 | - std::vector<v8::Local<v8::Object>> context_extensions, | ||
| 95 | + v8::LocalVector<v8::String> params, | ||
| 96 | + v8::LocalVector<v8::Object> context_extensions, | ||
| 97 | 97 | v8::ScriptCompiler::CompileOptions options, | |
| 98 | 98 | bool produce_cached_data, | |
| 99 | 99 | v8::Local<v8::Symbol> id_symbol, | |
@@ -189,7 +189,7 @@ v8::MaybeLocal<v8::Function> CompileFunction( | |||
| 189 | 189 | v8::Local<v8::Context> context, | |
| 190 | 190 | v8::Local<v8::String> filename, | |
| 191 | 191 | v8::Local<v8::String> content, | |
| 192 | - std::vector<v8::Local<v8::String>>* parameters); | ||
| 192 | + v8::LocalVector<v8::String>* parameters); | ||
| 193 | 193 | ||
| 194 | 194 | } // namespace contextify | |
| 195 | 195 | } // namespace node | |
| 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. | |
| 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; | |
@@ -1479,11 +1480,13 @@ void CompileSerializeMain(const FunctionCallbackInfo<Value>& args) { | |||
| 1479 | 1480 | Local<Context> context = isolate->GetCurrentContext(); | |
| 1480 | 1481 | // TODO(joyeecheung): do we need all of these? Maybe we would want a less | |
| 1481 | 1482 | // internal version of them. | |
| 1482 | - std::vector<Local<String>> parameters = { | ||
| 1483 | - FIXED_ONE_BYTE_STRING(isolate, "require"), | ||
| 1484 | - FIXED_ONE_BYTE_STRING(isolate, "__filename"), | ||
| 1485 | - FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | ||
| 1486 | - }; | ||
| 1483 | + LocalVector<String> parameters( | ||
| 1484 | + isolate, | ||
| 1485 | + { | ||
| 1486 | + FIXED_ONE_BYTE_STRING(isolate, "require"), | ||
| 1487 | + FIXED_ONE_BYTE_STRING(isolate, "__filename"), | ||
| 1488 | + FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | ||
| 1489 | + }); | ||
| 1487 | 1490 | Local<Function> fn; | |
| 1488 | 1491 | if (contextify::CompileFunction(context, filename, source, ¶meters) | |
| 1489 | 1492 | .ToLocal(&fn)) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments