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 <stdio.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <string.h>
|
---|
27 |
|
---|
28 | #include "value.h"
|
---|
29 | #include "object.h"
|
---|
30 | #include "types.h"
|
---|
31 | #include "interpreter.h"
|
---|
32 |
|
---|
33 | using namespace KJS;
|
---|
34 |
|
---|
35 | class TestFunctionImp : public ObjectImp {
|
---|
36 | public:
|
---|
37 | TestFunctionImp(int i, int length);
|
---|
38 | virtual bool implementsCall() const { return true; }
|
---|
39 | virtual Value call(ExecState *exec, Object &thisObj, const List &args);
|
---|
40 |
|
---|
41 | enum { Print, Debug, Quit };
|
---|
42 |
|
---|
43 | private:
|
---|
44 | int id;
|
---|
45 | };
|
---|
46 |
|
---|
47 | TestFunctionImp::TestFunctionImp(int i, int length) : ObjectImp(), id(i)
|
---|
48 | {
|
---|
49 | putDirect(lengthPropertyName,length,DontDelete|ReadOnly|DontEnum);
|
---|
50 | }
|
---|
51 |
|
---|
52 | Value TestFunctionImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
|
---|
53 | {
|
---|
54 | switch (id) {
|
---|
55 | case Print:
|
---|
56 | case Debug:
|
---|
57 | fprintf(stderr,"--> %s\n",args[0].toString(exec).ascii());
|
---|
58 | return Undefined();
|
---|
59 | case Quit:
|
---|
60 | exit(0);
|
---|
61 | return Undefined();
|
---|
62 | default:
|
---|
63 | break;
|
---|
64 | }
|
---|
65 |
|
---|
66 | return Undefined();
|
---|
67 | }
|
---|
68 |
|
---|
69 | class VersionFunctionImp : public ObjectImp {
|
---|
70 | public:
|
---|
71 | VersionFunctionImp() : ObjectImp() {}
|
---|
72 | virtual bool implementsCall() const { return true; }
|
---|
73 | virtual Value call(ExecState *exec, Object &thisObj, const List &args);
|
---|
74 | };
|
---|
75 |
|
---|
76 | Value VersionFunctionImp::call(ExecState */*exec*/, Object &/*thisObj*/, const List &/*args*/)
|
---|
77 | {
|
---|
78 | // We need this function for compatibility with the Mozilla JS tests but for now
|
---|
79 | // we don't actually do any version-specific handling
|
---|
80 | return Undefined();
|
---|
81 | }
|
---|
82 |
|
---|
83 | class GlobalImp : public ObjectImp {
|
---|
84 | public:
|
---|
85 | virtual UString className() const { return "global"; }
|
---|
86 | };
|
---|
87 |
|
---|
88 | int main(int argc, char **argv)
|
---|
89 | {
|
---|
90 | // expecting a filename
|
---|
91 | if (argc < 2) {
|
---|
92 | fprintf(stderr, "You have to specify at least one filename\n");
|
---|
93 | return -1;
|
---|
94 | }
|
---|
95 |
|
---|
96 | bool ret = true;
|
---|
97 | {
|
---|
98 | Interpreter::lock();
|
---|
99 |
|
---|
100 | Object global(new GlobalImp());
|
---|
101 |
|
---|
102 | // create interpreter
|
---|
103 | Interpreter interp(global);
|
---|
104 | // add debug() function
|
---|
105 | global.put(interp.globalExec(), "debug", Object(new TestFunctionImp(TestFunctionImp::Debug,1)));
|
---|
106 | // add "print" for compatibility with the mozilla js shell
|
---|
107 | global.put(interp.globalExec(), "print", Object(new TestFunctionImp(TestFunctionImp::Print,1)));
|
---|
108 | // add "quit" for compatibility with the mozilla js shell
|
---|
109 | global.put(interp.globalExec(), "quit", Object(new TestFunctionImp(TestFunctionImp::Quit,0)));
|
---|
110 | // add "version" for compatibility with the mozilla js shell
|
---|
111 | global.put(interp.globalExec(), "version", Object(new VersionFunctionImp()));
|
---|
112 |
|
---|
113 | for (int i = 1; i < argc; i++) {
|
---|
114 | int code_len = 0;
|
---|
115 | int code_alloc = 1024;
|
---|
116 | char *code = (char*)malloc(code_alloc);
|
---|
117 |
|
---|
118 | const char *file = argv[i];
|
---|
119 | if (strcmp(file, "-f") == 0)
|
---|
120 | continue;
|
---|
121 | FILE *f = fopen(file, "r");
|
---|
122 | if (!f) {
|
---|
123 | fprintf(stderr, "Error opening %s.\n", file);
|
---|
124 | return 2;
|
---|
125 | }
|
---|
126 |
|
---|
127 | while (!feof(f) && !ferror(f)) {
|
---|
128 | size_t len = fread(code+code_len,1,code_alloc-code_len,f);
|
---|
129 | code_len += len;
|
---|
130 | if (code_len >= code_alloc) {
|
---|
131 | code_alloc *= 2;
|
---|
132 | code = (char*)realloc(code,code_alloc);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | code = (char*)realloc(code,code_len+1);
|
---|
136 | code[code_len] = '\0';
|
---|
137 |
|
---|
138 | // run
|
---|
139 | Completion comp(interp.evaluate(file, 1, code));
|
---|
140 |
|
---|
141 | fclose(f);
|
---|
142 |
|
---|
143 | if (comp.complType() == Throw) {
|
---|
144 | ExecState *exec = interp.globalExec();
|
---|
145 | Value exVal = comp.value();
|
---|
146 | char *msg = exVal.toString(exec).ascii();
|
---|
147 | int lineno = -1;
|
---|
148 | if (exVal.type() == ObjectType) {
|
---|
149 | Value lineVal = Object::dynamicCast(exVal).get(exec,"line");
|
---|
150 | if (lineVal.type() == NumberType)
|
---|
151 | lineno = int(lineVal.toNumber(exec));
|
---|
152 | }
|
---|
153 | if (lineno != -1)
|
---|
154 | fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
|
---|
155 | else
|
---|
156 | fprintf(stderr,"Exception: %s\n",msg);
|
---|
157 | ret = false;
|
---|
158 | }
|
---|
159 | else if (comp.complType() == ReturnValue) {
|
---|
160 | char *msg = comp.value().toString(interp.globalExec()).ascii();
|
---|
161 | fprintf(stderr,"Return value: %s\n",msg);
|
---|
162 | }
|
---|
163 |
|
---|
164 | free(code);
|
---|
165 | }
|
---|
166 |
|
---|
167 | Interpreter::unlock();
|
---|
168 | } // end block, so that Interpreter and global get deleted
|
---|
169 |
|
---|
170 | if (ret)
|
---|
171 | fprintf(stderr, "OK.\n");
|
---|
172 |
|
---|
173 | #ifdef KJS_DEBUG_MEM
|
---|
174 | Interpreter::finalCheck();
|
---|
175 | #endif
|
---|
176 | return ret ? 0 : 3;
|
---|
177 | }
|
---|