| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifdef USE_READLINE_CONSOLE |
| 29 |
| 28 #include <cstdio> // NOLINT | 30 #include <cstdio> // NOLINT |
| 29 #include <string.h> // NOLINT | |
| 30 #include <readline/readline.h> // NOLINT | 31 #include <readline/readline.h> // NOLINT |
| 31 #include <readline/history.h> // NOLINT | 32 #include <readline/history.h> // NOLINT |
| 32 | 33 |
| 33 // The readline includes leaves RETURN defined which breaks V8 compilation. | 34 // The readline includes leaves RETURN defined which breaks V8 compilation. |
| 34 #undef RETURN | 35 #undef RETURN |
| 35 | 36 |
| 36 #include "d8.h" | 37 #include "d8.h" |
| 37 | 38 |
| 39 |
| 38 // There are incompatibilities between different versions and different | 40 // There are incompatibilities between different versions and different |
| 39 // implementations of readline. This smooths out one known incompatibility. | 41 // implementations of readline. This smooths out one known incompatibility. |
| 40 #if RL_READLINE_VERSION >= 0x0500 | 42 #if RL_READLINE_VERSION >= 0x0500 |
| 41 #define completion_matches rl_completion_matches | 43 #define completion_matches rl_completion_matches |
| 42 #endif | 44 #endif |
| 43 | 45 |
| 44 | 46 |
| 45 namespace v8 { | 47 namespace v8 { |
| 46 | 48 |
| 47 | 49 static char kWordBreakCharacters[] = {' ', '\t', '\n', '"', |
| 48 class ReadLineEditor: public LineEditor { | |
| 49 public: | |
| 50 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } | |
| 51 virtual Handle<String> Prompt(const char* prompt); | |
| 52 virtual bool Open(); | |
| 53 virtual bool Close(); | |
| 54 virtual void AddHistory(const char* str); | |
| 55 | |
| 56 static const char* kHistoryFileName; | |
| 57 static const int kMaxHistoryEntries; | |
| 58 | |
| 59 private: | |
| 60 #ifndef V8_SHARED | |
| 61 static char** AttemptedCompletion(const char* text, int start, int end); | |
| 62 static char* CompletionGenerator(const char* text, int state); | |
| 63 #endif // V8_SHARED | |
| 64 static char kWordBreakCharacters[]; | |
| 65 }; | |
| 66 | |
| 67 | |
| 68 static ReadLineEditor read_line_editor; | |
| 69 char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', | |
| 70 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', | 50 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', |
| 71 '\0'}; | 51 '\0'}; |
| 72 | 52 |
| 73 | 53 |
| 74 const char* ReadLineEditor::kHistoryFileName = ".d8_history"; | 54 static const char* kHistoryFileName = ".d8_history"; |
| 75 const int ReadLineEditor::kMaxHistoryEntries = 1000; | 55 static const int kMaxHistoryEntries = 1000; |
| 76 | 56 |
| 77 | 57 |
| 78 bool ReadLineEditor::Open() { | 58 char* CompletionGenerator(const char* text, int state) { |
| 59 static unsigned current_index; |
| 60 static Persistent<Array> current_completions; |
| 61 if (state == 0) { |
| 62 HandleScope scope; |
| 63 Local<String> full_text = String::New(rl_line_buffer, rl_point); |
| 64 Handle<Array> completions = |
| 65 Shell::GetCompletions(String::New(text), full_text); |
| 66 current_completions = Persistent<Array>::New(completions); |
| 67 current_index = 0; |
| 68 } |
| 69 if (current_index < current_completions->Length()) { |
| 70 HandleScope scope; |
| 71 Handle<Integer> index = Integer::New(current_index); |
| 72 Handle<Value> str_obj = current_completions->Get(index); |
| 73 current_index++; |
| 74 String::Utf8Value str(str_obj); |
| 75 return strdup(*str); |
| 76 } else { |
| 77 current_completions.Dispose(); |
| 78 current_completions.Clear(); |
| 79 return NULL; |
| 80 } |
| 81 } |
| 82 |
| 83 |
| 84 char** AttemptedCompletion(const char* text, |
| 85 int start, |
| 86 int end) { |
| 87 char** result = completion_matches(text, CompletionGenerator); |
| 88 rl_attempted_completion_over = true; |
| 89 return result; |
| 90 } |
| 91 |
| 92 |
| 93 void LineEditor::Open() { |
| 79 rl_initialize(); | 94 rl_initialize(); |
| 80 | |
| 81 #ifdef V8_SHARED | |
| 82 // Don't do completion on shared library mode | |
| 83 // http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC24 | |
| 84 rl_bind_key('\t', rl_insert); | |
| 85 #else | |
| 86 rl_attempted_completion_function = AttemptedCompletion; | 95 rl_attempted_completion_function = AttemptedCompletion; |
| 87 #endif // V8_SHARED | |
| 88 | |
| 89 rl_completer_word_break_characters = kWordBreakCharacters; | 96 rl_completer_word_break_characters = kWordBreakCharacters; |
| 90 rl_bind_key('\t', rl_complete); | 97 rl_bind_key('\t', rl_complete); |
| 91 using_history(); | 98 using_history(); |
| 92 stifle_history(kMaxHistoryEntries); | 99 stifle_history(kMaxHistoryEntries); |
| 93 return read_history(kHistoryFileName) == 0; | 100 read_history(kHistoryFileName); |
| 94 } | 101 } |
| 95 | 102 |
| 96 | 103 |
| 97 bool ReadLineEditor::Close() { | 104 LineEditor::~LineEditor() { |
| 98 return write_history(kHistoryFileName) == 0; | 105 write_history(kHistoryFileName); |
| 99 } | 106 } |
| 100 | 107 |
| 101 | 108 |
| 102 Handle<String> ReadLineEditor::Prompt(const char* prompt) { | 109 const char* LineEditor::name() { return "readline"; } |
| 103 char* result = NULL; | |
| 104 { // Release lock for blocking input. | |
| 105 Unlocker unlock(Isolate::GetCurrent()); | |
| 106 result = readline(prompt); | |
| 107 } | |
| 108 if (result != NULL) { | |
| 109 AddHistory(result); | |
| 110 } else { | |
| 111 return Handle<String>(); | |
| 112 } | |
| 113 return String::New(result); | |
| 114 } | |
| 115 | 110 |
| 116 | 111 |
| 117 void ReadLineEditor::AddHistory(const char* str) { | 112 void LineEditor::AddHistory(const char* str) { |
| 118 // Do not record empty input. | 113 // Do not record empty input. |
| 119 if (strlen(str) == 0) return; | 114 if (strlen(str) == 0) return; |
| 120 // Remove duplicate history entry. | 115 // Remove duplicate history entry. |
| 121 history_set_pos(history_length-1); | 116 history_set_pos(history_length-1); |
| 122 if (current_history()) { | 117 if (current_history()) { |
| 123 do { | 118 do { |
| 124 if (strcmp(current_history()->line, str) == 0) { | 119 if (strcmp(current_history()->line, str) == 0) { |
| 125 remove_history(where_history()); | 120 remove_history(where_history()); |
| 126 break; | 121 break; |
| 127 } | 122 } |
| 128 } while (previous_history()); | 123 } while (previous_history()); |
| 129 } | 124 } |
| 130 add_history(str); | 125 add_history(str); |
| 131 } | 126 } |
| 132 | 127 |
| 133 | 128 |
| 134 #ifndef V8_SHARED | 129 Handle<String> LineEditor::Prompt(const char* prompt) { |
| 135 char** ReadLineEditor::AttemptedCompletion(const char* text, | 130 char* result = NULL; |
| 136 int start, | 131 { // Release lock for blocking input. |
| 137 int end) { | 132 Unlocker unlock(Isolate::GetCurrent()); |
| 138 char** result = completion_matches(text, CompletionGenerator); | 133 result = readline(prompt); |
| 139 rl_attempted_completion_over = true; | 134 } |
| 140 return result; | 135 if (result != NULL) { |
| 136 AddHistory(result); |
| 137 } else { |
| 138 return Handle<String>(); |
| 139 } |
| 140 return String::New(result); |
| 141 } | 141 } |
| 142 | 142 |
| 143 } // namespace v8 |
| 143 | 144 |
| 144 char* ReadLineEditor::CompletionGenerator(const char* text, int state) { | 145 #endif // USE_READLINE_CONSOLE |
| 145 static unsigned current_index; | |
| 146 static Persistent<Array> current_completions; | |
| 147 if (state == 0) { | |
| 148 HandleScope scope; | |
| 149 Local<String> full_text = String::New(rl_line_buffer, rl_point); | |
| 150 Handle<Array> completions = | |
| 151 Shell::GetCompletions(String::New(text), full_text); | |
| 152 current_completions = Persistent<Array>::New(completions); | |
| 153 current_index = 0; | |
| 154 } | |
| 155 if (current_index < current_completions->Length()) { | |
| 156 HandleScope scope; | |
| 157 Handle<Integer> index = Integer::New(current_index); | |
| 158 Handle<Value> str_obj = current_completions->Get(index); | |
| 159 current_index++; | |
| 160 String::Utf8Value str(str_obj); | |
| 161 return strdup(*str); | |
| 162 } else { | |
| 163 current_completions.Dispose(); | |
| 164 current_completions.Clear(); | |
| 165 return NULL; | |
| 166 } | |
| 167 } | |
| 168 #endif // V8_SHARED | |
| 169 | |
| 170 | |
| 171 } // namespace v8 | |
| OLD | NEW |