blob: 371e529859e17bf4f757fdcb9143638c670ea88d [file] [log] [blame]
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_D8_H_
29#define V8_D8_H_
30
31
32// Disable exceptions on windows to not generate warnings from <map>.
33#define _HAS_EXCEPTIONS 0
34#include <map>
35
36#include "v8.h"
37
38
39namespace v8 {
40
41
42namespace i = v8::internal;
43
44
ager@chromium.orga74f0da2008-12-03 16:05:52 +000045// A single counter in a counter collection.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000046class Counter {
47 public:
ager@chromium.orga74f0da2008-12-03 16:05:52 +000048 static const int kMaxNameSize = 64;
49 int32_t* Bind(const char* name);
50 int32_t* ptr() { return &counter_; }
51 int32_t value() { return counter_; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000052 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +000053 int32_t counter_;
54 uint8_t name_[kMaxNameSize];
55};
56
57
58// A set of counters and associated information. An instance of this
59// class is stored directly in the memory-mapped counters file if
60// the --map-counters options is used
61class CounterCollection {
62 public:
63 CounterCollection();
64 Counter* GetNextCounter();
65 private:
66 static const unsigned kMaxCounters = 256;
67 uint32_t magic_number_;
68 uint32_t max_counters_;
69 uint32_t max_name_size_;
70 uint32_t counters_in_use_;
71 Counter counters_[kMaxCounters];
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000072};
73
74
75class Shell: public i::AllStatic {
76 public:
77 static bool ExecuteString(Handle<String> source,
78 Handle<Value> name,
79 bool print_result,
80 bool report_exceptions);
81 static void ReportException(TryCatch* try_catch);
82 static void Initialize();
83 static void OnExit();
ager@chromium.orga74f0da2008-12-03 16:05:52 +000084 static int* LookupCounter(const char* name);
85 static void MapCounters(const char* name);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000086 static Handle<String> ReadFile(const char* name);
87 static void RunShell();
88 static int Main(int argc, char* argv[]);
89 static Handle<Array> GetCompletions(Handle<String> text,
90 Handle<String> full);
91
92 static Handle<Value> Print(const Arguments& args);
93 static Handle<Value> Quit(const Arguments& args);
94 static Handle<Value> Version(const Arguments& args);
95 static Handle<Value> Load(const Arguments& args);
96
97 static const char* kHistoryFileName;
98 static const char* kPrompt;
99 private:
100 static Persistent<Context> utility_context_;
101 static Persistent<Context> evaluation_context_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000102 typedef std::map<const char*, Counter*> CounterMap;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000103 static CounterMap counter_map_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000104 // We statically allocate a set of local counters to be used if we
105 // don't want to store the stats in a memory-mapped file
106 static CounterCollection local_counters_;
107 static CounterCollection* counters_;
108 static i::OS::MemoryMappedFile* counters_file_;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000109};
110
111
112class LineEditor {
113 public:
114 enum Type { DUMB = 0, READLINE = 1 };
115 LineEditor(Type type, const char* name);
116 virtual ~LineEditor() { }
117
118 virtual i::SmartPointer<char> Prompt(const char* prompt) = 0;
119 virtual bool Open() { return true; }
120 virtual bool Close() { return true; }
121 virtual void AddHistory(const char* str) { }
122
123 const char* name() { return name_; }
124 static LineEditor* Get();
125 private:
126 Type type_;
127 const char* name_;
128 LineEditor* next_;
129 static LineEditor* first_;
130};
131
132
133} // namespace v8
134
135
136#endif // V8_D8_H_