OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> // NOLINT | 5 #include <stdio.h> // NOLINT |
6 #include <string.h> // NOLINT | 6 #include <string.h> // NOLINT |
7 #include <readline/readline.h> // NOLINT | 7 #include <readline/readline.h> // NOLINT |
8 #include <readline/history.h> // NOLINT | 8 #include <readline/history.h> // NOLINT |
9 | 9 |
10 // The readline includes leaves RETURN defined which breaks V8 compilation. | 10 // The readline includes leaves RETURN defined which breaks V8 compilation. |
11 #undef RETURN | 11 #undef RETURN |
12 | 12 |
13 #include "src/d8.h" | 13 #include "src/d8.h" |
14 | 14 |
15 // There are incompatibilities between different versions and different | 15 // There are incompatibilities between different versions and different |
16 // implementations of readline. This smooths out one known incompatibility. | 16 // implementations of readline. This smooths out one known incompatibility. |
17 #if RL_READLINE_VERSION >= 0x0500 | 17 #if RL_READLINE_VERSION >= 0x0500 |
18 #define completion_matches rl_completion_matches | 18 #define completion_matches rl_completion_matches |
19 #endif | 19 #endif |
20 | 20 |
21 | 21 |
22 namespace v8 { | 22 namespace v8 { |
23 | 23 |
24 | 24 |
25 class ReadLineEditor: public LineEditor { | 25 class ReadLineEditor: public LineEditor { |
26 public: | 26 public: |
27 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } | 27 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } |
28 virtual Local<String> Prompt(const char* prompt); | 28 virtual Handle<String> Prompt(const char* prompt); |
29 virtual bool Open(Isolate* isolate); | 29 virtual bool Open(Isolate* isolate); |
30 virtual bool Close(); | 30 virtual bool Close(); |
31 virtual void AddHistory(const char* str); | 31 virtual void AddHistory(const char* str); |
32 | 32 |
33 static const char* kHistoryFileName; | 33 static const char* kHistoryFileName; |
34 static const int kMaxHistoryEntries; | 34 static const int kMaxHistoryEntries; |
35 | 35 |
36 private: | 36 private: |
37 #ifndef V8_SHARED | 37 #ifndef V8_SHARED |
38 static char** AttemptedCompletion(const char* text, int start, int end); | 38 static char** AttemptedCompletion(const char* text, int start, int end); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 stifle_history(kMaxHistoryEntries); | 73 stifle_history(kMaxHistoryEntries); |
74 return read_history(kHistoryFileName) == 0; | 74 return read_history(kHistoryFileName) == 0; |
75 } | 75 } |
76 | 76 |
77 | 77 |
78 bool ReadLineEditor::Close() { | 78 bool ReadLineEditor::Close() { |
79 return write_history(kHistoryFileName) == 0; | 79 return write_history(kHistoryFileName) == 0; |
80 } | 80 } |
81 | 81 |
82 | 82 |
83 Local<String> ReadLineEditor::Prompt(const char* prompt) { | 83 Handle<String> ReadLineEditor::Prompt(const char* prompt) { |
84 char* result = NULL; | 84 char* result = NULL; |
85 result = readline(prompt); | 85 result = readline(prompt); |
86 if (result == NULL) return Local<String>(); | 86 if (result == NULL) return Handle<String>(); |
87 AddHistory(result); | 87 AddHistory(result); |
88 return String::NewFromUtf8(isolate_, result); | 88 return String::NewFromUtf8(isolate_, result); |
89 } | 89 } |
90 | 90 |
91 | 91 |
92 void ReadLineEditor::AddHistory(const char* str) { | 92 void ReadLineEditor::AddHistory(const char* str) { |
93 // Do not record empty input. | 93 // Do not record empty input. |
94 if (strlen(str) == 0) return; | 94 if (strlen(str) == 0) return; |
95 // Remove duplicate history entry. | 95 // Remove duplicate history entry. |
96 history_set_pos(history_length-1); | 96 history_set_pos(history_length-1); |
(...skipping 14 matching lines...) Expand all Loading... |
111 int start, | 111 int start, |
112 int end) { | 112 int end) { |
113 char** result = completion_matches(text, CompletionGenerator); | 113 char** result = completion_matches(text, CompletionGenerator); |
114 rl_attempted_completion_over = true; | 114 rl_attempted_completion_over = true; |
115 return result; | 115 return result; |
116 } | 116 } |
117 | 117 |
118 | 118 |
119 char* ReadLineEditor::CompletionGenerator(const char* text, int state) { | 119 char* ReadLineEditor::CompletionGenerator(const char* text, int state) { |
120 static unsigned current_index; | 120 static unsigned current_index; |
121 static Global<Array> current_completions; | 121 static Persistent<Array> current_completions; |
122 Isolate* isolate = read_line_editor.isolate_; | 122 Isolate* isolate = read_line_editor.isolate_; |
123 HandleScope scope(isolate); | 123 HandleScope scope(isolate); |
124 Local<Array> completions; | 124 Handle<Array> completions; |
125 if (state == 0) { | 125 if (state == 0) { |
126 Local<String> full_text = String::NewFromUtf8(isolate, | 126 Local<String> full_text = String::NewFromUtf8(isolate, |
127 rl_line_buffer, | 127 rl_line_buffer, |
128 String::kNormalString, | 128 String::kNormalString, |
129 rl_point); | 129 rl_point); |
130 completions = Shell::GetCompletions(isolate, | 130 completions = Shell::GetCompletions(isolate, |
131 String::NewFromUtf8(isolate, text), | 131 String::NewFromUtf8(isolate, text), |
132 full_text); | 132 full_text); |
133 current_completions.Reset(isolate, completions); | 133 current_completions.Reset(isolate, completions); |
134 current_index = 0; | 134 current_index = 0; |
135 } else { | 135 } else { |
136 completions = Local<Array>::New(isolate, current_completions); | 136 completions = Local<Array>::New(isolate, current_completions); |
137 } | 137 } |
138 if (current_index < completions->Length()) { | 138 if (current_index < completions->Length()) { |
139 Local<Integer> index = Integer::New(isolate, current_index); | 139 Handle<Integer> index = Integer::New(isolate, current_index); |
140 Local<Value> str_obj = completions->Get(index); | 140 Handle<Value> str_obj = completions->Get(index); |
141 current_index++; | 141 current_index++; |
142 String::Utf8Value str(str_obj); | 142 String::Utf8Value str(str_obj); |
143 return strdup(*str); | 143 return strdup(*str); |
144 } else { | 144 } else { |
145 current_completions.Reset(); | 145 current_completions.Reset(); |
146 return NULL; | 146 return NULL; |
147 } | 147 } |
148 } | 148 } |
149 #endif // V8_SHARED | 149 #endif // V8_SHARED |
150 | 150 |
151 | 151 |
152 } // namespace v8 | 152 } // namespace v8 |
OLD | NEW |