Files
node/src/node_snapshot_builder.h
Joyee Cheung 718f62bfcf bootstrap: unify snapshot builder and embedder entry points
- Run the embedder entry point directly through
  runEmbedderEntryPoint(), instead of going through another
  JS -> C++ trip through the function returned by
  getEmbedderEntryFunction()
- For --build-snapshot, read the snapshot script code directly in C++
  and pass it to SnapshotBuilder::Generate(), this makes the entry point
  more explicit instead of hiding it in JS land, and also makes it
  possible to invoke SnapshotBuilder::Generate() internally to create
  a custom snapshot.
- Previously we used process.execPath for the embedder to create
  __filename and __dirname in the snapshot builder script while using
  process.argv[1] for --build-snapshot (where it's always set) which
  results in inconsistencies. We now require the embedder to also set
  args[1] when creating the Environment if they intend to run snapshot
  scripts with a context that contains __filename and __dirname, which
  would be derived from args[1]. If they prefer not to include
  build-time paths in the snapshot, we now provide
  node::GetAnonymousMainPath() as an alternative.

PR-URL: https://github.com/nodejs/node/pull/48242
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-06-11 00:34:19 +02:00

53 lines
1.6 KiB
C++

#ifndef SRC_NODE_SNAPSHOT_BUILDER_H_
#define SRC_NODE_SNAPSHOT_BUILDER_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include <cstdint>
#include <optional>
#include <string_view>
#include "node_exit_code.h"
#include "node_mutex.h"
#include "v8.h"
namespace node {
class ExternalReferenceRegistry;
struct SnapshotData;
class NODE_EXTERN_PRIVATE SnapshotBuilder {
public:
static ExitCode Generate(std::ostream& out,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
std::optional<std::string_view> main_script);
// Generate the snapshot into out.
static ExitCode Generate(SnapshotData* out,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
std::optional<std::string_view> main_script);
// If nullptr is returned, the binary is not built with embedded
// snapshot.
static const SnapshotData* GetEmbeddedSnapshotData();
static void InitializeIsolateParams(const SnapshotData* data,
v8::Isolate::CreateParams* params);
static const std::vector<intptr_t>& CollectExternalReferences();
static ExitCode CreateSnapshot(
SnapshotData* out,
CommonEnvironmentSetup* setup,
/*SnapshotMetadata::Type*/ uint8_t snapshot_type);
private:
static std::unique_ptr<ExternalReferenceRegistry> registry_;
};
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_NODE_SNAPSHOT_BUILDER_H_