ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 31 | #include "v8.h" |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 32 | #include "hashmap.h" |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | namespace v8 { |
| 36 | |
| 37 | |
| 38 | namespace i = v8::internal; |
| 39 | |
| 40 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 41 | // A single counter in a counter collection. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 42 | class Counter { |
| 43 | public: |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 44 | static const int kMaxNameSize = 64; |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 45 | int32_t* Bind(const char* name, bool histogram); |
| 46 | int32_t* ptr() { return &count_; } |
| 47 | int32_t count() { return count_; } |
| 48 | int32_t sample_total() { return sample_total_; } |
| 49 | bool is_histogram() { return is_histogram_; } |
| 50 | void AddSample(int32_t sample); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 51 | private: |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 52 | int32_t count_; |
| 53 | int32_t sample_total_; |
| 54 | bool is_histogram_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 55 | uint8_t name_[kMaxNameSize]; |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | // A set of counters and associated information. An instance of this |
| 60 | // class is stored directly in the memory-mapped counters file if |
| 61 | // the --map-counters options is used |
| 62 | class CounterCollection { |
| 63 | public: |
| 64 | CounterCollection(); |
| 65 | Counter* GetNextCounter(); |
| 66 | private: |
| 67 | static const unsigned kMaxCounters = 256; |
| 68 | uint32_t magic_number_; |
| 69 | uint32_t max_counters_; |
| 70 | uint32_t max_name_size_; |
| 71 | uint32_t counters_in_use_; |
| 72 | Counter counters_[kMaxCounters]; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 76 | class CounterMap { |
| 77 | public: |
| 78 | CounterMap(): hash_map_(Match) { } |
| 79 | Counter* Lookup(const char* name) { |
| 80 | i::HashMap::Entry* answer = hash_map_.Lookup( |
| 81 | const_cast<char*>(name), |
| 82 | Hash(name), |
| 83 | false); |
| 84 | if (!answer) return NULL; |
| 85 | return reinterpret_cast<Counter*>(answer->value); |
| 86 | } |
| 87 | void Set(const char* name, Counter* value) { |
| 88 | i::HashMap::Entry* answer = hash_map_.Lookup( |
| 89 | const_cast<char*>(name), |
| 90 | Hash(name), |
| 91 | true); |
| 92 | ASSERT(answer != NULL); |
| 93 | answer->value = value; |
| 94 | } |
| 95 | class Iterator { |
| 96 | public: |
| 97 | explicit Iterator(CounterMap* map) |
| 98 | : map_(&map->hash_map_), entry_(map_->Start()) { } |
| 99 | void Next() { entry_ = map_->Next(entry_); } |
| 100 | bool More() { return entry_ != NULL; } |
| 101 | const char* CurrentKey() { return static_cast<const char*>(entry_->key); } |
| 102 | Counter* CurrentValue() { return static_cast<Counter*>(entry_->value); } |
| 103 | private: |
| 104 | i::HashMap* map_; |
| 105 | i::HashMap::Entry* entry_; |
| 106 | }; |
| 107 | private: |
| 108 | static int Hash(const char* name); |
| 109 | static bool Match(void* key1, void* key2); |
| 110 | i::HashMap hash_map_; |
| 111 | }; |
| 112 | |
| 113 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 114 | class Shell: public i::AllStatic { |
| 115 | public: |
| 116 | static bool ExecuteString(Handle<String> source, |
| 117 | Handle<Value> name, |
| 118 | bool print_result, |
| 119 | bool report_exceptions); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 120 | static const char* ToCString(const v8::String::Utf8Value& value); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 121 | static void ReportException(TryCatch* try_catch); |
| 122 | static void Initialize(); |
| 123 | static void OnExit(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 124 | static int* LookupCounter(const char* name); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 125 | static void* CreateHistogram(const char* name, |
| 126 | int min, |
| 127 | int max, |
| 128 | size_t buckets); |
| 129 | static void AddHistogramSample(void* histogram, int sample); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 130 | static void MapCounters(const char* name); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 131 | static Handle<String> ReadFile(const char* name); |
| 132 | static void RunShell(); |
| 133 | static int Main(int argc, char* argv[]); |
| 134 | static Handle<Array> GetCompletions(Handle<String> text, |
| 135 | Handle<String> full); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 136 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 137 | static Handle<Object> DebugMessageDetails(Handle<String> message); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 138 | static Handle<Value> DebugCommandToJSONRequest(Handle<String> command); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 139 | #endif |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 140 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 141 | #ifdef WIN32 |
| 142 | #undef Yield |
| 143 | #endif |
| 144 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 145 | static Handle<Value> Print(const Arguments& args); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 146 | static Handle<Value> Write(const Arguments& args); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 147 | static Handle<Value> Yield(const Arguments& args); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 148 | static Handle<Value> Quit(const Arguments& args); |
| 149 | static Handle<Value> Version(const Arguments& args); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 150 | static Handle<Value> Read(const Arguments& args); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 151 | static Handle<Value> ReadLine(const Arguments& args); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 152 | static Handle<Value> Load(const Arguments& args); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 153 | // The OS object on the global object contains methods for performing |
| 154 | // operating system calls: |
| 155 | // |
| 156 | // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will |
| 157 | // run the command, passing the arguments to the program. The standard output |
| 158 | // of the program will be picked up and returned as a multiline string. If |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 159 | // timeout1 is present then it should be a number. -1 indicates no timeout |
| 160 | // and a positive number is used as a timeout in milliseconds that limits the |
| 161 | // time spent waiting between receiving output characters from the program. |
| 162 | // timeout2, if present, should be a number indicating the limit in |
| 163 | // milliseconds on the total running time of the program. Exceptions are |
| 164 | // thrown on timeouts or other errors or if the exit status of the program |
| 165 | // indicates an error. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 166 | // |
| 167 | // os.chdir(dir) changes directory to the given directory. Throws an |
| 168 | // exception/ on error. |
| 169 | // |
| 170 | // os.setenv(variable, value) sets an environment variable. Repeated calls to |
| 171 | // this method leak memory due to the API of setenv in the standard C library. |
| 172 | // |
| 173 | // os.umask(alue) calls the umask system call and returns the old umask. |
| 174 | // |
| 175 | // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded |
| 176 | // with the current umask. Intermediate directories are created if necessary. |
| 177 | // An exception is not thrown if the directory already exists. Analogous to |
| 178 | // the "mkdir -p" command. |
| 179 | static Handle<Value> OSObject(const Arguments& args); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 180 | static Handle<Value> System(const Arguments& args); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 181 | static Handle<Value> ChangeDirectory(const Arguments& args); |
| 182 | static Handle<Value> SetEnvironment(const Arguments& args); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 183 | static Handle<Value> UnsetEnvironment(const Arguments& args); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 184 | static Handle<Value> SetUMask(const Arguments& args); |
| 185 | static Handle<Value> MakeDirectory(const Arguments& args); |
| 186 | static Handle<Value> RemoveDirectory(const Arguments& args); |
| 187 | |
| 188 | static void AddOSMethods(Handle<ObjectTemplate> os_template); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 189 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 190 | static Handle<Context> utility_context() { return utility_context_; } |
| 191 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 192 | static const char* kHistoryFileName; |
| 193 | static const char* kPrompt; |
| 194 | private: |
| 195 | static Persistent<Context> utility_context_; |
| 196 | static Persistent<Context> evaluation_context_; |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 197 | static CounterMap* counter_map_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 198 | // We statically allocate a set of local counters to be used if we |
| 199 | // don't want to store the stats in a memory-mapped file |
| 200 | static CounterCollection local_counters_; |
| 201 | static CounterCollection* counters_; |
| 202 | static i::OS::MemoryMappedFile* counters_file_; |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 203 | static Counter* GetCounter(const char* name, bool is_histogram); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | |
| 207 | class LineEditor { |
| 208 | public: |
| 209 | enum Type { DUMB = 0, READLINE = 1 }; |
| 210 | LineEditor(Type type, const char* name); |
| 211 | virtual ~LineEditor() { } |
| 212 | |
| 213 | virtual i::SmartPointer<char> Prompt(const char* prompt) = 0; |
| 214 | virtual bool Open() { return true; } |
| 215 | virtual bool Close() { return true; } |
| 216 | virtual void AddHistory(const char* str) { } |
| 217 | |
| 218 | const char* name() { return name_; } |
| 219 | static LineEditor* Get(); |
| 220 | private: |
| 221 | Type type_; |
| 222 | const char* name_; |
| 223 | LineEditor* next_; |
| 224 | static LineEditor* first_; |
| 225 | }; |
| 226 | |
| 227 | |
| 228 | } // namespace v8 |
| 229 | |
| 230 | |
| 231 | #endif // V8_D8_H_ |