Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Side by Side Diff: preparser/preparser-process.cc

Issue 6783011: Reapply changelist r7436. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include <stdlib.h> 28 #include <stdlib.h>
29 #include <stdarg.h> 29 #include <stdarg.h>
30 #include <stdio.h>
31
30 #include "../include/v8stdint.h" 32 #include "../include/v8stdint.h"
31 #include "../include/v8-preparser.h" 33 #include "../include/v8-preparser.h"
32 #include "unicode-inl.h"
33 34
34 enum ResultCode { kSuccess = 0, kErrorReading = 1, kErrorWriting = 2 }; 35 // This file is only used for testing the stand-alone preparser
35 36 // library.
36 namespace v8 { 37 // The first (and only) argument must be the path of a JavaScript file.
37 namespace internal { 38 // This file is preparsed and the resulting preparser data is written
38 39 // to stdout. Diagnostic output is output on stderr.
39 // THIS FILE IS PROOF-OF-CONCEPT ONLY. 40 // The file must contain only ASCII characters (UTF-8 isn't supported).
40 // The final goal is a stand-alone preparser library. 41 // The file is read into memory, so it should have a reasonable size.
41 42
42 43
43 class UTF8InputStream : public v8::UnicodeInputStream { 44 // Adapts an ASCII string to the UnicodeInputStream interface.
45 class AsciiInputStream : public v8::UnicodeInputStream {
44 public: 46 public:
45 UTF8InputStream(uint8_t* buffer, size_t length) 47 AsciiInputStream(uint8_t* buffer, size_t length)
46 : buffer_(buffer), 48 : buffer_(buffer),
47 offset_(0), 49 end_offset_(static_cast<int>(length)),
48 pos_(0), 50 offset_(0) { }
49 end_offset_(static_cast<int>(length)) { }
50 51
51 virtual ~UTF8InputStream() { } 52 virtual ~AsciiInputStream() { }
52 53
53 virtual void PushBack(int32_t ch) { 54 virtual void PushBack(int32_t ch) {
54 // Pushback assumes that the character pushed back is the 55 offset_--;
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 56 #ifdef DEBUG
60 if (static_cast<unsigned>(ch) <= unibrow::Utf8::kMaxOneByteChar) { 57 if (offset_ < 0 ||
61 if (ch != buffer_[offset_]) { 58 (ch != ((offset_ >= end_offset_) ? -1 : buffer_[offset_]))) {
62 fprintf(stderr, "Invalid pushback: '%c'.", ch); 59 fprintf(stderr, "Invalid pushback: '%c' at offset %d.", ch, offset_);
63 exit(1); 60 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 } 61 }
75 #endif 62 #endif
76 } 63 }
77 64
78 virtual int32_t Next() { 65 virtual int32_t Next() {
79 if (offset_ == end_offset_) return -1; 66 if (offset_ >= end_offset_) {
80 uint8_t first_char = buffer_[offset_]; 67 offset_++; // Increment anyway to allow symmetric pushbacks.
81 if (first_char <= unibrow::Utf8::kMaxOneByteChar) { 68 return -1;
82 pos_++;
83 offset_++;
84 return static_cast<int32_t>(first_char);
85 } 69 }
86 unibrow::uchar codepoint = 70 uint8_t next_char = buffer_[offset_];
87 unibrow::Utf8::CalculateValue(buffer_ + offset_, 71 #ifdef DEBUG
88 end_offset_ - offset_, 72 if (next_char > 0x7fu) {
89 &offset_); 73 fprintf(stderr, "Non-ASCII character in input: '%c'.", next_char);
90 pos_++; 74 exit(1);
91 return static_cast<int32_t>(codepoint); 75 }
76 #endif
77 offset_++;
78 return static_cast<int32_t>(next_char);
92 } 79 }
93 80
94 private: 81 private:
95 const uint8_t* buffer_; 82 const uint8_t* buffer_;
96 unsigned offset_; 83 const int end_offset_;
97 unsigned pos_; 84 int offset_;
98 unsigned end_offset_;
99 }; 85 };
100 86
101 87
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) { 88 bool ReadBuffer(FILE* source, void* buffer, size_t length) {
130 size_t actually_read = fread(buffer, 1, length, source); 89 size_t actually_read = fread(buffer, 1, length, source);
131 return (actually_read == length); 90 return (actually_read == length);
132 } 91 }
133 92
134 93
135 bool WriteBuffer(FILE* dest, const void* buffer, size_t length) { 94 bool WriteBuffer(FILE* dest, const void* buffer, size_t length) {
136 size_t actually_written = fwrite(buffer, 1, length, dest); 95 size_t actually_written = fwrite(buffer, 1, length, dest);
137 return (actually_written == length); 96 return (actually_written == length);
138 } 97 }
139 98
140 99
141 template <typename T> 100 template <typename T>
142 class ScopedPointer { 101 class ScopedPointer {
143 public: 102 public:
144 explicit ScopedPointer(T* pointer) : pointer_(pointer) {} 103 explicit ScopedPointer(T* pointer) : pointer_(pointer) {}
145 ~ScopedPointer() { delete[] pointer_; } 104 ~ScopedPointer() { delete[] pointer_; }
146 T& operator[](int index) { return pointer_[index]; } 105 T& operator[](int index) { return pointer_[index]; }
147 T* operator*() { return pointer_ ;} 106 T* operator*() { return pointer_ ;}
148 private: 107 private:
149 T* pointer_; 108 T* pointer_;
150 }; 109 };
151 110
152 111
153 // Preparse input and output result on stdout. 112 int main(int argc, char* argv[]) {
154 int PreParseIO(FILE* input) { 113 // Check for filename argument.
155 fprintf(stderr, "LOG: Enter parsing loop\n"); 114 if (argc < 2) {
156 bool ok = true; 115 fprintf(stderr, "ERROR: No filename on command line.\n");
157 uint32_t length = ReadUInt32(input, &ok); 116 fflush(stderr);
158 fprintf(stderr, "LOG: Input length: %d\n", length); 117 return EXIT_FAILURE;
159 if (!ok) return kErrorReading; 118 }
160 ScopedPointer<uint8_t> buffer(new uint8_t[length]); 119 const char* filename = argv[1];
161 120
162 if (!ReadBuffer(input, *buffer, length)) { 121 // Open JS file.
163 return kErrorReading; 122 FILE* input = fopen(filename, "rb");
164 } 123 if (input == NULL) {
165 UTF8InputStream input_buffer(*buffer, static_cast<size_t>(length)); 124 perror("ERROR: Error opening file");
166
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); 125 fflush(stderr);
172 // Report stack overflow error/no-preparser-data. 126 return EXIT_FAILURE;
173 WriteUInt32(stdout, 0, &ok);
174 if (!ok) return kErrorWriting;
175 return 0;
176 } 127 }
177 128
129 // Find length of JS file.
130 if (fseek(input, 0, SEEK_END) != 0) {
131 perror("ERROR: Error during seek");
132 fflush(stderr);
133 return EXIT_FAILURE;
134 }
135 size_t length = static_cast<size_t>(ftell(input));
136 rewind(input);
137
138 // Read JS file into memory buffer.
139 ScopedPointer<uint8_t> buffer(new uint8_t[length]);
140 if (!ReadBuffer(input, *buffer, length)) {
141 perror("ERROR: Reading file");
142 fflush(stderr);
143 return EXIT_FAILURE;
144 }
145 fclose(input);
146
147 // Preparse input file.
148 AsciiInputStream input_buffer(*buffer, length);
149 size_t kMaxStackSize = 64 * 1024 * sizeof(void*); // NOLINT
150 v8::PreParserData data = v8::Preparse(&input_buffer, kMaxStackSize);
151
152 // Fail if stack overflow.
153 if (data.stack_overflow()) {
154 fprintf(stderr, "ERROR: Stack overflow\n");
155 fflush(stderr);
156 return EXIT_FAILURE;
157 }
158
159 // Print preparser data to stdout.
178 uint32_t size = data.size(); 160 uint32_t size = data.size();
179 fprintf(stderr, "LOG: Success, data size: %u\n", size); 161 fprintf(stderr, "LOG: Success, data size: %u\n", size);
180 fflush(stderr); 162 fflush(stderr);
181 WriteUInt32(stdout, size, &ok);
182 if (!ok) return kErrorWriting;
183 if (!WriteBuffer(stdout, data.data(), size)) { 163 if (!WriteBuffer(stdout, data.data(), size)) {
184 return kErrorWriting; 164 perror("ERROR: Writing data");
165 return EXIT_FAILURE;
185 } 166 }
186 return 0; 167
168 return EXIT_SUCCESS;
187 } 169 }
188
189 } } // namespace v8::internal
190
191
192 int main(int argc, char* argv[]) {
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 }
199 int status = 0;
200 do {
201 status = v8::internal::PreParseIO(input);
202 } while (status == 0);
203 fprintf(stderr, "EXIT: Failure %d\n", status);
204 fflush(stderr);
205 return EXIT_FAILURE;
206 }
OLDNEW
« no previous file with comments | « SConstruct ('k') | test/preparser/empty.js » ('j') | tools/test.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698