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

kjs

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

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
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kjs by doxygen 1.9.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |