Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPL: Load and write history to file #49

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tools/repl/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/SmallString.h"

#include "hermes/CompilerDriver/CompilerDriver.h"
#include "hermes/ConsoleHost/ConsoleHost.h"
Expand Down Expand Up @@ -42,6 +45,9 @@

#define C_STRING(x) #x

static const std::string historyFileBaseName = ".hermes_history";
static const int historyMaxEntries = 500;

using namespace hermes;

static llvm::cl::opt<std::string> PromptString(
Expand Down Expand Up @@ -222,6 +228,26 @@ static bool needsAnotherLine(llvm::StringRef input) {
return !stack.empty();
}

#if HAVE_LIBREADLINE
// Load history file or create it
std::error_code loadHistoryFile(llvm::SmallString<128>& historyFile) {
if (!llvm::sys::path::home_directory(historyFile)) {
// Use ENOENT here since it could not found a home directory
return std::error_code(ENOENT, std::system_category());
}

llvm::sys::path::append(historyFile, historyFileBaseName);

auto err = ::read_history(historyFile.c_str());
if (err != 0) {
// Return a error_code object from a errno enum
return std::error_code(err, std::system_category());
}

return std::error_code();
}
#endif

// This is the vm driver.
int main(int argc, char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal("Hermes REPL");
Expand Down Expand Up @@ -288,6 +314,14 @@ int main(int argc, char **argv) {

runtime->getHeap().runtimeWillExecute();

#if HAVE_LIBREADLINE
llvm::SmallString<128> historyFile{};
auto historyErr = loadHistoryFile(historyFile);
if (historyErr && historyErr.value() != ENOENT) {
llvm::errs() << "Could not load history file: " << historyErr.message() << '\n';
}
#endif

// SetUnbuffered because there is no explicit flush after prompt (>>).
// There is also no explicitly flush at end of line. (An automatic flush
// mechanism is not guaranteed to be present, from my experiment on Windows)
Expand All @@ -300,6 +334,12 @@ int main(int argc, char **argv) {
(readResult == ReadResult::INTERRUPT && code.empty())) {
// EOF or user exit on non-continuation line.
llvm::outs() << '\n';
#if HAVE_LIBREADLINE
if (history_length > 0) {
::stifle_history(historyMaxEntries);
::write_history(historyFile.c_str());
}
#endif
return 0;
}

Expand Down