• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 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 
30 #include "globals.h"
31 #include "checks.h"
32 #include "allocation.h"
33 #include "utils.h"
34 #include "list.h"
35 #include "scanner-base.h"
36 #include "preparse-data.h"
37 #include "preparser.h"
38 
39 namespace v8 {
40 namespace internal {
41 
42 // UTF16Buffer based on a v8::UnicodeInputStream.
43 class InputStreamUTF16Buffer : public UC16CharacterStream {
44  public:
45   /* The InputStreamUTF16Buffer maintains an internal buffer
46    * that is filled in chunks from the UC16CharacterStream.
47    * It also maintains unlimited pushback capability, but optimized
48    * for small pushbacks.
49    * The pushback_buffer_ pointer points to the limit of pushbacks
50    * in the current buffer. There is room for a few pushback'ed chars before
51    * the buffer containing the most recently read chunk. If this is overflowed,
52    * an external buffer is allocated/reused to hold further pushbacks, and
53    * pushback_buffer_ and buffer_cursor_/buffer_end_ now points to the
54    * new buffer. When this buffer is read to the end again, the cursor is
55    * switched back to the internal buffer
56    */
InputStreamUTF16Buffer(v8::UnicodeInputStream * stream)57   explicit InputStreamUTF16Buffer(v8::UnicodeInputStream* stream)
58       : UC16CharacterStream(),
59         stream_(stream),
60         pushback_buffer_(buffer_),
61         pushback_buffer_end_cache_(NULL),
62         pushback_buffer_backing_(NULL),
63         pushback_buffer_backing_size_(0) {
64     buffer_cursor_ = buffer_end_ = buffer_ + kPushBackSize;
65   }
66 
~InputStreamUTF16Buffer()67   virtual ~InputStreamUTF16Buffer() {
68     if (pushback_buffer_backing_ != NULL) {
69       DeleteArray(pushback_buffer_backing_);
70     }
71   }
72 
PushBack(uc32 ch)73   virtual void PushBack(uc32 ch) {
74     ASSERT(pos_ > 0);
75     if (ch == kEndOfInput) {
76       pos_--;
77       return;
78     }
79     if (buffer_cursor_ <= pushback_buffer_) {
80       // No more room in the current buffer to do pushbacks.
81       if (pushback_buffer_end_cache_ == NULL) {
82         // We have overflowed the pushback space at the beginning of buffer_.
83         // Switch to using a separate allocated pushback buffer.
84         if (pushback_buffer_backing_ == NULL) {
85           // Allocate a buffer the first time we need it.
86           pushback_buffer_backing_ = NewArray<uc16>(kPushBackSize);
87           pushback_buffer_backing_size_ = kPushBackSize;
88         }
89         pushback_buffer_ = pushback_buffer_backing_;
90         pushback_buffer_end_cache_ = buffer_end_;
91         buffer_end_ = pushback_buffer_backing_ + pushback_buffer_backing_size_;
92         buffer_cursor_ = buffer_end_ - 1;
93       } else {
94         // Hit the bottom of the allocated pushback buffer.
95         // Double the buffer and continue.
96         uc16* new_buffer = NewArray<uc16>(pushback_buffer_backing_size_ * 2);
97         memcpy(new_buffer + pushback_buffer_backing_size_,
98                pushback_buffer_backing_,
99                pushback_buffer_backing_size_);
100         DeleteArray(pushback_buffer_backing_);
101         buffer_cursor_ = new_buffer + pushback_buffer_backing_size_;
102         pushback_buffer_backing_ = pushback_buffer_ = new_buffer;
103         buffer_end_ = pushback_buffer_backing_ + pushback_buffer_backing_size_;
104       }
105     }
106     pushback_buffer_[buffer_cursor_ - pushback_buffer_- 1] =
107         static_cast<uc16>(ch);
108     pos_--;
109   }
110 
111  protected:
ReadBlock()112   virtual bool ReadBlock() {
113     if (pushback_buffer_end_cache_ != NULL) {
114       buffer_cursor_ = buffer_;
115       buffer_end_ = pushback_buffer_end_cache_;
116       pushback_buffer_end_cache_ = NULL;
117       return buffer_end_ > buffer_cursor_;
118     }
119     // Copy the top of the buffer into the pushback area.
120     int32_t value;
121     uc16* buffer_start = buffer_ + kPushBackSize;
122     buffer_cursor_ = buffer_end_ = buffer_start;
123     while ((value = stream_->Next()) >= 0) {
124       if (value > static_cast<int32_t>(unibrow::Utf8::kMaxThreeByteChar)) {
125         value = unibrow::Utf8::kBadChar;
126       }
127       // buffer_end_ is a const pointer, but buffer_ is writable.
128       buffer_start[buffer_end_++ - buffer_start] = static_cast<uc16>(value);
129       if (buffer_end_ == buffer_ + kPushBackSize + kBufferSize) break;
130     }
131     return buffer_end_ > buffer_start;
132   }
133 
SlowSeekForward(unsigned pos)134   virtual unsigned SlowSeekForward(unsigned pos) {
135     // Seeking in the input is not used by preparsing.
136     // It's only used by the real parser based on preparser data.
137     UNIMPLEMENTED();
138     return 0;
139   }
140 
141  private:
142   static const unsigned kBufferSize = 512;
143   static const unsigned kPushBackSize = 16;
144   v8::UnicodeInputStream* const stream_;
145   // Buffer holding first kPushBackSize characters of pushback buffer,
146   // then kBufferSize chars of read-ahead.
147   // The pushback buffer is only used if pushing back characters past
148   // the start of a block.
149   uc16 buffer_[kPushBackSize + kBufferSize];
150   // Limit of pushbacks before new allocation is necessary.
151   uc16* pushback_buffer_;
152   // Only if that pushback buffer at the start of buffer_ isn't sufficient
153   // is the following used.
154   const uc16* pushback_buffer_end_cache_;
155   uc16* pushback_buffer_backing_;
156   unsigned pushback_buffer_backing_size_;
157 };
158 
159 
160 class StandAloneJavaScriptScanner : public JavaScriptScanner {
161  public:
StandAloneJavaScriptScanner(UnicodeCache * unicode_cache)162   explicit StandAloneJavaScriptScanner(UnicodeCache* unicode_cache)
163       : JavaScriptScanner(unicode_cache) { }
164 
Initialize(UC16CharacterStream * source)165   void Initialize(UC16CharacterStream* source) {
166     source_ = source;
167     Init();
168     // Skip initial whitespace allowing HTML comment ends just like
169     // after a newline and scan first token.
170     has_line_terminator_before_next_ = true;
171     SkipWhiteSpace();
172     Scan();
173   }
174 };
175 
176 
177 // Functions declared by allocation.h and implemented in both api.cc (for v8)
178 // or here (for a stand-alone preparser).
179 
FatalProcessOutOfMemory(const char * reason)180 void FatalProcessOutOfMemory(const char* reason) {
181   V8_Fatal(__FILE__, __LINE__, reason);
182 }
183 
EnableSlowAsserts()184 bool EnableSlowAsserts() { return true; }
185 
186 }  // namespace internal.
187 
188 
~UnicodeInputStream()189 UnicodeInputStream::~UnicodeInputStream() { }
190 
191 
Preparse(UnicodeInputStream * input,size_t max_stack)192 PreParserData Preparse(UnicodeInputStream* input, size_t max_stack) {
193   internal::InputStreamUTF16Buffer buffer(input);
194   uintptr_t stack_limit = reinterpret_cast<uintptr_t>(&buffer) - max_stack;
195   internal::UnicodeCache unicode_cache;
196   internal::StandAloneJavaScriptScanner scanner(&unicode_cache);
197   scanner.Initialize(&buffer);
198   internal::CompleteParserRecorder recorder;
199   preparser::PreParser::PreParseResult result =
200       preparser::PreParser::PreParseProgram(&scanner,
201                                             &recorder,
202                                             true,
203                                             stack_limit);
204   if (result == preparser::PreParser::kPreParseStackOverflow) {
205     return PreParserData::StackOverflow();
206   }
207   internal::Vector<unsigned> pre_data = recorder.ExtractData();
208   size_t size = pre_data.length() * sizeof(pre_data[0]);
209   unsigned char* data = reinterpret_cast<unsigned char*>(pre_data.start());
210   return PreParserData(size, data);
211 }
212 
213 }  // namespace v8.
214 
215 
216 // Used by ASSERT macros and other immediate exits.
V8_Fatal(const char * file,int line,const char * format,...)217 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
218   exit(EXIT_FAILURE);
219 }
220