Skip to content

Commit

Permalink
src: rewrite task runner in c++
Browse files Browse the repository at this point in the history
PR-URL: nodejs#52609
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Daniel Lemire <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
  • Loading branch information
anonrig authored May 2, 2024
1 parent 905ab3e commit c5cfdd4
Show file tree
Hide file tree
Showing 15 changed files with 404 additions and 189 deletions.
74 changes: 0 additions & 74 deletions lib/internal/main/run.js

This file was deleted.

37 changes: 0 additions & 37 deletions lib/internal/shell.js

This file was deleted.

2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
'src/node_stat_watcher.cc',
'src/node_symbols.cc',
'src/node_task_queue.cc',
'src/node_task_runner.cc',
'src/node_trace_events.cc',
'src/node_types.cc',
'src/node_url.cc',
Expand Down Expand Up @@ -397,6 +398,7 @@
'test/cctest/test_base_object_ptr.cc',
'test/cctest/test_cppgc.cc',
'test/cctest/test_node_postmortem_metadata.cc',
'test/cctest/test_node_task_runner.cc',
'test/cctest/test_environment.cc',
'test/cctest/test_linked_binding.cc',
'test/cctest/test_node_api.cc',
Expand Down
29 changes: 21 additions & 8 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "node.h"
#include "node_dotenv.h"
#include "node_task_runner.h"

// ========== local headers ==========

Expand Down Expand Up @@ -360,10 +361,6 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
return StartExecution(env, "internal/main/watch_mode");
}

if (!env->options()->run.empty()) {
return StartExecution(env, "internal/main/run");
}

if (!first_argv.empty() && first_argv != "-") {
return StartExecution(env, "internal/main/run_main_module");
}
Expand Down Expand Up @@ -976,11 +973,11 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
InitializeNodeWithArgsInternal(argv, exec_argv, errors, flags));
}

static std::unique_ptr<InitializationResultImpl>
static std::shared_ptr<InitializationResultImpl>
InitializeOncePerProcessInternal(const std::vector<std::string>& args,
ProcessInitializationFlags::Flags flags =
ProcessInitializationFlags::kNoFlags) {
auto result = std::make_unique<InitializationResultImpl>();
auto result = std::make_shared<InitializationResultImpl>();
result->args_ = args;

if (!(flags & ProcessInitializationFlags::kNoParseGlobalDebugVariables)) {
Expand Down Expand Up @@ -1010,6 +1007,22 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
}
}

if (!per_process::cli_options->run.empty()) {
// TODO(@anonrig): Handle NODE_NO_WARNINGS, NODE_REDIRECT_WARNINGS,
// --disable-warning and --redirect-warnings.
if (per_process::cli_options->per_isolate->per_env->warnings) {
fprintf(stderr,
"ExperimentalWarning: Task runner is an experimental feature and "
"might change at any time\n\n");
}

auto positional_args = task_runner::GetPositionalArgs(args);
result->early_return_ = true;
task_runner::RunTask(
result, per_process::cli_options->run, positional_args);
return result;
}

