Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [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 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 28 | #include <stdlib.h> |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 29 | #include <stdarg.h> |
| 30 | #include "../include/v8stdint.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 31 | #include "../include/v8-preparser.h" |
| 32 | #include "unicode-inl.h" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 33 | |
| 34 | enum ResultCode { kSuccess = 0, kErrorReading = 1, kErrorWriting = 2 }; |
| 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | // THIS FILE IS PROOF-OF-CONCEPT ONLY. |
| 40 | // The final goal is a stand-alone preparser library. |
| 41 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 42 | |
| 43 | class UTF8InputStream : public v8::UnicodeInputStream { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 44 | public: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 45 | UTF8InputStream(uint8_t* buffer, size_t length) |
| 46 | : buffer_(buffer), |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 47 | offset_(0), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 48 | pos_(0), |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 49 | end_offset_(static_cast<int>(length)) { } |
| 50 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 51 | virtual ~UTF8InputStream() { } |
| 52 | |
| 53 | virtual void PushBack(int32_t ch) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 54 | // Pushback assumes that the character pushed back is the |
| 55 | // one that was most recently read, and jumps back in the |
| 56 | // UTF-8 stream by the length of that character's encoding. |
| 57 | offset_ -= unibrow::Utf8::Length(ch); |
| 58 | pos_--; |
| 59 | #ifdef DEBUG |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 60 | if (static_cast<unsigned>(ch) <= unibrow::Utf8::kMaxOneByteChar) { |
| 61 | if (ch != buffer_[offset_]) { |
| 62 | fprintf(stderr, "Invalid pushback: '%c'.", ch); |
| 63 | exit(1); |
| 64 | } |
| 65 | } else { |
| 66 | unsigned tmp = 0; |
| 67 | if (static_cast<unibrow::uchar>(ch) != |
| 68 | unibrow::Utf8::CalculateValue(buffer_ + offset_, |
| 69 | end_offset_ - offset_, |
| 70 | &tmp)) { |
| 71 | fprintf(stderr, "Invalid pushback: 0x%x.", ch); |
| 72 | exit(1); |
| 73 | } |
| 74 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 75 | #endif |
| 76 | } |
| 77 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 78 | virtual int32_t Next() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 79 | if (offset_ == end_offset_) return -1; |
| 80 | uint8_t first_char = buffer_[offset_]; |
| 81 | if (first_char <= unibrow::Utf8::kMaxOneByteChar) { |
| 82 | pos_++; |
| 83 | offset_++; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 84 | return static_cast<int32_t>(first_char); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 85 | } |
| 86 | unibrow::uchar codepoint = |
| 87 | unibrow::Utf8::CalculateValue(buffer_ + offset_, |
| 88 | end_offset_ - offset_, |
| 89 | &offset_); |
| 90 | pos_++; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 91 | return static_cast<int32_t>(codepoint); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private: |
| 95 | const uint8_t* buffer_; |
| 96 | unsigned offset_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 97 | unsigned pos_; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 98 | unsigned end_offset_; |
| 99 | }; |
| 100 | |
| 101 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 102 | // Write a number to dest in network byte order. |
| 103 | void WriteUInt32(FILE* dest, uint32_t value, bool* ok) { |
| 104 | for (int i = 3; i >= 0; i--) { |
| 105 | uint8_t byte = static_cast<uint8_t>(value >> (i << 3)); |
| 106 | int result = fputc(byte, dest); |
| 107 | if (result == EOF) { |
| 108 | *ok = false; |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Read number from FILE* in network byte order. |
| 115 | uint32_t ReadUInt32(FILE* source, bool* ok) { |
| 116 | uint32_t n = 0; |
| 117 | for (int i = 0; i < 4; i++) { |
| 118 | int c = fgetc(source); |
| 119 | if (c == EOF) { |
| 120 | *ok = false; |
| 121 | return 0; |
| 122 | } |
| 123 | n = (n << 8) + static_cast<uint32_t>(c); |
| 124 | } |
| 125 | return n; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | bool ReadBuffer(FILE* source, void* buffer, size_t length) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 130 | size_t actually_read = fread(buffer, 1, length, source); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 131 | return (actually_read == length); |
| 132 | } |
| 133 | |
| 134 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 135 | bool WriteBuffer(FILE* dest, const void* buffer, size_t length) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 136 | size_t actually_written = fwrite(buffer, 1, length, dest); |
| 137 | return (actually_written == length); |
| 138 | } |
| 139 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 140 | |
| 141 | template <typename T> |
| 142 | class ScopedPointer { |
| 143 | public: |
| 144 | explicit ScopedPointer(T* pointer) : pointer_(pointer) {} |
| 145 | ~ScopedPointer() { delete[] pointer_; } |
| 146 | T& operator[](int index) { return pointer_[index]; } |
| 147 | T* operator*() { return pointer_ ;} |
| 148 | private: |
| 149 | T* pointer_; |
| 150 | }; |
| 151 | |
| 152 | |
| 153 | // Preparse input and output result on stdout. |
| 154 | int PreParseIO(FILE* input) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 155 | fprintf(stderr, "LOG: Enter parsing loop\n"); |
| 156 | bool ok = true; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 157 | uint32_t length = ReadUInt32(input, &ok); |
| 158 | fprintf(stderr, "LOG: Input length: %d\n", length); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 159 | if (!ok) return kErrorReading; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 160 | ScopedPointer<uint8_t> buffer(new uint8_t[length]); |
| 161 | |
| 162 | if (!ReadBuffer(input, *buffer, length)) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 163 | return kErrorReading; |
| 164 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 165 | UTF8InputStream input_buffer(*buffer, static_cast<size_t>(length)); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 166 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 167 | v8::PreParserData data = |
| 168 | v8::Preparse(&input_buffer, 64 * 1024 * sizeof(void*)); // NOLINT |
| 169 | if (data.stack_overflow()) { |
| 170 | fprintf(stderr, "LOG: Stack overflow\n"); |
| 171 | fflush(stderr); |
| 172 | // Report stack overflow error/no-preparser-data. |
| 173 | WriteUInt32(stdout, 0, &ok); |
| 174 | if (!ok) return kErrorWriting; |
| 175 | return 0; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 176 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 177 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 178 | uint32_t size = data.size(); |
| 179 | fprintf(stderr, "LOG: Success, data size: %u\n", size); |
| 180 | fflush(stderr); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 181 | WriteUInt32(stdout, size, &ok); |
| 182 | if (!ok) return kErrorWriting; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 183 | if (!WriteBuffer(stdout, data.data(), size)) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 184 | return kErrorWriting; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 189 | } } // namespace v8::internal |
| 190 | |
| 191 | |
| 192 | int main(int argc, char* argv[]) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 193 | FILE* input = stdin; |
| 194 | if (argc > 1) { |
| 195 | char* arg = argv[1]; |
| 196 | input = fopen(arg, "rb"); |
| 197 | if (input == NULL) return EXIT_FAILURE; |
| 198 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 199 | int status = 0; |
| 200 | do { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 201 | status = v8::internal::PreParseIO(input); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 202 | } while (status == 0); |
| 203 | fprintf(stderr, "EXIT: Failure %d\n", status); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 204 | fflush(stderr); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 205 | return EXIT_FAILURE; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 206 | } |