1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2000 Harri Porten ([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., 59 Temple Place - Suite 330,
|
---|
19 | * Boston, MA 02111-1307, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <string.h>
|
---|
25 |
|
---|
26 | #include "value.h"
|
---|
27 | #include "object.h"
|
---|
28 | #include "types.h"
|
---|
29 | #include "interpreter.h"
|
---|
30 |
|
---|
31 | using namespace KJS;
|
---|
32 |
|
---|
33 | class TestFunctionImp : public ObjectImp {
|
---|
34 | public:
|
---|
35 | TestFunctionImp() : ObjectImp() {}
|
---|
36 | virtual bool implementsCall() const { return true; }
|
---|
37 | virtual Value call(ExecState *exec, Object &thisObj, const List &args);
|
---|
38 | };
|
---|
39 |
|
---|
40 | Value TestFunctionImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
|
---|
41 | {
|
---|
42 | fprintf(stderr,"--> %s\n",args[0].toString(exec).ascii());
|
---|
43 | return Undefined();
|
---|
44 | }
|
---|
45 |
|
---|
46 | class VersionFunctionImp : public ObjectImp {
|
---|
47 | public:
|
---|
48 | VersionFunctionImp() : ObjectImp() {}
|
---|
49 | virtual bool implementsCall() const { return true; }
|
---|
50 | virtual Value call(ExecState *exec, Object &thisObj, const List &args);
|
---|
51 | };
|
---|
52 |
|
---|
53 | Value VersionFunctionImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
|
---|
54 | {
|
---|
55 | // We need this function for compatibility with the Mozilla JS tests but for now
|
---|
56 | // we don't actually do any version-specific handling
|
---|
57 | return Undefined();
|
---|
58 | }
|
---|
59 |
|
---|
60 | class GlobalImp : public ObjectImp {
|
---|
61 | public:
|
---|
62 | virtual UString className() const { return "global"; }
|
---|
63 | };
|
---|
64 |
|
---|
65 | int main(int argc, char **argv)
|
---|
66 | {
|
---|
67 | // expecting a filename
|
---|
68 | if (argc < 2) {
|
---|
69 | fprintf(stderr, "You have to specify at least one filename\n");
|
---|
70 | return -1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | bool ret = true;
|
---|
74 | {
|
---|
75 | Object global(new GlobalImp());
|
---|
76 |
|
---|
77 | // create interpreter
|
---|
78 | Interpreter interp(global);
|
---|
79 | // add debug() function
|
---|
80 | global.put(interp.globalExec(), Identifier("debug"), Object(new TestFunctionImp()));
|
---|
81 | // add "print" for compatibility with the mozilla js shell
|
---|
82 | global.put(interp.globalExec(), Identifier("print"), Object(new TestFunctionImp()));
|
---|
83 | // add "version" for compatibility with the mozilla js shell
|
---|
84 | global.put(interp.globalExec(), Identifier("version"), Object(new VersionFunctionImp()));
|
---|
85 |
|
---|
86 | const int BufferSize = 200000;
|
---|
87 | char code[BufferSize];
|
---|
88 |
|
---|
89 | for (int i = 1; i < argc; i++) {
|
---|
90 | const char *file = argv[i];
|
---|
91 | if (strcmp(file, "-f") == 0)
|
---|
92 | continue;
|
---|
93 | FILE *f = fopen(file, "r");
|
---|
94 | if (!f) {
|
---|
95 | fprintf(stderr, "Error opening %s.\n", file);
|
---|
96 | return 2;
|
---|
97 | }
|
---|
98 | int num = fread(code, 1, BufferSize, f);
|
---|
99 | code[num] = '\0';
|
---|
100 | if(num >= BufferSize)
|
---|
101 | fprintf(stderr, "Warning: File may have been too long.\n");
|
---|
102 |
|
---|
103 | // run
|
---|
104 | Completion comp(interp.evaluate(code));
|
---|
105 |
|
---|
106 | fclose(f);
|
---|
107 |
|
---|
108 | if (comp.complType() == Throw) {
|
---|
109 | ExecState *exec = interp.globalExec();
|
---|
110 | Value exVal = comp.value();
|
---|
111 | char *msg = exVal.toString(exec).ascii();
|
---|
112 | int lineno = -1;
|
---|
113 | if (exVal.type() == ObjectType) {
|
---|
114 | Value lineVal = Object::dynamicCast(exVal).get(exec,Identifier("line"));
|
---|
115 | if (lineVal.type() == NumberType)
|
---|
116 | lineno = int(lineVal.toNumber(exec));
|
---|
117 | }
|
---|
118 | if (lineno != -1)
|
---|
119 | fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
|
---|
120 | else
|
---|
121 | fprintf(stderr,"Exception: %s\n",msg);
|
---|
122 | ret = false;
|
---|
123 | }
|
---|
124 | else if (comp.complType() == ReturnValue) {
|
---|
125 | char *msg = comp.value().toString(interp.globalExec()).ascii();
|
---|
126 | fprintf(stderr,"Return value: %s\n",msg);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | } // end block, so that Interpreter and global get deleted
|
---|
131 |
|
---|
132 | if (ret)
|
---|
133 | fprintf(stderr, "OK.\n");
|
---|
134 |
|
---|
135 | #ifdef KJS_DEBUG_MEM
|
---|
136 | Interpreter::finalCheck();
|
---|
137 | #endif
|
---|
138 | return ret ? 0 : 3;
|
---|
139 | }
|
---|