OLD | NEW |
(Empty) | |
| 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 #include "../include/v8-preparser.h" |
| 29 #include "globals.h" |
| 30 #include "checks.h" |
| 31 #include "allocation.h" |
| 32 #include "utils.h" |
| 33 #include "list.h" |
| 34 #include "scanner-base.h" |
| 35 #include "preparse-data.h" |
| 36 #include "preparser.h" |
| 37 |
| 38 namespace v8 { |
| 39 namespace internal { |
| 40 |
| 41 // UTF16Buffer based on a v8::UnicodeInputStream. |
| 42 class InputStreamUTF16Buffer : public UTF16Buffer { |
| 43 public: |
| 44 explicit InputStreamUTF16Buffer(UnicodeInputStream* stream) |
| 45 : UTF16Buffer(), |
| 46 stream_(stream) { } |
| 47 |
| 48 virtual ~InputStreamUTF16Buffer() { } |
| 49 |
| 50 virtual void PushBack(uc32 ch) { |
| 51 stream_->PushBack(ch); |
| 52 pos_--; |
| 53 } |
| 54 |
| 55 virtual uc32 Advance() { |
| 56 uc32 result = stream_->Next(); |
| 57 if (result >= 0) pos_++; |
| 58 return result; |
| 59 } |
| 60 |
| 61 virtual void SeekForward(int pos) { |
| 62 // Seeking in the input is not used by preparsing. |
| 63 // It's only used by the real parser based on preparser data. |
| 64 UNIMPLEMENTED(); |
| 65 } |
| 66 |
| 67 private: |
| 68 v8::UnicodeInputStream* const stream_; |
| 69 }; |
| 70 |
| 71 |
| 72 class StandAloneJavaScriptScanner : public JavaScriptScanner { |
| 73 public: |
| 74 void Initialize(UTF16Buffer* source) { |
| 75 source_ = source; |
| 76 literal_flags_ = kLiteralString | kLiteralIdentifier; |
| 77 Init(); |
| 78 // Skip initial whitespace allowing HTML comment ends just like |
| 79 // after a newline and scan first token. |
| 80 has_line_terminator_before_next_ = true; |
| 81 SkipWhiteSpace(); |
| 82 Scan(); |
| 83 } |
| 84 }; |
| 85 |
| 86 |
| 87 // Functions declared by allocation.h |
| 88 |
| 89 void FatalProcessOutOfMemory(const char* reason) { |
| 90 V8_Fatal(__FILE__, __LINE__, reason); |
| 91 } |
| 92 |
| 93 bool EnableSlowAsserts() { return true; } |
| 94 |
| 95 |
| 96 } // namespace internal. |
| 97 |
| 98 |
| 99 UnicodeInputStream::~UnicodeInputStream() { } |
| 100 |
| 101 |
| 102 PreParserData Preparse(UnicodeInputStream* input, size_t max_stack) { |
| 103 internal::InputStreamUTF16Buffer buffer(input); |
| 104 uintptr_t stack_limit = reinterpret_cast<uintptr_t>(&buffer) - max_stack; |
| 105 internal::StandAloneJavaScriptScanner scanner; |
| 106 scanner.Initialize(&buffer); |
| 107 internal::CompleteParserRecorder recorder; |
| 108 preparser::PreParser::PreParseResult result = |
| 109 preparser::PreParser::PreParseProgram(&scanner, |
| 110 &recorder, |
| 111 true, |
| 112 stack_limit); |
| 113 if (result == preparser::PreParser::kPreParseStackOverflow) { |
| 114 return PreParserData::StackOverflow(); |
| 115 } |
| 116 internal::Vector<unsigned> pre_data = recorder.ExtractData(); |
| 117 size_t size = pre_data.length() * sizeof(pre_data[0]); |
| 118 unsigned char* data = reinterpret_cast<unsigned char*>(pre_data.start()); |
| 119 return PreParserData(size, data); |
| 120 } |
| 121 |
| 122 } // namespace v8. |
| 123 |
| 124 |
| 125 // Used by ASSERT macros and other immediate exits. |
| 126 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { |
| 127 exit(EXIT_FAILURE); |
| 128 } |
OLD | NEW |