erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | |
ulan@chromium.org | d6899c3 | 2012-05-18 14:12:25 +0000 | [diff] [blame] | 29 | // Defined when linking against shared lib on Windows. |
| 30 | #if defined(USING_V8_SHARED) && !defined(V8_SHARED) |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 31 | #define V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 34 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 35 | #include <bzlib.h> |
| 36 | #endif |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 37 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 38 | #include <errno.h> |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 40 | #include <string.h> |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 41 | #include <sys/stat.h> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 42 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 43 | #ifdef V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 44 | #include <assert.h> |
| 45 | #include "../include/v8-testing.h" |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 46 | #endif // V8_SHARED |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 47 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 48 | #include "d8.h" |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 49 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 50 | #ifndef V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 51 | #include "api.h" |
| 52 | #include "checks.h" |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 53 | #include "d8-debug.h" |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 54 | #include "debug.h" |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 55 | #include "natives.h" |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 56 | #include "platform.h" |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 57 | #include "v8.h" |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 58 | #endif // V8_SHARED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 59 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 60 | #if !defined(_WIN32) && !defined(_WIN64) |
| 61 | #include <unistd.h> // NOLINT |
| 62 | #endif |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 63 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 64 | #ifndef ASSERT |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 65 | #define ASSERT(condition) assert(condition) |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 66 | #endif |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 67 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 68 | namespace v8 { |
| 69 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 70 | LineEditor *LineEditor::first_ = NULL; |
| 71 | |
| 72 | |
| 73 | LineEditor::LineEditor(Type type, const char* name) |
| 74 | : type_(type), |
| 75 | name_(name), |
| 76 | next_(first_) { |
| 77 | first_ = this; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | LineEditor* LineEditor::Get() { |
| 82 | LineEditor* current = first_; |
| 83 | LineEditor* best = current; |
| 84 | while (current != NULL) { |
| 85 | if (current->type_ > best->type_) |
| 86 | best = current; |
| 87 | current = current->next_; |
| 88 | } |
| 89 | return best; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | class DumbLineEditor: public LineEditor { |
| 94 | public: |
| 95 | DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 96 | virtual Handle<String> Prompt(const char* prompt); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | |
| 100 | static DumbLineEditor dumb_line_editor; |
| 101 | |
| 102 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 103 | Handle<String> DumbLineEditor::Prompt(const char* prompt) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 104 | printf("%s", prompt); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 105 | return Shell::ReadFromStdin(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 109 | #ifndef V8_SHARED |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 110 | CounterMap* Shell::counter_map_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 111 | i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; |
| 112 | CounterCollection Shell::local_counters_; |
| 113 | CounterCollection* Shell::counters_ = &local_counters_; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 114 | i::Mutex* Shell::context_mutex_(i::OS::CreateMutex()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 115 | Persistent<Context> Shell::utility_context_; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 116 | #endif // V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 117 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 118 | LineEditor* Shell::console = NULL; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 119 | Persistent<Context> Shell::evaluation_context_; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 120 | ShellOptions Shell::options; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 121 | const char* Shell::kPrompt = "d8> "; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 122 | |
| 123 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 124 | const int MB = 1024 * 1024; |
| 125 | |
| 126 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 127 | #ifndef V8_SHARED |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 128 | bool CounterMap::Match(void* key1, void* key2) { |
| 129 | const char* name1 = reinterpret_cast<const char*>(key1); |
| 130 | const char* name2 = reinterpret_cast<const char*>(key2); |
| 131 | return strcmp(name1, name2) == 0; |
| 132 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 133 | #endif // V8_SHARED |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 134 | |
| 135 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 136 | // Converts a V8 value to a C string. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 137 | const char* Shell::ToCString(const v8::String::Utf8Value& value) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 138 | return *value ? *value : "<string conversion failed>"; |
| 139 | } |
| 140 | |
| 141 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 142 | // Executes a string within the current v8 context. |
| 143 | bool Shell::ExecuteString(Handle<String> source, |
| 144 | Handle<Value> name, |
| 145 | bool print_result, |
| 146 | bool report_exceptions) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 147 | #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 148 | bool FLAG_debugger = i::FLAG_debugger; |
| 149 | #else |
| 150 | bool FLAG_debugger = false; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 151 | #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 152 | HandleScope handle_scope; |
| 153 | TryCatch try_catch; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 154 | options.script_executed = true; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 155 | if (FLAG_debugger) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 156 | // When debugging make exceptions appear to be uncaught. |
| 157 | try_catch.SetVerbose(true); |
| 158 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 159 | Handle<Script> script = Script::Compile(source, name); |
| 160 | if (script.IsEmpty()) { |
| 161 | // Print errors that happened during compilation. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 162 | if (report_exceptions && !FLAG_debugger) |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 163 | ReportException(&try_catch); |
| 164 | return false; |
| 165 | } else { |
| 166 | Handle<Value> result = script->Run(); |
| 167 | if (result.IsEmpty()) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 168 | ASSERT(try_catch.HasCaught()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 169 | // Print errors that happened during execution. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 170 | if (report_exceptions && !FLAG_debugger) |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 171 | ReportException(&try_catch); |
| 172 | return false; |
| 173 | } else { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 174 | ASSERT(!try_catch.HasCaught()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 175 | if (print_result && !result->IsUndefined()) { |
| 176 | // If all went well and the result wasn't undefined then print |
| 177 | // the returned value. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 178 | v8::String::Utf8Value str(result); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 179 | size_t count = fwrite(*str, sizeof(**str), str.length(), stdout); |
| 180 | (void) count; // Silence GCC-4.5.x "unused result" warning. |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 181 | printf("\n"); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
| 189 | Handle<Value> Shell::Print(const Arguments& args) { |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 190 | Handle<Value> val = Write(args); |
| 191 | printf("\n"); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 192 | fflush(stdout); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 193 | return val; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | Handle<Value> Shell::Write(const Arguments& args) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 198 | for (int i = 0; i < args.Length(); i++) { |
| 199 | HandleScope handle_scope; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 200 | if (i != 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 201 | printf(" "); |
| 202 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 203 | v8::String::Utf8Value str(args[i]); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 204 | int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 205 | if (n != str.length()) { |
| 206 | printf("Error in fwrite\n"); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 207 | Exit(1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 208 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 209 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 210 | return Undefined(); |
| 211 | } |
| 212 | |
| 213 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 214 | Handle<Value> Shell::EnableProfiler(const Arguments& args) { |
| 215 | V8::ResumeProfiler(); |
| 216 | return Undefined(); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | Handle<Value> Shell::DisableProfiler(const Arguments& args) { |
| 221 | V8::PauseProfiler(); |
| 222 | return Undefined(); |
| 223 | } |
| 224 | |
| 225 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 226 | Handle<Value> Shell::Read(const Arguments& args) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 227 | String::Utf8Value file(args[0]); |
| 228 | if (*file == NULL) { |
| 229 | return ThrowException(String::New("Error loading file")); |
| 230 | } |
| 231 | Handle<String> source = ReadFile(*file); |
| 232 | if (source.IsEmpty()) { |
| 233 | return ThrowException(String::New("Error loading file")); |
| 234 | } |
| 235 | return source; |
| 236 | } |
| 237 | |
| 238 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 239 | Handle<String> Shell::ReadFromStdin() { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 240 | static const int kBufferSize = 256; |
| 241 | char buffer[kBufferSize]; |
| 242 | Handle<String> accumulator = String::New(""); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 243 | int length; |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 244 | while (true) { |
| 245 | // Continue reading if the line ends with an escape '\\' or the line has |
| 246 | // not been fully read into the buffer yet (does not end with '\n'). |
| 247 | // If fgets gets an error, just give up. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 248 | char* input = NULL; |
| 249 | { // Release lock for blocking input. |
| 250 | Unlocker unlock(Isolate::GetCurrent()); |
| 251 | input = fgets(buffer, kBufferSize, stdin); |
| 252 | } |
| 253 | if (input == NULL) return Handle<String>(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 254 | length = static_cast<int>(strlen(buffer)); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 255 | if (length == 0) { |
| 256 | return accumulator; |
| 257 | } else if (buffer[length-1] != '\n') { |
| 258 | accumulator = String::Concat(accumulator, String::New(buffer, length)); |
| 259 | } else if (length > 1 && buffer[length-2] == '\\') { |
| 260 | buffer[length-2] = '\n'; |
| 261 | accumulator = String::Concat(accumulator, String::New(buffer, length-1)); |
| 262 | } else { |
| 263 | return String::Concat(accumulator, String::New(buffer, length-1)); |
| 264 | } |
| 265 | } |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 269 | Handle<Value> Shell::Load(const Arguments& args) { |
| 270 | for (int i = 0; i < args.Length(); i++) { |
| 271 | HandleScope handle_scope; |
| 272 | String::Utf8Value file(args[i]); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 273 | if (*file == NULL) { |
| 274 | return ThrowException(String::New("Error loading file")); |
| 275 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 276 | Handle<String> source = ReadFile(*file); |
| 277 | if (source.IsEmpty()) { |
| 278 | return ThrowException(String::New("Error loading file")); |
| 279 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 280 | if (!ExecuteString(source, String::New(*file), false, true)) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 281 | return ThrowException(String::New("Error executing file")); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | return Undefined(); |
| 285 | } |
| 286 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 287 | static int32_t convertToInt(Local<Value> value_in, TryCatch* try_catch) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 288 | if (value_in->IsInt32()) { |
| 289 | return value_in->Int32Value(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 290 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 291 | |
| 292 | Local<Value> number = value_in->ToNumber(); |
| 293 | if (try_catch->HasCaught()) return 0; |
| 294 | |
| 295 | ASSERT(number->IsNumber()); |
| 296 | Local<Int32> int32 = number->ToInt32(); |
| 297 | if (try_catch->HasCaught() || int32.IsEmpty()) return 0; |
| 298 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 299 | int32_t value = int32->Int32Value(); |
| 300 | if (try_catch->HasCaught()) return 0; |
| 301 | |
| 302 | return value; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | static int32_t convertToUint(Local<Value> value_in, TryCatch* try_catch) { |
| 307 | int32_t raw_value = convertToInt(value_in, try_catch); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 308 | if (try_catch->HasCaught()) return 0; |
| 309 | |
| 310 | if (raw_value < 0) { |
| 311 | ThrowException(String::New("Array length must not be negative.")); |
| 312 | return 0; |
| 313 | } |
| 314 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 315 | static const int kMaxLength = 0x3fffffff; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 316 | #ifndef V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 317 | ASSERT(kMaxLength == i::ExternalArray::kMaxLength); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 318 | #endif // V8_SHARED |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 319 | if (raw_value > static_cast<int32_t>(kMaxLength)) { |
| 320 | ThrowException( |
| 321 | String::New("Array length exceeds maximum length.")); |
| 322 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 323 | return raw_value; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 327 | // TODO(rossberg): should replace these by proper uses of HasInstance, |
| 328 | // once we figure out a good way to make the templates global. |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 329 | const char kArrayBufferMarkerPropName[] = "d8::_is_array_buffer_"; |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 330 | const char kArrayMarkerPropName[] = "d8::_is_typed_array_"; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 331 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 332 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 333 | Handle<Value> Shell::CreateExternalArrayBuffer(Handle<Object> buffer, |
| 334 | int32_t length) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 335 | static const int32_t kMaxSize = 0x7fffffff; |
| 336 | // Make sure the total size fits into a (signed) int. |
| 337 | if (length < 0 || length > kMaxSize) { |
| 338 | return ThrowException(String::New("ArrayBuffer exceeds maximum size (2G)")); |
| 339 | } |
| 340 | uint8_t* data = new uint8_t[length]; |
| 341 | if (data == NULL) { |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 342 | return ThrowException(String::New("Memory allocation failed")); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 343 | } |
| 344 | memset(data, 0, length); |
| 345 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 346 | buffer->SetHiddenValue(String::New(kArrayBufferMarkerPropName), True()); |
| 347 | Persistent<Object> persistent_array = Persistent<Object>::New(buffer); |
| 348 | persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
| 349 | persistent_array.MarkIndependent(); |
| 350 | V8::AdjustAmountOfExternalAllocatedMemory(length); |
| 351 | |
| 352 | buffer->SetIndexedPropertiesToExternalArrayData( |
| 353 | data, v8::kExternalByteArray, length); |
| 354 | buffer->Set(String::New("byteLength"), Int32::New(length), ReadOnly); |
| 355 | |
| 356 | return buffer; |
| 357 | } |
| 358 | |
| 359 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 360 | Handle<Value> Shell::ArrayBuffer(const Arguments& args) { |
| 361 | if (!args.IsConstructCall()) { |
| 362 | Handle<Value>* rec_args = new Handle<Value>[args.Length()]; |
| 363 | for (int i = 0; i < args.Length(); ++i) rec_args[i] = args[i]; |
| 364 | Handle<Value> result = args.Callee()->NewInstance(args.Length(), rec_args); |
| 365 | delete[] rec_args; |
| 366 | return result; |
| 367 | } |
| 368 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 369 | if (args.Length() == 0) { |
| 370 | return ThrowException( |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 371 | String::New("ArrayBuffer constructor must have one argument")); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 372 | } |
| 373 | TryCatch try_catch; |
| 374 | int32_t length = convertToUint(args[0], &try_catch); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 375 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 376 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 377 | return CreateExternalArrayBuffer(args.This(), length); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | Handle<Object> Shell::CreateExternalArray(Handle<Object> array, |
| 382 | Handle<Object> buffer, |
| 383 | ExternalArrayType type, |
| 384 | int32_t length, |
| 385 | int32_t byteLength, |
| 386 | int32_t byteOffset, |
| 387 | int32_t element_size) { |
| 388 | ASSERT(element_size == 1 || element_size == 2 || |
| 389 | element_size == 4 || element_size == 8); |
| 390 | ASSERT(byteLength == length * element_size); |
| 391 | |
| 392 | void* data = buffer->GetIndexedPropertiesExternalArrayData(); |
| 393 | ASSERT(data != NULL); |
| 394 | |
| 395 | array->SetIndexedPropertiesToExternalArrayData( |
| 396 | static_cast<uint8_t*>(data) + byteOffset, type, length); |
| 397 | array->SetHiddenValue(String::New(kArrayMarkerPropName), Int32::New(type)); |
| 398 | array->Set(String::New("byteLength"), Int32::New(byteLength), ReadOnly); |
| 399 | array->Set(String::New("byteOffset"), Int32::New(byteOffset), ReadOnly); |
| 400 | array->Set(String::New("length"), Int32::New(length), ReadOnly); |
| 401 | array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size)); |
| 402 | array->Set(String::New("buffer"), buffer, ReadOnly); |
| 403 | |
| 404 | return array; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 405 | } |
| 406 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 407 | |
| 408 | Handle<Value> Shell::CreateExternalArray(const Arguments& args, |
| 409 | ExternalArrayType type, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 410 | int32_t element_size) { |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 411 | if (!args.IsConstructCall()) { |
| 412 | Handle<Value>* rec_args = new Handle<Value>[args.Length()]; |
| 413 | for (int i = 0; i < args.Length(); ++i) rec_args[i] = args[i]; |
| 414 | Handle<Value> result = args.Callee()->NewInstance(args.Length(), rec_args); |
| 415 | delete[] rec_args; |
| 416 | return result; |
| 417 | } |
| 418 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 419 | TryCatch try_catch; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 420 | ASSERT(element_size == 1 || element_size == 2 || |
| 421 | element_size == 4 || element_size == 8); |
| 422 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 423 | // All of the following constructors are supported: |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 424 | // TypedArray(unsigned long length) |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 425 | // TypedArray(type[] array) |
| 426 | // TypedArray(TypedArray array) |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 427 | // TypedArray(ArrayBuffer buffer, |
| 428 | // optional unsigned long byteOffset, |
| 429 | // optional unsigned long length) |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 430 | Handle<Object> buffer; |
| 431 | int32_t length; |
| 432 | int32_t byteLength; |
| 433 | int32_t byteOffset; |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 434 | bool init_from_array = false; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 435 | if (args.Length() == 0) { |
| 436 | return ThrowException( |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 437 | String::New("Array constructor must have at least one argument")); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 438 | } |
| 439 | if (args[0]->IsObject() && |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 440 | !args[0]->ToObject()->GetHiddenValue( |
| 441 | String::New(kArrayBufferMarkerPropName)).IsEmpty()) { |
| 442 | // Construct from ArrayBuffer. |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 443 | buffer = args[0]->ToObject(); |
| 444 | int32_t bufferLength = |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 445 | convertToUint(buffer->Get(String::New("byteLength")), &try_catch); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 446 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 447 | |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 448 | if (args.Length() < 2 || args[1]->IsUndefined()) { |
| 449 | byteOffset = 0; |
| 450 | } else { |
| 451 | byteOffset = convertToUint(args[1], &try_catch); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 452 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 453 | if (byteOffset > bufferLength) { |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 454 | return ThrowException(String::New("byteOffset out of bounds")); |
| 455 | } |
| 456 | if (byteOffset % element_size != 0) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 457 | return ThrowException( |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 458 | String::New("byteOffset must be multiple of element size")); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 461 | |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 462 | if (args.Length() < 3 || args[2]->IsUndefined()) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 463 | byteLength = bufferLength - byteOffset; |
| 464 | length = byteLength / element_size; |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 465 | if (byteLength % element_size != 0) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 466 | return ThrowException( |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 467 | String::New("buffer size must be multiple of element size")); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 468 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 469 | } else { |
| 470 | length = convertToUint(args[2], &try_catch); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 471 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 472 | byteLength = length * element_size; |
| 473 | if (byteOffset + byteLength > bufferLength) { |
| 474 | return ThrowException(String::New("length out of bounds")); |
| 475 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 476 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 477 | } else { |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 478 | if (args[0]->IsObject() && |
| 479 | args[0]->ToObject()->Has(String::New("length"))) { |
| 480 | // Construct from array. |
| 481 | length = convertToUint( |
| 482 | args[0]->ToObject()->Get(String::New("length")), &try_catch); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 483 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 484 | init_from_array = true; |
| 485 | } else { |
| 486 | // Construct from size. |
| 487 | length = convertToUint(args[0], &try_catch); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 488 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 489 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 490 | byteLength = length * element_size; |
| 491 | byteOffset = 0; |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 492 | |
| 493 | Handle<Object> global = Context::GetCurrent()->Global(); |
| 494 | Handle<Value> array_buffer = global->Get(String::New("ArrayBuffer")); |
| 495 | ASSERT(!try_catch.HasCaught() && array_buffer->IsFunction()); |
| 496 | Handle<Value> buffer_args[] = { Uint32::New(byteLength) }; |
| 497 | Handle<Value> result = Handle<Function>::Cast(array_buffer)->NewInstance( |
| 498 | 1, buffer_args); |
| 499 | if (try_catch.HasCaught()) return result; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 500 | buffer = result->ToObject(); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 501 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 502 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 503 | Handle<Object> array = CreateExternalArray( |
| 504 | args.This(), buffer, type, length, byteLength, byteOffset, element_size); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 505 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 506 | if (init_from_array) { |
| 507 | Handle<Object> init = args[0]->ToObject(); |
| 508 | for (int i = 0; i < length; ++i) array->Set(i, init->Get(i)); |
| 509 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 510 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 511 | return array; |
| 512 | } |
| 513 | |
| 514 | |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 515 | Handle<Value> Shell::ArrayBufferSlice(const Arguments& args) { |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 516 | TryCatch try_catch; |
| 517 | |
| 518 | if (!args.This()->IsObject()) { |
| 519 | return ThrowException( |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 520 | String::New("'slice' invoked on non-object receiver")); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | Local<Object> self = args.This(); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 524 | Local<Value> marker = |
| 525 | self->GetHiddenValue(String::New(kArrayBufferMarkerPropName)); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 526 | if (marker.IsEmpty()) { |
| 527 | return ThrowException( |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 528 | String::New("'slice' invoked on wrong receiver type")); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 529 | } |
| 530 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 531 | int32_t length = |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 532 | convertToUint(self->Get(String::New("byteLength")), &try_catch); |
| 533 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 534 | |
| 535 | if (args.Length() == 0) { |
| 536 | return ThrowException( |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 537 | String::New("'slice' must have at least one argument")); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 538 | } |
| 539 | int32_t begin = convertToInt(args[0], &try_catch); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 540 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 541 | if (begin < 0) begin += length; |
| 542 | if (begin < 0) begin = 0; |
| 543 | if (begin > length) begin = length; |
| 544 | |
| 545 | int32_t end; |
| 546 | if (args.Length() < 2 || args[1]->IsUndefined()) { |
| 547 | end = length; |
| 548 | } else { |
| 549 | end = convertToInt(args[1], &try_catch); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 550 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 551 | if (end < 0) end += length; |
| 552 | if (end < 0) end = 0; |
| 553 | if (end > length) end = length; |
| 554 | if (end < begin) end = begin; |
| 555 | } |
| 556 | |
| 557 | Local<Function> constructor = Local<Function>::Cast(self->GetConstructor()); |
| 558 | Handle<Value> new_args[] = { Uint32::New(end - begin) }; |
| 559 | Handle<Value> result = constructor->NewInstance(1, new_args); |
| 560 | if (try_catch.HasCaught()) return result; |
| 561 | Handle<Object> buffer = result->ToObject(); |
| 562 | uint8_t* dest = |
| 563 | static_cast<uint8_t*>(buffer->GetIndexedPropertiesExternalArrayData()); |
| 564 | uint8_t* src = begin + static_cast<uint8_t*>( |
| 565 | self->GetIndexedPropertiesExternalArrayData()); |
| 566 | memcpy(dest, src, end - begin); |
| 567 | |
| 568 | return buffer; |
| 569 | } |
| 570 | |
| 571 | |
| 572 | Handle<Value> Shell::ArraySubArray(const Arguments& args) { |
| 573 | TryCatch try_catch; |
| 574 | |
| 575 | if (!args.This()->IsObject()) { |
| 576 | return ThrowException( |
| 577 | String::New("'subarray' invoked on non-object receiver")); |
| 578 | } |
| 579 | |
| 580 | Local<Object> self = args.This(); |
| 581 | Local<Value> marker = self->GetHiddenValue(String::New(kArrayMarkerPropName)); |
| 582 | if (marker.IsEmpty()) { |
| 583 | return ThrowException( |
| 584 | String::New("'subarray' invoked on wrong receiver type")); |
| 585 | } |
| 586 | |
| 587 | Handle<Object> buffer = self->Get(String::New("buffer"))->ToObject(); |
| 588 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 589 | int32_t length = |
| 590 | convertToUint(self->Get(String::New("length")), &try_catch); |
| 591 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 592 | int32_t byteOffset = |
| 593 | convertToUint(self->Get(String::New("byteOffset")), &try_catch); |
| 594 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 595 | int32_t element_size = |
| 596 | convertToUint(self->Get(String::New("BYTES_PER_ELEMENT")), &try_catch); |
| 597 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 598 | |
| 599 | if (args.Length() == 0) { |
| 600 | return ThrowException( |
| 601 | String::New("'subarray' must have at least one argument")); |
| 602 | } |
| 603 | int32_t begin = convertToInt(args[0], &try_catch); |
| 604 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 605 | if (begin < 0) begin += length; |
| 606 | if (begin < 0) begin = 0; |
| 607 | if (begin > length) begin = length; |
| 608 | |
| 609 | int32_t end; |
| 610 | if (args.Length() < 2 || args[1]->IsUndefined()) { |
| 611 | end = length; |
| 612 | } else { |
| 613 | end = convertToInt(args[1], &try_catch); |
| 614 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 615 | if (end < 0) end += length; |
| 616 | if (end < 0) end = 0; |
| 617 | if (end > length) end = length; |
| 618 | if (end < begin) end = begin; |
| 619 | } |
| 620 | |
| 621 | length = end - begin; |
| 622 | byteOffset += begin * element_size; |
| 623 | |
| 624 | Local<Function> constructor = Local<Function>::Cast(self->GetConstructor()); |
| 625 | Handle<Value> construct_args[] = { |
| 626 | buffer, Uint32::New(byteOffset), Uint32::New(length) |
| 627 | }; |
| 628 | return constructor->NewInstance(3, construct_args); |
| 629 | } |
| 630 | |
| 631 | |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 632 | Handle<Value> Shell::ArraySet(const Arguments& args) { |
| 633 | TryCatch try_catch; |
| 634 | |
| 635 | if (!args.This()->IsObject()) { |
| 636 | return ThrowException( |
| 637 | String::New("'set' invoked on non-object receiver")); |
| 638 | } |
| 639 | |
| 640 | Local<Object> self = args.This(); |
| 641 | Local<Value> marker = self->GetHiddenValue(String::New(kArrayMarkerPropName)); |
| 642 | if (marker.IsEmpty()) { |
| 643 | return ThrowException( |
| 644 | String::New("'set' invoked on wrong receiver type")); |
| 645 | } |
| 646 | int32_t length = |
| 647 | convertToUint(self->Get(String::New("length")), &try_catch); |
| 648 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 649 | int32_t element_size = |
| 650 | convertToUint(self->Get(String::New("BYTES_PER_ELEMENT")), &try_catch); |
| 651 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 652 | |
| 653 | if (args.Length() == 0) { |
| 654 | return ThrowException( |
| 655 | String::New("'set' must have at least one argument")); |
| 656 | } |
| 657 | if (!args[0]->IsObject() || |
| 658 | !args[0]->ToObject()->Has(String::New("length"))) { |
| 659 | return ThrowException( |
| 660 | String::New("'set' invoked with non-array argument")); |
| 661 | } |
| 662 | Handle<Object> source = args[0]->ToObject(); |
| 663 | int32_t source_length = |
| 664 | convertToUint(source->Get(String::New("length")), &try_catch); |
| 665 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 666 | |
| 667 | int32_t offset; |
| 668 | if (args.Length() < 2 || args[1]->IsUndefined()) { |
| 669 | offset = 0; |
| 670 | } else { |
| 671 | offset = convertToUint(args[1], &try_catch); |
| 672 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 673 | } |
| 674 | if (offset + source_length > length) { |
| 675 | return ThrowException(String::New("offset or source length out of bounds")); |
| 676 | } |
| 677 | |
| 678 | int32_t source_element_size; |
| 679 | if (source->GetHiddenValue(String::New(kArrayMarkerPropName)).IsEmpty()) { |
| 680 | source_element_size = 0; |
| 681 | } else { |
| 682 | source_element_size = |
| 683 | convertToUint(source->Get(String::New("BYTES_PER_ELEMENT")), &try_catch); |
| 684 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 685 | } |
| 686 | |
| 687 | if (element_size == source_element_size && |
| 688 | self->GetConstructor()->StrictEquals(source->GetConstructor())) { |
| 689 | // Use memmove on the array buffers. |
| 690 | Handle<Object> buffer = self->Get(String::New("buffer"))->ToObject(); |
| 691 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 692 | Handle<Object> source_buffer = |
| 693 | source->Get(String::New("buffer"))->ToObject(); |
| 694 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 695 | int32_t byteOffset = |
| 696 | convertToUint(self->Get(String::New("byteOffset")), &try_catch); |
| 697 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 698 | int32_t source_byteOffset = |
| 699 | convertToUint(source->Get(String::New("byteOffset")), &try_catch); |
| 700 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 701 | |
| 702 | uint8_t* dest = byteOffset + offset * element_size + static_cast<uint8_t*>( |
| 703 | buffer->GetIndexedPropertiesExternalArrayData()); |
| 704 | uint8_t* src = source_byteOffset + static_cast<uint8_t*>( |
| 705 | source_buffer->GetIndexedPropertiesExternalArrayData()); |
| 706 | memmove(dest, src, source_length * element_size); |
| 707 | } else if (source_element_size == 0) { |
| 708 | // Source is not a typed array, copy element-wise sequentially. |
| 709 | for (int i = 0; i < source_length; ++i) { |
| 710 | self->Set(offset + i, source->Get(i)); |
| 711 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 712 | } |
| 713 | } else { |
| 714 | // Need to copy element-wise to make the right conversions. |
| 715 | Handle<Object> buffer = self->Get(String::New("buffer"))->ToObject(); |
| 716 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 717 | Handle<Object> source_buffer = |
| 718 | source->Get(String::New("buffer"))->ToObject(); |
| 719 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 720 | |
| 721 | if (buffer->StrictEquals(source_buffer)) { |
| 722 | // Same backing store, need to handle overlap correctly. |
| 723 | // This gets a bit tricky in the case of different element sizes |
| 724 | // (which, of course, is extremely unlikely to ever occur in practice). |
| 725 | int32_t byteOffset = |
| 726 | convertToUint(self->Get(String::New("byteOffset")), &try_catch); |
| 727 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 728 | int32_t source_byteOffset = |
| 729 | convertToUint(source->Get(String::New("byteOffset")), &try_catch); |
| 730 | if (try_catch.HasCaught()) return try_catch.ReThrow(); |
| 731 | |
| 732 | // Copy as much as we can from left to right. |
| 733 | int i = 0; |
| 734 | int32_t next_dest_offset = byteOffset + (offset + 1) * element_size; |
| 735 | int32_t next_src_offset = source_byteOffset + source_element_size; |
| 736 | while (i < length && next_dest_offset <= next_src_offset) { |
| 737 | self->Set(offset + i, source->Get(i)); |
| 738 | ++i; |
| 739 | next_dest_offset += element_size; |
| 740 | next_src_offset += source_element_size; |
| 741 | } |
| 742 | // Of what's left, copy as much as we can from right to left. |
| 743 | int j = length - 1; |
| 744 | int32_t dest_offset = byteOffset + (offset + j) * element_size; |
| 745 | int32_t src_offset = source_byteOffset + j * source_element_size; |
| 746 | while (j >= i && dest_offset >= src_offset) { |
| 747 | self->Set(offset + j, source->Get(j)); |
| 748 | --j; |
| 749 | dest_offset -= element_size; |
| 750 | src_offset -= source_element_size; |
| 751 | } |
| 752 | // There can be at most 8 entries left in the middle that need buffering |
| 753 | // (because the largest element_size is 8 times the smallest). |
| 754 | ASSERT(j+1 - i <= 8); |
| 755 | Handle<Value> temp[8]; |
| 756 | for (int k = i; k <= j; ++k) { |
| 757 | temp[k - i] = source->Get(k); |
| 758 | } |
| 759 | for (int k = i; k <= j; ++k) { |
| 760 | self->Set(offset + k, temp[k - i]); |
| 761 | } |
| 762 | } else { |
| 763 | // Different backing stores, safe to copy element-wise sequentially. |
| 764 | for (int i = 0; i < source_length; ++i) |
| 765 | self->Set(offset + i, source->Get(i)); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return Undefined(); |
| 770 | } |
| 771 | |
| 772 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 773 | void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 774 | HandleScope scope; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 775 | int32_t length = |
| 776 | object->ToObject()->Get(String::New("byteLength"))->Uint32Value(); |
| 777 | V8::AdjustAmountOfExternalAllocatedMemory(-length); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 778 | delete[] static_cast<uint8_t*>(data); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 779 | object.Dispose(); |
| 780 | } |
| 781 | |
| 782 | |
| 783 | Handle<Value> Shell::Int8Array(const Arguments& args) { |
| 784 | return CreateExternalArray(args, v8::kExternalByteArray, sizeof(int8_t)); |
| 785 | } |
| 786 | |
| 787 | |
| 788 | Handle<Value> Shell::Uint8Array(const Arguments& args) { |
| 789 | return CreateExternalArray(args, kExternalUnsignedByteArray, sizeof(uint8_t)); |
| 790 | } |
| 791 | |
| 792 | |
| 793 | Handle<Value> Shell::Int16Array(const Arguments& args) { |
| 794 | return CreateExternalArray(args, kExternalShortArray, sizeof(int16_t)); |
| 795 | } |
| 796 | |
| 797 | |
| 798 | Handle<Value> Shell::Uint16Array(const Arguments& args) { |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 799 | return CreateExternalArray( |
| 800 | args, kExternalUnsignedShortArray, sizeof(uint16_t)); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 801 | } |
| 802 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 803 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 804 | Handle<Value> Shell::Int32Array(const Arguments& args) { |
| 805 | return CreateExternalArray(args, kExternalIntArray, sizeof(int32_t)); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | Handle<Value> Shell::Uint32Array(const Arguments& args) { |
| 810 | return CreateExternalArray(args, kExternalUnsignedIntArray, sizeof(uint32_t)); |
| 811 | } |
| 812 | |
| 813 | |
| 814 | Handle<Value> Shell::Float32Array(const Arguments& args) { |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 815 | return CreateExternalArray( |
| 816 | args, kExternalFloatArray, sizeof(float)); // NOLINT |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | |
| 820 | Handle<Value> Shell::Float64Array(const Arguments& args) { |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 821 | return CreateExternalArray( |
| 822 | args, kExternalDoubleArray, sizeof(double)); // NOLINT |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 826 | Handle<Value> Shell::Uint8ClampedArray(const Arguments& args) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 827 | return CreateExternalArray(args, kExternalPixelArray, sizeof(uint8_t)); |
| 828 | } |
| 829 | |
| 830 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 831 | Handle<Value> Shell::Yield(const Arguments& args) { |
| 832 | v8::Unlocker unlocker; |
| 833 | return Undefined(); |
| 834 | } |
| 835 | |
| 836 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 837 | Handle<Value> Shell::Quit(const Arguments& args) { |
| 838 | int exit_code = args[0]->Int32Value(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 839 | #ifndef V8_SHARED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 840 | OnExit(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 841 | #endif // V8_SHARED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 842 | exit(exit_code); |
| 843 | return Undefined(); |
| 844 | } |
| 845 | |
| 846 | |
| 847 | Handle<Value> Shell::Version(const Arguments& args) { |
| 848 | return String::New(V8::GetVersion()); |
| 849 | } |
| 850 | |
| 851 | |
| 852 | void Shell::ReportException(v8::TryCatch* try_catch) { |
| 853 | HandleScope handle_scope; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 854 | #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) |
| 855 | bool enter_context = !Context::InContext(); |
| 856 | if (enter_context) utility_context_->Enter(); |
| 857 | #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 858 | v8::String::Utf8Value exception(try_catch->Exception()); |
| 859 | const char* exception_string = ToCString(exception); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 860 | Handle<Message> message = try_catch->Message(); |
| 861 | if (message.IsEmpty()) { |
| 862 | // V8 didn't provide any extra information about this error; just |
| 863 | // print the exception. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 864 | printf("%s\n", exception_string); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 865 | } else { |
| 866 | // Print (filename):(line number): (message). |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 867 | v8::String::Utf8Value filename(message->GetScriptResourceName()); |
| 868 | const char* filename_string = ToCString(filename); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 869 | int linenum = message->GetLineNumber(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 870 | printf("%s:%i: %s\n", filename_string, linenum, exception_string); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 871 | // Print line of source code. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 872 | v8::String::Utf8Value sourceline(message->GetSourceLine()); |
| 873 | const char* sourceline_string = ToCString(sourceline); |
| 874 | printf("%s\n", sourceline_string); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 875 | // Print wavy underline (GetUnderline is deprecated). |
| 876 | int start = message->GetStartColumn(); |
| 877 | for (int i = 0; i < start; i++) { |
| 878 | printf(" "); |
| 879 | } |
| 880 | int end = message->GetEndColumn(); |
| 881 | for (int i = start; i < end; i++) { |
| 882 | printf("^"); |
| 883 | } |
| 884 | printf("\n"); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 885 | v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 886 | if (stack_trace.length() > 0) { |
| 887 | const char* stack_trace_string = ToCString(stack_trace); |
| 888 | printf("%s\n", stack_trace_string); |
| 889 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 890 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 891 | printf("\n"); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 892 | #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) |
| 893 | if (enter_context) utility_context_->Exit(); |
| 894 | #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 898 | #ifndef V8_SHARED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 899 | Handle<Array> Shell::GetCompletions(Handle<String> text, Handle<String> full) { |
| 900 | HandleScope handle_scope; |
| 901 | Context::Scope context_scope(utility_context_); |
| 902 | Handle<Object> global = utility_context_->Global(); |
| 903 | Handle<Value> fun = global->Get(String::New("GetCompletions")); |
| 904 | static const int kArgc = 3; |
| 905 | Handle<Value> argv[kArgc] = { evaluation_context_->Global(), text, full }; |
| 906 | Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); |
| 907 | return handle_scope.Close(Handle<Array>::Cast(val)); |
| 908 | } |
| 909 | |
| 910 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 911 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 912 | Handle<Object> Shell::DebugMessageDetails(Handle<String> message) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 913 | Context::Scope context_scope(utility_context_); |
| 914 | Handle<Object> global = utility_context_->Global(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 915 | Handle<Value> fun = global->Get(String::New("DebugMessageDetails")); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 916 | static const int kArgc = 1; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 917 | Handle<Value> argv[kArgc] = { message }; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 918 | Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 919 | return Handle<Object>::Cast(val); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | |
| 923 | Handle<Value> Shell::DebugCommandToJSONRequest(Handle<String> command) { |
| 924 | Context::Scope context_scope(utility_context_); |
| 925 | Handle<Object> global = utility_context_->Global(); |
| 926 | Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest")); |
| 927 | static const int kArgc = 1; |
| 928 | Handle<Value> argv[kArgc] = { command }; |
| 929 | Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); |
| 930 | return val; |
| 931 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 932 | |
| 933 | |
| 934 | void Shell::DispatchDebugMessages() { |
| 935 | v8::Context::Scope scope(Shell::evaluation_context_); |
| 936 | v8::Debug::ProcessDebugMessages(); |
| 937 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 938 | #endif // ENABLE_DEBUGGER_SUPPORT |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 939 | #endif // V8_SHARED |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 940 | |
| 941 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 942 | #ifndef V8_SHARED |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 943 | int32_t* Counter::Bind(const char* name, bool is_histogram) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 944 | int i; |
| 945 | for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) |
| 946 | name_[i] = static_cast<char>(name[i]); |
| 947 | name_[i] = '\0'; |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 948 | is_histogram_ = is_histogram; |
| 949 | return ptr(); |
| 950 | } |
| 951 | |
| 952 | |
| 953 | void Counter::AddSample(int32_t sample) { |
| 954 | count_++; |
| 955 | sample_total_ += sample; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | |
| 959 | CounterCollection::CounterCollection() { |
| 960 | magic_number_ = 0xDEADFACE; |
| 961 | max_counters_ = kMaxCounters; |
| 962 | max_name_size_ = Counter::kMaxNameSize; |
| 963 | counters_in_use_ = 0; |
| 964 | } |
| 965 | |
| 966 | |
| 967 | Counter* CounterCollection::GetNextCounter() { |
| 968 | if (counters_in_use_ == kMaxCounters) return NULL; |
| 969 | return &counters_[counters_in_use_++]; |
| 970 | } |
| 971 | |
| 972 | |
| 973 | void Shell::MapCounters(const char* name) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 974 | counters_file_ = i::OS::MemoryMappedFile::create( |
| 975 | name, sizeof(CounterCollection), &local_counters_); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 976 | void* memory = (counters_file_ == NULL) ? |
| 977 | NULL : counters_file_->memory(); |
| 978 | if (memory == NULL) { |
| 979 | printf("Could not map counters file %s\n", name); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 980 | Exit(1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 981 | } |
| 982 | counters_ = static_cast<CounterCollection*>(memory); |
| 983 | V8::SetCounterFunction(LookupCounter); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 984 | V8::SetCreateHistogramFunction(CreateHistogram); |
| 985 | V8::SetAddHistogramSampleFunction(AddHistogramSample); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 989 | int CounterMap::Hash(const char* name) { |
| 990 | int h = 0; |
| 991 | int c; |
| 992 | while ((c = *name++) != 0) { |
| 993 | h += h << 5; |
| 994 | h += c; |
| 995 | } |
| 996 | return h; |
| 997 | } |
| 998 | |
| 999 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1000 | Counter* Shell::GetCounter(const char* name, bool is_histogram) { |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1001 | Counter* counter = counter_map_->Lookup(name); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1002 | |
| 1003 | if (counter == NULL) { |
| 1004 | counter = counters_->GetNextCounter(); |
| 1005 | if (counter != NULL) { |
| 1006 | counter_map_->Set(name, counter); |
| 1007 | counter->Bind(name, is_histogram); |
| 1008 | } |
| 1009 | } else { |
| 1010 | ASSERT(counter->is_histogram() == is_histogram); |
| 1011 | } |
| 1012 | return counter; |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | int* Shell::LookupCounter(const char* name) { |
| 1017 | Counter* counter = GetCounter(name, false); |
| 1018 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1019 | if (counter != NULL) { |
| 1020 | return counter->ptr(); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1021 | } else { |
| 1022 | return NULL; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1023 | } |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | void* Shell::CreateHistogram(const char* name, |
| 1028 | int min, |
| 1029 | int max, |
| 1030 | size_t buckets) { |
| 1031 | return GetCounter(name, true); |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | void Shell::AddHistogramSample(void* histogram, int sample) { |
| 1036 | Counter* counter = reinterpret_cast<Counter*>(histogram); |
| 1037 | counter->AddSample(sample); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1040 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1041 | void Shell::InstallUtilityScript() { |
| 1042 | Locker lock; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1043 | HandleScope scope; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1044 | // If we use the utility context, we have to set the security tokens so that |
| 1045 | // utility, evaluation and debug context can all access each other. |
| 1046 | utility_context_->SetSecurityToken(Undefined()); |
| 1047 | evaluation_context_->SetSecurityToken(Undefined()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1048 | Context::Scope utility_scope(utility_context_); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1049 | |
| 1050 | #ifdef ENABLE_DEBUGGER_SUPPORT |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1051 | if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1052 | // Install the debugger object in the utility scope |
| 1053 | i::Debug* debug = i::Isolate::Current()->debug(); |
| 1054 | debug->Load(); |
| 1055 | i::Handle<i::JSObject> js_debug |
| 1056 | = i::Handle<i::JSObject>(debug->debug_context()->global()); |
| 1057 | utility_context_->Global()->Set(String::New("$debug"), |
| 1058 | Utils::ToLocal(js_debug)); |
| 1059 | debug->debug_context()->set_security_token(HEAP->undefined_value()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1060 | #endif // ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1061 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1062 | // Run the d8 shell utility script in the utility context |
| 1063 | int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); |
| 1064 | i::Vector<const char> shell_source = |
| 1065 | i::NativesCollection<i::D8>::GetRawScriptSource(source_index); |
| 1066 | i::Vector<const char> shell_source_name = |
| 1067 | i::NativesCollection<i::D8>::GetScriptName(source_index); |
| 1068 | Handle<String> source = String::New(shell_source.start(), |
| 1069 | shell_source.length()); |
| 1070 | Handle<String> name = String::New(shell_source_name.start(), |
| 1071 | shell_source_name.length()); |
| 1072 | Handle<Script> script = Script::Compile(source, name); |
| 1073 | script->Run(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1074 | // Mark the d8 shell script as native to avoid it showing up as normal source |
| 1075 | // in the debugger. |
| 1076 | i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); |
| 1077 | i::Handle<i::Script> script_object = compiled_script->IsJSFunction() |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1078 | ? i::Handle<i::Script>(i::Script::cast( |
| 1079 | i::JSFunction::cast(*compiled_script)->shared()->script())) |
| 1080 | : i::Handle<i::Script>(i::Script::cast( |
| 1081 | i::SharedFunctionInfo::cast(*compiled_script)->script())); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1082 | script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1083 | |
| 1084 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1085 | // Start the in-process debugger if requested. |
| 1086 | if (i::FLAG_debugger && !i::FLAG_debugger_agent) { |
| 1087 | v8::Debug::SetDebugEventListener(HandleDebugEvent); |
| 1088 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1089 | #endif // ENABLE_DEBUGGER_SUPPORT |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1090 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1091 | #endif // V8_SHARED |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1092 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1093 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1094 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 1095 | class BZip2Decompressor : public v8::StartupDataDecompressor { |
| 1096 | public: |
| 1097 | virtual ~BZip2Decompressor() { } |
| 1098 | |
| 1099 | protected: |
| 1100 | virtual int DecompressData(char* raw_data, |
| 1101 | int* raw_data_size, |
| 1102 | const char* compressed_data, |
| 1103 | int compressed_data_size) { |
| 1104 | ASSERT_EQ(v8::StartupData::kBZip2, |
| 1105 | v8::V8::GetCompressedStartupDataAlgorithm()); |
| 1106 | unsigned int decompressed_size = *raw_data_size; |
| 1107 | int result = |
| 1108 | BZ2_bzBuffToBuffDecompress(raw_data, |
| 1109 | &decompressed_size, |
| 1110 | const_cast<char*>(compressed_data), |
| 1111 | compressed_data_size, |
| 1112 | 0, 1); |
| 1113 | if (result == BZ_OK) { |
| 1114 | *raw_data_size = decompressed_size; |
| 1115 | } |
| 1116 | return result; |
| 1117 | } |
| 1118 | }; |
| 1119 | #endif |
| 1120 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1121 | |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 1122 | Handle<FunctionTemplate> Shell::CreateArrayBufferTemplate( |
| 1123 | InvocationCallback fun) { |
| 1124 | Handle<FunctionTemplate> buffer_template = FunctionTemplate::New(fun); |
| 1125 | Local<Template> proto_template = buffer_template->PrototypeTemplate(); |
| 1126 | proto_template->Set(String::New("slice"), |
| 1127 | FunctionTemplate::New(ArrayBufferSlice)); |
| 1128 | return buffer_template; |
| 1129 | } |
| 1130 | |
| 1131 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1132 | Handle<FunctionTemplate> Shell::CreateArrayTemplate(InvocationCallback fun) { |
| 1133 | Handle<FunctionTemplate> array_template = FunctionTemplate::New(fun); |
| 1134 | Local<Template> proto_template = array_template->PrototypeTemplate(); |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 1135 | proto_template->Set(String::New("set"), FunctionTemplate::New(ArraySet)); |
| 1136 | proto_template->Set(String::New("subarray"), |
| 1137 | FunctionTemplate::New(ArraySubArray)); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1138 | return array_template; |
| 1139 | } |
| 1140 | |
| 1141 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1142 | Handle<ObjectTemplate> Shell::CreateGlobalTemplate() { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1143 | Handle<ObjectTemplate> global_template = ObjectTemplate::New(); |
| 1144 | global_template->Set(String::New("print"), FunctionTemplate::New(Print)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1145 | global_template->Set(String::New("write"), FunctionTemplate::New(Write)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1146 | global_template->Set(String::New("read"), FunctionTemplate::New(Read)); |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1147 | global_template->Set(String::New("readbuffer"), |
| 1148 | FunctionTemplate::New(ReadBuffer)); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 1149 | global_template->Set(String::New("readline"), |
| 1150 | FunctionTemplate::New(ReadLine)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1151 | global_template->Set(String::New("load"), FunctionTemplate::New(Load)); |
| 1152 | global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); |
| 1153 | global_template->Set(String::New("version"), FunctionTemplate::New(Version)); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1154 | global_template->Set(String::New("enableProfiler"), |
| 1155 | FunctionTemplate::New(EnableProfiler)); |
| 1156 | global_template->Set(String::New("disableProfiler"), |
| 1157 | FunctionTemplate::New(DisableProfiler)); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1158 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1159 | // Bind the handlers for external arrays. |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1160 | PropertyAttribute attr = |
| 1161 | static_cast<PropertyAttribute>(ReadOnly | DontDelete); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1162 | global_template->Set(String::New("ArrayBuffer"), |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame^] | 1163 | CreateArrayBufferTemplate(ArrayBuffer), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1164 | global_template->Set(String::New("Int8Array"), |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1165 | CreateArrayTemplate(Int8Array), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1166 | global_template->Set(String::New("Uint8Array"), |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1167 | CreateArrayTemplate(Uint8Array), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1168 | global_template->Set(String::New("Int16Array"), |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1169 | CreateArrayTemplate(Int16Array), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1170 | global_template->Set(String::New("Uint16Array"), |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1171 | CreateArrayTemplate(Uint16Array), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1172 | global_template->Set(String::New("Int32Array"), |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1173 | CreateArrayTemplate(Int32Array), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1174 | global_template->Set(String::New("Uint32Array"), |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1175 | CreateArrayTemplate(Uint32Array), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1176 | global_template->Set(String::New("Float32Array"), |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1177 | CreateArrayTemplate(Float32Array), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1178 | global_template->Set(String::New("Float64Array"), |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 1179 | CreateArrayTemplate(Float64Array), attr); |
| 1180 | global_template->Set(String::New("Uint8ClampedArray"), |
| 1181 | CreateArrayTemplate(Uint8ClampedArray), attr); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1182 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1183 | #ifdef LIVE_OBJECT_LIST |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1184 | global_template->Set(String::New("lol_is_enabled"), True()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1185 | #else |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1186 | global_template->Set(String::New("lol_is_enabled"), False()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1187 | #endif |
| 1188 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1189 | #if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64) |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1190 | Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); |
| 1191 | AddOSMethods(os_templ); |
| 1192 | global_template->Set(String::New("os"), os_templ); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1193 | #endif // V8_SHARED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1194 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1195 | return global_template; |
| 1196 | } |
| 1197 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1198 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1199 | void Shell::Initialize() { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1200 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 1201 | BZip2Decompressor startup_data_decompressor; |
| 1202 | int bz2_result = startup_data_decompressor.Decompress(); |
| 1203 | if (bz2_result != BZ_OK) { |
| 1204 | fprintf(stderr, "bzip error code: %d\n", bz2_result); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1205 | Exit(1); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1206 | } |
| 1207 | #endif |
| 1208 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1209 | #ifndef V8_SHARED |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1210 | Shell::counter_map_ = new CounterMap(); |
| 1211 | // Set up counters |
| 1212 | if (i::StrLength(i::FLAG_map_counters) != 0) |
| 1213 | MapCounters(i::FLAG_map_counters); |
| 1214 | if (i::FLAG_dump_counters) { |
| 1215 | V8::SetCounterFunction(LookupCounter); |
| 1216 | V8::SetCreateHistogramFunction(CreateHistogram); |
| 1217 | V8::SetAddHistogramSampleFunction(AddHistogramSample); |
| 1218 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1219 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1220 | if (options.test_shell) return; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1221 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1222 | #ifndef V8_SHARED |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1223 | Locker lock; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1224 | HandleScope scope; |
| 1225 | Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1226 | utility_context_ = Context::New(NULL, global_template); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1227 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1228 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1229 | // Start the debugger agent if requested. |
| 1230 | if (i::FLAG_debugger_agent) { |
| 1231 | v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1232 | v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1233 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1234 | #endif // ENABLE_DEBUGGER_SUPPORT |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1235 | #endif // V8_SHARED |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1236 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1237 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1238 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1239 | Persistent<Context> Shell::CreateEvaluationContext() { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1240 | #ifndef V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1241 | // This needs to be a critical section since this is not thread-safe |
| 1242 | i::ScopedLock lock(context_mutex_); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1243 | #endif // V8_SHARED |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1244 | // Initialize the global objects |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1245 | Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1246 | Persistent<Context> context = Context::New(NULL, global_template); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1247 | ASSERT(!context.IsEmpty()); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1248 | Context::Scope scope(context); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1249 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1250 | #ifndef V8_SHARED |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1251 | i::JSArguments js_args = i::FLAG_js_arguments; |
| 1252 | i::Handle<i::FixedArray> arguments_array = |
| 1253 | FACTORY->NewFixedArray(js_args.argc()); |
| 1254 | for (int j = 0; j < js_args.argc(); j++) { |
| 1255 | i::Handle<i::String> arg = |
| 1256 | FACTORY->NewStringFromUtf8(i::CStrVector(js_args[j])); |
| 1257 | arguments_array->set(j, *arg); |
| 1258 | } |
| 1259 | i::Handle<i::JSArray> arguments_jsarray = |
| 1260 | FACTORY->NewJSArrayWithElements(arguments_array); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1261 | context->Global()->Set(String::New("arguments"), |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1262 | Utils::ToLocal(arguments_jsarray)); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1263 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1264 | return context; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1268 | void Shell::Exit(int exit_code) { |
| 1269 | // Use _exit instead of exit to avoid races between isolate |
| 1270 | // threads and static destructors. |
| 1271 | fflush(stdout); |
| 1272 | fflush(stderr); |
| 1273 | _exit(exit_code); |
| 1274 | } |
| 1275 | |
| 1276 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1277 | #ifndef V8_SHARED |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1278 | struct CounterAndKey { |
| 1279 | Counter* counter; |
| 1280 | const char* key; |
| 1281 | }; |
| 1282 | |
| 1283 | |
| 1284 | int CompareKeys(const void* a, const void* b) { |
| 1285 | return strcmp(static_cast<const CounterAndKey*>(a)->key, |
| 1286 | static_cast<const CounterAndKey*>(b)->key); |
| 1287 | } |
| 1288 | |
| 1289 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1290 | void Shell::OnExit() { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1291 | if (console != NULL) console->Close(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1292 | if (i::FLAG_dump_counters) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1293 | int number_of_counters = 0; |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1294 | for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1295 | number_of_counters++; |
| 1296 | } |
| 1297 | CounterAndKey* counters = new CounterAndKey[number_of_counters]; |
| 1298 | int j = 0; |
| 1299 | for (CounterMap::Iterator i(counter_map_); i.More(); i.Next(), j++) { |
| 1300 | counters[j].counter = i.CurrentValue(); |
| 1301 | counters[j].key = i.CurrentKey(); |
| 1302 | } |
| 1303 | qsort(counters, number_of_counters, sizeof(counters[0]), CompareKeys); |
| 1304 | printf("+--------------------------------------------+-------------+\n"); |
| 1305 | printf("| Name | Value |\n"); |
| 1306 | printf("+--------------------------------------------+-------------+\n"); |
| 1307 | for (j = 0; j < number_of_counters; j++) { |
| 1308 | Counter* counter = counters[j].counter; |
| 1309 | const char* key = counters[j].key; |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1310 | if (counter->is_histogram()) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1311 | printf("| c:%-40s | %11i |\n", key, counter->count()); |
| 1312 | printf("| t:%-40s | %11i |\n", key, counter->sample_total()); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1313 | } else { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1314 | printf("| %-42s | %11i |\n", key, counter->count()); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1315 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1316 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1317 | printf("+--------------------------------------------+-------------+\n"); |
| 1318 | delete [] counters; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1319 | } |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 1320 | delete counters_file_; |
| 1321 | delete counter_map_; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1322 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1323 | #endif // V8_SHARED |
| 1324 | |
| 1325 | |
| 1326 | static FILE* FOpen(const char* path, const char* mode) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1327 | #if defined(_MSC_VER) && (defined(_WIN32) || defined(_WIN64)) |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1328 | FILE* result; |
| 1329 | if (fopen_s(&result, path, mode) == 0) { |
| 1330 | return result; |
| 1331 | } else { |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | #else |
| 1335 | FILE* file = fopen(path, mode); |
| 1336 | if (file == NULL) return NULL; |
| 1337 | struct stat file_stat; |
| 1338 | if (fstat(fileno(file), &file_stat) != 0) return NULL; |
| 1339 | bool is_regular_file = ((file_stat.st_mode & S_IFREG) != 0); |
| 1340 | if (is_regular_file) return file; |
| 1341 | fclose(file); |
| 1342 | return NULL; |
| 1343 | #endif |
| 1344 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1345 | |
| 1346 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1347 | static char* ReadChars(const char* name, int* size_out) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1348 | // Release the V8 lock while reading files. |
| 1349 | v8::Unlocker unlocker(Isolate::GetCurrent()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1350 | FILE* file = FOpen(name, "rb"); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1351 | if (file == NULL) return NULL; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1352 | |
| 1353 | fseek(file, 0, SEEK_END); |
| 1354 | int size = ftell(file); |
| 1355 | rewind(file); |
| 1356 | |
| 1357 | char* chars = new char[size + 1]; |
| 1358 | chars[size] = '\0'; |
| 1359 | for (int i = 0; i < size;) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 1360 | int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1361 | i += read; |
| 1362 | } |
| 1363 | fclose(file); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1364 | *size_out = size; |
| 1365 | return chars; |
| 1366 | } |
| 1367 | |
| 1368 | |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1369 | Handle<Value> Shell::ReadBuffer(const Arguments& args) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1370 | ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1371 | String::Utf8Value filename(args[0]); |
| 1372 | int length; |
| 1373 | if (*filename == NULL) { |
| 1374 | return ThrowException(String::New("Error loading file")); |
| 1375 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1376 | |
| 1377 | uint8_t* data = reinterpret_cast<uint8_t*>(ReadChars(*filename, &length)); |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1378 | if (data == NULL) { |
| 1379 | return ThrowException(String::New("Error reading file")); |
| 1380 | } |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1381 | Handle<Object> buffer = Object::New(); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 1382 | buffer->SetHiddenValue(String::New(kArrayBufferMarkerPropName), True()); |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1383 | Persistent<Object> persistent_buffer = Persistent<Object>::New(buffer); |
| 1384 | persistent_buffer.MakeWeak(data, ExternalArrayWeakCallback); |
| 1385 | persistent_buffer.MarkIndependent(); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1386 | V8::AdjustAmountOfExternalAllocatedMemory(length); |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1387 | |
| 1388 | buffer->SetIndexedPropertiesToExternalArrayData( |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1389 | data, kExternalUnsignedByteArray, length); |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1390 | buffer->Set(String::New("byteLength"), |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1391 | Int32::New(static_cast<int32_t>(length)), ReadOnly); |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 1392 | return buffer; |
| 1393 | } |
| 1394 | |
| 1395 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1396 | #ifndef V8_SHARED |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1397 | static char* ReadToken(char* data, char token) { |
| 1398 | char* next = i::OS::StrChr(data, token); |
| 1399 | if (next != NULL) { |
| 1400 | *next = '\0'; |
| 1401 | return (next + 1); |
| 1402 | } |
| 1403 | |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | |
| 1407 | |
| 1408 | static char* ReadLine(char* data) { |
| 1409 | return ReadToken(data, '\n'); |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | static char* ReadWord(char* data) { |
| 1414 | return ReadToken(data, ' '); |
| 1415 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1416 | #endif // V8_SHARED |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1417 | |
| 1418 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1419 | // Reads a file into a v8 string. |
| 1420 | Handle<String> Shell::ReadFile(const char* name) { |
| 1421 | int size = 0; |
| 1422 | char* chars = ReadChars(name, &size); |
| 1423 | if (chars == NULL) return Handle<String>(); |
| 1424 | Handle<String> result = String::New(chars); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1425 | delete[] chars; |
| 1426 | return result; |
| 1427 | } |
| 1428 | |
| 1429 | |
| 1430 | void Shell::RunShell() { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1431 | Locker locker; |
| 1432 | Context::Scope context_scope(evaluation_context_); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1433 | HandleScope outer_scope; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1434 | Handle<String> name = String::New("(d8)"); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1435 | console = LineEditor::Get(); |
| 1436 | printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1437 | console->Open(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1438 | while (true) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1439 | HandleScope inner_scope; |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1440 | Handle<String> input = console->Prompt(Shell::kPrompt); |
| 1441 | if (input.IsEmpty()) break; |
| 1442 | ExecuteString(input, name, true, true); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1443 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1444 | printf("\n"); |
| 1445 | } |
| 1446 | |
| 1447 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1448 | #ifndef V8_SHARED |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1449 | class ShellThread : public i::Thread { |
| 1450 | public: |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1451 | // Takes ownership of the underlying char array of |files|. |
| 1452 | ShellThread(int no, char* files) |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1453 | : Thread("d8:ShellThread"), |
| 1454 | no_(no), files_(files) { } |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1455 | |
| 1456 | ~ShellThread() { |
| 1457 | delete[] files_; |
| 1458 | } |
| 1459 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1460 | virtual void Run(); |
| 1461 | private: |
| 1462 | int no_; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1463 | char* files_; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1464 | }; |
| 1465 | |
| 1466 | |
| 1467 | void ShellThread::Run() { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1468 | char* ptr = files_; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1469 | while ((ptr != NULL) && (*ptr != '\0')) { |
| 1470 | // For each newline-separated line. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1471 | char* next_line = ReadLine(ptr); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1472 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1473 | if (*ptr == '#') { |
| 1474 | // Skip comment lines. |
| 1475 | ptr = next_line; |
| 1476 | continue; |
| 1477 | } |
| 1478 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1479 | // Prepare the context for this thread. |
| 1480 | Locker locker; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1481 | HandleScope outer_scope; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1482 | Persistent<Context> thread_context = Shell::CreateEvaluationContext(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1483 | Context::Scope context_scope(thread_context); |
| 1484 | |
| 1485 | while ((ptr != NULL) && (*ptr != '\0')) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1486 | HandleScope inner_scope; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1487 | char* filename = ptr; |
| 1488 | ptr = ReadWord(ptr); |
| 1489 | |
| 1490 | // Skip empty strings. |
| 1491 | if (strlen(filename) == 0) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1492 | continue; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | Handle<String> str = Shell::ReadFile(filename); |
| 1496 | if (str.IsEmpty()) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1497 | printf("File '%s' not found\n", filename); |
| 1498 | Shell::Exit(1); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | Shell::ExecuteString(str, String::New(filename), false, false); |
| 1502 | } |
| 1503 | |
| 1504 | thread_context.Dispose(); |
| 1505 | ptr = next_line; |
| 1506 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1507 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1508 | #endif // V8_SHARED |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1509 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1510 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1511 | SourceGroup::~SourceGroup() { |
| 1512 | #ifndef V8_SHARED |
| 1513 | delete next_semaphore_; |
| 1514 | next_semaphore_ = NULL; |
| 1515 | delete done_semaphore_; |
| 1516 | done_semaphore_ = NULL; |
| 1517 | delete thread_; |
| 1518 | thread_ = NULL; |
| 1519 | #endif // V8_SHARED |
| 1520 | } |
| 1521 | |
| 1522 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1523 | void SourceGroup::Execute() { |
| 1524 | for (int i = begin_offset_; i < end_offset_; ++i) { |
| 1525 | const char* arg = argv_[i]; |
| 1526 | if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { |
| 1527 | // Execute argument given to -e option directly. |
| 1528 | HandleScope handle_scope; |
| 1529 | Handle<String> file_name = String::New("unnamed"); |
| 1530 | Handle<String> source = String::New(argv_[i + 1]); |
| 1531 | if (!Shell::ExecuteString(source, file_name, false, true)) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1532 | Shell::Exit(1); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1533 | } |
| 1534 | ++i; |
| 1535 | } else if (arg[0] == '-') { |
| 1536 | // Ignore other options. They have been parsed already. |
| 1537 | } else { |
| 1538 | // Use all other arguments as names of files to load and run. |
| 1539 | HandleScope handle_scope; |
| 1540 | Handle<String> file_name = String::New(arg); |
| 1541 | Handle<String> source = ReadFile(arg); |
| 1542 | if (source.IsEmpty()) { |
| 1543 | printf("Error reading '%s'\n", arg); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1544 | Shell::Exit(1); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1545 | } |
| 1546 | if (!Shell::ExecuteString(source, file_name, false, true)) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1547 | Shell::Exit(1); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | |
| 1554 | Handle<String> SourceGroup::ReadFile(const char* name) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1555 | int size; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1556 | char* chars = ReadChars(name, &size); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1557 | if (chars == NULL) return Handle<String>(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1558 | Handle<String> result = String::New(chars, size); |
| 1559 | delete[] chars; |
| 1560 | return result; |
| 1561 | } |
| 1562 | |
| 1563 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1564 | #ifndef V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1565 | i::Thread::Options SourceGroup::GetThreadOptions() { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1566 | // On some systems (OSX 10.6) the stack size default is 0.5Mb or less |
| 1567 | // which is not enough to parse the big literal expressions used in tests. |
| 1568 | // The stack size should be at least StackGuard::kLimitSize + some |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1569 | // OS-specific padding for thread startup code. 2Mbytes seems to be enough. |
| 1570 | return i::Thread::Options("IsolateThread", 2 * MB); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | |
| 1574 | void SourceGroup::ExecuteInThread() { |
| 1575 | Isolate* isolate = Isolate::New(); |
| 1576 | do { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1577 | if (next_semaphore_ != NULL) next_semaphore_->Wait(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1578 | { |
| 1579 | Isolate::Scope iscope(isolate); |
| 1580 | Locker lock(isolate); |
| 1581 | HandleScope scope; |
| 1582 | Persistent<Context> context = Shell::CreateEvaluationContext(); |
| 1583 | { |
| 1584 | Context::Scope cscope(context); |
| 1585 | Execute(); |
| 1586 | } |
| 1587 | context.Dispose(); |
| 1588 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1589 | if (done_semaphore_ != NULL) done_semaphore_->Signal(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1590 | } while (!Shell::options.last_run); |
| 1591 | isolate->Dispose(); |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | void SourceGroup::StartExecuteInThread() { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1596 | if (thread_ == NULL) { |
| 1597 | thread_ = new IsolateThread(this); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1598 | thread_->Start(); |
| 1599 | } |
| 1600 | next_semaphore_->Signal(); |
| 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | void SourceGroup::WaitForThread() { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1605 | if (thread_ == NULL) return; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1606 | if (Shell::options.last_run) { |
| 1607 | thread_->Join(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1608 | } else { |
| 1609 | done_semaphore_->Wait(); |
| 1610 | } |
| 1611 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1612 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1613 | |
| 1614 | |
| 1615 | bool Shell::SetOptions(int argc, char* argv[]) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1616 | for (int i = 0; i < argc; i++) { |
| 1617 | if (strcmp(argv[i], "--stress-opt") == 0) { |
| 1618 | options.stress_opt = true; |
| 1619 | argv[i] = NULL; |
| 1620 | } else if (strcmp(argv[i], "--stress-deopt") == 0) { |
| 1621 | options.stress_deopt = true; |
| 1622 | argv[i] = NULL; |
| 1623 | } else if (strcmp(argv[i], "--noalways-opt") == 0) { |
| 1624 | // No support for stressing if we can't use --always-opt. |
| 1625 | options.stress_opt = false; |
| 1626 | options.stress_deopt = false; |
| 1627 | } else if (strcmp(argv[i], "--shell") == 0) { |
| 1628 | options.interactive_shell = true; |
| 1629 | argv[i] = NULL; |
| 1630 | } else if (strcmp(argv[i], "--test") == 0) { |
| 1631 | options.test_shell = true; |
| 1632 | argv[i] = NULL; |
| 1633 | } else if (strcmp(argv[i], "--preemption") == 0) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1634 | #ifdef V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1635 | printf("D8 with shared library does not support multi-threading\n"); |
| 1636 | return false; |
| 1637 | #else |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1638 | options.use_preemption = true; |
| 1639 | argv[i] = NULL; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1640 | #endif // V8_SHARED |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 1641 | } else if (strcmp(argv[i], "--nopreemption") == 0) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1642 | #ifdef V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1643 | printf("D8 with shared library does not support multi-threading\n"); |
| 1644 | return false; |
| 1645 | #else |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1646 | options.use_preemption = false; |
| 1647 | argv[i] = NULL; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1648 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1649 | } else if (strcmp(argv[i], "--preemption-interval") == 0) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1650 | #ifdef V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1651 | printf("D8 with shared library does not support multi-threading\n"); |
| 1652 | return false; |
| 1653 | #else |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1654 | if (++i < argc) { |
| 1655 | argv[i-1] = NULL; |
| 1656 | char* end = NULL; |
| 1657 | options.preemption_interval = strtol(argv[i], &end, 10); // NOLINT |
| 1658 | if (options.preemption_interval <= 0 |
| 1659 | || *end != '\0' |
| 1660 | || errno == ERANGE) { |
| 1661 | printf("Invalid value for --preemption-interval '%s'\n", argv[i]); |
| 1662 | return false; |
| 1663 | } |
| 1664 | argv[i] = NULL; |
| 1665 | } else { |
| 1666 | printf("Missing value for --preemption-interval\n"); |
| 1667 | return false; |
| 1668 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1669 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1670 | } else if (strcmp(argv[i], "-f") == 0) { |
| 1671 | // Ignore any -f flags for compatibility with other stand-alone |
| 1672 | // JavaScript engines. |
| 1673 | continue; |
| 1674 | } else if (strcmp(argv[i], "--isolate") == 0) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1675 | #ifdef V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1676 | printf("D8 with shared library does not support multi-threading\n"); |
| 1677 | return false; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1678 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1679 | options.num_isolates++; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1680 | } else if (strcmp(argv[i], "-p") == 0) { |
| 1681 | #ifdef V8_SHARED |
| 1682 | printf("D8 with shared library does not support multi-threading\n"); |
| 1683 | return false; |
| 1684 | #else |
| 1685 | options.num_parallel_files++; |
| 1686 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1687 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1688 | #ifdef V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1689 | else if (strcmp(argv[i], "--dump-counters") == 0) { |
| 1690 | printf("D8 with shared library does not include counters\n"); |
| 1691 | return false; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1692 | } else if (strcmp(argv[i], "--debugger") == 0) { |
| 1693 | printf("Javascript debugger not included\n"); |
| 1694 | return false; |
| 1695 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1696 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1699 | #ifndef V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1700 | // Run parallel threads if we are not using --isolate |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1701 | options.parallel_files = new char*[options.num_parallel_files]; |
| 1702 | int parallel_files_set = 0; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1703 | for (int i = 1; i < argc; i++) { |
| 1704 | if (argv[i] == NULL) continue; |
| 1705 | if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { |
| 1706 | if (options.num_isolates > 1) { |
| 1707 | printf("-p is not compatible with --isolate\n"); |
| 1708 | return false; |
| 1709 | } |
| 1710 | argv[i] = NULL; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1711 | i++; |
| 1712 | options.parallel_files[parallel_files_set] = argv[i]; |
| 1713 | parallel_files_set++; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1714 | argv[i] = NULL; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1715 | } |
| 1716 | } |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1717 | if (parallel_files_set != options.num_parallel_files) { |
| 1718 | printf("-p requires a file containing a list of files as parameter\n"); |
| 1719 | return false; |
| 1720 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1721 | #endif // V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1722 | |
| 1723 | v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 1724 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1725 | // Set up isolated source groups. |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1726 | options.isolate_sources = new SourceGroup[options.num_isolates]; |
| 1727 | SourceGroup* current = options.isolate_sources; |
| 1728 | current->Begin(argv, 1); |
| 1729 | for (int i = 1; i < argc; i++) { |
| 1730 | const char* str = argv[i]; |
| 1731 | if (strcmp(str, "--isolate") == 0) { |
| 1732 | current->End(i); |
| 1733 | current++; |
| 1734 | current->Begin(argv, i + 1); |
| 1735 | } else if (strncmp(argv[i], "--", 2) == 0) { |
| 1736 | printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]); |
| 1737 | } |
| 1738 | } |
| 1739 | current->End(argc); |
| 1740 | |
| 1741 | return true; |
| 1742 | } |
| 1743 | |
| 1744 | |
| 1745 | int Shell::RunMain(int argc, char* argv[]) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1746 | #ifndef V8_SHARED |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1747 | i::List<i::Thread*> threads(1); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1748 | if (options.parallel_files != NULL) { |
| 1749 | for (int i = 0; i < options.num_parallel_files; i++) { |
| 1750 | char* files = NULL; |
| 1751 | { Locker lock(Isolate::GetCurrent()); |
| 1752 | int size = 0; |
| 1753 | files = ReadChars(options.parallel_files[i], &size); |
| 1754 | } |
| 1755 | if (files == NULL) { |
| 1756 | printf("File list '%s' not found\n", options.parallel_files[i]); |
| 1757 | Exit(1); |
| 1758 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1759 | ShellThread* thread = new ShellThread(threads.length(), files); |
| 1760 | thread->Start(); |
| 1761 | threads.Add(thread); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1762 | } |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1763 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1764 | for (int i = 1; i < options.num_isolates; ++i) { |
| 1765 | options.isolate_sources[i].StartExecuteInThread(); |
| 1766 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1767 | #endif // V8_SHARED |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1768 | { // NOLINT |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1769 | Locker lock; |
| 1770 | HandleScope scope; |
| 1771 | Persistent<Context> context = CreateEvaluationContext(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1772 | if (options.last_run) { |
| 1773 | // Keep using the same context in the interactive shell. |
| 1774 | evaluation_context_ = context; |
| 1775 | #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) |
| 1776 | // If the interactive debugger is enabled make sure to activate |
| 1777 | // it before running the files passed on the command line. |
| 1778 | if (i::FLAG_debugger) { |
| 1779 | InstallUtilityScript(); |
| 1780 | } |
| 1781 | #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT |
| 1782 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1783 | { |
| 1784 | Context::Scope cscope(context); |
| 1785 | options.isolate_sources[0].Execute(); |
| 1786 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1787 | if (!options.last_run) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1788 | context.Dispose(); |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 1789 | #if !defined(V8_SHARED) |
| 1790 | if (i::FLAG_send_idle_notification) { |
| 1791 | const int kLongIdlePauseInMs = 1000; |
| 1792 | V8::ContextDisposedNotification(); |
| 1793 | V8::IdleNotification(kLongIdlePauseInMs); |
| 1794 | } |
| 1795 | #endif // !V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1796 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1797 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1798 | #ifndef V8_SHARED |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1799 | // Start preemption if threads have been created and preemption is enabled. |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1800 | if (threads.length() > 0 |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1801 | && options.use_preemption) { |
| 1802 | Locker::StartPreemption(options.preemption_interval); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1803 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1804 | #endif // V8_SHARED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1805 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1806 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1807 | #ifndef V8_SHARED |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1808 | for (int i = 1; i < options.num_isolates; ++i) { |
| 1809 | options.isolate_sources[i].WaitForThread(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1810 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1811 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1812 | for (int i = 0; i < threads.length(); i++) { |
| 1813 | i::Thread* thread = threads[i]; |
| 1814 | thread->Join(); |
| 1815 | delete thread; |
| 1816 | } |
| 1817 | |
| 1818 | if (threads.length() > 0 && options.use_preemption) { |
| 1819 | Locker lock; |
| 1820 | Locker::StopPreemption(); |
| 1821 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1822 | #endif // V8_SHARED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1823 | return 0; |
| 1824 | } |
| 1825 | |
| 1826 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1827 | int Shell::Main(int argc, char* argv[]) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1828 | if (!SetOptions(argc, argv)) return 1; |
| 1829 | Initialize(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1830 | |
| 1831 | int result = 0; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1832 | if (options.stress_opt || options.stress_deopt) { |
| 1833 | Testing::SetStressRunType( |
| 1834 | options.stress_opt ? Testing::kStressTypeOpt |
| 1835 | : Testing::kStressTypeDeopt); |
| 1836 | int stress_runs = Testing::GetStressRuns(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1837 | for (int i = 0; i < stress_runs && result == 0; i++) { |
| 1838 | printf("============ Stress %d/%d ============\n", i + 1, stress_runs); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1839 | Testing::PrepareStressRun(i); |
| 1840 | options.last_run = (i == stress_runs - 1); |
| 1841 | result = RunMain(argc, argv); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1842 | } |
| 1843 | printf("======== Full Deoptimization =======\n"); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1844 | Testing::DeoptimizeAll(); |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 1845 | #if !defined(V8_SHARED) |
| 1846 | } else if (i::FLAG_stress_runs > 0) { |
| 1847 | int stress_runs = i::FLAG_stress_runs; |
| 1848 | for (int i = 0; i < stress_runs && result == 0; i++) { |
| 1849 | printf("============ Run %d/%d ============\n", i + 1, stress_runs); |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 1850 | options.last_run = (i == stress_runs - 1); |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 1851 | result = RunMain(argc, argv); |
| 1852 | } |
| 1853 | #endif |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1854 | } else { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1855 | result = RunMain(argc, argv); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1858 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1859 | #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1860 | // Run remote debugger if requested, but never on --test |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1861 | if (i::FLAG_remote_debugger && !options.test_shell) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1862 | InstallUtilityScript(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1863 | RunRemoteDebugger(i::FLAG_debugger_port); |
| 1864 | return 0; |
| 1865 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1866 | #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1867 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1868 | // Run interactive shell if explicitly requested or if no script has been |
| 1869 | // executed, but never on --test |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1870 | |
| 1871 | if (( options.interactive_shell |
| 1872 | || !options.script_executed ) |
| 1873 | && !options.test_shell ) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1874 | #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) |
| 1875 | if (!i::FLAG_debugger) { |
| 1876 | InstallUtilityScript(); |
| 1877 | } |
| 1878 | #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1879 | RunShell(); |
| 1880 | } |
| 1881 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1882 | V8::Dispose(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1883 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 1884 | #ifndef V8_SHARED |
| 1885 | OnExit(); |
| 1886 | #endif // V8_SHARED |
| 1887 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1888 | return result; |
| 1889 | } |
| 1890 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1891 | } // namespace v8 |
| 1892 | |
| 1893 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1894 | #ifndef GOOGLE3 |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1895 | int main(int argc, char* argv[]) { |
| 1896 | return v8::Shell::Main(argc, argv); |
| 1897 | } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1898 | #endif |