v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
v8utils.cc
Go to the documentation of this file.
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 <stdarg.h>
29 
30 #include "v8.h"
31 
32 #include "platform.h"
33 
34 #include "sys/stat.h"
35 
36 namespace v8 {
37 namespace internal {
38 
39 
40 void PrintF(const char* format, ...) {
41  va_list arguments;
42  va_start(arguments, format);
43  OS::VPrint(format, arguments);
44  va_end(arguments);
45 }
46 
47 
48 void PrintF(FILE* out, const char* format, ...) {
49  va_list arguments;
50  va_start(arguments, format);
51  OS::VFPrint(out, format, arguments);
52  va_end(arguments);
53 }
54 
55 
56 void PrintPID(const char* format, ...) {
58  va_list arguments;
59  va_start(arguments, format);
60  OS::VPrint(format, arguments);
61  va_end(arguments);
62 }
63 
64 
65 void Flush(FILE* out) {
66  fflush(out);
67 }
68 
69 
70 char* ReadLine(const char* prompt) {
71  char* result = NULL;
72  char line_buf[256];
73  int offset = 0;
74  bool keep_going = true;
75  fprintf(stdout, "%s", prompt);
76  fflush(stdout);
77  while (keep_going) {
78  if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) {
79  // fgets got an error. Just give up.
80  if (result != NULL) {
81  DeleteArray(result);
82  }
83  return NULL;
84  }
85  int len = StrLength(line_buf);
86  if (len > 1 &&
87  line_buf[len - 2] == '\\' &&
88  line_buf[len - 1] == '\n') {
89  // When we read a line that ends with a "\" we remove the escape and
90  // append the remainder.
91  line_buf[len - 2] = '\n';
92  line_buf[len - 1] = 0;
93  len -= 1;
94  } else if ((len > 0) && (line_buf[len - 1] == '\n')) {
95  // Since we read a new line we are done reading the line. This
96  // will exit the loop after copying this buffer into the result.
97  keep_going = false;
98  }
99  if (result == NULL) {
100  // Allocate the initial result and make room for the terminating '\0'
101  result = NewArray<char>(len + 1);
102  } else {
103  // Allocate a new result with enough room for the new addition.
104  int new_len = offset + len + 1;
105  char* new_result = NewArray<char>(new_len);
106  // Copy the existing input into the new array and set the new
107  // array as the result.
108  memcpy(new_result, result, offset * kCharSize);
109  DeleteArray(result);
110  result = new_result;
111  }
112  // Copy the newly read line into the result.
113  memcpy(result + offset, line_buf, len * kCharSize);
114  offset += len;
115  }
116  ASSERT(result != NULL);
117  result[offset] = '\0';
118  return result;
119 }
120 
121 
122 char* ReadCharsFromFile(FILE* file,
123  int* size,
124  int extra_space,
125  bool verbose,
126  const char* filename) {
127  if (file == NULL || fseek(file, 0, SEEK_END) != 0) {
128  if (verbose) {
129  OS::PrintError("Cannot read from file %s.\n", filename);
130  }
131  return NULL;
132  }
133 
134  // Get the size of the file and rewind it.
135  *size = ftell(file);
136  rewind(file);
137 
138  char* result = NewArray<char>(*size + extra_space);
139  for (int i = 0; i < *size && feof(file) == 0;) {
140  int read = static_cast<int>(fread(&result[i], 1, *size - i, file));
141  if (read != (*size - i) && ferror(file) != 0) {
142  fclose(file);
143  DeleteArray(result);
144  return NULL;
145  }
146  i += read;
147  }
148  return result;
149 }
150 
151 
152 char* ReadCharsFromFile(const char* filename,
153  int* size,
154  int extra_space,
155  bool verbose) {
156  FILE* file = OS::FOpen(filename, "rb");
157  char* result = ReadCharsFromFile(file, size, extra_space, verbose, filename);
158  if (file != NULL) fclose(file);
159  return result;
160 }
161 
162 
163 byte* ReadBytes(const char* filename, int* size, bool verbose) {
164  char* chars = ReadCharsFromFile(filename, size, 0, verbose);
165  return reinterpret_cast<byte*>(chars);
166 }
167 
168 
169 static Vector<const char> SetVectorContents(char* chars,
170  int size,
171  bool* exists) {
172  if (!chars) {
173  *exists = false;
174  return Vector<const char>::empty();
175  }
176  chars[size] = '\0';
177  *exists = true;
178  return Vector<const char>(chars, size);
179 }
180 
181 
182 Vector<const char> ReadFile(const char* filename,
183  bool* exists,
184  bool verbose) {
185  int size;
186  char* result = ReadCharsFromFile(filename, &size, 1, verbose);
187  return SetVectorContents(result, size, exists);
188 }
189 
190 
192  bool* exists,
193  bool verbose) {
194  int size;
195  char* result = ReadCharsFromFile(file, &size, 1, verbose, "");
196  return SetVectorContents(result, size, exists);
197 }
198 
199 
200 int WriteCharsToFile(const char* str, int size, FILE* f) {
201  int total = 0;
202  while (total < size) {
203  int write = static_cast<int>(fwrite(str, 1, size - total, f));
204  if (write == 0) {
205  return total;
206  }
207  total += write;
208  str += write;
209  }
210  return total;
211 }
212 
213 
214 int AppendChars(const char* filename,
215  const char* str,
216  int size,
217  bool verbose) {
218  FILE* f = OS::FOpen(filename, "ab");
219  if (f == NULL) {
220  if (verbose) {
221  OS::PrintError("Cannot open file %s for writing.\n", filename);
222  }
223  return 0;
224  }
225  int written = WriteCharsToFile(str, size, f);
226  fclose(f);
227  return written;
228 }
229 
230 
231 int WriteChars(const char* filename,
232  const char* str,
233  int size,
234  bool verbose) {
235  FILE* f = OS::FOpen(filename, "wb");
236  if (f == NULL) {
237  if (verbose) {
238  OS::PrintError("Cannot open file %s for writing.\n", filename);
239  }
240  return 0;
241  }
242  int written = WriteCharsToFile(str, size, f);
243  fclose(f);
244  return written;
245 }
246 
247 
248 int WriteBytes(const char* filename,
249  const byte* bytes,
250  int size,
251  bool verbose) {
252  const char* str = reinterpret_cast<const char*>(bytes);
253  return WriteChars(filename, str, size, verbose);
254 }
255 
256 
257 
258 void StringBuilder::AddFormatted(const char* format, ...) {
259  va_list arguments;
260  va_start(arguments, format);
261  AddFormattedList(format, arguments);
262  va_end(arguments);
263 }
264 
265 
266 void StringBuilder::AddFormattedList(const char* format, va_list list) {
268  int n = OS::VSNPrintF(buffer_ + position_, format, list);
269  if (n < 0 || n >= (buffer_.length() - position_)) {
271  } else {
272  position_ += n;
273  }
274 }
275 
276 
278  : filename_(NULL),
279  data_(NULL),
280  length_(0),
281  remove_file_on_cleanup_(false) {
282  Init(filename);
283 }
284 
285 
287  MemoryMappedExternalResource(const char* filename,
288  bool remove_file_on_cleanup)
289  : filename_(NULL),
290  data_(NULL),
291  length_(0),
292  remove_file_on_cleanup_(remove_file_on_cleanup) {
293  Init(filename);
294 }
295 
296 
298  // Release the resources if we had successfully acquired them:
299  if (file_ != NULL) {
300  delete file_;
301  if (remove_file_on_cleanup_) {
302  OS::Remove(filename_);
303  }
304  DeleteArray<char>(filename_);
305  }
306 }
307 
308 
309 void MemoryMappedExternalResource::Init(const char* filename) {
310  file_ = OS::MemoryMappedFile::open(filename);
311  if (file_ != NULL) {
312  filename_ = StrDup(filename);
313  data_ = reinterpret_cast<char*>(file_->memory());
314  length_ = file_->size();
315  }
316 }
317 
318 
319 bool MemoryMappedExternalResource::EnsureIsAscii(bool abort_if_failed) const {
320  bool is_ascii = true;
321 
322  int line_no = 1;
323  const char* start_of_line = data_;
324  const char* end = data_ + length_;
325  for (const char* p = data_; p < end; p++) {
326  char c = *p;
327  if ((c & 0x80) != 0) {
328  // Non-ASCII detected:
329  is_ascii = false;
330 
331  // Report the error and abort if appropriate:
332  if (abort_if_failed) {
333  int char_no = static_cast<int>(p - start_of_line) - 1;
334 
335  ASSERT(filename_ != NULL);
336  PrintF("\n\n\n"
337  "Abort: Non-Ascii character 0x%.2x in file %s line %d char %d",
338  c, filename_, line_no, char_no);
339 
340  // Allow for some context up to kNumberOfLeadingContextChars chars
341  // before the offending non-ASCII char to help the user see where
342  // the offending char is.
343  const int kNumberOfLeadingContextChars = 10;
344  const char* err_context = p - kNumberOfLeadingContextChars;
345  if (err_context < data_) {
346  err_context = data_;
347  }
348  // Compute the length of the error context and print it.
349  int err_context_length = static_cast<int>(p - err_context);
350  if (err_context_length != 0) {
351  PrintF(" after \"%.*s\"", err_context_length, err_context);
352  }
353  PrintF(".\n\n\n");
354  OS::Abort();
355  }
356 
357  break; // Non-ASCII detected. No need to continue scanning.
358  }
359  if (c == '\n') {
360  start_of_line = p;
361  line_no++;
362  }
363  }
364 
365  return is_ascii;
366 }
367 
368 
369 } } // namespace v8::internal
void PrintF(const char *format,...)
Definition: v8utils.cc:40
static int VSNPrintF(Vector< char > str, const char *format, va_list args)
char * ReadCharsFromFile(FILE *file, int *size, int extra_space, bool verbose, const char *filename)
Definition: v8utils.cc:122
#define ASSERT(condition)
Definition: checks.h:270
static void VFPrint(FILE *out, const char *format, va_list args)
byte * ReadBytes(const char *filename, int *size, bool verbose)
Definition: v8utils.cc:163
char * ReadLine(const char *prompt)
Definition: v8utils.cc:70
static MemoryMappedFile * open(const char *name)
uint8_t byte
Definition: globals.h:156
static void Abort()
Vector< const char > ReadFile(const char *filename, bool *exists, bool verbose)
Definition: v8utils.cc:182
static FILE * FOpen(const char *path, const char *mode)
static void VPrint(const char *format, va_list args)
void AddFormatted(const char *format,...)
Definition: v8utils.cc:258
static int GetCurrentProcessId()
int length() const
Definition: utils.h:384
int WriteCharsToFile(const char *str, int size, FILE *f)
Definition: v8utils.cc:200
activate correct semantics for inheriting readonliness false
Definition: flags.cc:141
int StrLength(const char *string)
Definition: utils.h:234
static void Print(const char *format,...)
void AddFormattedList(const char *format, va_list list)
Definition: v8utils.cc:266
int WriteChars(const char *filename, const char *str, int size, bool verbose)
Definition: v8utils.cc:231
int AppendChars(const char *filename, const char *str, int size, bool verbose)
Definition: v8utils.cc:214
static void PrintError(const char *format,...)
void PrintPID(const char *format,...)
Definition: v8utils.cc:56
static Vector< T > empty()
Definition: utils.h:442
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
char * StrDup(const char *str)
Definition: allocation.cc:85
static bool Remove(const char *path)
int WriteBytes(const char *filename, const byte *bytes, int size, bool verbose)
Definition: v8utils.cc:248
void DeleteArray(T *array)
Definition: allocation.h:91
void Flush(FILE *out)
Definition: v8utils.cc:65
const int kCharSize
Definition: globals.h:215
MemoryMappedExternalResource(const char *filename)
Definition: v8utils.cc:277