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 | * Copyright (C) 2004 Apple Computer, Inc.
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Library General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Library General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Library General Public License
|
---|
18 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
19 | * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
|
---|
20 | * Boston, MA 02110-1301, USA.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "config.h"
|
---|
25 | #include <stdio.h>
|
---|
26 | #include <stdlib.h>
|
---|
27 | #include <string.h>
|
---|
28 |
|
---|
29 | #include "HashTraits.h"
|
---|
30 | #include "value.h"
|
---|
31 | #include "object.h"
|
---|
32 | #include "types.h"
|
---|
33 | #include "interpreter.h"
|
---|
34 | #include "JSLock.h"
|
---|
35 |
|
---|
36 | using namespace KJS;
|
---|
37 | using namespace KXMLCore;
|
---|
38 |
|
---|
39 | class TestFunctionImp : public JSObject {
|
---|
40 | public:
|
---|
41 | TestFunctionImp(int i, int length);
|
---|
42 | virtual bool implementsCall() const { return true; }
|
---|
43 | virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
|
---|
44 |
|
---|
45 | enum { Print, Debug, Quit, GC };
|
---|
46 |
|
---|
47 | private:
|
---|
48 | int id;
|
---|
49 | };
|
---|
50 |
|
---|
51 | TestFunctionImp::TestFunctionImp(int i, int length) : JSObject(), id(i)
|
---|
52 | {
|
---|
53 | putDirect(lengthPropertyName,length,DontDelete|ReadOnly|DontEnum);
|
---|
54 | }
|
---|
55 |
|
---|
56 | JSValue *TestFunctionImp::callAsFunction(ExecState *exec, JSObject */*thisObj*/, const List &args)
|
---|
57 | {
|
---|
58 | switch (id) {
|
---|
59 | case Print:
|
---|
60 | case Debug:
|
---|
61 | fprintf(stderr,"--> %s\n",args[0]->toString(exec).ascii());
|
---|
62 | return jsUndefined();
|
---|
63 | case Quit:
|
---|
64 | exit(0);
|
---|
65 | return jsUndefined();
|
---|
66 | case GC:
|
---|
67 | {
|
---|
68 | JSLock lock;
|
---|
69 | Interpreter::collect();
|
---|
70 | }
|
---|
71 | break;
|
---|
72 | default:
|
---|
73 | break;
|
---|
74 | }
|
---|
75 |
|
---|
76 | return jsUndefined();
|
---|
77 | }
|
---|
78 |
|
---|
79 | class VersionFunctionImp : public JSObject {
|
---|
80 | public:
|
---|
81 | VersionFunctionImp() : JSObject() {}
|
---|
82 | virtual bool implementsCall() const { return true; }
|
---|
83 | virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
|
---|
84 | };
|
---|
85 |
|
---|
86 | JSValue *VersionFunctionImp::callAsFunction(ExecState */*exec*/, JSObject */*thisObj*/, const List &/*args*/)
|
---|
87 | {
|
---|
88 | // We need this function for compatibility with the Mozilla JS tests but for now
|
---|
89 | // we don't actually do any version-specific handling
|
---|
90 | return jsUndefined();
|
---|
91 | }
|
---|
92 |
|
---|
93 | class GlobalImp : public JSObject {
|
---|
94 | public:
|
---|
95 | virtual UString className() const { return "global"; }
|
---|
96 | };
|
---|
97 |
|
---|
98 | int main(int argc, char **argv)
|
---|
99 | {
|
---|
100 | // expecting a filename
|
---|
101 | if (argc < 2) {
|
---|
102 | fprintf(stderr, "You have to specify at least one filename\n");
|
---|
103 | return -1;
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool ret = true;
|
---|
107 | {
|
---|
108 | JSLock lock;
|
---|
109 |
|
---|
110 | // Unit tests for KXMLCore::IsInteger. Don't have a better place for them now.
|
---|
111 | // FIXME: move these once we create a unit test directory for KXMLCore.
|
---|
112 | assert(IsInteger<bool>::value);
|
---|
113 | assert(IsInteger<char>::value);
|
---|
114 | assert(IsInteger<signed char>::value);
|
---|
115 | assert(IsInteger<unsigned char>::value);
|
---|
116 | assert(IsInteger<short>::value);
|
---|
117 | assert(IsInteger<unsigned short>::value);
|
---|
118 | assert(IsInteger<int>::value);
|
---|
119 | assert(IsInteger<unsigned int>::value);
|
---|
120 | assert(IsInteger<long>::value);
|
---|
121 | assert(IsInteger<unsigned long>::value);
|
---|
122 | assert(IsInteger<long long>::value);
|
---|
123 | assert(IsInteger<unsigned long long>::value);
|
---|
124 |
|
---|
125 | assert(!IsInteger<char *>::value);
|
---|
126 | assert(!IsInteger<const char *>::value);
|
---|
127 | assert(!IsInteger<volatile char *>::value);
|
---|
128 | assert(!IsInteger<double>::value);
|
---|
129 | assert(!IsInteger<float>::value);
|
---|
130 | assert(!IsInteger<GlobalImp>::value);
|
---|
131 |
|
---|
132 | JSObject *global(new GlobalImp());
|
---|
133 |
|
---|
134 | // create interpreter
|
---|
135 | Interpreter interp(global);
|
---|
136 | // add debug() function
|
---|
137 | global->put(interp.globalExec(), "debug", new TestFunctionImp(TestFunctionImp::Debug, 1));
|
---|
138 | // add "print" for compatibility with the mozilla js shell
|
---|
139 | global->put(interp.globalExec(), "print", new TestFunctionImp(TestFunctionImp::Print, 1));
|
---|
140 | // add "quit" for compatibility with the mozilla js shell
|
---|
141 | global->put(interp.globalExec(), "quit", new TestFunctionImp(TestFunctionImp::Quit, 0));
|
---|
142 | // add "gc" for compatibility with the mozilla js shell
|
---|
143 | global->put(interp.globalExec(), "gc", new TestFunctionImp(TestFunctionImp::GC, 0));
|
---|
144 | // add "version" for compatibility with the mozilla js shell
|
---|
145 | global->put(interp.globalExec(), "version", new VersionFunctionImp());
|
---|
146 |
|
---|
147 | for (int i = 1; i < argc; i++) {
|
---|
148 | int code_len = 0;
|
---|
149 | int code_alloc = 1024;
|
---|
150 | char *code = (char*)malloc(code_alloc);
|
---|
151 |
|
---|
152 | const char *file = argv[i];
|
---|
153 | if (strcmp(file, "-f") == 0)
|
---|
154 | continue;
|
---|
155 | FILE *f = fopen(file, "r");
|
---|
156 | if (!f) {
|
---|
157 | fprintf(stderr, "Error opening %s.\n", file);
|
---|
158 | return 2;
|
---|
159 | }
|
---|
160 |
|
---|
161 | while (!feof(f) && !ferror(f)) {
|
---|
162 | size_t len = fread(code+code_len,1,code_alloc-code_len,f);
|
---|
163 | code_len += len;
|
---|
164 | if (code_len >= code_alloc) {
|
---|
165 | code_alloc *= 2;
|
---|
166 | code = (char*)realloc(code,code_alloc);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | code = (char*)realloc(code,code_len+1);
|
---|
170 | code[code_len] = '\0';
|
---|
171 |
|
---|
172 | // run
|
---|
173 | Completion comp(interp.evaluate(file, 1, code));
|
---|
174 |
|
---|
175 | fclose(f);
|
---|
176 |
|
---|
177 | if (comp.complType() == Throw) {
|
---|
178 | ExecState *exec = interp.globalExec();
|
---|
179 | JSValue *exVal = comp.value();
|
---|
180 | char *msg = exVal->toString(exec).ascii();
|
---|
181 | int lineno = -1;
|
---|
182 | if (exVal->isObject()) {
|
---|
183 | JSValue *lineVal = static_cast<JSObject *>(exVal)->get(exec,"line");
|
---|
184 | if (lineVal->isNumber())
|
---|
185 | lineno = int(lineVal->toNumber(exec));
|
---|
186 | }
|
---|
187 | if (lineno != -1)
|
---|
188 | fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
|
---|
189 | else
|
---|
190 | fprintf(stderr,"Exception: %s\n",msg);
|
---|
191 | ret = false;
|
---|
192 | }
|
---|
193 | else if (comp.complType() == ReturnValue) {
|
---|
194 | char *msg = comp.value()->toString(interp.globalExec()).ascii();
|
---|
195 | fprintf(stderr,"Return value: %s\n",msg);
|
---|
196 | }
|
---|
197 |
|
---|
198 | free(code);
|
---|
199 | }
|
---|
200 | } // end block, so that interpreter gets deleted
|
---|
201 |
|
---|
202 | if (ret)
|
---|
203 | fprintf(stderr, "OK.\n");
|
---|
204 |
|
---|
205 | #ifdef KJS_DEBUG_MEM
|
---|
206 | Interpreter::finalCheck();
|
---|
207 | #endif
|
---|
208 | return ret ? 0 : 3;
|
---|
209 | }
|
---|