blob: b0aeb81e2a4d801f7ed9579fccbc6c23a8f58add [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);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000211}
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[]) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000270 // Parse ["throws" [<exn-type> [<start> [<end>]]]].
Ben Murdoch257744e2011-11-30 15:57:28 +0000271 ExceptionExpectation expects;
Ben Murdoch257744e2011-11-30 15:57:28 +0000272 int arg_index = 0;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000273 while (argc > arg_index && strncmp("throws", argv[arg_index], 7)) {
274 arg_index++;
275 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000276 if (argc > arg_index) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000277 expects.throws = true;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000278 arg_index++;
279 if (argc > arg_index && !IsFlag(argv[arg_index])) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000280 expects.type = argv[arg_index];
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000281 arg_index++;
282 if (argc > arg_index && !IsFlag(argv[arg_index])) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000283 expects.beg_pos = atoi(argv[arg_index]); // NOLINT
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000284 arg_index++;
285 if (argc > arg_index && !IsFlag(argv[arg_index])) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000286 expects.end_pos = atoi(argv[arg_index]); // NOLINT
Ben Murdoch257744e2011-11-30 15:57:28 +0000287 }
288 }
289 }
290 }
291 return expects;
292}
293
294
295int main(int argc, const char* argv[]) {
296 // Parse command line.
297 // Format: preparser (<scriptfile> | -e "<source>")
298 // ["throws" [<exn-type> [<start> [<end>]]]]
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000299 // Any flags (except an initial -e) are ignored.
300 // Flags must not separate "throws" and its arguments.
Ben Murdoch257744e2011-11-30 15:57:28 +0000301
302 // Check for mandatory filename argument.
303 int arg_index = 1;
304 if (argc <= arg_index) {
305 fail(NULL, "ERROR: No filename on command line.\n");
306 }
307 const uint8_t* source = NULL;
308 const char* filename = argv[arg_index];
309 if (!strcmp(filename, "-e")) {
310 arg_index++;
311 if (argc <= arg_index) {
312 fail(NULL, "ERROR: No source after -e on command line.\n");
313 }
314 source = reinterpret_cast<const uint8_t*>(argv[arg_index]);
315 }
316 // Check remainder of command line for exception expectations.
317 arg_index++;
318 ExceptionExpectation expects =
319 ParseExpectation(argc - arg_index, argv + arg_index);
320
321 ScopedPointer<uint8_t> buffer;
322 size_t length;
323
324 if (source == NULL) {
325 // Open JS file.
326 FILE* input = fopen(filename, "rb");
327 if (input == NULL) {
328 perror("ERROR: Error opening file");
329 fflush(stderr);
330 return EXIT_FAILURE;
331 }
332 // Find length of JS file.
333 if (fseek(input, 0, SEEK_END) != 0) {
334 perror("ERROR: Error during seek");
335 fflush(stderr);
336 return EXIT_FAILURE;
337 }
338 length = static_cast<size_t>(ftell(input));
339 rewind(input);
340 // Read JS file into memory buffer.
341 buffer = new uint8_t[length];
342 if (!ReadBuffer(input, *buffer, length)) {
343 perror("ERROR: Reading file");
344 fflush(stderr);
345 return EXIT_FAILURE;
346 }
347 fclose(input);
348 source = *buffer;
349 } else {
350 length = strlen(reinterpret_cast<const char*>(source));
351 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100352
353 // Preparse input file.
Ben Murdoch257744e2011-11-30 15:57:28 +0000354 AsciiInputStream input_buffer(source, length);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100355 size_t kMaxStackSize = 64 * 1024 * sizeof(void*); // NOLINT
356 v8::PreParserData data = v8::Preparse(&input_buffer, kMaxStackSize);
357
358 // Fail if stack overflow.
359 if (data.stack_overflow()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000360 fail(&data, "ERROR: Stack overflow\n");
Ben Murdoch8b112d22011-06-08 16:22:53 +0100361 }
362
Ben Murdoch257744e2011-11-30 15:57:28 +0000363 // Check that the expected exception is thrown, if an exception is
364 // expected.
365 CheckException(&data, &expects);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800366
Ben Murdoch8b112d22011-06-08 16:22:53 +0100367 return EXIT_SUCCESS;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800368}