V8 Project
d8-readline.cc
Go to the documentation of this file.
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
3 // found in the LICENSE file.
4 
5 #include <stdio.h> // NOLINT
6 #include <string.h> // NOLINT
7 #include <readline/readline.h> // NOLINT
8 #include <readline/history.h> // NOLINT
9 
10 // The readline includes leaves RETURN defined which breaks V8 compilation.
11 #undef RETURN
12 
13 #include "src/d8.h"
14 
15 // There are incompatibilities between different versions and different
16 // implementations of readline. This smooths out one known incompatibility.
17 #if RL_READLINE_VERSION >= 0x0500
18 #define completion_matches rl_completion_matches
19 #endif
20 
21 
22 namespace v8 {
23 
24 
25 class ReadLineEditor: public LineEditor {
26  public:
28  virtual Handle<String> Prompt(const char* prompt);
29  virtual bool Open(Isolate* isolate);
30  virtual bool Close();
31  virtual void AddHistory(const char* str);
32 
33  static const char* kHistoryFileName;
34  static const int kMaxHistoryEntries;
35 
36  private:
37 #ifndef V8_SHARED
38  static char** AttemptedCompletion(const char* text, int start, int end);
39  static char* CompletionGenerator(const char* text, int state);
40 #endif // V8_SHARED
41  static char kWordBreakCharacters[];
42 
44 };
45 
46 
48 char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"',
49  '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(',
50  '\0'};
51 
52 
53 const char* ReadLineEditor::kHistoryFileName = ".d8_history";
54 const int ReadLineEditor::kMaxHistoryEntries = 1000;
55 
56 
57 bool ReadLineEditor::Open(Isolate* isolate) {
58  isolate_ = isolate;
59 
60  rl_initialize();
61 
62 #ifdef V8_SHARED
63  // Don't do completion on shared library mode
64  // http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC24
65  rl_bind_key('\t', rl_insert);
66 #else
67  rl_attempted_completion_function = AttemptedCompletion;
68 #endif // V8_SHARED
69 
70  rl_completer_word_break_characters = kWordBreakCharacters;
71  rl_bind_key('\t', rl_complete);
72  using_history();
73  stifle_history(kMaxHistoryEntries);
74  return read_history(kHistoryFileName) == 0;
75 }
76 
77 
79  return write_history(kHistoryFileName) == 0;
80 }
81 
82 
84  char* result = NULL;
85  result = readline(prompt);
86  if (result == NULL) return Handle<String>();
87  AddHistory(result);
88  return String::NewFromUtf8(isolate_, result);
89 }
90 
91 
92 void ReadLineEditor::AddHistory(const char* str) {
93  // Do not record empty input.
94  if (strlen(str) == 0) return;
95  // Remove duplicate history entry.
96  history_set_pos(history_length-1);
97  if (current_history()) {
98  do {
99  if (strcmp(current_history()->line, str) == 0) {
100  remove_history(where_history());
101  break;
102  }
103  } while (previous_history());
104  }
105  add_history(str);
106 }
107 
108 
109 #ifndef V8_SHARED
110 char** ReadLineEditor::AttemptedCompletion(const char* text,
111  int start,
112  int end) {
113  char** result = completion_matches(text, CompletionGenerator);
114  rl_attempted_completion_over = true;
115  return result;
116 }
117 
118 
119 char* ReadLineEditor::CompletionGenerator(const char* text, int state) {
120  static unsigned current_index;
121  static Persistent<Array> current_completions;
122  Isolate* isolate = read_line_editor.isolate_;
123  HandleScope scope(isolate);
124  Handle<Array> completions;
125  if (state == 0) {
126  Local<String> full_text = String::NewFromUtf8(isolate,
127  rl_line_buffer,
129  rl_point);
130  completions = Shell::GetCompletions(isolate,
131  String::NewFromUtf8(isolate, text),
132  full_text);
133  current_completions.Reset(isolate, completions);
134  current_index = 0;
135  } else {
136  completions = Local<Array>::New(isolate, current_completions);
137  }
138  if (current_index < completions->Length()) {
139  Handle<Integer> index = Integer::New(isolate, current_index);
140  Handle<Value> str_obj = completions->Get(index);
141  current_index++;
142  String::Utf8Value str(str_obj);
143  return strdup(*str);
144  } else {
145  current_completions.Reset();
146  return NULL;
147  }
148 }
149 #endif // V8_SHARED
150 
151 
152 } // namespace v8
A stack-allocated class that governs a number of local handles.
Definition: v8.h:802
An object reference managed by the v8 garbage collector.
Definition: v8.h:198
static Local< Integer > New(Isolate *isolate, int32_t value)
Definition: api.cc:6281
Isolate represents an isolated instance of the V8 engine.
Definition: v8.h:4356
@ READLINE
Definition: d8.h:99
A light-weight stack-allocated object handle.
Definition: v8.h:334
static Local< T > New(Isolate *isolate, Handle< T > that)
Create a local handle for the content of another handle.
Definition: v8.h:5987
void Reset()
If non-empty, destroy the underlying storage cell IsEmpty() will return true after this call.
Definition: v8.h:6082
A PersistentBase which allows copy and assignment.
Definition: v8.h:627
static char * CompletionGenerator(const char *text, int state)
Definition: d8-readline.cc:119
static char kWordBreakCharacters[]
Definition: d8-readline.cc:41
static const char * kHistoryFileName
Definition: d8-readline.cc:33
static char ** AttemptedCompletion(const char *text, int start, int end)
Definition: d8-readline.cc:110
virtual Handle< String > Prompt(const char *prompt)
Definition: d8-readline.cc:83
Isolate * isolate_
Definition: d8-readline.cc:43
virtual bool Close()
Definition: d8-readline.cc:78
virtual bool Open(Isolate *isolate)
Definition: d8-readline.cc:57
virtual void AddHistory(const char *str)
Definition: d8-readline.cc:92
static const int kMaxHistoryEntries
Definition: d8-readline.cc:34
static Handle< Array > GetCompletions(Isolate *isolate, Handle< String > text, Handle< String > full)
Definition: d8.cc:638
Converts an object to a UTF-8-encoded character array.
Definition: v8.h:2048
@ kNormalString
Definition: v8.h:1963
static Local< String > NewFromUtf8(Isolate *isolate, const char *data, NewStringType type=kNormalString, int length=-1)
Allocates a new string from UTF-8 data.
Definition: api.cc:5447
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
static ReadLineEditor read_line_editor
Definition: d8-readline.cc:47