| Index: src/d8-readline.cc
|
| diff --git a/src/d8-readline.cc b/src/d8-readline.cc
|
| index 89892631f76ca0a9811c83038dbfed39d6789f3a..c64afbd8f502b7c0e585444a141b5e5890b93b9a 100644
|
| --- a/src/d8-readline.cc
|
| +++ b/src/d8-readline.cc
|
| @@ -25,8 +25,9 @@
|
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
| +#ifdef USE_READLINE_CONSOLE
|
| +
|
| #include <cstdio> // NOLINT
|
| -#include <string.h> // NOLINT
|
| #include <readline/readline.h> // NOLINT
|
| #include <readline/history.h> // NOLINT
|
|
|
| @@ -35,6 +36,7 @@
|
|
|
| #include "d8.h"
|
|
|
| +
|
| // There are incompatibilities between different versions and different
|
| // implementations of readline. This smooths out one known incompatibility.
|
| #if RL_READLINE_VERSION >= 0x0500
|
| @@ -44,77 +46,70 @@
|
|
|
| namespace v8 {
|
|
|
| +static char kWordBreakCharacters[] = {' ', '\t', '\n', '"',
|
| + '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(',
|
| + '\0'};
|
|
|
| -class ReadLineEditor: public LineEditor {
|
| - public:
|
| - ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { }
|
| - virtual Handle<String> Prompt(const char* prompt);
|
| - virtual bool Open();
|
| - virtual bool Close();
|
| - virtual void AddHistory(const char* str);
|
| -
|
| - static const char* kHistoryFileName;
|
| - static const int kMaxHistoryEntries;
|
|
|
| - private:
|
| -#ifndef V8_SHARED
|
| - static char** AttemptedCompletion(const char* text, int start, int end);
|
| - static char* CompletionGenerator(const char* text, int state);
|
| -#endif // V8_SHARED
|
| - static char kWordBreakCharacters[];
|
| -};
|
| +static const char* kHistoryFileName = ".d8_history";
|
| +static const int kMaxHistoryEntries = 1000;
|
|
|
|
|
| -static ReadLineEditor read_line_editor;
|
| -char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"',
|
| - '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(',
|
| - '\0'};
|
| +char* CompletionGenerator(const char* text, int state) {
|
| + static unsigned current_index;
|
| + static Persistent<Array> current_completions;
|
| + if (state == 0) {
|
| + HandleScope scope;
|
| + Local<String> full_text = String::New(rl_line_buffer, rl_point);
|
| + Handle<Array> completions =
|
| + Shell::GetCompletions(String::New(text), full_text);
|
| + current_completions = Persistent<Array>::New(completions);
|
| + current_index = 0;
|
| + }
|
| + if (current_index < current_completions->Length()) {
|
| + HandleScope scope;
|
| + Handle<Integer> index = Integer::New(current_index);
|
| + Handle<Value> str_obj = current_completions->Get(index);
|
| + current_index++;
|
| + String::Utf8Value str(str_obj);
|
| + return strdup(*str);
|
| + } else {
|
| + current_completions.Dispose();
|
| + current_completions.Clear();
|
| + return NULL;
|
| + }
|
| +}
|
|
|
|
|
| -const char* ReadLineEditor::kHistoryFileName = ".d8_history";
|
| -const int ReadLineEditor::kMaxHistoryEntries = 1000;
|
| +char** AttemptedCompletion(const char* text,
|
| + int start,
|
| + int end) {
|
| + char** result = completion_matches(text, CompletionGenerator);
|
| + rl_attempted_completion_over = true;
|
| + return result;
|
| +}
|
|
|
|
|
| -bool ReadLineEditor::Open() {
|
| +void LineEditor::Open() {
|
| rl_initialize();
|
| -
|
| -#ifdef V8_SHARED
|
| - // Don't do completion on shared library mode
|
| - // http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC24
|
| - rl_bind_key('\t', rl_insert);
|
| -#else
|
| rl_attempted_completion_function = AttemptedCompletion;
|
| -#endif // V8_SHARED
|
| -
|
| rl_completer_word_break_characters = kWordBreakCharacters;
|
| rl_bind_key('\t', rl_complete);
|
| using_history();
|
| stifle_history(kMaxHistoryEntries);
|
| - return read_history(kHistoryFileName) == 0;
|
| + read_history(kHistoryFileName);
|
| }
|
|
|
|
|
| -bool ReadLineEditor::Close() {
|
| - return write_history(kHistoryFileName) == 0;
|
| +LineEditor::~LineEditor() {
|
| + write_history(kHistoryFileName);
|
| }
|
|
|
|
|
| -Handle<String> ReadLineEditor::Prompt(const char* prompt) {
|
| - char* result = NULL;
|
| - { // Release lock for blocking input.
|
| - Unlocker unlock(Isolate::GetCurrent());
|
| - result = readline(prompt);
|
| - }
|
| - if (result != NULL) {
|
| - AddHistory(result);
|
| - } else {
|
| - return Handle<String>();
|
| - }
|
| - return String::New(result);
|
| -}
|
| +const char* LineEditor::name() { return "readline"; }
|
|
|
|
|
| -void ReadLineEditor::AddHistory(const char* str) {
|
| +void LineEditor::AddHistory(const char* str) {
|
| // Do not record empty input.
|
| if (strlen(str) == 0) return;
|
| // Remove duplicate history entry.
|
| @@ -131,41 +126,20 @@ void ReadLineEditor::AddHistory(const char* str) {
|
| }
|
|
|
|
|
| -#ifndef V8_SHARED
|
| -char** ReadLineEditor::AttemptedCompletion(const char* text,
|
| - int start,
|
| - int end) {
|
| - char** result = completion_matches(text, CompletionGenerator);
|
| - rl_attempted_completion_over = true;
|
| - return result;
|
| -}
|
| -
|
| -
|
| -char* ReadLineEditor::CompletionGenerator(const char* text, int state) {
|
| - static unsigned current_index;
|
| - static Persistent<Array> current_completions;
|
| - if (state == 0) {
|
| - HandleScope scope;
|
| - Local<String> full_text = String::New(rl_line_buffer, rl_point);
|
| - Handle<Array> completions =
|
| - Shell::GetCompletions(String::New(text), full_text);
|
| - current_completions = Persistent<Array>::New(completions);
|
| - current_index = 0;
|
| +Handle<String> LineEditor::Prompt(const char* prompt) {
|
| + char* result = NULL;
|
| + { // Release lock for blocking input.
|
| + Unlocker unlock(Isolate::GetCurrent());
|
| + result = readline(prompt);
|
| }
|
| - if (current_index < current_completions->Length()) {
|
| - HandleScope scope;
|
| - Handle<Integer> index = Integer::New(current_index);
|
| - Handle<Value> str_obj = current_completions->Get(index);
|
| - current_index++;
|
| - String::Utf8Value str(str_obj);
|
| - return strdup(*str);
|
| + if (result != NULL) {
|
| + AddHistory(result);
|
| } else {
|
| - current_completions.Dispose();
|
| - current_completions.Clear();
|
| - return NULL;
|
| + return Handle<String>();
|
| }
|
| + return String::New(result);
|
| }
|
| -#endif // V8_SHARED
|
| -
|
|
|
| } // namespace v8
|
| +
|
| +#endif // USE_READLINE_CONSOLE
|
|
|