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-pointers.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 = 512;
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(Isolate* isolate) { 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() { return current_; }
135 static LineEditor* current_;
153 void Begin(char** argv, int offset) {
154 argv_ = const_cast<const char**>(argv);
155 begin_offset_ = offset;
158 void End(int offset) { end_offset_ = offset; }
160 void Execute(Isolate* isolate);
163 void StartExecuteInThread();
164 void WaitForThread();
167 class IsolateThread : public i::Thread {
169 explicit IsolateThread(SourceGroup* group)
170 : i::Thread(GetThreadOptions()), group_(group) {}
173 group_->ExecuteInThread();
180 static i::Thread::Options GetThreadOptions();
181 void ExecuteInThread();
183 i::Semaphore next_semaphore_;
184 i::Semaphore done_semaphore_;
188 void ExitShell(int exit_code);
189 Handle<String> ReadFile(Isolate* isolate, const char* name);
197 class BinaryResource : public v8::String::ExternalAsciiStringResource {
199 BinaryResource(const char* string, int length)
209 virtual const char* data() const { return data_; }
210 virtual size_t length() const { return length_; }
222 num_parallel_files(0),
223 parallel_files(NULL),
225 script_executed(false),
227 send_idle_notification(false),
230 interactive_shell(false),
232 dump_heap_constants(false),
233 expected_to_throw(false),
234 mock_arraybuffer_allocator(false),
236 isolate_sources(NULL) { }
240 delete[] parallel_files;
242 delete[] isolate_sources;
246 int num_parallel_files;
247 char** parallel_files;
249 bool script_executed;
251 bool send_idle_notification;
254 bool interactive_shell;
256 bool dump_heap_constants;
257 bool expected_to_throw;
258 bool mock_arraybuffer_allocator;
260 SourceGroup* isolate_sources;
266 class Shell : public i::AllStatic {
270 static bool ExecuteString(Isolate* isolate,
271 Handle<String> source,
274 bool report_exceptions);
275 static const char* ToCString(const v8::String::Utf8Value& value);
276 static void ReportException(Isolate* isolate, TryCatch* try_catch);
277 static Handle<String> ReadFile(Isolate* isolate, const char* name);
278 static Local<Context> CreateEvaluationContext(Isolate* isolate);
279 static int RunMain(Isolate* isolate, int argc, char* argv[]);
280 static int Main(int argc, char* argv[]);
281 static void Exit(int exit_code);
282 static void OnExit();
285 static Handle<Array> GetCompletions(Isolate* isolate,
287 Handle<String> full);
288 static int* LookupCounter(const char* name);
289 static void* CreateHistogram(const char* name,
293 static void AddHistogramSample(void* histogram, int sample);
294 static void MapCounters(const char* name);
296 #ifdef ENABLE_DEBUGGER_SUPPORT
297 static Local<Object> DebugMessageDetails(Isolate* isolate,
298 Handle<String> message);
299 static Local<Value> DebugCommandToJSONRequest(Isolate* isolate,
300 Handle<String> command);
301 static void DispatchDebugMessages();
302 #endif // ENABLE_DEBUGGER_SUPPORT
304 static void PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args);
307 static void RealmCurrent(const v8::FunctionCallbackInfo<v8::Value>& args);
308 static void RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args);
309 static void RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args);
310 static void RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args);
311 static void RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
312 static void RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args);
313 static void RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args);
314 static void RealmSharedGet(Local<String> property,
315 const PropertyCallbackInfo<Value>& info);
316 static void RealmSharedSet(Local<String> property,
318 const PropertyCallbackInfo<void>& info);
320 static void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
321 static void Write(const v8::FunctionCallbackInfo<v8::Value>& args);
322 static void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
323 static void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
324 static void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
325 static void ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
326 static Handle<String> ReadFromStdin(Isolate* isolate);
327 static void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) {
328 args.GetReturnValue().Set(ReadFromStdin(args.GetIsolate()));
330 static void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
331 static void ArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
332 static void Int8Array(const v8::FunctionCallbackInfo<v8::Value>& args);
333 static void Uint8Array(const v8::FunctionCallbackInfo<v8::Value>& args);
334 static void Int16Array(const v8::FunctionCallbackInfo<v8::Value>& args);
335 static void Uint16Array(const v8::FunctionCallbackInfo<v8::Value>& args);
336 static void Int32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
337 static void Uint32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
338 static void Float32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
339 static void Float32x4Array(const v8::FunctionCallbackInfo<v8::Value>& args);
340 static void Int32x4Array(const v8::FunctionCallbackInfo<v8::Value>& args);
341 static void Float64Array(const v8::FunctionCallbackInfo<v8::Value>& args);
342 static void Uint8ClampedArray(
343 const v8::FunctionCallbackInfo<v8::Value>& args);
344 static void ArrayBufferSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
345 static void ArraySubArray(const v8::FunctionCallbackInfo<v8::Value>& args);
346 static void ArraySet(const v8::FunctionCallbackInfo<v8::Value>& args);
347 // The OS object on the global object contains methods for performing
348 // operating system calls:
350 // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will
351 // run the command, passing the arguments to the program. The standard output
352 // of the program will be picked up and returned as a multiline string. If
353 // timeout1 is present then it should be a number. -1 indicates no timeout
354 // and a positive number is used as a timeout in milliseconds that limits the
355 // time spent waiting between receiving output characters from the program.
356 // timeout2, if present, should be a number indicating the limit in
357 // milliseconds on the total running time of the program. Exceptions are
358 // thrown on timeouts or other errors or if the exit status of the program
359 // indicates an error.
361 // os.chdir(dir) changes directory to the given directory. Throws an
362 // exception/ on error.
364 // os.setenv(variable, value) sets an environment variable. Repeated calls to
365 // this method leak memory due to the API of setenv in the standard C library.
367 // os.umask(alue) calls the umask system call and returns the old umask.
369 // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded
370 // with the current umask. Intermediate directories are created if necessary.
371 // An exception is not thrown if the directory already exists. Analogous to
372 // the "mkdir -p" command.
373 static void OSObject(const v8::FunctionCallbackInfo<v8::Value>& args);
374 static void System(const v8::FunctionCallbackInfo<v8::Value>& args);
375 static void ChangeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
376 static void SetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args);
377 static void UnsetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args);
378 static void SetUMask(const v8::FunctionCallbackInfo<v8::Value>& args);
379 static void MakeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
380 static void RemoveDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
382 static void AddOSMethods(v8::Isolate* isolate,
383 Handle<ObjectTemplate> os_template);
385 static const char* kPrompt;
386 static ShellOptions options;
389 static Persistent<Context> evaluation_context_;
391 static Persistent<Context> utility_context_;
392 static CounterMap* counter_map_;
393 // We statically allocate a set of local counters to be used if we
394 // don't want to store the stats in a memory-mapped file
395 static CounterCollection local_counters_;
396 static CounterCollection* counters_;
397 static i::OS::MemoryMappedFile* counters_file_;
398 static i::Mutex context_mutex_;
399 static const i::TimeTicks kInitialTicks;
401 static Counter* GetCounter(const char* name, bool is_histogram);
402 static void InstallUtilityScript(Isolate* isolate);
404 static void Initialize(Isolate* isolate);
405 static void InitializeDebugger(Isolate* isolate);
406 static void RunShell(Isolate* isolate);
407 static bool SetOptions(int argc, char* argv[]);
408 static Handle<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate);
409 static Handle<FunctionTemplate> CreateArrayBufferTemplate(FunctionCallback);
410 static Handle<FunctionTemplate> CreateArrayTemplate(FunctionCallback);
411 static Handle<Value> CreateExternalArrayBuffer(Isolate* isolate,
412 Handle<Object> buffer,
414 static Handle<Object> CreateExternalArray(Isolate* isolate,
415 Handle<Object> array,
416 Handle<Object> buffer,
417 ExternalArrayType type,
421 int32_t element_size);
422 static void CreateExternalArray(
423 const v8::FunctionCallbackInfo<v8::Value>& args,
424 ExternalArrayType type,
425 int32_t element_size);
426 static void ExternalArrayWeakCallback(Isolate* isolate,
427 Persistent<Object>* object,