source: webkit/trunk/Source/JavaScriptCore/jsc.cpp@ 99333

Last change on this file since 99333 was 99223, checked in by [email protected], 14 years ago

De-virtualize JSObject::className
https://bugs.webkit.org/show_bug.cgi?id=71428

Reviewed by Sam Weinig.

Source/JavaScriptCore:

Added className to the MethodTable, changed all the virtual
implementations of className to static ones, and replaced
all call sites with corresponding lookups in the MethodTable.

  • API/JSCallbackObject.h:
  • API/JSCallbackObjectFunctions.h:

(JSC::::className):

(JSC::DebuggerActivation::className):

  • debugger/DebuggerActivation.h:
  • jsc.cpp:

(GlobalObject::createStructure):

  • profiler/Profiler.cpp:

(JSC::Profiler::createCallIdentifier):

  • runtime/ClassInfo.h:
  • runtime/JSCell.cpp:

(JSC::JSCell::className):

  • runtime/JSCell.h:
  • runtime/JSObject.cpp:

(JSC::JSObject::className):

  • runtime/JSObject.h:
  • runtime/ObjectPrototype.cpp:

(JSC::objectProtoFuncToString):

  • testRegExp.cpp:

(GlobalObject::createStructure):

Source/JavaScriptGlue:

Added className to the MethodTable, changed all the virtual
implementations of className to static ones, and replaced
all call sites with corresponding lookups in the MethodTable.

  • JSUtils.cpp:

(KJSValueToCFTypeInternal):

Source/WebCore:

No new tests.

Added className to the MethodTable, changed all the virtual
implementations of className to static ones, and replaced
all call sites with corresponding lookups in the MethodTable.

  • bindings/js/JSDOMWindowShell.cpp:

(WebCore::JSDOMWindowShell::className):

  • bindings/js/JSDOMWindowShell.h:
  • bindings/js/JSInjectedScriptHostCustom.cpp:

(WebCore::JSInjectedScriptHost::internalConstructorName):

  • bridge/testqtbindings.cpp:

(Global::className):

Source/WebKit/efl:

Added className to the MethodTable, changed all the virtual
implementations of className to static ones, and replaced
all call sites with corresponding lookups in the MethodTable.

  • ewk/ewk_js.cpp:

(ewk_js_npobject_to_object):

  • Property svn:eol-style set to native