if (!(flags & ProcessInitializationFlags::kNoPrintHelpOrVersionOutput)) {
if (per_process::cli_options->print_version) {
printf("%s\n", NODE_VERSION);
Expand Down Expand Up @@ -1169,7 +1182,7 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
return result;
}

std::unique_ptr<InitializationResult> InitializeOncePerProcess(
std::shared_ptr<InitializationResult> InitializeOncePerProcess(
const std::vector<std::string>& args,
ProcessInitializationFlags::Flags flags) {
return InitializeOncePerProcessInternal(args, flags);
Expand Down Expand Up @@ -1375,7 +1388,7 @@ static ExitCode StartInternal(int argc, char** argv) {
// Hack around with the argv pointer. Used for process.title = "blah".
argv = uv_setup_args(argc, argv);

std::unique_ptr<InitializationResultImpl> result =
std::shared_ptr<InitializationResultImpl> result =
InitializeOncePerProcessInternal(
std::vector<std::string>(argv, argv + argc));
for (const std::string& error : result->errors()) {
Expand Down
4 changes: 2 additions & 2 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ NODE_DEPRECATED("Use InitializeOncePerProcess() instead",
// including the arguments split into argv/exec_argv, a list of potential
// errors encountered during initialization, and a potential suggested
// exit code.
NODE_EXTERN std::unique_ptr<InitializationResult> InitializeOncePerProcess(
NODE_EXTERN std::shared_ptr<InitializationResult> InitializeOncePerProcess(
const std::vector<std::string>& args,
ProcessInitializationFlags::Flags flags =
ProcessInitializationFlags::kNoFlags);
Expand All @@ -358,7 +358,7 @@ NODE_EXTERN std::unique_ptr<InitializationResult> InitializeOncePerProcess(
NODE_EXTERN void TearDownOncePerProcess();
// Convenience overload for specifying multiple flags without having
// to worry about casts.
inline std::unique_ptr<InitializationResult> InitializeOncePerProcess(
inline std::shared_ptr<InitializationResult> InitializeOncePerProcess(
const std::vector<std::string>& args,
std::initializer_list<ProcessInitializationFlags::Flags> list) {
uint64_t flags_accum = ProcessInitializationFlags::kNoFlags;
Expand Down
24 changes: 0 additions & 24 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,28 +367,6 @@ void BindingData::GetNearestParentPackageJSONType(
args.GetReturnValue().Set(Array::New(realm->isolate(), values, 3));
}

void BindingData::GetPackageJSONScripts(
const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
std::string_view path = "package.json";

THROW_IF_INSUFFICIENT_PERMISSIONS(
realm->env(), permission::PermissionScope::kFileSystemRead, path);

auto package_json = GetPackageJSON(realm, path);
if (package_json == nullptr) {
printf("Can't read package.json\n");
return;
} else if (!package_json->scripts.has_value()) {
printf("Can't read package.json \"scripts\" object\n");
return;
}

args.GetReturnValue().Set(
ToV8Value(realm->context(), package_json->scripts.value())
.ToLocalChecked());
}

void BindingData::GetPackageScopeConfig(
const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
Expand Down Expand Up @@ -469,7 +447,6 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
"getNearestParentPackageJSON",
GetNearestParentPackageJSON);
SetMethod(isolate, target, "getPackageScopeConfig", GetPackageScopeConfig);
SetMethod(isolate, target, "getPackageJSONScripts", GetPackageJSONScripts);
}

void BindingData::CreatePerContextProperties(Local<Object> target,
Expand All @@ -486,7 +463,6 @@ void BindingData::RegisterExternalReferences(
registry->Register(GetNearestParentPackageJSONType);
registry->Register(GetNearestParentPackageJSON);
registry->Register(GetPackageScopeConfig);
registry->Register(GetPackageJSONScripts);
}

} // namespace modules
Expand Down
9 changes: 5 additions & 4 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,7 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
"process V8 profiler output generated using --prof",
&EnvironmentOptions::prof_process);
// Options after --prof-process are passed through to the prof processor.
AddAlias("--prof-process", { "--prof-process", "--" });
AddOption("--run",
"Run a script specified in package.json",
&EnvironmentOptions::run);
AddAlias("--prof-process", {"--prof-process", "--"});
#if HAVE_INSPECTOR
AddOption("--cpu-prof",
"Start the V8 CPU profiler on start up, and write the CPU profile "
Expand Down Expand Up @@ -1069,6 +1066,10 @@ PerProcessOptionsParser::PerProcessOptionsParser(
"Generate a blob that can be embedded into the single executable "
"application",
&PerProcessOptions::experimental_sea_config);

AddOption("--run",
"Run a script specified in package.json",
&PerProcessOptions::run);
}

inline std::string RemoveBrackets(const std::string& host) {
Expand Down
2 changes: 1 addition & 1 deletion src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ class EnvironmentOptions : public Options {
bool heap_prof = false;
#endif // HAVE_INSPECTOR
std::string redirect_warnings;
std::string run;
std::string diagnostic_dir;
std::string env_file;
bool has_env_file_string = false;
Expand Down Expand Up @@ -280,6 +279,7 @@ class PerProcessOptions : public Options {
bool print_v8_help = false;
bool print_version = false;
std::string experimental_sea_config;
std::string run;

#ifdef NODE_HAVE_I18N_SUPPORT
std::string icu_data_dir;
Expand Down
Loading

0 comments on commit c5cfdd4

Please sign in to comment.