Skip to content

Commit

Permalink
src: use more LocalVetor
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jan 2, 2025
1 parent 935a472 commit cf7258c
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 76 deletions.
66 changes: 36 additions & 30 deletions src/node_builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void BuiltinLoader::AddExternalizedBuiltin(const char* id,
MaybeLocal<Function> BuiltinLoader::LookupAndCompileInternal(
Local<Context> context,
const char* id,
std::vector<Local<String>>* parameters,
const std::span<Local<String>>& parameters,
Realm* optional_realm) {
Isolate* isolate = context->GetIsolate();
EscapableHandleScope scope(isolate);
Expand Down Expand Up @@ -318,8 +318,8 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompileInternal(
MaybeLocal<Function> maybe_fun =
ScriptCompiler::CompileFunction(context,
&script_source,
parameters->size(),
parameters->data(),
parameters.size(),
parameters.data(),
0,
nullptr,
options);
Expand Down Expand Up @@ -388,53 +388,59 @@ void BuiltinLoader::SaveCodeCache(const char* id, Local<Function> fun) {
MaybeLocal<Function> BuiltinLoader::LookupAndCompile(Local<Context> context,
const char* id,
Realm* optional_realm) {
std::vector<Local<String>> parameters;
Isolate* isolate = context->GetIsolate();
// Detects parameters of the scripts based on module ids.
// internal/bootstrap/realm: process, getLinkedBinding,
// getInternalBinding, primordials
if (strcmp(id, "internal/bootstrap/realm") == 0) {
parameters = {
Local<String> parameters[] = {
FIXED_ONE_BYTE_STRING(isolate, "process"),
FIXED_ONE_BYTE_STRING(isolate, "getLinkedBinding"),
FIXED_ONE_BYTE_STRING(isolate, "getInternalBinding"),
FIXED_ONE_BYTE_STRING(isolate, "primordials"),
};
} else if (strncmp(id,
"internal/per_context/",
strlen("internal/per_context/")) == 0) {
std::span<Local<String>> span(&parameters[0], arraysize(parameters));
return LookupAndCompileInternal(context, id, span, optional_realm);
}

if (strncmp(id, "internal/per_context/",
strlen("internal/per_context/")) == 0) {
// internal/per_context/*: global, exports, primordials
parameters = {
Local<String> parameters[] = {
FIXED_ONE_BYTE_STRING(isolate, "exports"),
FIXED_ONE_BYTE_STRING(isolate, "primordials"),
};
} else if (strncmp(id, "internal/main/", strlen("internal/main/")) == 0 ||
strncmp(id,
"internal/bootstrap/",
strlen("internal/bootstrap/")) == 0) {
std::span<Local<String>> span(&parameters[0], arraysize(parameters));
return LookupAndCompileInternal(context, id, span, optional_realm);
}

if (strncmp(id, "internal/main/", strlen("internal/main/")) == 0 ||
strncmp(id,
"internal/bootstrap/",
strlen("internal/bootstrap/")) == 0) {
// internal/main/*, internal/bootstrap/*: process, require,
// internalBinding, primordials
parameters = {
Local<String> parameters[] = {
FIXED_ONE_BYTE_STRING(isolate, "process"),
FIXED_ONE_BYTE_STRING(isolate, "require"),
FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
FIXED_ONE_BYTE_STRING(isolate, "primordials"),
};
} else {
// others: exports, require, module, process, internalBinding, primordials
parameters = {
FIXED_ONE_BYTE_STRING(isolate, "exports"),
FIXED_ONE_BYTE_STRING(isolate, "require"),
FIXED_ONE_BYTE_STRING(isolate, "module"),
FIXED_ONE_BYTE_STRING(isolate, "process"),
FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
FIXED_ONE_BYTE_STRING(isolate, "primordials"),
};
}

MaybeLocal<Function> maybe =
LookupAndCompileInternal(context, id, &parameters, optional_realm);
return maybe;
std::span<Local<String>> span(&parameters[0], arraysize(parameters));
return LookupAndCompileInternal(context, id, span, optional_realm);
}

// others: exports, require, module, process, internalBinding, primordials
Local<String> parameters[] = {
FIXED_ONE_BYTE_STRING(isolate, "exports"),
FIXED_ONE_BYTE_STRING(isolate, "require"),
FIXED_ONE_BYTE_STRING(isolate, "module"),
FIXED_ONE_BYTE_STRING(isolate, "process"),
FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
FIXED_ONE_BYTE_STRING(isolate, "primordials"),
};
std::span<Local<String>> span(&parameters[0], arraysize(parameters));
return LookupAndCompileInternal(context, id, span, optional_realm);
}

MaybeLocal<Value> BuiltinLoader::CompileAndCall(Local<Context> context,
Expand Down Expand Up @@ -503,7 +509,7 @@ MaybeLocal<Value> BuiltinLoader::CompileAndCall(Local<Context> context,
MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
Local<Context> context,
const char* id,
std::vector<Local<String>>* parameters,
const std::span<Local<String>>& parameters,
Realm* optional_realm) {
return LookupAndCompileInternal(context, id, parameters, optional_realm);
}
Expand Down
5 changes: 3 additions & 2 deletions src/node_builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <map>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -100,7 +101,7 @@ class NODE_EXTERN_PRIVATE BuiltinLoader {
v8::MaybeLocal<v8::Function> LookupAndCompile(
v8::Local<v8::Context> context,
const char* id,
std::vector<v8::Local<v8::String>>* parameters,
const std::span<v8::Local<v8::String>>& parameters,
Realm* optional_realm);

v8::MaybeLocal<v8::Value> CompileAndCall(v8::Local<v8::Context> context,
Expand Down Expand Up @@ -156,7 +157,7 @@ class NODE_EXTERN_PRIVATE BuiltinLoader {
v8::MaybeLocal<v8::Function> LookupAndCompileInternal(
v8::Local<v8::Context> context,
const char* id,
std::vector<v8::Local<v8::String>>* parameters,
const std::span<v8::Local<v8::String>>& parameters,
Realm* optional_realm);
void SaveCodeCache(const char* id, v8::Local<v8::Function> fn);

Expand Down
47 changes: 25 additions & 22 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1154,17 +1154,18 @@ Maybe<void> StoreCodeCacheResult(
}

// TODO(RaisinTen): Reuse in ContextifyContext::CompileFunction().
MaybeLocal<Function> CompileFunction(Local<Context> context,
Local<String> filename,
Local<String> content,
std::vector<Local<String>>* parameters) {
MaybeLocal<Function> CompileFunction(
Local<Context> context,
Local<String> filename,
Local<String> content,
const std::span<Local<String>>& parameters) {
ScriptOrigin script_origin(filename, 0, 0, true);
ScriptCompiler::Source script_source(content, script_origin);

return ScriptCompiler::CompileFunction(context,
&script_source,
parameters->size(),
parameters->data(),
parameters.size(),
parameters.data(),
0,
nullptr);
}
Expand Down Expand Up @@ -1466,7 +1467,7 @@ void ContextifyContext::CompileFunction(
Context::Scope scope(parsing_context);

// Read context extensions from buffer
std::vector<Local<Object>> context_extensions;
LocalVector<Object> context_extensions(isolate);
if (!context_extensions_buf.IsEmpty()) {
for (uint32_t n = 0; n < context_extensions_buf->Length(); n++) {
Local<Value> val;
Expand All @@ -1477,7 +1478,7 @@ void ContextifyContext::CompileFunction(
}

// Read params from params buffer
std::vector<Local<String>> params;
LocalVector<String> params(isolate);
if (!params_buf.IsEmpty()) {
for (uint32_t n = 0; n < params_buf->Length(); n++) {
Local<Value> val;
Expand All @@ -1488,11 +1489,14 @@ void ContextifyContext::CompileFunction(
}

TryCatchScope try_catch(env);
std::span<Local<String>> parms(params.data(), params.size());
std::span<Local<Object>> exts(context_extensions.data(),
context_extensions.size());
Local<Object> result = CompileFunctionAndCacheResult(env,
parsing_context,
&source,
params,
context_extensions,
parms,
exts,
options,
produce_cached_data,
id_symbol,
Expand All @@ -1509,22 +1513,21 @@ void ContextifyContext::CompileFunction(
args.GetReturnValue().Set(result);
}

static std::vector<Local<String>> GetCJSParameters(IsolateData* data) {
return {
data->exports_string(),
data->require_string(),
data->module_string(),
data->__filename_string(),
data->__dirname_string(),
};
static void GetCJSParameters(LocalVector<String>* vec, IsolateData* data) {
vec->reserve(5);
vec->push_back(data->exports_string());
vec->push_back(data->require_string());
vec->push_back(data->module_string());
vec->push_back(data->__filename_string());
vec->push_back(data->__dirname_string());
}

Local<Object> ContextifyContext::CompileFunctionAndCacheResult(
Environment* env,
Local<Context> parsing_context,
ScriptCompiler::Source* source,
std::vector<Local<String>> params,
std::vector<Local<Object>> context_extensions,
const std::span<Local<String>>& params,
const std::span<Local<Object>>& context_extensions,
ScriptCompiler::CompileOptions options,
bool produce_cached_data,
Local<Symbol> id_symbol,
Expand Down Expand Up @@ -1660,9 +1663,9 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(
options = ScriptCompiler::kConsumeCodeCache;
}

std::vector<Local<String>> params;
LocalVector<String> params(env->isolate());
if (is_cjs_scope) {
params = GetCJSParameters(env->isolate_data());
GetCJSParameters(&params, env->isolate_data());
}
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunction(
context,
Expand Down
6 changes: 3 additions & 3 deletions src/node_contextify.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class ContextifyContext : public BaseObject {
Environment* env,
v8::Local<v8::Context> parsing_context,
v8::ScriptCompiler::Source* source,
std::vector<v8::Local<v8::String>> params,
std::vector<v8::Local<v8::Object>> context_extensions,
const std::span<v8::Local<v8::String>>& params,
const std::span<v8::Local<v8::Object>>& context_extensions,
v8::ScriptCompiler::CompileOptions options,
bool produce_cached_data,
v8::Local<v8::Symbol> id_symbol,
Expand Down Expand Up @@ -188,7 +188,7 @@ v8::MaybeLocal<v8::Function> CompileFunction(
v8::Local<v8::Context> context,
v8::Local<v8::String> filename,
v8::Local<v8::String> content,
std::vector<v8::Local<v8::String>>* parameters);
const std::span<v8::Local<v8::String>>& parameters);

} // namespace contextify
} // namespace node
Expand Down
17 changes: 11 additions & 6 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "node_messaging.h"

#include <span>
#include "async_wrap-inl.h"
#include "debug_utils-inl.h"
#include "memory_tracker-inl.h"
Expand Down Expand Up @@ -27,6 +28,7 @@ using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::LocalVector;
using v8::Maybe;
using v8::MaybeLocal;
using v8::Nothing;
Expand Down Expand Up @@ -74,7 +76,7 @@ class DeserializerDelegate : public ValueDeserializer::Delegate {
Message* m,
Environment* env,
const std::vector<BaseObjectPtr<BaseObject>>& host_objects,
const std::vector<Local<SharedArrayBuffer>>& shared_array_buffers,
const std::span<Local<SharedArrayBuffer>>& shared_array_buffers,
const std::vector<CompiledWasmModule>& wasm_modules,
const std::optional<SharedValueConveyor>& shared_value_conveyor)
: env_(env),
Expand Down Expand Up @@ -129,7 +131,7 @@ class DeserializerDelegate : public ValueDeserializer::Delegate {
private:
Environment* env_;
const std::vector<BaseObjectPtr<BaseObject>>& host_objects_;
const std::vector<Local<SharedArrayBuffer>>& shared_array_buffers_;
const std::span<Local<SharedArrayBuffer>>& shared_array_buffers_;
const std::vector<CompiledWasmModule>& wasm_modules_;
const std::optional<SharedValueConveyor>& shared_value_conveyor_;
};
Expand Down Expand Up @@ -188,18 +190,21 @@ MaybeLocal<Value> Message::Deserialize(Environment* env,
}
transferables_.clear();

std::vector<Local<SharedArrayBuffer>> shared_array_buffers;
LocalVector<SharedArrayBuffer> shared_array_buffers(env->isolate());
// Attach all transferred SharedArrayBuffers to their new Isolate.
for (uint32_t i = 0; i < shared_array_buffers_.size(); ++i) {
Local<SharedArrayBuffer> sab =
SharedArrayBuffer::New(env->isolate(), shared_array_buffers_[i]);
shared_array_buffers.push_back(sab);
}
std::span<Local<SharedArrayBuffer>> shared_bufs(
shared_array_buffers.data(),
shared_array_buffers.size());

DeserializerDelegate delegate(this,
env,
host_objects,
shared_array_buffers,
shared_bufs,
wasm_modules_,
shared_value_conveyor_);
ValueDeserializer deserializer(
Expand Down Expand Up @@ -477,7 +482,7 @@ Maybe<bool> Message::Serialize(Environment* env,
ValueSerializer serializer(env->isolate(), &delegate);
delegate.serializer = &serializer;

std::vector<Local<ArrayBuffer>> array_buffers;
LocalVector<ArrayBuffer> array_buffers(env->isolate());
for (uint32_t i = 0; i < transfer_list_v.length(); ++i) {
Local<Value> entry_val = transfer_list_v[i];
if (!entry_val->IsObject()) {
Expand Down Expand Up @@ -999,7 +1004,7 @@ static Maybe<bool> ReadIterable(Environment* env,
return Nothing<bool>();
if (!next->IsFunction()) return Just(false);

std::vector<Local<Value>> entries;
LocalVector<Value> entries(isolate);
while (env->can_call_into_js()) {
Local<Value> result;
if (!next.As<Function>()->Call(context, iterator, 0, nullptr)
Expand Down
Loading

0 comments on commit cf7258c

Please sign in to comment.