# kjs Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# testkjs.cpp

    00001 // -*- c-basic-offset: 2 -*-
    00002 /*
    00003  *  This file is part of the KDE libraries
    00004  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
    00005  *
    00006  *  This library is free software; you can redistribute it and/or
    00007  *  modify it under the terms of the GNU Lesser General Public
    00008  *  License as published by the Free Software Foundation; either
    00009  *  version 2 of the License, or (at your option) any later version.
    00010  *
    00011  *  This library is distributed in the hope that it will be useful,
    00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014  *  Lesser General Public License for more details.
    00015  *
    00016  *  You should have received a copy of the GNU Lesser General Public License
    00017  *  along with this library; see the file COPYING.LIB.  If not, write to
    00018  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019  *  Boston, MA 02111-1307, USA.
    00020  *
    00021  *  $Id: testkjs.cpp,v 1.22 2002/06/19 08:21:00 domi Exp $
    00022  */
    00023
    00024 #include <stdio.h>
    00025
    00026 #include "value.h"
    00027 #include "object.h"
    00028 #include "types.h"
    00029 #include "interpreter.h"
    00030
    00031 using namespace KJS;
    00032
    00033 class TestFunctionImp : public ObjectImp {
    00034 public:
    00035   TestFunctionImp() : ObjectImp() {}
    00036   virtual bool implementsCall() const { return true; }
    00037   virtual Value call(ExecState *exec, Object &thisObj, const List &args);
    00038 };
    00039
    00040 Value TestFunctionImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
    00041 {
    00042   fprintf(stderr,"--> %s\n",args[0].toString(exec).ascii());
    00043   return Undefined();
    00044 }
    00045
    00046 class GlobalImp : public ObjectImp {
    00047 public:
    00048   virtual UString className() const { return "global"; }
    00049 };
    00050
    00051 int main(int argc, char **argv)
    00052 {
    00053   // expecting a filename
    00054   if (argc < 2) {
    00055     fprintf(stderr, "You have to specify at least one filename\n");
    00056     return -1;
    00057   }
    00058
    00059   bool ret = true;
    00060   {
    00061     Object global(new GlobalImp());
    00062
    00063     // create interpreter
    00064     Interpreter interp(global);
    00065     // add debug() function
    00066     global.put(interp.globalExec(),"debug", Object(new TestFunctionImp()));
    00067     // add "print" for compatibility with the mozilla js shell
    00068     global.put(interp.globalExec(),"print", Object(new TestFunctionImp()));
    00069
    00070     const int BufferSize = 200000;
    00071     char code[BufferSize];
    00072
    00073     for (int i = 1; i < argc; i++) {
    00074       const char *file = argv[i];
    00075       FILE *f = fopen(file, "r");
    00076       if (!f) {
    00077         fprintf(stderr, "Error opening %s.\n", file);
    00078         return 2;
    00079       }
    00080       int num = fread(code, 1, BufferSize, f);
    00081       code[num] = '\0';
    00082       if(num >= BufferSize)
    00083         fprintf(stderr, "Warning: File may have been too long.\n");
    00084
    00085       // run
    00086       Completion comp(interp.evaluate(code));
    00087
    00088       fclose(f);
    00089
    00090       if (comp.complType() == Throw) {
    00091         ExecState *exec = interp.globalExec();
    00092         Value exVal = comp.value();
    00093         char *msg = exVal.toString(exec).ascii();
    00094         int lineno = -1;
    00095         if (exVal.type() == ObjectType) {
    00096           Value lineVal = Object::dynamicCast(exVal).get(exec,"line");
    00097           if (lineVal.type() == NumberType)
    00098             lineno = int(lineVal.toNumber(exec));
    00099         }
    00100         if (lineno != -1)
    00101           fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
    00102         else
    00103           fprintf(stderr,"Exception: %s\n",msg);
    00104         ret = false;
    00105       }
    00106       else if (comp.complType() == ReturnValue) {
    00107         char *msg = comp.value().toString(interp.globalExec()).ascii();
    00108         fprintf(stderr,"Return value: %s\n",msg);
    00109       }
    00110     }
    00111
    00112   } // end block, so that Interpreter and global get deleted
    00113
    00114   if (ret)
    00115     fprintf(stderr, "OK.\n");
    00116
    00117 #ifdef KJS_DEBUG_MEM
    00118   Interpreter::finalCheck();
    00119 #endif
    00120   return ret ? 0 : 1;
    00121 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:35 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001