testkjs.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 &, 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
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
00064 Interpreter interp(global);
00065
00066 global.put(interp.globalExec(),"debug", Object(new TestFunctionImp()));
00067
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
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 }
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.4.