File size: 19.8 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2006 Bjoern Graf ([email protected])
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 "CurrentTime.h"
28#include "ExceptionHelpers.h"
29#include "InitializeThreading.h"
30#include "JSArray.h"
31#include "JSFunction.h"
32#include "JSLock.h"
33#include "JSString.h"
34#include "SamplingTool.h"
35#include <math.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#if !OS(WINDOWS)
41#include <unistd.h>
42#endif
43
44#if HAVE(READLINE)
45#include <readline/history.h>
46#include <readline/readline.h>
47#endif
48
49#if HAVE(SYS_TIME_H)
50#include <sys/time.h>
51#endif
52
53#if HAVE(SIGNAL_H)
54#include <signal.h>
55#endif
56
57#if COMPILER(MSVC) && !OS(WINCE)
58#include <crtdbg.h>
59#include <mmsystem.h>
60#include <windows.h>
61#endif
62
63#if PLATFORM(QT)
64#include <QCoreApplication>
65#include <QDateTime>
66#endif
67
68using namespace JSC;
69using namespace WTF;
70
71static void cleanupGlobalData(JSGlobalData*);
72static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer);
73
74static EncodedJSValue JSC_HOST_CALL functionPrint(ExecState*);
75static EncodedJSValue JSC_HOST_CALL functionDebug(ExecState*);
76static EncodedJSValue JSC_HOST_CALL functionGC(ExecState*);
77#ifndef NDEBUG
78static EncodedJSValue JSC_HOST_CALL functionReleaseExecutableMemory(ExecState*);
79#endif
80static EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*);
81static EncodedJSValue JSC_HOST_CALL functionRun(ExecState*);
82static EncodedJSValue JSC_HOST_CALL functionLoad(ExecState*);
83static EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState*);
84static EncodedJSValue JSC_HOST_CALL functionReadline(ExecState*);
85static EncodedJSValue JSC_HOST_CALL functionPreciseTime(ExecState*);
86static NO_RETURN_WITH_VALUE EncodedJSValue JSC_HOST_CALL functionQuit(ExecState*);
87
88#if ENABLE(SAMPLING_FLAGS)
89static EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState*);
90static EncodedJSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState*);
91#endif
92
93struct Script {
94 bool isFile;
95 char* argument;
96
97 Script(bool isFile, char *argument)
98 : isFile(isFile)
99 , argument(argument)
100 {
101 }
102};
103
104struct Options {
105 Options()
106 : interactive(false)
107 , dump(false)
108 {
109 }
110
111 bool interactive;
112 bool dump;
113 Vector<Script> scripts;
114 Vector<UString> arguments;
115};
116
117static const char interactivePrompt[] = "> ";
118
119class StopWatch {
120public:
121 void start();
122 void stop();
123 long getElapsedMS(); // call stop() first
124
125private:
126 double m_startTime;
127 double m_stopTime;
128};
129
130void StopWatch::start()
131{
132 m_startTime = currentTime();
133}
134
135void StopWatch::stop()
136{
137 m_stopTime = currentTime();
138}
139
140long StopWatch::getElapsedMS()
141{
142 return static_cast<long>((m_stopTime - m_startTime) * 1000);
143}
144
145class GlobalObject : public JSGlobalObject {
146private:
147 GlobalObject(JSGlobalData&, Structure*);
148
149public:
150 typedef JSGlobalObject Base;
151
152 static GlobalObject* create(JSGlobalData& globalData, Structure* structure, const Vector<UString>& arguments)
153 {
154 GlobalObject* object = new (allocateCell<GlobalObject>(globalData.heap)) GlobalObject(globalData, structure);
155 object->finishCreation(globalData, arguments);
156 return object;
157 }
158
159 static const ClassInfo s_info;
160
161 static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
162 {
163 return Structure::create(globalData, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), &s_info);
164 }
165
166protected:
167 void finishCreation(JSGlobalData& globalData, const Vector<UString>& arguments)
168 {
169 Base::finishCreation(globalData);
170
171 addFunction(globalData, "debug", functionDebug, 1);
172 addFunction(globalData, "print", functionPrint, 1);
173 addFunction(globalData, "quit", functionQuit, 0);
174 addFunction(globalData, "gc", functionGC, 0);
175#ifndef NDEBUG
176 addFunction(globalData, "releaseExecutableMemory", functionReleaseExecutableMemory, 0);
177#endif
178 addFunction(globalData, "version", functionVersion, 1);
179 addFunction(globalData, "run", functionRun, 1);
180 addFunction(globalData, "load", functionLoad, 1);
181 addFunction(globalData, "checkSyntax", functionCheckSyntax, 1);
182 addFunction(globalData, "readline", functionReadline, 0);
183 addFunction(globalData, "preciseTime", functionPreciseTime, 0);
184#if ENABLE(SAMPLING_FLAGS)
185 addFunction(globalData, "setSamplingFlags", functionSetSamplingFlags, 1);
186 addFunction(globalData, "clearSamplingFlags", functionClearSamplingFlags, 1);
187#endif
188
189 JSObject* array = constructEmptyArray(globalExec());
190 for (size_t i = 0; i < arguments.size(); ++i)
191 array->methodTable()->putByIndex(array, globalExec(), i, jsString(globalExec(), arguments[i]));
192 putDirect(globalData, Identifier(globalExec(), "arguments"), array);
193 }
194
195 void addFunction(JSGlobalData& globalData, const char* name, NativeFunction function, unsigned arguments)
196 {
197 Identifier identifier(globalExec(), name);
198 putDirect(globalData, identifier, JSFunction::create(globalExec(), this, arguments, identifier, function));
199 }
200};
201COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
202ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
203
204const ClassInfo GlobalObject::s_info = { "global", &JSGlobalObject::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(GlobalObject) };
205
206GlobalObject::GlobalObject(JSGlobalData& globalData, Structure* structure)
207 : JSGlobalObject(globalData, structure)
208{
209}
210
211EncodedJSValue JSC_HOST_CALL functionPrint(ExecState* exec)
212{
213 for (unsigned i = 0; i < exec->argumentCount(); ++i) {
214 if (i)
215 putchar(' ');
216
217 printf("%s", exec->argument(i).toString(exec).utf8().data());
218 }
219
220 putchar('\n');
221 fflush(stdout);
222 return JSValue::encode(jsUndefined());
223}
224
225EncodedJSValue JSC_HOST_CALL functionDebug(ExecState* exec)
226{
227 fprintf(stderr, "--> %s\n", exec->argument(0).toString(exec).utf8().data());
228 return JSValue::encode(jsUndefined());
229}
230
231EncodedJSValue JSC_HOST_CALL functionGC(ExecState* exec)
232{
233 JSLock lock(SilenceAssertionsOnly);
234 exec->heap()->collectAllGarbage();
235 return JSValue::encode(jsUndefined());
236}
237
238#ifndef NDEBUG
239EncodedJSValue JSC_HOST_CALL functionReleaseExecutableMemory(ExecState* exec)
240{
241 JSLock lock(SilenceAssertionsOnly);
242 exec->globalData().releaseExecutableMemory();
243 return JSValue::encode(jsUndefined());
244}
245#endif
246
247EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*)
248{
249 // We need this function for compatibility with the Mozilla JS tests but for now
250 // we don't actually do any version-specific handling
251 return JSValue::encode(jsUndefined());
252}
253
254EncodedJSValue JSC_HOST_CALL functionRun(ExecState* exec)
255{
256 UString fileName = exec->argument(0).toString(exec);
257 Vector<char> script;
258 if (!fillBufferWithContentsOfFile(fileName, script))
259 return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
260
261 GlobalObject* globalObject = GlobalObject::create(exec->globalData(), GlobalObject::createStructure(exec->globalData(), jsNull()), Vector<UString>());
262
263 StopWatch stopWatch;
264 stopWatch.start();
265 evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
266 stopWatch.stop();
267
268 return JSValue::encode(jsNumber(stopWatch.getElapsedMS()));
269}
270
271EncodedJSValue JSC_HOST_CALL functionLoad(ExecState* exec)
272{
273 UString fileName = exec->argument(0).toString(exec);
274 Vector<char> script;
275 if (!fillBufferWithContentsOfFile(fileName, script))
276 return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
277
278 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
279
280 JSValue evaluationException;
281 JSValue result = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName), JSValue(), &evaluationException);
282 if (evaluationException)
283 throwError(exec, evaluationException);
284 return JSValue::encode(result);
285}
286
287EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState* exec)
288{
289 UString fileName = exec->argument(0).toString(exec);
290 Vector<char> script;
291 if (!fillBufferWithContentsOfFile(fileName, script))
292 return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
293
294 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
295
296 StopWatch stopWatch;
297 stopWatch.start();
298
299 JSValue syntaxException;
300 bool validSyntax = checkSyntax(globalObject->globalExec(), makeSource(script.data(), fileName), &syntaxException);
301 stopWatch.stop();
302
303 if (!validSyntax)
304 throwError(exec, syntaxException);
305 return JSValue::encode(jsNumber(stopWatch.getElapsedMS()));
306}
307
308#if ENABLE(SAMPLING_FLAGS)
309EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState* exec)
310{
311 for (unsigned i = 0; i < exec->argumentCount(); ++i) {
312 unsigned flag = static_cast<unsigned>(exec->argument(i).toNumber(exec));
313 if ((flag >= 1) && (flag <= 32))
314 SamplingFlags::setFlag(flag);
315 }
316 return JSValue::encode(jsNull());
317}
318
319EncodedJSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState* exec)
320{
321 for (unsigned i = 0; i < exec->argumentCount(); ++i) {
322 unsigned flag = static_cast<unsigned>(exec->argument(i).toNumber(exec));
323 if ((flag >= 1) && (flag <= 32))
324 SamplingFlags::clearFlag(flag);
325 }
326 return JSValue::encode(jsNull());
327}
328#endif
329
330EncodedJSValue JSC_HOST_CALL functionReadline(ExecState* exec)
331{
332 Vector<char, 256> line;
333 int c;
334 while ((c = getchar()) != EOF) {
335 // FIXME: Should we also break on \r?
336 if (c == '\n')
337 break;
338 line.append(c);
339 }
340 line.append('\0');
341 return JSValue::encode(jsString(exec, line.data()));
342}
343
344EncodedJSValue JSC_HOST_CALL functionPreciseTime(ExecState*)
345{
346 return JSValue::encode(jsNumber(currentTime()));
347}
348
349EncodedJSValue JSC_HOST_CALL functionQuit(ExecState* exec)
350{
351 // Technically, destroying the heap in the middle of JS execution is a no-no,
352 // but we want to maintain compatibility with the Mozilla test suite, so
353 // we pretend that execution has terminated to avoid ASSERTs, then tear down the heap.
354 exec->globalData().dynamicGlobalObject = 0;
355
356 cleanupGlobalData(&exec->globalData());
357 exit(EXIT_SUCCESS);
358
359#if COMPILER(MSVC) && OS(WINCE)
360 // Without this, Visual Studio will complain that this method does not return a value.
361 return JSValue::encode(jsUndefined());
362#endif
363}
364
365// Use SEH for Release builds only to get rid of the crash report dialog
366// (luckily the same tests fail in Release and Debug builds so far). Need to
367// be in a separate main function because the jscmain function requires object
368// unwinding.
369
370#if COMPILER(MSVC) && !COMPILER(INTEL) && !defined(_DEBUG) && !OS(WINCE)
371#define TRY __try {
372#define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
373#else
374#define TRY
375#define EXCEPT(x)
376#endif
377
378int jscmain(int argc, char** argv, JSGlobalData*);
379
380int main(int argc, char** argv)
381{
382#if OS(WINDOWS)
383#if !OS(WINCE)
384 // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
385 // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
386 // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
387 ::SetErrorMode(0);
388#endif
389
390#if defined(_DEBUG)
391 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
392 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
393 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
394 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
395 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
396 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
397#endif
398
399 timeBeginPeriod(1);
400#endif
401
402#if PLATFORM(QT)
403 QCoreApplication app(argc, argv);
404#endif
405
406 // Initialize JSC before getting JSGlobalData.
407 JSC::initializeThreading();
408
409 // We can't use destructors in the following code because it uses Windows
410 // Structured Exception Handling
411 int res = 0;
412 JSGlobalData* globalData = JSGlobalData::create(ThreadStackTypeLarge, LargeHeap).leakRef();
413 TRY
414 res = jscmain(argc, argv, globalData);
415 EXCEPT(res = 3)
416
417 cleanupGlobalData(globalData);
418 return res;
419}
420
421static void cleanupGlobalData(JSGlobalData* globalData)
422{
423 JSLock lock(SilenceAssertionsOnly);
424 globalData->clearBuiltinStructures();
425 globalData->heap.destroy();
426 globalData->deref();
427}
428
429static bool runWithScripts(GlobalObject* globalObject, const Vector<Script>& scripts, bool dump)
430{
431 UString script;
432 UString fileName;
433 Vector<char> scriptBuffer;
434
435 if (dump)
436 BytecodeGenerator::setDumpsGeneratedCode(true);
437
438 JSGlobalData& globalData = globalObject->globalData();
439
440#if ENABLE(SAMPLING_FLAGS)
441 SamplingFlags::start();
442#endif
443
444 bool success = true;
445 for (size_t i = 0; i < scripts.size(); i++) {
446 if (scripts[i].isFile) {
447 fileName = scripts[i].argument;
448 if (!fillBufferWithContentsOfFile(fileName, scriptBuffer))
449 return false; // fail early so we can catch missing files
450 script = scriptBuffer.data();
451 } else {
452 script = scripts[i].argument;
453 fileName = "[Command Line]";
454 }
455
456 globalData.startSampling();
457
458 JSValue evaluationException;
459 JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script, fileName), JSValue(), &evaluationException);
460 success = success && !evaluationException;
461 if (dump) {
462 if (evaluationException)
463 printf("Exception: %s\n", evaluationException.toString(globalObject->globalExec()).utf8().data());
464 else
465 printf("End: %s\n", returnValue.toString(globalObject->globalExec()).utf8().data());
466 }
467
468 globalData.stopSampling();
469 globalObject->globalExec()->clearException();
470 }
471
472#if ENABLE(SAMPLING_FLAGS)
473 SamplingFlags::stop();
474#endif
475 globalData.dumpSampleData(globalObject->globalExec());
476#if ENABLE(SAMPLING_COUNTERS)
477 AbstractSamplingCounter::dump();
478#endif
479#if ENABLE(REGEXP_TRACING)
480 globalData.dumpRegExpTrace();
481#endif
482 return success;
483}
484
485#define RUNNING_FROM_XCODE 0
486
487static void runInteractive(GlobalObject* globalObject)
488{
489 UString interpreterName("Interpreter");
490
491 while (true) {
492#if HAVE(READLINE) && !RUNNING_FROM_XCODE
493 char* line = readline(interactivePrompt);
494 if (!line)
495 break;
496 if (line[0])
497 add_history(line);
498 JSValue evaluationException;
499 JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line, interpreterName), JSValue(), &evaluationException);
500 free(line);
501#else
502 printf("%s", interactivePrompt);
503 Vector<char, 256> line;
504 int c;
505 while ((c = getchar()) != EOF) {
506 // FIXME: Should we also break on \r?
507 if (c == '\n')
508 break;
509 line.append(c);
510 }
511 if (line.isEmpty())
512 break;
513 line.append('\0');
514
515 JSValue evaluationException;
516 JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line.data(), interpreterName), JSValue(), &evaluationException);
517#endif
518 if (evaluationException)
519 printf("Exception: %s\n", evaluationException.toString(globalObject->globalExec()).utf8().data());
520 else
521 printf("%s\n", returnValue.toString(globalObject->globalExec()).utf8().data());
522
523 globalObject->globalExec()->clearException();
524 }
525 printf("\n");
526}
527
528static NO_RETURN void printUsageStatement(JSGlobalData* globalData, bool help = false)
529{
530 fprintf(stderr, "Usage: jsc [options] [files] [-- arguments]\n");
531 fprintf(stderr, " -d Dumps bytecode (debug builds only)\n");
532 fprintf(stderr, " -e Evaluate argument as script code\n");
533 fprintf(stderr, " -f Specifies a source file (deprecated)\n");
534 fprintf(stderr, " -h|--help Prints this help message\n");
535 fprintf(stderr, " -i Enables interactive mode (default if no files are specified)\n");
536#if HAVE(SIGNAL_H)
537 fprintf(stderr, " -s Installs signal handlers that exit on a crash (Unix platforms only)\n");
538#endif
539
540 cleanupGlobalData(globalData);
541 exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
542}
543
544static void parseArguments(int argc, char** argv, Options& options, JSGlobalData* globalData)
545{
546 int i = 1;
547 for (; i < argc; ++i) {
548 const char* arg = argv[i];
549 if (!strcmp(arg, "-f")) {
550 if (++i == argc)
551 printUsageStatement(globalData);
552 options.scripts.append(Script(true, argv[i]));
553 continue;
554 }
555 if (!strcmp(arg, "-e")) {
556 if (++i == argc)
557 printUsageStatement(globalData);
558 options.scripts.append(Script(false, argv[i]));
559 continue;
560 }
561 if (!strcmp(arg, "-i")) {
562 options.interactive = true;
563 continue;
564 }
565 if (!strcmp(arg, "-d")) {
566 options.dump = true;
567 continue;
568 }
569 if (!strcmp(arg, "-s")) {
570#if HAVE(SIGNAL_H)
571 signal(SIGILL, _exit);
572 signal(SIGFPE, _exit);
573 signal(SIGBUS, _exit);
574 signal(SIGSEGV, _exit);
575#endif
576 continue;
577 }
578 if (!strcmp(arg, "--")) {
579 ++i;
580 break;
581 }
582 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
583 printUsageStatement(globalData, true);
584 options.scripts.append(Script(true, argv[i]));
585 }
586
587 if (options.scripts.isEmpty())
588 options.interactive = true;
589
590 for (; i < argc; ++i)
591 options.arguments.append(argv[i]);
592}
593
594int jscmain(int argc, char** argv, JSGlobalData* globalData)
595{
596 JSLock lock(SilenceAssertionsOnly);
597
598 Options options;
599 parseArguments(argc, argv, options, globalData);
600
601 GlobalObject* globalObject = GlobalObject::create(*globalData, GlobalObject::createStructure(*globalData, jsNull()), options.arguments);
602 bool success = runWithScripts(globalObject, options.scripts, options.dump);
603 if (options.interactive && success)
604 runInteractive(globalObject);
605
606 return success ? 0 : 3;
607}
608
609static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer)
610{
611 FILE* f = fopen(fileName.utf8().data(), "r");
612 if (!f) {
613 fprintf(stderr, "Could not open file: %s\n", fileName.utf8().data());
614 return false;
615 }
616
617 size_t bufferSize = 0;
618 size_t bufferCapacity = 1024;
619
620 buffer.resize(bufferCapacity);
621
622 while (!feof(f) && !ferror(f)) {
623 bufferSize += fread(buffer.data() + bufferSize, 1, bufferCapacity - bufferSize, f);
624 if (bufferSize == bufferCapacity) { // guarantees space for trailing '\0'
625 bufferCapacity *= 2;
626 buffer.resize(bufferCapacity);
627 }
628 }
629 fclose(f);
630 buffer[bufferSize] = '\0';
631
632 if (buffer[0] == '#' && buffer[1] == '!')
633 buffer[0] = buffer[1] = '/';
634
635 return true;
636}
Note: See TracBrowser for help on using the repository browser.