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 |
|
---|
25 | #include "value.h"
|
---|
26 | #include "object.h"
|
---|
27 | #include "types.h"
|
---|
28 | #include "interpreter.h"
|
---|
29 |
|
---|
30 | using namespace KJS;
|
---|
31 |
|
---|
32 | class TestFunctionImp : public ObjectImp {
|
---|
33 | public:
|
---|
34 | TestFunctionImp() : ObjectImp() {}
|
---|
35 | virtual bool implementsCall() const { return true; }
|
---|
36 | virtual Value call(ExecState *exec, Object &thisObj, const List &args);
|
---|
37 | };
|
---|
38 |
|
---|
39 | Value TestFunctionImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
|
---|
40 | {
|
---|
41 | fprintf(stderr,"--> %s\n",args[0].toString(exec).ascii());
|
---|
42 | return Undefined();
|
---|
43 | }
|
---|
44 |
|
---|
45 | class GlobalImp : public ObjectImp {
|
---|
46 | public:
|
---|
47 | virtual UString className() const { return "global"; }
|
---|
48 | };
|
---|
49 |
|
---|
50 | int main(int argc, char **argv)
|
---|
51 | {
|
---|
52 | // expecting a filename
|
---|
53 | if (argc < 2) {
|
---|
54 | fprintf(stderr, "You have to specify at least one filename\n");
|
---|
55 | return -1;
|
---|
56 | }
|
---|
57 |
|
---|
58 | bool ret = true;
|
---|
59 | {
|
---|
60 | Object global(new GlobalImp());
|
---|
61 |
|
---|
62 | // create interpreter
|
---|
63 | Interpreter interp(global);
|
---|
64 | // add debug() function
|
---|
65 | global.put(interp.globalExec(),"debug", Object(new TestFunctionImp()));
|
---|
66 | // add "print" for compatibility with the mozilla js shell
|
---|
67 | global.put(interp.globalExec(),"print", Object(new TestFunctionImp()));
|
---|
68 |
|
---|
69 | const int BufferSize = 200000;
|
---|
70 | char code[BufferSize];
|
---|
71 |
|
---|
72 | for (int i = 1; i < argc; i++) {
|
---|
73 | const char *file = argv[i];
|
---|
74 | FILE *f = fopen(file, "r");
|
---|
75 | if (!f) {
|
---|
76 | fprintf(stderr, "Error opening %s.\n", file);
|
---|
77 | return 2;
|
---|
78 | }
|
---|
79 | int num = fread(code, 1, BufferSize, f);
|
---|
80 | code[num] = '\0';
|
---|
81 | if(num >= BufferSize)
|
---|
82 | fprintf(stderr, "Warning: File may have been too long.\n");
|
---|
83 |
|
---|
84 | // run
|
---|
85 | Completion comp(interp.evaluate(code));
|
---|
86 |
|
---|
87 | fclose(f);
|
---|
88 |
|
---|
89 | if (comp.complType() == Throw) {
|
---|
90 | ExecState *exec = interp.globalExec();
|
---|
91 | Value exVal = comp.value();
|
---|
92 | char *msg = exVal.toString(exec).ascii();
|
---|
93 | int lineno = -1;
|
---|
94 | if (exVal.type() == ObjectType) {
|
---|
95 | Value lineVal = Object::dynamicCast(exVal).get(exec,"line");
|
---|
96 | if (lineVal.type() == NumberType)
|
---|
97 | lineno = int(lineVal.toNumber(exec));
|
---|
98 | }
|
---|
99 | if (lineno != -1)
|
---|
100 | fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
|
---|
101 | else
|
---|
102 | fprintf(stderr,"Exception: %s\n",msg);
|
---|
103 | ret = false;
|
---|
104 | }
|
---|
105 | else if (comp.complType() == ReturnValue) {
|
---|
106 | char *msg = comp.value().toString(interp.globalExec()).ascii();
|
---|
107 | fprintf(stderr,"Return value: %s\n",msg);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | } // end block, so that Interpreter and global get deleted
|
---|
112 |
|
---|
113 | if (ret)
|
---|
114 | fprintf(stderr, "OK.\n");
|
---|
115 |
|
---|
116 | #ifdef KJS_DEBUG_MEM
|
---|
117 | Interpreter::finalCheck();
|
---|
118 | #endif
|
---|
119 | return ret ? 0 : 1;
|
---|
120 | }
|
---|