blob: a19c66b718168a37223affa7780e29791a2e4b0a [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002// 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 Murdochb0fe1622011-05-05 13:52:32 +010028#include <stdlib.h>
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080029#include <stdarg.h>
Ben Murdoch8b112d22011-06-08 16:22:53 +010030#include <stdio.h>
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include <string.h>
Ben Murdoch8b112d22011-06-08 16:22:53 +010032
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080033#include "../include/v8stdint.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010034#include "../include/v8-preparser.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080035
Ben Murdoch257744e2011-11-30 15:57:28 +000036#include "../src/preparse-data-format.h"
37
38namespace i = v8::internal;
39
Ben Murdoch8b112d22011-06-08 16:22:53 +010040// This file is only used for testing the stand-alone preparser
41// library.
Ben Murdoch257744e2011-11-30 15:57:28 +000042// The first argument must be the path of a JavaScript source file, or
43// the flags "-e" and the next argument is then the source of a JavaScript
44// program.
45// Optionally this can be followed by the word "throws" (case sensitive),
46// which signals that the parsing is expected to throw - the default is
47// to expect the parsing to not throw.
48// The command line can further be followed by a message text (the
49// *type* of the exception to throw), and even more optionally, the
50// start and end position reported with the exception.
51//
52// This source file is preparsed and tested against the expectations, and if
53// successful, the resulting preparser data is written to stdout.
54// Diagnostic output is output on stderr.
55// The source file must contain only ASCII characters (UTF-8 isn't supported).
Ben Murdoch8b112d22011-06-08 16:22:53 +010056// The file is read into memory, so it should have a reasonable size.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080057
Ben Murdochb0fe1622011-05-05 13:52:32 +010058
Ben Murdoch8b112d22011-06-08 16:22:53 +010059// Adapts an ASCII string to the UnicodeInputStream interface.
60class AsciiInputStream : public v8::UnicodeInputStream {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080061 public:
Ben Murdoch257744e2011-11-30 15:57:28 +000062 AsciiInputStream(const uint8_t* buffer, size_t length)
Ben Murdochb0fe1622011-05-05 13:52:32 +010063 : buffer_(buffer),
Ben Murdoch8b112d22011-06-08 16:22:53 +010064 end_offset_(static_cast<int>(length)),
65 offset_(0) { }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080066
Ben Murdoch8b112d22011-06-08 16:22:53 +010067 virtual ~AsciiInputStream() { }
Ben Murdochb0fe1622011-05-05 13:52:32 +010068
69 virtual void PushBack(int32_t ch) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010070 offset_--;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080071#ifdef DEBUG
Ben Murdoch8b112d22011-06-08 16:22:53 +010072 if (offset_ < 0 ||
73 (ch != ((offset_ >= end_offset_) ? -1 : buffer_[offset_]))) {
74 fprintf(stderr, "Invalid pushback: '%c' at offset %d.", ch, offset_);
75 exit(1);
Ben Murdochb0fe1622011-05-05 13:52:32 +010076 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080077#endif
78 }
79
Ben Murdochb0fe1622011-05-05 13:52:32 +010080 virtual int32_t Next() {
Ben Murdoch8b112d22011-06-08 16:22:53 +010081 if (offset_ >= end_offset_) {
82 offset_++; // Increment anyway to allow symmetric pushbacks.
83 return -1;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080084 }
Ben Murdoch8b112d22011-06-08 16:22:53 +010085 uint8_t next_char = buffer_[offset_];
86#ifdef DEBUG
87 if (next_char > 0x7fu) {
88 fprintf(stderr, "Non-ASCII character in input: '%c'.", next_char);
89 exit(1);
90 }
91#endif
92 offset_++;
93 return static_cast<int32_t>(next_char);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080094 }
95
96 private:
97 const uint8_t* buffer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +010098 const int end_offset_;
99 int offset_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800100};
101
102
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800103bool ReadBuffer(FILE* source, void* buffer, size_t length) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100104 size_t actually_read = fread(buffer, 1, length, source);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800105 return (actually_read == length);
106}
107
108
Ben Murdochb0fe1622011-05-05 13:52:32 +0100109bool WriteBuffer(FILE* dest, const void* buffer, size_t length) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800110 size_t actually_written = fwrite(buffer, 1, length, dest);
111 return (actually_written == length);
112}
113
Ben Murdochb0fe1622011-05-05 13:52:32 +0100114
Ben Murdoch257744e2011-11-30 15:57:28 +0000115class PreparseDataInterpreter {
116 public:
117 PreparseDataInterpreter(const uint8_t* data, int length)
118 : data_(data), length_(length), message_(NULL) { }
119
120 ~PreparseDataInterpreter() {
121 if (message_ != NULL) delete[] message_;
122 }
123
124 bool valid() {
125 int header_length =
126 i::PreparseDataConstants::kHeaderSize * sizeof(int); // NOLINT
127 return length_ >= header_length;
128 }
129
130 bool throws() {
131 return valid() &&
132 word(i::PreparseDataConstants::kHasErrorOffset) != 0;
133 }
134
135 const char* message() {
136 if (message_ != NULL) return message_;
137 if (!throws()) return NULL;
138 int text_pos = i::PreparseDataConstants::kHeaderSize +
139 i::PreparseDataConstants::kMessageTextPos;
140 int length = word(text_pos);
141 char* buffer = new char[length + 1];
142 for (int i = 1; i <= length; i++) {
143 int character = word(text_pos + i);
144 buffer[i - 1] = character;
145 }
146 buffer[length] = '\0';
147 message_ = buffer;
148 return buffer;
149 }
150
151 int beg_pos() {
152 if (!throws()) return -1;
153 return word(i::PreparseDataConstants::kHeaderSize +
154 i::PreparseDataConstants::kMessageStartPos);
155 }
156
157 int end_pos() {
158 if (!throws()) return -1;
159 return word(i::PreparseDataConstants::kHeaderSize +
160 i::PreparseDataConstants::kMessageEndPos);
161 }
162
163 private:
164 int word(int offset) {
165 const int* word_data = reinterpret_cast<const int*>(data_);
166 if (word_data + offset < reinterpret_cast<const int*>(data_ + length_)) {
167 return word_data[offset];
168 }
169 return -1;
170 }
171
172 const uint8_t* const data_;
173 const int length_;
174 const char* message_;
175};
176
177
Ben Murdochb0fe1622011-05-05 13:52:32 +0100178template <typename T>
179class ScopedPointer {
180 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000181 explicit ScopedPointer() : pointer_(NULL) {}
Ben Murdochb0fe1622011-05-05 13:52:32 +0100182 explicit ScopedPointer(T* pointer) : pointer_(pointer) {}
Ben Murdoch257744e2011-11-30 15:57:28 +0000183 ~ScopedPointer() { if (pointer_ != NULL) delete[] pointer_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100184 T& operator[](int index) { return pointer_[index]; }
185 T* operator*() { return pointer_ ;}
Ben Murdoch257744e2011-11-30 15:57:28 +0000186 T* operator=(T* new_value) {
187 if (pointer_ != NULL) delete[] pointer_;
188 pointer_ = new_value;
189 return new_value;
190 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100191 private:
192 T* pointer_;
193};
194
195
Ben Murdoch8b112d22011-06-08 16:22:53 +0100196
Ben Murdoch257744e2011-11-30 15:57:28 +0000197void fail(v8::PreParserData* data, const char* message, ...) {
198 va_list args;
199 va_start(args, message);
200 vfprintf(stderr, message, args);
201 va_end(args);
202 fflush(stderr);
203 // Print preparser data to stdout.
204 uint32_t size = data->size();
205 fprintf(stderr, "LOG: data size: %u\n", size);
206 if (!WriteBuffer(stdout, data->data(), size)) {
207 perror("ERROR: Writing data");
Ben Murdoch8b112d22011-06-08 16:22:53 +0100208 fflush(stderr);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800209 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000210 exit(EXIT_FAILURE);
211};
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800212
Ben Murdoch8b112d22011-06-08 16:22:53 +0100213
Ben Murdoch257744e2011-11-30 15:57:28 +0000214bool IsFlag(const char* arg) {
215 // Anything starting with '-' is considered a flag.
216 // It's summarily ignored for now.
217 return arg[0] == '-';
218}
219
220
221struct ExceptionExpectation {
222 ExceptionExpectation()
223 : throws(false), type(NULL), beg_pos(-1), end_pos(-1) { }
224 bool throws;
225 const char* type;
226 int beg_pos;
227 int end_pos;
228};
229
230
231void CheckException(v8::PreParserData* data,
232 ExceptionExpectation* expects) {
233 PreparseDataInterpreter reader(data->data(), data->size());
234 if (expects->throws) {
235 if (!reader.throws()) {
236 if (expects->type == NULL) {
237 fail(data, "Didn't throw as expected\n");
238 } else {
239 fail(data, "Didn't throw \"%s\" as expected\n", expects->type);
240 }
241 }
242 if (expects->type != NULL) {
243 const char* actual_message = reader.message();
244 if (strcmp(expects->type, actual_message)) {
245 fail(data, "Wrong error message. Expected <%s>, found <%s> at %d..%d\n",
246 expects->type, actual_message, reader.beg_pos(), reader.end_pos());
247 }
248 }
249 if (expects->beg_pos >= 0) {
250 if (expects->beg_pos != reader.beg_pos()) {
251 fail(data, "Wrong error start position: Expected %i, found %i\n",
252 expects->beg_pos, reader.beg_pos());
253 }
254 }
255 if (expects->end_pos >= 0) {
256 if (expects->end_pos != reader.end_pos()) {
257 fail(data, "Wrong error end position: Expected %i, found %i\n",
258 expects->end_pos, reader.end_pos());
259 }
260 }
261 } else if (reader.throws()) {
262 const char* message = reader.message();
263 fail(data, "Throws unexpectedly with message: %s at location %d-%d\n",
264 message, reader.beg_pos(), reader.end_pos());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100265 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000266}
267
268
269ExceptionExpectation ParseExpectation(int argc, const char* argv[]) {
270 ExceptionExpectation expects;
271
272 // Parse exception expectations from (the remainder of) the command line.
273 int arg_index = 0;
274 // Skip any flags.
275 while (argc > arg_index && IsFlag(argv[arg_index])) arg_index++;
276 if (argc > arg_index) {
277 if (strncmp("throws", argv[arg_index], 7)) {
278 // First argument after filename, if present, must be the verbatim
279 // "throws", marking that the preparsing should fail with an exception.
280 fail(NULL, "ERROR: Extra arguments not prefixed by \"throws\".\n");
281 }
282 expects.throws = true;
283 do {
284 arg_index++;
285 } while (argc > arg_index && IsFlag(argv[arg_index]));
286 if (argc > arg_index) {
287 // Next argument is the exception type identifier.
288 expects.type = argv[arg_index];
289 do {
290 arg_index++;
291 } while (argc > arg_index && IsFlag(argv[arg_index]));
292 if (argc > arg_index) {
293 expects.beg_pos = atoi(argv[arg_index]);
294 do {
295 arg_index++;
296 } while (argc > arg_index && IsFlag(argv[arg_index]));
297 if (argc > arg_index) {
298 expects.end_pos = atoi(argv[arg_index]);
299 }
300 }
301 }
302 }
303 return expects;
304}
305
306
307int main(int argc, const char* argv[]) {
308 // Parse command line.
309 // Format: preparser (<scriptfile> | -e "<source>")
310 // ["throws" [<exn-type> [<start> [<end>]]]]
311 // Any flags (except an initial -s) are ignored.
312
313 // Check for mandatory filename argument.
314 int arg_index = 1;
315 if (argc <= arg_index) {
316 fail(NULL, "ERROR: No filename on command line.\n");
317 }
318 const uint8_t* source = NULL;
319 const char* filename = argv[arg_index];
320 if (!strcmp(filename, "-e")) {
321 arg_index++;
322 if (argc <= arg_index) {
323 fail(NULL, "ERROR: No source after -e on command line.\n");
324 }
325 source = reinterpret_cast<const uint8_t*>(argv[arg_index]);
326 }
327 // Check remainder of command line for exception expectations.
328 arg_index++;
329 ExceptionExpectation expects =
330 ParseExpectation(argc - arg_index, argv + arg_index);
331
332 ScopedPointer<uint8_t> buffer;
333 size_t length;
334
335 if (source == NULL) {
336 // Open JS file.
337 FILE* input = fopen(filename, "rb");
338 if (input == NULL) {
339 perror("ERROR: Error opening file");
340 fflush(stderr);
341 return EXIT_FAILURE;
342 }
343 // Find length of JS file.
344 if (fseek(input, 0, SEEK_END) != 0) {
345 perror("ERROR: Error during seek");
346 fflush(stderr);
347 return EXIT_FAILURE;
348 }
349 length = static_cast<size_t>(ftell(input));
350 rewind(input);
351 // Read JS file into memory buffer.
352 buffer = new uint8_t[length];
353 if (!ReadBuffer(input, *buffer, length)) {
354 perror("ERROR: Reading file");
355 fflush(stderr);
356 return EXIT_FAILURE;
357 }
358 fclose(input);
359 source = *buffer;
360 } else {
361 length = strlen(reinterpret_cast<const char*>(source));
362 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100363
364 // Preparse input file.
Ben Murdoch257744e2011-11-30 15:57:28 +0000365 AsciiInputStream input_buffer(source, length);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100366 size_t kMaxStackSize = 64 * 1024 * sizeof(void*); // NOLINT
367 v8::PreParserData data = v8::Preparse(&input_buffer, kMaxStackSize);
368
369 // Fail if stack overflow.
370 if (data.stack_overflow()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000371 fail(&data, "ERROR: Stack overflow\n");
Ben Murdoch8b112d22011-06-08 16:22:53 +0100372 }
373
Ben Murdoch257744e2011-11-30 15:57:28 +0000374 // Check that the expected exception is thrown, if an exception is
375 // expected.
376 CheckException(&data, &expects);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800377
Ben Murdoch8b112d22011-06-08 16:22:53 +0100378 return EXIT_SUCCESS;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800379}