blob: 15d8d5d50fc96214df64ce7925487bdd360c068a [file] [log] [blame]
Ben Murdoch589d6972011-11-30 16:04:58 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Murdoch69a99ed2011-11-30 16:03:39 +000031#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000032#include "allocation.h"
33#include "hashmap.h"
Ben Murdoch589d6972011-11-30 16:04:58 +000034#include "smart-array-pointer.h"
35#include "v8.h"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000036#else
37#include "../include/v8.h"
Ben Murdoch69a99ed2011-11-30 16:03:39 +000038#endif // V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40namespace v8 {
41
Steve Blocka7e24c12009-10-30 11:49:00 +000042
Ben Murdoch69a99ed2011-11-30 16:03:39 +000043#ifndef V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +000044// A single counter in a counter collection.
45class Counter {
46 public:
47 static const int kMaxNameSize = 64;
48 int32_t* Bind(const char* name, bool histogram);
49 int32_t* ptr() { return &count_; }
50 int32_t count() { return count_; }
51 int32_t sample_total() { return sample_total_; }
52 bool is_histogram() { return is_histogram_; }
53 void AddSample(int32_t sample);
54 private:
55 int32_t count_;
56 int32_t sample_total_;
57 bool is_histogram_;
58 uint8_t name_[kMaxNameSize];
59};
60
61
62// A set of counters and associated information. An instance of this
63// class is stored directly in the memory-mapped counters file if
64// the --map-counters options is used
65class CounterCollection {
66 public:
67 CounterCollection();
68 Counter* GetNextCounter();
69 private:
70 static const unsigned kMaxCounters = 256;
71 uint32_t magic_number_;
72 uint32_t max_counters_;
73 uint32_t max_name_size_;
74 uint32_t counters_in_use_;
75 Counter counters_[kMaxCounters];
76};
77
78
79class CounterMap {
80 public:
81 CounterMap(): hash_map_(Match) { }
82 Counter* Lookup(const char* name) {
83 i::HashMap::Entry* answer = hash_map_.Lookup(
84 const_cast<char*>(name),
85 Hash(name),
86 false);
87 if (!answer) return NULL;
88 return reinterpret_cast<Counter*>(answer->value);
89 }
90 void Set(const char* name, Counter* value) {
91 i::HashMap::Entry* answer = hash_map_.Lookup(
92 const_cast<char*>(name),
93 Hash(name),
94 true);
95 ASSERT(answer != NULL);
96 answer->value = value;
97 }
98 class Iterator {
99 public:
100 explicit Iterator(CounterMap* map)
101 : map_(&map->hash_map_), entry_(map_->Start()) { }
102 void Next() { entry_ = map_->Next(entry_); }
103 bool More() { return entry_ != NULL; }
104 const char* CurrentKey() { return static_cast<const char*>(entry_->key); }
105 Counter* CurrentValue() { return static_cast<Counter*>(entry_->value); }
106 private:
107 i::HashMap* map_;
108 i::HashMap::Entry* entry_;
109 };
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000110
Steve Blocka7e24c12009-10-30 11:49:00 +0000111 private:
112 static int Hash(const char* name);
113 static bool Match(void* key1, void* key2);
114 i::HashMap hash_map_;
115};
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000116#endif // V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +0000117
118
Ben Murdoch589d6972011-11-30 16:04:58 +0000119#ifndef V8_SHARED
120class LineEditor {
121 public:
122 enum Type { DUMB = 0, READLINE = 1 };
123 LineEditor(Type type, const char* name);
124 virtual ~LineEditor() { }
125
126 virtual i::SmartArrayPointer<char> Prompt(const char* prompt) = 0;
127 virtual bool Open() { return true; }
128 virtual bool Close() { return true; }
129 virtual void AddHistory(const char* str) { }
130
131 const char* name() { return name_; }
132 static LineEditor* Get();
133 private:
134 Type type_;
135 const char* name_;
136 LineEditor* next_;
137 static LineEditor* first_;
138};
139#endif // V8_SHARED
140
141
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000142class SourceGroup {
143 public:
144 SourceGroup() :
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000145#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000146 next_semaphore_(v8::internal::OS::CreateSemaphore(0)),
147 done_semaphore_(v8::internal::OS::CreateSemaphore(0)),
148 thread_(NULL),
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000149#endif // V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000150 argv_(NULL),
151 begin_offset_(0),
Ben Murdoch589d6972011-11-30 16:04:58 +0000152 end_offset_(0) {}
153
154 ~SourceGroup();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000155
156 void Begin(char** argv, int offset) {
157 argv_ = const_cast<const char**>(argv);
158 begin_offset_ = offset;
159 }
160
161 void End(int offset) { end_offset_ = offset; }
162
163 void Execute();
164
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000165#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000166 void StartExecuteInThread();
167 void WaitForThread();
168
169 private:
170 class IsolateThread : public i::Thread {
171 public:
172 explicit IsolateThread(SourceGroup* group)
173 : i::Thread(GetThreadOptions()), group_(group) {}
174
175 virtual void Run() {
176 group_->ExecuteInThread();
177 }
178
179 private:
180 SourceGroup* group_;
181 };
182
183 static i::Thread::Options GetThreadOptions();
184 void ExecuteInThread();
185
186 i::Semaphore* next_semaphore_;
187 i::Semaphore* done_semaphore_;
188 i::Thread* thread_;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000189#endif // V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000190
191 void ExitShell(int exit_code);
192 Handle<String> ReadFile(const char* name);
193
194 const char** argv_;
195 int begin_offset_;
196 int end_offset_;
197};
198
199
200class ShellOptions {
201 public:
202 ShellOptions() :
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000203#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000204 use_preemption(true),
205 preemption_interval(10),
Ben Murdoch589d6972011-11-30 16:04:58 +0000206 num_parallel_files(0),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000207 parallel_files(NULL),
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000208#endif // V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000209 script_executed(false),
210 last_run(true),
211 stress_opt(false),
212 stress_deopt(false),
213 interactive_shell(false),
214 test_shell(false),
215 num_isolates(1),
216 isolate_sources(NULL) { }
217
Ben Murdoch589d6972011-11-30 16:04:58 +0000218 ~ShellOptions() {
219#ifndef V8_SHARED
220 delete[] parallel_files;
221#endif // V8_SHARED
222 delete[] isolate_sources;
223 }
224
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000225#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000226 bool use_preemption;
227 int preemption_interval;
Ben Murdoch589d6972011-11-30 16:04:58 +0000228 int num_parallel_files;
229 char** parallel_files;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000230#endif // V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000231 bool script_executed;
232 bool last_run;
233 bool stress_opt;
234 bool stress_deopt;
235 bool interactive_shell;
236 bool test_shell;
237 int num_isolates;
238 SourceGroup* isolate_sources;
239};
240
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000241#ifdef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000242class Shell {
243#else
244class Shell : public i::AllStatic {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000245#endif // V8_SHARED
Ben Murdoch589d6972011-11-30 16:04:58 +0000246
Steve Blocka7e24c12009-10-30 11:49:00 +0000247 public:
248 static bool ExecuteString(Handle<String> source,
249 Handle<Value> name,
250 bool print_result,
251 bool report_exceptions);
Steve Block6ded16b2010-05-10 14:33:55 +0100252 static const char* ToCString(const v8::String::Utf8Value& value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 static void ReportException(TryCatch* try_catch);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000254 static Handle<String> ReadFile(const char* name);
255 static Persistent<Context> CreateEvaluationContext();
256 static int RunMain(int argc, char* argv[]);
257 static int Main(int argc, char* argv[]);
Ben Murdoch589d6972011-11-30 16:04:58 +0000258 static void Exit(int exit_code);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000259
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000260#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000261 static Handle<Array> GetCompletions(Handle<String> text,
262 Handle<String> full);
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 static void OnExit();
264 static int* LookupCounter(const char* name);
265 static void* CreateHistogram(const char* name,
266 int min,
267 int max,
268 size_t buckets);
269 static void AddHistogramSample(void* histogram, int sample);
270 static void MapCounters(const char* name);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000271#endif // V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000272
Steve Blocka7e24c12009-10-30 11:49:00 +0000273#ifdef ENABLE_DEBUGGER_SUPPORT
274 static Handle<Object> DebugMessageDetails(Handle<String> message);
275 static Handle<Value> DebugCommandToJSONRequest(Handle<String> command);
276#endif
277
Ben Murdochb0fe1622011-05-05 13:52:32 +0100278#ifdef WIN32
279#undef Yield
280#endif
281
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 static Handle<Value> Print(const Arguments& args);
283 static Handle<Value> Write(const Arguments& args);
284 static Handle<Value> Yield(const Arguments& args);
285 static Handle<Value> Quit(const Arguments& args);
286 static Handle<Value> Version(const Arguments& args);
Ben Murdoch589d6972011-11-30 16:04:58 +0000287 static Handle<Value> EnableProfiler(const Arguments& args);
288 static Handle<Value> DisableProfiler(const Arguments& args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 static Handle<Value> Read(const Arguments& args);
290 static Handle<Value> ReadLine(const Arguments& args);
291 static Handle<Value> Load(const Arguments& args);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000292 static Handle<Value> Int8Array(const Arguments& args);
293 static Handle<Value> Uint8Array(const Arguments& args);
294 static Handle<Value> Int16Array(const Arguments& args);
295 static Handle<Value> Uint16Array(const Arguments& args);
296 static Handle<Value> Int32Array(const Arguments& args);
297 static Handle<Value> Uint32Array(const Arguments& args);
298 static Handle<Value> Float32Array(const Arguments& args);
299 static Handle<Value> Float64Array(const Arguments& args);
300 static Handle<Value> PixelArray(const Arguments& args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000301 // The OS object on the global object contains methods for performing
302 // operating system calls:
303 //
304 // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will
305 // run the command, passing the arguments to the program. The standard output
306 // of the program will be picked up and returned as a multiline string. If
307 // timeout1 is present then it should be a number. -1 indicates no timeout
308 // and a positive number is used as a timeout in milliseconds that limits the
309 // time spent waiting between receiving output characters from the program.
310 // timeout2, if present, should be a number indicating the limit in
311 // milliseconds on the total running time of the program. Exceptions are
312 // thrown on timeouts or other errors or if the exit status of the program
313 // indicates an error.
314 //
315 // os.chdir(dir) changes directory to the given directory. Throws an
316 // exception/ on error.
317 //
318 // os.setenv(variable, value) sets an environment variable. Repeated calls to
319 // this method leak memory due to the API of setenv in the standard C library.
320 //
321 // os.umask(alue) calls the umask system call and returns the old umask.
322 //
323 // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded
324 // with the current umask. Intermediate directories are created if necessary.
325 // An exception is not thrown if the directory already exists. Analogous to
326 // the "mkdir -p" command.
327 static Handle<Value> OSObject(const Arguments& args);
328 static Handle<Value> System(const Arguments& args);
329 static Handle<Value> ChangeDirectory(const Arguments& args);
330 static Handle<Value> SetEnvironment(const Arguments& args);
Steve Block6ded16b2010-05-10 14:33:55 +0100331 static Handle<Value> UnsetEnvironment(const Arguments& args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 static Handle<Value> SetUMask(const Arguments& args);
333 static Handle<Value> MakeDirectory(const Arguments& args);
334 static Handle<Value> RemoveDirectory(const Arguments& args);
335
336 static void AddOSMethods(Handle<ObjectTemplate> os_template);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000337#ifndef V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 static const char* kHistoryFileName;
Ben Murdoch589d6972011-11-30 16:04:58 +0000339 static const int kMaxHistoryEntries;
340 static LineEditor* console;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000341#endif // V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 static const char* kPrompt;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000343 static ShellOptions options;
344
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 static Persistent<Context> evaluation_context_;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000347#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000348 static Persistent<Context> utility_context_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000349 static CounterMap* counter_map_;
350 // We statically allocate a set of local counters to be used if we
351 // don't want to store the stats in a memory-mapped file
352 static CounterCollection local_counters_;
353 static CounterCollection* counters_;
354 static i::OS::MemoryMappedFile* counters_file_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000355 static i::Mutex* context_mutex_;
356
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 static Counter* GetCounter(const char* name, bool is_histogram);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000358 static void InstallUtilityScript();
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000359#endif // V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000360 static void Initialize();
361 static void RunShell();
362 static bool SetOptions(int argc, char* argv[]);
363 static Handle<ObjectTemplate> CreateGlobalTemplate();
364 static Handle<Value> CreateExternalArray(const Arguments& args,
365 ExternalArrayType type,
366 size_t element_size);
367 static void ExternalArrayWeakCallback(Persistent<Value> object, void* data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000368};
369
370
Steve Blocka7e24c12009-10-30 11:49:00 +0000371} // namespace v8
372
373
374#endif // V8_D8_H_