From 8e027195b48a7e510ba48d67af68b880ab9cd6a9 Mon Sep 17 00:00:00 2001 From: yangguo Date: Thu, 23 Jul 2015 06:20:14 -0700 Subject: [PATCH] Remove readline support from d8. Nobody seems to use it. A good alternative is rlwrap. R=jochen@chromium.org Review URL: https://codereview.chromium.org/1250223002 Cr-Commit-Position: refs/heads/master@{#29810} --- BUILD.gn | 2 +- Makefile | 4 -- src/d8-readline.cc | 155 --------------------------------------------- src/d8.cc | 69 +++----------------- src/d8.gyp | 5 -- src/d8.h | 22 ------- src/d8.js | 45 ------------- 7 files changed, 9 insertions(+), 293 deletions(-) delete mode 100644 src/d8-readline.cc diff --git a/BUILD.gn b/BUILD.gn index 434b7b8ce9..fc153a2266 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1751,7 +1751,7 @@ if ((current_toolchain == host_toolchain && v8_toolset_for_d8 == "host") || "//build/config/sanitizers:deps", ] - # TODO(jochen): Add support for readline and vtunejit. + # TODO(jochen): Add support for vtunejit. if (is_posix) { sources += [ "src/d8-posix.cc" ] diff --git a/Makefile b/Makefile index 7f161c64c9..8d209a0b88 100644 --- a/Makefile +++ b/Makefile @@ -44,10 +44,6 @@ endif ifdef component GYPFLAGS += -Dcomponent=$(component) endif -# console=readline -ifdef console - GYPFLAGS += -Dconsole=$(console) -endif # disassembler=on ifeq ($(disassembler), on) GYPFLAGS += -Dv8_enable_disassembler=1 diff --git a/src/d8-readline.cc b/src/d8-readline.cc deleted file mode 100644 index e57b7567e3..0000000000 --- a/src/d8-readline.cc +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright 2012 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include // NOLINT -#include // NOLINT -#include // NOLINT -#include // NOLINT - -// The readline includes leaves RETURN defined which breaks V8 compilation. -#undef RETURN - -#include "src/d8.h" - -// There are incompatibilities between different versions and different -// implementations of readline. This smooths out one known incompatibility. -#if RL_READLINE_VERSION >= 0x0500 -#define completion_matches rl_completion_matches -#endif - - -namespace v8 { - - -class ReadLineEditor: public LineEditor { - public: - ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { } - virtual Local Prompt(const char* prompt); - virtual bool Open(Isolate* isolate); - virtual bool Close(); - virtual void AddHistory(const char* str); - - static const char* kHistoryFileName; - static const int kMaxHistoryEntries; - - private: -#ifndef V8_SHARED - static char** AttemptedCompletion(const char* text, int start, int end); - static char* CompletionGenerator(const char* text, int state); -#endif // V8_SHARED - static char kWordBreakCharacters[]; - - Isolate* isolate_; -}; - - -static ReadLineEditor read_line_editor; -char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"', - '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(', - '\0'}; - - -const char* ReadLineEditor::kHistoryFileName = ".d8_history"; -const int ReadLineEditor::kMaxHistoryEntries = 1000; - - -bool ReadLineEditor::Open(Isolate* isolate) { - isolate_ = isolate; - - rl_initialize(); - -#ifdef V8_SHARED - // Don't do completion on shared library mode - // http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC24 - rl_bind_key('\t', rl_insert); -#else - rl_attempted_completion_function = AttemptedCompletion; -#endif // V8_SHARED - - rl_completer_word_break_characters = kWordBreakCharacters; - rl_bind_key('\t', rl_complete); - using_history(); - stifle_history(kMaxHistoryEntries); - return read_history(kHistoryFileName) == 0; -} - - -bool ReadLineEditor::Close() { - return write_history(kHistoryFileName) == 0; -} - - -Local ReadLineEditor::Prompt(const char* prompt) { - char* result = NULL; - result = readline(prompt); - if (result == NULL) return Local(); - AddHistory(result); - return String::NewFromUtf8(isolate_, result, NewStringType::kNormal) - .ToLocalChecked(); -} - - -void ReadLineEditor::AddHistory(const char* str) { - // Do not record empty input. - if (strlen(str) == 0) return; - // Remove duplicate history entry. - history_set_pos(history_length-1); - if (current_history()) { - do { - if (strcmp(current_history()->line, str) == 0) { - remove_history(where_history()); - break; - } - } while (previous_history()); - } - add_history(str); -} - - -#ifndef V8_SHARED -char** ReadLineEditor::AttemptedCompletion(const char* text, - int start, - int end) { - char** result = completion_matches(text, CompletionGenerator); - rl_attempted_completion_over = true; - return result; -} - - -char* ReadLineEditor::CompletionGenerator(const char* text, int state) { - static unsigned current_index; - static Global current_completions; - Isolate* isolate = read_line_editor.isolate_; - HandleScope scope(isolate); - Local completions; - if (state == 0) { - Local full_text = - String::NewFromUtf8(isolate, rl_line_buffer, NewStringType::kNormal, - rl_point) - .ToLocalChecked(); - completions = Shell::GetCompletions( - isolate, String::NewFromUtf8(isolate, text, NewStringType::kNormal) - .ToLocalChecked(), - full_text); - current_completions.Reset(isolate, completions); - current_index = 0; - } else { - completions = Local::New(isolate, current_completions); - } - if (current_index < completions->Length()) { - Local context(isolate->GetCurrentContext()); - Local index = Integer::New(isolate, current_index); - Local str_obj = completions->Get(context, index).ToLocalChecked(); - current_index++; - String::Utf8Value str(str_obj); - return strdup(*str); - } else { - current_completions.Reset(); - return NULL; - } -} -#endif // V8_SHARED - - -} // namespace v8 diff --git a/src/d8.cc b/src/d8.cc index 66ea3a15f7..52df5c035d 100644 --- a/src/d8.cc +++ b/src/d8.cc @@ -167,37 +167,6 @@ class PerIsolateData { }; -LineEditor *LineEditor::current_ = NULL; - - -LineEditor::LineEditor(Type type, const char* name) - : type_(type), name_(name) { - if (current_ == NULL || current_->type_ < type) current_ = this; -} - - -class DumbLineEditor: public LineEditor { - public: - explicit DumbLineEditor(Isolate* isolate) - : LineEditor(LineEditor::DUMB, "dumb"), isolate_(isolate) { } - virtual Local Prompt(const char* prompt); - - private: - Isolate* isolate_; -}; - - -Local DumbLineEditor::Prompt(const char* prompt) { - printf("%s", prompt); -#if defined(__native_client__) - // Native Client libc is used to being embedded in Chrome and - // has trouble recognizing when to flush. - fflush(stdout); -#endif - return Shell::ReadFromStdin(isolate_); -} - - #ifndef V8_SHARED CounterMap* Shell::counter_map_; base::OS::MemoryMappedFile* Shell::counters_file_ = NULL; @@ -216,7 +185,6 @@ i::List Shell::externalized_shared_contents_; Global Shell::evaluation_context_; ArrayBuffer::Allocator* Shell::array_buffer_allocator; ShellOptions Shell::options; -const char* Shell::kPrompt = "d8> "; base::OnceType Shell::quit_once_ = V8_ONCE_INIT; #ifndef V8_SHARED @@ -919,28 +887,6 @@ void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { #ifndef V8_SHARED -Local Shell::GetCompletions(Isolate* isolate, Local text, - Local full) { - EscapableHandleScope handle_scope(isolate); - v8::Local utility_context = - v8::Local::New(isolate, utility_context_); - v8::Context::Scope context_scope(utility_context); - Local global = utility_context->Global(); - Local fun = global->Get(utility_context, - String::NewFromUtf8(isolate, "GetCompletions", - NewStringType::kNormal) - .ToLocalChecked()).ToLocalChecked(); - static const int kArgc = 3; - v8::Local evaluation_context = - v8::Local::New(isolate, evaluation_context_); - Local argv[kArgc] = {evaluation_context->Global(), text, full}; - Local val = Local::Cast(fun) - ->Call(utility_context, global, kArgc, argv) - .ToLocalChecked(); - return handle_scope.Escape(Local::Cast(val)); -} - - int32_t* Counter::Bind(const char* name, bool is_histogram) { int i; for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) @@ -1285,8 +1231,6 @@ inline bool operator<(const CounterAndKey& lhs, const CounterAndKey& rhs) { void Shell::OnExit(v8::Isolate* isolate) { - LineEditor* line_editor = LineEditor::Get(); - if (line_editor) line_editor->Close(); #ifndef V8_SHARED reinterpret_cast(isolate)->DumpAndResetCompilationStats(); if (i::FLAG_dump_counters) { @@ -1444,12 +1388,16 @@ void Shell::RunShell(Isolate* isolate) { Local name = String::NewFromUtf8(isolate, "(d8)", NewStringType::kNormal) .ToLocalChecked(); - LineEditor* console = LineEditor::Get(); - printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); - console->Open(isolate); + printf("V8 version %s\n", V8::GetVersion()); while (true) { HandleScope inner_scope(isolate); - Local input = console->Prompt(Shell::kPrompt); + printf(" d8>"); +#if defined(__native_client__) + // Native Client libc is used to being embedded in Chrome and + // has trouble recognizing when to flush. + fflush(stdout); +#endif + Local input = Shell::ReadFromStdin(isolate); if (input.IsEmpty()) break; ExecuteString(isolate, input, name, true, true); } @@ -2434,7 +2382,6 @@ int Shell::Main(int argc, char* argv[]) { } #endif Isolate* isolate = Isolate::New(create_params); - DumbLineEditor dumb_line_editor(isolate); { Isolate::Scope scope(isolate); Initialize(isolate); diff --git a/src/d8.gyp b/src/d8.gyp index a4a1f8e862..473279b3c7 100644 --- a/src/d8.gyp +++ b/src/d8.gyp @@ -28,7 +28,6 @@ { 'variables': { 'v8_code': 1, - 'console%': '', # Enable support for Intel VTune. Supported on ia32/x64 only 'v8_enable_vtunejit%': 0, 'v8_enable_i18n_support%': 1, @@ -61,10 +60,6 @@ [ 'want_separate_host_toolset==1', { 'toolsets': [ '<(v8_toolset_for_d8)', ], }], - [ 'console=="readline"', { - 'libraries': [ '-lreadline', ], - 'sources': [ 'd8-readline.cc' ], - }], ['(OS=="linux" or OS=="mac" or OS=="freebsd" or OS=="netbsd" \ or OS=="openbsd" or OS=="solaris" or OS=="android" \ or OS=="qnx" or OS=="aix")', { diff --git a/src/d8.h b/src/d8.h index d63352e353..39c3b74931 100644 --- a/src/d8.h +++ b/src/d8.h @@ -93,26 +93,6 @@ class CounterMap { #endif // !V8_SHARED -class LineEditor { - public: - enum Type { DUMB = 0, READLINE = 1 }; - LineEditor(Type type, const char* name); - virtual ~LineEditor() { } - - virtual Local Prompt(const char* prompt) = 0; - virtual bool Open(Isolate* isolate) { return true; } - virtual bool Close() { return true; } - virtual void AddHistory(const char* str) { } - - const char* name() { return name_; } - static LineEditor* Get() { return current_; } - private: - Type type_; - const char* name_; - static LineEditor* current_; -}; - - class SourceGroup { public: SourceGroup() : @@ -380,8 +360,6 @@ class Shell : public i::AllStatic { const SerializationData& data, int* offset); static void CleanupWorkers(); - static Local GetCompletions(Isolate* isolate, Local text, - Local full); static int* LookupCounter(const char* name); static void* CreateHistogram(const char* name, int min, diff --git a/src/d8.js b/src/d8.js index 65646f7bf2..8d55c788e2 100644 --- a/src/d8.js +++ b/src/d8.js @@ -4,51 +4,6 @@ "use strict"; -String.prototype.startsWith = function (str) { - if (str.length > this.length) { - return false; - } - return this.substr(0, str.length) == str; -}; - -function ToInspectableObject(obj) { - if (!obj && typeof obj === 'object') { - return UNDEFINED; - } else { - return Object(obj); - } -} - -function GetCompletions(global, last, full) { - var full_tokens = full.split(); - full = full_tokens.pop(); - var parts = full.split('.'); - parts.pop(); - var current = global; - for (var i = 0; i < parts.length; i++) { - var part = parts[i]; - var next = current[part]; - if (!next) { - return []; - } - current = next; - } - var result = []; - current = ToInspectableObject(current); - while (typeof current !== 'undefined') { - var mirror = new $debug.ObjectMirror(current); - var properties = mirror.properties(); - for (var i = 0; i < properties.length; i++) { - var name = properties[i].name(); - if (typeof name === 'string' && name.startsWith(last)) { - result.push(name); - } - } - current = ToInspectableObject(Object.getPrototypeOf(current)); - } - return result; -} - // A more universal stringify that supports more types than JSON. // Used by the d8 shell to output results. var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects