Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | |
| 29 | #include "v8.h" |
| 30 | |
| 31 | #include "liveedit.h" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 32 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 33 | #include "compiler.h" |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 34 | #include "debug.h" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 35 | #include "global-handles.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 36 | #include "memory.h" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 37 | #include "oprofile-agent.h" |
| 38 | #include "parser.h" |
| 39 | #include "scopeinfo.h" |
| 40 | #include "scopes.h" |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 41 | |
| 42 | namespace v8 { |
| 43 | namespace internal { |
| 44 | |
| 45 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 46 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 47 | |
| 48 | |
| 49 | // A simple implementation of dynamic programming algorithm. It solves |
| 50 | // the problem of finding the difference of 2 arrays. It uses a table of results |
| 51 | // of subproblems. Each cell contains a number together with 2-bit flag |
| 52 | // that helps building the chunk list. |
| 53 | class Differencer { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 54 | public: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 55 | explicit Differencer(Comparator::Input* input) |
| 56 | : input_(input), len1_(input->getLength1()), len2_(input->getLength2()) { |
| 57 | buffer_ = NewArray<int>(len1_ * len2_); |
| 58 | } |
| 59 | ~Differencer() { |
| 60 | DeleteArray(buffer_); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 63 | void Initialize() { |
| 64 | int array_size = len1_ * len2_; |
| 65 | for (int i = 0; i < array_size; i++) { |
| 66 | buffer_[i] = kEmptyCellValue; |
| 67 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 70 | // Makes sure that result for the full problem is calculated and stored |
| 71 | // in the table together with flags showing a path through subproblems. |
| 72 | void FillTable() { |
| 73 | CompareUpToTail(0, 0); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 76 | void SaveResult(Comparator::Output* chunk_writer) { |
| 77 | ResultWriter writer(chunk_writer); |
| 78 | |
| 79 | int pos1 = 0; |
| 80 | int pos2 = 0; |
| 81 | while (true) { |
| 82 | if (pos1 < len1_) { |
| 83 | if (pos2 < len2_) { |
| 84 | Direction dir = get_direction(pos1, pos2); |
| 85 | switch (dir) { |
| 86 | case EQ: |
| 87 | writer.eq(); |
| 88 | pos1++; |
| 89 | pos2++; |
| 90 | break; |
| 91 | case SKIP1: |
| 92 | writer.skip1(1); |
| 93 | pos1++; |
| 94 | break; |
| 95 | case SKIP2: |
| 96 | case SKIP_ANY: |
| 97 | writer.skip2(1); |
| 98 | pos2++; |
| 99 | break; |
| 100 | default: |
| 101 | UNREACHABLE(); |
| 102 | } |
| 103 | } else { |
| 104 | writer.skip1(len1_ - pos1); |
| 105 | break; |
| 106 | } |
| 107 | } else { |
| 108 | if (len2_ != pos2) { |
| 109 | writer.skip2(len2_ - pos2); |
| 110 | } |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | writer.close(); |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | Comparator::Input* input_; |
| 119 | int* buffer_; |
| 120 | int len1_; |
| 121 | int len2_; |
| 122 | |
| 123 | enum Direction { |
| 124 | EQ = 0, |
| 125 | SKIP1, |
| 126 | SKIP2, |
| 127 | SKIP_ANY, |
| 128 | |
| 129 | MAX_DIRECTION_FLAG_VALUE = SKIP_ANY |
| 130 | }; |
| 131 | |
| 132 | // Computes result for a subtask and optionally caches it in the buffer table. |
| 133 | // All results values are shifted to make space for flags in the lower bits. |
| 134 | int CompareUpToTail(int pos1, int pos2) { |
| 135 | if (pos1 < len1_) { |
| 136 | if (pos2 < len2_) { |
| 137 | int cached_res = get_value4(pos1, pos2); |
| 138 | if (cached_res == kEmptyCellValue) { |
| 139 | Direction dir; |
| 140 | int res; |
| 141 | if (input_->equals(pos1, pos2)) { |
| 142 | res = CompareUpToTail(pos1 + 1, pos2 + 1); |
| 143 | dir = EQ; |
| 144 | } else { |
| 145 | int res1 = CompareUpToTail(pos1 + 1, pos2) + |
| 146 | (1 << kDirectionSizeBits); |
| 147 | int res2 = CompareUpToTail(pos1, pos2 + 1) + |
| 148 | (1 << kDirectionSizeBits); |
| 149 | if (res1 == res2) { |
| 150 | res = res1; |
| 151 | dir = SKIP_ANY; |
| 152 | } else if (res1 < res2) { |
| 153 | res = res1; |
| 154 | dir = SKIP1; |
| 155 | } else { |
| 156 | res = res2; |
| 157 | dir = SKIP2; |
| 158 | } |
| 159 | } |
| 160 | set_value4_and_dir(pos1, pos2, res, dir); |
| 161 | cached_res = res; |
| 162 | } |
| 163 | return cached_res; |
| 164 | } else { |
| 165 | return (len1_ - pos1) << kDirectionSizeBits; |
| 166 | } |
| 167 | } else { |
| 168 | return (len2_ - pos2) << kDirectionSizeBits; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | inline int& get_cell(int i1, int i2) { |
| 173 | return buffer_[i1 + i2 * len1_]; |
| 174 | } |
| 175 | |
| 176 | // Each cell keeps a value plus direction. Value is multiplied by 4. |
| 177 | void set_value4_and_dir(int i1, int i2, int value4, Direction dir) { |
| 178 | ASSERT((value4 & kDirectionMask) == 0); |
| 179 | get_cell(i1, i2) = value4 | dir; |
| 180 | } |
| 181 | |
| 182 | int get_value4(int i1, int i2) { |
| 183 | return get_cell(i1, i2) & (kMaxUInt32 ^ kDirectionMask); |
| 184 | } |
| 185 | Direction get_direction(int i1, int i2) { |
| 186 | return static_cast<Direction>(get_cell(i1, i2) & kDirectionMask); |
| 187 | } |
| 188 | |
| 189 | static const int kDirectionSizeBits = 2; |
| 190 | static const int kDirectionMask = (1 << kDirectionSizeBits) - 1; |
| 191 | static const int kEmptyCellValue = -1 << kDirectionSizeBits; |
| 192 | |
| 193 | // This method only holds static assert statement (unfortunately you cannot |
| 194 | // place one in class scope). |
| 195 | void StaticAssertHolder() { |
| 196 | STATIC_ASSERT(MAX_DIRECTION_FLAG_VALUE < (1 << kDirectionSizeBits)); |
| 197 | } |
| 198 | |
| 199 | class ResultWriter { |
| 200 | public: |
| 201 | explicit ResultWriter(Comparator::Output* chunk_writer) |
| 202 | : chunk_writer_(chunk_writer), pos1_(0), pos2_(0), |
| 203 | pos1_begin_(-1), pos2_begin_(-1), has_open_chunk_(false) { |
| 204 | } |
| 205 | void eq() { |
| 206 | FlushChunk(); |
| 207 | pos1_++; |
| 208 | pos2_++; |
| 209 | } |
| 210 | void skip1(int len1) { |
| 211 | StartChunk(); |
| 212 | pos1_ += len1; |
| 213 | } |
| 214 | void skip2(int len2) { |
| 215 | StartChunk(); |
| 216 | pos2_ += len2; |
| 217 | } |
| 218 | void close() { |
| 219 | FlushChunk(); |
| 220 | } |
| 221 | |
| 222 | private: |
| 223 | Comparator::Output* chunk_writer_; |
| 224 | int pos1_; |
| 225 | int pos2_; |
| 226 | int pos1_begin_; |
| 227 | int pos2_begin_; |
| 228 | bool has_open_chunk_; |
| 229 | |
| 230 | void StartChunk() { |
| 231 | if (!has_open_chunk_) { |
| 232 | pos1_begin_ = pos1_; |
| 233 | pos2_begin_ = pos2_; |
| 234 | has_open_chunk_ = true; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void FlushChunk() { |
| 239 | if (has_open_chunk_) { |
| 240 | chunk_writer_->AddChunk(pos1_begin_, pos2_begin_, |
| 241 | pos1_ - pos1_begin_, pos2_ - pos2_begin_); |
| 242 | has_open_chunk_ = false; |
| 243 | } |
| 244 | } |
| 245 | }; |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | void Comparator::CalculateDifference(Comparator::Input* input, |
| 250 | Comparator::Output* result_writer) { |
| 251 | Differencer differencer(input); |
| 252 | differencer.Initialize(); |
| 253 | differencer.FillTable(); |
| 254 | differencer.SaveResult(result_writer); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | static bool CompareSubstrings(Handle<String> s1, int pos1, |
| 259 | Handle<String> s2, int pos2, int len) { |
| 260 | static StringInputBuffer buf1; |
| 261 | static StringInputBuffer buf2; |
| 262 | buf1.Reset(*s1); |
| 263 | buf1.Seek(pos1); |
| 264 | buf2.Reset(*s2); |
| 265 | buf2.Seek(pos2); |
| 266 | for (int i = 0; i < len; i++) { |
| 267 | ASSERT(buf1.has_more() && buf2.has_more()); |
| 268 | if (buf1.GetNext() != buf2.GetNext()) { |
| 269 | return false; |
| 270 | } |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | // Wraps raw n-elements line_ends array as a list of n+1 lines. The last line |
| 277 | // never has terminating new line character. |
| 278 | class LineEndsWrapper { |
| 279 | public: |
| 280 | explicit LineEndsWrapper(Handle<String> string) |
| 281 | : ends_array_(CalculateLineEnds(string, false)), |
| 282 | string_len_(string->length()) { |
| 283 | } |
| 284 | int length() { |
| 285 | return ends_array_->length() + 1; |
| 286 | } |
| 287 | // Returns start for any line including start of the imaginary line after |
| 288 | // the last line. |
| 289 | int GetLineStart(int index) { |
| 290 | if (index == 0) { |
| 291 | return 0; |
| 292 | } else { |
| 293 | return GetLineEnd(index - 1); |
| 294 | } |
| 295 | } |
| 296 | int GetLineEnd(int index) { |
| 297 | if (index == ends_array_->length()) { |
| 298 | // End of the last line is always an end of the whole string. |
| 299 | // If the string ends with a new line character, the last line is an |
| 300 | // empty string after this character. |
| 301 | return string_len_; |
| 302 | } else { |
| 303 | return GetPosAfterNewLine(index); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | private: |
| 308 | Handle<FixedArray> ends_array_; |
| 309 | int string_len_; |
| 310 | |
| 311 | int GetPosAfterNewLine(int index) { |
| 312 | return Smi::cast(ends_array_->get(index))->value() + 1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 313 | } |
| 314 | }; |
| 315 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 316 | |
| 317 | // Represents 2 strings as 2 arrays of lines. |
| 318 | class LineArrayCompareInput : public Comparator::Input { |
| 319 | public: |
| 320 | LineArrayCompareInput(Handle<String> s1, Handle<String> s2, |
| 321 | LineEndsWrapper line_ends1, LineEndsWrapper line_ends2) |
| 322 | : s1_(s1), s2_(s2), line_ends1_(line_ends1), line_ends2_(line_ends2) { |
| 323 | } |
| 324 | int getLength1() { |
| 325 | return line_ends1_.length(); |
| 326 | } |
| 327 | int getLength2() { |
| 328 | return line_ends2_.length(); |
| 329 | } |
| 330 | bool equals(int index1, int index2) { |
| 331 | int line_start1 = line_ends1_.GetLineStart(index1); |
| 332 | int line_start2 = line_ends2_.GetLineStart(index2); |
| 333 | int line_end1 = line_ends1_.GetLineEnd(index1); |
| 334 | int line_end2 = line_ends2_.GetLineEnd(index2); |
| 335 | int len1 = line_end1 - line_start1; |
| 336 | int len2 = line_end2 - line_start2; |
| 337 | if (len1 != len2) { |
| 338 | return false; |
| 339 | } |
| 340 | return CompareSubstrings(s1_, line_start1, s2_, line_start2, len1); |
| 341 | } |
| 342 | |
| 343 | private: |
| 344 | Handle<String> s1_; |
| 345 | Handle<String> s2_; |
| 346 | LineEndsWrapper line_ends1_; |
| 347 | LineEndsWrapper line_ends2_; |
| 348 | }; |
| 349 | |
| 350 | |
| 351 | // Stores compare result in JSArray. Each chunk is stored as 3 array elements: |
| 352 | // (pos1_begin, pos1_end, pos2_end). |
| 353 | class LineArrayCompareOutput : public Comparator::Output { |
| 354 | public: |
| 355 | LineArrayCompareOutput(LineEndsWrapper line_ends1, LineEndsWrapper line_ends2) |
| 356 | : array_(Factory::NewJSArray(10)), current_size_(0), |
| 357 | line_ends1_(line_ends1), line_ends2_(line_ends2) { |
| 358 | } |
| 359 | |
| 360 | void AddChunk(int line_pos1, int line_pos2, int line_len1, int line_len2) { |
| 361 | int char_pos1 = line_ends1_.GetLineStart(line_pos1); |
| 362 | int char_pos2 = line_ends2_.GetLineStart(line_pos2); |
| 363 | int char_len1 = line_ends1_.GetLineStart(line_pos1 + line_len1) - char_pos1; |
| 364 | int char_len2 = line_ends2_.GetLineStart(line_pos2 + line_len2) - char_pos2; |
| 365 | |
| 366 | SetElement(array_, current_size_, Handle<Object>(Smi::FromInt(char_pos1))); |
| 367 | SetElement(array_, current_size_ + 1, |
| 368 | Handle<Object>(Smi::FromInt(char_pos1 + char_len1))); |
| 369 | SetElement(array_, current_size_ + 2, |
| 370 | Handle<Object>(Smi::FromInt(char_pos2 + char_len2))); |
| 371 | current_size_ += 3; |
| 372 | } |
| 373 | |
| 374 | Handle<JSArray> GetResult() { |
| 375 | return array_; |
| 376 | } |
| 377 | |
| 378 | private: |
| 379 | Handle<JSArray> array_; |
| 380 | int current_size_; |
| 381 | LineEndsWrapper line_ends1_; |
| 382 | LineEndsWrapper line_ends2_; |
| 383 | }; |
| 384 | |
| 385 | |
| 386 | Handle<JSArray> LiveEdit::CompareStringsLinewise(Handle<String> s1, |
| 387 | Handle<String> s2) { |
| 388 | LineEndsWrapper line_ends1(s1); |
| 389 | LineEndsWrapper line_ends2(s2); |
| 390 | |
| 391 | LineArrayCompareInput input(s1, s2, line_ends1, line_ends2); |
| 392 | LineArrayCompareOutput output(line_ends1, line_ends2); |
| 393 | |
| 394 | Comparator::CalculateDifference(&input, &output); |
| 395 | |
| 396 | return output.GetResult(); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static void CompileScriptForTracker(Handle<Script> script) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 401 | // TODO(635): support extensions. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 402 | PostponeInterruptsScope postpone; |
| 403 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 404 | // Build AST. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 405 | CompilationInfo info(script); |
| 406 | info.MarkAsGlobal(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame^] | 407 | if (ParserApi::Parse(&info)) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 408 | // Compile the code. |
| 409 | LiveEditFunctionTracker tracker(info.function()); |
| 410 | if (Compiler::MakeCodeForLiveEdit(&info)) { |
| 411 | ASSERT(!info.code().is_null()); |
| 412 | tracker.RecordRootFunctionInfo(info.code()); |
| 413 | } else { |
| 414 | Top::StackOverflow(); |
| 415 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 416 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 419 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 420 | // Unwraps JSValue object, returning its field "value" |
| 421 | static Handle<Object> UnwrapJSValue(Handle<JSValue> jsValue) { |
| 422 | return Handle<Object>(jsValue->value()); |
| 423 | } |
| 424 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 425 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 426 | // Wraps any object into a OpaqueReference, that will hide the object |
| 427 | // from JavaScript. |
| 428 | static Handle<JSValue> WrapInJSValue(Object* object) { |
| 429 | Handle<JSFunction> constructor = Top::opaque_reference_function(); |
| 430 | Handle<JSValue> result = |
| 431 | Handle<JSValue>::cast(Factory::NewJSObject(constructor)); |
| 432 | result->set_value(object); |
| 433 | return result; |
| 434 | } |
| 435 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 436 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 437 | // Simple helper class that creates more or less typed structures over |
| 438 | // JSArray object. This is an adhoc method of passing structures from C++ |
| 439 | // to JavaScript. |
| 440 | template<typename S> |
| 441 | class JSArrayBasedStruct { |
| 442 | public: |
| 443 | static S Create() { |
| 444 | Handle<JSArray> array = Factory::NewJSArray(S::kSize_); |
| 445 | return S(array); |
| 446 | } |
| 447 | static S cast(Object* object) { |
| 448 | JSArray* array = JSArray::cast(object); |
| 449 | Handle<JSArray> array_handle(array); |
| 450 | return S(array_handle); |
| 451 | } |
| 452 | explicit JSArrayBasedStruct(Handle<JSArray> array) : array_(array) { |
| 453 | } |
| 454 | Handle<JSArray> GetJSArray() { |
| 455 | return array_; |
| 456 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 457 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 458 | protected: |
| 459 | void SetField(int field_position, Handle<Object> value) { |
| 460 | SetElement(array_, field_position, value); |
| 461 | } |
| 462 | void SetSmiValueField(int field_position, int value) { |
| 463 | SetElement(array_, field_position, Handle<Smi>(Smi::FromInt(value))); |
| 464 | } |
| 465 | Object* GetField(int field_position) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 466 | return array_->GetElementNoExceptionThrown(field_position); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 467 | } |
| 468 | int GetSmiValueField(int field_position) { |
| 469 | Object* res = GetField(field_position); |
| 470 | return Smi::cast(res)->value(); |
| 471 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 472 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 473 | private: |
| 474 | Handle<JSArray> array_; |
| 475 | }; |
| 476 | |
| 477 | |
| 478 | // Represents some function compilation details. This structure will be used |
| 479 | // from JavaScript. It contains Code object, which is kept wrapped |
| 480 | // into a BlindReference for sanitizing reasons. |
| 481 | class FunctionInfoWrapper : public JSArrayBasedStruct<FunctionInfoWrapper> { |
| 482 | public: |
| 483 | explicit FunctionInfoWrapper(Handle<JSArray> array) |
| 484 | : JSArrayBasedStruct<FunctionInfoWrapper>(array) { |
| 485 | } |
| 486 | void SetInitialProperties(Handle<String> name, int start_position, |
| 487 | int end_position, int param_num, int parent_index) { |
| 488 | HandleScope scope; |
| 489 | this->SetField(kFunctionNameOffset_, name); |
| 490 | this->SetSmiValueField(kStartPositionOffset_, start_position); |
| 491 | this->SetSmiValueField(kEndPositionOffset_, end_position); |
| 492 | this->SetSmiValueField(kParamNumOffset_, param_num); |
| 493 | this->SetSmiValueField(kParentIndexOffset_, parent_index); |
| 494 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 495 | void SetFunctionCode(Handle<Code> function_code, |
| 496 | Handle<Object> code_scope_info) { |
| 497 | Handle<JSValue> code_wrapper = WrapInJSValue(*function_code); |
| 498 | this->SetField(kCodeOffset_, code_wrapper); |
| 499 | |
| 500 | Handle<JSValue> scope_wrapper = WrapInJSValue(*code_scope_info); |
| 501 | this->SetField(kCodeScopeInfoOffset_, scope_wrapper); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 502 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 503 | void SetOuterScopeInfo(Handle<Object> scope_info_array) { |
| 504 | this->SetField(kOuterScopeInfoOffset_, scope_info_array); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 505 | } |
| 506 | void SetSharedFunctionInfo(Handle<SharedFunctionInfo> info) { |
| 507 | Handle<JSValue> info_holder = WrapInJSValue(*info); |
| 508 | this->SetField(kSharedFunctionInfoOffset_, info_holder); |
| 509 | } |
| 510 | int GetParentIndex() { |
| 511 | return this->GetSmiValueField(kParentIndexOffset_); |
| 512 | } |
| 513 | Handle<Code> GetFunctionCode() { |
| 514 | Handle<Object> raw_result = UnwrapJSValue(Handle<JSValue>( |
| 515 | JSValue::cast(this->GetField(kCodeOffset_)))); |
| 516 | return Handle<Code>::cast(raw_result); |
| 517 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 518 | Handle<Object> GetCodeScopeInfo() { |
| 519 | Handle<Object> raw_result = UnwrapJSValue(Handle<JSValue>( |
| 520 | JSValue::cast(this->GetField(kCodeScopeInfoOffset_)))); |
| 521 | return raw_result; |
| 522 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 523 | int GetStartPosition() { |
| 524 | return this->GetSmiValueField(kStartPositionOffset_); |
| 525 | } |
| 526 | int GetEndPosition() { |
| 527 | return this->GetSmiValueField(kEndPositionOffset_); |
| 528 | } |
| 529 | |
| 530 | private: |
| 531 | static const int kFunctionNameOffset_ = 0; |
| 532 | static const int kStartPositionOffset_ = 1; |
| 533 | static const int kEndPositionOffset_ = 2; |
| 534 | static const int kParamNumOffset_ = 3; |
| 535 | static const int kCodeOffset_ = 4; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 536 | static const int kCodeScopeInfoOffset_ = 5; |
| 537 | static const int kOuterScopeInfoOffset_ = 6; |
| 538 | static const int kParentIndexOffset_ = 7; |
| 539 | static const int kSharedFunctionInfoOffset_ = 8; |
| 540 | static const int kSize_ = 9; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 541 | |
| 542 | friend class JSArrayBasedStruct<FunctionInfoWrapper>; |
| 543 | }; |
| 544 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 545 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 546 | // Wraps SharedFunctionInfo along with some of its fields for passing it |
| 547 | // back to JavaScript. SharedFunctionInfo object itself is additionally |
| 548 | // wrapped into BlindReference for sanitizing reasons. |
| 549 | class SharedInfoWrapper : public JSArrayBasedStruct<SharedInfoWrapper> { |
| 550 | public: |
| 551 | static bool IsInstance(Handle<JSArray> array) { |
| 552 | return array->length() == Smi::FromInt(kSize_) && |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 553 | array->GetElementNoExceptionThrown(kSharedInfoOffset_)->IsJSValue(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | explicit SharedInfoWrapper(Handle<JSArray> array) |
| 557 | : JSArrayBasedStruct<SharedInfoWrapper>(array) { |
| 558 | } |
| 559 | |
| 560 | void SetProperties(Handle<String> name, int start_position, int end_position, |
| 561 | Handle<SharedFunctionInfo> info) { |
| 562 | HandleScope scope; |
| 563 | this->SetField(kFunctionNameOffset_, name); |
| 564 | Handle<JSValue> info_holder = WrapInJSValue(*info); |
| 565 | this->SetField(kSharedInfoOffset_, info_holder); |
| 566 | this->SetSmiValueField(kStartPositionOffset_, start_position); |
| 567 | this->SetSmiValueField(kEndPositionOffset_, end_position); |
| 568 | } |
| 569 | Handle<SharedFunctionInfo> GetInfo() { |
| 570 | Object* element = this->GetField(kSharedInfoOffset_); |
| 571 | Handle<JSValue> value_wrapper(JSValue::cast(element)); |
| 572 | Handle<Object> raw_result = UnwrapJSValue(value_wrapper); |
| 573 | return Handle<SharedFunctionInfo>::cast(raw_result); |
| 574 | } |
| 575 | |
| 576 | private: |
| 577 | static const int kFunctionNameOffset_ = 0; |
| 578 | static const int kStartPositionOffset_ = 1; |
| 579 | static const int kEndPositionOffset_ = 2; |
| 580 | static const int kSharedInfoOffset_ = 3; |
| 581 | static const int kSize_ = 4; |
| 582 | |
| 583 | friend class JSArrayBasedStruct<SharedInfoWrapper>; |
| 584 | }; |
| 585 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 586 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 587 | class FunctionInfoListener { |
| 588 | public: |
| 589 | FunctionInfoListener() { |
| 590 | current_parent_index_ = -1; |
| 591 | len_ = 0; |
| 592 | result_ = Factory::NewJSArray(10); |
| 593 | } |
| 594 | |
| 595 | void FunctionStarted(FunctionLiteral* fun) { |
| 596 | HandleScope scope; |
| 597 | FunctionInfoWrapper info = FunctionInfoWrapper::Create(); |
| 598 | info.SetInitialProperties(fun->name(), fun->start_position(), |
| 599 | fun->end_position(), fun->num_parameters(), |
| 600 | current_parent_index_); |
| 601 | current_parent_index_ = len_; |
| 602 | SetElement(result_, len_, info.GetJSArray()); |
| 603 | len_++; |
| 604 | } |
| 605 | |
| 606 | void FunctionDone() { |
| 607 | HandleScope scope; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 608 | Object* element = |
| 609 | result_->GetElementNoExceptionThrown(current_parent_index_); |
| 610 | FunctionInfoWrapper info = FunctionInfoWrapper::cast(element); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 611 | current_parent_index_ = info.GetParentIndex(); |
| 612 | } |
| 613 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 614 | // Saves only function code, because for a script function we |
| 615 | // may never create a SharedFunctionInfo object. |
| 616 | void FunctionCode(Handle<Code> function_code) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 617 | Object* element = |
| 618 | result_->GetElementNoExceptionThrown(current_parent_index_); |
| 619 | FunctionInfoWrapper info = FunctionInfoWrapper::cast(element); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 620 | info.SetFunctionCode(function_code, Handle<Object>(Heap::null_value())); |
| 621 | } |
| 622 | |
| 623 | // Saves full information about a function: its code, its scope info |
| 624 | // and a SharedFunctionInfo object. |
| 625 | void FunctionInfo(Handle<SharedFunctionInfo> shared, Scope* scope) { |
| 626 | if (!shared->IsSharedFunctionInfo()) { |
| 627 | return; |
| 628 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 629 | Object* element = |
| 630 | result_->GetElementNoExceptionThrown(current_parent_index_); |
| 631 | FunctionInfoWrapper info = FunctionInfoWrapper::cast(element); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 632 | info.SetFunctionCode(Handle<Code>(shared->code()), |
| 633 | Handle<Object>(shared->scope_info())); |
| 634 | info.SetSharedFunctionInfo(shared); |
| 635 | |
| 636 | Handle<Object> scope_info_list(SerializeFunctionScope(scope)); |
| 637 | info.SetOuterScopeInfo(scope_info_list); |
| 638 | } |
| 639 | |
| 640 | Handle<JSArray> GetResult() { return result_; } |
| 641 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 642 | private: |
| 643 | Object* SerializeFunctionScope(Scope* scope) { |
| 644 | HandleScope handle_scope; |
| 645 | |
| 646 | Handle<JSArray> scope_info_list = Factory::NewJSArray(10); |
| 647 | int scope_info_length = 0; |
| 648 | |
| 649 | // Saves some description of scope. It stores name and indexes of |
| 650 | // variables in the whole scope chain. Null-named slots delimit |
| 651 | // scopes of this chain. |
| 652 | Scope* outer_scope = scope->outer_scope(); |
| 653 | if (outer_scope == NULL) { |
| 654 | return Heap::undefined_value(); |
| 655 | } |
| 656 | do { |
| 657 | ZoneList<Variable*> list(10); |
| 658 | outer_scope->CollectUsedVariables(&list); |
| 659 | int j = 0; |
| 660 | for (int i = 0; i < list.length(); i++) { |
| 661 | Variable* var1 = list[i]; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 662 | Slot* slot = var1->AsSlot(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 663 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
| 664 | if (j != i) { |
| 665 | list[j] = var1; |
| 666 | } |
| 667 | j++; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // Sort it. |
| 672 | for (int k = 1; k < j; k++) { |
| 673 | int l = k; |
| 674 | for (int m = k + 1; m < j; m++) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 675 | if (list[l]->AsSlot()->index() > list[m]->AsSlot()->index()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 676 | l = m; |
| 677 | } |
| 678 | } |
| 679 | list[k] = list[l]; |
| 680 | } |
| 681 | for (int i = 0; i < j; i++) { |
| 682 | SetElement(scope_info_list, scope_info_length, list[i]->name()); |
| 683 | scope_info_length++; |
| 684 | SetElement(scope_info_list, scope_info_length, |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 685 | Handle<Smi>(Smi::FromInt(list[i]->AsSlot()->index()))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 686 | scope_info_length++; |
| 687 | } |
| 688 | SetElement(scope_info_list, scope_info_length, |
| 689 | Handle<Object>(Heap::null_value())); |
| 690 | scope_info_length++; |
| 691 | |
| 692 | outer_scope = outer_scope->outer_scope(); |
| 693 | } while (outer_scope != NULL); |
| 694 | |
| 695 | return *scope_info_list; |
| 696 | } |
| 697 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 698 | Handle<JSArray> result_; |
| 699 | int len_; |
| 700 | int current_parent_index_; |
| 701 | }; |
| 702 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 703 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 704 | static FunctionInfoListener* active_function_info_listener = NULL; |
| 705 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 706 | JSArray* LiveEdit::GatherCompileInfo(Handle<Script> script, |
| 707 | Handle<String> source) { |
| 708 | CompilationZoneScope zone_scope(DELETE_ON_EXIT); |
| 709 | |
| 710 | FunctionInfoListener listener; |
| 711 | Handle<Object> original_source = Handle<Object>(script->source()); |
| 712 | script->set_source(*source); |
| 713 | active_function_info_listener = &listener; |
| 714 | CompileScriptForTracker(script); |
| 715 | active_function_info_listener = NULL; |
| 716 | script->set_source(*original_source); |
| 717 | |
| 718 | return *(listener.GetResult()); |
| 719 | } |
| 720 | |
| 721 | |
| 722 | void LiveEdit::WrapSharedFunctionInfos(Handle<JSArray> array) { |
| 723 | HandleScope scope; |
| 724 | int len = Smi::cast(array->length())->value(); |
| 725 | for (int i = 0; i < len; i++) { |
| 726 | Handle<SharedFunctionInfo> info( |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 727 | SharedFunctionInfo::cast(array->GetElementNoExceptionThrown(i))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 728 | SharedInfoWrapper info_wrapper = SharedInfoWrapper::Create(); |
| 729 | Handle<String> name_handle(String::cast(info->name())); |
| 730 | info_wrapper.SetProperties(name_handle, info->start_position(), |
| 731 | info->end_position(), info); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 732 | SetElement(array, i, info_wrapper.GetJSArray()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | |
| 737 | // Visitor that collects all references to a particular code object, |
| 738 | // including "CODE_TARGET" references in other code objects. |
| 739 | // It works in context of ZoneScope. |
| 740 | class ReferenceCollectorVisitor : public ObjectVisitor { |
| 741 | public: |
| 742 | explicit ReferenceCollectorVisitor(Code* original) |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 743 | : original_(original), rvalues_(10), reloc_infos_(10), code_entries_(10) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | virtual void VisitPointers(Object** start, Object** end) { |
| 747 | for (Object** p = start; p < end; p++) { |
| 748 | if (*p == original_) { |
| 749 | rvalues_.Add(p); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 754 | virtual void VisitCodeEntry(Address entry) { |
| 755 | if (Code::GetObjectFromEntryAddress(entry) == original_) { |
| 756 | code_entries_.Add(entry); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | virtual void VisitCodeTarget(RelocInfo* rinfo) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 761 | if (RelocInfo::IsCodeTarget(rinfo->rmode()) && |
| 762 | Code::GetCodeFromTargetAddress(rinfo->target_address()) == original_) { |
| 763 | reloc_infos_.Add(*rinfo); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | virtual void VisitDebugTarget(RelocInfo* rinfo) { |
| 768 | VisitCodeTarget(rinfo); |
| 769 | } |
| 770 | |
| 771 | // Post-visiting method that iterates over all collected references and |
| 772 | // modifies them. |
| 773 | void Replace(Code* substitution) { |
| 774 | for (int i = 0; i < rvalues_.length(); i++) { |
| 775 | *(rvalues_[i]) = substitution; |
| 776 | } |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 777 | Address substitution_entry = substitution->instruction_start(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 778 | for (int i = 0; i < reloc_infos_.length(); i++) { |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 779 | reloc_infos_[i].set_target_address(substitution_entry); |
| 780 | } |
| 781 | for (int i = 0; i < code_entries_.length(); i++) { |
| 782 | Address entry = code_entries_[i]; |
| 783 | Memory::Address_at(entry) = substitution_entry; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
| 787 | private: |
| 788 | Code* original_; |
| 789 | ZoneList<Object**> rvalues_; |
| 790 | ZoneList<RelocInfo> reloc_infos_; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 791 | ZoneList<Address> code_entries_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 792 | }; |
| 793 | |
| 794 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 795 | // Finds all references to original and replaces them with substitution. |
| 796 | static void ReplaceCodeObject(Code* original, Code* substitution) { |
| 797 | ASSERT(!Heap::InNewSpace(substitution)); |
| 798 | |
| 799 | AssertNoAllocation no_allocations_please; |
| 800 | |
| 801 | // A zone scope for ReferenceCollectorVisitor. |
| 802 | ZoneScope scope(DELETE_ON_EXIT); |
| 803 | |
| 804 | ReferenceCollectorVisitor visitor(original); |
| 805 | |
| 806 | // Iterate over all roots. Stack frames may have pointer into original code, |
| 807 | // so temporary replace the pointers with offset numbers |
| 808 | // in prologue/epilogue. |
| 809 | { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 810 | Heap::IterateStrongRoots(&visitor, VISIT_ALL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | // Now iterate over all pointers of all objects, including code_target |
| 814 | // implicit pointers. |
| 815 | HeapIterator iterator; |
| 816 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 817 | obj->Iterate(&visitor); |
| 818 | } |
| 819 | |
| 820 | visitor.Replace(substitution); |
| 821 | } |
| 822 | |
| 823 | |
| 824 | // Check whether the code is natural function code (not a lazy-compile stub |
| 825 | // code). |
| 826 | static bool IsJSFunctionCode(Code* code) { |
| 827 | return code->kind() == Code::FUNCTION; |
| 828 | } |
| 829 | |
| 830 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 831 | MaybeObject* LiveEdit::ReplaceFunctionCode( |
| 832 | Handle<JSArray> new_compile_info_array, |
| 833 | Handle<JSArray> shared_info_array) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 834 | HandleScope scope; |
| 835 | |
| 836 | if (!SharedInfoWrapper::IsInstance(shared_info_array)) { |
| 837 | return Top::ThrowIllegalOperation(); |
| 838 | } |
| 839 | |
| 840 | FunctionInfoWrapper compile_info_wrapper(new_compile_info_array); |
| 841 | SharedInfoWrapper shared_info_wrapper(shared_info_array); |
| 842 | |
| 843 | Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); |
| 844 | |
| 845 | if (IsJSFunctionCode(shared_info->code())) { |
| 846 | ReplaceCodeObject(shared_info->code(), |
| 847 | *(compile_info_wrapper.GetFunctionCode())); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 848 | Handle<Object> code_scope_info = compile_info_wrapper.GetCodeScopeInfo(); |
| 849 | if (code_scope_info->IsFixedArray()) { |
| 850 | shared_info->set_scope_info(SerializedScopeInfo::cast(*code_scope_info)); |
| 851 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | if (shared_info->debug_info()->IsDebugInfo()) { |
| 855 | Handle<DebugInfo> debug_info(DebugInfo::cast(shared_info->debug_info())); |
| 856 | Handle<Code> new_original_code = |
| 857 | Factory::CopyCode(compile_info_wrapper.GetFunctionCode()); |
| 858 | debug_info->set_original_code(*new_original_code); |
| 859 | } |
| 860 | |
| 861 | shared_info->set_start_position(compile_info_wrapper.GetStartPosition()); |
| 862 | shared_info->set_end_position(compile_info_wrapper.GetEndPosition()); |
| 863 | |
| 864 | shared_info->set_construct_stub( |
| 865 | Builtins::builtin(Builtins::JSConstructStubGeneric)); |
| 866 | |
| 867 | return Heap::undefined_value(); |
| 868 | } |
| 869 | |
| 870 | |
| 871 | // TODO(635): Eval caches its scripts (same text -- same compiled info). |
| 872 | // Make sure we clear such caches. |
| 873 | void LiveEdit::SetFunctionScript(Handle<JSValue> function_wrapper, |
| 874 | Handle<Object> script_handle) { |
| 875 | Handle<SharedFunctionInfo> shared_info = |
| 876 | Handle<SharedFunctionInfo>::cast(UnwrapJSValue(function_wrapper)); |
| 877 | shared_info->set_script(*script_handle); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | // For a script text change (defined as position_change_array), translates |
| 882 | // position in unchanged text to position in changed text. |
| 883 | // Text change is a set of non-overlapping regions in text, that have changed |
| 884 | // their contents and length. It is specified as array of groups of 3 numbers: |
| 885 | // (change_begin, change_end, change_end_new_position). |
| 886 | // Each group describes a change in text; groups are sorted by change_begin. |
| 887 | // Only position in text beyond any changes may be successfully translated. |
| 888 | // If a positions is inside some region that changed, result is currently |
| 889 | // undefined. |
| 890 | static int TranslatePosition(int original_position, |
| 891 | Handle<JSArray> position_change_array) { |
| 892 | int position_diff = 0; |
| 893 | int array_len = Smi::cast(position_change_array->length())->value(); |
| 894 | // TODO(635): binary search may be used here |
| 895 | for (int i = 0; i < array_len; i += 3) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 896 | Object* element = position_change_array->GetElementNoExceptionThrown(i); |
| 897 | int chunk_start = Smi::cast(element)->value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 898 | if (original_position < chunk_start) { |
| 899 | break; |
| 900 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 901 | element = position_change_array->GetElementNoExceptionThrown(i + 1); |
| 902 | int chunk_end = Smi::cast(element)->value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 903 | // Position mustn't be inside a chunk. |
| 904 | ASSERT(original_position >= chunk_end); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 905 | element = position_change_array->GetElementNoExceptionThrown(i + 2); |
| 906 | int chunk_changed_end = Smi::cast(element)->value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 907 | position_diff = chunk_changed_end - chunk_end; |
| 908 | } |
| 909 | |
| 910 | return original_position + position_diff; |
| 911 | } |
| 912 | |
| 913 | |
| 914 | // Auto-growing buffer for writing relocation info code section. This buffer |
| 915 | // is a simplified version of buffer from Assembler. Unlike Assembler, this |
| 916 | // class is platform-independent and it works without dealing with instructions. |
| 917 | // As specified by RelocInfo format, the buffer is filled in reversed order: |
| 918 | // from upper to lower addresses. |
| 919 | // It uses NewArray/DeleteArray for memory management. |
| 920 | class RelocInfoBuffer { |
| 921 | public: |
| 922 | RelocInfoBuffer(int buffer_initial_capicity, byte* pc) { |
| 923 | buffer_size_ = buffer_initial_capicity + kBufferGap; |
| 924 | buffer_ = NewArray<byte>(buffer_size_); |
| 925 | |
| 926 | reloc_info_writer_.Reposition(buffer_ + buffer_size_, pc); |
| 927 | } |
| 928 | ~RelocInfoBuffer() { |
| 929 | DeleteArray(buffer_); |
| 930 | } |
| 931 | |
| 932 | // As specified by RelocInfo format, the buffer is filled in reversed order: |
| 933 | // from upper to lower addresses. |
| 934 | void Write(const RelocInfo* rinfo) { |
| 935 | if (buffer_ + kBufferGap >= reloc_info_writer_.pos()) { |
| 936 | Grow(); |
| 937 | } |
| 938 | reloc_info_writer_.Write(rinfo); |
| 939 | } |
| 940 | |
| 941 | Vector<byte> GetResult() { |
| 942 | // Return the bytes from pos up to end of buffer. |
| 943 | int result_size = |
| 944 | static_cast<int>((buffer_ + buffer_size_) - reloc_info_writer_.pos()); |
| 945 | return Vector<byte>(reloc_info_writer_.pos(), result_size); |
| 946 | } |
| 947 | |
| 948 | private: |
| 949 | void Grow() { |
| 950 | // Compute new buffer size. |
| 951 | int new_buffer_size; |
| 952 | if (buffer_size_ < 2 * KB) { |
| 953 | new_buffer_size = 4 * KB; |
| 954 | } else { |
| 955 | new_buffer_size = 2 * buffer_size_; |
| 956 | } |
| 957 | // Some internal data structures overflow for very large buffers, |
| 958 | // they must ensure that kMaximalBufferSize is not too large. |
| 959 | if (new_buffer_size > kMaximalBufferSize) { |
| 960 | V8::FatalProcessOutOfMemory("RelocInfoBuffer::GrowBuffer"); |
| 961 | } |
| 962 | |
| 963 | // Setup new buffer. |
| 964 | byte* new_buffer = NewArray<byte>(new_buffer_size); |
| 965 | |
| 966 | // Copy the data. |
| 967 | int curently_used_size = |
| 968 | static_cast<int>(buffer_ + buffer_size_ - reloc_info_writer_.pos()); |
| 969 | memmove(new_buffer + new_buffer_size - curently_used_size, |
| 970 | reloc_info_writer_.pos(), curently_used_size); |
| 971 | |
| 972 | reloc_info_writer_.Reposition( |
| 973 | new_buffer + new_buffer_size - curently_used_size, |
| 974 | reloc_info_writer_.last_pc()); |
| 975 | |
| 976 | DeleteArray(buffer_); |
| 977 | buffer_ = new_buffer; |
| 978 | buffer_size_ = new_buffer_size; |
| 979 | } |
| 980 | |
| 981 | RelocInfoWriter reloc_info_writer_; |
| 982 | byte* buffer_; |
| 983 | int buffer_size_; |
| 984 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 985 | static const int kBufferGap = RelocInfoWriter::kMaxSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 986 | static const int kMaximalBufferSize = 512*MB; |
| 987 | }; |
| 988 | |
| 989 | // Patch positions in code (changes relocation info section) and possibly |
| 990 | // returns new instance of code. |
| 991 | static Handle<Code> PatchPositionsInCode(Handle<Code> code, |
| 992 | Handle<JSArray> position_change_array) { |
| 993 | |
| 994 | RelocInfoBuffer buffer_writer(code->relocation_size(), |
| 995 | code->instruction_start()); |
| 996 | |
| 997 | { |
| 998 | AssertNoAllocation no_allocations_please; |
| 999 | for (RelocIterator it(*code); !it.done(); it.next()) { |
| 1000 | RelocInfo* rinfo = it.rinfo(); |
| 1001 | if (RelocInfo::IsPosition(rinfo->rmode())) { |
| 1002 | int position = static_cast<int>(rinfo->data()); |
| 1003 | int new_position = TranslatePosition(position, |
| 1004 | position_change_array); |
| 1005 | if (position != new_position) { |
| 1006 | RelocInfo info_copy(rinfo->pc(), rinfo->rmode(), new_position); |
| 1007 | buffer_writer.Write(&info_copy); |
| 1008 | continue; |
| 1009 | } |
| 1010 | } |
| 1011 | buffer_writer.Write(it.rinfo()); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | Vector<byte> buffer = buffer_writer.GetResult(); |
| 1016 | |
| 1017 | if (buffer.length() == code->relocation_size()) { |
| 1018 | // Simply patch relocation area of code. |
| 1019 | memcpy(code->relocation_start(), buffer.start(), buffer.length()); |
| 1020 | return code; |
| 1021 | } else { |
| 1022 | // Relocation info section now has different size. We cannot simply |
| 1023 | // rewrite it inside code object. Instead we have to create a new |
| 1024 | // code object. |
| 1025 | Handle<Code> result(Factory::CopyCode(code, buffer)); |
| 1026 | return result; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1031 | MaybeObject* LiveEdit::PatchFunctionPositions( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1032 | Handle<JSArray> shared_info_array, Handle<JSArray> position_change_array) { |
| 1033 | |
| 1034 | if (!SharedInfoWrapper::IsInstance(shared_info_array)) { |
| 1035 | return Top::ThrowIllegalOperation(); |
| 1036 | } |
| 1037 | |
| 1038 | SharedInfoWrapper shared_info_wrapper(shared_info_array); |
| 1039 | Handle<SharedFunctionInfo> info = shared_info_wrapper.GetInfo(); |
| 1040 | |
| 1041 | int old_function_start = info->start_position(); |
| 1042 | int new_function_start = TranslatePosition(old_function_start, |
| 1043 | position_change_array); |
| 1044 | info->set_start_position(new_function_start); |
| 1045 | info->set_end_position(TranslatePosition(info->end_position(), |
| 1046 | position_change_array)); |
| 1047 | |
| 1048 | info->set_function_token_position( |
| 1049 | TranslatePosition(info->function_token_position(), |
| 1050 | position_change_array)); |
| 1051 | |
| 1052 | if (IsJSFunctionCode(info->code())) { |
| 1053 | // Patch relocation info section of the code. |
| 1054 | Handle<Code> patched_code = PatchPositionsInCode(Handle<Code>(info->code()), |
| 1055 | position_change_array); |
| 1056 | if (*patched_code != info->code()) { |
| 1057 | // Replace all references to the code across the heap. In particular, |
| 1058 | // some stubs may refer to this code and this code may be being executed |
| 1059 | // on stack (it is safe to substitute the code object on stack, because |
| 1060 | // we only change the structure of rinfo and leave instructions |
| 1061 | // untouched). |
| 1062 | ReplaceCodeObject(info->code(), *patched_code); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | return Heap::undefined_value(); |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | static Handle<Script> CreateScriptCopy(Handle<Script> original) { |
| 1071 | Handle<String> original_source(String::cast(original->source())); |
| 1072 | |
| 1073 | Handle<Script> copy = Factory::NewScript(original_source); |
| 1074 | |
| 1075 | copy->set_name(original->name()); |
| 1076 | copy->set_line_offset(original->line_offset()); |
| 1077 | copy->set_column_offset(original->column_offset()); |
| 1078 | copy->set_data(original->data()); |
| 1079 | copy->set_type(original->type()); |
| 1080 | copy->set_context_data(original->context_data()); |
| 1081 | copy->set_compilation_type(original->compilation_type()); |
| 1082 | copy->set_eval_from_shared(original->eval_from_shared()); |
| 1083 | copy->set_eval_from_instructions_offset( |
| 1084 | original->eval_from_instructions_offset()); |
| 1085 | |
| 1086 | return copy; |
| 1087 | } |
| 1088 | |
| 1089 | |
| 1090 | Object* LiveEdit::ChangeScriptSource(Handle<Script> original_script, |
| 1091 | Handle<String> new_source, |
| 1092 | Handle<Object> old_script_name) { |
| 1093 | Handle<Object> old_script_object; |
| 1094 | if (old_script_name->IsString()) { |
| 1095 | Handle<Script> old_script = CreateScriptCopy(original_script); |
| 1096 | old_script->set_name(String::cast(*old_script_name)); |
| 1097 | old_script_object = old_script; |
| 1098 | Debugger::OnAfterCompile(old_script, Debugger::SEND_WHEN_DEBUGGING); |
| 1099 | } else { |
| 1100 | old_script_object = Handle<Object>(Heap::null_value()); |
| 1101 | } |
| 1102 | |
| 1103 | original_script->set_source(*new_source); |
| 1104 | |
| 1105 | // Drop line ends so that they will be recalculated. |
| 1106 | original_script->set_line_ends(Heap::undefined_value()); |
| 1107 | |
| 1108 | return *old_script_object; |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | void LiveEdit::ReplaceRefToNestedFunction( |
| 1114 | Handle<JSValue> parent_function_wrapper, |
| 1115 | Handle<JSValue> orig_function_wrapper, |
| 1116 | Handle<JSValue> subst_function_wrapper) { |
| 1117 | |
| 1118 | Handle<SharedFunctionInfo> parent_shared = |
| 1119 | Handle<SharedFunctionInfo>::cast(UnwrapJSValue(parent_function_wrapper)); |
| 1120 | Handle<SharedFunctionInfo> orig_shared = |
| 1121 | Handle<SharedFunctionInfo>::cast(UnwrapJSValue(orig_function_wrapper)); |
| 1122 | Handle<SharedFunctionInfo> subst_shared = |
| 1123 | Handle<SharedFunctionInfo>::cast(UnwrapJSValue(subst_function_wrapper)); |
| 1124 | |
| 1125 | for (RelocIterator it(parent_shared->code()); !it.done(); it.next()) { |
| 1126 | if (it.rinfo()->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 1127 | if (it.rinfo()->target_object() == *orig_shared) { |
| 1128 | it.rinfo()->set_target_object(*subst_shared); |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | // Check an activation against list of functions. If there is a function |
| 1136 | // that matches, its status in result array is changed to status argument value. |
| 1137 | static bool CheckActivation(Handle<JSArray> shared_info_array, |
| 1138 | Handle<JSArray> result, StackFrame* frame, |
| 1139 | LiveEdit::FunctionPatchabilityStatus status) { |
| 1140 | if (!frame->is_java_script()) { |
| 1141 | return false; |
| 1142 | } |
| 1143 | int len = Smi::cast(shared_info_array->length())->value(); |
| 1144 | for (int i = 0; i < len; i++) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1145 | JSValue* wrapper = |
| 1146 | JSValue::cast(shared_info_array->GetElementNoExceptionThrown(i)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1147 | Handle<SharedFunctionInfo> shared( |
| 1148 | SharedFunctionInfo::cast(wrapper->value())); |
| 1149 | |
| 1150 | if (frame->code() == shared->code()) { |
| 1151 | SetElement(result, i, Handle<Smi>(Smi::FromInt(status))); |
| 1152 | return true; |
| 1153 | } |
| 1154 | } |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | // Iterates over handler chain and removes all elements that are inside |
| 1160 | // frames being dropped. |
| 1161 | static bool FixTryCatchHandler(StackFrame* top_frame, |
| 1162 | StackFrame* bottom_frame) { |
| 1163 | Address* pointer_address = |
| 1164 | &Memory::Address_at(Top::get_address_from_id(Top::k_handler_address)); |
| 1165 | |
| 1166 | while (*pointer_address < top_frame->sp()) { |
| 1167 | pointer_address = &Memory::Address_at(*pointer_address); |
| 1168 | } |
| 1169 | Address* above_frame_address = pointer_address; |
| 1170 | while (*pointer_address < bottom_frame->fp()) { |
| 1171 | pointer_address = &Memory::Address_at(*pointer_address); |
| 1172 | } |
| 1173 | bool change = *above_frame_address != *pointer_address; |
| 1174 | *above_frame_address = *pointer_address; |
| 1175 | return change; |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | // Removes specified range of frames from stack. There may be 1 or more |
| 1180 | // frames in range. Anyway the bottom frame is restarted rather than dropped, |
| 1181 | // and therefore has to be a JavaScript frame. |
| 1182 | // Returns error message or NULL. |
| 1183 | static const char* DropFrames(Vector<StackFrame*> frames, |
| 1184 | int top_frame_index, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1185 | int bottom_js_frame_index, |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1186 | Debug::FrameDropMode* mode, |
| 1187 | Object*** restarter_frame_function_pointer) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1188 | if (!Debug::kFrameDropperSupported) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1189 | return "Stack manipulations are not supported in this architecture."; |
| 1190 | } |
| 1191 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1192 | StackFrame* pre_top_frame = frames[top_frame_index - 1]; |
| 1193 | StackFrame* top_frame = frames[top_frame_index]; |
| 1194 | StackFrame* bottom_js_frame = frames[bottom_js_frame_index]; |
| 1195 | |
| 1196 | ASSERT(bottom_js_frame->is_java_script()); |
| 1197 | |
| 1198 | // Check the nature of the top frame. |
| 1199 | if (pre_top_frame->code()->is_inline_cache_stub() && |
| 1200 | pre_top_frame->code()->ic_state() == DEBUG_BREAK) { |
| 1201 | // OK, we can drop inline cache calls. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1202 | *mode = Debug::FRAME_DROPPED_IN_IC_CALL; |
| 1203 | } else if (pre_top_frame->code() == Debug::debug_break_slot()) { |
| 1204 | // OK, we can drop debug break slot. |
| 1205 | *mode = Debug::FRAME_DROPPED_IN_DEBUG_SLOT_CALL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1206 | } else if (pre_top_frame->code() == |
| 1207 | Builtins::builtin(Builtins::FrameDropper_LiveEdit)) { |
| 1208 | // OK, we can drop our own code. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1209 | *mode = Debug::FRAME_DROPPED_IN_DIRECT_CALL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1210 | } else if (pre_top_frame->code()->kind() == Code::STUB && |
| 1211 | pre_top_frame->code()->major_key()) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1212 | // Entry from our unit tests, it's fine, we support this case. |
| 1213 | *mode = Debug::FRAME_DROPPED_IN_DIRECT_CALL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1214 | } else { |
| 1215 | return "Unknown structure of stack above changing function"; |
| 1216 | } |
| 1217 | |
| 1218 | Address unused_stack_top = top_frame->sp(); |
| 1219 | Address unused_stack_bottom = bottom_js_frame->fp() |
| 1220 | - Debug::kFrameDropperFrameSize * kPointerSize // Size of the new frame. |
| 1221 | + kPointerSize; // Bigger address end is exclusive. |
| 1222 | |
| 1223 | if (unused_stack_top > unused_stack_bottom) { |
| 1224 | return "Not enough space for frame dropper frame"; |
| 1225 | } |
| 1226 | |
| 1227 | // Committing now. After this point we should return only NULL value. |
| 1228 | |
| 1229 | FixTryCatchHandler(pre_top_frame, bottom_js_frame); |
| 1230 | // Make sure FixTryCatchHandler is idempotent. |
| 1231 | ASSERT(!FixTryCatchHandler(pre_top_frame, bottom_js_frame)); |
| 1232 | |
| 1233 | Handle<Code> code(Builtins::builtin(Builtins::FrameDropper_LiveEdit)); |
| 1234 | top_frame->set_pc(code->entry()); |
| 1235 | pre_top_frame->SetCallerFp(bottom_js_frame->fp()); |
| 1236 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1237 | *restarter_frame_function_pointer = |
| 1238 | Debug::SetUpFrameDropperFrame(bottom_js_frame, code); |
| 1239 | |
| 1240 | ASSERT((**restarter_frame_function_pointer)->IsJSFunction()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1241 | |
| 1242 | for (Address a = unused_stack_top; |
| 1243 | a < unused_stack_bottom; |
| 1244 | a += kPointerSize) { |
| 1245 | Memory::Object_at(a) = Smi::FromInt(0); |
| 1246 | } |
| 1247 | |
| 1248 | return NULL; |
| 1249 | } |
| 1250 | |
| 1251 | |
| 1252 | static bool IsDropableFrame(StackFrame* frame) { |
| 1253 | return !frame->is_exit(); |
| 1254 | } |
| 1255 | |
| 1256 | // Fills result array with statuses of functions. Modifies the stack |
| 1257 | // removing all listed function if possible and if do_drop is true. |
| 1258 | static const char* DropActivationsInActiveThread( |
| 1259 | Handle<JSArray> shared_info_array, Handle<JSArray> result, bool do_drop) { |
| 1260 | |
| 1261 | ZoneScope scope(DELETE_ON_EXIT); |
| 1262 | Vector<StackFrame*> frames = CreateStackMap(); |
| 1263 | |
| 1264 | int array_len = Smi::cast(shared_info_array->length())->value(); |
| 1265 | |
| 1266 | int top_frame_index = -1; |
| 1267 | int frame_index = 0; |
| 1268 | for (; frame_index < frames.length(); frame_index++) { |
| 1269 | StackFrame* frame = frames[frame_index]; |
| 1270 | if (frame->id() == Debug::break_frame_id()) { |
| 1271 | top_frame_index = frame_index; |
| 1272 | break; |
| 1273 | } |
| 1274 | if (CheckActivation(shared_info_array, result, frame, |
| 1275 | LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE)) { |
| 1276 | // We are still above break_frame. It is not a target frame, |
| 1277 | // it is a problem. |
| 1278 | return "Debugger mark-up on stack is not found"; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | if (top_frame_index == -1) { |
| 1283 | // We haven't found break frame, but no function is blocking us anyway. |
| 1284 | return NULL; |
| 1285 | } |
| 1286 | |
| 1287 | bool target_frame_found = false; |
| 1288 | int bottom_js_frame_index = top_frame_index; |
| 1289 | bool c_code_found = false; |
| 1290 | |
| 1291 | for (; frame_index < frames.length(); frame_index++) { |
| 1292 | StackFrame* frame = frames[frame_index]; |
| 1293 | if (!IsDropableFrame(frame)) { |
| 1294 | c_code_found = true; |
| 1295 | break; |
| 1296 | } |
| 1297 | if (CheckActivation(shared_info_array, result, frame, |
| 1298 | LiveEdit::FUNCTION_BLOCKED_ON_ACTIVE_STACK)) { |
| 1299 | target_frame_found = true; |
| 1300 | bottom_js_frame_index = frame_index; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | if (c_code_found) { |
| 1305 | // There is a C frames on stack. Check that there are no target frames |
| 1306 | // below them. |
| 1307 | for (; frame_index < frames.length(); frame_index++) { |
| 1308 | StackFrame* frame = frames[frame_index]; |
| 1309 | if (frame->is_java_script()) { |
| 1310 | if (CheckActivation(shared_info_array, result, frame, |
| 1311 | LiveEdit::FUNCTION_BLOCKED_UNDER_NATIVE_CODE)) { |
| 1312 | // Cannot drop frame under C frames. |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | if (!do_drop) { |
| 1320 | // We are in check-only mode. |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | |
| 1324 | if (!target_frame_found) { |
| 1325 | // Nothing to drop. |
| 1326 | return NULL; |
| 1327 | } |
| 1328 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1329 | Debug::FrameDropMode drop_mode = Debug::FRAMES_UNTOUCHED; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1330 | Object** restarter_frame_function_pointer = NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1331 | const char* error_message = DropFrames(frames, top_frame_index, |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1332 | bottom_js_frame_index, &drop_mode, |
| 1333 | &restarter_frame_function_pointer); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1334 | |
| 1335 | if (error_message != NULL) { |
| 1336 | return error_message; |
| 1337 | } |
| 1338 | |
| 1339 | // Adjust break_frame after some frames has been dropped. |
| 1340 | StackFrame::Id new_id = StackFrame::NO_ID; |
| 1341 | for (int i = bottom_js_frame_index + 1; i < frames.length(); i++) { |
| 1342 | if (frames[i]->type() == StackFrame::JAVA_SCRIPT) { |
| 1343 | new_id = frames[i]->id(); |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1347 | Debug::FramesHaveBeenDropped(new_id, drop_mode, |
| 1348 | restarter_frame_function_pointer); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1349 | |
| 1350 | // Replace "blocked on active" with "replaced on active" status. |
| 1351 | for (int i = 0; i < array_len; i++) { |
| 1352 | if (result->GetElement(i) == |
| 1353 | Smi::FromInt(LiveEdit::FUNCTION_BLOCKED_ON_ACTIVE_STACK)) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1354 | Handle<Object> replaced( |
| 1355 | Smi::FromInt(LiveEdit::FUNCTION_REPLACED_ON_ACTIVE_STACK)); |
| 1356 | SetElement(result, i, replaced); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | return NULL; |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | class InactiveThreadActivationsChecker : public ThreadVisitor { |
| 1364 | public: |
| 1365 | InactiveThreadActivationsChecker(Handle<JSArray> shared_info_array, |
| 1366 | Handle<JSArray> result) |
| 1367 | : shared_info_array_(shared_info_array), result_(result), |
| 1368 | has_blocked_functions_(false) { |
| 1369 | } |
| 1370 | void VisitThread(ThreadLocalTop* top) { |
| 1371 | for (StackFrameIterator it(top); !it.done(); it.Advance()) { |
| 1372 | has_blocked_functions_ |= CheckActivation( |
| 1373 | shared_info_array_, result_, it.frame(), |
| 1374 | LiveEdit::FUNCTION_BLOCKED_ON_OTHER_STACK); |
| 1375 | } |
| 1376 | } |
| 1377 | bool HasBlockedFunctions() { |
| 1378 | return has_blocked_functions_; |
| 1379 | } |
| 1380 | |
| 1381 | private: |
| 1382 | Handle<JSArray> shared_info_array_; |
| 1383 | Handle<JSArray> result_; |
| 1384 | bool has_blocked_functions_; |
| 1385 | }; |
| 1386 | |
| 1387 | |
| 1388 | Handle<JSArray> LiveEdit::CheckAndDropActivations( |
| 1389 | Handle<JSArray> shared_info_array, bool do_drop) { |
| 1390 | int len = Smi::cast(shared_info_array->length())->value(); |
| 1391 | |
| 1392 | Handle<JSArray> result = Factory::NewJSArray(len); |
| 1393 | |
| 1394 | // Fill the default values. |
| 1395 | for (int i = 0; i < len; i++) { |
| 1396 | SetElement(result, i, |
| 1397 | Handle<Smi>(Smi::FromInt(FUNCTION_AVAILABLE_FOR_PATCH))); |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | // First check inactive threads. Fail if some functions are blocked there. |
| 1402 | InactiveThreadActivationsChecker inactive_threads_checker(shared_info_array, |
| 1403 | result); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1404 | ThreadManager::IterateArchivedThreads(&inactive_threads_checker); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1405 | if (inactive_threads_checker.HasBlockedFunctions()) { |
| 1406 | return result; |
| 1407 | } |
| 1408 | |
| 1409 | // Try to drop activations from the current stack. |
| 1410 | const char* error_message = |
| 1411 | DropActivationsInActiveThread(shared_info_array, result, do_drop); |
| 1412 | if (error_message != NULL) { |
| 1413 | // Add error message as an array extra element. |
| 1414 | Vector<const char> vector_message(error_message, StrLength(error_message)); |
| 1415 | Handle<String> str = Factory::NewStringFromAscii(vector_message); |
| 1416 | SetElement(result, len, str); |
| 1417 | } |
| 1418 | return result; |
| 1419 | } |
| 1420 | |
| 1421 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1422 | LiveEditFunctionTracker::LiveEditFunctionTracker(FunctionLiteral* fun) { |
| 1423 | if (active_function_info_listener != NULL) { |
| 1424 | active_function_info_listener->FunctionStarted(fun); |
| 1425 | } |
| 1426 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1427 | |
| 1428 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1429 | LiveEditFunctionTracker::~LiveEditFunctionTracker() { |
| 1430 | if (active_function_info_listener != NULL) { |
| 1431 | active_function_info_listener->FunctionDone(); |
| 1432 | } |
| 1433 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1434 | |
| 1435 | |
| 1436 | void LiveEditFunctionTracker::RecordFunctionInfo( |
| 1437 | Handle<SharedFunctionInfo> info, FunctionLiteral* lit) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1438 | if (active_function_info_listener != NULL) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1439 | active_function_info_listener->FunctionInfo(info, lit->scope()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1442 | |
| 1443 | |
| 1444 | void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { |
| 1445 | active_function_info_listener->FunctionCode(code); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1446 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1447 | |
| 1448 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1449 | bool LiveEditFunctionTracker::IsActive() { |
| 1450 | return active_function_info_listener != NULL; |
| 1451 | } |
| 1452 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1453 | |
| 1454 | #else // ENABLE_DEBUGGER_SUPPORT |
| 1455 | |
| 1456 | // This ifdef-else-endif section provides working or stub implementation of |
| 1457 | // LiveEditFunctionTracker. |
| 1458 | LiveEditFunctionTracker::LiveEditFunctionTracker(FunctionLiteral* fun) { |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | LiveEditFunctionTracker::~LiveEditFunctionTracker() { |
| 1463 | } |
| 1464 | |
| 1465 | |
| 1466 | void LiveEditFunctionTracker::RecordFunctionInfo( |
| 1467 | Handle<SharedFunctionInfo> info, FunctionLiteral* lit) { |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { |
| 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | bool LiveEditFunctionTracker::IsActive() { |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
| 1479 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 1480 | |
| 1481 | |
| 1482 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1483 | } } // namespace v8::internal |