fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +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 | |
| 29 | #include "v8.h" |
| 30 | |
| 31 | #include "liveedit.h" |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 32 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | #include "compilation-cache.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 34 | #include "compiler.h" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 35 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 36 | #include "deoptimizer.h" |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 37 | #include "global-handles.h" |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 38 | #include "parser.h" |
| 39 | #include "scopeinfo.h" |
| 40 | #include "scopes.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 41 | #include "v8memory.h" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 42 | |
| 43 | namespace v8 { |
| 44 | namespace internal { |
| 45 | |
| 46 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 47 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 48 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 49 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 50 | void SetElementNonStrict(Handle<JSObject> object, |
| 51 | uint32_t index, |
| 52 | Handle<Object> value) { |
| 53 | // Ignore return value from SetElement. It can only be a failure if there |
| 54 | // are element setters causing exceptions and the debugger context has none |
| 55 | // of these. |
| 56 | Handle<Object> no_failure; |
| 57 | no_failure = SetElement(object, index, value, kNonStrictMode); |
| 58 | ASSERT(!no_failure.is_null()); |
| 59 | USE(no_failure); |
| 60 | } |
| 61 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 62 | // A simple implementation of dynamic programming algorithm. It solves |
| 63 | // the problem of finding the difference of 2 arrays. It uses a table of results |
| 64 | // of subproblems. Each cell contains a number together with 2-bit flag |
| 65 | // that helps building the chunk list. |
| 66 | class Differencer { |
| 67 | public: |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 68 | explicit Differencer(Comparator::Input* input) |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 69 | : input_(input), len1_(input->GetLength1()), len2_(input->GetLength2()) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 70 | buffer_ = NewArray<int>(len1_ * len2_); |
| 71 | } |
| 72 | ~Differencer() { |
| 73 | DeleteArray(buffer_); |
| 74 | } |
| 75 | |
| 76 | void Initialize() { |
| 77 | int array_size = len1_ * len2_; |
| 78 | for (int i = 0; i < array_size; i++) { |
| 79 | buffer_[i] = kEmptyCellValue; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Makes sure that result for the full problem is calculated and stored |
| 84 | // in the table together with flags showing a path through subproblems. |
| 85 | void FillTable() { |
| 86 | CompareUpToTail(0, 0); |
| 87 | } |
| 88 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 89 | void SaveResult(Comparator::Output* chunk_writer) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 90 | ResultWriter writer(chunk_writer); |
| 91 | |
| 92 | int pos1 = 0; |
| 93 | int pos2 = 0; |
| 94 | while (true) { |
| 95 | if (pos1 < len1_) { |
| 96 | if (pos2 < len2_) { |
| 97 | Direction dir = get_direction(pos1, pos2); |
| 98 | switch (dir) { |
| 99 | case EQ: |
| 100 | writer.eq(); |
| 101 | pos1++; |
| 102 | pos2++; |
| 103 | break; |
| 104 | case SKIP1: |
| 105 | writer.skip1(1); |
| 106 | pos1++; |
| 107 | break; |
| 108 | case SKIP2: |
| 109 | case SKIP_ANY: |
| 110 | writer.skip2(1); |
| 111 | pos2++; |
| 112 | break; |
| 113 | default: |
| 114 | UNREACHABLE(); |
| 115 | } |
| 116 | } else { |
| 117 | writer.skip1(len1_ - pos1); |
| 118 | break; |
| 119 | } |
| 120 | } else { |
| 121 | if (len2_ != pos2) { |
| 122 | writer.skip2(len2_ - pos2); |
| 123 | } |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | writer.close(); |
| 128 | } |
| 129 | |
| 130 | private: |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 131 | Comparator::Input* input_; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 132 | int* buffer_; |
| 133 | int len1_; |
| 134 | int len2_; |
| 135 | |
| 136 | enum Direction { |
| 137 | EQ = 0, |
| 138 | SKIP1, |
| 139 | SKIP2, |
| 140 | SKIP_ANY, |
| 141 | |
| 142 | MAX_DIRECTION_FLAG_VALUE = SKIP_ANY |
| 143 | }; |
| 144 | |
| 145 | // Computes result for a subtask and optionally caches it in the buffer table. |
| 146 | // All results values are shifted to make space for flags in the lower bits. |
| 147 | int CompareUpToTail(int pos1, int pos2) { |
| 148 | if (pos1 < len1_) { |
| 149 | if (pos2 < len2_) { |
| 150 | int cached_res = get_value4(pos1, pos2); |
| 151 | if (cached_res == kEmptyCellValue) { |
| 152 | Direction dir; |
| 153 | int res; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 154 | if (input_->Equals(pos1, pos2)) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 155 | res = CompareUpToTail(pos1 + 1, pos2 + 1); |
| 156 | dir = EQ; |
| 157 | } else { |
| 158 | int res1 = CompareUpToTail(pos1 + 1, pos2) + |
| 159 | (1 << kDirectionSizeBits); |
| 160 | int res2 = CompareUpToTail(pos1, pos2 + 1) + |
| 161 | (1 << kDirectionSizeBits); |
| 162 | if (res1 == res2) { |
| 163 | res = res1; |
| 164 | dir = SKIP_ANY; |
| 165 | } else if (res1 < res2) { |
| 166 | res = res1; |
| 167 | dir = SKIP1; |
| 168 | } else { |
| 169 | res = res2; |
| 170 | dir = SKIP2; |
| 171 | } |
| 172 | } |
| 173 | set_value4_and_dir(pos1, pos2, res, dir); |
| 174 | cached_res = res; |
| 175 | } |
| 176 | return cached_res; |
| 177 | } else { |
| 178 | return (len1_ - pos1) << kDirectionSizeBits; |
| 179 | } |
| 180 | } else { |
| 181 | return (len2_ - pos2) << kDirectionSizeBits; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | inline int& get_cell(int i1, int i2) { |
| 186 | return buffer_[i1 + i2 * len1_]; |
| 187 | } |
| 188 | |
| 189 | // Each cell keeps a value plus direction. Value is multiplied by 4. |
| 190 | void set_value4_and_dir(int i1, int i2, int value4, Direction dir) { |
| 191 | ASSERT((value4 & kDirectionMask) == 0); |
| 192 | get_cell(i1, i2) = value4 | dir; |
| 193 | } |
| 194 | |
| 195 | int get_value4(int i1, int i2) { |
| 196 | return get_cell(i1, i2) & (kMaxUInt32 ^ kDirectionMask); |
| 197 | } |
| 198 | Direction get_direction(int i1, int i2) { |
| 199 | return static_cast<Direction>(get_cell(i1, i2) & kDirectionMask); |
| 200 | } |
| 201 | |
| 202 | static const int kDirectionSizeBits = 2; |
| 203 | static const int kDirectionMask = (1 << kDirectionSizeBits) - 1; |
| 204 | static const int kEmptyCellValue = -1 << kDirectionSizeBits; |
| 205 | |
| 206 | // This method only holds static assert statement (unfortunately you cannot |
| 207 | // place one in class scope). |
| 208 | void StaticAssertHolder() { |
| 209 | STATIC_ASSERT(MAX_DIRECTION_FLAG_VALUE < (1 << kDirectionSizeBits)); |
| 210 | } |
| 211 | |
| 212 | class ResultWriter { |
| 213 | public: |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 214 | explicit ResultWriter(Comparator::Output* chunk_writer) |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 215 | : chunk_writer_(chunk_writer), pos1_(0), pos2_(0), |
| 216 | pos1_begin_(-1), pos2_begin_(-1), has_open_chunk_(false) { |
| 217 | } |
| 218 | void eq() { |
| 219 | FlushChunk(); |
| 220 | pos1_++; |
| 221 | pos2_++; |
| 222 | } |
| 223 | void skip1(int len1) { |
| 224 | StartChunk(); |
| 225 | pos1_ += len1; |
| 226 | } |
| 227 | void skip2(int len2) { |
| 228 | StartChunk(); |
| 229 | pos2_ += len2; |
| 230 | } |
| 231 | void close() { |
| 232 | FlushChunk(); |
| 233 | } |
| 234 | |
| 235 | private: |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 236 | Comparator::Output* chunk_writer_; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 237 | int pos1_; |
| 238 | int pos2_; |
| 239 | int pos1_begin_; |
| 240 | int pos2_begin_; |
| 241 | bool has_open_chunk_; |
| 242 | |
| 243 | void StartChunk() { |
| 244 | if (!has_open_chunk_) { |
| 245 | pos1_begin_ = pos1_; |
| 246 | pos2_begin_ = pos2_; |
| 247 | has_open_chunk_ = true; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void FlushChunk() { |
| 252 | if (has_open_chunk_) { |
| 253 | chunk_writer_->AddChunk(pos1_begin_, pos2_begin_, |
| 254 | pos1_ - pos1_begin_, pos2_ - pos2_begin_); |
| 255 | has_open_chunk_ = false; |
| 256 | } |
| 257 | } |
| 258 | }; |
| 259 | }; |
| 260 | |
| 261 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 262 | void Comparator::CalculateDifference(Comparator::Input* input, |
| 263 | Comparator::Output* result_writer) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 264 | Differencer differencer(input); |
| 265 | differencer.Initialize(); |
| 266 | differencer.FillTable(); |
| 267 | differencer.SaveResult(result_writer); |
| 268 | } |
| 269 | |
| 270 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 271 | static bool CompareSubstrings(Handle<String> s1, int pos1, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 272 | Handle<String> s2, int pos2, int len) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 273 | for (int i = 0; i < len; i++) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 274 | if (s1->Get(i + pos1) != s2->Get(i + pos2)) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 282 | // Additional to Input interface. Lets switch Input range to subrange. |
| 283 | // More elegant way would be to wrap one Input as another Input object |
| 284 | // and translate positions there, but that would cost us additional virtual |
| 285 | // call per comparison. |
| 286 | class SubrangableInput : public Comparator::Input { |
| 287 | public: |
| 288 | virtual void SetSubrange1(int offset, int len) = 0; |
| 289 | virtual void SetSubrange2(int offset, int len) = 0; |
| 290 | }; |
| 291 | |
| 292 | |
| 293 | class SubrangableOutput : public Comparator::Output { |
| 294 | public: |
| 295 | virtual void SetSubrange1(int offset, int len) = 0; |
| 296 | virtual void SetSubrange2(int offset, int len) = 0; |
| 297 | }; |
| 298 | |
| 299 | |
| 300 | static int min(int a, int b) { |
| 301 | return a < b ? a : b; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | // Finds common prefix and suffix in input. This parts shouldn't take space in |
| 306 | // linear programming table. Enable subranging in input and output. |
| 307 | static void NarrowDownInput(SubrangableInput* input, |
| 308 | SubrangableOutput* output) { |
| 309 | const int len1 = input->GetLength1(); |
| 310 | const int len2 = input->GetLength2(); |
| 311 | |
| 312 | int common_prefix_len; |
| 313 | int common_suffix_len; |
| 314 | |
| 315 | { |
| 316 | common_prefix_len = 0; |
| 317 | int prefix_limit = min(len1, len2); |
| 318 | while (common_prefix_len < prefix_limit && |
| 319 | input->Equals(common_prefix_len, common_prefix_len)) { |
| 320 | common_prefix_len++; |
| 321 | } |
| 322 | |
| 323 | common_suffix_len = 0; |
| 324 | int suffix_limit = min(len1 - common_prefix_len, len2 - common_prefix_len); |
| 325 | |
| 326 | while (common_suffix_len < suffix_limit && |
| 327 | input->Equals(len1 - common_suffix_len - 1, |
| 328 | len2 - common_suffix_len - 1)) { |
| 329 | common_suffix_len++; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (common_prefix_len > 0 || common_suffix_len > 0) { |
| 334 | int new_len1 = len1 - common_suffix_len - common_prefix_len; |
| 335 | int new_len2 = len2 - common_suffix_len - common_prefix_len; |
| 336 | |
| 337 | input->SetSubrange1(common_prefix_len, new_len1); |
| 338 | input->SetSubrange2(common_prefix_len, new_len2); |
| 339 | |
| 340 | output->SetSubrange1(common_prefix_len, new_len1); |
| 341 | output->SetSubrange2(common_prefix_len, new_len2); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 346 | // A helper class that writes chunk numbers into JSArray. |
| 347 | // Each chunk is stored as 3 array elements: (pos1_begin, pos1_end, pos2_end). |
| 348 | class CompareOutputArrayWriter { |
| 349 | public: |
| 350 | CompareOutputArrayWriter() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 351 | : array_(FACTORY->NewJSArray(10)), current_size_(0) {} |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 352 | |
| 353 | Handle<JSArray> GetResult() { |
| 354 | return array_; |
| 355 | } |
| 356 | |
| 357 | void WriteChunk(int char_pos1, int char_pos2, int char_len1, int char_len2) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 358 | SetElementNonStrict(array_, |
| 359 | current_size_, |
| 360 | Handle<Object>(Smi::FromInt(char_pos1))); |
| 361 | SetElementNonStrict(array_, |
| 362 | current_size_ + 1, |
| 363 | Handle<Object>(Smi::FromInt(char_pos1 + char_len1))); |
| 364 | SetElementNonStrict(array_, |
| 365 | current_size_ + 2, |
| 366 | Handle<Object>(Smi::FromInt(char_pos2 + char_len2))); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 367 | current_size_ += 3; |
| 368 | } |
| 369 | |
| 370 | private: |
| 371 | Handle<JSArray> array_; |
| 372 | int current_size_; |
| 373 | }; |
| 374 | |
| 375 | |
| 376 | // Represents 2 strings as 2 arrays of tokens. |
| 377 | // TODO(LiveEdit): Currently it's actually an array of charactres. |
| 378 | // Make array of tokens instead. |
| 379 | class TokensCompareInput : public Comparator::Input { |
| 380 | public: |
| 381 | TokensCompareInput(Handle<String> s1, int offset1, int len1, |
| 382 | Handle<String> s2, int offset2, int len2) |
| 383 | : s1_(s1), offset1_(offset1), len1_(len1), |
| 384 | s2_(s2), offset2_(offset2), len2_(len2) { |
| 385 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 386 | virtual int GetLength1() { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 387 | return len1_; |
| 388 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 389 | virtual int GetLength2() { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 390 | return len2_; |
| 391 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 392 | bool Equals(int index1, int index2) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 393 | return s1_->Get(offset1_ + index1) == s2_->Get(offset2_ + index2); |
| 394 | } |
| 395 | |
| 396 | private: |
| 397 | Handle<String> s1_; |
| 398 | int offset1_; |
| 399 | int len1_; |
| 400 | Handle<String> s2_; |
| 401 | int offset2_; |
| 402 | int len2_; |
| 403 | }; |
| 404 | |
| 405 | |
| 406 | // Stores compare result in JSArray. Converts substring positions |
| 407 | // to absolute positions. |
| 408 | class TokensCompareOutput : public Comparator::Output { |
| 409 | public: |
| 410 | TokensCompareOutput(CompareOutputArrayWriter* array_writer, |
| 411 | int offset1, int offset2) |
| 412 | : array_writer_(array_writer), offset1_(offset1), offset2_(offset2) { |
| 413 | } |
| 414 | |
| 415 | void AddChunk(int pos1, int pos2, int len1, int len2) { |
| 416 | array_writer_->WriteChunk(pos1 + offset1_, pos2 + offset2_, len1, len2); |
| 417 | } |
| 418 | |
| 419 | private: |
| 420 | CompareOutputArrayWriter* array_writer_; |
| 421 | int offset1_; |
| 422 | int offset2_; |
| 423 | }; |
| 424 | |
| 425 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 426 | // Wraps raw n-elements line_ends array as a list of n+1 lines. The last line |
| 427 | // never has terminating new line character. |
| 428 | class LineEndsWrapper { |
| 429 | public: |
| 430 | explicit LineEndsWrapper(Handle<String> string) |
| 431 | : ends_array_(CalculateLineEnds(string, false)), |
| 432 | string_len_(string->length()) { |
| 433 | } |
| 434 | int length() { |
| 435 | return ends_array_->length() + 1; |
| 436 | } |
| 437 | // Returns start for any line including start of the imaginary line after |
| 438 | // the last line. |
| 439 | int GetLineStart(int index) { |
| 440 | if (index == 0) { |
| 441 | return 0; |
| 442 | } else { |
| 443 | return GetLineEnd(index - 1); |
| 444 | } |
| 445 | } |
| 446 | int GetLineEnd(int index) { |
| 447 | if (index == ends_array_->length()) { |
| 448 | // End of the last line is always an end of the whole string. |
| 449 | // If the string ends with a new line character, the last line is an |
| 450 | // empty string after this character. |
| 451 | return string_len_; |
| 452 | } else { |
| 453 | return GetPosAfterNewLine(index); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | private: |
| 458 | Handle<FixedArray> ends_array_; |
| 459 | int string_len_; |
| 460 | |
| 461 | int GetPosAfterNewLine(int index) { |
| 462 | return Smi::cast(ends_array_->get(index))->value() + 1; |
| 463 | } |
| 464 | }; |
| 465 | |
| 466 | |
| 467 | // Represents 2 strings as 2 arrays of lines. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 468 | class LineArrayCompareInput : public SubrangableInput { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 469 | public: |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 470 | LineArrayCompareInput(Handle<String> s1, Handle<String> s2, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 471 | LineEndsWrapper line_ends1, LineEndsWrapper line_ends2) |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 472 | : s1_(s1), s2_(s2), line_ends1_(line_ends1), |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 473 | line_ends2_(line_ends2), |
| 474 | subrange_offset1_(0), subrange_offset2_(0), |
| 475 | subrange_len1_(line_ends1_.length()), |
| 476 | subrange_len2_(line_ends2_.length()) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 477 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 478 | int GetLength1() { |
| 479 | return subrange_len1_; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 480 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 481 | int GetLength2() { |
| 482 | return subrange_len2_; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 483 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 484 | bool Equals(int index1, int index2) { |
| 485 | index1 += subrange_offset1_; |
| 486 | index2 += subrange_offset2_; |
| 487 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 488 | int line_start1 = line_ends1_.GetLineStart(index1); |
| 489 | int line_start2 = line_ends2_.GetLineStart(index2); |
| 490 | int line_end1 = line_ends1_.GetLineEnd(index1); |
| 491 | int line_end2 = line_ends2_.GetLineEnd(index2); |
| 492 | int len1 = line_end1 - line_start1; |
| 493 | int len2 = line_end2 - line_start2; |
| 494 | if (len1 != len2) { |
| 495 | return false; |
| 496 | } |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 497 | return CompareSubstrings(s1_, line_start1, s2_, line_start2, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 498 | len1); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 499 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 500 | void SetSubrange1(int offset, int len) { |
| 501 | subrange_offset1_ = offset; |
| 502 | subrange_len1_ = len; |
| 503 | } |
| 504 | void SetSubrange2(int offset, int len) { |
| 505 | subrange_offset2_ = offset; |
| 506 | subrange_len2_ = len; |
| 507 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 508 | |
| 509 | private: |
| 510 | Handle<String> s1_; |
| 511 | Handle<String> s2_; |
| 512 | LineEndsWrapper line_ends1_; |
| 513 | LineEndsWrapper line_ends2_; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 514 | int subrange_offset1_; |
| 515 | int subrange_offset2_; |
| 516 | int subrange_len1_; |
| 517 | int subrange_len2_; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 521 | // Stores compare result in JSArray. For each chunk tries to conduct |
| 522 | // a fine-grained nested diff token-wise. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 523 | class TokenizingLineArrayCompareOutput : public SubrangableOutput { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 524 | public: |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 525 | TokenizingLineArrayCompareOutput(LineEndsWrapper line_ends1, |
| 526 | LineEndsWrapper line_ends2, |
| 527 | Handle<String> s1, Handle<String> s2) |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 528 | : line_ends1_(line_ends1), line_ends2_(line_ends2), s1_(s1), s2_(s2), |
| 529 | subrange_offset1_(0), subrange_offset2_(0) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void AddChunk(int line_pos1, int line_pos2, int line_len1, int line_len2) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 533 | line_pos1 += subrange_offset1_; |
| 534 | line_pos2 += subrange_offset2_; |
| 535 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 536 | int char_pos1 = line_ends1_.GetLineStart(line_pos1); |
| 537 | int char_pos2 = line_ends2_.GetLineStart(line_pos2); |
| 538 | int char_len1 = line_ends1_.GetLineStart(line_pos1 + line_len1) - char_pos1; |
| 539 | int char_len2 = line_ends2_.GetLineStart(line_pos2 + line_len2) - char_pos2; |
| 540 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 541 | if (char_len1 < CHUNK_LEN_LIMIT && char_len2 < CHUNK_LEN_LIMIT) { |
| 542 | // Chunk is small enough to conduct a nested token-level diff. |
| 543 | HandleScope subTaskScope; |
| 544 | |
| 545 | TokensCompareInput tokens_input(s1_, char_pos1, char_len1, |
| 546 | s2_, char_pos2, char_len2); |
| 547 | TokensCompareOutput tokens_output(&array_writer_, char_pos1, |
| 548 | char_pos2); |
| 549 | |
| 550 | Comparator::CalculateDifference(&tokens_input, &tokens_output); |
| 551 | } else { |
| 552 | array_writer_.WriteChunk(char_pos1, char_pos2, char_len1, char_len2); |
| 553 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 554 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 555 | void SetSubrange1(int offset, int len) { |
| 556 | subrange_offset1_ = offset; |
| 557 | } |
| 558 | void SetSubrange2(int offset, int len) { |
| 559 | subrange_offset2_ = offset; |
| 560 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 561 | |
| 562 | Handle<JSArray> GetResult() { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 563 | return array_writer_.GetResult(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | private: |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 567 | static const int CHUNK_LEN_LIMIT = 800; |
| 568 | |
| 569 | CompareOutputArrayWriter array_writer_; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 570 | LineEndsWrapper line_ends1_; |
| 571 | LineEndsWrapper line_ends2_; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 572 | Handle<String> s1_; |
| 573 | Handle<String> s2_; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 574 | int subrange_offset1_; |
| 575 | int subrange_offset2_; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 579 | Handle<JSArray> LiveEdit::CompareStrings(Handle<String> s1, |
| 580 | Handle<String> s2) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 581 | s1 = FlattenGetString(s1); |
| 582 | s2 = FlattenGetString(s2); |
| 583 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 584 | LineEndsWrapper line_ends1(s1); |
| 585 | LineEndsWrapper line_ends2(s2); |
| 586 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 587 | LineArrayCompareInput input(s1, s2, line_ends1, line_ends2); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 588 | TokenizingLineArrayCompareOutput output(line_ends1, line_ends2, s1, s2); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 589 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 590 | NarrowDownInput(&input, &output); |
| 591 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 592 | Comparator::CalculateDifference(&input, &output); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 593 | |
| 594 | return output.GetResult(); |
| 595 | } |
| 596 | |
| 597 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 598 | static void CompileScriptForTracker(Isolate* isolate, Handle<Script> script) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 599 | // TODO(635): support extensions. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 600 | PostponeInterruptsScope postpone(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 601 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 602 | // Build AST. |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 603 | CompilationInfo info(script); |
| 604 | info.MarkAsGlobal(); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 605 | if (ParserApi::Parse(&info)) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 606 | // Compile the code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 607 | LiveEditFunctionTracker tracker(info.isolate(), info.function()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 608 | if (Compiler::MakeCodeForLiveEdit(&info)) { |
| 609 | ASSERT(!info.code().is_null()); |
| 610 | tracker.RecordRootFunctionInfo(info.code()); |
| 611 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 612 | info.isolate()->StackOverflow(); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 613 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 614 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 615 | } |
| 616 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 617 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 618 | // Unwraps JSValue object, returning its field "value" |
| 619 | static Handle<Object> UnwrapJSValue(Handle<JSValue> jsValue) { |
| 620 | return Handle<Object>(jsValue->value()); |
| 621 | } |
| 622 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 623 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 624 | // Wraps any object into a OpaqueReference, that will hide the object |
| 625 | // from JavaScript. |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 626 | static Handle<JSValue> WrapInJSValue(Handle<Object> object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 627 | Handle<JSFunction> constructor = |
| 628 | Isolate::Current()->opaque_reference_function(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 629 | Handle<JSValue> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 630 | Handle<JSValue>::cast(FACTORY->NewJSObject(constructor)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 631 | result->set_value(*object); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 632 | return result; |
| 633 | } |
| 634 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 635 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 636 | // Simple helper class that creates more or less typed structures over |
| 637 | // JSArray object. This is an adhoc method of passing structures from C++ |
| 638 | // to JavaScript. |
| 639 | template<typename S> |
| 640 | class JSArrayBasedStruct { |
| 641 | public: |
| 642 | static S Create() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 643 | Handle<JSArray> array = FACTORY->NewJSArray(S::kSize_); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 644 | return S(array); |
| 645 | } |
| 646 | static S cast(Object* object) { |
| 647 | JSArray* array = JSArray::cast(object); |
| 648 | Handle<JSArray> array_handle(array); |
| 649 | return S(array_handle); |
| 650 | } |
| 651 | explicit JSArrayBasedStruct(Handle<JSArray> array) : array_(array) { |
| 652 | } |
| 653 | Handle<JSArray> GetJSArray() { |
| 654 | return array_; |
| 655 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 656 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 657 | protected: |
| 658 | void SetField(int field_position, Handle<Object> value) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 659 | SetElementNonStrict(array_, field_position, value); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 660 | } |
| 661 | void SetSmiValueField(int field_position, int value) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 662 | SetElementNonStrict(array_, |
| 663 | field_position, |
| 664 | Handle<Smi>(Smi::FromInt(value))); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 665 | } |
| 666 | Object* GetField(int field_position) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 667 | return array_->GetElementNoExceptionThrown(field_position); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 668 | } |
| 669 | int GetSmiValueField(int field_position) { |
| 670 | Object* res = GetField(field_position); |
| 671 | return Smi::cast(res)->value(); |
| 672 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 673 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 674 | private: |
| 675 | Handle<JSArray> array_; |
| 676 | }; |
| 677 | |
| 678 | |
| 679 | // Represents some function compilation details. This structure will be used |
| 680 | // from JavaScript. It contains Code object, which is kept wrapped |
| 681 | // into a BlindReference for sanitizing reasons. |
| 682 | class FunctionInfoWrapper : public JSArrayBasedStruct<FunctionInfoWrapper> { |
| 683 | public: |
| 684 | explicit FunctionInfoWrapper(Handle<JSArray> array) |
| 685 | : JSArrayBasedStruct<FunctionInfoWrapper>(array) { |
| 686 | } |
| 687 | void SetInitialProperties(Handle<String> name, int start_position, |
| 688 | int end_position, int param_num, int parent_index) { |
| 689 | HandleScope scope; |
| 690 | this->SetField(kFunctionNameOffset_, name); |
| 691 | this->SetSmiValueField(kStartPositionOffset_, start_position); |
| 692 | this->SetSmiValueField(kEndPositionOffset_, end_position); |
| 693 | this->SetSmiValueField(kParamNumOffset_, param_num); |
| 694 | this->SetSmiValueField(kParentIndexOffset_, parent_index); |
| 695 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 696 | void SetFunctionCode(Handle<Code> function_code, |
| 697 | Handle<Object> code_scope_info) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 698 | Handle<JSValue> code_wrapper = WrapInJSValue(function_code); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 699 | this->SetField(kCodeOffset_, code_wrapper); |
| 700 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 701 | Handle<JSValue> scope_wrapper = WrapInJSValue(code_scope_info); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 702 | this->SetField(kCodeScopeInfoOffset_, scope_wrapper); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 703 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 704 | void SetOuterScopeInfo(Handle<Object> scope_info_array) { |
| 705 | this->SetField(kOuterScopeInfoOffset_, scope_info_array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 706 | } |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 707 | void SetSharedFunctionInfo(Handle<SharedFunctionInfo> info) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 708 | Handle<JSValue> info_holder = WrapInJSValue(info); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 709 | this->SetField(kSharedFunctionInfoOffset_, info_holder); |
| 710 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 711 | int GetParentIndex() { |
| 712 | return this->GetSmiValueField(kParentIndexOffset_); |
| 713 | } |
| 714 | Handle<Code> GetFunctionCode() { |
| 715 | Handle<Object> raw_result = UnwrapJSValue(Handle<JSValue>( |
| 716 | JSValue::cast(this->GetField(kCodeOffset_)))); |
| 717 | return Handle<Code>::cast(raw_result); |
| 718 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 719 | Handle<Object> GetCodeScopeInfo() { |
| 720 | Handle<Object> raw_result = UnwrapJSValue(Handle<JSValue>( |
| 721 | JSValue::cast(this->GetField(kCodeScopeInfoOffset_)))); |
| 722 | return raw_result; |
| 723 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 724 | int GetStartPosition() { |
| 725 | return this->GetSmiValueField(kStartPositionOffset_); |
| 726 | } |
| 727 | int GetEndPosition() { |
| 728 | return this->GetSmiValueField(kEndPositionOffset_); |
| 729 | } |
| 730 | |
| 731 | private: |
| 732 | static const int kFunctionNameOffset_ = 0; |
| 733 | static const int kStartPositionOffset_ = 1; |
| 734 | static const int kEndPositionOffset_ = 2; |
| 735 | static const int kParamNumOffset_ = 3; |
| 736 | static const int kCodeOffset_ = 4; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 737 | static const int kCodeScopeInfoOffset_ = 5; |
| 738 | static const int kOuterScopeInfoOffset_ = 6; |
| 739 | static const int kParentIndexOffset_ = 7; |
| 740 | static const int kSharedFunctionInfoOffset_ = 8; |
| 741 | static const int kSize_ = 9; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 742 | |
| 743 | friend class JSArrayBasedStruct<FunctionInfoWrapper>; |
| 744 | }; |
| 745 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 746 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 747 | // Wraps SharedFunctionInfo along with some of its fields for passing it |
| 748 | // back to JavaScript. SharedFunctionInfo object itself is additionally |
| 749 | // wrapped into BlindReference for sanitizing reasons. |
| 750 | class SharedInfoWrapper : public JSArrayBasedStruct<SharedInfoWrapper> { |
| 751 | public: |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 752 | static bool IsInstance(Handle<JSArray> array) { |
| 753 | return array->length() == Smi::FromInt(kSize_) && |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 754 | array->GetElementNoExceptionThrown(kSharedInfoOffset_)->IsJSValue(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 755 | } |
| 756 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 757 | explicit SharedInfoWrapper(Handle<JSArray> array) |
| 758 | : JSArrayBasedStruct<SharedInfoWrapper>(array) { |
| 759 | } |
| 760 | |
| 761 | void SetProperties(Handle<String> name, int start_position, int end_position, |
| 762 | Handle<SharedFunctionInfo> info) { |
| 763 | HandleScope scope; |
| 764 | this->SetField(kFunctionNameOffset_, name); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 765 | Handle<JSValue> info_holder = WrapInJSValue(info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 766 | this->SetField(kSharedInfoOffset_, info_holder); |
| 767 | this->SetSmiValueField(kStartPositionOffset_, start_position); |
| 768 | this->SetSmiValueField(kEndPositionOffset_, end_position); |
| 769 | } |
| 770 | Handle<SharedFunctionInfo> GetInfo() { |
| 771 | Object* element = this->GetField(kSharedInfoOffset_); |
| 772 | Handle<JSValue> value_wrapper(JSValue::cast(element)); |
| 773 | Handle<Object> raw_result = UnwrapJSValue(value_wrapper); |
| 774 | return Handle<SharedFunctionInfo>::cast(raw_result); |
| 775 | } |
| 776 | |
| 777 | private: |
| 778 | static const int kFunctionNameOffset_ = 0; |
| 779 | static const int kStartPositionOffset_ = 1; |
| 780 | static const int kEndPositionOffset_ = 2; |
| 781 | static const int kSharedInfoOffset_ = 3; |
| 782 | static const int kSize_ = 4; |
| 783 | |
| 784 | friend class JSArrayBasedStruct<SharedInfoWrapper>; |
| 785 | }; |
| 786 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 787 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 788 | class FunctionInfoListener { |
| 789 | public: |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 790 | FunctionInfoListener() { |
| 791 | current_parent_index_ = -1; |
| 792 | len_ = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 793 | result_ = FACTORY->NewJSArray(10); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 794 | } |
| 795 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 796 | void FunctionStarted(FunctionLiteral* fun) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 797 | HandleScope scope; |
| 798 | FunctionInfoWrapper info = FunctionInfoWrapper::Create(); |
| 799 | info.SetInitialProperties(fun->name(), fun->start_position(), |
| 800 | fun->end_position(), fun->num_parameters(), |
| 801 | current_parent_index_); |
| 802 | current_parent_index_ = len_; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 803 | SetElementNonStrict(result_, len_, info.GetJSArray()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 804 | len_++; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | void FunctionDone() { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 808 | HandleScope scope; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 809 | FunctionInfoWrapper info = |
| 810 | FunctionInfoWrapper::cast( |
| 811 | result_->GetElementNoExceptionThrown(current_parent_index_)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 812 | current_parent_index_ = info.GetParentIndex(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 813 | } |
| 814 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 815 | // Saves only function code, because for a script function we |
| 816 | // may never create a SharedFunctionInfo object. |
| 817 | void FunctionCode(Handle<Code> function_code) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 818 | FunctionInfoWrapper info = |
| 819 | FunctionInfoWrapper::cast( |
| 820 | result_->GetElementNoExceptionThrown(current_parent_index_)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 821 | info.SetFunctionCode(function_code, Handle<Object>(HEAP->null_value())); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | // Saves full information about a function: its code, its scope info |
| 825 | // and a SharedFunctionInfo object. |
| 826 | void FunctionInfo(Handle<SharedFunctionInfo> shared, Scope* scope) { |
| 827 | if (!shared->IsSharedFunctionInfo()) { |
| 828 | return; |
| 829 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 830 | FunctionInfoWrapper info = |
| 831 | FunctionInfoWrapper::cast( |
| 832 | result_->GetElementNoExceptionThrown(current_parent_index_)); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 833 | info.SetFunctionCode(Handle<Code>(shared->code()), |
| 834 | Handle<Object>(shared->scope_info())); |
| 835 | info.SetSharedFunctionInfo(shared); |
| 836 | |
| 837 | Handle<Object> scope_info_list(SerializeFunctionScope(scope)); |
| 838 | info.SetOuterScopeInfo(scope_info_list); |
| 839 | } |
| 840 | |
| 841 | Handle<JSArray> GetResult() { return result_; } |
| 842 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 843 | private: |
| 844 | Object* SerializeFunctionScope(Scope* scope) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 845 | HandleScope handle_scope; |
| 846 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 847 | Handle<JSArray> scope_info_list = FACTORY->NewJSArray(10); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 848 | int scope_info_length = 0; |
| 849 | |
| 850 | // Saves some description of scope. It stores name and indexes of |
| 851 | // variables in the whole scope chain. Null-named slots delimit |
| 852 | // scopes of this chain. |
| 853 | Scope* outer_scope = scope->outer_scope(); |
| 854 | if (outer_scope == NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 855 | return HEAP->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 856 | } |
| 857 | do { |
| 858 | ZoneList<Variable*> list(10); |
| 859 | outer_scope->CollectUsedVariables(&list); |
| 860 | int j = 0; |
| 861 | for (int i = 0; i < list.length(); i++) { |
| 862 | Variable* var1 = list[i]; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 863 | Slot* slot = var1->AsSlot(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 864 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
| 865 | if (j != i) { |
| 866 | list[j] = var1; |
| 867 | } |
| 868 | j++; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | // Sort it. |
| 873 | for (int k = 1; k < j; k++) { |
| 874 | int l = k; |
| 875 | for (int m = k + 1; m < j; m++) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 876 | if (list[l]->AsSlot()->index() > list[m]->AsSlot()->index()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 877 | l = m; |
| 878 | } |
| 879 | } |
| 880 | list[k] = list[l]; |
| 881 | } |
| 882 | for (int i = 0; i < j; i++) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 883 | SetElementNonStrict(scope_info_list, |
| 884 | scope_info_length, |
| 885 | list[i]->name()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 886 | scope_info_length++; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 887 | SetElementNonStrict( |
| 888 | scope_info_list, |
| 889 | scope_info_length, |
| 890 | Handle<Smi>(Smi::FromInt(list[i]->AsSlot()->index()))); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 891 | scope_info_length++; |
| 892 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 893 | SetElementNonStrict(scope_info_list, |
| 894 | scope_info_length, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 895 | Handle<Object>(HEAP->null_value())); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 896 | scope_info_length++; |
| 897 | |
| 898 | outer_scope = outer_scope->outer_scope(); |
| 899 | } while (outer_scope != NULL); |
| 900 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 901 | return *scope_info_list; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 902 | } |
| 903 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 904 | Handle<JSArray> result_; |
| 905 | int len_; |
| 906 | int current_parent_index_; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 907 | }; |
| 908 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 909 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 910 | JSArray* LiveEdit::GatherCompileInfo(Handle<Script> script, |
| 911 | Handle<String> source) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 912 | Isolate* isolate = Isolate::Current(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 913 | ZoneScope zone_scope(isolate, DELETE_ON_EXIT); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 914 | |
| 915 | FunctionInfoListener listener; |
| 916 | Handle<Object> original_source = Handle<Object>(script->source()); |
| 917 | script->set_source(*source); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 918 | isolate->set_active_function_info_listener(&listener); |
| 919 | CompileScriptForTracker(isolate, script); |
| 920 | isolate->set_active_function_info_listener(NULL); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 921 | script->set_source(*original_source); |
| 922 | |
| 923 | return *(listener.GetResult()); |
| 924 | } |
| 925 | |
| 926 | |
| 927 | void LiveEdit::WrapSharedFunctionInfos(Handle<JSArray> array) { |
| 928 | HandleScope scope; |
| 929 | int len = Smi::cast(array->length())->value(); |
| 930 | for (int i = 0; i < len; i++) { |
| 931 | Handle<SharedFunctionInfo> info( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 932 | SharedFunctionInfo::cast(array->GetElementNoExceptionThrown(i))); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 933 | SharedInfoWrapper info_wrapper = SharedInfoWrapper::Create(); |
| 934 | Handle<String> name_handle(String::cast(info->name())); |
| 935 | info_wrapper.SetProperties(name_handle, info->start_position(), |
| 936 | info->end_position(), info); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 937 | SetElementNonStrict(array, i, info_wrapper.GetJSArray()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
| 941 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 942 | // Visitor that collects all references to a particular code object, |
| 943 | // including "CODE_TARGET" references in other code objects. |
| 944 | // It works in context of ZoneScope. |
| 945 | class ReferenceCollectorVisitor : public ObjectVisitor { |
| 946 | public: |
| 947 | explicit ReferenceCollectorVisitor(Code* original) |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 948 | : original_(original), rvalues_(10), reloc_infos_(10), code_entries_(10) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | virtual void VisitPointers(Object** start, Object** end) { |
| 952 | for (Object** p = start; p < end; p++) { |
| 953 | if (*p == original_) { |
| 954 | rvalues_.Add(p); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 959 | virtual void VisitCodeEntry(Address entry) { |
| 960 | if (Code::GetObjectFromEntryAddress(entry) == original_) { |
| 961 | code_entries_.Add(entry); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | virtual void VisitCodeTarget(RelocInfo* rinfo) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 966 | if (RelocInfo::IsCodeTarget(rinfo->rmode()) && |
| 967 | Code::GetCodeFromTargetAddress(rinfo->target_address()) == original_) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 968 | reloc_infos_.Add(*rinfo); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | virtual void VisitDebugTarget(RelocInfo* rinfo) { |
| 973 | VisitCodeTarget(rinfo); |
| 974 | } |
| 975 | |
| 976 | // Post-visiting method that iterates over all collected references and |
| 977 | // modifies them. |
| 978 | void Replace(Code* substitution) { |
| 979 | for (int i = 0; i < rvalues_.length(); i++) { |
| 980 | *(rvalues_[i]) = substitution; |
| 981 | } |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 982 | Address substitution_entry = substitution->instruction_start(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 983 | for (int i = 0; i < reloc_infos_.length(); i++) { |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 984 | reloc_infos_[i].set_target_address(substitution_entry); |
| 985 | } |
| 986 | for (int i = 0; i < code_entries_.length(); i++) { |
| 987 | Address entry = code_entries_[i]; |
| 988 | Memory::Address_at(entry) = substitution_entry; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
| 992 | private: |
| 993 | Code* original_; |
| 994 | ZoneList<Object**> rvalues_; |
| 995 | ZoneList<RelocInfo> reloc_infos_; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 996 | ZoneList<Address> code_entries_; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 997 | }; |
| 998 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 999 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1000 | // Finds all references to original and replaces them with substitution. |
| 1001 | static void ReplaceCodeObject(Code* original, Code* substitution) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1002 | ASSERT(!HEAP->InNewSpace(substitution)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1003 | |
| 1004 | AssertNoAllocation no_allocations_please; |
| 1005 | |
| 1006 | // A zone scope for ReferenceCollectorVisitor. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1007 | ZoneScope scope(Isolate::Current(), DELETE_ON_EXIT); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1008 | |
| 1009 | ReferenceCollectorVisitor visitor(original); |
| 1010 | |
| 1011 | // Iterate over all roots. Stack frames may have pointer into original code, |
| 1012 | // so temporary replace the pointers with offset numbers |
| 1013 | // in prologue/epilogue. |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 1014 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1015 | HEAP->IterateStrongRoots(&visitor, VISIT_ALL); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 1016 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1017 | |
| 1018 | // Now iterate over all pointers of all objects, including code_target |
| 1019 | // implicit pointers. |
| 1020 | HeapIterator iterator; |
| 1021 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 1022 | obj->Iterate(&visitor); |
| 1023 | } |
| 1024 | |
| 1025 | visitor.Replace(substitution); |
| 1026 | } |
| 1027 | |
| 1028 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1029 | // Check whether the code is natural function code (not a lazy-compile stub |
| 1030 | // code). |
| 1031 | static bool IsJSFunctionCode(Code* code) { |
| 1032 | return code->kind() == Code::FUNCTION; |
| 1033 | } |
| 1034 | |
| 1035 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1036 | // Returns true if an instance of candidate were inlined into function's code. |
| 1037 | static bool IsInlined(JSFunction* function, SharedFunctionInfo* candidate) { |
| 1038 | AssertNoAllocation no_gc; |
| 1039 | |
| 1040 | if (function->code()->kind() != Code::OPTIMIZED_FUNCTION) return false; |
| 1041 | |
| 1042 | DeoptimizationInputData* data = |
| 1043 | DeoptimizationInputData::cast(function->code()->deoptimization_data()); |
| 1044 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1045 | if (data == HEAP->empty_fixed_array()) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1046 | |
| 1047 | FixedArray* literals = data->LiteralArray(); |
| 1048 | |
| 1049 | int inlined_count = data->InlinedFunctionCount()->value(); |
| 1050 | for (int i = 0; i < inlined_count; ++i) { |
| 1051 | JSFunction* inlined = JSFunction::cast(literals->get(i)); |
| 1052 | if (inlined->shared() == candidate) return true; |
| 1053 | } |
| 1054 | |
| 1055 | return false; |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | class DependentFunctionsDeoptimizingVisitor : public OptimizedFunctionVisitor { |
| 1060 | public: |
| 1061 | explicit DependentFunctionsDeoptimizingVisitor( |
| 1062 | SharedFunctionInfo* function_info) |
| 1063 | : function_info_(function_info) {} |
| 1064 | |
| 1065 | virtual void EnterContext(Context* context) { |
| 1066 | } |
| 1067 | |
| 1068 | virtual void VisitFunction(JSFunction* function) { |
| 1069 | if (function->shared() == function_info_ || |
| 1070 | IsInlined(function, function_info_)) { |
| 1071 | Deoptimizer::DeoptimizeFunction(function); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | virtual void LeaveContext(Context* context) { |
| 1076 | } |
| 1077 | |
| 1078 | private: |
| 1079 | SharedFunctionInfo* function_info_; |
| 1080 | }; |
| 1081 | |
| 1082 | |
| 1083 | static void DeoptimizeDependentFunctions(SharedFunctionInfo* function_info) { |
| 1084 | AssertNoAllocation no_allocation; |
| 1085 | |
| 1086 | DependentFunctionsDeoptimizingVisitor visitor(function_info); |
| 1087 | Deoptimizer::VisitAllOptimizedFunctions(&visitor); |
| 1088 | } |
| 1089 | |
| 1090 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1091 | MaybeObject* LiveEdit::ReplaceFunctionCode( |
| 1092 | Handle<JSArray> new_compile_info_array, |
| 1093 | Handle<JSArray> shared_info_array) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1094 | HandleScope scope; |
| 1095 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1096 | if (!SharedInfoWrapper::IsInstance(shared_info_array)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1097 | return Isolate::Current()->ThrowIllegalOperation(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1100 | FunctionInfoWrapper compile_info_wrapper(new_compile_info_array); |
| 1101 | SharedInfoWrapper shared_info_wrapper(shared_info_array); |
| 1102 | |
| 1103 | Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); |
| 1104 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1105 | if (IsJSFunctionCode(shared_info->code())) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1106 | Handle<Code> code = compile_info_wrapper.GetFunctionCode(); |
| 1107 | ReplaceCodeObject(shared_info->code(), *code); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1108 | Handle<Object> code_scope_info = compile_info_wrapper.GetCodeScopeInfo(); |
| 1109 | if (code_scope_info->IsFixedArray()) { |
| 1110 | shared_info->set_scope_info(SerializedScopeInfo::cast(*code_scope_info)); |
| 1111 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | if (shared_info->debug_info()->IsDebugInfo()) { |
| 1115 | Handle<DebugInfo> debug_info(DebugInfo::cast(shared_info->debug_info())); |
| 1116 | Handle<Code> new_original_code = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1117 | FACTORY->CopyCode(compile_info_wrapper.GetFunctionCode()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1118 | debug_info->set_original_code(*new_original_code); |
| 1119 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1120 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1121 | int start_position = compile_info_wrapper.GetStartPosition(); |
| 1122 | int end_position = compile_info_wrapper.GetEndPosition(); |
| 1123 | shared_info->set_start_position(start_position); |
| 1124 | shared_info->set_end_position(end_position); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1125 | |
| 1126 | shared_info->set_construct_stub( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1127 | Isolate::Current()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1128 | Builtins::kJSConstructStubGeneric)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1129 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1130 | DeoptimizeDependentFunctions(*shared_info); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1131 | Isolate::Current()->compilation_cache()->Remove(shared_info); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1132 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1133 | return HEAP->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1137 | MaybeObject* LiveEdit::FunctionSourceUpdated( |
| 1138 | Handle<JSArray> shared_info_array) { |
| 1139 | HandleScope scope; |
| 1140 | |
| 1141 | if (!SharedInfoWrapper::IsInstance(shared_info_array)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1142 | return Isolate::Current()->ThrowIllegalOperation(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | SharedInfoWrapper shared_info_wrapper(shared_info_array); |
| 1146 | Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); |
| 1147 | |
| 1148 | DeoptimizeDependentFunctions(*shared_info); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1149 | Isolate::Current()->compilation_cache()->Remove(shared_info); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1150 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1151 | return HEAP->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1155 | void LiveEdit::SetFunctionScript(Handle<JSValue> function_wrapper, |
| 1156 | Handle<Object> script_handle) { |
| 1157 | Handle<SharedFunctionInfo> shared_info = |
| 1158 | Handle<SharedFunctionInfo>::cast(UnwrapJSValue(function_wrapper)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1159 | shared_info->set_script(*script_handle); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1160 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1161 | Isolate::Current()->compilation_cache()->Remove(shared_info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | |
| 1165 | // For a script text change (defined as position_change_array), translates |
| 1166 | // position in unchanged text to position in changed text. |
| 1167 | // Text change is a set of non-overlapping regions in text, that have changed |
| 1168 | // their contents and length. It is specified as array of groups of 3 numbers: |
| 1169 | // (change_begin, change_end, change_end_new_position). |
| 1170 | // Each group describes a change in text; groups are sorted by change_begin. |
| 1171 | // Only position in text beyond any changes may be successfully translated. |
| 1172 | // If a positions is inside some region that changed, result is currently |
| 1173 | // undefined. |
| 1174 | static int TranslatePosition(int original_position, |
| 1175 | Handle<JSArray> position_change_array) { |
| 1176 | int position_diff = 0; |
| 1177 | int array_len = Smi::cast(position_change_array->length())->value(); |
| 1178 | // TODO(635): binary search may be used here |
| 1179 | for (int i = 0; i < array_len; i += 3) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1180 | Object* element = position_change_array->GetElementNoExceptionThrown(i); |
| 1181 | int chunk_start = Smi::cast(element)->value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1182 | if (original_position < chunk_start) { |
| 1183 | break; |
| 1184 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1185 | element = position_change_array->GetElementNoExceptionThrown(i + 1); |
| 1186 | int chunk_end = Smi::cast(element)->value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1187 | // Position mustn't be inside a chunk. |
| 1188 | ASSERT(original_position >= chunk_end); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1189 | element = position_change_array->GetElementNoExceptionThrown(i + 2); |
| 1190 | int chunk_changed_end = Smi::cast(element)->value(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1191 | position_diff = chunk_changed_end - chunk_end; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | return original_position + position_diff; |
| 1195 | } |
| 1196 | |
| 1197 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1198 | // Auto-growing buffer for writing relocation info code section. This buffer |
| 1199 | // is a simplified version of buffer from Assembler. Unlike Assembler, this |
| 1200 | // class is platform-independent and it works without dealing with instructions. |
| 1201 | // As specified by RelocInfo format, the buffer is filled in reversed order: |
| 1202 | // from upper to lower addresses. |
| 1203 | // It uses NewArray/DeleteArray for memory management. |
| 1204 | class RelocInfoBuffer { |
| 1205 | public: |
| 1206 | RelocInfoBuffer(int buffer_initial_capicity, byte* pc) { |
| 1207 | buffer_size_ = buffer_initial_capicity + kBufferGap; |
| 1208 | buffer_ = NewArray<byte>(buffer_size_); |
| 1209 | |
| 1210 | reloc_info_writer_.Reposition(buffer_ + buffer_size_, pc); |
| 1211 | } |
| 1212 | ~RelocInfoBuffer() { |
| 1213 | DeleteArray(buffer_); |
| 1214 | } |
| 1215 | |
| 1216 | // As specified by RelocInfo format, the buffer is filled in reversed order: |
| 1217 | // from upper to lower addresses. |
| 1218 | void Write(const RelocInfo* rinfo) { |
| 1219 | if (buffer_ + kBufferGap >= reloc_info_writer_.pos()) { |
| 1220 | Grow(); |
| 1221 | } |
| 1222 | reloc_info_writer_.Write(rinfo); |
| 1223 | } |
| 1224 | |
| 1225 | Vector<byte> GetResult() { |
| 1226 | // Return the bytes from pos up to end of buffer. |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1227 | int result_size = |
| 1228 | static_cast<int>((buffer_ + buffer_size_) - reloc_info_writer_.pos()); |
| 1229 | return Vector<byte>(reloc_info_writer_.pos(), result_size); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | private: |
| 1233 | void Grow() { |
| 1234 | // Compute new buffer size. |
| 1235 | int new_buffer_size; |
| 1236 | if (buffer_size_ < 2 * KB) { |
| 1237 | new_buffer_size = 4 * KB; |
| 1238 | } else { |
| 1239 | new_buffer_size = 2 * buffer_size_; |
| 1240 | } |
| 1241 | // Some internal data structures overflow for very large buffers, |
| 1242 | // they must ensure that kMaximalBufferSize is not too large. |
| 1243 | if (new_buffer_size > kMaximalBufferSize) { |
| 1244 | V8::FatalProcessOutOfMemory("RelocInfoBuffer::GrowBuffer"); |
| 1245 | } |
| 1246 | |
| 1247 | // Setup new buffer. |
| 1248 | byte* new_buffer = NewArray<byte>(new_buffer_size); |
| 1249 | |
| 1250 | // Copy the data. |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1251 | int curently_used_size = |
| 1252 | static_cast<int>(buffer_ + buffer_size_ - reloc_info_writer_.pos()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1253 | memmove(new_buffer + new_buffer_size - curently_used_size, |
| 1254 | reloc_info_writer_.pos(), curently_used_size); |
| 1255 | |
| 1256 | reloc_info_writer_.Reposition( |
| 1257 | new_buffer + new_buffer_size - curently_used_size, |
| 1258 | reloc_info_writer_.last_pc()); |
| 1259 | |
| 1260 | DeleteArray(buffer_); |
| 1261 | buffer_ = new_buffer; |
| 1262 | buffer_size_ = new_buffer_size; |
| 1263 | } |
| 1264 | |
| 1265 | RelocInfoWriter reloc_info_writer_; |
| 1266 | byte* buffer_; |
| 1267 | int buffer_size_; |
| 1268 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1269 | static const int kBufferGap = RelocInfoWriter::kMaxSize; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1270 | static const int kMaximalBufferSize = 512*MB; |
| 1271 | }; |
| 1272 | |
| 1273 | // Patch positions in code (changes relocation info section) and possibly |
| 1274 | // returns new instance of code. |
| 1275 | static Handle<Code> PatchPositionsInCode(Handle<Code> code, |
| 1276 | Handle<JSArray> position_change_array) { |
| 1277 | |
| 1278 | RelocInfoBuffer buffer_writer(code->relocation_size(), |
| 1279 | code->instruction_start()); |
| 1280 | |
| 1281 | { |
| 1282 | AssertNoAllocation no_allocations_please; |
| 1283 | for (RelocIterator it(*code); !it.done(); it.next()) { |
| 1284 | RelocInfo* rinfo = it.rinfo(); |
| 1285 | if (RelocInfo::IsPosition(rinfo->rmode())) { |
| 1286 | int position = static_cast<int>(rinfo->data()); |
| 1287 | int new_position = TranslatePosition(position, |
| 1288 | position_change_array); |
| 1289 | if (position != new_position) { |
| 1290 | RelocInfo info_copy(rinfo->pc(), rinfo->rmode(), new_position); |
| 1291 | buffer_writer.Write(&info_copy); |
| 1292 | continue; |
| 1293 | } |
| 1294 | } |
| 1295 | buffer_writer.Write(it.rinfo()); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | Vector<byte> buffer = buffer_writer.GetResult(); |
| 1300 | |
| 1301 | if (buffer.length() == code->relocation_size()) { |
| 1302 | // Simply patch relocation area of code. |
| 1303 | memcpy(code->relocation_start(), buffer.start(), buffer.length()); |
| 1304 | return code; |
| 1305 | } else { |
| 1306 | // Relocation info section now has different size. We cannot simply |
| 1307 | // rewrite it inside code object. Instead we have to create a new |
| 1308 | // code object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1309 | Handle<Code> result(FACTORY->CopyCode(code, buffer)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1310 | return result; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1315 | MaybeObject* LiveEdit::PatchFunctionPositions( |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1316 | Handle<JSArray> shared_info_array, Handle<JSArray> position_change_array) { |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1317 | |
| 1318 | if (!SharedInfoWrapper::IsInstance(shared_info_array)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1319 | return Isolate::Current()->ThrowIllegalOperation(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1322 | SharedInfoWrapper shared_info_wrapper(shared_info_array); |
| 1323 | Handle<SharedFunctionInfo> info = shared_info_wrapper.GetInfo(); |
| 1324 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1325 | int old_function_start = info->start_position(); |
| 1326 | int new_function_start = TranslatePosition(old_function_start, |
| 1327 | position_change_array); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1328 | int new_function_end = TranslatePosition(info->end_position(), |
| 1329 | position_change_array); |
| 1330 | int new_function_token_pos = |
| 1331 | TranslatePosition(info->function_token_position(), position_change_array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1332 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1333 | info->set_start_position(new_function_start); |
| 1334 | info->set_end_position(new_function_end); |
| 1335 | info->set_function_token_position(new_function_token_pos); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1336 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1337 | if (IsJSFunctionCode(info->code())) { |
| 1338 | // Patch relocation info section of the code. |
| 1339 | Handle<Code> patched_code = PatchPositionsInCode(Handle<Code>(info->code()), |
| 1340 | position_change_array); |
| 1341 | if (*patched_code != info->code()) { |
| 1342 | // Replace all references to the code across the heap. In particular, |
| 1343 | // some stubs may refer to this code and this code may be being executed |
| 1344 | // on stack (it is safe to substitute the code object on stack, because |
| 1345 | // we only change the structure of rinfo and leave instructions |
| 1346 | // untouched). |
| 1347 | ReplaceCodeObject(info->code(), *patched_code); |
| 1348 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1349 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1350 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1351 | return HEAP->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1352 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1353 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1354 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1355 | static Handle<Script> CreateScriptCopy(Handle<Script> original) { |
| 1356 | Handle<String> original_source(String::cast(original->source())); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1357 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1358 | Handle<Script> copy = FACTORY->NewScript(original_source); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1359 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1360 | copy->set_name(original->name()); |
| 1361 | copy->set_line_offset(original->line_offset()); |
| 1362 | copy->set_column_offset(original->column_offset()); |
| 1363 | copy->set_data(original->data()); |
| 1364 | copy->set_type(original->type()); |
| 1365 | copy->set_context_data(original->context_data()); |
| 1366 | copy->set_compilation_type(original->compilation_type()); |
| 1367 | copy->set_eval_from_shared(original->eval_from_shared()); |
| 1368 | copy->set_eval_from_instructions_offset( |
| 1369 | original->eval_from_instructions_offset()); |
| 1370 | |
| 1371 | return copy; |
| 1372 | } |
| 1373 | |
| 1374 | |
| 1375 | Object* LiveEdit::ChangeScriptSource(Handle<Script> original_script, |
| 1376 | Handle<String> new_source, |
| 1377 | Handle<Object> old_script_name) { |
| 1378 | Handle<Object> old_script_object; |
| 1379 | if (old_script_name->IsString()) { |
| 1380 | Handle<Script> old_script = CreateScriptCopy(original_script); |
| 1381 | old_script->set_name(String::cast(*old_script_name)); |
| 1382 | old_script_object = old_script; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1383 | Isolate::Current()->debugger()->OnAfterCompile( |
| 1384 | old_script, Debugger::SEND_WHEN_DEBUGGING); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1385 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1386 | old_script_object = Handle<Object>(HEAP->null_value()); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | original_script->set_source(*new_source); |
| 1390 | |
| 1391 | // Drop line ends so that they will be recalculated. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1392 | original_script->set_line_ends(HEAP->undefined_value()); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1393 | |
| 1394 | return *old_script_object; |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | void LiveEdit::ReplaceRefToNestedFunction( |
| 1400 | Handle<JSValue> parent_function_wrapper, |
| 1401 | Handle<JSValue> orig_function_wrapper, |
| 1402 | Handle<JSValue> subst_function_wrapper) { |
| 1403 | |
| 1404 | Handle<SharedFunctionInfo> parent_shared = |
| 1405 | Handle<SharedFunctionInfo>::cast(UnwrapJSValue(parent_function_wrapper)); |
| 1406 | Handle<SharedFunctionInfo> orig_shared = |
| 1407 | Handle<SharedFunctionInfo>::cast(UnwrapJSValue(orig_function_wrapper)); |
| 1408 | Handle<SharedFunctionInfo> subst_shared = |
| 1409 | Handle<SharedFunctionInfo>::cast(UnwrapJSValue(subst_function_wrapper)); |
| 1410 | |
| 1411 | for (RelocIterator it(parent_shared->code()); !it.done(); it.next()) { |
| 1412 | if (it.rinfo()->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 1413 | if (it.rinfo()->target_object() == *orig_shared) { |
| 1414 | it.rinfo()->set_target_object(*subst_shared); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1415 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1416 | } |
| 1417 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | // Check an activation against list of functions. If there is a function |
| 1422 | // that matches, its status in result array is changed to status argument value. |
| 1423 | static bool CheckActivation(Handle<JSArray> shared_info_array, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1424 | Handle<JSArray> result, |
| 1425 | StackFrame* frame, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1426 | LiveEdit::FunctionPatchabilityStatus status) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1427 | if (!frame->is_java_script()) return false; |
| 1428 | |
| 1429 | Handle<JSFunction> function( |
| 1430 | JSFunction::cast(JavaScriptFrame::cast(frame)->function())); |
| 1431 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1432 | int len = Smi::cast(shared_info_array->length())->value(); |
| 1433 | for (int i = 0; i < len; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1434 | JSValue* wrapper = |
| 1435 | JSValue::cast(shared_info_array->GetElementNoExceptionThrown(i)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1436 | Handle<SharedFunctionInfo> shared( |
| 1437 | SharedFunctionInfo::cast(wrapper->value())); |
| 1438 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1439 | if (function->shared() == *shared || IsInlined(*function, *shared)) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1440 | SetElementNonStrict(result, i, Handle<Smi>(Smi::FromInt(status))); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1441 | return true; |
| 1442 | } |
| 1443 | } |
| 1444 | return false; |
| 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | // Iterates over handler chain and removes all elements that are inside |
| 1449 | // frames being dropped. |
| 1450 | static bool FixTryCatchHandler(StackFrame* top_frame, |
| 1451 | StackFrame* bottom_frame) { |
| 1452 | Address* pointer_address = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1453 | &Memory::Address_at(Isolate::Current()->get_address_from_id( |
| 1454 | Isolate::k_handler_address)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1455 | |
| 1456 | while (*pointer_address < top_frame->sp()) { |
| 1457 | pointer_address = &Memory::Address_at(*pointer_address); |
| 1458 | } |
| 1459 | Address* above_frame_address = pointer_address; |
| 1460 | while (*pointer_address < bottom_frame->fp()) { |
| 1461 | pointer_address = &Memory::Address_at(*pointer_address); |
| 1462 | } |
| 1463 | bool change = *above_frame_address != *pointer_address; |
| 1464 | *above_frame_address = *pointer_address; |
| 1465 | return change; |
| 1466 | } |
| 1467 | |
| 1468 | |
| 1469 | // Removes specified range of frames from stack. There may be 1 or more |
| 1470 | // frames in range. Anyway the bottom frame is restarted rather than dropped, |
| 1471 | // and therefore has to be a JavaScript frame. |
| 1472 | // Returns error message or NULL. |
| 1473 | static const char* DropFrames(Vector<StackFrame*> frames, |
| 1474 | int top_frame_index, |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1475 | int bottom_js_frame_index, |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1476 | Debug::FrameDropMode* mode, |
| 1477 | Object*** restarter_frame_function_pointer) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1478 | if (!Debug::kFrameDropperSupported) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1479 | return "Stack manipulations are not supported in this architecture."; |
| 1480 | } |
| 1481 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1482 | StackFrame* pre_top_frame = frames[top_frame_index - 1]; |
| 1483 | StackFrame* top_frame = frames[top_frame_index]; |
| 1484 | StackFrame* bottom_js_frame = frames[bottom_js_frame_index]; |
| 1485 | |
| 1486 | ASSERT(bottom_js_frame->is_java_script()); |
| 1487 | |
| 1488 | // Check the nature of the top frame. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1489 | Isolate* isolate = Isolate::Current(); |
| 1490 | Code* pre_top_frame_code = pre_top_frame->LookupCode(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1491 | if (pre_top_frame_code->is_inline_cache_stub() && |
| 1492 | pre_top_frame_code->ic_state() == DEBUG_BREAK) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1493 | // OK, we can drop inline cache calls. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1494 | *mode = Debug::FRAME_DROPPED_IN_IC_CALL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1495 | } else if (pre_top_frame_code == |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1496 | isolate->debug()->debug_break_slot()) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1497 | // OK, we can drop debug break slot. |
| 1498 | *mode = Debug::FRAME_DROPPED_IN_DEBUG_SLOT_CALL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1499 | } else if (pre_top_frame_code == |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1500 | isolate->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1501 | Builtins::kFrameDropper_LiveEdit)) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1502 | // OK, we can drop our own code. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1503 | *mode = Debug::FRAME_DROPPED_IN_DIRECT_CALL; |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 1504 | } else if (pre_top_frame_code == |
| 1505 | isolate->builtins()->builtin(Builtins::kReturn_DebugBreak)) { |
| 1506 | *mode = Debug::FRAME_DROPPED_IN_RETURN_CALL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1507 | } else if (pre_top_frame_code->kind() == Code::STUB && |
| 1508 | pre_top_frame_code->major_key()) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1509 | // Entry from our unit tests, it's fine, we support this case. |
| 1510 | *mode = Debug::FRAME_DROPPED_IN_DIRECT_CALL; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1511 | } else { |
| 1512 | return "Unknown structure of stack above changing function"; |
| 1513 | } |
| 1514 | |
| 1515 | Address unused_stack_top = top_frame->sp(); |
| 1516 | Address unused_stack_bottom = bottom_js_frame->fp() |
| 1517 | - Debug::kFrameDropperFrameSize * kPointerSize // Size of the new frame. |
| 1518 | + kPointerSize; // Bigger address end is exclusive. |
| 1519 | |
| 1520 | if (unused_stack_top > unused_stack_bottom) { |
| 1521 | return "Not enough space for frame dropper frame"; |
| 1522 | } |
| 1523 | |
| 1524 | // Committing now. After this point we should return only NULL value. |
| 1525 | |
| 1526 | FixTryCatchHandler(pre_top_frame, bottom_js_frame); |
| 1527 | // Make sure FixTryCatchHandler is idempotent. |
| 1528 | ASSERT(!FixTryCatchHandler(pre_top_frame, bottom_js_frame)); |
| 1529 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1530 | Handle<Code> code = Isolate::Current()->builtins()->FrameDropper_LiveEdit(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1531 | top_frame->set_pc(code->entry()); |
| 1532 | pre_top_frame->SetCallerFp(bottom_js_frame->fp()); |
| 1533 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1534 | *restarter_frame_function_pointer = |
| 1535 | Debug::SetUpFrameDropperFrame(bottom_js_frame, code); |
| 1536 | |
| 1537 | ASSERT((**restarter_frame_function_pointer)->IsJSFunction()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1538 | |
| 1539 | for (Address a = unused_stack_top; |
| 1540 | a < unused_stack_bottom; |
| 1541 | a += kPointerSize) { |
| 1542 | Memory::Object_at(a) = Smi::FromInt(0); |
| 1543 | } |
| 1544 | |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | |
| 1548 | |
| 1549 | static bool IsDropableFrame(StackFrame* frame) { |
| 1550 | return !frame->is_exit(); |
| 1551 | } |
| 1552 | |
| 1553 | // Fills result array with statuses of functions. Modifies the stack |
| 1554 | // removing all listed function if possible and if do_drop is true. |
| 1555 | static const char* DropActivationsInActiveThread( |
| 1556 | Handle<JSArray> shared_info_array, Handle<JSArray> result, bool do_drop) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1557 | Isolate* isolate = Isolate::Current(); |
| 1558 | Debug* debug = isolate->debug(); |
| 1559 | ZoneScope scope(isolate, DELETE_ON_EXIT); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1560 | Vector<StackFrame*> frames = CreateStackMap(); |
| 1561 | |
| 1562 | int array_len = Smi::cast(shared_info_array->length())->value(); |
| 1563 | |
| 1564 | int top_frame_index = -1; |
| 1565 | int frame_index = 0; |
| 1566 | for (; frame_index < frames.length(); frame_index++) { |
| 1567 | StackFrame* frame = frames[frame_index]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1568 | if (frame->id() == debug->break_frame_id()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1569 | top_frame_index = frame_index; |
| 1570 | break; |
| 1571 | } |
| 1572 | if (CheckActivation(shared_info_array, result, frame, |
| 1573 | LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE)) { |
| 1574 | // We are still above break_frame. It is not a target frame, |
| 1575 | // it is a problem. |
| 1576 | return "Debugger mark-up on stack is not found"; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | if (top_frame_index == -1) { |
| 1581 | // We haven't found break frame, but no function is blocking us anyway. |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | |
| 1585 | bool target_frame_found = false; |
| 1586 | int bottom_js_frame_index = top_frame_index; |
| 1587 | bool c_code_found = false; |
| 1588 | |
| 1589 | for (; frame_index < frames.length(); frame_index++) { |
| 1590 | StackFrame* frame = frames[frame_index]; |
| 1591 | if (!IsDropableFrame(frame)) { |
| 1592 | c_code_found = true; |
| 1593 | break; |
| 1594 | } |
| 1595 | if (CheckActivation(shared_info_array, result, frame, |
| 1596 | LiveEdit::FUNCTION_BLOCKED_ON_ACTIVE_STACK)) { |
| 1597 | target_frame_found = true; |
| 1598 | bottom_js_frame_index = frame_index; |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | if (c_code_found) { |
| 1603 | // There is a C frames on stack. Check that there are no target frames |
| 1604 | // below them. |
| 1605 | for (; frame_index < frames.length(); frame_index++) { |
| 1606 | StackFrame* frame = frames[frame_index]; |
| 1607 | if (frame->is_java_script()) { |
| 1608 | if (CheckActivation(shared_info_array, result, frame, |
| 1609 | LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE)) { |
| 1610 | // Cannot drop frame under C frames. |
| 1611 | return NULL; |
| 1612 | } |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | if (!do_drop) { |
| 1618 | // We are in check-only mode. |
| 1619 | return NULL; |
| 1620 | } |
| 1621 | |
| 1622 | if (!target_frame_found) { |
| 1623 | // Nothing to drop. |
| 1624 | return NULL; |
| 1625 | } |
| 1626 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1627 | Debug::FrameDropMode drop_mode = Debug::FRAMES_UNTOUCHED; |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1628 | Object** restarter_frame_function_pointer = NULL; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1629 | const char* error_message = DropFrames(frames, top_frame_index, |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1630 | bottom_js_frame_index, &drop_mode, |
| 1631 | &restarter_frame_function_pointer); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1632 | |
| 1633 | if (error_message != NULL) { |
| 1634 | return error_message; |
| 1635 | } |
| 1636 | |
| 1637 | // Adjust break_frame after some frames has been dropped. |
| 1638 | StackFrame::Id new_id = StackFrame::NO_ID; |
| 1639 | for (int i = bottom_js_frame_index + 1; i < frames.length(); i++) { |
| 1640 | if (frames[i]->type() == StackFrame::JAVA_SCRIPT) { |
| 1641 | new_id = frames[i]->id(); |
| 1642 | break; |
| 1643 | } |
| 1644 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1645 | debug->FramesHaveBeenDropped(new_id, drop_mode, |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1646 | restarter_frame_function_pointer); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1647 | |
| 1648 | // Replace "blocked on active" with "replaced on active" status. |
| 1649 | for (int i = 0; i < array_len; i++) { |
| 1650 | if (result->GetElement(i) == |
| 1651 | Smi::FromInt(LiveEdit::FUNCTION_BLOCKED_ON_ACTIVE_STACK)) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1652 | Handle<Object> replaced( |
| 1653 | Smi::FromInt(LiveEdit::FUNCTION_REPLACED_ON_ACTIVE_STACK)); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1654 | SetElementNonStrict(result, i, replaced); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | return NULL; |
| 1658 | } |
| 1659 | |
| 1660 | |
| 1661 | class InactiveThreadActivationsChecker : public ThreadVisitor { |
| 1662 | public: |
| 1663 | InactiveThreadActivationsChecker(Handle<JSArray> shared_info_array, |
| 1664 | Handle<JSArray> result) |
| 1665 | : shared_info_array_(shared_info_array), result_(result), |
| 1666 | has_blocked_functions_(false) { |
| 1667 | } |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 1668 | void VisitThread(Isolate* isolate, ThreadLocalTop* top) { |
| 1669 | for (StackFrameIterator it(isolate, top); !it.done(); it.Advance()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1670 | has_blocked_functions_ |= CheckActivation( |
| 1671 | shared_info_array_, result_, it.frame(), |
| 1672 | LiveEdit::FUNCTION_BLOCKED_ON_OTHER_STACK); |
| 1673 | } |
| 1674 | } |
| 1675 | bool HasBlockedFunctions() { |
| 1676 | return has_blocked_functions_; |
| 1677 | } |
| 1678 | |
| 1679 | private: |
| 1680 | Handle<JSArray> shared_info_array_; |
| 1681 | Handle<JSArray> result_; |
| 1682 | bool has_blocked_functions_; |
| 1683 | }; |
| 1684 | |
| 1685 | |
| 1686 | Handle<JSArray> LiveEdit::CheckAndDropActivations( |
| 1687 | Handle<JSArray> shared_info_array, bool do_drop) { |
| 1688 | int len = Smi::cast(shared_info_array->length())->value(); |
| 1689 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1690 | Handle<JSArray> result = FACTORY->NewJSArray(len); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Fill the default values. |
| 1693 | for (int i = 0; i < len; i++) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1694 | SetElementNonStrict( |
| 1695 | result, |
| 1696 | i, |
| 1697 | Handle<Smi>(Smi::FromInt(FUNCTION_AVAILABLE_FOR_PATCH))); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | // First check inactive threads. Fail if some functions are blocked there. |
| 1702 | InactiveThreadActivationsChecker inactive_threads_checker(shared_info_array, |
| 1703 | result); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1704 | Isolate::Current()->thread_manager()->IterateArchivedThreads( |
| 1705 | &inactive_threads_checker); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1706 | if (inactive_threads_checker.HasBlockedFunctions()) { |
| 1707 | return result; |
| 1708 | } |
| 1709 | |
| 1710 | // Try to drop activations from the current stack. |
| 1711 | const char* error_message = |
| 1712 | DropActivationsInActiveThread(shared_info_array, result, do_drop); |
| 1713 | if (error_message != NULL) { |
| 1714 | // Add error message as an array extra element. |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1715 | Vector<const char> vector_message(error_message, StrLength(error_message)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1716 | Handle<String> str = FACTORY->NewStringFromAscii(vector_message); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1717 | SetElementNonStrict(result, len, str); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1718 | } |
| 1719 | return result; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1723 | LiveEditFunctionTracker::LiveEditFunctionTracker(Isolate* isolate, |
| 1724 | FunctionLiteral* fun) |
| 1725 | : isolate_(isolate) { |
| 1726 | if (isolate_->active_function_info_listener() != NULL) { |
| 1727 | isolate_->active_function_info_listener()->FunctionStarted(fun); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1728 | } |
| 1729 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1730 | |
| 1731 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1732 | LiveEditFunctionTracker::~LiveEditFunctionTracker() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1733 | if (isolate_->active_function_info_listener() != NULL) { |
| 1734 | isolate_->active_function_info_listener()->FunctionDone(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1735 | } |
| 1736 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1737 | |
| 1738 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1739 | void LiveEditFunctionTracker::RecordFunctionInfo( |
| 1740 | Handle<SharedFunctionInfo> info, FunctionLiteral* lit) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1741 | if (isolate_->active_function_info_listener() != NULL) { |
| 1742 | isolate_->active_function_info_listener()->FunctionInfo(info, lit->scope()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1745 | |
| 1746 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1747 | void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1748 | isolate_->active_function_info_listener()->FunctionCode(code); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1749 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1750 | |
| 1751 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1752 | bool LiveEditFunctionTracker::IsActive(Isolate* isolate) { |
| 1753 | return isolate->active_function_info_listener() != NULL; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1756 | |
| 1757 | #else // ENABLE_DEBUGGER_SUPPORT |
| 1758 | |
| 1759 | // This ifdef-else-endif section provides working or stub implementation of |
| 1760 | // LiveEditFunctionTracker. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1761 | LiveEditFunctionTracker::LiveEditFunctionTracker(Isolate* isolate, |
| 1762 | FunctionLiteral* fun) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | |
| 1766 | LiveEditFunctionTracker::~LiveEditFunctionTracker() { |
| 1767 | } |
| 1768 | |
| 1769 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1770 | void LiveEditFunctionTracker::RecordFunctionInfo( |
| 1771 | Handle<SharedFunctionInfo> info, FunctionLiteral* lit) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1775 | void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1779 | bool LiveEditFunctionTracker::IsActive(Isolate* isolate) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1780 | return false; |
| 1781 | } |
| 1782 | |
| 1783 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 1784 | |
| 1785 | |
| 1786 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1787 | } } // namespace v8::internal |