1 // Copyright 2012 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
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.
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.
32 #include "allocation.h"
34 #include "smart-array-pointer.h"
37 #include "../include/v8.h"
44 // A single counter in a counter collection.
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);
56 int32_t sample_total_;
58 uint8_t name_[kMaxNameSize];
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
65 class CounterCollection {
68 Counter* GetNextCounter();
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];
81 CounterMap(): hash_map_(Match) { }
82 Counter* Lookup(const char* name) {
83 i::HashMap::Entry* answer = hash_map_.Lookup(
84 const_cast<char*>(name),
87 if (!answer) return NULL;
88 return reinterpret_cast<Counter*>(answer->value);
90 void Set(const char* name, Counter* value) {
91 i::HashMap::Entry* answer = hash_map_.Lookup(
92 const_cast<char*>(name),
95 ASSERT(answer != NULL);
96 answer->value = value;
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); }
108 i::HashMap::Entry* entry_;
112 static int Hash(const char* name);
113 static bool Match(void* key1, void* key2);
114 i::HashMap hash_map_;
121 enum Type { DUMB = 0, READLINE = 1 };
122 LineEditor(Type type, const char* name);
123 virtual ~LineEditor() { }
125 virtual Handle<String> Prompt(const char* prompt) = 0;
126 virtual bool Open() { return true; }
127 virtual bool Close() { return true; }
128 virtual void AddHistory(const char* str) { }
130 const char* name() { return name_; }
131 static LineEditor* Get();
136 static LineEditor* first_;
144 next_semaphore_(v8::internal::OS::CreateSemaphore(0)),
145 done_semaphore_(v8::internal::OS::CreateSemaphore(0)),
154 void Begin(char** argv, int offset) {
155 argv_ = const_cast<const char**>(argv);
156 begin_offset_ = offset;
159 void End(int offset) { end_offset_ = offset; }
164 void StartExecuteInThread();
165 void WaitForThread();
168 class IsolateThread : public i::Thread {
170 explicit IsolateThread(SourceGroup* group)
171 : i::Thread(GetThreadOptions()), group_(group) {}
174 group_->ExecuteInThread();
181 static i::Thread::Options GetThreadOptions();
182 void ExecuteInThread();
184 i::Semaphore* next_semaphore_;
185 i::Semaphore* done_semaphore_;
189 void ExitShell(int exit_code);
190 Handle<String> ReadFile(const char* name);
198 class BinaryResource : public v8::String::ExternalAsciiStringResource {
200 BinaryResource(const char* string, int length)
210 virtual const char* data() const { return data_; }
211 virtual size_t length() const { return length_; }
223 use_preemption(true),
224 preemption_interval(10),
225 num_parallel_files(0),
226 parallel_files(NULL),
228 script_executed(false),
232 interactive_shell(false),
235 isolate_sources(NULL) { }
239 delete[] parallel_files;
241 delete[] isolate_sources;
246 int preemption_interval;
247 int num_parallel_files;
248 char** parallel_files;
250 bool script_executed;
254 bool interactive_shell;
257 SourceGroup* isolate_sources;
263 class Shell : public i::AllStatic {
267 static bool ExecuteString(Handle<String> source,
270 bool report_exceptions);
271 static const char* ToCString(const v8::String::Utf8Value& value);
272 static void ReportException(TryCatch* try_catch);
273 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[]);
277 static void Exit(int exit_code);
280 static Handle<Array> GetCompletions(Handle<String> text,
281 Handle<String> full);
282 static void OnExit();
283 static int* LookupCounter(const char* name);
284 static void* CreateHistogram(const char* name,
288 static void AddHistogramSample(void* histogram, int sample);
289 static void MapCounters(const char* name);
291 #ifdef ENABLE_DEBUGGER_SUPPORT
292 static Handle<Object> DebugMessageDetails(Handle<String> message);
293 static Handle<Value> DebugCommandToJSONRequest(Handle<String> command);
294 static void DispatchDebugMessages();
295 #endif // ENABLE_DEBUGGER_SUPPORT
302 static Handle<Value> Print(const Arguments& args);
303 static Handle<Value> Write(const Arguments& args);
304 static Handle<Value> Yield(const Arguments& args);
305 static Handle<Value> Quit(const Arguments& args);
306 static Handle<Value> Version(const Arguments& args);
307 static Handle<Value> EnableProfiler(const Arguments& args);
308 static Handle<Value> DisableProfiler(const Arguments& args);
309 static Handle<Value> Read(const Arguments& args);
310 static Handle<Value> ReadBinary(const Arguments& args);
311 static Handle<Value> ReadBuffer(const Arguments& args);
312 static Handle<String> ReadFromStdin();
313 static Handle<Value> ReadLine(const Arguments& args) {
314 return ReadFromStdin();
316 static Handle<Value> Load(const Arguments& args);
317 static Handle<Value> ArrayBuffer(const Arguments& args);
318 static Handle<Value> Int8Array(const Arguments& args);
319 static Handle<Value> Uint8Array(const Arguments& args);
320 static Handle<Value> Int16Array(const Arguments& args);
321 static Handle<Value> Uint16Array(const Arguments& args);
322 static Handle<Value> Int32Array(const Arguments& args);
323 static Handle<Value> Uint32Array(const Arguments& args);
324 static Handle<Value> Float32Array(const Arguments& args);
325 static Handle<Value> Float64Array(const Arguments& args);
326 static Handle<Value> PixelArray(const Arguments& args);
327 // The OS object on the global object contains methods for performing
328 // operating system calls:
330 // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will
331 // run the command, passing the arguments to the program. The standard output
332 // of the program will be picked up and returned as a multiline string. If
333 // timeout1 is present then it should be a number. -1 indicates no timeout
334 // and a positive number is used as a timeout in milliseconds that limits the
335 // time spent waiting between receiving output characters from the program.
336 // timeout2, if present, should be a number indicating the limit in
337 // milliseconds on the total running time of the program. Exceptions are
338 // thrown on timeouts or other errors or if the exit status of the program
339 // indicates an error.
341 // os.chdir(dir) changes directory to the given directory. Throws an
342 // exception/ on error.
344 // os.setenv(variable, value) sets an environment variable. Repeated calls to
345 // this method leak memory due to the API of setenv in the standard C library.
347 // os.umask(alue) calls the umask system call and returns the old umask.
349 // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded
350 // with the current umask. Intermediate directories are created if necessary.
351 // An exception is not thrown if the directory already exists. Analogous to
352 // the "mkdir -p" command.
353 static Handle<Value> OSObject(const Arguments& args);
354 static Handle<Value> System(const Arguments& args);
355 static Handle<Value> ChangeDirectory(const Arguments& args);
356 static Handle<Value> SetEnvironment(const Arguments& args);
357 static Handle<Value> UnsetEnvironment(const Arguments& args);
358 static Handle<Value> SetUMask(const Arguments& args);
359 static Handle<Value> MakeDirectory(const Arguments& args);
360 static Handle<Value> RemoveDirectory(const Arguments& args);
362 static void AddOSMethods(Handle<ObjectTemplate> os_template);
364 static LineEditor* console;
365 static const char* kPrompt;
366 static ShellOptions options;
369 static Persistent<Context> evaluation_context_;
371 static Persistent<Context> utility_context_;
372 static CounterMap* counter_map_;
373 // We statically allocate a set of local counters to be used if we
374 // don't want to store the stats in a memory-mapped file
375 static CounterCollection local_counters_;
376 static CounterCollection* counters_;
377 static i::OS::MemoryMappedFile* counters_file_;
378 static i::Mutex* context_mutex_;
380 static Counter* GetCounter(const char* name, bool is_histogram);
381 static void InstallUtilityScript();
383 static void Initialize();
384 static void RunShell();
385 static bool SetOptions(int argc, char* argv[]);
386 static Handle<ObjectTemplate> CreateGlobalTemplate();
387 static Handle<Value> CreateExternalArray(const Arguments& args,
388 ExternalArrayType type,
389 size_t element_size);
390 static void ExternalArrayWeakCallback(Persistent<Value> object, void* data);