Commit 73ac9928 authored by yangguo@chromium.org's avatar yangguo@chromium.org

Remove limit for d8 shell input length.

BUG=
TEST=

Review URL: https://chromiumcodereview.appspot.com/9232009

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@10418 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent a02dbe42
// Copyright 2011 the V8 project authors. All rights reserved. // Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are // modification, are permitted provided that the following conditions are
// met: // met:
...@@ -49,10 +49,14 @@ namespace v8 { ...@@ -49,10 +49,14 @@ namespace v8 {
class ReadLineEditor: public LineEditor { class ReadLineEditor: public LineEditor {
public: public:
ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { }
virtual i::SmartArrayPointer<char> Prompt(const char* prompt); virtual Handle<String> Prompt(const char* prompt);
virtual bool Open(); virtual bool Open();
virtual bool Close(); virtual bool Close();
virtual void AddHistory(const char* str); virtual void AddHistory(const char* str);
static const char* kHistoryFileName;
static const int kMaxHistoryEntries;
private: private:
static char** AttemptedCompletion(const char* text, int start, int end); static char** AttemptedCompletion(const char* text, int start, int end);
static char* CompletionGenerator(const char* text, int state); static char* CompletionGenerator(const char* text, int state);
...@@ -66,25 +70,34 @@ char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', ...@@ -66,25 +70,34 @@ char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"',
'\0'}; '\0'};
const char* ReadLineEditor::kHistoryFileName = ".d8_history";
const int ReadLineEditor::kMaxHistoryEntries = 1000;
bool ReadLineEditor::Open() { bool ReadLineEditor::Open() {
rl_initialize(); rl_initialize();
rl_attempted_completion_function = AttemptedCompletion; rl_attempted_completion_function = AttemptedCompletion;
rl_completer_word_break_characters = kWordBreakCharacters; rl_completer_word_break_characters = kWordBreakCharacters;
rl_bind_key('\t', rl_complete); rl_bind_key('\t', rl_complete);
using_history(); using_history();
stifle_history(Shell::kMaxHistoryEntries); stifle_history(kMaxHistoryEntries);
return read_history(Shell::kHistoryFileName) == 0; return read_history(kHistoryFileName) == 0;
} }
bool ReadLineEditor::Close() { bool ReadLineEditor::Close() {
return write_history(Shell::kHistoryFileName) == 0; return write_history(kHistoryFileName) == 0;
} }
i::SmartArrayPointer<char> ReadLineEditor::Prompt(const char* prompt) { Handle<String> ReadLineEditor::Prompt(const char* prompt) {
char* result = readline(prompt); char* result = readline(prompt);
return i::SmartArrayPointer<char>(result); if (result != NULL) {
AddHistory(result);
} else {
return Handle<String>();
}
return String::New(result);
} }
...@@ -118,10 +131,10 @@ char* ReadLineEditor::CompletionGenerator(const char* text, int state) { ...@@ -118,10 +131,10 @@ char* ReadLineEditor::CompletionGenerator(const char* text, int state) {
static unsigned current_index; static unsigned current_index;
static Persistent<Array> current_completions; static Persistent<Array> current_completions;
if (state == 0) { if (state == 0) {
i::SmartArrayPointer<char> full_text(i::StrNDup(rl_line_buffer, rl_point));
HandleScope scope; HandleScope scope;
Local<String> full_text = String::New(rl_line_buffer, rl_point);
Handle<Array> completions = Handle<Array> completions =
Shell::GetCompletions(String::New(text), String::New(*full_text)); Shell::GetCompletions(String::New(text), full_text);
current_completions = Persistent<Array>::New(completions); current_completions = Persistent<Array>::New(completions);
current_index = 0; current_index = 0;
} }
......
...@@ -66,11 +66,7 @@ ...@@ -66,11 +66,7 @@
namespace v8 { namespace v8 {
#ifndef V8_SHARED
LineEditor *LineEditor::first_ = NULL; LineEditor *LineEditor::first_ = NULL;
const char* Shell::kHistoryFileName = ".d8_history";
const int Shell::kMaxHistoryEntries = 1000;
LineEditor::LineEditor(Type type, const char* name) LineEditor::LineEditor(Type type, const char* name)
...@@ -96,31 +92,29 @@ LineEditor* LineEditor::Get() { ...@@ -96,31 +92,29 @@ LineEditor* LineEditor::Get() {
class DumbLineEditor: public LineEditor { class DumbLineEditor: public LineEditor {
public: public:
DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { } DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { }
virtual i::SmartArrayPointer<char> Prompt(const char* prompt); virtual Handle<String> Prompt(const char* prompt);
}; };
static DumbLineEditor dumb_line_editor; static DumbLineEditor dumb_line_editor;
i::SmartArrayPointer<char> DumbLineEditor::Prompt(const char* prompt) { Handle<String> DumbLineEditor::Prompt(const char* prompt) {
static const int kBufferSize = 256;
char buffer[kBufferSize];
printf("%s", prompt); printf("%s", prompt);
char* str = fgets(buffer, kBufferSize, stdin); return Shell::ReadFromStdin();
return i::SmartArrayPointer<char>(str ? i::StrDup(str) : str);
} }
#ifndef V8_SHARED
CounterMap* Shell::counter_map_; CounterMap* Shell::counter_map_;
i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; i::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
CounterCollection Shell::local_counters_; CounterCollection Shell::local_counters_;
CounterCollection* Shell::counters_ = &local_counters_; CounterCollection* Shell::counters_ = &local_counters_;
i::Mutex* Shell::context_mutex_(i::OS::CreateMutex()); i::Mutex* Shell::context_mutex_(i::OS::CreateMutex());
Persistent<Context> Shell::utility_context_; Persistent<Context> Shell::utility_context_;
LineEditor* Shell::console = NULL;
#endif // V8_SHARED #endif // V8_SHARED
LineEditor* Shell::console = NULL;
Persistent<Context> Shell::evaluation_context_; Persistent<Context> Shell::evaluation_context_;
ShellOptions Shell::options; ShellOptions Shell::options;
const char* Shell::kPrompt = "d8> "; const char* Shell::kPrompt = "d8> ";
...@@ -238,7 +232,7 @@ Handle<Value> Shell::Read(const Arguments& args) { ...@@ -238,7 +232,7 @@ Handle<Value> Shell::Read(const Arguments& args) {
} }
Handle<Value> Shell::ReadLine(const Arguments& args) { Handle<String> Shell::ReadFromStdin() {
static const int kBufferSize = 256; static const int kBufferSize = 256;
char buffer[kBufferSize]; char buffer[kBufferSize];
Handle<String> accumulator = String::New(""); Handle<String> accumulator = String::New("");
...@@ -247,7 +241,7 @@ Handle<Value> Shell::ReadLine(const Arguments& args) { ...@@ -247,7 +241,7 @@ Handle<Value> Shell::ReadLine(const Arguments& args) {
// Continue reading if the line ends with an escape '\\' or the line has // Continue reading if the line ends with an escape '\\' or the line has
// not been fully read into the buffer yet (does not end with '\n'). // not been fully read into the buffer yet (does not end with '\n').
// If fgets gets an error, just give up. // If fgets gets an error, just give up.
if (fgets(buffer, kBufferSize, stdin) == NULL) return Null(); if (fgets(buffer, kBufferSize, stdin) == NULL) return Handle<String>();
length = static_cast<int>(strlen(buffer)); length = static_cast<int>(strlen(buffer));
if (length == 0) { if (length == 0) {
return accumulator; return accumulator;
...@@ -1047,28 +1041,15 @@ void Shell::RunShell() { ...@@ -1047,28 +1041,15 @@ void Shell::RunShell() {
Context::Scope context_scope(evaluation_context_); Context::Scope context_scope(evaluation_context_);
HandleScope outer_scope; HandleScope outer_scope;
Handle<String> name = String::New("(d8)"); Handle<String> name = String::New("(d8)");
#ifndef V8_SHARED
console = LineEditor::Get(); console = LineEditor::Get();
printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name());
console->Open(); console->Open();
while (true) { while (true) {
i::SmartArrayPointer<char> input = console->Prompt(Shell::kPrompt);
if (input.is_empty()) break;
console->AddHistory(*input);
HandleScope inner_scope; HandleScope inner_scope;
ExecuteString(String::New(*input), name, true, true); Handle<String> input = console->Prompt(Shell::kPrompt);
if (input.IsEmpty()) break;
ExecuteString(input, name, true, true);
} }
#else
printf("V8 version %s [D8 light using shared library]\n", V8::GetVersion());
static const int kBufferSize = 256;
while (true) {
char buffer[kBufferSize];
printf("%s", Shell::kPrompt);
if (fgets(buffer, kBufferSize, stdin) == NULL) break;
HandleScope inner_scope;
ExecuteString(String::New(buffer), name, true, true);
}
#endif // V8_SHARED
printf("\n"); printf("\n");
} }
......
...@@ -116,14 +116,13 @@ class CounterMap { ...@@ -116,14 +116,13 @@ class CounterMap {
#endif // V8_SHARED #endif // V8_SHARED
#ifndef V8_SHARED
class LineEditor { class LineEditor {
public: public:
enum Type { DUMB = 0, READLINE = 1 }; enum Type { DUMB = 0, READLINE = 1 };
LineEditor(Type type, const char* name); LineEditor(Type type, const char* name);
virtual ~LineEditor() { } virtual ~LineEditor() { }
virtual i::SmartArrayPointer<char> Prompt(const char* prompt) = 0; virtual Handle<String> Prompt(const char* prompt) = 0;
virtual bool Open() { return true; } virtual bool Open() { return true; }
virtual bool Close() { return true; } virtual bool Close() { return true; }
virtual void AddHistory(const char* str) { } virtual void AddHistory(const char* str) { }
...@@ -136,7 +135,6 @@ class LineEditor { ...@@ -136,7 +135,6 @@ class LineEditor {
LineEditor* next_; LineEditor* next_;
static LineEditor* first_; static LineEditor* first_;
}; };
#endif // V8_SHARED
class SourceGroup { class SourceGroup {
...@@ -287,7 +285,10 @@ class Shell : public i::AllStatic { ...@@ -287,7 +285,10 @@ class Shell : public i::AllStatic {
static Handle<Value> EnableProfiler(const Arguments& args); static Handle<Value> EnableProfiler(const Arguments& args);
static Handle<Value> DisableProfiler(const Arguments& args); static Handle<Value> DisableProfiler(const Arguments& args);
static Handle<Value> Read(const Arguments& args); static Handle<Value> Read(const Arguments& args);
static Handle<Value> ReadLine(const Arguments& args); static Handle<String> ReadFromStdin();
static Handle<Value> ReadLine(const Arguments& args) {
return ReadFromStdin();
}
static Handle<Value> Load(const Arguments& args); static Handle<Value> Load(const Arguments& args);
static Handle<Value> ArrayBuffer(const Arguments& args); static Handle<Value> ArrayBuffer(const Arguments& args);
static Handle<Value> Int8Array(const Arguments& args); static Handle<Value> Int8Array(const Arguments& args);
...@@ -335,11 +336,8 @@ class Shell : public i::AllStatic { ...@@ -335,11 +336,8 @@ class Shell : public i::AllStatic {
static Handle<Value> RemoveDirectory(const Arguments& args); static Handle<Value> RemoveDirectory(const Arguments& args);
static void AddOSMethods(Handle<ObjectTemplate> os_template); static void AddOSMethods(Handle<ObjectTemplate> os_template);
#ifndef V8_SHARED
static const char* kHistoryFileName;
static const int kMaxHistoryEntries;
static LineEditor* console; static LineEditor* console;
#endif // V8_SHARED
static const char* kPrompt; static const char* kPrompt;
static ShellOptions options; static ShellOptions options;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment