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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 #endif | 42 #endif |
43 | 43 |
44 | 44 |
45 namespace v8 { | 45 namespace v8 { |
46 | 46 |
47 | 47 |
48 class ReadLineEditor: public LineEditor { | 48 class ReadLineEditor: public LineEditor { |
49 public: | 49 public: |
50 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } | 50 ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } |
51 virtual Handle<String> Prompt(const char* prompt); | 51 virtual Handle<String> Prompt(const char* prompt); |
52 virtual bool Open(); | 52 virtual bool Open(Isolate* isolate); |
53 virtual bool Close(); | 53 virtual bool Close(); |
54 virtual void AddHistory(const char* str); | 54 virtual void AddHistory(const char* str); |
55 | 55 |
56 static const char* kHistoryFileName; | 56 static const char* kHistoryFileName; |
57 static const int kMaxHistoryEntries; | 57 static const int kMaxHistoryEntries; |
58 | 58 |
59 private: | 59 private: |
60 #ifndef V8_SHARED | 60 #ifndef V8_SHARED |
61 static char** AttemptedCompletion(const char* text, int start, int end); | 61 static char** AttemptedCompletion(const char* text, int start, int end); |
62 static char* CompletionGenerator(const char* text, int state); | 62 static char* CompletionGenerator(const char* text, int state); |
63 #endif // V8_SHARED | 63 #endif // V8_SHARED |
64 static char kWordBreakCharacters[]; | 64 static char kWordBreakCharacters[]; |
| 65 |
| 66 Isolate* isolate_; |
65 }; | 67 }; |
66 | 68 |
67 | 69 |
68 static ReadLineEditor read_line_editor; | 70 static ReadLineEditor read_line_editor; |
69 char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', | 71 char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', |
70 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', | 72 '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', |
71 '\0'}; | 73 '\0'}; |
72 | 74 |
73 | 75 |
74 const char* ReadLineEditor::kHistoryFileName = ".d8_history"; | 76 const char* ReadLineEditor::kHistoryFileName = ".d8_history"; |
75 const int ReadLineEditor::kMaxHistoryEntries = 1000; | 77 const int ReadLineEditor::kMaxHistoryEntries = 1000; |
76 | 78 |
77 | 79 |
78 bool ReadLineEditor::Open() { | 80 bool ReadLineEditor::Open(Isolate* isolate) { |
| 81 isolate_ = isolate; |
| 82 |
79 rl_initialize(); | 83 rl_initialize(); |
80 | 84 |
81 #ifdef V8_SHARED | 85 #ifdef V8_SHARED |
82 // Don't do completion on shared library mode | 86 // Don't do completion on shared library mode |
83 // http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC24 | 87 // http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC24 |
84 rl_bind_key('\t', rl_insert); | 88 rl_bind_key('\t', rl_insert); |
85 #else | 89 #else |
86 rl_attempted_completion_function = AttemptedCompletion; | 90 rl_attempted_completion_function = AttemptedCompletion; |
87 #endif // V8_SHARED | 91 #endif // V8_SHARED |
88 | 92 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 int end) { | 141 int end) { |
138 char** result = completion_matches(text, CompletionGenerator); | 142 char** result = completion_matches(text, CompletionGenerator); |
139 rl_attempted_completion_over = true; | 143 rl_attempted_completion_over = true; |
140 return result; | 144 return result; |
141 } | 145 } |
142 | 146 |
143 | 147 |
144 char* ReadLineEditor::CompletionGenerator(const char* text, int state) { | 148 char* ReadLineEditor::CompletionGenerator(const char* text, int state) { |
145 static unsigned current_index; | 149 static unsigned current_index; |
146 static Persistent<Array> current_completions; | 150 static Persistent<Array> current_completions; |
| 151 Isolate* isolate = read_line_editor.isolate_; |
| 152 Locker lock(isolate); |
147 if (state == 0) { | 153 if (state == 0) { |
148 HandleScope scope; | 154 HandleScope scope; |
149 Local<String> full_text = String::New(rl_line_buffer, rl_point); | 155 Local<String> full_text = String::New(rl_line_buffer, rl_point); |
150 Handle<Array> completions = | 156 Handle<Array> completions = |
151 Shell::GetCompletions(String::New(text), full_text); | 157 Shell::GetCompletions(String::New(text), full_text); |
152 current_completions = Persistent<Array>::New(completions); | 158 current_completions = Persistent<Array>::New(isolate, completions); |
153 current_index = 0; | 159 current_index = 0; |
154 } | 160 } |
155 if (current_index < current_completions->Length()) { | 161 if (current_index < current_completions->Length()) { |
156 HandleScope scope; | 162 HandleScope scope; |
157 Handle<Integer> index = Integer::New(current_index); | 163 Handle<Integer> index = Integer::New(current_index); |
158 Handle<Value> str_obj = current_completions->Get(index); | 164 Handle<Value> str_obj = current_completions->Get(index); |
159 current_index++; | 165 current_index++; |
160 String::Utf8Value str(str_obj); | 166 String::Utf8Value str(str_obj); |
161 return strdup(*str); | 167 return strdup(*str); |
162 } else { | 168 } else { |
163 current_completions.Dispose(); | 169 current_completions.Dispose(isolate); |
164 current_completions.Clear(); | 170 current_completions.Clear(); |
165 return NULL; | 171 return NULL; |
166 } | 172 } |
167 } | 173 } |
168 #endif // V8_SHARED | 174 #endif // V8_SHARED |
169 | 175 |
170 | 176 |
171 } // namespace v8 | 177 } // namespace v8 |
OLD | NEW |