blob: be53f991d636bb6d178945daf82c2617677df468 [file] [log] [blame]
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001// Copyright 2012 the V8 project authors. All rights reserved.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +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
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000031#ifndef V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000032#include "allocation.h"
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +000033#include "hashmap.h"
kmillikin@chromium.org83e16822011-09-13 08:21:47 +000034#include "smart-array-pointer.h"
ricow@chromium.org55ee8072011-09-08 16:33:10 +000035#include "v8.h"
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000036#else
37#include "../include/v8.h"
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000038#endif // V8_SHARED
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000039
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000040namespace v8 {
41
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000042
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000043#ifndef V8_SHARED
ager@chromium.orga74f0da2008-12-03 16:05:52 +000044// A single counter in a counter collection.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000045class Counter {
46 public:
ager@chromium.orga74f0da2008-12-03 16:05:52 +000047 static const int kMaxNameSize = 64;
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000048 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);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000054 private:
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +000055 int32_t count_;
56 int32_t sample_total_;
57 bool is_histogram_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000058 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];
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000076};
77
78
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +000079class 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 };
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000110
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000111 private:
112 static int Hash(const char* name);
113 static bool Match(void* key1, void* key2);
114 i::HashMap hash_map_;
115};
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000116#endif // V8_SHARED
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000117
118
lrn@chromium.org34e60782011-09-15 07:25:40 +0000119class LineEditor {
120 public:
121 enum Type { DUMB = 0, READLINE = 1 };
122 LineEditor(Type type, const char* name);
123 virtual ~LineEditor() { }
124
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000125 virtual Handle<String> Prompt(const char* prompt) = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000126 virtual bool Open() { return true; }
127 virtual bool Close() { return true; }
128 virtual void AddHistory(const char* str) { }
129
130 const char* name() { return name_; }
131 static LineEditor* Get();
132 private:
133 Type type_;
134 const char* name_;
135 LineEditor* next_;
136 static LineEditor* first_;
137};
lrn@chromium.org34e60782011-09-15 07:25:40 +0000138
139
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000140class SourceGroup {
141 public:
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000142 SourceGroup() :
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000143#ifndef V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000144 next_semaphore_(v8::internal::OS::CreateSemaphore(0)),
145 done_semaphore_(v8::internal::OS::CreateSemaphore(0)),
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000146 thread_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000147#endif // V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000148 argv_(NULL),
149 begin_offset_(0),
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000150 end_offset_(0) {}
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000151
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000152 ~SourceGroup();
153
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000154 void Begin(char** argv, int offset) {
155 argv_ = const_cast<const char**>(argv);
156 begin_offset_ = offset;
157 }
158
159 void End(int offset) { end_offset_ = offset; }
160
161 void Execute();
162
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000163#ifndef V8_SHARED
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000164 void StartExecuteInThread();
165 void WaitForThread();
166
167 private:
168 class IsolateThread : public i::Thread {
169 public:
170 explicit IsolateThread(SourceGroup* group)
171 : i::Thread(GetThreadOptions()), group_(group) {}
172
173 virtual void Run() {
174 group_->ExecuteInThread();
175 }
176
177 private:
178 SourceGroup* group_;
179 };
180
181 static i::Thread::Options GetThreadOptions();
182 void ExecuteInThread();
183
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000184 i::Semaphore* next_semaphore_;
185 i::Semaphore* done_semaphore_;
186 i::Thread* thread_;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000187#endif // V8_SHARED
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000188
189 void ExitShell(int exit_code);
190 Handle<String> ReadFile(const char* name);
191
192 const char** argv_;
193 int begin_offset_;
194 int end_offset_;
195};
196
197
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000198class BinaryResource : public v8::String::ExternalAsciiStringResource {
199 public:
200 BinaryResource(const char* string, int length)
201 : data_(string),
202 length_(length) { }
203
204 ~BinaryResource() {
205 delete[] data_;
206 data_ = NULL;
207 length_ = 0;
208 }
209
210 virtual const char* data() const { return data_; }
211 virtual size_t length() const { return length_; }
212
213 private:
214 const char* data_;
215 size_t length_;
216};
217
218
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000219class ShellOptions {
220 public:
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000221 ShellOptions() :
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000222#ifndef V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000223 use_preemption(true),
224 preemption_interval(10),
lrn@chromium.org34e60782011-09-15 07:25:40 +0000225 num_parallel_files(0),
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000226 parallel_files(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000227#endif // V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000228 script_executed(false),
229 last_run(true),
230 stress_opt(false),
231 stress_deopt(false),
232 interactive_shell(false),
233 test_shell(false),
234 num_isolates(1),
235 isolate_sources(NULL) { }
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000236
lrn@chromium.org34e60782011-09-15 07:25:40 +0000237 ~ShellOptions() {
238#ifndef V8_SHARED
239 delete[] parallel_files;
240#endif // V8_SHARED
241 delete[] isolate_sources;
242 }
243
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000244#ifndef V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000245 bool use_preemption;
246 int preemption_interval;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000247 int num_parallel_files;
248 char** parallel_files;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000249#endif // V8_SHARED
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000250 bool script_executed;
251 bool last_run;
252 bool stress_opt;
253 bool stress_deopt;
254 bool interactive_shell;
255 bool test_shell;
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000256 int num_isolates;
257 SourceGroup* isolate_sources;
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000258};
259
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000260#ifdef V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000261class Shell {
262#else
263class Shell : public i::AllStatic {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000264#endif // V8_SHARED
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000265
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000266 public:
267 static bool ExecuteString(Handle<String> source,
268 Handle<Value> name,
269 bool print_result,
270 bool report_exceptions);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000271 static const char* ToCString(const v8::String::Utf8Value& value);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000272 static void ReportException(TryCatch* try_catch);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000273 static Handle<String> ReadFile(const char* name);
274 static Persistent<Context> CreateEvaluationContext();
275 static int RunMain(int argc, char* argv[]);
276 static int Main(int argc, char* argv[]);
lrn@chromium.org34e60782011-09-15 07:25:40 +0000277 static void Exit(int exit_code);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000278
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000279#ifndef V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000280 static Handle<Array> GetCompletions(Handle<String> text,
281 Handle<String> full);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000282 static void OnExit();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000283 static int* LookupCounter(const char* name);
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000284 static void* CreateHistogram(const char* name,
285 int min,
286 int max,
287 size_t buckets);
288 static void AddHistogramSample(void* histogram, int sample);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000289 static void MapCounters(const char* name);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000290
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000291#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000292 static Handle<Object> DebugMessageDetails(Handle<String> message);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000293 static Handle<Value> DebugCommandToJSONRequest(Handle<String> command);
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +0000294 static void DispatchDebugMessages();
295#endif // ENABLE_DEBUGGER_SUPPORT
296#endif // V8_SHARED
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000297
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000298#ifdef WIN32
299#undef Yield
300#endif
301
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000302 static Handle<Value> Print(const Arguments& args);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000303 static Handle<Value> Write(const Arguments& args);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000304 static Handle<Value> Yield(const Arguments& args);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000305 static Handle<Value> Quit(const Arguments& args);
306 static Handle<Value> Version(const Arguments& args);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000307 static Handle<Value> EnableProfiler(const Arguments& args);
308 static Handle<Value> DisableProfiler(const Arguments& args);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000309 static Handle<Value> Read(const Arguments& args);
yangguo@chromium.orgcb9affa2012-05-15 12:16:38 +0000310 static Handle<Value> ReadBuffer(const Arguments& args);
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000311 static Handle<String> ReadFromStdin();
312 static Handle<Value> ReadLine(const Arguments& args) {
313 return ReadFromStdin();
314 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000315 static Handle<Value> Load(const Arguments& args);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000316 static Handle<Value> ArrayBuffer(const Arguments& args);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000317 static Handle<Value> Int8Array(const Arguments& args);
318 static Handle<Value> Uint8Array(const Arguments& args);
319 static Handle<Value> Int16Array(const Arguments& args);
320 static Handle<Value> Uint16Array(const Arguments& args);
321 static Handle<Value> Int32Array(const Arguments& args);
322 static Handle<Value> Uint32Array(const Arguments& args);
323 static Handle<Value> Float32Array(const Arguments& args);
324 static Handle<Value> Float64Array(const Arguments& args);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000325 static Handle<Value> Uint8ClampedArray(const Arguments& args);
svenpanne@chromium.org619781a2012-07-05 08:22:44 +0000326 static Handle<Value> ArrayBufferSlice(const Arguments& args);
327 static Handle<Value> ArraySubArray(const Arguments& args);
328 static Handle<Value> ArraySet(const Arguments& args);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000329 // The OS object on the global object contains methods for performing
330 // operating system calls:
331 //
332 // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will
333 // run the command, passing the arguments to the program. The standard output
334 // of the program will be picked up and returned as a multiline string. If
ager@chromium.org41826e72009-03-30 13:30:57 +0000335 // timeout1 is present then it should be a number. -1 indicates no timeout
336 // and a positive number is used as a timeout in milliseconds that limits the
337 // time spent waiting between receiving output characters from the program.
338 // timeout2, if present, should be a number indicating the limit in
339 // milliseconds on the total running time of the program. Exceptions are
340 // thrown on timeouts or other errors or if the exit status of the program
341 // indicates an error.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000342 //
343 // os.chdir(dir) changes directory to the given directory. Throws an
344 // exception/ on error.
345 //
346 // os.setenv(variable, value) sets an environment variable. Repeated calls to
347 // this method leak memory due to the API of setenv in the standard C library.
348 //
349 // os.umask(alue) calls the umask system call and returns the old umask.
350 //
351 // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded
352 // with the current umask. Intermediate directories are created if necessary.
353 // An exception is not thrown if the directory already exists. Analogous to
354 // the "mkdir -p" command.
355 static Handle<Value> OSObject(const Arguments& args);
ager@chromium.org41826e72009-03-30 13:30:57 +0000356 static Handle<Value> System(const Arguments& args);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000357 static Handle<Value> ChangeDirectory(const Arguments& args);
358 static Handle<Value> SetEnvironment(const Arguments& args);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000359 static Handle<Value> UnsetEnvironment(const Arguments& args);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000360 static Handle<Value> SetUMask(const Arguments& args);
361 static Handle<Value> MakeDirectory(const Arguments& args);
362 static Handle<Value> RemoveDirectory(const Arguments& args);
363
364 static void AddOSMethods(Handle<ObjectTemplate> os_template);
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000365
lrn@chromium.org34e60782011-09-15 07:25:40 +0000366 static LineEditor* console;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000367 static const char* kPrompt;
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000368 static ShellOptions options;
369
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000370 private:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000371 static Persistent<Context> evaluation_context_;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000372#ifndef V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000373 static Persistent<Context> utility_context_;
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000374 static CounterMap* counter_map_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000375 // We statically allocate a set of local counters to be used if we
376 // don't want to store the stats in a memory-mapped file
377 static CounterCollection local_counters_;
378 static CounterCollection* counters_;
379 static i::OS::MemoryMappedFile* counters_file_;
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000380 static i::Mutex* context_mutex_;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000381
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000382 static Counter* GetCounter(const char* name, bool is_histogram);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000383 static void InstallUtilityScript();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000384#endif // V8_SHARED
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000385 static void Initialize();
386 static void RunShell();
387 static bool SetOptions(int argc, char* argv[]);
388 static Handle<ObjectTemplate> CreateGlobalTemplate();
svenpanne@chromium.org619781a2012-07-05 08:22:44 +0000389 static Handle<FunctionTemplate> CreateArrayBufferTemplate(InvocationCallback);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000390 static Handle<FunctionTemplate> CreateArrayTemplate(InvocationCallback);
391 static Handle<Value> CreateExternalArrayBuffer(Handle<Object> buffer,
392 int32_t size);
393 static Handle<Object> CreateExternalArray(Handle<Object> array,
394 Handle<Object> buffer,
395 ExternalArrayType type,
396 int32_t length,
397 int32_t byteLength,
398 int32_t byteOffset,
399 int32_t element_size);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000400 static Handle<Value> CreateExternalArray(const Arguments& args,
401 ExternalArrayType type,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000402 int32_t element_size);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000403 static void ExternalArrayWeakCallback(Persistent<Value> object, void* data);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000404};
405
406
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000407} // namespace v8
408
409
410#endif // V8_D8_H_