Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 32 | #ifndef V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 33 | #include "v8.h" |
| 34 | #include "allocation.h" |
| 35 | #include "hashmap.h" |
| 36 | #else |
| 37 | #include "../include/v8.h" |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 38 | #endif // V8_SHARED |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | |
| 40 | namespace v8 { |
| 41 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 43 | #ifndef V8_SHARED |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | // A single counter in a counter collection. |
| 45 | class 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 |
| 65 | class 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 | |
| 79 | class 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 110 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | private: |
| 112 | static int Hash(const char* name); |
| 113 | static bool Match(void* key1, void* key2); |
| 114 | i::HashMap hash_map_; |
| 115 | }; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 116 | #endif // V8_SHARED |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | |
| 118 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 119 | class SourceGroup { |
| 120 | public: |
| 121 | SourceGroup() : |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 122 | #ifndef V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 123 | next_semaphore_(v8::internal::OS::CreateSemaphore(0)), |
| 124 | done_semaphore_(v8::internal::OS::CreateSemaphore(0)), |
| 125 | thread_(NULL), |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 126 | #endif // V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 127 | argv_(NULL), |
| 128 | begin_offset_(0), |
| 129 | end_offset_(0) { } |
| 130 | |
| 131 | void Begin(char** argv, int offset) { |
| 132 | argv_ = const_cast<const char**>(argv); |
| 133 | begin_offset_ = offset; |
| 134 | } |
| 135 | |
| 136 | void End(int offset) { end_offset_ = offset; } |
| 137 | |
| 138 | void Execute(); |
| 139 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 140 | #ifndef V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 141 | void StartExecuteInThread(); |
| 142 | void WaitForThread(); |
| 143 | |
| 144 | private: |
| 145 | class IsolateThread : public i::Thread { |
| 146 | public: |
| 147 | explicit IsolateThread(SourceGroup* group) |
| 148 | : i::Thread(GetThreadOptions()), group_(group) {} |
| 149 | |
| 150 | virtual void Run() { |
| 151 | group_->ExecuteInThread(); |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | SourceGroup* group_; |
| 156 | }; |
| 157 | |
| 158 | static i::Thread::Options GetThreadOptions(); |
| 159 | void ExecuteInThread(); |
| 160 | |
| 161 | i::Semaphore* next_semaphore_; |
| 162 | i::Semaphore* done_semaphore_; |
| 163 | i::Thread* thread_; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 164 | #endif // V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 165 | |
| 166 | void ExitShell(int exit_code); |
| 167 | Handle<String> ReadFile(const char* name); |
| 168 | |
| 169 | const char** argv_; |
| 170 | int begin_offset_; |
| 171 | int end_offset_; |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | class ShellOptions { |
| 176 | public: |
| 177 | ShellOptions() : |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 178 | #ifndef V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 179 | use_preemption(true), |
| 180 | preemption_interval(10), |
| 181 | parallel_files(NULL), |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 182 | #endif // V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 183 | script_executed(false), |
| 184 | last_run(true), |
| 185 | stress_opt(false), |
| 186 | stress_deopt(false), |
| 187 | interactive_shell(false), |
| 188 | test_shell(false), |
| 189 | num_isolates(1), |
| 190 | isolate_sources(NULL) { } |
| 191 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 192 | #ifndef V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 193 | bool use_preemption; |
| 194 | int preemption_interval; |
| 195 | i::List< i::Vector<const char> >* parallel_files; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 196 | #endif // V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 197 | bool script_executed; |
| 198 | bool last_run; |
| 199 | bool stress_opt; |
| 200 | bool stress_deopt; |
| 201 | bool interactive_shell; |
| 202 | bool test_shell; |
| 203 | int num_isolates; |
| 204 | SourceGroup* isolate_sources; |
| 205 | }; |
| 206 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 207 | #ifdef V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 208 | class Shell { |
| 209 | #else |
| 210 | class Shell : public i::AllStatic { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 211 | #endif // V8_SHARED |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | public: |
| 213 | static bool ExecuteString(Handle<String> source, |
| 214 | Handle<Value> name, |
| 215 | bool print_result, |
| 216 | bool report_exceptions); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 217 | static const char* ToCString(const v8::String::Utf8Value& value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 218 | static void ReportException(TryCatch* try_catch); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 219 | static Handle<String> ReadFile(const char* name); |
| 220 | static Persistent<Context> CreateEvaluationContext(); |
| 221 | static int RunMain(int argc, char* argv[]); |
| 222 | static int Main(int argc, char* argv[]); |
| 223 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 224 | #ifndef V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 225 | static Handle<Array> GetCompletions(Handle<String> text, |
| 226 | Handle<String> full); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 227 | static void OnExit(); |
| 228 | static int* LookupCounter(const char* name); |
| 229 | static void* CreateHistogram(const char* name, |
| 230 | int min, |
| 231 | int max, |
| 232 | size_t buckets); |
| 233 | static void AddHistogramSample(void* histogram, int sample); |
| 234 | static void MapCounters(const char* name); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 235 | #endif // V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 236 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 238 | static Handle<Object> DebugMessageDetails(Handle<String> message); |
| 239 | static Handle<Value> DebugCommandToJSONRequest(Handle<String> command); |
| 240 | #endif |
| 241 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 242 | #ifdef WIN32 |
| 243 | #undef Yield |
| 244 | #endif |
| 245 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | static Handle<Value> Print(const Arguments& args); |
| 247 | static Handle<Value> Write(const Arguments& args); |
| 248 | static Handle<Value> Yield(const Arguments& args); |
| 249 | static Handle<Value> Quit(const Arguments& args); |
| 250 | static Handle<Value> Version(const Arguments& args); |
| 251 | static Handle<Value> Read(const Arguments& args); |
| 252 | static Handle<Value> ReadLine(const Arguments& args); |
| 253 | static Handle<Value> Load(const Arguments& args); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 254 | static Handle<Value> Int8Array(const Arguments& args); |
| 255 | static Handle<Value> Uint8Array(const Arguments& args); |
| 256 | static Handle<Value> Int16Array(const Arguments& args); |
| 257 | static Handle<Value> Uint16Array(const Arguments& args); |
| 258 | static Handle<Value> Int32Array(const Arguments& args); |
| 259 | static Handle<Value> Uint32Array(const Arguments& args); |
| 260 | static Handle<Value> Float32Array(const Arguments& args); |
| 261 | static Handle<Value> Float64Array(const Arguments& args); |
| 262 | static Handle<Value> PixelArray(const Arguments& args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | // The OS object on the global object contains methods for performing |
| 264 | // operating system calls: |
| 265 | // |
| 266 | // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will |
| 267 | // run the command, passing the arguments to the program. The standard output |
| 268 | // of the program will be picked up and returned as a multiline string. If |
| 269 | // timeout1 is present then it should be a number. -1 indicates no timeout |
| 270 | // and a positive number is used as a timeout in milliseconds that limits the |
| 271 | // time spent waiting between receiving output characters from the program. |
| 272 | // timeout2, if present, should be a number indicating the limit in |
| 273 | // milliseconds on the total running time of the program. Exceptions are |
| 274 | // thrown on timeouts or other errors or if the exit status of the program |
| 275 | // indicates an error. |
| 276 | // |
| 277 | // os.chdir(dir) changes directory to the given directory. Throws an |
| 278 | // exception/ on error. |
| 279 | // |
| 280 | // os.setenv(variable, value) sets an environment variable. Repeated calls to |
| 281 | // this method leak memory due to the API of setenv in the standard C library. |
| 282 | // |
| 283 | // os.umask(alue) calls the umask system call and returns the old umask. |
| 284 | // |
| 285 | // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded |
| 286 | // with the current umask. Intermediate directories are created if necessary. |
| 287 | // An exception is not thrown if the directory already exists. Analogous to |
| 288 | // the "mkdir -p" command. |
| 289 | static Handle<Value> OSObject(const Arguments& args); |
| 290 | static Handle<Value> System(const Arguments& args); |
| 291 | static Handle<Value> ChangeDirectory(const Arguments& args); |
| 292 | static Handle<Value> SetEnvironment(const Arguments& args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 293 | static Handle<Value> UnsetEnvironment(const Arguments& args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | static Handle<Value> SetUMask(const Arguments& args); |
| 295 | static Handle<Value> MakeDirectory(const Arguments& args); |
| 296 | static Handle<Value> RemoveDirectory(const Arguments& args); |
| 297 | |
| 298 | static void AddOSMethods(Handle<ObjectTemplate> os_template); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 299 | #ifndef V8_SHARED |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | static const char* kHistoryFileName; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 301 | #endif // V8_SHARED |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | static const char* kPrompt; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 303 | static ShellOptions options; |
| 304 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 306 | static Persistent<Context> evaluation_context_; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 307 | #ifndef V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 308 | static Persistent<Context> utility_context_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 309 | static CounterMap* counter_map_; |
| 310 | // We statically allocate a set of local counters to be used if we |
| 311 | // don't want to store the stats in a memory-mapped file |
| 312 | static CounterCollection local_counters_; |
| 313 | static CounterCollection* counters_; |
| 314 | static i::OS::MemoryMappedFile* counters_file_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 315 | static i::Mutex* context_mutex_; |
| 316 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | static Counter* GetCounter(const char* name, bool is_histogram); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 318 | static void InstallUtilityScript(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 319 | #endif // V8_SHARED |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 320 | static void Initialize(); |
| 321 | static void RunShell(); |
| 322 | static bool SetOptions(int argc, char* argv[]); |
| 323 | static Handle<ObjectTemplate> CreateGlobalTemplate(); |
| 324 | static Handle<Value> CreateExternalArray(const Arguments& args, |
| 325 | ExternalArrayType type, |
| 326 | size_t element_size); |
| 327 | static void ExternalArrayWeakCallback(Persistent<Value> object, void* data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 331 | #ifndef V8_SHARED |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | class LineEditor { |
| 333 | public: |
| 334 | enum Type { DUMB = 0, READLINE = 1 }; |
| 335 | LineEditor(Type type, const char* name); |
| 336 | virtual ~LineEditor() { } |
| 337 | |
| 338 | virtual i::SmartPointer<char> Prompt(const char* prompt) = 0; |
| 339 | virtual bool Open() { return true; } |
| 340 | virtual bool Close() { return true; } |
| 341 | virtual void AddHistory(const char* str) { } |
| 342 | |
| 343 | const char* name() { return name_; } |
| 344 | static LineEditor* Get(); |
| 345 | private: |
| 346 | Type type_; |
| 347 | const char* name_; |
| 348 | LineEditor* next_; |
| 349 | static LineEditor* first_; |
| 350 | }; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 351 | #endif // V8_SHARED |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 352 | |
| 353 | |
| 354 | } // namespace v8 |
| 355 | |
| 356 | |
| 357 | #endif // V8_D8_H_ |