Added development shell (d8) including readline support, counters and
authorchristian.plesner.hansen@gmail.com <christian.plesner.hansen@gmail.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 21 Oct 2008 09:12:27 +0000 (09:12 +0000)
committerchristian.plesner.hansen@gmail.com <christian.plesner.hansen@gmail.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 21 Oct 2008 09:12:27 +0000 (09:12 +0000)
completion.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@533 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

SConstruct
src/SConscript
src/d8-readline.cc [new file with mode: 0644]
src/d8.cc [new file with mode: 0644]
src/d8.h [new file with mode: 0644]
src/d8.js [new file with mode: 0644]
src/flag-definitions.h
src/natives.h
src/smart-pointer.h
tools/js2c.py

index 7789518..74aa56e 100644 (file)
@@ -216,6 +216,16 @@ SAMPLE_FLAGS = {
 }
 
 
+D8_FLAGS = {
+  'gcc': {
+    'console:readline': {
+      'LIBS': ['readline']
+    }
+  },
+  'msvc': { }
+}
+
+
 SUFFIXES = {
   'release': '',
   'debug': '_g'
@@ -312,6 +322,11 @@ SIMPLE_OPTIONS = {
     'values': ['MD5', 'timestamp'],
     'default': 'MD5',
     'help': 'set how the build system detects file changes'
+  },
+  'console': {
+    'values': ['dumb', 'readline'],
+    'default': 'dumb',
+    'help': 'the console to use for the d8 shell'
   }
 }
 
@@ -364,6 +379,7 @@ class BuildContext(object):
     self.library_targets = []
     self.cctest_targets = []
     self.sample_targets = []
+    self.d8_targets = []
     self.options = options
     self.env_overrides = env_overrides
     self.samples = samples
@@ -447,13 +463,15 @@ def BuildSpecific(env, mode, env_overrides):
   dtoa_flags = context.AddRelevantFlags(library_flags, DTOA_EXTRA_FLAGS)
   cctest_flags = context.AddRelevantFlags(v8_flags, CCTEST_EXTRA_FLAGS)
   sample_flags = context.AddRelevantFlags(os.environ, SAMPLE_FLAGS)
+  d8_flags = context.AddRelevantFlags(library_flags, D8_FLAGS)
 
   context.flags = {
     'v8': v8_flags,
     'jscre': jscre_flags,
     'dtoa': dtoa_flags,
     'cctest': cctest_flags,
-    'sample': sample_flags
+    'sample': sample_flags,
+    'd8': d8_flags
   }
 
   target_id = mode
@@ -462,13 +480,13 @@ def BuildSpecific(env, mode, env_overrides):
   env['LIBRARY'] = library_name
 
   # Build the object files by invoking SCons recursively.  
