]> git.saurik.com Git - apple/javascriptcore.git/blame - jsc.cpp
JavaScriptCore-554.1.tar.gz
[apple/javascriptcore.git] / jsc.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2006 Bjoern Graf (bjoern.graf@gmail.com)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24
25#include "BytecodeGenerator.h"
26#include "Completion.h"
27#include "InitializeThreading.h"
28#include "JSArray.h"
ba379fdc 29#include "JSFunction.h"
9dae56ea
A
30#include "JSLock.h"
31#include "PrototypeFunction.h"
32#include "SamplingTool.h"
33#include <math.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37
38#if !PLATFORM(WIN_OS)
39#include <unistd.h>
40#endif
41
42#if HAVE(READLINE)
43#include <readline/history.h>
44#include <readline/readline.h>
45#endif
46
47#if HAVE(SYS_TIME_H)
48#include <sys/time.h>
49#endif
50
ba379fdc 51#if HAVE(SIGNAL_H)
9dae56ea
A
52#include <signal.h>
53#endif
54
ba379fdc 55#if COMPILER(MSVC) && !PLATFORM(WINCE)
9dae56ea
A
56#include <crtdbg.h>
57#include <windows.h>
ba379fdc 58#include <mmsystem.h>
9dae56ea
A
59#endif
60
61#if PLATFORM(QT)
62#include <QCoreApplication>
63#include <QDateTime>
64#endif
65
66using namespace JSC;
67using namespace WTF;
68
69static void cleanupGlobalData(JSGlobalData*);
70static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer);
71
ba379fdc
A
72static JSValue JSC_HOST_CALL functionPrint(ExecState*, JSObject*, JSValue, const ArgList&);
73static JSValue JSC_HOST_CALL functionDebug(ExecState*, JSObject*, JSValue, const ArgList&);
74static JSValue JSC_HOST_CALL functionGC(ExecState*, JSObject*, JSValue, const ArgList&);
75static JSValue JSC_HOST_CALL functionVersion(ExecState*, JSObject*, JSValue, const ArgList&);
76static JSValue JSC_HOST_CALL functionRun(ExecState*, JSObject*, JSValue, const ArgList&);
77static JSValue JSC_HOST_CALL functionLoad(ExecState*, JSObject*, JSValue, const ArgList&);
78static JSValue JSC_HOST_CALL functionCheckSyntax(ExecState*, JSObject*, JSValue, const ArgList&);
79static JSValue JSC_HOST_CALL functionReadline(ExecState*, JSObject*, JSValue, const ArgList&);
80static NO_RETURN JSValue JSC_HOST_CALL functionQuit(ExecState*, JSObject*, JSValue, const ArgList&);
81
82#if ENABLE(SAMPLING_FLAGS)
83static JSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState*, JSObject*, JSValue, const ArgList&);
84static JSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState*, JSObject*, JSValue, const ArgList&);
85#endif
86
87struct Script {
88 bool isFile;
89 char *argument;
90
91 Script(bool isFile, char *argument)
92 : isFile(isFile)
93 , argument(argument)
94 {
95 }
96};
9dae56ea
A
97
98struct Options {
99 Options()
100 : interactive(false)
101 , dump(false)
102 {
103 }
104
105 bool interactive;
106 bool dump;
ba379fdc 107 Vector<Script> scripts;
9dae56ea
A
108 Vector<UString> arguments;
109};
110
111static const char interactivePrompt[] = "> ";
112static const UString interpreterName("Interpreter");
113
114class StopWatch {
115public:
116 void start();
117 void stop();
118 long getElapsedMS(); // call stop() first
119
120private:
121#if PLATFORM(QT)
122 uint m_startTime;
123 uint m_stopTime;
124#elif PLATFORM(WIN_OS)
125 DWORD m_startTime;
126 DWORD m_stopTime;
127#else
128 // Windows does not have timeval, disabling this class for now (bug 7399)
129 timeval m_startTime;
130 timeval m_stopTime;
131#endif
132};
133
134void StopWatch::start()
135{
136#if PLATFORM(QT)
137 QDateTime t = QDateTime::currentDateTime();
138 m_startTime = t.toTime_t() * 1000 + t.time().msec();
139#elif PLATFORM(WIN_OS)
140 m_startTime = timeGetTime();
141#else
142 gettimeofday(&m_startTime, 0);
143#endif
144}
145
146void StopWatch::stop()
147{
148#if PLATFORM(QT)
149 QDateTime t = QDateTime::currentDateTime();
150 m_stopTime = t.toTime_t() * 1000 + t.time().msec();
151#elif PLATFORM(WIN_OS)
152 m_stopTime = timeGetTime();
153#else
154 gettimeofday(&m_stopTime, 0);
155#endif
156}
157
158long StopWatch::getElapsedMS()
159{
160#if PLATFORM(WIN_OS) || PLATFORM(QT)
161 return m_stopTime - m_startTime;
162#else
163 timeval elapsedTime;
164 timersub(&m_stopTime, &m_startTime, &elapsedTime);
165
166 return elapsedTime.tv_sec * 1000 + lroundf(elapsedTime.tv_usec / 1000.0f);
167#endif
168}
169
170class GlobalObject : public JSGlobalObject {
171public:
172 GlobalObject(const Vector<UString>& arguments);
173 virtual UString className() const { return "global"; }
174};
175COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
176ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
177
178GlobalObject::GlobalObject(const Vector<UString>& arguments)
179 : JSGlobalObject()
180{
ba379fdc
A
181 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "debug"), functionDebug));
182 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "print"), functionPrint));
183 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "quit"), functionQuit));
184 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "gc"), functionGC));
185 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "version"), functionVersion));
186 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "run"), functionRun));
187 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "load"), functionLoad));
188 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "checkSyntax"), functionCheckSyntax));
189 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "readline"), functionReadline));
190
191#if ENABLE(SAMPLING_FLAGS)
192 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "setSamplingFlags"), functionSetSamplingFlags));
193 putDirectFunction(globalExec(), new (globalExec()) NativeFunctionWrapper(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "clearSamplingFlags"), functionClearSamplingFlags));
194#endif
9dae56ea
A
195
196 JSObject* array = constructEmptyArray(globalExec());
197 for (size_t i = 0; i < arguments.size(); ++i)
198 array->put(globalExec(), i, jsString(globalExec(), arguments[i]));
199 putDirect(Identifier(globalExec(), "arguments"), array);
200}
201
ba379fdc 202JSValue JSC_HOST_CALL functionPrint(ExecState* exec, JSObject*, JSValue, const ArgList& args)
9dae56ea
A
203{
204 for (unsigned i = 0; i < args.size(); ++i) {
205 if (i != 0)
206 putchar(' ');
207
ba379fdc 208 printf("%s", args.at(i).toString(exec).UTF8String().c_str());
9dae56ea
A
209 }
210
211 putchar('\n');
212 fflush(stdout);
213 return jsUndefined();
214}
215
ba379fdc 216JSValue JSC_HOST_CALL functionDebug(ExecState* exec, JSObject*, JSValue, const ArgList& args)
9dae56ea 217{
ba379fdc 218 fprintf(stderr, "--> %s\n", args.at(0).toString(exec).UTF8String().c_str());
9dae56ea
A
219 return jsUndefined();
220}
221
ba379fdc 222JSValue JSC_HOST_CALL functionGC(ExecState* exec, JSObject*, JSValue, const ArgList&)
9dae56ea
A
223{
224 JSLock lock(false);
225 exec->heap()->collect();
226 return jsUndefined();
227}
228
ba379fdc 229JSValue JSC_HOST_CALL functionVersion(ExecState*, JSObject*, JSValue, const ArgList&)
9dae56ea
A
230{
231 // We need this function for compatibility with the Mozilla JS tests but for now
232 // we don't actually do any version-specific handling
233 return jsUndefined();
234}
235
ba379fdc 236JSValue JSC_HOST_CALL functionRun(ExecState* exec, JSObject*, JSValue, const ArgList& args)
9dae56ea
A
237{
238 StopWatch stopWatch;
ba379fdc 239 UString fileName = args.at(0).toString(exec);
9dae56ea
A
240 Vector<char> script;
241 if (!fillBufferWithContentsOfFile(fileName, script))
242 return throwError(exec, GeneralError, "Could not open file.");
243
244 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
245
246 stopWatch.start();
247 evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
248 stopWatch.stop();
249
250 return jsNumber(globalObject->globalExec(), stopWatch.getElapsedMS());
251}
252
ba379fdc 253JSValue JSC_HOST_CALL functionLoad(ExecState* exec, JSObject* o, JSValue v, const ArgList& args)
9dae56ea 254{
ba379fdc
A
255 UNUSED_PARAM(o);
256 UNUSED_PARAM(v);
257 UString fileName = args.at(0).toString(exec);
9dae56ea
A
258 Vector<char> script;
259 if (!fillBufferWithContentsOfFile(fileName, script))
260 return throwError(exec, GeneralError, "Could not open file.");
261
262 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
ba379fdc
A
263 Completion result = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
264 if (result.complType() == Throw)
265 exec->setException(result.value());
266 return result.value();
267}
9dae56ea 268
ba379fdc
A
269JSValue JSC_HOST_CALL functionCheckSyntax(ExecState* exec, JSObject* o, JSValue v, const ArgList& args)
270{
271 UNUSED_PARAM(o);
272 UNUSED_PARAM(v);
273 UString fileName = args.at(0).toString(exec);
274 Vector<char> script;
275 if (!fillBufferWithContentsOfFile(fileName, script))
276 return throwError(exec, GeneralError, "Could not open file.");
277
278 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
279 Completion result = checkSyntax(globalObject->globalExec(), makeSource(script.data(), fileName));
280 if (result.complType() == Throw)
281 exec->setException(result.value());
282 return result.value();
9dae56ea
A
283}
284
ba379fdc
A
285#if ENABLE(SAMPLING_FLAGS)
286JSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState* exec, JSObject*, JSValue, const ArgList& args)
287{
288 for (unsigned i = 0; i < args.size(); ++i) {
289 unsigned flag = static_cast<unsigned>(args.at(i).toNumber(exec));
290 if ((flag >= 1) && (flag <= 32))
291 SamplingFlags::setFlag(flag);
292 }
293 return jsNull();
294}
295
296JSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState* exec, JSObject*, JSValue, const ArgList& args)
297{
298 for (unsigned i = 0; i < args.size(); ++i) {
299 unsigned flag = static_cast<unsigned>(args.at(i).toNumber(exec));
300 if ((flag >= 1) && (flag <= 32))
301 SamplingFlags::clearFlag(flag);
302 }
303 return jsNull();
304}
305#endif
306
307JSValue JSC_HOST_CALL functionReadline(ExecState* exec, JSObject*, JSValue, const ArgList&)
9dae56ea
A
308{
309 Vector<char, 256> line;
310 int c;
311 while ((c = getchar()) != EOF) {
312 // FIXME: Should we also break on \r?
313 if (c == '\n')
314 break;
315 line.append(c);
316 }
317 line.append('\0');
318 return jsString(exec, line.data());
319}
320
ba379fdc 321JSValue JSC_HOST_CALL functionQuit(ExecState* exec, JSObject*, JSValue, const ArgList&)
9dae56ea
A
322{
323 cleanupGlobalData(&exec->globalData());
324 exit(EXIT_SUCCESS);
325}
326
327// Use SEH for Release builds only to get rid of the crash report dialog
328// (luckily the same tests fail in Release and Debug builds so far). Need to
329// be in a separate main function because the jscmain function requires object
330// unwinding.
331
332#if COMPILER(MSVC) && !defined(_DEBUG)
333#define TRY __try {
334#define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
335#else
336#define TRY
337#define EXCEPT(x)
338#endif
339
340int jscmain(int argc, char** argv, JSGlobalData*);
341
342int main(int argc, char** argv)
343{
344#if defined(_DEBUG) && PLATFORM(WIN_OS)
345 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
346 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
347 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
348 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
349 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
350 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
351#endif
352
ba379fdc
A
353#if COMPILER(MSVC) && !PLATFORM(WINCE)
354 timeBeginPeriod(1);
355#endif
356
9dae56ea
A
357#if PLATFORM(QT)
358 QCoreApplication app(argc, argv);
359#endif
360
361 // Initialize JSC before getting JSGlobalData.
362 JSC::initializeThreading();
363
364 // We can't use destructors in the following code because it uses Windows
365 // Structured Exception Handling
366 int res = 0;
367 JSGlobalData* globalData = JSGlobalData::create().releaseRef();
368 TRY
369 res = jscmain(argc, argv, globalData);
370 EXCEPT(res = 3)
371
372 cleanupGlobalData(globalData);
373 return res;
374}
375
376static void cleanupGlobalData(JSGlobalData* globalData)
377{
378 JSLock lock(false);
379 globalData->heap.destroy();
380 globalData->deref();
381}
382
ba379fdc 383static bool runWithScripts(GlobalObject* globalObject, const Vector<Script>& scripts, bool dump)
9dae56ea 384{
ba379fdc
A
385 UString script;
386 UString fileName;
387 Vector<char> scriptBuffer;
9dae56ea
A
388
389 if (dump)
390 BytecodeGenerator::setDumpsGeneratedCode(true);
391
392#if ENABLE(OPCODE_SAMPLING)
393 Interpreter* interpreter = globalObject->globalData()->interpreter;
394 interpreter->setSampler(new SamplingTool(interpreter));
ba379fdc
A
395 interpreter->sampler()->setup();
396#endif
397#if ENABLE(SAMPLING_FLAGS)
398 SamplingFlags::start();
9dae56ea
A
399#endif
400
401 bool success = true;
ba379fdc
A
402 for (size_t i = 0; i < scripts.size(); i++) {
403 if (scripts[i].isFile) {
404 fileName = scripts[i].argument;
405 if (!fillBufferWithContentsOfFile(fileName, scriptBuffer))
406 return false; // fail early so we can catch missing files
407 script = scriptBuffer.data();
408 } else {
409 script = scripts[i].argument;
410 fileName = "[Command Line]";
411 }
9dae56ea 412
ba379fdc
A
413#if ENABLE(SAMPLING_THREAD)
414 SamplingThread::start();
9dae56ea 415#endif
ba379fdc
A
416
417 Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script, fileName));
9dae56ea
A
418 success = success && completion.complType() != Throw;
419 if (dump) {
420 if (completion.complType() == Throw)
421 printf("Exception: %s\n", completion.value().toString(globalObject->globalExec()).ascii());
422 else
423 printf("End: %s\n", completion.value().toString(globalObject->globalExec()).ascii());
424 }
425
ba379fdc
A
426#if ENABLE(SAMPLING_THREAD)
427 SamplingThread::stop();
9dae56ea 428#endif
ba379fdc
A
429
430 globalObject->globalExec()->clearException();
9dae56ea
A
431 }
432
ba379fdc
A
433#if ENABLE(SAMPLING_FLAGS)
434 SamplingFlags::stop();
435#endif
9dae56ea
A
436#if ENABLE(OPCODE_SAMPLING)
437 interpreter->sampler()->dump(globalObject->globalExec());
438 delete interpreter->sampler();
ba379fdc
A
439#endif
440#if ENABLE(SAMPLING_COUNTERS)
441 AbstractSamplingCounter::dump();
9dae56ea
A
442#endif
443 return success;
444}
445
ba379fdc
A
446#define RUNNING_FROM_XCODE 0
447
9dae56ea
A
448static void runInteractive(GlobalObject* globalObject)
449{
450 while (true) {
ba379fdc 451#if HAVE(READLINE) && !RUNNING_FROM_XCODE
9dae56ea
A
452 char* line = readline(interactivePrompt);
453 if (!line)
454 break;
455 if (line[0])
456 add_history(line);
457 Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line, interpreterName));
458 free(line);
459#else
ba379fdc 460 printf("%s", interactivePrompt);
9dae56ea
A
461 Vector<char, 256> line;
462 int c;
463 while ((c = getchar()) != EOF) {
464 // FIXME: Should we also break on \r?
465 if (c == '\n')
466 break;
467 line.append(c);
468 }
ba379fdc
A
469 if (line.isEmpty())
470 break;
9dae56ea
A
471 line.append('\0');
472 Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line.data(), interpreterName));
473#endif
474 if (completion.complType() == Throw)
475 printf("Exception: %s\n", completion.value().toString(globalObject->globalExec()).ascii());
476 else
477 printf("%s\n", completion.value().toString(globalObject->globalExec()).UTF8String().c_str());
478
479 globalObject->globalExec()->clearException();
480 }
481 printf("\n");
482}
483
ba379fdc 484static NO_RETURN void printUsageStatement(JSGlobalData* globalData, bool help = false)
9dae56ea
A
485{
486 fprintf(stderr, "Usage: jsc [options] [files] [-- arguments]\n");
487 fprintf(stderr, " -d Dumps bytecode (debug builds only)\n");
ba379fdc 488 fprintf(stderr, " -e Evaluate argument as script code\n");
9dae56ea
A
489 fprintf(stderr, " -f Specifies a source file (deprecated)\n");
490 fprintf(stderr, " -h|--help Prints this help message\n");
491 fprintf(stderr, " -i Enables interactive mode (default if no files are specified)\n");
ba379fdc 492#if HAVE(SIGNAL_H)
9dae56ea 493 fprintf(stderr, " -s Installs signal handlers that exit on a crash (Unix platforms only)\n");
ba379fdc
A
494#endif
495
496 cleanupGlobalData(globalData);
497 exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
9dae56ea
A
498}
499
ba379fdc 500static void parseArguments(int argc, char** argv, Options& options, JSGlobalData* globalData)
9dae56ea
A
501{
502 int i = 1;
503 for (; i < argc; ++i) {
504 const char* arg = argv[i];
505 if (strcmp(arg, "-f") == 0) {
506 if (++i == argc)
ba379fdc
A
507 printUsageStatement(globalData);
508 options.scripts.append(Script(true, argv[i]));
509 continue;
510 }
511 if (strcmp(arg, "-e") == 0) {
512 if (++i == argc)
513 printUsageStatement(globalData);
514 options.scripts.append(Script(false, argv[i]));
9dae56ea
A
515 continue;
516 }
517 if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
ba379fdc 518 printUsageStatement(globalData, true);
9dae56ea
A
519 }
520 if (strcmp(arg, "-i") == 0) {
521 options.interactive = true;
522 continue;
523 }
524 if (strcmp(arg, "-d") == 0) {
525 options.dump = true;
526 continue;
527 }
528 if (strcmp(arg, "-s") == 0) {
ba379fdc 529#if HAVE(SIGNAL_H)
9dae56ea
A
530 signal(SIGILL, _exit);
531 signal(SIGFPE, _exit);
532 signal(SIGBUS, _exit);
533 signal(SIGSEGV, _exit);
534#endif
535 continue;
536 }
537 if (strcmp(arg, "--") == 0) {
538 ++i;
539 break;
540 }
ba379fdc 541 options.scripts.append(Script(true, argv[i]));
9dae56ea
A
542 }
543
ba379fdc 544 if (options.scripts.isEmpty())
9dae56ea
A
545 options.interactive = true;
546
547 for (; i < argc; ++i)
548 options.arguments.append(argv[i]);
549}
550
551int jscmain(int argc, char** argv, JSGlobalData* globalData)
552{
553 JSLock lock(false);
554
555 Options options;
ba379fdc 556 parseArguments(argc, argv, options, globalData);
9dae56ea
A
557
558 GlobalObject* globalObject = new (globalData) GlobalObject(options.arguments);
ba379fdc 559 bool success = runWithScripts(globalObject, options.scripts, options.dump);
9dae56ea
A
560 if (options.interactive && success)
561 runInteractive(globalObject);
562
563 return success ? 0 : 3;
564}
565
566static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer)
567{
568 FILE* f = fopen(fileName.UTF8String().c_str(), "r");
569 if (!f) {
570 fprintf(stderr, "Could not open file: %s\n", fileName.UTF8String().c_str());
571 return false;
572 }
573
574 size_t buffer_size = 0;
575 size_t buffer_capacity = 1024;
576
577 buffer.resize(buffer_capacity);
578
579 while (!feof(f) && !ferror(f)) {
580 buffer_size += fread(buffer.data() + buffer_size, 1, buffer_capacity - buffer_size, f);
581 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
582 buffer_capacity *= 2;
583 buffer.resize(buffer_capacity);
584 }
585 }
586 fclose(f);
587 buffer[buffer_size] = '\0';
588
589 return true;
590}