Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4 | |
| 5 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6 | #include "src/v8.h" |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/liveedit.h" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 9 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/code-stubs.h" |
| 11 | #include "src/compilation-cache.h" |
| 12 | #include "src/compiler.h" |
| 13 | #include "src/debug.h" |
| 14 | #include "src/deoptimizer.h" |
| 15 | #include "src/global-handles.h" |
| 16 | #include "src/messages.h" |
| 17 | #include "src/parser.h" |
| 18 | #include "src/scopeinfo.h" |
| 19 | #include "src/scopes.h" |
| 20 | #include "src/v8memory.h" |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 21 | |
| 22 | namespace v8 { |
| 23 | namespace internal { |
| 24 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | void SetElementSloppy(Handle<JSObject> object, |
| 26 | uint32_t index, |
| 27 | Handle<Object> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 28 | // Ignore return value from SetElement. It can only be a failure if there |
| 29 | // are element setters causing exceptions and the debugger context has none |
| 30 | // of these. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | JSObject::SetElement(object, index, value, NONE, SLOPPY).Assert(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 35 | // A simple implementation of dynamic programming algorithm. It solves |
| 36 | // the problem of finding the difference of 2 arrays. It uses a table of results |
| 37 | // of subproblems. Each cell contains a number together with 2-bit flag |
| 38 | // that helps building the chunk list. |
| 39 | class Differencer { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 40 | public: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 41 | explicit Differencer(Comparator::Input* input) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 42 | : input_(input), len1_(input->GetLength1()), len2_(input->GetLength2()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 43 | buffer_ = NewArray<int>(len1_ * len2_); |
| 44 | } |
| 45 | ~Differencer() { |
| 46 | DeleteArray(buffer_); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 49 | void Initialize() { |
| 50 | int array_size = len1_ * len2_; |
| 51 | for (int i = 0; i < array_size; i++) { |
| 52 | buffer_[i] = kEmptyCellValue; |
| 53 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 56 | // Makes sure that result for the full problem is calculated and stored |
| 57 | // in the table together with flags showing a path through subproblems. |
| 58 | void FillTable() { |
| 59 | CompareUpToTail(0, 0); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 62 | void SaveResult(Comparator::Output* chunk_writer) { |
| 63 | ResultWriter writer(chunk_writer); |
| 64 | |
| 65 | int pos1 = 0; |
| 66 | int pos2 = 0; |
| 67 | while (true) { |
| 68 | if (pos1 < len1_) { |
| 69 | if (pos2 < len2_) { |
| 70 | Direction dir = get_direction(pos1, pos2); |
| 71 | switch (dir) { |
| 72 | case EQ: |
| 73 | writer.eq(); |
| 74 | pos1++; |
| 75 | pos2++; |
| 76 | break; |
| 77 | case SKIP1: |
| 78 | writer.skip1(1); |
| 79 | pos1++; |
| 80 | break; |
| 81 | case SKIP2: |
| 82 | case SKIP_ANY: |
| 83 | writer.skip2(1); |
| 84 | pos2++; |
| 85 | break; |
| 86 | default: |
| 87 | UNREACHABLE(); |
| 88 | } |
| 89 | } else { |
| 90 | writer.skip1(len1_ - pos1); |
| 91 | break; |
| 92 | } |
| 93 | } else { |
| 94 | if (len2_ != pos2) { |
| 95 | writer.skip2(len2_ - pos2); |
| 96 | } |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | writer.close(); |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | Comparator::Input* input_; |
| 105 | int* buffer_; |
| 106 | int len1_; |
| 107 | int len2_; |
| 108 | |
| 109 | enum Direction { |
| 110 | EQ = 0, |
| 111 | SKIP1, |
| 112 | SKIP2, |
| 113 | SKIP_ANY, |
| 114 | |
| 115 | MAX_DIRECTION_FLAG_VALUE = SKIP_ANY |
| 116 | }; |
| 117 | |
| 118 | // Computes result for a subtask and optionally caches it in the buffer table. |
| 119 | // All results values are shifted to make space for flags in the lower bits. |
| 120 | int CompareUpToTail(int pos1, int pos2) { |
| 121 | if (pos1 < len1_) { |
| 122 | if (pos2 < len2_) { |
| 123 | int cached_res = get_value4(pos1, pos2); |
| 124 | if (cached_res == kEmptyCellValue) { |
| 125 | Direction dir; |
| 126 | int res; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 127 | if (input_->Equals(pos1, pos2)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 128 | res = CompareUpToTail(pos1 + 1, pos2 + 1); |
| 129 | dir = EQ; |
| 130 | } else { |
| 131 | int res1 = CompareUpToTail(pos1 + 1, pos2) + |
| 132 | (1 << kDirectionSizeBits); |
| 133 | int res2 = CompareUpToTail(pos1, pos2 + 1) + |
| 134 | (1 << kDirectionSizeBits); |
| 135 | if (res1 == res2) { |
| 136 | res = res1; |
| 137 | dir = SKIP_ANY; |
| 138 | } else if (res1 < res2) { |
| 139 | res = res1; |
| 140 | dir = SKIP1; |
| 141 | } else { |
| 142 | res = res2; |
| 143 | dir = SKIP2; |
| 144 | } |
| 145 | } |
| 146 | set_value4_and_dir(pos1, pos2, res, dir); |
| 147 | cached_res = res; |
| 148 | } |
| 149 | return cached_res; |
| 150 | } else { |
| 151 | return (len1_ - pos1) << kDirectionSizeBits; |
| 152 | } |
| 153 | } else { |
| 154 | return (len2_ - pos2) << kDirectionSizeBits; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | inline int& get_cell(int i1, int i2) { |
| 159 | return buffer_[i1 + i2 * len1_]; |
| 160 | } |
| 161 | |
| 162 | // Each cell keeps a value plus direction. Value is multiplied by 4. |
| 163 | void set_value4_and_dir(int i1, int i2, int value4, Direction dir) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 164 | DCHECK((value4 & kDirectionMask) == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 165 | get_cell(i1, i2) = value4 | dir; |
| 166 | } |
| 167 | |
| 168 | int get_value4(int i1, int i2) { |
| 169 | return get_cell(i1, i2) & (kMaxUInt32 ^ kDirectionMask); |
| 170 | } |
| 171 | Direction get_direction(int i1, int i2) { |
| 172 | return static_cast<Direction>(get_cell(i1, i2) & kDirectionMask); |
| 173 | } |
| 174 | |
| 175 | static const int kDirectionSizeBits = 2; |
| 176 | static const int kDirectionMask = (1 << kDirectionSizeBits) - 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | static const int kEmptyCellValue = ~0u << kDirectionSizeBits; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 178 | |
| 179 | // This method only holds static assert statement (unfortunately you cannot |
| 180 | // place one in class scope). |
| 181 | void StaticAssertHolder() { |
| 182 | STATIC_ASSERT(MAX_DIRECTION_FLAG_VALUE < (1 << kDirectionSizeBits)); |
| 183 | } |
| 184 | |
| 185 | class ResultWriter { |
| 186 | public: |
| 187 | explicit ResultWriter(Comparator::Output* chunk_writer) |
| 188 | : chunk_writer_(chunk_writer), pos1_(0), pos2_(0), |
| 189 | pos1_begin_(-1), pos2_begin_(-1), has_open_chunk_(false) { |
| 190 | } |
| 191 | void eq() { |
| 192 | FlushChunk(); |
| 193 | pos1_++; |
| 194 | pos2_++; |
| 195 | } |
| 196 | void skip1(int len1) { |
| 197 | StartChunk(); |
| 198 | pos1_ += len1; |
| 199 | } |
| 200 | void skip2(int len2) { |
| 201 | StartChunk(); |
| 202 | pos2_ += len2; |
| 203 | } |
| 204 | void close() { |
| 205 | FlushChunk(); |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | Comparator::Output* chunk_writer_; |
| 210 | int pos1_; |
| 211 | int pos2_; |
| 212 | int pos1_begin_; |
| 213 | int pos2_begin_; |
| 214 | bool has_open_chunk_; |
| 215 | |
| 216 | void StartChunk() { |
| 217 | if (!has_open_chunk_) { |
| 218 | pos1_begin_ = pos1_; |
| 219 | pos2_begin_ = pos2_; |
| 220 | has_open_chunk_ = true; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void FlushChunk() { |
| 225 | if (has_open_chunk_) { |
| 226 | chunk_writer_->AddChunk(pos1_begin_, pos2_begin_, |
| 227 | pos1_ - pos1_begin_, pos2_ - pos2_begin_); |
| 228 | has_open_chunk_ = false; |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | void Comparator::CalculateDifference(Comparator::Input* input, |
| 236 | Comparator::Output* result_writer) { |
| 237 | Differencer differencer(input); |
| 238 | differencer.Initialize(); |
| 239 | differencer.FillTable(); |
| 240 | differencer.SaveResult(result_writer); |
| 241 | } |
| 242 | |
| 243 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 244 | static bool CompareSubstrings(Handle<String> s1, int pos1, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 245 | Handle<String> s2, int pos2, int len) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 246 | for (int i = 0; i < len; i++) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 247 | if (s1->Get(i + pos1) != s2->Get(i + pos2)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 255 | // Additional to Input interface. Lets switch Input range to subrange. |
| 256 | // More elegant way would be to wrap one Input as another Input object |
| 257 | // and translate positions there, but that would cost us additional virtual |
| 258 | // call per comparison. |
| 259 | class SubrangableInput : public Comparator::Input { |
| 260 | public: |
| 261 | virtual void SetSubrange1(int offset, int len) = 0; |
| 262 | virtual void SetSubrange2(int offset, int len) = 0; |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | class SubrangableOutput : public Comparator::Output { |
| 267 | public: |
| 268 | virtual void SetSubrange1(int offset, int len) = 0; |
| 269 | virtual void SetSubrange2(int offset, int len) = 0; |
| 270 | }; |
| 271 | |
| 272 | |
| 273 | static int min(int a, int b) { |
| 274 | return a < b ? a : b; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | // Finds common prefix and suffix in input. This parts shouldn't take space in |
| 279 | // linear programming table. Enable subranging in input and output. |
| 280 | static void NarrowDownInput(SubrangableInput* input, |
| 281 | SubrangableOutput* output) { |
| 282 | const int len1 = input->GetLength1(); |
| 283 | const int len2 = input->GetLength2(); |
| 284 | |
| 285 | int common_prefix_len; |
| 286 | int common_suffix_len; |
| 287 | |
| 288 | { |
| 289 | common_prefix_len = 0; |
| 290 | int prefix_limit = min(len1, len2); |
| 291 | while (common_prefix_len < prefix_limit && |
| 292 | input->Equals(common_prefix_len, common_prefix_len)) { |
| 293 | common_prefix_len++; |
| 294 | } |
| 295 | |
| 296 | common_suffix_len = 0; |
| 297 | int suffix_limit = min(len1 - common_prefix_len, len2 - common_prefix_len); |
| 298 | |
| 299 | while (common_suffix_len < suffix_limit && |
| 300 | input->Equals(len1 - common_suffix_len - 1, |
| 301 | len2 - common_suffix_len - 1)) { |
| 302 | common_suffix_len++; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (common_prefix_len > 0 || common_suffix_len > 0) { |
| 307 | int new_len1 = len1 - common_suffix_len - common_prefix_len; |
| 308 | int new_len2 = len2 - common_suffix_len - common_prefix_len; |
| 309 | |
| 310 | input->SetSubrange1(common_prefix_len, new_len1); |
| 311 | input->SetSubrange2(common_prefix_len, new_len2); |
| 312 | |
| 313 | output->SetSubrange1(common_prefix_len, new_len1); |
| 314 | output->SetSubrange2(common_prefix_len, new_len2); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 319 | // A helper class that writes chunk numbers into JSArray. |
| 320 | // Each chunk is stored as 3 array elements: (pos1_begin, pos1_end, pos2_end). |
| 321 | class CompareOutputArrayWriter { |
| 322 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 323 | explicit CompareOutputArrayWriter(Isolate* isolate) |
| 324 | : array_(isolate->factory()->NewJSArray(10)), current_size_(0) {} |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 325 | |
| 326 | Handle<JSArray> GetResult() { |
| 327 | return array_; |
| 328 | } |
| 329 | |
| 330 | void WriteChunk(int char_pos1, int char_pos2, int char_len1, int char_len2) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 331 | Isolate* isolate = array_->GetIsolate(); |
| 332 | SetElementSloppy(array_, |
| 333 | current_size_, |
| 334 | Handle<Object>(Smi::FromInt(char_pos1), isolate)); |
| 335 | SetElementSloppy(array_, |
| 336 | current_size_ + 1, |
| 337 | Handle<Object>(Smi::FromInt(char_pos1 + char_len1), |
| 338 | isolate)); |
| 339 | SetElementSloppy(array_, |
| 340 | current_size_ + 2, |
| 341 | Handle<Object>(Smi::FromInt(char_pos2 + char_len2), |
| 342 | isolate)); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 343 | current_size_ += 3; |
| 344 | } |
| 345 | |
| 346 | private: |
| 347 | Handle<JSArray> array_; |
| 348 | int current_size_; |
| 349 | }; |
| 350 | |
| 351 | |
| 352 | // Represents 2 strings as 2 arrays of tokens. |
| 353 | // TODO(LiveEdit): Currently it's actually an array of charactres. |
| 354 | // Make array of tokens instead. |
| 355 | class TokensCompareInput : public Comparator::Input { |
| 356 | public: |
| 357 | TokensCompareInput(Handle<String> s1, int offset1, int len1, |
| 358 | Handle<String> s2, int offset2, int len2) |
| 359 | : s1_(s1), offset1_(offset1), len1_(len1), |
| 360 | s2_(s2), offset2_(offset2), len2_(len2) { |
| 361 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 362 | virtual int GetLength1() { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 363 | return len1_; |
| 364 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 365 | virtual int GetLength2() { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 366 | return len2_; |
| 367 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 368 | bool Equals(int index1, int index2) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 369 | return s1_->Get(offset1_ + index1) == s2_->Get(offset2_ + index2); |
| 370 | } |
| 371 | |
| 372 | private: |
| 373 | Handle<String> s1_; |
| 374 | int offset1_; |
| 375 | int len1_; |
| 376 | Handle<String> s2_; |
| 377 | int offset2_; |
| 378 | int len2_; |
| 379 | }; |
| 380 | |
| 381 | |
| 382 | // Stores compare result in JSArray. Converts substring positions |
| 383 | // to absolute positions. |
| 384 | class TokensCompareOutput : public Comparator::Output { |
| 385 | public: |
| 386 | TokensCompareOutput(CompareOutputArrayWriter* array_writer, |
| 387 | int offset1, int offset2) |
| 388 | : array_writer_(array_writer), offset1_(offset1), offset2_(offset2) { |
| 389 | } |
| 390 | |
| 391 | void AddChunk(int pos1, int pos2, int len1, int len2) { |
| 392 | array_writer_->WriteChunk(pos1 + offset1_, pos2 + offset2_, len1, len2); |
| 393 | } |
| 394 | |
| 395 | private: |
| 396 | CompareOutputArrayWriter* array_writer_; |
| 397 | int offset1_; |
| 398 | int offset2_; |
| 399 | }; |
| 400 | |
| 401 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 402 | // Wraps raw n-elements line_ends array as a list of n+1 lines. The last line |
| 403 | // never has terminating new line character. |
| 404 | class LineEndsWrapper { |
| 405 | public: |
| 406 | explicit LineEndsWrapper(Handle<String> string) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 407 | : ends_array_(String::CalculateLineEnds(string, false)), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 408 | string_len_(string->length()) { |
| 409 | } |
| 410 | int length() { |
| 411 | return ends_array_->length() + 1; |
| 412 | } |
| 413 | // Returns start for any line including start of the imaginary line after |
| 414 | // the last line. |
| 415 | int GetLineStart(int index) { |
| 416 | if (index == 0) { |
| 417 | return 0; |
| 418 | } else { |
| 419 | return GetLineEnd(index - 1); |
| 420 | } |
| 421 | } |
| 422 | int GetLineEnd(int index) { |
| 423 | if (index == ends_array_->length()) { |
| 424 | // End of the last line is always an end of the whole string. |
| 425 | // If the string ends with a new line character, the last line is an |
| 426 | // empty string after this character. |
| 427 | return string_len_; |
| 428 | } else { |
| 429 | return GetPosAfterNewLine(index); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | private: |
| 434 | Handle<FixedArray> ends_array_; |
| 435 | int string_len_; |
| 436 | |
| 437 | int GetPosAfterNewLine(int index) { |
| 438 | return Smi::cast(ends_array_->get(index))->value() + 1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 439 | } |
| 440 | }; |
| 441 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 442 | |
| 443 | // Represents 2 strings as 2 arrays of lines. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 444 | class LineArrayCompareInput : public SubrangableInput { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 445 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 446 | LineArrayCompareInput(Handle<String> s1, Handle<String> s2, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 447 | LineEndsWrapper line_ends1, LineEndsWrapper line_ends2) |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 448 | : s1_(s1), s2_(s2), line_ends1_(line_ends1), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 449 | line_ends2_(line_ends2), |
| 450 | subrange_offset1_(0), subrange_offset2_(0), |
| 451 | subrange_len1_(line_ends1_.length()), |
| 452 | subrange_len2_(line_ends2_.length()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 453 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 454 | int GetLength1() { |
| 455 | return subrange_len1_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 456 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 457 | int GetLength2() { |
| 458 | return subrange_len2_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 459 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 460 | bool Equals(int index1, int index2) { |
| 461 | index1 += subrange_offset1_; |
| 462 | index2 += subrange_offset2_; |
| 463 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 464 | int line_start1 = line_ends1_.GetLineStart(index1); |
| 465 | int line_start2 = line_ends2_.GetLineStart(index2); |
| 466 | int line_end1 = line_ends1_.GetLineEnd(index1); |
| 467 | int line_end2 = line_ends2_.GetLineEnd(index2); |
| 468 | int len1 = line_end1 - line_start1; |
| 469 | int len2 = line_end2 - line_start2; |
| 470 | if (len1 != len2) { |
| 471 | return false; |
| 472 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 473 | return CompareSubstrings(s1_, line_start1, s2_, line_start2, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 474 | len1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 475 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 476 | void SetSubrange1(int offset, int len) { |
| 477 | subrange_offset1_ = offset; |
| 478 | subrange_len1_ = len; |
| 479 | } |
| 480 | void SetSubrange2(int offset, int len) { |
| 481 | subrange_offset2_ = offset; |
| 482 | subrange_len2_ = len; |
| 483 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 484 | |
| 485 | private: |
| 486 | Handle<String> s1_; |
| 487 | Handle<String> s2_; |
| 488 | LineEndsWrapper line_ends1_; |
| 489 | LineEndsWrapper line_ends2_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 490 | int subrange_offset1_; |
| 491 | int subrange_offset2_; |
| 492 | int subrange_len1_; |
| 493 | int subrange_len2_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 497 | // Stores compare result in JSArray. For each chunk tries to conduct |
| 498 | // a fine-grained nested diff token-wise. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 499 | class TokenizingLineArrayCompareOutput : public SubrangableOutput { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 500 | public: |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 501 | TokenizingLineArrayCompareOutput(LineEndsWrapper line_ends1, |
| 502 | LineEndsWrapper line_ends2, |
| 503 | Handle<String> s1, Handle<String> s2) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 504 | : array_writer_(s1->GetIsolate()), |
| 505 | line_ends1_(line_ends1), line_ends2_(line_ends2), s1_(s1), s2_(s2), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 506 | subrange_offset1_(0), subrange_offset2_(0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void AddChunk(int line_pos1, int line_pos2, int line_len1, int line_len2) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 510 | line_pos1 += subrange_offset1_; |
| 511 | line_pos2 += subrange_offset2_; |
| 512 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 513 | int char_pos1 = line_ends1_.GetLineStart(line_pos1); |
| 514 | int char_pos2 = line_ends2_.GetLineStart(line_pos2); |
| 515 | int char_len1 = line_ends1_.GetLineStart(line_pos1 + line_len1) - char_pos1; |
| 516 | int char_len2 = line_ends2_.GetLineStart(line_pos2 + line_len2) - char_pos2; |
| 517 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 518 | if (char_len1 < CHUNK_LEN_LIMIT && char_len2 < CHUNK_LEN_LIMIT) { |
| 519 | // Chunk is small enough to conduct a nested token-level diff. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 520 | HandleScope subTaskScope(s1_->GetIsolate()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 521 | |
| 522 | TokensCompareInput tokens_input(s1_, char_pos1, char_len1, |
| 523 | s2_, char_pos2, char_len2); |
| 524 | TokensCompareOutput tokens_output(&array_writer_, char_pos1, |
| 525 | char_pos2); |
| 526 | |
| 527 | Comparator::CalculateDifference(&tokens_input, &tokens_output); |
| 528 | } else { |
| 529 | array_writer_.WriteChunk(char_pos1, char_pos2, char_len1, char_len2); |
| 530 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 531 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 532 | void SetSubrange1(int offset, int len) { |
| 533 | subrange_offset1_ = offset; |
| 534 | } |
| 535 | void SetSubrange2(int offset, int len) { |
| 536 | subrange_offset2_ = offset; |
| 537 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 538 | |
| 539 | Handle<JSArray> GetResult() { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 540 | return array_writer_.GetResult(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | private: |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 544 | static const int CHUNK_LEN_LIMIT = 800; |
| 545 | |
| 546 | CompareOutputArrayWriter array_writer_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 547 | LineEndsWrapper line_ends1_; |
| 548 | LineEndsWrapper line_ends2_; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 549 | Handle<String> s1_; |
| 550 | Handle<String> s2_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 551 | int subrange_offset1_; |
| 552 | int subrange_offset2_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 553 | }; |
| 554 | |
| 555 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 556 | Handle<JSArray> LiveEdit::CompareStrings(Handle<String> s1, |
| 557 | Handle<String> s2) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 558 | s1 = String::Flatten(s1); |
| 559 | s2 = String::Flatten(s2); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 560 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 561 | LineEndsWrapper line_ends1(s1); |
| 562 | LineEndsWrapper line_ends2(s2); |
| 563 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 564 | LineArrayCompareInput input(s1, s2, line_ends1, line_ends2); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 565 | TokenizingLineArrayCompareOutput output(line_ends1, line_ends2, s1, s2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 566 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 567 | NarrowDownInput(&input, &output); |
| 568 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 569 | Comparator::CalculateDifference(&input, &output); |
| 570 | |
| 571 | return output.GetResult(); |
| 572 | } |
| 573 | |
| 574 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 575 | // Unwraps JSValue object, returning its field "value" |
| 576 | static Handle<Object> UnwrapJSValue(Handle<JSValue> jsValue) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 577 | return Handle<Object>(jsValue->value(), jsValue->GetIsolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 580 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 581 | // Wraps any object into a OpaqueReference, that will hide the object |
| 582 | // from JavaScript. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 583 | static Handle<JSValue> WrapInJSValue(Handle<HeapObject> object) { |
| 584 | Isolate* isolate = object->GetIsolate(); |
| 585 | Handle<JSFunction> constructor = isolate->opaque_reference_function(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 586 | Handle<JSValue> result = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 587 | Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 588 | result->set_value(*object); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 589 | return result; |
| 590 | } |
| 591 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 592 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 593 | static Handle<SharedFunctionInfo> UnwrapSharedFunctionInfoFromJSValue( |
| 594 | Handle<JSValue> jsValue) { |
| 595 | Object* shared = jsValue->value(); |
| 596 | CHECK(shared->IsSharedFunctionInfo()); |
| 597 | return Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(shared)); |
| 598 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 599 | |
| 600 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 601 | static int GetArrayLength(Handle<JSArray> array) { |
| 602 | Object* length = array->length(); |
| 603 | CHECK(length->IsSmi()); |
| 604 | return Smi::cast(length)->value(); |
| 605 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 606 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 607 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 608 | void FunctionInfoWrapper::SetInitialProperties(Handle<String> name, |
| 609 | int start_position, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 610 | int end_position, int param_num, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 611 | int literal_count, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 612 | int parent_index) { |
| 613 | HandleScope scope(isolate()); |
| 614 | this->SetField(kFunctionNameOffset_, name); |
| 615 | this->SetSmiValueField(kStartPositionOffset_, start_position); |
| 616 | this->SetSmiValueField(kEndPositionOffset_, end_position); |
| 617 | this->SetSmiValueField(kParamNumOffset_, param_num); |
| 618 | this->SetSmiValueField(kLiteralNumOffset_, literal_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 619 | this->SetSmiValueField(kParentIndexOffset_, parent_index); |
| 620 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 621 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 622 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 623 | void FunctionInfoWrapper::SetFunctionCode(Handle<Code> function_code, |
| 624 | Handle<HeapObject> code_scope_info) { |
| 625 | Handle<JSValue> code_wrapper = WrapInJSValue(function_code); |
| 626 | this->SetField(kCodeOffset_, code_wrapper); |
| 627 | |
| 628 | Handle<JSValue> scope_wrapper = WrapInJSValue(code_scope_info); |
| 629 | this->SetField(kCodeScopeInfoOffset_, scope_wrapper); |
| 630 | } |
| 631 | |
| 632 | |
| 633 | void FunctionInfoWrapper::SetSharedFunctionInfo( |
| 634 | Handle<SharedFunctionInfo> info) { |
| 635 | Handle<JSValue> info_holder = WrapInJSValue(info); |
| 636 | this->SetField(kSharedFunctionInfoOffset_, info_holder); |
| 637 | } |
| 638 | |
| 639 | |
| 640 | Handle<Code> FunctionInfoWrapper::GetFunctionCode() { |
| 641 | Handle<Object> element = this->GetField(kCodeOffset_); |
| 642 | Handle<JSValue> value_wrapper = Handle<JSValue>::cast(element); |
| 643 | Handle<Object> raw_result = UnwrapJSValue(value_wrapper); |
| 644 | CHECK(raw_result->IsCode()); |
| 645 | return Handle<Code>::cast(raw_result); |
| 646 | } |
| 647 | |
| 648 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 649 | MaybeHandle<TypeFeedbackVector> FunctionInfoWrapper::GetFeedbackVector() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 650 | Handle<Object> element = this->GetField(kSharedFunctionInfoOffset_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 651 | if (element->IsJSValue()) { |
| 652 | Handle<JSValue> value_wrapper = Handle<JSValue>::cast(element); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 653 | Handle<Object> raw_result = UnwrapJSValue(value_wrapper); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 654 | Handle<SharedFunctionInfo> shared = |
| 655 | Handle<SharedFunctionInfo>::cast(raw_result); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 656 | return Handle<TypeFeedbackVector>(shared->feedback_vector(), isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 657 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 658 | // Scripts may never have a SharedFunctionInfo created. |
| 659 | return MaybeHandle<TypeFeedbackVector>(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 660 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 661 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 662 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 663 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 664 | Handle<Object> FunctionInfoWrapper::GetCodeScopeInfo() { |
| 665 | Handle<Object> element = this->GetField(kCodeScopeInfoOffset_); |
| 666 | return UnwrapJSValue(Handle<JSValue>::cast(element)); |
| 667 | } |
| 668 | |
| 669 | |
| 670 | void SharedInfoWrapper::SetProperties(Handle<String> name, |
| 671 | int start_position, |
| 672 | int end_position, |
| 673 | Handle<SharedFunctionInfo> info) { |
| 674 | HandleScope scope(isolate()); |
| 675 | this->SetField(kFunctionNameOffset_, name); |
| 676 | Handle<JSValue> info_holder = WrapInJSValue(info); |
| 677 | this->SetField(kSharedInfoOffset_, info_holder); |
| 678 | this->SetSmiValueField(kStartPositionOffset_, start_position); |
| 679 | this->SetSmiValueField(kEndPositionOffset_, end_position); |
| 680 | } |
| 681 | |
| 682 | |
| 683 | Handle<SharedFunctionInfo> SharedInfoWrapper::GetInfo() { |
| 684 | Handle<Object> element = this->GetField(kSharedInfoOffset_); |
| 685 | Handle<JSValue> value_wrapper = Handle<JSValue>::cast(element); |
| 686 | return UnwrapSharedFunctionInfoFromJSValue(value_wrapper); |
| 687 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 688 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 689 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 690 | class FunctionInfoListener { |
| 691 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 692 | explicit FunctionInfoListener(Isolate* isolate) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 693 | current_parent_index_ = -1; |
| 694 | len_ = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 695 | result_ = isolate->factory()->NewJSArray(10); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | void FunctionStarted(FunctionLiteral* fun) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 699 | HandleScope scope(isolate()); |
| 700 | FunctionInfoWrapper info = FunctionInfoWrapper::Create(isolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 701 | info.SetInitialProperties(fun->name(), fun->start_position(), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 702 | fun->end_position(), fun->parameter_count(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 703 | fun->materialized_literal_count(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 704 | current_parent_index_); |
| 705 | current_parent_index_ = len_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 706 | SetElementSloppy(result_, len_, info.GetJSArray()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 707 | len_++; |
| 708 | } |
| 709 | |
| 710 | void FunctionDone() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 711 | HandleScope scope(isolate()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 712 | FunctionInfoWrapper info = |
| 713 | FunctionInfoWrapper::cast( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 714 | *Object::GetElement( |
| 715 | isolate(), result_, current_parent_index_).ToHandleChecked()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 716 | current_parent_index_ = info.GetParentIndex(); |
| 717 | } |
| 718 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 719 | // Saves only function code, because for a script function we |
| 720 | // may never create a SharedFunctionInfo object. |
| 721 | void FunctionCode(Handle<Code> function_code) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 722 | FunctionInfoWrapper info = |
| 723 | FunctionInfoWrapper::cast( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 724 | *Object::GetElement( |
| 725 | isolate(), result_, current_parent_index_).ToHandleChecked()); |
| 726 | info.SetFunctionCode(function_code, |
| 727 | Handle<HeapObject>(isolate()->heap()->null_value())); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // Saves full information about a function: its code, its scope info |
| 731 | // and a SharedFunctionInfo object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 732 | void FunctionInfo(Handle<SharedFunctionInfo> shared, Scope* scope, |
| 733 | Zone* zone) { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 734 | if (!shared->IsSharedFunctionInfo()) { |
| 735 | return; |
| 736 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 737 | FunctionInfoWrapper info = |
| 738 | FunctionInfoWrapper::cast( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 739 | *Object::GetElement( |
| 740 | isolate(), result_, current_parent_index_).ToHandleChecked()); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 741 | info.SetFunctionCode(Handle<Code>(shared->code()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 742 | Handle<HeapObject>(shared->scope_info())); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 743 | info.SetSharedFunctionInfo(shared); |
| 744 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 745 | Handle<Object> scope_info_list = SerializeFunctionScope(scope, zone); |
| 746 | info.SetFunctionScopeInfo(scope_info_list); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | Handle<JSArray> GetResult() { return result_; } |
| 750 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 751 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 752 | Isolate* isolate() const { return result_->GetIsolate(); } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 753 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 754 | Handle<Object> SerializeFunctionScope(Scope* scope, Zone* zone) { |
| 755 | Handle<JSArray> scope_info_list = isolate()->factory()->NewJSArray(10); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 756 | int scope_info_length = 0; |
| 757 | |
| 758 | // Saves some description of scope. It stores name and indexes of |
| 759 | // variables in the whole scope chain. Null-named slots delimit |
| 760 | // scopes of this chain. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 761 | Scope* current_scope = scope; |
| 762 | while (current_scope != NULL) { |
| 763 | HandleScope handle_scope(isolate()); |
| 764 | ZoneList<Variable*> stack_list(current_scope->StackLocalCount(), zone); |
| 765 | ZoneList<Variable*> context_list( |
| 766 | current_scope->ContextLocalCount(), zone); |
| 767 | current_scope->CollectStackAndContextLocals(&stack_list, &context_list); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 768 | context_list.Sort(&Variable::CompareIndex); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 769 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 770 | for (int i = 0; i < context_list.length(); i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 771 | SetElementSloppy(scope_info_list, |
| 772 | scope_info_length, |
| 773 | context_list[i]->name()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 774 | scope_info_length++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 775 | SetElementSloppy( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 776 | scope_info_list, |
| 777 | scope_info_length, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 778 | Handle<Smi>(Smi::FromInt(context_list[i]->index()), isolate())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 779 | scope_info_length++; |
| 780 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 781 | SetElementSloppy(scope_info_list, |
| 782 | scope_info_length, |
| 783 | Handle<Object>(isolate()->heap()->null_value(), |
| 784 | isolate())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 785 | scope_info_length++; |
| 786 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 787 | current_scope = current_scope->outer_scope(); |
| 788 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 789 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 790 | return scope_info_list; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 793 | Handle<JSArray> result_; |
| 794 | int len_; |
| 795 | int current_parent_index_; |
| 796 | }; |
| 797 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 798 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 799 | void LiveEdit::InitializeThreadLocal(Debug* debug) { |
| 800 | debug->thread_local_.frame_drop_mode_ = LiveEdit::FRAMES_UNTOUCHED; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 804 | bool LiveEdit::SetAfterBreakTarget(Debug* debug) { |
| 805 | Code* code = NULL; |
| 806 | Isolate* isolate = debug->isolate_; |
| 807 | switch (debug->thread_local_.frame_drop_mode_) { |
| 808 | case FRAMES_UNTOUCHED: |
| 809 | return false; |
| 810 | case FRAME_DROPPED_IN_IC_CALL: |
| 811 | // We must have been calling IC stub. Do not go there anymore. |
| 812 | code = isolate->builtins()->builtin(Builtins::kPlainReturn_LiveEdit); |
| 813 | break; |
| 814 | case FRAME_DROPPED_IN_DEBUG_SLOT_CALL: |
| 815 | // Debug break slot stub does not return normally, instead it manually |
| 816 | // cleans the stack and jumps. We should patch the jump address. |
| 817 | code = isolate->builtins()->builtin(Builtins::kFrameDropper_LiveEdit); |
| 818 | break; |
| 819 | case FRAME_DROPPED_IN_DIRECT_CALL: |
| 820 | // Nothing to do, after_break_target is not used here. |
| 821 | return true; |
| 822 | case FRAME_DROPPED_IN_RETURN_CALL: |
| 823 | code = isolate->builtins()->builtin(Builtins::kFrameDropper_LiveEdit); |
| 824 | break; |
| 825 | case CURRENTLY_SET_MODE: |
| 826 | UNREACHABLE(); |
| 827 | break; |
| 828 | } |
| 829 | debug->after_break_target_ = code->entry(); |
| 830 | return true; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | MaybeHandle<JSArray> LiveEdit::GatherCompileInfo(Handle<Script> script, |
| 835 | Handle<String> source) { |
| 836 | Isolate* isolate = script->GetIsolate(); |
| 837 | |
| 838 | FunctionInfoListener listener(isolate); |
| 839 | Handle<Object> original_source = |
| 840 | Handle<Object>(script->source(), isolate); |
| 841 | script->set_source(*source); |
| 842 | isolate->set_active_function_info_listener(&listener); |
| 843 | |
| 844 | { |
| 845 | // Creating verbose TryCatch from public API is currently the only way to |
| 846 | // force code save location. We do not use this the object directly. |
| 847 | v8::TryCatch try_catch; |
| 848 | try_catch.SetVerbose(true); |
| 849 | |
| 850 | // A logical 'try' section. |
| 851 | Compiler::CompileForLiveEdit(script); |
| 852 | } |
| 853 | |
| 854 | // A logical 'catch' section. |
| 855 | Handle<JSObject> rethrow_exception; |
| 856 | if (isolate->has_pending_exception()) { |
| 857 | Handle<Object> exception(isolate->pending_exception(), isolate); |
| 858 | MessageLocation message_location = isolate->GetMessageLocation(); |
| 859 | |
| 860 | isolate->clear_pending_message(); |
| 861 | isolate->clear_pending_exception(); |
| 862 | |
| 863 | // If possible, copy positions from message object to exception object. |
| 864 | if (exception->IsJSObject() && !message_location.script().is_null()) { |
| 865 | rethrow_exception = Handle<JSObject>::cast(exception); |
| 866 | |
| 867 | Factory* factory = isolate->factory(); |
| 868 | Handle<String> start_pos_key = factory->InternalizeOneByteString( |
| 869 | STATIC_CHAR_VECTOR("startPosition")); |
| 870 | Handle<String> end_pos_key = |
| 871 | factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("endPosition")); |
| 872 | Handle<String> script_obj_key = |
| 873 | factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("scriptObject")); |
| 874 | Handle<Smi> start_pos( |
| 875 | Smi::FromInt(message_location.start_pos()), isolate); |
| 876 | Handle<Smi> end_pos(Smi::FromInt(message_location.end_pos()), isolate); |
| 877 | Handle<JSObject> script_obj = |
| 878 | Script::GetWrapper(message_location.script()); |
| 879 | Object::SetProperty(rethrow_exception, start_pos_key, start_pos, SLOPPY) |
| 880 | .Assert(); |
| 881 | Object::SetProperty(rethrow_exception, end_pos_key, end_pos, SLOPPY) |
| 882 | .Assert(); |
| 883 | Object::SetProperty(rethrow_exception, script_obj_key, script_obj, SLOPPY) |
| 884 | .Assert(); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | // A logical 'finally' section. |
| 889 | isolate->set_active_function_info_listener(NULL); |
| 890 | script->set_source(*original_source); |
| 891 | |
| 892 | if (rethrow_exception.is_null()) { |
| 893 | return listener.GetResult(); |
| 894 | } else { |
| 895 | return isolate->Throw<JSArray>(rethrow_exception); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
| 899 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 900 | void LiveEdit::WrapSharedFunctionInfos(Handle<JSArray> array) { |
| 901 | Isolate* isolate = array->GetIsolate(); |
| 902 | HandleScope scope(isolate); |
| 903 | int len = GetArrayLength(array); |
| 904 | for (int i = 0; i < len; i++) { |
| 905 | Handle<SharedFunctionInfo> info( |
| 906 | SharedFunctionInfo::cast( |
| 907 | *Object::GetElement(isolate, array, i).ToHandleChecked())); |
| 908 | SharedInfoWrapper info_wrapper = SharedInfoWrapper::Create(isolate); |
| 909 | Handle<String> name_handle(String::cast(info->name())); |
| 910 | info_wrapper.SetProperties(name_handle, info->start_position(), |
| 911 | info->end_position(), info); |
| 912 | SetElementSloppy(array, i, info_wrapper.GetJSArray()); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | |
| 917 | // Visitor that finds all references to a particular code object, |
| 918 | // including "CODE_TARGET" references in other code objects and replaces |
| 919 | // them on the fly. |
| 920 | class ReplacingVisitor : public ObjectVisitor { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 921 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 922 | explicit ReplacingVisitor(Code* original, Code* substitution) |
| 923 | : original_(original), substitution_(substitution) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | virtual void VisitPointers(Object** start, Object** end) { |
| 927 | for (Object** p = start; p < end; p++) { |
| 928 | if (*p == original_) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 929 | *p = substitution_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 934 | virtual void VisitCodeEntry(Address entry) { |
| 935 | if (Code::GetObjectFromEntryAddress(entry) == original_) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 936 | Address substitution_entry = substitution_->instruction_start(); |
| 937 | Memory::Address_at(entry) = substitution_entry; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
| 941 | virtual void VisitCodeTarget(RelocInfo* rinfo) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 942 | if (RelocInfo::IsCodeTarget(rinfo->rmode()) && |
| 943 | Code::GetCodeFromTargetAddress(rinfo->target_address()) == original_) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 944 | Address substitution_entry = substitution_->instruction_start(); |
| 945 | rinfo->set_target_address(substitution_entry); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
| 949 | virtual void VisitDebugTarget(RelocInfo* rinfo) { |
| 950 | VisitCodeTarget(rinfo); |
| 951 | } |
| 952 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 953 | private: |
| 954 | Code* original_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 955 | Code* substitution_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 956 | }; |
| 957 | |
| 958 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 959 | // Finds all references to original and replaces them with substitution. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 960 | static void ReplaceCodeObject(Handle<Code> original, |
| 961 | Handle<Code> substitution) { |
| 962 | // Perform a full GC in order to ensure that we are not in the middle of an |
| 963 | // incremental marking phase when we are replacing the code object. |
| 964 | // Since we are not in an incremental marking phase we can write pointers |
| 965 | // to code objects (that are never in new space) without worrying about |
| 966 | // write barriers. |
| 967 | Heap* heap = original->GetHeap(); |
| 968 | HeapIterator iterator(heap); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 969 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 970 | DCHECK(!heap->InNewSpace(*substitution)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 971 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 972 | ReplacingVisitor visitor(*original, *substitution); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 973 | |
| 974 | // Iterate over all roots. Stack frames may have pointer into original code, |
| 975 | // so temporary replace the pointers with offset numbers |
| 976 | // in prologue/epilogue. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 977 | heap->IterateRoots(&visitor, VISIT_ALL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 978 | |
| 979 | // Now iterate over all pointers of all objects, including code_target |
| 980 | // implicit pointers. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 981 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 982 | obj->Iterate(&visitor); |
| 983 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 987 | // Patch function literals. |
| 988 | // Name 'literals' is a misnomer. Rather it's a cache for complex object |
| 989 | // boilerplates and for a native context. We must clean cached values. |
| 990 | // Additionally we may need to allocate a new array if number of literals |
| 991 | // changed. |
| 992 | class LiteralFixer { |
| 993 | public: |
| 994 | static void PatchLiterals(FunctionInfoWrapper* compile_info_wrapper, |
| 995 | Handle<SharedFunctionInfo> shared_info, |
| 996 | Isolate* isolate) { |
| 997 | int new_literal_count = compile_info_wrapper->GetLiteralCount(); |
| 998 | if (new_literal_count > 0) { |
| 999 | new_literal_count += JSFunction::kLiteralsPrefixSize; |
| 1000 | } |
| 1001 | int old_literal_count = shared_info->num_literals(); |
| 1002 | |
| 1003 | if (old_literal_count == new_literal_count) { |
| 1004 | // If literal count didn't change, simply go over all functions |
| 1005 | // and clear literal arrays. |
| 1006 | ClearValuesVisitor visitor; |
| 1007 | IterateJSFunctions(shared_info, &visitor); |
| 1008 | } else { |
| 1009 | // When literal count changes, we have to create new array instances. |
| 1010 | // Since we cannot create instances when iterating heap, we should first |
| 1011 | // collect all functions and fix their literal arrays. |
| 1012 | Handle<FixedArray> function_instances = |
| 1013 | CollectJSFunctions(shared_info, isolate); |
| 1014 | for (int i = 0; i < function_instances->length(); i++) { |
| 1015 | Handle<JSFunction> fun(JSFunction::cast(function_instances->get(i))); |
| 1016 | Handle<FixedArray> old_literals(fun->literals()); |
| 1017 | Handle<FixedArray> new_literals = |
| 1018 | isolate->factory()->NewFixedArray(new_literal_count); |
| 1019 | if (new_literal_count > 0) { |
| 1020 | Handle<Context> native_context; |
| 1021 | if (old_literals->length() > |
| 1022 | JSFunction::kLiteralNativeContextIndex) { |
| 1023 | native_context = Handle<Context>( |
| 1024 | JSFunction::NativeContextFromLiterals(fun->literals())); |
| 1025 | } else { |
| 1026 | native_context = Handle<Context>(fun->context()->native_context()); |
| 1027 | } |
| 1028 | new_literals->set(JSFunction::kLiteralNativeContextIndex, |
| 1029 | *native_context); |
| 1030 | } |
| 1031 | fun->set_literals(*new_literals); |
| 1032 | } |
| 1033 | |
| 1034 | shared_info->set_num_literals(new_literal_count); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | private: |
| 1039 | // Iterates all function instances in the HEAP that refers to the |
| 1040 | // provided shared_info. |
| 1041 | template<typename Visitor> |
| 1042 | static void IterateJSFunctions(Handle<SharedFunctionInfo> shared_info, |
| 1043 | Visitor* visitor) { |
| 1044 | HeapIterator iterator(shared_info->GetHeap()); |
| 1045 | for (HeapObject* obj = iterator.next(); obj != NULL; |
| 1046 | obj = iterator.next()) { |
| 1047 | if (obj->IsJSFunction()) { |
| 1048 | JSFunction* function = JSFunction::cast(obj); |
| 1049 | if (function->shared() == *shared_info) { |
| 1050 | visitor->visit(function); |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | // Finds all instances of JSFunction that refers to the provided shared_info |
| 1057 | // and returns array with them. |
| 1058 | static Handle<FixedArray> CollectJSFunctions( |
| 1059 | Handle<SharedFunctionInfo> shared_info, Isolate* isolate) { |
| 1060 | CountVisitor count_visitor; |
| 1061 | count_visitor.count = 0; |
| 1062 | IterateJSFunctions(shared_info, &count_visitor); |
| 1063 | int size = count_visitor.count; |
| 1064 | |
| 1065 | Handle<FixedArray> result = isolate->factory()->NewFixedArray(size); |
| 1066 | if (size > 0) { |
| 1067 | CollectVisitor collect_visitor(result); |
| 1068 | IterateJSFunctions(shared_info, &collect_visitor); |
| 1069 | } |
| 1070 | return result; |
| 1071 | } |
| 1072 | |
| 1073 | class ClearValuesVisitor { |
| 1074 | public: |
| 1075 | void visit(JSFunction* fun) { |
| 1076 | FixedArray* literals = fun->literals(); |
| 1077 | int len = literals->length(); |
| 1078 | for (int j = JSFunction::kLiteralsPrefixSize; j < len; j++) { |
| 1079 | literals->set_undefined(j); |
| 1080 | } |
| 1081 | } |
| 1082 | }; |
| 1083 | |
| 1084 | class CountVisitor { |
| 1085 | public: |
| 1086 | void visit(JSFunction* fun) { |
| 1087 | count++; |
| 1088 | } |
| 1089 | int count; |
| 1090 | }; |
| 1091 | |
| 1092 | class CollectVisitor { |
| 1093 | public: |
| 1094 | explicit CollectVisitor(Handle<FixedArray> output) |
| 1095 | : m_output(output), m_pos(0) {} |
| 1096 | |
| 1097 | void visit(JSFunction* fun) { |
| 1098 | m_output->set(m_pos, fun); |
| 1099 | m_pos++; |
| 1100 | } |
| 1101 | private: |
| 1102 | Handle<FixedArray> m_output; |
| 1103 | int m_pos; |
| 1104 | }; |
| 1105 | }; |
| 1106 | |
| 1107 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1108 | // Check whether the code is natural function code (not a lazy-compile stub |
| 1109 | // code). |
| 1110 | static bool IsJSFunctionCode(Code* code) { |
| 1111 | return code->kind() == Code::FUNCTION; |
| 1112 | } |
| 1113 | |
| 1114 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1115 | // Returns true if an instance of candidate were inlined into function's code. |
| 1116 | static bool IsInlined(JSFunction* function, SharedFunctionInfo* candidate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1117 | DisallowHeapAllocation no_gc; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1118 | |
| 1119 | if (function->code()->kind() != Code::OPTIMIZED_FUNCTION) return false; |
| 1120 | |
| 1121 | DeoptimizationInputData* data = |
| 1122 | DeoptimizationInputData::cast(function->code()->deoptimization_data()); |
| 1123 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1124 | if (data == function->GetIsolate()->heap()->empty_fixed_array()) { |
| 1125 | return false; |
| 1126 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1127 | |
| 1128 | FixedArray* literals = data->LiteralArray(); |
| 1129 | |
| 1130 | int inlined_count = data->InlinedFunctionCount()->value(); |
| 1131 | for (int i = 0; i < inlined_count; ++i) { |
| 1132 | JSFunction* inlined = JSFunction::cast(literals->get(i)); |
| 1133 | if (inlined->shared() == candidate) return true; |
| 1134 | } |
| 1135 | |
| 1136 | return false; |
| 1137 | } |
| 1138 | |
| 1139 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1140 | // Marks code that shares the same shared function info or has inlined |
| 1141 | // code that shares the same function info. |
| 1142 | class DependentFunctionMarker: public OptimizedFunctionVisitor { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1143 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1144 | SharedFunctionInfo* shared_info_; |
| 1145 | bool found_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1146 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1147 | explicit DependentFunctionMarker(SharedFunctionInfo* shared_info) |
| 1148 | : shared_info_(shared_info), found_(false) { } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1149 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1150 | virtual void EnterContext(Context* context) { } // Don't care. |
| 1151 | virtual void LeaveContext(Context* context) { } // Don't care. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1152 | virtual void VisitFunction(JSFunction* function) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1153 | // It should be guaranteed by the iterator that everything is optimized. |
| 1154 | DCHECK(function->code()->kind() == Code::OPTIMIZED_FUNCTION); |
| 1155 | if (shared_info_ == function->shared() || |
| 1156 | IsInlined(function, shared_info_)) { |
| 1157 | // Mark the code for deoptimization. |
| 1158 | function->code()->set_marked_for_deoptimization(true); |
| 1159 | found_ = true; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1160 | } |
| 1161 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1162 | }; |
| 1163 | |
| 1164 | |
| 1165 | static void DeoptimizeDependentFunctions(SharedFunctionInfo* function_info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1166 | DisallowHeapAllocation no_allocation; |
| 1167 | DependentFunctionMarker marker(function_info); |
| 1168 | // TODO(titzer): need to traverse all optimized code to find OSR code here. |
| 1169 | Deoptimizer::VisitAllOptimizedFunctions(function_info->GetIsolate(), &marker); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1170 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1171 | if (marker.found_) { |
| 1172 | // Only go through with the deoptimization if something was found. |
| 1173 | Deoptimizer::DeoptimizeMarkedCode(function_info->GetIsolate()); |
| 1174 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1178 | void LiveEdit::ReplaceFunctionCode( |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1179 | Handle<JSArray> new_compile_info_array, |
| 1180 | Handle<JSArray> shared_info_array) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1181 | Isolate* isolate = new_compile_info_array->GetIsolate(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1182 | |
| 1183 | FunctionInfoWrapper compile_info_wrapper(new_compile_info_array); |
| 1184 | SharedInfoWrapper shared_info_wrapper(shared_info_array); |
| 1185 | |
| 1186 | Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); |
| 1187 | |
| 1188 | if (IsJSFunctionCode(shared_info->code())) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1189 | Handle<Code> code = compile_info_wrapper.GetFunctionCode(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1190 | ReplaceCodeObject(Handle<Code>(shared_info->code()), code); |
| 1191 | Handle<Object> code_scope_info = compile_info_wrapper.GetCodeScopeInfo(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1192 | if (code_scope_info->IsFixedArray()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1193 | shared_info->set_scope_info(ScopeInfo::cast(*code_scope_info)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1194 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1195 | shared_info->DisableOptimization(kLiveEdit); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1196 | // Update the type feedback vector, if needed. |
| 1197 | MaybeHandle<TypeFeedbackVector> feedback_vector = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1198 | compile_info_wrapper.GetFeedbackVector(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1199 | if (!feedback_vector.is_null()) { |
| 1200 | shared_info->set_feedback_vector(*feedback_vector.ToHandleChecked()); |
| 1201 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | if (shared_info->debug_info()->IsDebugInfo()) { |
| 1205 | Handle<DebugInfo> debug_info(DebugInfo::cast(shared_info->debug_info())); |
| 1206 | Handle<Code> new_original_code = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1207 | isolate->factory()->CopyCode(compile_info_wrapper.GetFunctionCode()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1208 | debug_info->set_original_code(*new_original_code); |
| 1209 | } |
| 1210 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1211 | int start_position = compile_info_wrapper.GetStartPosition(); |
| 1212 | int end_position = compile_info_wrapper.GetEndPosition(); |
| 1213 | shared_info->set_start_position(start_position); |
| 1214 | shared_info->set_end_position(end_position); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1215 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1216 | LiteralFixer::PatchLiterals(&compile_info_wrapper, shared_info, isolate); |
| 1217 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1218 | shared_info->set_construct_stub( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1219 | isolate->builtins()->builtin(Builtins::kJSConstructStubGeneric)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1220 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1221 | DeoptimizeDependentFunctions(*shared_info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1222 | isolate->compilation_cache()->Remove(shared_info); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1226 | void LiveEdit::FunctionSourceUpdated(Handle<JSArray> shared_info_array) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1227 | SharedInfoWrapper shared_info_wrapper(shared_info_array); |
| 1228 | Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); |
| 1229 | |
| 1230 | DeoptimizeDependentFunctions(*shared_info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1231 | shared_info_array->GetIsolate()->compilation_cache()->Remove(shared_info); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1235 | void LiveEdit::SetFunctionScript(Handle<JSValue> function_wrapper, |
| 1236 | Handle<Object> script_handle) { |
| 1237 | Handle<SharedFunctionInfo> shared_info = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1238 | UnwrapSharedFunctionInfoFromJSValue(function_wrapper); |
| 1239 | CHECK(script_handle->IsScript() || script_handle->IsUndefined()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1240 | shared_info->set_script(*script_handle); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1241 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1242 | function_wrapper->GetIsolate()->compilation_cache()->Remove(shared_info); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | // For a script text change (defined as position_change_array), translates |
| 1247 | // position in unchanged text to position in changed text. |
| 1248 | // Text change is a set of non-overlapping regions in text, that have changed |
| 1249 | // their contents and length. It is specified as array of groups of 3 numbers: |
| 1250 | // (change_begin, change_end, change_end_new_position). |
| 1251 | // Each group describes a change in text; groups are sorted by change_begin. |
| 1252 | // Only position in text beyond any changes may be successfully translated. |
| 1253 | // If a positions is inside some region that changed, result is currently |
| 1254 | // undefined. |
| 1255 | static int TranslatePosition(int original_position, |
| 1256 | Handle<JSArray> position_change_array) { |
| 1257 | int position_diff = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1258 | int array_len = GetArrayLength(position_change_array); |
| 1259 | Isolate* isolate = position_change_array->GetIsolate(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1260 | // TODO(635): binary search may be used here |
| 1261 | for (int i = 0; i < array_len; i += 3) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1262 | HandleScope scope(isolate); |
| 1263 | Handle<Object> element = Object::GetElement( |
| 1264 | isolate, position_change_array, i).ToHandleChecked(); |
| 1265 | CHECK(element->IsSmi()); |
| 1266 | int chunk_start = Handle<Smi>::cast(element)->value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1267 | if (original_position < chunk_start) { |
| 1268 | break; |
| 1269 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1270 | element = Object::GetElement( |
| 1271 | isolate, position_change_array, i + 1).ToHandleChecked(); |
| 1272 | CHECK(element->IsSmi()); |
| 1273 | int chunk_end = Handle<Smi>::cast(element)->value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1274 | // Position mustn't be inside a chunk. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1275 | DCHECK(original_position >= chunk_end); |
| 1276 | element = Object::GetElement( |
| 1277 | isolate, position_change_array, i + 2).ToHandleChecked(); |
| 1278 | CHECK(element->IsSmi()); |
| 1279 | int chunk_changed_end = Handle<Smi>::cast(element)->value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1280 | position_diff = chunk_changed_end - chunk_end; |
| 1281 | } |
| 1282 | |
| 1283 | return original_position + position_diff; |
| 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | // Auto-growing buffer for writing relocation info code section. This buffer |
| 1288 | // is a simplified version of buffer from Assembler. Unlike Assembler, this |
| 1289 | // class is platform-independent and it works without dealing with instructions. |
| 1290 | // As specified by RelocInfo format, the buffer is filled in reversed order: |
| 1291 | // from upper to lower addresses. |
| 1292 | // It uses NewArray/DeleteArray for memory management. |
| 1293 | class RelocInfoBuffer { |
| 1294 | public: |
| 1295 | RelocInfoBuffer(int buffer_initial_capicity, byte* pc) { |
| 1296 | buffer_size_ = buffer_initial_capicity + kBufferGap; |
| 1297 | buffer_ = NewArray<byte>(buffer_size_); |
| 1298 | |
| 1299 | reloc_info_writer_.Reposition(buffer_ + buffer_size_, pc); |
| 1300 | } |
| 1301 | ~RelocInfoBuffer() { |
| 1302 | DeleteArray(buffer_); |
| 1303 | } |
| 1304 | |
| 1305 | // As specified by RelocInfo format, the buffer is filled in reversed order: |
| 1306 | // from upper to lower addresses. |
| 1307 | void Write(const RelocInfo* rinfo) { |
| 1308 | if (buffer_ + kBufferGap >= reloc_info_writer_.pos()) { |
| 1309 | Grow(); |
| 1310 | } |
| 1311 | reloc_info_writer_.Write(rinfo); |
| 1312 | } |
| 1313 | |
| 1314 | Vector<byte> GetResult() { |
| 1315 | // Return the bytes from pos up to end of buffer. |
| 1316 | int result_size = |
| 1317 | static_cast<int>((buffer_ + buffer_size_) - reloc_info_writer_.pos()); |
| 1318 | return Vector<byte>(reloc_info_writer_.pos(), result_size); |
| 1319 | } |
| 1320 | |
| 1321 | private: |
| 1322 | void Grow() { |
| 1323 | // Compute new buffer size. |
| 1324 | int new_buffer_size; |
| 1325 | if (buffer_size_ < 2 * KB) { |
| 1326 | new_buffer_size = 4 * KB; |
| 1327 | } else { |
| 1328 | new_buffer_size = 2 * buffer_size_; |
| 1329 | } |
| 1330 | // Some internal data structures overflow for very large buffers, |
| 1331 | // they must ensure that kMaximalBufferSize is not too large. |
| 1332 | if (new_buffer_size > kMaximalBufferSize) { |
| 1333 | V8::FatalProcessOutOfMemory("RelocInfoBuffer::GrowBuffer"); |
| 1334 | } |
| 1335 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1336 | // Set up new buffer. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1337 | byte* new_buffer = NewArray<byte>(new_buffer_size); |
| 1338 | |
| 1339 | // Copy the data. |
| 1340 | int curently_used_size = |
| 1341 | static_cast<int>(buffer_ + buffer_size_ - reloc_info_writer_.pos()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1342 | MemMove(new_buffer + new_buffer_size - curently_used_size, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1343 | reloc_info_writer_.pos(), curently_used_size); |
| 1344 | |
| 1345 | reloc_info_writer_.Reposition( |
| 1346 | new_buffer + new_buffer_size - curently_used_size, |
| 1347 | reloc_info_writer_.last_pc()); |
| 1348 | |
| 1349 | DeleteArray(buffer_); |
| 1350 | buffer_ = new_buffer; |
| 1351 | buffer_size_ = new_buffer_size; |
| 1352 | } |
| 1353 | |
| 1354 | RelocInfoWriter reloc_info_writer_; |
| 1355 | byte* buffer_; |
| 1356 | int buffer_size_; |
| 1357 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1358 | static const int kBufferGap = RelocInfoWriter::kMaxSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1359 | static const int kMaximalBufferSize = 512*MB; |
| 1360 | }; |
| 1361 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1362 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1363 | // Patch positions in code (changes relocation info section) and possibly |
| 1364 | // returns new instance of code. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1365 | static Handle<Code> PatchPositionsInCode( |
| 1366 | Handle<Code> code, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1367 | Handle<JSArray> position_change_array) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1368 | Isolate* isolate = code->GetIsolate(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1369 | |
| 1370 | RelocInfoBuffer buffer_writer(code->relocation_size(), |
| 1371 | code->instruction_start()); |
| 1372 | |
| 1373 | { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1374 | for (RelocIterator it(*code); !it.done(); it.next()) { |
| 1375 | RelocInfo* rinfo = it.rinfo(); |
| 1376 | if (RelocInfo::IsPosition(rinfo->rmode())) { |
| 1377 | int position = static_cast<int>(rinfo->data()); |
| 1378 | int new_position = TranslatePosition(position, |
| 1379 | position_change_array); |
| 1380 | if (position != new_position) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1381 | RelocInfo info_copy(rinfo->pc(), rinfo->rmode(), new_position, NULL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1382 | buffer_writer.Write(&info_copy); |
| 1383 | continue; |
| 1384 | } |
| 1385 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1386 | if (RelocInfo::IsRealRelocMode(rinfo->rmode())) { |
| 1387 | buffer_writer.Write(it.rinfo()); |
| 1388 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | Vector<byte> buffer = buffer_writer.GetResult(); |
| 1393 | |
| 1394 | if (buffer.length() == code->relocation_size()) { |
| 1395 | // Simply patch relocation area of code. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1396 | MemCopy(code->relocation_start(), buffer.start(), buffer.length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1397 | return code; |
| 1398 | } else { |
| 1399 | // Relocation info section now has different size. We cannot simply |
| 1400 | // rewrite it inside code object. Instead we have to create a new |
| 1401 | // code object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1402 | Handle<Code> result(isolate->factory()->CopyCode(code, buffer)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1403 | return result; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1408 | void LiveEdit::PatchFunctionPositions(Handle<JSArray> shared_info_array, |
| 1409 | Handle<JSArray> position_change_array) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1410 | SharedInfoWrapper shared_info_wrapper(shared_info_array); |
| 1411 | Handle<SharedFunctionInfo> info = shared_info_wrapper.GetInfo(); |
| 1412 | |
| 1413 | int old_function_start = info->start_position(); |
| 1414 | int new_function_start = TranslatePosition(old_function_start, |
| 1415 | position_change_array); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1416 | int new_function_end = TranslatePosition(info->end_position(), |
| 1417 | position_change_array); |
| 1418 | int new_function_token_pos = |
| 1419 | TranslatePosition(info->function_token_position(), position_change_array); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1420 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1421 | info->set_start_position(new_function_start); |
| 1422 | info->set_end_position(new_function_end); |
| 1423 | info->set_function_token_position(new_function_token_pos); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1424 | |
| 1425 | if (IsJSFunctionCode(info->code())) { |
| 1426 | // Patch relocation info section of the code. |
| 1427 | Handle<Code> patched_code = PatchPositionsInCode(Handle<Code>(info->code()), |
| 1428 | position_change_array); |
| 1429 | if (*patched_code != info->code()) { |
| 1430 | // Replace all references to the code across the heap. In particular, |
| 1431 | // some stubs may refer to this code and this code may be being executed |
| 1432 | // on stack (it is safe to substitute the code object on stack, because |
| 1433 | // we only change the structure of rinfo and leave instructions |
| 1434 | // untouched). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1435 | ReplaceCodeObject(Handle<Code>(info->code()), patched_code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1436 | } |
| 1437 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | |
| 1441 | static Handle<Script> CreateScriptCopy(Handle<Script> original) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1442 | Isolate* isolate = original->GetIsolate(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1443 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1444 | Handle<String> original_source(String::cast(original->source())); |
| 1445 | Handle<Script> copy = isolate->factory()->NewScript(original_source); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1446 | |
| 1447 | copy->set_name(original->name()); |
| 1448 | copy->set_line_offset(original->line_offset()); |
| 1449 | copy->set_column_offset(original->column_offset()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1450 | copy->set_type(original->type()); |
| 1451 | copy->set_context_data(original->context_data()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1452 | copy->set_eval_from_shared(original->eval_from_shared()); |
| 1453 | copy->set_eval_from_instructions_offset( |
| 1454 | original->eval_from_instructions_offset()); |
| 1455 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1456 | // Copy all the flags, but clear compilation state. |
| 1457 | copy->set_flags(original->flags()); |
| 1458 | copy->set_compilation_state(Script::COMPILATION_STATE_INITIAL); |
| 1459 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1460 | return copy; |
| 1461 | } |
| 1462 | |
| 1463 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1464 | Handle<Object> LiveEdit::ChangeScriptSource(Handle<Script> original_script, |
| 1465 | Handle<String> new_source, |
| 1466 | Handle<Object> old_script_name) { |
| 1467 | Isolate* isolate = original_script->GetIsolate(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1468 | Handle<Object> old_script_object; |
| 1469 | if (old_script_name->IsString()) { |
| 1470 | Handle<Script> old_script = CreateScriptCopy(original_script); |
| 1471 | old_script->set_name(String::cast(*old_script_name)); |
| 1472 | old_script_object = old_script; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1473 | isolate->debug()->OnAfterCompile(old_script); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1474 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1475 | old_script_object = isolate->factory()->null_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | original_script->set_source(*new_source); |
| 1479 | |
| 1480 | // Drop line ends so that they will be recalculated. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1481 | original_script->set_line_ends(isolate->heap()->undefined_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1482 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1483 | return old_script_object; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | void LiveEdit::ReplaceRefToNestedFunction( |
| 1489 | Handle<JSValue> parent_function_wrapper, |
| 1490 | Handle<JSValue> orig_function_wrapper, |
| 1491 | Handle<JSValue> subst_function_wrapper) { |
| 1492 | |
| 1493 | Handle<SharedFunctionInfo> parent_shared = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1494 | UnwrapSharedFunctionInfoFromJSValue(parent_function_wrapper); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1495 | Handle<SharedFunctionInfo> orig_shared = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1496 | UnwrapSharedFunctionInfoFromJSValue(orig_function_wrapper); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1497 | Handle<SharedFunctionInfo> subst_shared = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1498 | UnwrapSharedFunctionInfoFromJSValue(subst_function_wrapper); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1499 | |
| 1500 | for (RelocIterator it(parent_shared->code()); !it.done(); it.next()) { |
| 1501 | if (it.rinfo()->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 1502 | if (it.rinfo()->target_object() == *orig_shared) { |
| 1503 | it.rinfo()->set_target_object(*subst_shared); |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | // Check an activation against list of functions. If there is a function |
| 1511 | // that matches, its status in result array is changed to status argument value. |
| 1512 | static bool CheckActivation(Handle<JSArray> shared_info_array, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1513 | Handle<JSArray> result, |
| 1514 | StackFrame* frame, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1515 | LiveEdit::FunctionPatchabilityStatus status) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1516 | if (!frame->is_java_script()) return false; |
| 1517 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1518 | Handle<JSFunction> function(JavaScriptFrame::cast(frame)->function()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1519 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1520 | Isolate* isolate = shared_info_array->GetIsolate(); |
| 1521 | int len = GetArrayLength(shared_info_array); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1522 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1523 | HandleScope scope(isolate); |
| 1524 | Handle<Object> element = |
| 1525 | Object::GetElement(isolate, shared_info_array, i).ToHandleChecked(); |
| 1526 | Handle<JSValue> jsvalue = Handle<JSValue>::cast(element); |
| 1527 | Handle<SharedFunctionInfo> shared = |
| 1528 | UnwrapSharedFunctionInfoFromJSValue(jsvalue); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1529 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1530 | if (function->shared() == *shared || IsInlined(*function, *shared)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1531 | SetElementSloppy(result, i, Handle<Smi>(Smi::FromInt(status), isolate)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1532 | return true; |
| 1533 | } |
| 1534 | } |
| 1535 | return false; |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | // Iterates over handler chain and removes all elements that are inside |
| 1540 | // frames being dropped. |
| 1541 | static bool FixTryCatchHandler(StackFrame* top_frame, |
| 1542 | StackFrame* bottom_frame) { |
| 1543 | Address* pointer_address = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1544 | &Memory::Address_at(top_frame->isolate()->get_address_from_id( |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1545 | Isolate::kHandlerAddress)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1546 | |
| 1547 | while (*pointer_address < top_frame->sp()) { |
| 1548 | pointer_address = &Memory::Address_at(*pointer_address); |
| 1549 | } |
| 1550 | Address* above_frame_address = pointer_address; |
| 1551 | while (*pointer_address < bottom_frame->fp()) { |
| 1552 | pointer_address = &Memory::Address_at(*pointer_address); |
| 1553 | } |
| 1554 | bool change = *above_frame_address != *pointer_address; |
| 1555 | *above_frame_address = *pointer_address; |
| 1556 | return change; |
| 1557 | } |
| 1558 | |
| 1559 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1560 | // Initializes an artificial stack frame. The data it contains is used for: |
| 1561 | // a. successful work of frame dropper code which eventually gets control, |
| 1562 | // b. being compatible with regular stack structure for various stack |
| 1563 | // iterators. |
| 1564 | // Returns address of stack allocated pointer to restarted function, |
| 1565 | // the value that is called 'restarter_frame_function_pointer'. The value |
| 1566 | // at this address (possibly updated by GC) may be used later when preparing |
| 1567 | // 'step in' operation. |
| 1568 | // Frame structure (conforms InternalFrame structure): |
| 1569 | // -- code |
| 1570 | // -- SMI maker |
| 1571 | // -- function (slot is called "context") |
| 1572 | // -- frame base |
| 1573 | static Object** SetUpFrameDropperFrame(StackFrame* bottom_js_frame, |
| 1574 | Handle<Code> code) { |
| 1575 | DCHECK(bottom_js_frame->is_java_script()); |
| 1576 | |
| 1577 | Address fp = bottom_js_frame->fp(); |
| 1578 | |
| 1579 | // Move function pointer into "context" slot. |
| 1580 | Memory::Object_at(fp + StandardFrameConstants::kContextOffset) = |
| 1581 | Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset); |
| 1582 | |
| 1583 | Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code; |
| 1584 | Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) = |
| 1585 | Smi::FromInt(StackFrame::INTERNAL); |
| 1586 | |
| 1587 | return reinterpret_cast<Object**>(&Memory::Object_at( |
| 1588 | fp + StandardFrameConstants::kContextOffset)); |
| 1589 | } |
| 1590 | |
| 1591 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1592 | // Removes specified range of frames from stack. There may be 1 or more |
| 1593 | // frames in range. Anyway the bottom frame is restarted rather than dropped, |
| 1594 | // and therefore has to be a JavaScript frame. |
| 1595 | // Returns error message or NULL. |
| 1596 | static const char* DropFrames(Vector<StackFrame*> frames, |
| 1597 | int top_frame_index, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1598 | int bottom_js_frame_index, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1599 | LiveEdit::FrameDropMode* mode, |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1600 | Object*** restarter_frame_function_pointer) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1601 | if (!LiveEdit::kFrameDropperSupported) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1602 | return "Stack manipulations are not supported in this architecture."; |
| 1603 | } |
| 1604 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1605 | StackFrame* pre_top_frame = frames[top_frame_index - 1]; |
| 1606 | StackFrame* top_frame = frames[top_frame_index]; |
| 1607 | StackFrame* bottom_js_frame = frames[bottom_js_frame_index]; |
| 1608 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1609 | DCHECK(bottom_js_frame->is_java_script()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1610 | |
| 1611 | // Check the nature of the top frame. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1612 | Isolate* isolate = bottom_js_frame->isolate(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1613 | Code* pre_top_frame_code = pre_top_frame->LookupCode(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1614 | bool frame_has_padding = true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1615 | if (pre_top_frame_code->is_inline_cache_stub() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1616 | pre_top_frame_code->is_debug_stub()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1617 | // OK, we can drop inline cache calls. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1618 | *mode = LiveEdit::FRAME_DROPPED_IN_IC_CALL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1619 | } else if (pre_top_frame_code == |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1620 | isolate->builtins()->builtin(Builtins::kSlot_DebugBreak)) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1621 | // OK, we can drop debug break slot. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1622 | *mode = LiveEdit::FRAME_DROPPED_IN_DEBUG_SLOT_CALL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1623 | } else if (pre_top_frame_code == |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1624 | isolate->builtins()->builtin(Builtins::kFrameDropper_LiveEdit)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1625 | // OK, we can drop our own code. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1626 | pre_top_frame = frames[top_frame_index - 2]; |
| 1627 | top_frame = frames[top_frame_index - 1]; |
| 1628 | *mode = LiveEdit::CURRENTLY_SET_MODE; |
| 1629 | frame_has_padding = false; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1630 | } else if (pre_top_frame_code == |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1631 | isolate->builtins()->builtin(Builtins::kReturn_DebugBreak)) { |
| 1632 | *mode = LiveEdit::FRAME_DROPPED_IN_RETURN_CALL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1633 | } else if (pre_top_frame_code->kind() == Code::STUB && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1634 | CodeStub::GetMajorKey(pre_top_frame_code) == CodeStub::CEntry) { |
| 1635 | // Entry from our unit tests on 'debugger' statement. |
| 1636 | // It's fine, we support this case. |
| 1637 | *mode = LiveEdit::FRAME_DROPPED_IN_DIRECT_CALL; |
| 1638 | // We don't have a padding from 'debugger' statement call. |
| 1639 | // Here the stub is CEntry, it's not debug-only and can't be padded. |
| 1640 | // If anyone would complain, a proxy padded stub could be added. |
| 1641 | frame_has_padding = false; |
| 1642 | } else if (pre_top_frame->type() == StackFrame::ARGUMENTS_ADAPTOR) { |
| 1643 | // This must be adaptor that remain from the frame dropping that |
| 1644 | // is still on stack. A frame dropper frame must be above it. |
| 1645 | DCHECK(frames[top_frame_index - 2]->LookupCode() == |
| 1646 | isolate->builtins()->builtin(Builtins::kFrameDropper_LiveEdit)); |
| 1647 | pre_top_frame = frames[top_frame_index - 3]; |
| 1648 | top_frame = frames[top_frame_index - 2]; |
| 1649 | *mode = LiveEdit::CURRENTLY_SET_MODE; |
| 1650 | frame_has_padding = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1651 | } else { |
| 1652 | return "Unknown structure of stack above changing function"; |
| 1653 | } |
| 1654 | |
| 1655 | Address unused_stack_top = top_frame->sp(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1656 | int new_frame_size = LiveEdit::kFrameDropperFrameSize * kPointerSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1657 | Address unused_stack_bottom = bottom_js_frame->fp() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1658 | - new_frame_size + kPointerSize; // Bigger address end is exclusive. |
| 1659 | |
| 1660 | Address* top_frame_pc_address = top_frame->pc_address(); |
| 1661 | |
| 1662 | // top_frame may be damaged below this point. Do not used it. |
| 1663 | DCHECK(!(top_frame = NULL)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1664 | |
| 1665 | if (unused_stack_top > unused_stack_bottom) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1666 | if (frame_has_padding) { |
| 1667 | int shortage_bytes = |
| 1668 | static_cast<int>(unused_stack_top - unused_stack_bottom); |
| 1669 | |
| 1670 | Address padding_start = pre_top_frame->fp() - |
| 1671 | LiveEdit::kFrameDropperFrameSize * kPointerSize; |
| 1672 | |
| 1673 | Address padding_pointer = padding_start; |
| 1674 | Smi* padding_object = Smi::FromInt(LiveEdit::kFramePaddingValue); |
| 1675 | while (Memory::Object_at(padding_pointer) == padding_object) { |
| 1676 | padding_pointer -= kPointerSize; |
| 1677 | } |
| 1678 | int padding_counter = |
| 1679 | Smi::cast(Memory::Object_at(padding_pointer))->value(); |
| 1680 | if (padding_counter * kPointerSize < shortage_bytes) { |
| 1681 | return "Not enough space for frame dropper frame " |
| 1682 | "(even with padding frame)"; |
| 1683 | } |
| 1684 | Memory::Object_at(padding_pointer) = |
| 1685 | Smi::FromInt(padding_counter - shortage_bytes / kPointerSize); |
| 1686 | |
| 1687 | StackFrame* pre_pre_frame = frames[top_frame_index - 2]; |
| 1688 | |
| 1689 | MemMove(padding_start + kPointerSize - shortage_bytes, |
| 1690 | padding_start + kPointerSize, |
| 1691 | LiveEdit::kFrameDropperFrameSize * kPointerSize); |
| 1692 | |
| 1693 | pre_top_frame->UpdateFp(pre_top_frame->fp() - shortage_bytes); |
| 1694 | pre_pre_frame->SetCallerFp(pre_top_frame->fp()); |
| 1695 | unused_stack_top -= shortage_bytes; |
| 1696 | |
| 1697 | STATIC_ASSERT(sizeof(Address) == kPointerSize); |
| 1698 | top_frame_pc_address -= shortage_bytes / kPointerSize; |
| 1699 | } else { |
| 1700 | return "Not enough space for frame dropper frame"; |
| 1701 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | // Committing now. After this point we should return only NULL value. |
| 1705 | |
| 1706 | FixTryCatchHandler(pre_top_frame, bottom_js_frame); |
| 1707 | // Make sure FixTryCatchHandler is idempotent. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1708 | DCHECK(!FixTryCatchHandler(pre_top_frame, bottom_js_frame)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1709 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1710 | Handle<Code> code = isolate->builtins()->FrameDropper_LiveEdit(); |
| 1711 | *top_frame_pc_address = code->entry(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1712 | pre_top_frame->SetCallerFp(bottom_js_frame->fp()); |
| 1713 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1714 | *restarter_frame_function_pointer = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1715 | SetUpFrameDropperFrame(bottom_js_frame, code); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1716 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1717 | DCHECK((**restarter_frame_function_pointer)->IsJSFunction()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1718 | |
| 1719 | for (Address a = unused_stack_top; |
| 1720 | a < unused_stack_bottom; |
| 1721 | a += kPointerSize) { |
| 1722 | Memory::Object_at(a) = Smi::FromInt(0); |
| 1723 | } |
| 1724 | |
| 1725 | return NULL; |
| 1726 | } |
| 1727 | |
| 1728 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1729 | // Describes a set of call frames that execute any of listed functions. |
| 1730 | // Finding no such frames does not mean error. |
| 1731 | class MultipleFunctionTarget { |
| 1732 | public: |
| 1733 | MultipleFunctionTarget(Handle<JSArray> shared_info_array, |
| 1734 | Handle<JSArray> result) |
| 1735 | : m_shared_info_array(shared_info_array), |
| 1736 | m_result(result) {} |
| 1737 | bool MatchActivation(StackFrame* frame, |
| 1738 | LiveEdit::FunctionPatchabilityStatus status) { |
| 1739 | return CheckActivation(m_shared_info_array, m_result, frame, status); |
| 1740 | } |
| 1741 | const char* GetNotFoundMessage() const { |
| 1742 | return NULL; |
| 1743 | } |
| 1744 | private: |
| 1745 | Handle<JSArray> m_shared_info_array; |
| 1746 | Handle<JSArray> m_result; |
| 1747 | }; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1748 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1749 | |
| 1750 | // Drops all call frame matched by target and all frames above them. |
| 1751 | template<typename TARGET> |
| 1752 | static const char* DropActivationsInActiveThreadImpl( |
| 1753 | Isolate* isolate, |
| 1754 | TARGET& target, // NOLINT |
| 1755 | bool do_drop) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1756 | Debug* debug = isolate->debug(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1757 | Zone zone(isolate); |
| 1758 | Vector<StackFrame*> frames = CreateStackMap(isolate, &zone); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1759 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1760 | |
| 1761 | int top_frame_index = -1; |
| 1762 | int frame_index = 0; |
| 1763 | for (; frame_index < frames.length(); frame_index++) { |
| 1764 | StackFrame* frame = frames[frame_index]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1765 | if (frame->id() == debug->break_frame_id()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1766 | top_frame_index = frame_index; |
| 1767 | break; |
| 1768 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1769 | if (target.MatchActivation( |
| 1770 | frame, LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1771 | // We are still above break_frame. It is not a target frame, |
| 1772 | // it is a problem. |
| 1773 | return "Debugger mark-up on stack is not found"; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | if (top_frame_index == -1) { |
| 1778 | // We haven't found break frame, but no function is blocking us anyway. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1779 | return target.GetNotFoundMessage(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | bool target_frame_found = false; |
| 1783 | int bottom_js_frame_index = top_frame_index; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1784 | bool non_droppable_frame_found = false; |
| 1785 | LiveEdit::FunctionPatchabilityStatus non_droppable_reason; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1786 | |
| 1787 | for (; frame_index < frames.length(); frame_index++) { |
| 1788 | StackFrame* frame = frames[frame_index]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1789 | if (frame->is_exit()) { |
| 1790 | non_droppable_frame_found = true; |
| 1791 | non_droppable_reason = LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1792 | break; |
| 1793 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1794 | if (frame->is_java_script() && |
| 1795 | JavaScriptFrame::cast(frame)->function()->shared()->is_generator()) { |
| 1796 | non_droppable_frame_found = true; |
| 1797 | non_droppable_reason = LiveEdit::FUNCTION_BLOCKED_UNDER_GENERATOR; |
| 1798 | break; |
| 1799 | } |
| 1800 | if (target.MatchActivation( |
| 1801 | frame, LiveEdit::FUNCTION_BLOCKED_ON_ACTIVE_STACK)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1802 | target_frame_found = true; |
| 1803 | bottom_js_frame_index = frame_index; |
| 1804 | } |
| 1805 | } |
| 1806 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1807 | if (non_droppable_frame_found) { |
| 1808 | // There is a C or generator frame on stack. We can't drop C frames, and we |
| 1809 | // can't restart generators. Check that there are no target frames below |
| 1810 | // them. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1811 | for (; frame_index < frames.length(); frame_index++) { |
| 1812 | StackFrame* frame = frames[frame_index]; |
| 1813 | if (frame->is_java_script()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1814 | if (target.MatchActivation(frame, non_droppable_reason)) { |
| 1815 | // Fail. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1816 | return NULL; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | if (!do_drop) { |
| 1823 | // We are in check-only mode. |
| 1824 | return NULL; |
| 1825 | } |
| 1826 | |
| 1827 | if (!target_frame_found) { |
| 1828 | // Nothing to drop. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1829 | return target.GetNotFoundMessage(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1830 | } |
| 1831 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1832 | LiveEdit::FrameDropMode drop_mode = LiveEdit::FRAMES_UNTOUCHED; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1833 | Object** restarter_frame_function_pointer = NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1834 | const char* error_message = DropFrames(frames, top_frame_index, |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1835 | bottom_js_frame_index, &drop_mode, |
| 1836 | &restarter_frame_function_pointer); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1837 | |
| 1838 | if (error_message != NULL) { |
| 1839 | return error_message; |
| 1840 | } |
| 1841 | |
| 1842 | // Adjust break_frame after some frames has been dropped. |
| 1843 | StackFrame::Id new_id = StackFrame::NO_ID; |
| 1844 | for (int i = bottom_js_frame_index + 1; i < frames.length(); i++) { |
| 1845 | if (frames[i]->type() == StackFrame::JAVA_SCRIPT) { |
| 1846 | new_id = frames[i]->id(); |
| 1847 | break; |
| 1848 | } |
| 1849 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1850 | debug->FramesHaveBeenDropped( |
| 1851 | new_id, drop_mode, restarter_frame_function_pointer); |
| 1852 | return NULL; |
| 1853 | } |
| 1854 | |
| 1855 | |
| 1856 | // Fills result array with statuses of functions. Modifies the stack |
| 1857 | // removing all listed function if possible and if do_drop is true. |
| 1858 | static const char* DropActivationsInActiveThread( |
| 1859 | Handle<JSArray> shared_info_array, Handle<JSArray> result, bool do_drop) { |
| 1860 | MultipleFunctionTarget target(shared_info_array, result); |
| 1861 | |
| 1862 | const char* message = DropActivationsInActiveThreadImpl( |
| 1863 | shared_info_array->GetIsolate(), target, do_drop); |
| 1864 | if (message) { |
| 1865 | return message; |
| 1866 | } |
| 1867 | |
| 1868 | Isolate* isolate = shared_info_array->GetIsolate(); |
| 1869 | int array_len = GetArrayLength(shared_info_array); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1870 | |
| 1871 | // Replace "blocked on active" with "replaced on active" status. |
| 1872 | for (int i = 0; i < array_len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1873 | Handle<Object> obj = |
| 1874 | Object::GetElement(isolate, result, i).ToHandleChecked(); |
| 1875 | if (*obj == Smi::FromInt(LiveEdit::FUNCTION_BLOCKED_ON_ACTIVE_STACK)) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1876 | Handle<Object> replaced( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1877 | Smi::FromInt(LiveEdit::FUNCTION_REPLACED_ON_ACTIVE_STACK), isolate); |
| 1878 | SetElementSloppy(result, i, replaced); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1879 | } |
| 1880 | } |
| 1881 | return NULL; |
| 1882 | } |
| 1883 | |
| 1884 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1885 | bool LiveEdit::FindActiveGenerators(Handle<FixedArray> shared_info_array, |
| 1886 | Handle<FixedArray> result, |
| 1887 | int len) { |
| 1888 | Isolate* isolate = shared_info_array->GetIsolate(); |
| 1889 | bool found_suspended_activations = false; |
| 1890 | |
| 1891 | DCHECK_LE(len, result->length()); |
| 1892 | |
| 1893 | FunctionPatchabilityStatus active = FUNCTION_BLOCKED_ACTIVE_GENERATOR; |
| 1894 | |
| 1895 | Heap* heap = isolate->heap(); |
| 1896 | HeapIterator iterator(heap); |
| 1897 | HeapObject* obj = NULL; |
| 1898 | while ((obj = iterator.next()) != NULL) { |
| 1899 | if (!obj->IsJSGeneratorObject()) continue; |
| 1900 | |
| 1901 | JSGeneratorObject* gen = JSGeneratorObject::cast(obj); |
| 1902 | if (gen->is_closed()) continue; |
| 1903 | |
| 1904 | HandleScope scope(isolate); |
| 1905 | |
| 1906 | for (int i = 0; i < len; i++) { |
| 1907 | Handle<JSValue> jsvalue = |
| 1908 | Handle<JSValue>::cast(FixedArray::get(shared_info_array, i)); |
| 1909 | Handle<SharedFunctionInfo> shared = |
| 1910 | UnwrapSharedFunctionInfoFromJSValue(jsvalue); |
| 1911 | |
| 1912 | if (gen->function()->shared() == *shared) { |
| 1913 | result->set(i, Smi::FromInt(active)); |
| 1914 | found_suspended_activations = true; |
| 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | return found_suspended_activations; |
| 1920 | } |
| 1921 | |
| 1922 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1923 | class InactiveThreadActivationsChecker : public ThreadVisitor { |
| 1924 | public: |
| 1925 | InactiveThreadActivationsChecker(Handle<JSArray> shared_info_array, |
| 1926 | Handle<JSArray> result) |
| 1927 | : shared_info_array_(shared_info_array), result_(result), |
| 1928 | has_blocked_functions_(false) { |
| 1929 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1930 | void VisitThread(Isolate* isolate, ThreadLocalTop* top) { |
| 1931 | for (StackFrameIterator it(isolate, top); !it.done(); it.Advance()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1932 | has_blocked_functions_ |= CheckActivation( |
| 1933 | shared_info_array_, result_, it.frame(), |
| 1934 | LiveEdit::FUNCTION_BLOCKED_ON_OTHER_STACK); |
| 1935 | } |
| 1936 | } |
| 1937 | bool HasBlockedFunctions() { |
| 1938 | return has_blocked_functions_; |
| 1939 | } |
| 1940 | |
| 1941 | private: |
| 1942 | Handle<JSArray> shared_info_array_; |
| 1943 | Handle<JSArray> result_; |
| 1944 | bool has_blocked_functions_; |
| 1945 | }; |
| 1946 | |
| 1947 | |
| 1948 | Handle<JSArray> LiveEdit::CheckAndDropActivations( |
| 1949 | Handle<JSArray> shared_info_array, bool do_drop) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1950 | Isolate* isolate = shared_info_array->GetIsolate(); |
| 1951 | int len = GetArrayLength(shared_info_array); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1952 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1953 | DCHECK(shared_info_array->HasFastElements()); |
| 1954 | Handle<FixedArray> shared_info_array_elements( |
| 1955 | FixedArray::cast(shared_info_array->elements())); |
| 1956 | |
| 1957 | Handle<JSArray> result = isolate->factory()->NewJSArray(len); |
| 1958 | Handle<FixedArray> result_elements = |
| 1959 | JSObject::EnsureWritableFastElements(result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1960 | |
| 1961 | // Fill the default values. |
| 1962 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1963 | FunctionPatchabilityStatus status = FUNCTION_AVAILABLE_FOR_PATCH; |
| 1964 | result_elements->set(i, Smi::FromInt(status)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1965 | } |
| 1966 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1967 | // Scan the heap for active generators -- those that are either currently |
| 1968 | // running (as we wouldn't want to restart them, because we don't know where |
| 1969 | // to restart them from) or suspended. Fail if any one corresponds to the set |
| 1970 | // of functions being edited. |
| 1971 | if (FindActiveGenerators(shared_info_array_elements, result_elements, len)) { |
| 1972 | return result; |
| 1973 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1974 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1975 | // Check inactive threads. Fail if some functions are blocked there. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1976 | InactiveThreadActivationsChecker inactive_threads_checker(shared_info_array, |
| 1977 | result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1978 | isolate->thread_manager()->IterateArchivedThreads( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1979 | &inactive_threads_checker); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1980 | if (inactive_threads_checker.HasBlockedFunctions()) { |
| 1981 | return result; |
| 1982 | } |
| 1983 | |
| 1984 | // Try to drop activations from the current stack. |
| 1985 | const char* error_message = |
| 1986 | DropActivationsInActiveThread(shared_info_array, result, do_drop); |
| 1987 | if (error_message != NULL) { |
| 1988 | // Add error message as an array extra element. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1989 | Handle<String> str = |
| 1990 | isolate->factory()->NewStringFromAsciiChecked(error_message); |
| 1991 | SetElementSloppy(result, len, str); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1992 | } |
| 1993 | return result; |
| 1994 | } |
| 1995 | |
| 1996 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1997 | // Describes a single callframe a target. Not finding this frame |
| 1998 | // means an error. |
| 1999 | class SingleFrameTarget { |
| 2000 | public: |
| 2001 | explicit SingleFrameTarget(JavaScriptFrame* frame) |
| 2002 | : m_frame(frame), |
| 2003 | m_saved_status(LiveEdit::FUNCTION_AVAILABLE_FOR_PATCH) {} |
| 2004 | |
| 2005 | bool MatchActivation(StackFrame* frame, |
| 2006 | LiveEdit::FunctionPatchabilityStatus status) { |
| 2007 | if (frame->fp() == m_frame->fp()) { |
| 2008 | m_saved_status = status; |
| 2009 | return true; |
| 2010 | } |
| 2011 | return false; |
| 2012 | } |
| 2013 | const char* GetNotFoundMessage() const { |
| 2014 | return "Failed to found requested frame"; |
| 2015 | } |
| 2016 | LiveEdit::FunctionPatchabilityStatus saved_status() { |
| 2017 | return m_saved_status; |
| 2018 | } |
| 2019 | private: |
| 2020 | JavaScriptFrame* m_frame; |
| 2021 | LiveEdit::FunctionPatchabilityStatus m_saved_status; |
| 2022 | }; |
| 2023 | |
| 2024 | |
| 2025 | // Finds a drops required frame and all frames above. |
| 2026 | // Returns error message or NULL. |
| 2027 | const char* LiveEdit::RestartFrame(JavaScriptFrame* frame) { |
| 2028 | SingleFrameTarget target(frame); |
| 2029 | |
| 2030 | const char* result = DropActivationsInActiveThreadImpl( |
| 2031 | frame->isolate(), target, true); |
| 2032 | if (result != NULL) { |
| 2033 | return result; |
| 2034 | } |
| 2035 | if (target.saved_status() == LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE) { |
| 2036 | return "Function is blocked under native code"; |
| 2037 | } |
| 2038 | if (target.saved_status() == LiveEdit::FUNCTION_BLOCKED_UNDER_GENERATOR) { |
| 2039 | return "Function is blocked under a generator activation"; |
| 2040 | } |
| 2041 | return NULL; |
| 2042 | } |
| 2043 | |
| 2044 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2045 | LiveEditFunctionTracker::LiveEditFunctionTracker(Isolate* isolate, |
| 2046 | FunctionLiteral* fun) |
| 2047 | : isolate_(isolate) { |
| 2048 | if (isolate_->active_function_info_listener() != NULL) { |
| 2049 | isolate_->active_function_info_listener()->FunctionStarted(fun); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2050 | } |
| 2051 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2052 | |
| 2053 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2054 | LiveEditFunctionTracker::~LiveEditFunctionTracker() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2055 | if (isolate_->active_function_info_listener() != NULL) { |
| 2056 | isolate_->active_function_info_listener()->FunctionDone(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2057 | } |
| 2058 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2059 | |
| 2060 | |
| 2061 | void LiveEditFunctionTracker::RecordFunctionInfo( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2062 | Handle<SharedFunctionInfo> info, FunctionLiteral* lit, |
| 2063 | Zone* zone) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2064 | if (isolate_->active_function_info_listener() != NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2065 | isolate_->active_function_info_listener()->FunctionInfo(info, lit->scope(), |
| 2066 | zone); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2067 | } |
| 2068 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2069 | |
| 2070 | |
| 2071 | void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2072 | isolate_->active_function_info_listener()->FunctionCode(code); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2073 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2074 | |
| 2075 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2076 | bool LiveEditFunctionTracker::IsActive(Isolate* isolate) { |
| 2077 | return isolate->active_function_info_listener() != NULL; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2080 | } } // namespace v8::internal |