blob: dc0232295d2f80c1cfdca11417746606d3c3067c [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 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
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "v8.h"
33#include "hashmap.h"
34
Ben Murdoch257744e2011-11-30 15:57:28 +000035#ifdef COMPRESS_STARTUP_DATA_BZ2
36#error Using compressed startup data is not supported for D8
37#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39namespace v8 {
40
41
42namespace i = v8::internal;
43
44
45// A single counter in a counter collection.
46class Counter {
47 public:
48 static const int kMaxNameSize = 64;
49 int32_t* Bind(const char* name, bool histogram);
50 int32_t* ptr() { return &count_; }
51 int32_t count() { return count_; }
52 int32_t sample_total() { return sample_total_; }
53 bool is_histogram() { return is_histogram_; }
54 void AddSample(int32_t sample);
55 private:
56 int32_t count_;
57 int32_t sample_total_;
58 bool is_histogram_;
59 uint8_t name_[kMaxNameSize];
60};
61
62
63// A set of counters and associated information. An instance of this
64// class is stored directly in the memory-mapped counters file if
65// the --map-counters options is used
66class CounterCollection {
67 public:
68 CounterCollection();
69 Counter* GetNextCounter();
70 private:
71 static const unsigned kMaxCounters = 256;
72 uint32_t magic_number_;
73 uint32_t max_counters_;
74 uint32_t max_name_size_;
75 uint32_t counters_in_use_;
76 Counter counters_[kMaxCounters];
77};
78
79
80class CounterMap {
81 public:
82 CounterMap(): hash_map_(Match) { }
83 Counter* Lookup(const char* name) {
84 i::HashMap::Entry* answer = hash_map_.Lookup(
85 const_cast<char*>(name),
86 Hash(name),
87 false);
88 if (!answer) return NULL;
89 return reinterpret_cast<Counter*>(answer->value);
90 }
91 void Set(const char* name, Counter* value) {
92 i::HashMap::Entry* answer = hash_map_.Lookup(
93 const_cast<char*>(name),
94 Hash(name),
95 true);
96 ASSERT(answer != NULL);
97 answer->value = value;
98 }
99 class Iterator {
100 public:
101 explicit Iterator(CounterMap* map)
102 : map_(&map->hash_map_), entry_(map_->Start()) { }
103 void Next() { entry_ = map_->Next(entry_); }
104 bool More() { return entry_ != NULL; }
105 const char* CurrentKey() { return static_cast<const char*>(entry_->key); }
106 Counter* CurrentValue() { return static_cast<Counter*>(entry_->value); }
107 private:
108 i::HashMap* map_;
109 i::HashMap::Entry* entry_;
110 };
111 private:
112 static int Hash(const char* name);
113 static bool Match(void* key1, void* key2);
114 i::HashMap hash_map_;
115};
116
117
118class Shell: public i::AllStatic {
119 public:
120 static bool ExecuteString(Handle<String> source,
121 Handle<Value> name,
122 bool print_result,
123 bool report_exceptions);
Steve Block6ded16b2010-05-10 14:33:55 +0100124 static const char* ToCString(const v8::String::Utf8Value& value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 static void ReportException(TryCatch* try_catch);
126 static void Initialize();
127 static void OnExit();
128 static int* LookupCounter(const char* name);
129 static void* CreateHistogram(const char* name,
130 int min,
131 int max,
132 size_t buckets);
133 static void AddHistogramSample(void* histogram, int sample);
134 static void MapCounters(const char* name);
135 static Handle<String> ReadFile(const char* name);
136 static void RunShell();
137 static int Main(int argc, char* argv[]);
138 static Handle<Array> GetCompletions(Handle<String> text,
139 Handle<String> full);
140#ifdef ENABLE_DEBUGGER_SUPPORT
141 static Handle<Object> DebugMessageDetails(Handle<String> message);
142 static Handle<Value> DebugCommandToJSONRequest(Handle<String> command);
143#endif
144
Ben Murdochb0fe1622011-05-05 13:52:32 +0100145#ifdef WIN32
146#undef Yield
147#endif
148
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 static Handle<Value> Print(const Arguments& args);
150 static Handle<Value> Write(const Arguments& args);
151 static Handle<Value> Yield(const Arguments& args);
152 static Handle<Value> Quit(const Arguments& args);
153 static Handle<Value> Version(const Arguments& args);
154 static Handle<Value> Read(const Arguments& args);
155 static Handle<Value> ReadLine(const Arguments& args);
156 static Handle<Value> Load(const Arguments& args);
157 // The OS object on the global object contains methods for performing
158 // operating system calls:
159 //
160 // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will
161 // run the command, passing the arguments to the program. The standard output
162 // of the program will be picked up and returned as a multiline string. If
163 // timeout1 is present then it should be a number. -1 indicates no timeout
164 // and a positive number is used as a timeout in milliseconds that limits the
165 // time spent waiting between receiving output characters from the program.
166 // timeout2, if present, should be a number indicating the limit in
167 // milliseconds on the total running time of the program. Exceptions are
168 // thrown on timeouts or other errors or if the exit status of the program
169 // indicates an error.
170 //
171 // os.chdir(dir) changes directory to the given directory. Throws an
172 // exception/ on error.
173 //
174 // os.setenv(variable, value) sets an environment variable. Repeated calls to
175 // this method leak memory due to the API of setenv in the standard C library.
176 //
177 // os.umask(alue) calls the umask system call and returns the old umask.
178 //
179 // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded
180 // with the current umask. Intermediate directories are created if necessary.
181 // An exception is not thrown if the directory already exists. Analogous to
182 // the "mkdir -p" command.
183 static Handle<Value> OSObject(const Arguments& args);
184 static Handle<Value> System(const Arguments& args);
185 static Handle<Value> ChangeDirectory(const Arguments& args);
186 static Handle<Value> SetEnvironment(const Arguments& args);
Steve Block6ded16b2010-05-10 14:33:55 +0100187 static Handle<Value> UnsetEnvironment(const Arguments& args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 static Handle<Value> SetUMask(const Arguments& args);
189 static Handle<Value> MakeDirectory(const Arguments& args);
190 static Handle<Value> RemoveDirectory(const Arguments& args);
191
192 static void AddOSMethods(Handle<ObjectTemplate> os_template);
193
194 static Handle<Context> utility_context() { return utility_context_; }
195
196 static const char* kHistoryFileName;
197 static const char* kPrompt;
198 private:
199 static Persistent<Context> utility_context_;
200 static Persistent<Context> evaluation_context_;
201 static CounterMap* counter_map_;
202 // We statically allocate a set of local counters to be used if we
203 // don't want to store the stats in a memory-mapped file
204 static CounterCollection local_counters_;
205 static CounterCollection* counters_;
206 static i::OS::MemoryMappedFile* counters_file_;
207 static Counter* GetCounter(const char* name, bool is_histogram);
208};
209
210
211class LineEditor {
212 public:
213 enum Type { DUMB = 0, READLINE = 1 };
214 LineEditor(Type type, const char* name);
215 virtual ~LineEditor() { }
216
217 virtual i::SmartPointer<char> Prompt(const char* prompt) = 0;
218 virtual bool Open() { return true; }
219 virtual bool Close() { return true; }
220 virtual void AddHistory(const char* str) { }
221
222 const char* name() { return name_; }
223 static LineEditor* Get();
224 private:
225 Type type_;
226 const char* name_;
227 LineEditor* next_;
228 static LineEditor* first_;
229};
230
231
232} // namespace v8
233
234
235#endif // V8_D8_H_