-  object_files = env.SConscript(
+  (object_files, shell_files) = env.SConscript(
     join('src', 'SConscript'),
     build_dir=join('obj', target_id),
     exports='context',
     duplicate=False
   )
-  
+
   # Link the object files into a library.
   env.Replace(**context.flags['v8'])
   context.ApplyEnvOverrides(env)
@@ -482,6 +500,11 @@ def BuildSpecific(env, mode, env_overrides):
     library = env.SharedLibrary(library_name, object_files, PDB=pdb_name)
   context.library_targets.append(library)
   
+  d8_env = Environment()
+  d8_env.Replace(**context.flags['d8'])
+  shell = d8_env.Program('d8' + suffix, object_files + shell_files)
+  context.d8_targets.append(shell)
+
   for sample in context.samples:
     sample_env = Environment(LIBRARY=library_name)
     sample_env.Replace(**context.flags['sample'])
@@ -514,22 +537,25 @@ def Build():
   Help(opts.GenerateHelpText(env))
   VerifyOptions(env)
   env_overrides = ParseEnvOverrides(env['env'])
-  
+
   SourceSignatures(env['sourcesignatures'])
-  
+
   libraries = []
   cctests = []
   samples = []
+  d8s = []
   modes = SplitList(env['mode'])
   for mode in modes:
     context = BuildSpecific(env.Copy(), mode, env_overrides)
     libraries += context.library_targets
     cctests += context.cctest_targets
     samples += context.sample_targets
+    d8s += context.d8_targets
 
   env.Alias('library', libraries)
   env.Alias('cctests', cctests)
   env.Alias('sample', samples)
+  env.Alias('d8', d8s)
   
   if env['sample']:
     env.Default('sample')
index 725f935..dd3357b 100644 (file)
@@ -64,6 +64,16 @@ SOURCES = {
 }
 
 
+D8_FILES = {
+  'all': [
+    'd8.cc'
+  ],
+  'console:readline': [
+    'd8-readline.cc'
+  ]
+}
+
+
 LIBRARY_FILES = '''
 runtime.js
 v8natives.js
@@ -104,11 +114,16 @@ def ConfigureObjectFiles():
   # Build the standard platform-independent source files.
   source_files = context.GetRelevantSources(SOURCES)
 
+  d8_files = context.GetRelevantSources(D8_FILES)
+  d8_js = env.JS2C('d8-js.cc', 'd8.js', TYPE='D8')
+  d8_js_obj = context.ConfigureObject(env, d8_js, CPPPATH=['.'])
+  d8_objs = [context.ConfigureObject(env, [d8_files]), d8_js_obj]
+
   # Combine the JavaScript library files into a single C++ file and
   # compile it.
   library_files = [s for s in LIBRARY_FILES]
   library_files.append('macros.py')
-  libraries_src, libraries_empty_src = env.JS2C(['libraries.cc', 'libraries-empty.cc'], library_files)
+  libraries_src, libraries_empty_src = env.JS2C(['libraries.cc', 'libraries-empty.cc'], library_files, TYPE='CORE')
   libraries_obj = context.ConfigureObject(env, libraries_src, CPPPATH=['.'])
 
   # Build JSCRE.
@@ -137,8 +152,9 @@ def ConfigureObjectFiles():
   else:
     snapshot_obj = empty_snapshot_obj
 
-  return [non_snapshot_files, libraries_obj, snapshot_obj]
+  library_objs = [non_snapshot_files, libraries_obj, snapshot_obj]
+  return (library_objs, d8_objs)
 
 
-library_objects = ConfigureObjectFiles()
-Return('library_objects')
+(library_objs, d8_objs) = ConfigureObjectFiles()
+Return('library_objs d8_objs')
diff --git a/src/d8-readline.cc b/src/d8-readline.cc
new file mode 100644 (file)
index 0000000..4cfee8b
--- /dev/null
@@ -0,0 +1,120 @@
+// Copyright 2008 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+#include <readline/readline.h>
+#include <readline/history.h>
+
+
+#include "d8.h"
+
+
+namespace v8 {
+
+
+class ReadLineEditor: public LineEditor {
+ public:
+  ReadLineEditor() : LineEditor(LineEditor::READLINE, "readline") { }
+  virtual i::SmartPointer<char> Prompt(const char* prompt);
+  virtual bool Open();
+  virtual bool Close();
+  virtual void AddHistory(const char* str);
+ private:
+  static char** AttemptedCompletion(const char* text, int start, int end);
+  static char* CompletionGenerator(const char* text, int state);
+  static char kWordBreakCharacters[];
+};
+
+
+static ReadLineEditor read_line_editor;
+char ReadLineEditor::kWordBreakCharacters[] = {' ', '\t', '\n', '"',
+    '\\', '\'', '`', '@', '.', '>', '<', '=', ';', '|', '&', '{', '(',
+    '\0'};
+
+
+bool ReadLineEditor::Open() {
+  rl_initialize();
+  rl_attempted_completion_function = AttemptedCompletion;
+  rl_completer_word_break_characters = kWordBreakCharacters;
+  rl_bind_key('\t', rl_complete);
+  using_history();
+  return read_history(Shell::kHistoryFileName) == 0;
+}
+
+
+bool ReadLineEditor::Close() {
+  return write_history(Shell::kHistoryFileName) == 0;
+}
+
+
+i::SmartPointer<char> ReadLineEditor::Prompt(const char* prompt) {
+  char* result = readline(prompt);
+  return i::SmartPointer<char>(result);
+}
+
+
+void ReadLineEditor::AddHistory(const char* str) {
+  add_history(str);
+}
+
+
+char** ReadLineEditor::AttemptedCompletion(const char* text,
+                                           int start,
+                                           int end) {
+  char** result = rl_completion_matches(text, CompletionGenerator);
+  rl_attempted_completion_over = true;
+  return result;
+}
+
+
+char* ReadLineEditor::CompletionGenerator(const char* text, int state) {
+  static unsigned current_index;
+  static Persistent<Array> current_completions;
+  if (state == 0) {
+    i::SmartPointer<char> full_text(strndup(rl_line_buffer, rl_point));
+    HandleScope scope;
+    Handle<Array> completions =
+      Shell::GetCompletions(String::New(text), String::New(*full_text));
+    current_completions = Persistent<Array>::New(completions);
+    current_index = 0;
+  }
+  if (current_index < current_completions->Length()) {
+    HandleScope scope;
+    Handle<Integer> index = Integer::New(current_index);
+    Handle<Value> str_obj = current_completions->Get(index);
+    current_index++;
+    String::Utf8Value str(str_obj);
+    return strdup(*str);
+  } else {
+    current_completions.Dispose();
+    current_completions.Clear();
+    return NULL;
+  }
+}
+
+
+}  // namespace v8
diff --git a/src/d8.cc b/src/d8.cc
new file mode 100644 (file)
index 0000000..e69e3d9
--- /dev/null
+++ b/src/d8.cc
@@ -0,0 +1,349 @@
+// Copyright 2008 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+#include "d8.h"
+#include "debug.h"
+#include "api.h"
+#include "natives.h"
+
+
+namespace v8 {
+
+
+const char* Shell::kHistoryFileName = ".d8_history";
+const char* Shell::kPrompt = "d8> ";
+
+
+LineEditor *LineEditor::first_ = NULL;
+
+
+LineEditor::LineEditor(Type type, const char* name)
+    : type_(type),
+      name_(name),
+      next_(first_) {
+  first_ = this;
+}
+
+
+LineEditor* LineEditor::Get() {
+  LineEditor* current = first_;
+  LineEditor* best = current;
+  while (current != NULL) {
+    if (current->type_ > best->type_)
+      best = current;
+    current = current->next_;
+  }
+  return best;
+}
+
+
+class DumbLineEditor: public LineEditor {
+ public:
+  DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { }
+  virtual i::SmartPointer<char> Prompt(const char* prompt);
+};
+
+
+static DumbLineEditor dumb_line_editor;
+
+
+i::SmartPointer<char> DumbLineEditor::Prompt(const char* prompt) {
+  static const int kBufferSize = 256;
+  char buffer[kBufferSize];
+  printf("%s", prompt);
+  char* str = fgets(buffer, kBufferSize, stdin);
+  return i::SmartPointer<char>(str ? i::OS::StrDup(str) : str);
+}
+
+
+Shell::CounterMap Shell::counter_map_;
+Persistent<Context> Shell::utility_context_;
+Persistent<Context> Shell::evaluation_context_;
+
+
+// Executes a string within the current v8 context.
+bool Shell::ExecuteString(Handle<String> source,
+                          Handle<Value> name,
+                          bool print_result,
+                          bool report_exceptions) {
+  HandleScope handle_scope;
+  TryCatch try_catch;
+  Handle<Script> script = Script::Compile(source, name);
+  if (script.IsEmpty()) {
+    // Print errors that happened during compilation.
+    if (report_exceptions)
+      ReportException(&try_catch);
+    return false;
+  } else {
+    Handle<Value> result = script->Run();
+    if (result.IsEmpty()) {
+      // Print errors that happened during execution.
+      if (report_exceptions)
+        ReportException(&try_catch);
+      return false;
+    } else {
+      if (print_result && !result->IsUndefined()) {
+        // If all went well and the result wasn't undefined then print
+        // the returned value.
+        String::Utf8Value str(result);
+        printf("%s\n", *str);
+      }
+      return true;
+    }
+  }
+}
+
+
+Handle<Value> Shell::Print(const Arguments& args) {
+  bool first = true;
+  for (int i = 0; i < args.Length(); i++) {
+    HandleScope handle_scope;
+    if (first) {
+      first = false;
+    } else {
+      printf(" ");
+    }
+    String::Utf8Value str(args[i]);
+    printf("%s", *str);
+  }
+  printf("\n");
+  return Undefined();
+}
+
+
+Handle<Value> Shell::Load(const Arguments& args) {
+  for (int i = 0; i < args.Length(); i++) {
+    HandleScope handle_scope;
+    String::Utf8Value file(args[i]);
+    Handle<String> source = ReadFile(*file);
+    if (source.IsEmpty()) {
+      return ThrowException(String::New("Error loading file"));
+    }
+    if (!ExecuteString(source, String::New(*file), false, false)) {
+      return ThrowException(String::New("Error executing  file"));
+    }
+  }
+  return Undefined();
+}
+
+
+Handle<Value> Shell::Quit(const Arguments& args) {
+  int exit_code = args[0]->Int32Value();
+  OnExit();
+  exit(exit_code);
+  return Undefined();
+}
+
+
+Handle<Value> Shell::Version(const Arguments& args) {
+  return String::New(V8::GetVersion());
+}
+
+
+void Shell::ReportException(v8::TryCatch* try_catch) {
+  HandleScope handle_scope;
+  String::Utf8Value exception(try_catch->Exception());
+  Handle<Message> message = try_catch->Message();
+  if (message.IsEmpty()) {
+    // V8 didn't provide any extra information about this error; just
+    // print the exception.
+    printf("%s\n", *exception);
+  } else {
+    // Print (filename):(line number): (message).
+    String::Utf8Value filename(message->GetScriptResourceName());
+    int linenum = message->GetLineNumber();
+    printf("%s:%i: %s\n", *filename, linenum, *exception);
+    // Print line of source code.
+    String::Utf8Value sourceline(message->GetSourceLine());
+    printf("%s\n", *sourceline);
+    // Print wavy underline (GetUnderline is deprecated).
+    int start = message->GetStartColumn();
+    for (int i = 0; i < start; i++) {
+      printf(" ");
+    }
+    int end = message->GetEndColumn();
+    for (int i = start; i < end; i++) {
+      printf("^");
+    }
+    printf("\n");
+  }
+}
+
+
+Handle<Array> Shell::GetCompletions(Handle<String> text, Handle<String> full) {
+  HandleScope handle_scope;
+  Context::Scope context_scope(utility_context_);
+  Handle<Object> global = utility_context_->Global();
+  Handle<Value> fun = global->Get(String::New("GetCompletions"));
+  static const int kArgc = 3;
+  Handle<Value> argv[kArgc] = { evaluation_context_->Global(), text, full };
+  Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
+  return handle_scope.Close(Handle<Array>::Cast(val));
+}
+
+
+int* Shell::LookupCounter(const wchar_t* name) {
+  CounterMap::iterator item = counter_map_.find(name);
+  if (item != counter_map_.end()) {
+    Counter* result = (*item).second;
+    return result->GetValuePtr();
+  }
+  Counter* result = new Counter(name);
+  counter_map_[name] = result;
+  return result->GetValuePtr();
+}
+
+
+void Shell::Initialize() {
+  // Set up counters
+  if (i::FLAG_dump_counters)
+    V8::SetCounterFunction(LookupCounter);
+  // Initialize the global objects
+  HandleScope scope;
+  Handle<ObjectTemplate> global_template = ObjectTemplate::New();
+  global_template->Set(String::New("print"), FunctionTemplate::New(Print));
+  global_template->Set(String::New("load"), FunctionTemplate::New(Load));
+  global_template->Set(String::New("quit"), FunctionTemplate::New(Quit));
+  global_template->Set(String::New("version"), FunctionTemplate::New(Version));
+
+  utility_context_ = Context::New(NULL, global_template);
+  utility_context_->SetSecurityToken(Undefined());
+  Context::Scope utility_scope(utility_context_);
+
+  // Install the debugger object in the utility scope
+  i::Debug::Load();
+  i::JSObject* raw_debug = i::Debug::debug_context()->global();
+  i::JSGlobalObject* debug = i::JSGlobalObject::cast(raw_debug);
+  debug->set_security_token(i::Heap::undefined_value());
+  utility_context_->Global()->Set(String::New("$debug"),
+                                  Utils::ToLocal(&raw_debug));
+
+  // Run the d8 shell utility script in the utility context
+  int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
+  i::Vector<const char> shell_source
+      = i::NativesCollection<i::D8>::GetScriptSource(source_index);
+  i::Vector<const char> shell_source_name
+      = i::NativesCollection<i::D8>::GetScriptName(source_index);
+  Handle<String> source = String::New(shell_source.start(),
+                                      shell_source.length());
+  Handle<String> name = String::New(shell_source_name.start(),
+                                    shell_source_name.length());
+  Script::Compile(source, name)->Run();
+
+  // Create the evaluation context
+  evaluation_context_ = Context::New(NULL, global_template);
+  evaluation_context_->SetSecurityToken(Undefined());
+}
+
+
+void Shell::OnExit() {
+  if (i::FLAG_dump_counters) {
+    ::printf("+----------------------------------------+----------+\n");
+    ::printf("| Name                                   | Value    |\n");
+    ::printf("+----------------------------------------+----------+\n");
+    for (CounterMap::iterator i = counter_map_.begin();
+         i != counter_map_.end();
+         i++) {
+      Counter* counter = (*i).second;
+      ::printf("| %-38ls | %8i |\n", counter->name(), counter->value());
+    }
+    ::printf("+----------------------------------------+----------+\n");
+  }
+}
+
+
+// Reads a file into a v8 string.
+Handle<String> Shell::ReadFile(const char* name) {
+  FILE* file = i::OS::FOpen(name, "rb");
+  if (file == NULL) return Handle<String>();
+
+  fseek(file, 0, SEEK_END);
+  int size = ftell(file);
+  rewind(file);
+
+  char* chars = new char[size + 1];
+  chars[size] = '\0';
+  for (int i = 0; i < size;) {
+    int read = fread(&chars[i], 1, size - i, file);
+    i += read;
+  }
+  fclose(file);
+  Handle<String> result = String::New(chars, size);
+  delete[] chars;
+  return result;
+}
+
+
+void Shell::RunShell() {
+  LineEditor* editor = LineEditor::Get();
+  printf("V8 version %s [console: %s]\n", V8::GetVersion(), editor->name());
+  editor->Open();
+  while (true) {
+    HandleScope handle_scope;
+    i::SmartPointer<char> input = editor->Prompt(Shell::kPrompt);
+    if (input.is_empty())
+      break;
+    editor->AddHistory(*input);
+    Handle<String> name = String::New("(d8)");
+    ExecuteString(String::New(*input), name, true, true);
+  }
+  editor->Close();
+  printf("\n");
+}
+
+
+int Shell::Main(int argc, char* argv[]) {
+  i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
+  Initialize();
+  bool run_shell = (argc == 1);
+  Context::Scope context_scope(evaluation_context_);
+  for (int i = 1; i < argc; i++) {
+    char* str = argv[i];
+    HandleScope handle_scope;
+    Handle<String> file_name = v8::String::New(str);
+    Handle<String> source = ReadFile(str);
+    if (source.IsEmpty()) {
+      printf("Error reading '%s'\n", str);
+      return 1;
+    }
+    if (!ExecuteString(source, file_name, false, true))
+      return 1;
+  }
+  if (run_shell)
+    RunShell();
+  OnExit();
+  return 0;
+}
+
+
+}  // namespace v8
+
+
+int main(int argc, char* argv[]) {
+  return v8::Shell::Main(argc, argv);
+}
diff --git a/src/d8.h b/src/d8.h
new file mode 100644 (file)
index 0000000..7e988c4
--- /dev/null
+++ b/src/d8.h
@@ -0,0 +1,113 @@
+// Copyright 2008 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef V8_D8_H_
+#define V8_D8_H_
+
+
+// Disable exceptions on windows to not generate warnings from <map>.
+#define _HAS_EXCEPTIONS 0
+#include <map>
+
+#include "v8.h"
+
+
+namespace v8 {
+
+
+namespace i = v8::internal;
+
+
+class Counter {
+ public:
+  explicit Counter(const wchar_t* name)
+    : name_(name), value_(0) { }
+  int* GetValuePtr() { return &value_; }
+  const wchar_t* name() { return name_; }
+  int value() { return value_; }
+ private:
+  const wchar_t* name_;
+  int value_;
+};
+
+
+class Shell: public i::AllStatic {
+ public:
+  static bool ExecuteString(Handle<String> source,
+                            Handle<Value> name,
+                            bool print_result,
+                            bool report_exceptions);
+  static void ReportException(TryCatch* try_catch);
+  static void Initialize();
+  static void OnExit();
+  static int* LookupCounter(const wchar_t* name);
+  static Handle<String> ReadFile(const char* name);
+  static void RunShell();
+  static int Main(int argc, char* argv[]);
+  static Handle<Array> GetCompletions(Handle<String> text,
+                                      Handle<String> full);
+
+  static Handle<Value> Print(const Arguments& args);
+  static Handle<Value> Quit(const Arguments& args);
+  static Handle<Value> Version(const Arguments& args);
+  static Handle<Value> Load(const Arguments& args);
+
+  static const char* kHistoryFileName;
+  static const char* kPrompt;
+ private:
+  static Persistent<Context> utility_context_;
+  static Persistent<Context> evaluation_context_;
+  typedef std::map<const wchar_t*, Counter*> CounterMap;
+  static CounterMap counter_map_;
+};
+
+
+class LineEditor {
+ public:
+  enum Type { DUMB = 0, READLINE = 1 };
+  LineEditor(Type type, const char* name);
+  virtual ~LineEditor() { }
+
+  virtual i::SmartPointer<char> Prompt(const char* prompt) = 0;
+  virtual bool Open() { return true; }
+  virtual bool Close() { return true; }
+  virtual void AddHistory(const char* str) { }
+
+  const char* name() { return name_; }
+  static LineEditor* Get();
+ private:
+  Type type_;
+  const char* name_;
+  LineEditor* next_;
+  static LineEditor* first_;
+};
+
+
+}  // namespace v8
+
+
+#endif  // V8_D8_H_
diff --git a/src/d8.js b/src/d8.js
new file mode 100644 (file)
index 0000000..cf8b60c
--- /dev/null
+++ b/src/d8.js
@@ -0,0 +1,70 @@
+// Copyright 2008 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// How crappy is it that I have to implement completely basic stuff
+// like this myself?  Answer: very.
+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 void 0;
+  } 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(current.__proto__);
+  }
+  return result;
+}
index 6508c77..c4de404 100644 (file)
@@ -191,7 +191,11 @@ DEFINE_string(testing_serialization_file, "/tmp/serdes",
               "file in which to serialize heap")
 #endif
 
+//
+// Dev shell flags
+//
 
+DEFINE_bool(dump_counters, false, "Dump counters on exit")
 
 //
 // Debug only flags
@@ -302,7 +306,6 @@ DEFINE_string(logfile, "v8.log", "Specify the name of the log file.")
 // codegen-ia32.cc / codegen-arm.cc
 DEFINE_bool(print_code, false, "print generated code")
 
-
 // Cleanup...
 #undef FLAG_FULL
 #undef FLAG_READONLY
index 0bb879f..3eb8090 100644 (file)
@@ -34,7 +34,12 @@ typedef bool (*NativeSourceCallback)(Vector<const char> name,
                                      Vector<const char> source,
                                      int index);
 
-class Natives {
+enum NativeType {
+  CORE, D8
+};
+
+template <NativeType type>
+class NativesCollection {
  public:
   // Number of built-in scripts.
   static int GetBuiltinsCount();
@@ -50,6 +55,8 @@ class Natives {
   static Vector<const char> GetScriptName(int index);
 };
 
+typedef NativesCollection<CORE> Natives;
+
 } }  // namespace v8::internal
 
 #endif  // V8_NATIVES_H_
index 03fd229..c39df16 100644 (file)
@@ -86,7 +86,7 @@ class SmartPointer {
   // the copy constructor it removes the pointer in the original to avoid
   // double freeing.
   inline SmartPointer& operator=(const SmartPointer<T>& rhs) {
-    ASSERT(p == NULL);
+    ASSERT(is_empty());
     T* tmp = rhs.p;  // swap to handle self-assignment
     const_cast<SmartPointer<T>&>(rhs).p = NULL;
     p = tmp;
@@ -94,6 +94,11 @@ class SmartPointer {
   }
 
 
+  inline bool is_empty() {
+    return p == NULL;
+  }
+
+
  private:
   T* p;
 };
index 181edb6..36903b4 100755 (executable)
@@ -200,25 +200,30 @@ namespace internal {
 
 %(source_lines)s\
 
-  int Natives::GetBuiltinsCount() {
+  template <>
+  int NativesCollection<%(type)s>::GetBuiltinsCount() {
     return %(builtin_count)i;
   }
 
-  int Natives::GetDelayCount() {
+  template <>
+  int NativesCollection<%(type)s>::GetDelayCount() {
     return %(delay_count)i;
   }
 
-  int Natives::GetIndex(const char* name) {
+  template <>
+  int NativesCollection<%(type)s>::GetIndex(const char* name) {
 %(get_index_cases)s\
     return -1;
   }
 
-  Vector<const char> Natives::GetScriptSource(int index) {
+  template <>
+  Vector<const char> NativesCollection<%(type)s>::GetScriptSource(int index) {
 %(get_script_source_cases)s\
     return Vector<const char>("", 0);
   }
 
-  Vector<const char> Natives::GetScriptName(int index) {
+  template <>
+  Vector<const char> NativesCollection<%(type)s>::GetScriptName(int index) {
 %(get_script_name_cases)s\
     return Vector<const char>("", 0);
   }
@@ -323,20 +328,24 @@ def JS2C(source, target, env):
     'source_lines': "\n".join(source_lines),
     'get_index_cases': "".join(get_index_cases),
     'get_script_source_cases': "".join(get_script_source_cases),
-    'get_script_name_cases': "".join(get_script_name_cases)
-  })
-  output.close()
-  output = open(str(target[1]), "w")
-  output.write(HEADER_TEMPLATE % {
-    'builtin_count': len(ids) + len(delay_ids),
-    'delay_count': len(delay_ids),
-    'source_lines': "\n".join(source_lines_empty),
-    'get_index_cases': "".join(get_index_cases),
-    'get_script_source_cases': "".join(get_script_source_cases),
-    'get_script_name_cases': "".join(get_script_name_cases)
+    'get_script_name_cases': "".join(get_script_name_cases),
+    'type': env['TYPE']
   })
   output.close()
 
+  if len(target) > 1:
+    output = open(str(target[1]), "w")
+    output.write(HEADER_TEMPLATE % {
+      'builtin_count': len(ids) + len(delay_ids),
+      'delay_count': len(delay_ids),
+      'source_lines': "\n".join(source_lines_empty),
+      'get_index_cases': "".join(get_index_cases),
+      'get_script_source_cases': "".join(get_script_source_cases),
+      'get_script_name_cases': "".join(get_script_name_cases),
+      'type': env['TYPE']
+    })
+    output.close()
+
 def main():
   natives = sys.argv[1]
   natives_empty = sys.argv[2]