• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
testkjs.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "value.h"
27#include "object.h"
28#include "types.h"
29#include "interpreter.h"
30
31using namespace KJS;
32
33class TestFunctionImp : public ObjectImp {
34public:
35 TestFunctionImp(int i, int length);
36 virtual bool implementsCall() const { return true; }
37 virtual Value call(ExecState *exec, Object &thisObj, const List &args);
38
39 enum { Print, Debug, Quit };
40
41private:
42 int id;
43};
44
45TestFunctionImp::TestFunctionImp(int i, int length) : ObjectImp(), id(i)
46{
47 putDirect(lengthPropertyName,length,DontDelete|ReadOnly|DontEnum);
48}
49
50Value TestFunctionImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
51{
52 switch (id) {
53 case Print:
54 case Debug:
55 fprintf(stderr,"--> %s\n",args[0].toString(exec).ascii());
56 return Undefined();
57 case Quit:
58 exit(0);
59 return Undefined();
60 default:
61 break;
62 }
63
64 return Undefined();
65}
66
67class VersionFunctionImp : public ObjectImp {
68public:
69 VersionFunctionImp() : ObjectImp() {}
70 virtual bool implementsCall() const { return true; }
71 virtual Value call(ExecState *exec, Object &thisObj, const List &args);
72};
73
74Value VersionFunctionImp::call(ExecState */*exec*/, Object &/*thisObj*/, const List &/*args*/)
75{
76 // We need this function for compatibility with the Mozilla JS tests but for now
77 // we don't actually do any version-specific handling
78 return Undefined();
79}
80
81class GlobalImp : public ObjectImp {
82public:
83 virtual UString className() const { return "global"; }
84};
85
86int main(int argc, char **argv)
87{
88 // expecting a filename
89 if (argc < 2) {
90 fprintf(stderr, "You have to specify at least one filename\n");
91 return -1;
92 }
93
94 bool ret = true;
95 {
96 Object global(new GlobalImp());
97
98 // create interpreter
99 Interpreter interp(global);
100 // add debug() function
101 global.put(interp.globalExec(), "debug", Object(new TestFunctionImp(TestFunctionImp::Debug,1)));
102 // add "print" for compatibility with the mozilla js shell
103 global.put(interp.globalExec(), "print", Object(new TestFunctionImp(TestFunctionImp::Print,1)));
104 // add "quit" for compatibility with the mozilla js shell
105 global.put(interp.globalExec(), "quit", Object(new TestFunctionImp(TestFunctionImp::Quit,0)));
106 // add "version" for compatibility with the mozilla js shell
107 global.put(interp.globalExec(), "version", Object(new VersionFunctionImp()));
108
109 for (int i = 1; i < argc; i++) {
110 int code_len = 0;
111 int code_alloc = 1024;
112 char *code = (char*)malloc(code_alloc);
113
114 const char *file = argv[i];
115 if (strcmp(file, "-f") == 0)
116 continue;
117 FILE *f = fopen(file, "r");
118 if (!f) {
119 fprintf(stderr, "Error opening %s.\n", file);
120 return 2;
121 }
122
123 while (!feof(f) && !ferror(f)) {
124 size_t len = fread(code+code_len,1,code_alloc-code_len,f);
125 code_len += len;
126 if (code_len >= code_alloc) {
127 code_alloc *= 2;
128 code = (char*)realloc(code,code_alloc);
129 }
130 }
131 code = (char*)realloc(code,code_len+1);
132 code[code_len] = '\0';
133
134 // run
135 Completion comp(interp.evaluate(code));
136
137 fclose(f);
138
139 if (comp.complType() == Throw) {
140 ExecState *exec = interp.globalExec();
141 Value exVal = comp.value();
142 char *msg = exVal.toString(exec).ascii();
143 int lineno = -1;
144 if (exVal.type() == ObjectType) {
145 Value lineVal = Object::dynamicCast(exVal).get(exec,"line");
146 if (lineVal.type() == NumberType)
147 lineno = int(lineVal.toNumber(exec));
148 }
149 if (lineno != -1)
150 fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
151 else
152 fprintf(stderr,"Exception: %s\n",msg);
153 ret = false;
154 }
155 else if (comp.complType() == ReturnValue) {
156 char *msg = comp.value().toString(interp.globalExec()).ascii();
157 fprintf(stderr,"Return value: %s\n",msg);
158 }
159
160 free(code);
161 }
162
163 } // end block, so that Interpreter and global get deleted
164
165 if (ret)
166 fprintf(stderr, "OK.\n");
167
168#ifdef KJS_DEBUG_MEM
169 Interpreter::finalCheck();
170#endif
171 return ret ? 0 : 3;
172}
KJS::Completion
Completion objects are used to convey the return status and value from functions.
Definition: completion.h:48
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::Interpreter
Interpreter objects can be used to evaluate ECMAScript code.
Definition: interpreter.h:172
KJS::List
Native list type.
Definition: list.h:48
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::UString::ascii
char * ascii() const
Convert the Unicode string to plain ASCII chars chopping of any higher bytes.
Definition: ustring.cpp:485
KJS::Undefined
Represents an primitive Undefined value.
Definition: value.h:269
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167
KJS::Value::toString
UString toString(ExecState *exec) const
Performs the ToString type conversion operation on this value (ECMA 9.8)
Definition: value.h:246
KJS::Value::type
Type type() const
Returns the type of value.
Definition: value.h:195
KJS::Value::toNumber
double toNumber(ExecState *exec) const
Performs the ToNumber type conversion operation on this value (ECMA 9.3)
Definition: value.h:221

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.9.4
This website is maintained by Timothy Pearson.