设置每个进程状态


\Setting up a per-process state

Node.js 需要一些每个进程的状态管理才能运行:

\Node.js requires some per-process state management in order to run:

  • Node.js CLI 选项 的参数解析,

    \Arguments parsing for Node.js CLI options,

  • V8 每个进程要求,例如 v8::Platform 实例。

    \V8 per-process requirements, such as a v8::Platform instance.

下面的示例展示了如何设置这些。一些类名分别来自 nodev8 C++ 命名空间。

\The following example shows how these can be set up. Some class names are from the node and v8 C++ namespaces, respectively.

int main(int argc, char** argv) {
 argv = uv_setup_args(argc, argv);
 std::vector<std::string> args(argv, argv + argc);
 // Parse Node.js CLI options, and print any errors that have occurred while
 // trying to parse them.
 std::unique_ptr<node::InitializationResult> result =
 node::InitializeOncePerProcess(args, {
 node::ProcessInitializationFlags::kNoInitializeV8,
 node::ProcessInitializationFlags::kNoInitializeNodeV8Platform
 });
 for (const std::string& error : result->errors())
 fprintf(stderr, "%s: %s\n", args[0].c_str(), error.c_str());
 if (result->early_return() != 0) {
 return result->exit_code();
 }
 // Create a v8::Platform instance. `MultiIsolatePlatform::Create()` is a way
 // to create a v8::Platform instance that Node.js can use when creating
 // Worker threads. When no `MultiIsolatePlatform` instance is present,
 // Worker threads are disabled.
 std::unique_ptr<MultiIsolatePlatform> platform =
 MultiIsolatePlatform::Create(4);
 V8::InitializePlatform(platform.get());
 V8::Initialize();
 // See below for the contents of this function.
 int ret = RunNodeInstance(
 platform.get(), result->args(), result->exec_args());
 V8::Dispose();
 V8::DisposePlatform();
 node::TearDownOncePerProcess();
 return ret;
} 

AltStyle によって変換されたページ (->オリジナル) /