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