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