blob: 0b01e8af155b36e0738907b242cc304e7955c19b [file] [log] [blame]
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ager@chromium.org5c838252010-02-19 08:53:10 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29#include "v8.h"
30
31#include "liveedit.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000032
kasperl@chromium.orga5551262010-12-07 12:49:48 +000033#include "compilation-cache.h"
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000034#include "compiler.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000035#include "debug.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000036#include "deoptimizer.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000037#include "global-handles.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000038#include "parser.h"
39#include "scopeinfo.h"
40#include "scopes.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000041#include "v8memory.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000042
43namespace v8 {
44namespace internal {
45
46
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000047#ifdef ENABLE_DEBUGGER_SUPPORT
48
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000049
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +000050void SetElementNonStrict(Handle<JSObject> object,
51 uint32_t index,
52 Handle<Object> value) {
53 // Ignore return value from SetElement. It can only be a failure if there
54 // are element setters causing exceptions and the debugger context has none
55 // of these.
56 Handle<Object> no_failure;
57 no_failure = SetElement(object, index, value, kNonStrictMode);
58 ASSERT(!no_failure.is_null());
59 USE(no_failure);
60}
61
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000062// A simple implementation of dynamic programming algorithm. It solves
63// the problem of finding the difference of 2 arrays. It uses a table of results
64// of subproblems. Each cell contains a number together with 2-bit flag
65// that helps building the chunk list.
66class Differencer {
67 public:
lrn@chromium.orgc34f5802010-04-28 12:53:43 +000068 explicit Differencer(Comparator::Input* input)
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000069 : input_(input), len1_(input->GetLength1()), len2_(input->GetLength2()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000070 buffer_ = NewArray<int>(len1_ * len2_);
71 }
72 ~Differencer() {
73 DeleteArray(buffer_);
74 }
75
76 void Initialize() {
77 int array_size = len1_ * len2_;
78 for (int i = 0; i < array_size; i++) {
79 buffer_[i] = kEmptyCellValue;
80 }
81 }
82
83 // Makes sure that result for the full problem is calculated and stored
84 // in the table together with flags showing a path through subproblems.
85 void FillTable() {
86 CompareUpToTail(0, 0);
87 }
88
lrn@chromium.orgc34f5802010-04-28 12:53:43 +000089 void SaveResult(Comparator::Output* chunk_writer) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000090 ResultWriter writer(chunk_writer);
91
92 int pos1 = 0;
93 int pos2 = 0;
94 while (true) {
95 if (pos1 < len1_) {
96 if (pos2 < len2_) {
97 Direction dir = get_direction(pos1, pos2);
98 switch (dir) {
99 case EQ:
100 writer.eq();
101 pos1++;
102 pos2++;
103 break;
104 case SKIP1:
105 writer.skip1(1);
106 pos1++;
107 break;
108 case SKIP2:
109 case SKIP_ANY:
110 writer.skip2(1);
111 pos2++;
112 break;
113 default:
114 UNREACHABLE();
115 }
116 } else {
117 writer.skip1(len1_ - pos1);
118 break;
119 }
120 } else {
121 if (len2_ != pos2) {
122 writer.skip2(len2_ - pos2);
123 }
124 break;
125 }
126 }
127 writer.close();
128 }
129
130 private:
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000131 Comparator::Input* input_;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000132 int* buffer_;
133 int len1_;
134 int len2_;
135
136 enum Direction {
137 EQ = 0,
138 SKIP1,
139 SKIP2,
140 SKIP_ANY,
141
142 MAX_DIRECTION_FLAG_VALUE = SKIP_ANY
143 };
144
145 // Computes result for a subtask and optionally caches it in the buffer table.
146 // All results values are shifted to make space for flags in the lower bits.
147 int CompareUpToTail(int pos1, int pos2) {
148 if (pos1 < len1_) {
149 if (pos2 < len2_) {
150 int cached_res = get_value4(pos1, pos2);
151 if (cached_res == kEmptyCellValue) {
152 Direction dir;
153 int res;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000154 if (input_->Equals(pos1, pos2)) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000155 res = CompareUpToTail(pos1 + 1, pos2 + 1);
156 dir = EQ;
157 } else {
158 int res1 = CompareUpToTail(pos1 + 1, pos2) +
159 (1 << kDirectionSizeBits);
160 int res2 = CompareUpToTail(pos1, pos2 + 1) +
161 (1 << kDirectionSizeBits);
162 if (res1 == res2) {
163 res = res1;
164 dir = SKIP_ANY;
165 } else if (res1 < res2) {
166 res = res1;
167 dir = SKIP1;
168 } else {
169 res = res2;
170 dir = SKIP2;
171 }
172 }
173 set_value4_and_dir(pos1, pos2, res, dir);
174 cached_res = res;
175 }
176 return cached_res;
177 } else {
178 return (len1_ - pos1) << kDirectionSizeBits;
179 }
180 } else {
181 return (len2_ - pos2) << kDirectionSizeBits;
182 }
183 }
184
185 inline int& get_cell(int i1, int i2) {
186 return buffer_[i1 + i2 * len1_];
187 }
188
189 // Each cell keeps a value plus direction. Value is multiplied by 4.
190 void set_value4_and_dir(int i1, int i2, int value4, Direction dir) {
191 ASSERT((value4 & kDirectionMask) == 0);
192 get_cell(i1, i2) = value4 | dir;
193 }
194
195 int get_value4(int i1, int i2) {
196 return get_cell(i1, i2) & (kMaxUInt32 ^ kDirectionMask);
197 }
198 Direction get_direction(int i1, int i2) {
199 return static_cast<Direction>(get_cell(i1, i2) & kDirectionMask);
200 }
201
202 static const int kDirectionSizeBits = 2;
203 static const int kDirectionMask = (1 << kDirectionSizeBits) - 1;
204 static const int kEmptyCellValue = -1 << kDirectionSizeBits;
205
206 // This method only holds static assert statement (unfortunately you cannot
207 // place one in class scope).
208 void StaticAssertHolder() {
209 STATIC_ASSERT(MAX_DIRECTION_FLAG_VALUE < (1 << kDirectionSizeBits));
210 }
211
212 class ResultWriter {
213 public:
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000214 explicit ResultWriter(Comparator::Output* chunk_writer)
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000215 : chunk_writer_(chunk_writer), pos1_(0), pos2_(0),
216 pos1_begin_(-1), pos2_begin_(-1), has_open_chunk_(false) {
217 }
218 void eq() {
219 FlushChunk();
220 pos1_++;
221 pos2_++;
222 }
223 void skip1(int len1) {
224 StartChunk();
225 pos1_ += len1;
226 }
227 void skip2(int len2) {
228 StartChunk();
229 pos2_ += len2;
230 }
231 void close() {
232 FlushChunk();
233 }
234
235 private:
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000236 Comparator::Output* chunk_writer_;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000237 int pos1_;
238 int pos2_;
239 int pos1_begin_;
240 int pos2_begin_;
241 bool has_open_chunk_;
242
243 void StartChunk() {
244 if (!has_open_chunk_) {
245 pos1_begin_ = pos1_;
246 pos2_begin_ = pos2_;
247 has_open_chunk_ = true;
248 }
249 }
250
251 void FlushChunk() {
252 if (has_open_chunk_) {
253 chunk_writer_->AddChunk(pos1_begin_, pos2_begin_,
254 pos1_ - pos1_begin_, pos2_ - pos2_begin_);
255 has_open_chunk_ = false;
256 }
257 }
258 };
259};
260
261
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000262void Comparator::CalculateDifference(Comparator::Input* input,
263 Comparator::Output* result_writer) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000264 Differencer differencer(input);
265 differencer.Initialize();
266 differencer.FillTable();
267 differencer.SaveResult(result_writer);
268}
269
270
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000271static bool CompareSubstrings(Handle<String> s1, int pos1,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000272 Handle<String> s2, int pos2, int len) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000273 for (int i = 0; i < len; i++) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000274 if (s1->Get(i + pos1) != s2->Get(i + pos2)) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000275 return false;
276 }
277 }
278 return true;
279}
280
281
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000282// Additional to Input interface. Lets switch Input range to subrange.
283// More elegant way would be to wrap one Input as another Input object
284// and translate positions there, but that would cost us additional virtual
285// call per comparison.
286class SubrangableInput : public Comparator::Input {
287 public:
288 virtual void SetSubrange1(int offset, int len) = 0;
289 virtual void SetSubrange2(int offset, int len) = 0;
290};
291
292
293class SubrangableOutput : public Comparator::Output {
294 public:
295 virtual void SetSubrange1(int offset, int len) = 0;
296 virtual void SetSubrange2(int offset, int len) = 0;
297};
298
299
300static int min(int a, int b) {
301 return a < b ? a : b;
302}
303
304
305// Finds common prefix and suffix in input. This parts shouldn't take space in
306// linear programming table. Enable subranging in input and output.
307static void NarrowDownInput(SubrangableInput* input,
308 SubrangableOutput* output) {
309 const int len1 = input->GetLength1();
310 const int len2 = input->GetLength2();
311
312 int common_prefix_len;
313 int common_suffix_len;
314
315 {
316 common_prefix_len = 0;
317 int prefix_limit = min(len1, len2);
318 while (common_prefix_len < prefix_limit &&
319 input->Equals(common_prefix_len, common_prefix_len)) {
320 common_prefix_len++;
321 }
322
323 common_suffix_len = 0;
324 int suffix_limit = min(len1 - common_prefix_len, len2 - common_prefix_len);
325
326 while (common_suffix_len < suffix_limit &&
327 input->Equals(len1 - common_suffix_len - 1,
328 len2 - common_suffix_len - 1)) {
329 common_suffix_len++;
330 }
331 }
332
333 if (common_prefix_len > 0 || common_suffix_len > 0) {
334 int new_len1 = len1 - common_suffix_len - common_prefix_len;
335 int new_len2 = len2 - common_suffix_len - common_prefix_len;
336
337 input->SetSubrange1(common_prefix_len, new_len1);
338 input->SetSubrange2(common_prefix_len, new_len2);
339
340 output->SetSubrange1(common_prefix_len, new_len1);
341 output->SetSubrange2(common_prefix_len, new_len2);
342 }
343}
344
345
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000346// A helper class that writes chunk numbers into JSArray.
347// Each chunk is stored as 3 array elements: (pos1_begin, pos1_end, pos2_end).
348class CompareOutputArrayWriter {
349 public:
350 CompareOutputArrayWriter()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000351 : array_(FACTORY->NewJSArray(10)), current_size_(0) {}
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000352
353 Handle<JSArray> GetResult() {
354 return array_;
355 }
356
357 void WriteChunk(int char_pos1, int char_pos2, int char_len1, int char_len2) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000358 SetElementNonStrict(array_,
359 current_size_,
360 Handle<Object>(Smi::FromInt(char_pos1)));
361 SetElementNonStrict(array_,
362 current_size_ + 1,
363 Handle<Object>(Smi::FromInt(char_pos1 + char_len1)));
364 SetElementNonStrict(array_,
365 current_size_ + 2,
366 Handle<Object>(Smi::FromInt(char_pos2 + char_len2)));
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000367 current_size_ += 3;
368 }
369
370 private:
371 Handle<JSArray> array_;
372 int current_size_;
373};
374
375
376// Represents 2 strings as 2 arrays of tokens.
377// TODO(LiveEdit): Currently it's actually an array of charactres.
378// Make array of tokens instead.
379class TokensCompareInput : public Comparator::Input {
380 public:
381 TokensCompareInput(Handle<String> s1, int offset1, int len1,
382 Handle<String> s2, int offset2, int len2)
383 : s1_(s1), offset1_(offset1), len1_(len1),
384 s2_(s2), offset2_(offset2), len2_(len2) {
385 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000386 virtual int GetLength1() {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000387 return len1_;
388 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000389 virtual int GetLength2() {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000390 return len2_;
391 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000392 bool Equals(int index1, int index2) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000393 return s1_->Get(offset1_ + index1) == s2_->Get(offset2_ + index2);
394 }
395
396 private:
397 Handle<String> s1_;
398 int offset1_;
399 int len1_;
400 Handle<String> s2_;
401 int offset2_;
402 int len2_;
403};
404
405
406// Stores compare result in JSArray. Converts substring positions
407// to absolute positions.
408class TokensCompareOutput : public Comparator::Output {
409 public:
410 TokensCompareOutput(CompareOutputArrayWriter* array_writer,
411 int offset1, int offset2)
412 : array_writer_(array_writer), offset1_(offset1), offset2_(offset2) {
413 }
414
415 void AddChunk(int pos1, int pos2, int len1, int len2) {
416 array_writer_->WriteChunk(pos1 + offset1_, pos2 + offset2_, len1, len2);
417 }
418
419 private:
420 CompareOutputArrayWriter* array_writer_;
421 int offset1_;
422 int offset2_;
423};
424
425
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000426// Wraps raw n-elements line_ends array as a list of n+1 lines. The last line
427// never has terminating new line character.
428class LineEndsWrapper {
429 public:
430 explicit LineEndsWrapper(Handle<String> string)
431 : ends_array_(CalculateLineEnds(string, false)),
432 string_len_(string->length()) {
433 }
434 int length() {
435 return ends_array_->length() + 1;
436 }
437 // Returns start for any line including start of the imaginary line after
438 // the last line.
439 int GetLineStart(int index) {
440 if (index == 0) {
441 return 0;
442 } else {
443 return GetLineEnd(index - 1);
444 }
445 }
446 int GetLineEnd(int index) {
447 if (index == ends_array_->length()) {
448 // End of the last line is always an end of the whole string.
449 // If the string ends with a new line character, the last line is an
450 // empty string after this character.
451 return string_len_;
452 } else {
453 return GetPosAfterNewLine(index);
454 }
455 }
456
457 private:
458 Handle<FixedArray> ends_array_;
459 int string_len_;
460
461 int GetPosAfterNewLine(int index) {
462 return Smi::cast(ends_array_->get(index))->value() + 1;
463 }
464};
465
466
467// Represents 2 strings as 2 arrays of lines.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000468class LineArrayCompareInput : public SubrangableInput {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000469 public:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000470 LineArrayCompareInput(Handle<String> s1, Handle<String> s2,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000471 LineEndsWrapper line_ends1, LineEndsWrapper line_ends2)
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000472 : s1_(s1), s2_(s2), line_ends1_(line_ends1),
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000473 line_ends2_(line_ends2),
474 subrange_offset1_(0), subrange_offset2_(0),
475 subrange_len1_(line_ends1_.length()),
476 subrange_len2_(line_ends2_.length()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000477 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000478 int GetLength1() {
479 return subrange_len1_;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000480 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000481 int GetLength2() {
482 return subrange_len2_;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000483 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000484 bool Equals(int index1, int index2) {
485 index1 += subrange_offset1_;
486 index2 += subrange_offset2_;
487
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000488 int line_start1 = line_ends1_.GetLineStart(index1);
489 int line_start2 = line_ends2_.GetLineStart(index2);
490 int line_end1 = line_ends1_.GetLineEnd(index1);
491 int line_end2 = line_ends2_.GetLineEnd(index2);
492 int len1 = line_end1 - line_start1;
493 int len2 = line_end2 - line_start2;
494 if (len1 != len2) {
495 return false;
496 }
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000497 return CompareSubstrings(s1_, line_start1, s2_, line_start2,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000498 len1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000499 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000500 void SetSubrange1(int offset, int len) {
501 subrange_offset1_ = offset;
502 subrange_len1_ = len;
503 }
504 void SetSubrange2(int offset, int len) {
505 subrange_offset2_ = offset;
506 subrange_len2_ = len;
507 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000508
509 private:
510 Handle<String> s1_;
511 Handle<String> s2_;
512 LineEndsWrapper line_ends1_;
513 LineEndsWrapper line_ends2_;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000514 int subrange_offset1_;
515 int subrange_offset2_;
516 int subrange_len1_;
517 int subrange_len2_;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000518};
519
520
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000521// Stores compare result in JSArray. For each chunk tries to conduct
522// a fine-grained nested diff token-wise.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000523class TokenizingLineArrayCompareOutput : public SubrangableOutput {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000524 public:
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000525 TokenizingLineArrayCompareOutput(LineEndsWrapper line_ends1,
526 LineEndsWrapper line_ends2,
527 Handle<String> s1, Handle<String> s2)
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000528 : line_ends1_(line_ends1), line_ends2_(line_ends2), s1_(s1), s2_(s2),
529 subrange_offset1_(0), subrange_offset2_(0) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000530 }
531
532 void AddChunk(int line_pos1, int line_pos2, int line_len1, int line_len2) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000533 line_pos1 += subrange_offset1_;
534 line_pos2 += subrange_offset2_;
535
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000536 int char_pos1 = line_ends1_.GetLineStart(line_pos1);
537 int char_pos2 = line_ends2_.GetLineStart(line_pos2);
538 int char_len1 = line_ends1_.GetLineStart(line_pos1 + line_len1) - char_pos1;
539 int char_len2 = line_ends2_.GetLineStart(line_pos2 + line_len2) - char_pos2;
540
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000541 if (char_len1 < CHUNK_LEN_LIMIT && char_len2 < CHUNK_LEN_LIMIT) {
542 // Chunk is small enough to conduct a nested token-level diff.
543 HandleScope subTaskScope;
544
545 TokensCompareInput tokens_input(s1_, char_pos1, char_len1,
546 s2_, char_pos2, char_len2);
547 TokensCompareOutput tokens_output(&array_writer_, char_pos1,
548 char_pos2);
549
550 Comparator::CalculateDifference(&tokens_input, &tokens_output);
551 } else {
552 array_writer_.WriteChunk(char_pos1, char_pos2, char_len1, char_len2);
553 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000554 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000555 void SetSubrange1(int offset, int len) {
556 subrange_offset1_ = offset;
557 }
558 void SetSubrange2(int offset, int len) {
559 subrange_offset2_ = offset;
560 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000561
562 Handle<JSArray> GetResult() {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000563 return array_writer_.GetResult();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000564 }
565
566 private:
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000567 static const int CHUNK_LEN_LIMIT = 800;
568
569 CompareOutputArrayWriter array_writer_;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000570 LineEndsWrapper line_ends1_;
571 LineEndsWrapper line_ends2_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000572 Handle<String> s1_;
573 Handle<String> s2_;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000574 int subrange_offset1_;
575 int subrange_offset2_;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000576};
577
578
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000579Handle<JSArray> LiveEdit::CompareStrings(Handle<String> s1,
580 Handle<String> s2) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000581 s1 = FlattenGetString(s1);
582 s2 = FlattenGetString(s2);
583
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000584 LineEndsWrapper line_ends1(s1);
585 LineEndsWrapper line_ends2(s2);
586
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000587 LineArrayCompareInput input(s1, s2, line_ends1, line_ends2);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000588 TokenizingLineArrayCompareOutput output(line_ends1, line_ends2, s1, s2);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000589
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000590 NarrowDownInput(&input, &output);
591
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000592 Comparator::CalculateDifference(&input, &output);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000593
594 return output.GetResult();
595}
596
597
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000598static void CompileScriptForTracker(Isolate* isolate, Handle<Script> script) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000599 // TODO(635): support extensions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000600 PostponeInterruptsScope postpone(isolate);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000601
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000602 // Build AST.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000603 CompilationInfo info(script);
604 info.MarkAsGlobal();
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000605 if (ParserApi::Parse(&info)) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000606 // Compile the code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000607 LiveEditFunctionTracker tracker(info.isolate(), info.function());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000608 if (Compiler::MakeCodeForLiveEdit(&info)) {
609 ASSERT(!info.code().is_null());
610 tracker.RecordRootFunctionInfo(info.code());
611 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000612 info.isolate()->StackOverflow();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000613 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000614 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000615}
616
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000617
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000618// Unwraps JSValue object, returning its field "value"
619static Handle<Object> UnwrapJSValue(Handle<JSValue> jsValue) {
620 return Handle<Object>(jsValue->value());
621}
622
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000623
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000624// Wraps any object into a OpaqueReference, that will hide the object
625// from JavaScript.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000626static Handle<JSValue> WrapInJSValue(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000627 Handle<JSFunction> constructor =
628 Isolate::Current()->opaque_reference_function();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000629 Handle<JSValue> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000630 Handle<JSValue>::cast(FACTORY->NewJSObject(constructor));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000631 result->set_value(*object);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000632 return result;
633}
634
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000635
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000636// Simple helper class that creates more or less typed structures over
637// JSArray object. This is an adhoc method of passing structures from C++
638// to JavaScript.
639template<typename S>
640class JSArrayBasedStruct {
641 public:
642 static S Create() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000643 Handle<JSArray> array = FACTORY->NewJSArray(S::kSize_);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000644 return S(array);
645 }
646 static S cast(Object* object) {
647 JSArray* array = JSArray::cast(object);
648 Handle<JSArray> array_handle(array);
649 return S(array_handle);
650 }
651 explicit JSArrayBasedStruct(Handle<JSArray> array) : array_(array) {
652 }
653 Handle<JSArray> GetJSArray() {
654 return array_;
655 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000656
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000657 protected:
658 void SetField(int field_position, Handle<Object> value) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000659 SetElementNonStrict(array_, field_position, value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000660 }
661 void SetSmiValueField(int field_position, int value) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000662 SetElementNonStrict(array_,
663 field_position,
664 Handle<Smi>(Smi::FromInt(value)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000665 }
666 Object* GetField(int field_position) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000667 return array_->GetElementNoExceptionThrown(field_position);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000668 }
669 int GetSmiValueField(int field_position) {
670 Object* res = GetField(field_position);
671 return Smi::cast(res)->value();
672 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000673
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000674 private:
675 Handle<JSArray> array_;
676};
677
678
679// Represents some function compilation details. This structure will be used
680// from JavaScript. It contains Code object, which is kept wrapped
681// into a BlindReference for sanitizing reasons.
682class FunctionInfoWrapper : public JSArrayBasedStruct<FunctionInfoWrapper> {
683 public:
684 explicit FunctionInfoWrapper(Handle<JSArray> array)
685 : JSArrayBasedStruct<FunctionInfoWrapper>(array) {
686 }
687 void SetInitialProperties(Handle<String> name, int start_position,
688 int end_position, int param_num, int parent_index) {
689 HandleScope scope;
690 this->SetField(kFunctionNameOffset_, name);
691 this->SetSmiValueField(kStartPositionOffset_, start_position);
692 this->SetSmiValueField(kEndPositionOffset_, end_position);
693 this->SetSmiValueField(kParamNumOffset_, param_num);
694 this->SetSmiValueField(kParentIndexOffset_, parent_index);
695 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000696 void SetFunctionCode(Handle<Code> function_code,
697 Handle<Object> code_scope_info) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000698 Handle<JSValue> code_wrapper = WrapInJSValue(function_code);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000699 this->SetField(kCodeOffset_, code_wrapper);
700
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000701 Handle<JSValue> scope_wrapper = WrapInJSValue(code_scope_info);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000702 this->SetField(kCodeScopeInfoOffset_, scope_wrapper);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000703 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000704 void SetOuterScopeInfo(Handle<Object> scope_info_array) {
705 this->SetField(kOuterScopeInfoOffset_, scope_info_array);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000706 }
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000707 void SetSharedFunctionInfo(Handle<SharedFunctionInfo> info) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000708 Handle<JSValue> info_holder = WrapInJSValue(info);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000709 this->SetField(kSharedFunctionInfoOffset_, info_holder);
710 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000711 int GetParentIndex() {
712 return this->GetSmiValueField(kParentIndexOffset_);
713 }
714 Handle<Code> GetFunctionCode() {
715 Handle<Object> raw_result = UnwrapJSValue(Handle<JSValue>(
716 JSValue::cast(this->GetField(kCodeOffset_))));
717 return Handle<Code>::cast(raw_result);
718 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000719 Handle<Object> GetCodeScopeInfo() {
720 Handle<Object> raw_result = UnwrapJSValue(Handle<JSValue>(
721 JSValue::cast(this->GetField(kCodeScopeInfoOffset_))));
722 return raw_result;
723 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000724 int GetStartPosition() {
725 return this->GetSmiValueField(kStartPositionOffset_);
726 }
727 int GetEndPosition() {
728 return this->GetSmiValueField(kEndPositionOffset_);
729 }
730
731 private:
732 static const int kFunctionNameOffset_ = 0;
733 static const int kStartPositionOffset_ = 1;
734 static const int kEndPositionOffset_ = 2;
735 static const int kParamNumOffset_ = 3;
736 static const int kCodeOffset_ = 4;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000737 static const int kCodeScopeInfoOffset_ = 5;
738 static const int kOuterScopeInfoOffset_ = 6;
739 static const int kParentIndexOffset_ = 7;
740 static const int kSharedFunctionInfoOffset_ = 8;
741 static const int kSize_ = 9;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000742
743 friend class JSArrayBasedStruct<FunctionInfoWrapper>;
744};
745
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000746
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000747// Wraps SharedFunctionInfo along with some of its fields for passing it
748// back to JavaScript. SharedFunctionInfo object itself is additionally
749// wrapped into BlindReference for sanitizing reasons.
750class SharedInfoWrapper : public JSArrayBasedStruct<SharedInfoWrapper> {
751 public:
ager@chromium.orgac091b72010-05-05 07:34:42 +0000752 static bool IsInstance(Handle<JSArray> array) {
753 return array->length() == Smi::FromInt(kSize_) &&
lrn@chromium.org303ada72010-10-27 09:33:13 +0000754 array->GetElementNoExceptionThrown(kSharedInfoOffset_)->IsJSValue();
ager@chromium.orgac091b72010-05-05 07:34:42 +0000755 }
756
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000757 explicit SharedInfoWrapper(Handle<JSArray> array)
758 : JSArrayBasedStruct<SharedInfoWrapper>(array) {
759 }
760
761 void SetProperties(Handle<String> name, int start_position, int end_position,
762 Handle<SharedFunctionInfo> info) {
763 HandleScope scope;
764 this->SetField(kFunctionNameOffset_, name);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000765 Handle<JSValue> info_holder = WrapInJSValue(info);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000766 this->SetField(kSharedInfoOffset_, info_holder);
767 this->SetSmiValueField(kStartPositionOffset_, start_position);
768 this->SetSmiValueField(kEndPositionOffset_, end_position);
769 }
770 Handle<SharedFunctionInfo> GetInfo() {
771 Object* element = this->GetField(kSharedInfoOffset_);
772 Handle<JSValue> value_wrapper(JSValue::cast(element));
773 Handle<Object> raw_result = UnwrapJSValue(value_wrapper);
774 return Handle<SharedFunctionInfo>::cast(raw_result);
775 }
776
777 private:
778 static const int kFunctionNameOffset_ = 0;
779 static const int kStartPositionOffset_ = 1;
780 static const int kEndPositionOffset_ = 2;
781 static const int kSharedInfoOffset_ = 3;
782 static const int kSize_ = 4;
783
784 friend class JSArrayBasedStruct<SharedInfoWrapper>;
785};
786
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000787
ager@chromium.org5c838252010-02-19 08:53:10 +0000788class FunctionInfoListener {
789 public:
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000790 FunctionInfoListener() {
791 current_parent_index_ = -1;
792 len_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000793 result_ = FACTORY->NewJSArray(10);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000794 }
795
ager@chromium.org5c838252010-02-19 08:53:10 +0000796 void FunctionStarted(FunctionLiteral* fun) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000797 HandleScope scope;
798 FunctionInfoWrapper info = FunctionInfoWrapper::Create();
799 info.SetInitialProperties(fun->name(), fun->start_position(),
800 fun->end_position(), fun->num_parameters(),
801 current_parent_index_);
802 current_parent_index_ = len_;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000803 SetElementNonStrict(result_, len_, info.GetJSArray());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000804 len_++;
ager@chromium.org5c838252010-02-19 08:53:10 +0000805 }
806
807 void FunctionDone() {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000808 HandleScope scope;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000809 FunctionInfoWrapper info =
810 FunctionInfoWrapper::cast(
811 result_->GetElementNoExceptionThrown(current_parent_index_));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000812 current_parent_index_ = info.GetParentIndex();
ager@chromium.org5c838252010-02-19 08:53:10 +0000813 }
814
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000815 // Saves only function code, because for a script function we
816 // may never create a SharedFunctionInfo object.
817 void FunctionCode(Handle<Code> function_code) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000818 FunctionInfoWrapper info =
819 FunctionInfoWrapper::cast(
820 result_->GetElementNoExceptionThrown(current_parent_index_));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000821 info.SetFunctionCode(function_code, Handle<Object>(HEAP->null_value()));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000822 }
823
824 // Saves full information about a function: its code, its scope info
825 // and a SharedFunctionInfo object.
826 void FunctionInfo(Handle<SharedFunctionInfo> shared, Scope* scope) {
827 if (!shared->IsSharedFunctionInfo()) {
828 return;
829 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000830 FunctionInfoWrapper info =
831 FunctionInfoWrapper::cast(
832 result_->GetElementNoExceptionThrown(current_parent_index_));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000833 info.SetFunctionCode(Handle<Code>(shared->code()),
834 Handle<Object>(shared->scope_info()));
835 info.SetSharedFunctionInfo(shared);
836
837 Handle<Object> scope_info_list(SerializeFunctionScope(scope));
838 info.SetOuterScopeInfo(scope_info_list);
839 }
840
841 Handle<JSArray> GetResult() { return result_; }
842
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000843 private:
844 Object* SerializeFunctionScope(Scope* scope) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000845 HandleScope handle_scope;
846
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000847 Handle<JSArray> scope_info_list = FACTORY->NewJSArray(10);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000848 int scope_info_length = 0;
849
850 // Saves some description of scope. It stores name and indexes of
851 // variables in the whole scope chain. Null-named slots delimit
852 // scopes of this chain.
853 Scope* outer_scope = scope->outer_scope();
854 if (outer_scope == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000855 return HEAP->undefined_value();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000856 }
857 do {
858 ZoneList<Variable*> list(10);
859 outer_scope->CollectUsedVariables(&list);
860 int j = 0;
861 for (int i = 0; i < list.length(); i++) {
862 Variable* var1 = list[i];
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000863 Slot* slot = var1->AsSlot();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000864 if (slot != NULL && slot->type() == Slot::CONTEXT) {
865 if (j != i) {
866 list[j] = var1;
867 }
868 j++;
869 }
870 }
871
872 // Sort it.
873 for (int k = 1; k < j; k++) {
874 int l = k;
875 for (int m = k + 1; m < j; m++) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000876 if (list[l]->AsSlot()->index() > list[m]->AsSlot()->index()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000877 l = m;
878 }
879 }
880 list[k] = list[l];
881 }
882 for (int i = 0; i < j; i++) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000883 SetElementNonStrict(scope_info_list,
884 scope_info_length,
885 list[i]->name());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000886 scope_info_length++;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000887 SetElementNonStrict(
888 scope_info_list,
889 scope_info_length,
890 Handle<Smi>(Smi::FromInt(list[i]->AsSlot()->index())));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000891 scope_info_length++;
892 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000893 SetElementNonStrict(scope_info_list,
894 scope_info_length,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000895 Handle<Object>(HEAP->null_value()));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000896 scope_info_length++;
897
898 outer_scope = outer_scope->outer_scope();
899 } while (outer_scope != NULL);
900
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000901 return *scope_info_list;
ager@chromium.org5c838252010-02-19 08:53:10 +0000902 }
903
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000904 Handle<JSArray> result_;
905 int len_;
906 int current_parent_index_;
ager@chromium.org5c838252010-02-19 08:53:10 +0000907};
908
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000909
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000910JSArray* LiveEdit::GatherCompileInfo(Handle<Script> script,
911 Handle<String> source) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000912 Isolate* isolate = Isolate::Current();
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000913 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000914
915 FunctionInfoListener listener;
916 Handle<Object> original_source = Handle<Object>(script->source());
917 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000918 isolate->set_active_function_info_listener(&listener);
919 CompileScriptForTracker(isolate, script);
920 isolate->set_active_function_info_listener(NULL);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000921 script->set_source(*original_source);
922
923 return *(listener.GetResult());
924}
925
926
927void LiveEdit::WrapSharedFunctionInfos(Handle<JSArray> array) {
928 HandleScope scope;
929 int len = Smi::cast(array->length())->value();
930 for (int i = 0; i < len; i++) {
931 Handle<SharedFunctionInfo> info(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000932 SharedFunctionInfo::cast(array->GetElementNoExceptionThrown(i)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000933 SharedInfoWrapper info_wrapper = SharedInfoWrapper::Create();
934 Handle<String> name_handle(String::cast(info->name()));
935 info_wrapper.SetProperties(name_handle, info->start_position(),
936 info->end_position(), info);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000937 SetElementNonStrict(array, i, info_wrapper.GetJSArray());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000938 }
939}
940
941
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000942// Visitor that collects all references to a particular code object,
943// including "CODE_TARGET" references in other code objects.
944// It works in context of ZoneScope.
945class ReferenceCollectorVisitor : public ObjectVisitor {
946 public:
947 explicit ReferenceCollectorVisitor(Code* original)
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000948 : original_(original), rvalues_(10), reloc_infos_(10), code_entries_(10) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000949 }
950
951 virtual void VisitPointers(Object** start, Object** end) {
952 for (Object** p = start; p < end; p++) {
953 if (*p == original_) {
954 rvalues_.Add(p);
955 }
956 }
957 }
958
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000959 virtual void VisitCodeEntry(Address entry) {
960 if (Code::GetObjectFromEntryAddress(entry) == original_) {
961 code_entries_.Add(entry);
962 }
963 }
964
965 virtual void VisitCodeTarget(RelocInfo* rinfo) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000966 if (RelocInfo::IsCodeTarget(rinfo->rmode()) &&
967 Code::GetCodeFromTargetAddress(rinfo->target_address()) == original_) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000968 reloc_infos_.Add(*rinfo);
969 }
970 }
971
972 virtual void VisitDebugTarget(RelocInfo* rinfo) {
973 VisitCodeTarget(rinfo);
974 }
975
976 // Post-visiting method that iterates over all collected references and
977 // modifies them.
978 void Replace(Code* substitution) {
979 for (int i = 0; i < rvalues_.length(); i++) {
980 *(rvalues_[i]) = substitution;
981 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000982 Address substitution_entry = substitution->instruction_start();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000983 for (int i = 0; i < reloc_infos_.length(); i++) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000984 reloc_infos_[i].set_target_address(substitution_entry);
985 }
986 for (int i = 0; i < code_entries_.length(); i++) {
987 Address entry = code_entries_[i];
988 Memory::Address_at(entry) = substitution_entry;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000989 }
990 }
991
992 private:
993 Code* original_;
994 ZoneList<Object**> rvalues_;
995 ZoneList<RelocInfo> reloc_infos_;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000996 ZoneList<Address> code_entries_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000997};
998
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000999
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001000// Finds all references to original and replaces them with substitution.
1001static void ReplaceCodeObject(Code* original, Code* substitution) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001002 ASSERT(!HEAP->InNewSpace(substitution));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001003
1004 AssertNoAllocation no_allocations_please;
1005
1006 // A zone scope for ReferenceCollectorVisitor.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001007 ZoneScope scope(Isolate::Current(), DELETE_ON_EXIT);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001008
1009 ReferenceCollectorVisitor visitor(original);
1010
1011 // Iterate over all roots. Stack frames may have pointer into original code,
1012 // so temporary replace the pointers with offset numbers
1013 // in prologue/epilogue.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001014 {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001015 HEAP->IterateStrongRoots(&visitor, VISIT_ALL);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001016 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001017
1018 // Now iterate over all pointers of all objects, including code_target
1019 // implicit pointers.
1020 HeapIterator iterator;
1021 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
1022 obj->Iterate(&visitor);
1023 }
1024
1025 visitor.Replace(substitution);
1026}
1027
1028
ager@chromium.org357bf652010-04-12 11:30:10 +00001029// Check whether the code is natural function code (not a lazy-compile stub
1030// code).
1031static bool IsJSFunctionCode(Code* code) {
1032 return code->kind() == Code::FUNCTION;
1033}
1034
1035
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001036// Returns true if an instance of candidate were inlined into function's code.
1037static bool IsInlined(JSFunction* function, SharedFunctionInfo* candidate) {
1038 AssertNoAllocation no_gc;
1039
1040 if (function->code()->kind() != Code::OPTIMIZED_FUNCTION) return false;
1041
1042 DeoptimizationInputData* data =
1043 DeoptimizationInputData::cast(function->code()->deoptimization_data());
1044
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001045 if (data == HEAP->empty_fixed_array()) return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001046
1047 FixedArray* literals = data->LiteralArray();
1048
1049 int inlined_count = data->InlinedFunctionCount()->value();
1050 for (int i = 0; i < inlined_count; ++i) {
1051 JSFunction* inlined = JSFunction::cast(literals->get(i));
1052 if (inlined->shared() == candidate) return true;
1053 }
1054
1055 return false;
1056}
1057
1058
1059class DependentFunctionsDeoptimizingVisitor : public OptimizedFunctionVisitor {
1060 public:
1061 explicit DependentFunctionsDeoptimizingVisitor(
1062 SharedFunctionInfo* function_info)
1063 : function_info_(function_info) {}
1064
1065 virtual void EnterContext(Context* context) {
1066 }
1067
1068 virtual void VisitFunction(JSFunction* function) {
1069 if (function->shared() == function_info_ ||
1070 IsInlined(function, function_info_)) {
1071 Deoptimizer::DeoptimizeFunction(function);
1072 }
1073 }
1074
1075 virtual void LeaveContext(Context* context) {
1076 }
1077
1078 private:
1079 SharedFunctionInfo* function_info_;
1080};
1081
1082
1083static void DeoptimizeDependentFunctions(SharedFunctionInfo* function_info) {
1084 AssertNoAllocation no_allocation;
1085
1086 DependentFunctionsDeoptimizingVisitor visitor(function_info);
1087 Deoptimizer::VisitAllOptimizedFunctions(&visitor);
1088}
1089
1090
lrn@chromium.org303ada72010-10-27 09:33:13 +00001091MaybeObject* LiveEdit::ReplaceFunctionCode(
1092 Handle<JSArray> new_compile_info_array,
1093 Handle<JSArray> shared_info_array) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001094 HandleScope scope;
1095
ager@chromium.orgac091b72010-05-05 07:34:42 +00001096 if (!SharedInfoWrapper::IsInstance(shared_info_array)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001097 return Isolate::Current()->ThrowIllegalOperation();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001098 }
1099
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001100 FunctionInfoWrapper compile_info_wrapper(new_compile_info_array);
1101 SharedInfoWrapper shared_info_wrapper(shared_info_array);
1102
1103 Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo();
1104
ager@chromium.org357bf652010-04-12 11:30:10 +00001105 if (IsJSFunctionCode(shared_info->code())) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001106 Handle<Code> code = compile_info_wrapper.GetFunctionCode();
1107 ReplaceCodeObject(shared_info->code(), *code);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001108 Handle<Object> code_scope_info = compile_info_wrapper.GetCodeScopeInfo();
1109 if (code_scope_info->IsFixedArray()) {
1110 shared_info->set_scope_info(SerializedScopeInfo::cast(*code_scope_info));
1111 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001112 }
1113
1114 if (shared_info->debug_info()->IsDebugInfo()) {
1115 Handle<DebugInfo> debug_info(DebugInfo::cast(shared_info->debug_info()));
1116 Handle<Code> new_original_code =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001117 FACTORY->CopyCode(compile_info_wrapper.GetFunctionCode());
ager@chromium.org357bf652010-04-12 11:30:10 +00001118 debug_info->set_original_code(*new_original_code);
1119 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001120
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001121 int start_position = compile_info_wrapper.GetStartPosition();
1122 int end_position = compile_info_wrapper.GetEndPosition();
1123 shared_info->set_start_position(start_position);
1124 shared_info->set_end_position(end_position);
ager@chromium.org357bf652010-04-12 11:30:10 +00001125
1126 shared_info->set_construct_stub(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001127 Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001128 Builtins::kJSConstructStubGeneric));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001129
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001130 DeoptimizeDependentFunctions(*shared_info);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001131 Isolate::Current()->compilation_cache()->Remove(shared_info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001132
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001133 return HEAP->undefined_value();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001134}
1135
1136
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001137MaybeObject* LiveEdit::FunctionSourceUpdated(
1138 Handle<JSArray> shared_info_array) {
1139 HandleScope scope;
1140
1141 if (!SharedInfoWrapper::IsInstance(shared_info_array)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001142 return Isolate::Current()->ThrowIllegalOperation();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001143 }
1144
1145 SharedInfoWrapper shared_info_wrapper(shared_info_array);
1146 Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo();
1147
1148 DeoptimizeDependentFunctions(*shared_info);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001149 Isolate::Current()->compilation_cache()->Remove(shared_info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001150
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001151 return HEAP->undefined_value();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001152}
1153
1154
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001155void LiveEdit::SetFunctionScript(Handle<JSValue> function_wrapper,
1156 Handle<Object> script_handle) {
1157 Handle<SharedFunctionInfo> shared_info =
1158 Handle<SharedFunctionInfo>::cast(UnwrapJSValue(function_wrapper));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001159 shared_info->set_script(*script_handle);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001160
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001161 Isolate::Current()->compilation_cache()->Remove(shared_info);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001162}
1163
1164
1165// For a script text change (defined as position_change_array), translates
1166// position in unchanged text to position in changed text.
1167// Text change is a set of non-overlapping regions in text, that have changed
1168// their contents and length. It is specified as array of groups of 3 numbers:
1169// (change_begin, change_end, change_end_new_position).
1170// Each group describes a change in text; groups are sorted by change_begin.
1171// Only position in text beyond any changes may be successfully translated.
1172// If a positions is inside some region that changed, result is currently
1173// undefined.
1174static int TranslatePosition(int original_position,
1175 Handle<JSArray> position_change_array) {
1176 int position_diff = 0;
1177 int array_len = Smi::cast(position_change_array->length())->value();
1178 // TODO(635): binary search may be used here
1179 for (int i = 0; i < array_len; i += 3) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001180 Object* element = position_change_array->GetElementNoExceptionThrown(i);
1181 int chunk_start = Smi::cast(element)->value();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001182 if (original_position < chunk_start) {
1183 break;
1184 }
lrn@chromium.org303ada72010-10-27 09:33:13 +00001185 element = position_change_array->GetElementNoExceptionThrown(i + 1);
1186 int chunk_end = Smi::cast(element)->value();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001187 // Position mustn't be inside a chunk.
1188 ASSERT(original_position >= chunk_end);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001189 element = position_change_array->GetElementNoExceptionThrown(i + 2);
1190 int chunk_changed_end = Smi::cast(element)->value();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001191 position_diff = chunk_changed_end - chunk_end;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001192 }
1193
1194 return original_position + position_diff;
1195}
1196
1197
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001198// Auto-growing buffer for writing relocation info code section. This buffer
1199// is a simplified version of buffer from Assembler. Unlike Assembler, this
1200// class is platform-independent and it works without dealing with instructions.
1201// As specified by RelocInfo format, the buffer is filled in reversed order:
1202// from upper to lower addresses.
1203// It uses NewArray/DeleteArray for memory management.
1204class RelocInfoBuffer {
1205 public:
1206 RelocInfoBuffer(int buffer_initial_capicity, byte* pc) {
1207 buffer_size_ = buffer_initial_capicity + kBufferGap;
1208 buffer_ = NewArray<byte>(buffer_size_);
1209
1210 reloc_info_writer_.Reposition(buffer_ + buffer_size_, pc);
1211 }
1212 ~RelocInfoBuffer() {
1213 DeleteArray(buffer_);
1214 }
1215
1216 // As specified by RelocInfo format, the buffer is filled in reversed order:
1217 // from upper to lower addresses.
1218 void Write(const RelocInfo* rinfo) {
1219 if (buffer_ + kBufferGap >= reloc_info_writer_.pos()) {
1220 Grow();
1221 }
1222 reloc_info_writer_.Write(rinfo);
1223 }
1224
1225 Vector<byte> GetResult() {
1226 // Return the bytes from pos up to end of buffer.
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001227 int result_size =
1228 static_cast<int>((buffer_ + buffer_size_) - reloc_info_writer_.pos());
1229 return Vector<byte>(reloc_info_writer_.pos(), result_size);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001230 }
1231
1232 private:
1233 void Grow() {
1234 // Compute new buffer size.
1235 int new_buffer_size;
1236 if (buffer_size_ < 2 * KB) {
1237 new_buffer_size = 4 * KB;
1238 } else {
1239 new_buffer_size = 2 * buffer_size_;
1240 }
1241 // Some internal data structures overflow for very large buffers,
1242 // they must ensure that kMaximalBufferSize is not too large.
1243 if (new_buffer_size > kMaximalBufferSize) {
1244 V8::FatalProcessOutOfMemory("RelocInfoBuffer::GrowBuffer");
1245 }
1246
1247 // Setup new buffer.
1248 byte* new_buffer = NewArray<byte>(new_buffer_size);
1249
1250 // Copy the data.
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001251 int curently_used_size =
1252 static_cast<int>(buffer_ + buffer_size_ - reloc_info_writer_.pos());
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001253 memmove(new_buffer + new_buffer_size - curently_used_size,
1254 reloc_info_writer_.pos(), curently_used_size);
1255
1256 reloc_info_writer_.Reposition(
1257 new_buffer + new_buffer_size - curently_used_size,
1258 reloc_info_writer_.last_pc());
1259
1260 DeleteArray(buffer_);
1261 buffer_ = new_buffer;
1262 buffer_size_ = new_buffer_size;
1263 }
1264
1265 RelocInfoWriter reloc_info_writer_;
1266 byte* buffer_;
1267 int buffer_size_;
1268
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001269 static const int kBufferGap = RelocInfoWriter::kMaxSize;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001270 static const int kMaximalBufferSize = 512*MB;
1271};
1272
1273// Patch positions in code (changes relocation info section) and possibly
1274// returns new instance of code.
1275static Handle<Code> PatchPositionsInCode(Handle<Code> code,
1276 Handle<JSArray> position_change_array) {
1277
1278 RelocInfoBuffer buffer_writer(code->relocation_size(),
1279 code->instruction_start());
1280
1281 {
1282 AssertNoAllocation no_allocations_please;
1283 for (RelocIterator it(*code); !it.done(); it.next()) {
1284 RelocInfo* rinfo = it.rinfo();
1285 if (RelocInfo::IsPosition(rinfo->rmode())) {
1286 int position = static_cast<int>(rinfo->data());
1287 int new_position = TranslatePosition(position,
1288 position_change_array);
1289 if (position != new_position) {
1290 RelocInfo info_copy(rinfo->pc(), rinfo->rmode(), new_position);
1291 buffer_writer.Write(&info_copy);
1292 continue;
1293 }
1294 }
1295 buffer_writer.Write(it.rinfo());
1296 }
1297 }
1298
1299 Vector<byte> buffer = buffer_writer.GetResult();
1300
1301 if (buffer.length() == code->relocation_size()) {
1302 // Simply patch relocation area of code.
1303 memcpy(code->relocation_start(), buffer.start(), buffer.length());
1304 return code;
1305 } else {
1306 // Relocation info section now has different size. We cannot simply
1307 // rewrite it inside code object. Instead we have to create a new
1308 // code object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001309 Handle<Code> result(FACTORY->CopyCode(code, buffer));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001310 return result;
1311 }
1312}
1313
1314
lrn@chromium.org303ada72010-10-27 09:33:13 +00001315MaybeObject* LiveEdit::PatchFunctionPositions(
ager@chromium.org357bf652010-04-12 11:30:10 +00001316 Handle<JSArray> shared_info_array, Handle<JSArray> position_change_array) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001317
1318 if (!SharedInfoWrapper::IsInstance(shared_info_array)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001319 return Isolate::Current()->ThrowIllegalOperation();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001320 }
1321
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001322 SharedInfoWrapper shared_info_wrapper(shared_info_array);
1323 Handle<SharedFunctionInfo> info = shared_info_wrapper.GetInfo();
1324
ager@chromium.org357bf652010-04-12 11:30:10 +00001325 int old_function_start = info->start_position();
1326 int new_function_start = TranslatePosition(old_function_start,
1327 position_change_array);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001328 int new_function_end = TranslatePosition(info->end_position(),
1329 position_change_array);
1330 int new_function_token_pos =
1331 TranslatePosition(info->function_token_position(), position_change_array);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001332
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001333 info->set_start_position(new_function_start);
1334 info->set_end_position(new_function_end);
1335 info->set_function_token_position(new_function_token_pos);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001336
ager@chromium.org357bf652010-04-12 11:30:10 +00001337 if (IsJSFunctionCode(info->code())) {
1338 // Patch relocation info section of the code.
1339 Handle<Code> patched_code = PatchPositionsInCode(Handle<Code>(info->code()),
1340 position_change_array);
1341 if (*patched_code != info->code()) {
1342 // Replace all references to the code across the heap. In particular,
1343 // some stubs may refer to this code and this code may be being executed
1344 // on stack (it is safe to substitute the code object on stack, because
1345 // we only change the structure of rinfo and leave instructions
1346 // untouched).
1347 ReplaceCodeObject(info->code(), *patched_code);
1348 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001349 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001350
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001351 return HEAP->undefined_value();
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001352}
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001353
ager@chromium.org357bf652010-04-12 11:30:10 +00001354
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001355static Handle<Script> CreateScriptCopy(Handle<Script> original) {
1356 Handle<String> original_source(String::cast(original->source()));
ager@chromium.org357bf652010-04-12 11:30:10 +00001357
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001358 Handle<Script> copy = FACTORY->NewScript(original_source);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001359
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001360 copy->set_name(original->name());
1361 copy->set_line_offset(original->line_offset());
1362 copy->set_column_offset(original->column_offset());
1363 copy->set_data(original->data());
1364 copy->set_type(original->type());
1365 copy->set_context_data(original->context_data());
1366 copy->set_compilation_type(original->compilation_type());
1367 copy->set_eval_from_shared(original->eval_from_shared());
1368 copy->set_eval_from_instructions_offset(
1369 original->eval_from_instructions_offset());
1370
1371 return copy;
1372}
1373
1374
1375Object* LiveEdit::ChangeScriptSource(Handle<Script> original_script,
1376 Handle<String> new_source,
1377 Handle<Object> old_script_name) {
1378 Handle<Object> old_script_object;
1379 if (old_script_name->IsString()) {
1380 Handle<Script> old_script = CreateScriptCopy(original_script);
1381 old_script->set_name(String::cast(*old_script_name));
1382 old_script_object = old_script;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001383 Isolate::Current()->debugger()->OnAfterCompile(
1384 old_script, Debugger::SEND_WHEN_DEBUGGING);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001385 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001386 old_script_object = Handle<Object>(HEAP->null_value());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001387 }
1388
1389 original_script->set_source(*new_source);
1390
1391 // Drop line ends so that they will be recalculated.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001392 original_script->set_line_ends(HEAP->undefined_value());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001393
1394 return *old_script_object;
1395}
1396
1397
1398
1399void LiveEdit::ReplaceRefToNestedFunction(
1400 Handle<JSValue> parent_function_wrapper,
1401 Handle<JSValue> orig_function_wrapper,
1402 Handle<JSValue> subst_function_wrapper) {
1403
1404 Handle<SharedFunctionInfo> parent_shared =
1405 Handle<SharedFunctionInfo>::cast(UnwrapJSValue(parent_function_wrapper));
1406 Handle<SharedFunctionInfo> orig_shared =
1407 Handle<SharedFunctionInfo>::cast(UnwrapJSValue(orig_function_wrapper));
1408 Handle<SharedFunctionInfo> subst_shared =
1409 Handle<SharedFunctionInfo>::cast(UnwrapJSValue(subst_function_wrapper));
1410
1411 for (RelocIterator it(parent_shared->code()); !it.done(); it.next()) {
1412 if (it.rinfo()->rmode() == RelocInfo::EMBEDDED_OBJECT) {
1413 if (it.rinfo()->target_object() == *orig_shared) {
1414 it.rinfo()->set_target_object(*subst_shared);
ager@chromium.org357bf652010-04-12 11:30:10 +00001415 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001416 }
1417 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001418}
1419
1420
1421// Check an activation against list of functions. If there is a function
1422// that matches, its status in result array is changed to status argument value.
1423static bool CheckActivation(Handle<JSArray> shared_info_array,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001424 Handle<JSArray> result,
1425 StackFrame* frame,
ager@chromium.org357bf652010-04-12 11:30:10 +00001426 LiveEdit::FunctionPatchabilityStatus status) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001427 if (!frame->is_java_script()) return false;
1428
1429 Handle<JSFunction> function(
1430 JSFunction::cast(JavaScriptFrame::cast(frame)->function()));
1431
ager@chromium.org357bf652010-04-12 11:30:10 +00001432 int len = Smi::cast(shared_info_array->length())->value();
1433 for (int i = 0; i < len; i++) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001434 JSValue* wrapper =
1435 JSValue::cast(shared_info_array->GetElementNoExceptionThrown(i));
ager@chromium.org357bf652010-04-12 11:30:10 +00001436 Handle<SharedFunctionInfo> shared(
1437 SharedFunctionInfo::cast(wrapper->value()));
1438
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001439 if (function->shared() == *shared || IsInlined(*function, *shared)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001440 SetElementNonStrict(result, i, Handle<Smi>(Smi::FromInt(status)));
ager@chromium.org357bf652010-04-12 11:30:10 +00001441 return true;
1442 }
1443 }
1444 return false;
1445}
1446
1447
1448// Iterates over handler chain and removes all elements that are inside
1449// frames being dropped.
1450static bool FixTryCatchHandler(StackFrame* top_frame,
1451 StackFrame* bottom_frame) {
1452 Address* pointer_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001453 &Memory::Address_at(Isolate::Current()->get_address_from_id(
1454 Isolate::k_handler_address));
ager@chromium.org357bf652010-04-12 11:30:10 +00001455
1456 while (*pointer_address < top_frame->sp()) {
1457 pointer_address = &Memory::Address_at(*pointer_address);
1458 }
1459 Address* above_frame_address = pointer_address;
1460 while (*pointer_address < bottom_frame->fp()) {
1461 pointer_address = &Memory::Address_at(*pointer_address);
1462 }
1463 bool change = *above_frame_address != *pointer_address;
1464 *above_frame_address = *pointer_address;
1465 return change;
1466}
1467
1468
1469// Removes specified range of frames from stack. There may be 1 or more
1470// frames in range. Anyway the bottom frame is restarted rather than dropped,
1471// and therefore has to be a JavaScript frame.
1472// Returns error message or NULL.
1473static const char* DropFrames(Vector<StackFrame*> frames,
1474 int top_frame_index,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001475 int bottom_js_frame_index,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001476 Debug::FrameDropMode* mode,
1477 Object*** restarter_frame_function_pointer) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001478 if (!Debug::kFrameDropperSupported) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001479 return "Stack manipulations are not supported in this architecture.";
1480 }
1481
ager@chromium.org357bf652010-04-12 11:30:10 +00001482 StackFrame* pre_top_frame = frames[top_frame_index - 1];
1483 StackFrame* top_frame = frames[top_frame_index];
1484 StackFrame* bottom_js_frame = frames[bottom_js_frame_index];
1485
1486 ASSERT(bottom_js_frame->is_java_script());
1487
1488 // Check the nature of the top frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001489 Isolate* isolate = Isolate::Current();
1490 Code* pre_top_frame_code = pre_top_frame->LookupCode();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001491 if (pre_top_frame_code->is_inline_cache_stub() &&
1492 pre_top_frame_code->ic_state() == DEBUG_BREAK) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001493 // OK, we can drop inline cache calls.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001494 *mode = Debug::FRAME_DROPPED_IN_IC_CALL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001495 } else if (pre_top_frame_code ==
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001496 isolate->debug()->debug_break_slot()) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001497 // OK, we can drop debug break slot.
1498 *mode = Debug::FRAME_DROPPED_IN_DEBUG_SLOT_CALL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001499 } else if (pre_top_frame_code ==
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001500 isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001501 Builtins::kFrameDropper_LiveEdit)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001502 // OK, we can drop our own code.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001503 *mode = Debug::FRAME_DROPPED_IN_DIRECT_CALL;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001504 } else if (pre_top_frame_code ==
1505 isolate->builtins()->builtin(Builtins::kReturn_DebugBreak)) {
1506 *mode = Debug::FRAME_DROPPED_IN_RETURN_CALL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001507 } else if (pre_top_frame_code->kind() == Code::STUB &&
1508 pre_top_frame_code->major_key()) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001509 // Entry from our unit tests, it's fine, we support this case.
1510 *mode = Debug::FRAME_DROPPED_IN_DIRECT_CALL;
ager@chromium.org357bf652010-04-12 11:30:10 +00001511 } else {
1512 return "Unknown structure of stack above changing function";
1513 }
1514
1515 Address unused_stack_top = top_frame->sp();
1516 Address unused_stack_bottom = bottom_js_frame->fp()
1517 - Debug::kFrameDropperFrameSize * kPointerSize // Size of the new frame.
1518 + kPointerSize; // Bigger address end is exclusive.
1519
1520 if (unused_stack_top > unused_stack_bottom) {
1521 return "Not enough space for frame dropper frame";
1522 }
1523
1524 // Committing now. After this point we should return only NULL value.
1525
1526 FixTryCatchHandler(pre_top_frame, bottom_js_frame);
1527 // Make sure FixTryCatchHandler is idempotent.
1528 ASSERT(!FixTryCatchHandler(pre_top_frame, bottom_js_frame));
1529
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001530 Handle<Code> code = Isolate::Current()->builtins()->FrameDropper_LiveEdit();
ager@chromium.org357bf652010-04-12 11:30:10 +00001531 top_frame->set_pc(code->entry());
1532 pre_top_frame->SetCallerFp(bottom_js_frame->fp());
1533
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001534 *restarter_frame_function_pointer =
1535 Debug::SetUpFrameDropperFrame(bottom_js_frame, code);
1536
1537 ASSERT((**restarter_frame_function_pointer)->IsJSFunction());
ager@chromium.org357bf652010-04-12 11:30:10 +00001538
1539 for (Address a = unused_stack_top;
1540 a < unused_stack_bottom;
1541 a += kPointerSize) {
1542 Memory::Object_at(a) = Smi::FromInt(0);
1543 }
1544
1545 return NULL;
1546}
1547
1548
1549static bool IsDropableFrame(StackFrame* frame) {
1550 return !frame->is_exit();
1551}
1552
1553// Fills result array with statuses of functions. Modifies the stack
1554// removing all listed function if possible and if do_drop is true.
1555static const char* DropActivationsInActiveThread(
1556 Handle<JSArray> shared_info_array, Handle<JSArray> result, bool do_drop) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00001557 Isolate* isolate = Isolate::Current();
1558 Debug* debug = isolate->debug();
1559 ZoneScope scope(isolate, DELETE_ON_EXIT);
ager@chromium.org357bf652010-04-12 11:30:10 +00001560 Vector<StackFrame*> frames = CreateStackMap();
1561
1562 int array_len = Smi::cast(shared_info_array->length())->value();
1563
1564 int top_frame_index = -1;
1565 int frame_index = 0;
1566 for (; frame_index < frames.length(); frame_index++) {
1567 StackFrame* frame = frames[frame_index];
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001568 if (frame->id() == debug->break_frame_id()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001569 top_frame_index = frame_index;
1570 break;
1571 }
1572 if (CheckActivation(shared_info_array, result, frame,
1573 LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE)) {
1574 // We are still above break_frame. It is not a target frame,
1575 // it is a problem.
1576 return "Debugger mark-up on stack is not found";
1577 }
1578 }
1579
1580 if (top_frame_index == -1) {
1581 // We haven't found break frame, but no function is blocking us anyway.
1582 return NULL;
1583 }
1584
1585 bool target_frame_found = false;
1586 int bottom_js_frame_index = top_frame_index;
1587 bool c_code_found = false;
1588
1589 for (; frame_index < frames.length(); frame_index++) {
1590 StackFrame* frame = frames[frame_index];
1591 if (!IsDropableFrame(frame)) {
1592 c_code_found = true;
1593 break;
1594 }
1595 if (CheckActivation(shared_info_array, result, frame,
1596 LiveEdit::FUNCTION_BLOCKED_ON_ACTIVE_STACK)) {
1597 target_frame_found = true;
1598 bottom_js_frame_index = frame_index;
1599 }
1600 }
1601
1602 if (c_code_found) {
1603 // There is a C frames on stack. Check that there are no target frames
1604 // below them.
1605 for (; frame_index < frames.length(); frame_index++) {
1606 StackFrame* frame = frames[frame_index];
1607 if (frame->is_java_script()) {
1608 if (CheckActivation(shared_info_array, result, frame,
1609 LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE)) {
1610 // Cannot drop frame under C frames.
1611 return NULL;
1612 }
1613 }
1614 }
1615 }
1616
1617 if (!do_drop) {
1618 // We are in check-only mode.
1619 return NULL;
1620 }
1621
1622 if (!target_frame_found) {
1623 // Nothing to drop.
1624 return NULL;
1625 }
1626
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001627 Debug::FrameDropMode drop_mode = Debug::FRAMES_UNTOUCHED;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001628 Object** restarter_frame_function_pointer = NULL;
ager@chromium.org357bf652010-04-12 11:30:10 +00001629 const char* error_message = DropFrames(frames, top_frame_index,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001630 bottom_js_frame_index, &drop_mode,
1631 &restarter_frame_function_pointer);
ager@chromium.org357bf652010-04-12 11:30:10 +00001632
1633 if (error_message != NULL) {
1634 return error_message;
1635 }
1636
1637 // Adjust break_frame after some frames has been dropped.
1638 StackFrame::Id new_id = StackFrame::NO_ID;
1639 for (int i = bottom_js_frame_index + 1; i < frames.length(); i++) {
1640 if (frames[i]->type() == StackFrame::JAVA_SCRIPT) {
1641 new_id = frames[i]->id();
1642 break;
1643 }
1644 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001645 debug->FramesHaveBeenDropped(new_id, drop_mode,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001646 restarter_frame_function_pointer);
ager@chromium.org357bf652010-04-12 11:30:10 +00001647
1648 // Replace "blocked on active" with "replaced on active" status.
1649 for (int i = 0; i < array_len; i++) {
1650 if (result->GetElement(i) ==
1651 Smi::FromInt(LiveEdit::FUNCTION_BLOCKED_ON_ACTIVE_STACK)) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001652 Handle<Object> replaced(
1653 Smi::FromInt(LiveEdit::FUNCTION_REPLACED_ON_ACTIVE_STACK));
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001654 SetElementNonStrict(result, i, replaced);
ager@chromium.org357bf652010-04-12 11:30:10 +00001655 }
1656 }
1657 return NULL;
1658}
1659
1660
1661class InactiveThreadActivationsChecker : public ThreadVisitor {
1662 public:
1663 InactiveThreadActivationsChecker(Handle<JSArray> shared_info_array,
1664 Handle<JSArray> result)
1665 : shared_info_array_(shared_info_array), result_(result),
1666 has_blocked_functions_(false) {
1667 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001668 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1669 for (StackFrameIterator it(isolate, top); !it.done(); it.Advance()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001670 has_blocked_functions_ |= CheckActivation(
1671 shared_info_array_, result_, it.frame(),
1672 LiveEdit::FUNCTION_BLOCKED_ON_OTHER_STACK);
1673 }
1674 }
1675 bool HasBlockedFunctions() {
1676 return has_blocked_functions_;
1677 }
1678
1679 private:
1680 Handle<JSArray> shared_info_array_;
1681 Handle<JSArray> result_;
1682 bool has_blocked_functions_;
1683};
1684
1685
1686Handle<JSArray> LiveEdit::CheckAndDropActivations(
1687 Handle<JSArray> shared_info_array, bool do_drop) {
1688 int len = Smi::cast(shared_info_array->length())->value();
1689
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001690 Handle<JSArray> result = FACTORY->NewJSArray(len);
ager@chromium.org357bf652010-04-12 11:30:10 +00001691
1692 // Fill the default values.
1693 for (int i = 0; i < len; i++) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001694 SetElementNonStrict(
1695 result,
1696 i,
1697 Handle<Smi>(Smi::FromInt(FUNCTION_AVAILABLE_FOR_PATCH)));
ager@chromium.org357bf652010-04-12 11:30:10 +00001698 }
1699
1700
1701 // First check inactive threads. Fail if some functions are blocked there.
1702 InactiveThreadActivationsChecker inactive_threads_checker(shared_info_array,
1703 result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001704 Isolate::Current()->thread_manager()->IterateArchivedThreads(
1705 &inactive_threads_checker);
ager@chromium.org357bf652010-04-12 11:30:10 +00001706 if (inactive_threads_checker.HasBlockedFunctions()) {
1707 return result;
1708 }
1709
1710 // Try to drop activations from the current stack.
1711 const char* error_message =
1712 DropActivationsInActiveThread(shared_info_array, result, do_drop);
1713 if (error_message != NULL) {
1714 // Add error message as an array extra element.
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001715 Vector<const char> vector_message(error_message, StrLength(error_message));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001716 Handle<String> str = FACTORY->NewStringFromAscii(vector_message);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001717 SetElementNonStrict(result, len, str);
ager@chromium.org357bf652010-04-12 11:30:10 +00001718 }
1719 return result;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001720}
1721
1722
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001723LiveEditFunctionTracker::LiveEditFunctionTracker(Isolate* isolate,
1724 FunctionLiteral* fun)
1725 : isolate_(isolate) {
1726 if (isolate_->active_function_info_listener() != NULL) {
1727 isolate_->active_function_info_listener()->FunctionStarted(fun);
ager@chromium.org5c838252010-02-19 08:53:10 +00001728 }
1729}
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001730
1731
ager@chromium.org5c838252010-02-19 08:53:10 +00001732LiveEditFunctionTracker::~LiveEditFunctionTracker() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001733 if (isolate_->active_function_info_listener() != NULL) {
1734 isolate_->active_function_info_listener()->FunctionDone();
ager@chromium.org5c838252010-02-19 08:53:10 +00001735 }
1736}
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001737
1738
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001739void LiveEditFunctionTracker::RecordFunctionInfo(
1740 Handle<SharedFunctionInfo> info, FunctionLiteral* lit) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001741 if (isolate_->active_function_info_listener() != NULL) {
1742 isolate_->active_function_info_listener()->FunctionInfo(info, lit->scope());
ager@chromium.org5c838252010-02-19 08:53:10 +00001743 }
1744}
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001745
1746
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001747void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001748 isolate_->active_function_info_listener()->FunctionCode(code);
ager@chromium.org5c838252010-02-19 08:53:10 +00001749}
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001750
1751
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001752bool LiveEditFunctionTracker::IsActive(Isolate* isolate) {
1753 return isolate->active_function_info_listener() != NULL;
ager@chromium.org5c838252010-02-19 08:53:10 +00001754}
1755
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001756
1757#else // ENABLE_DEBUGGER_SUPPORT
1758
1759// This ifdef-else-endif section provides working or stub implementation of
1760// LiveEditFunctionTracker.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001761LiveEditFunctionTracker::LiveEditFunctionTracker(Isolate* isolate,
1762 FunctionLiteral* fun) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001763}
1764
1765
1766LiveEditFunctionTracker::~LiveEditFunctionTracker() {
1767}
1768
1769
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001770void LiveEditFunctionTracker::RecordFunctionInfo(
1771 Handle<SharedFunctionInfo> info, FunctionLiteral* lit) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001772}
1773
1774
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001775void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001776}
1777
1778
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001779bool LiveEditFunctionTracker::IsActive(Isolate* isolate) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001780 return false;
1781}
1782
1783#endif // ENABLE_DEBUGGER_SUPPORT
1784
1785
1786
ager@chromium.org5c838252010-02-19 08:53:10 +00001787} } // namespace v8::internal