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