source: webkit/trunk/JavaScriptCore/kjs/testkjs.cpp@ 9768

Last change on this file since 9768 was 9768, checked in by ggaren, 20 years ago

-rolled in patches for http://bugzilla.opendarwin.org/show_bug.cgi?id=3945
[PATCH] Safe merges of comments and other trivialities from KDE's kjs

-patch by Martijn Klingens <[email protected]>

  • kjs/array_instance.h:
  • kjs/array_object.cpp:
  • kjs/array_object.h:
  • kjs/bool_object.cpp:
  • kjs/bool_object.h:
  • kjs/collector.cpp:
  • kjs/collector.h:
  • kjs/completion.h:
  • kjs/context.h:
  • kjs/date_object.cpp:
  • kjs/date_object.h:
  • kjs/debugger.cpp:
  • kjs/debugger.h:
  • kjs/dtoa.h:
  • kjs/error_object.cpp:
  • kjs/error_object.h:
  • kjs/function.cpp:
  • kjs/function.h:
  • kjs/function_object.cpp:
  • kjs/function_object.h:
  • kjs/grammar.y:
  • kjs/identifier.cpp:
  • kjs/identifier.h:
  • kjs/internal.cpp:
  • kjs/internal.h:
  • kjs/interpreter.cpp:
  • kjs/interpreter.h:
  • kjs/interpreter_map.cpp:
  • kjs/interpreter_map.h:
  • kjs/lexer.cpp:
  • kjs/lexer.h:
  • kjs/list.cpp:
  • kjs/list.h:
  • kjs/lookup.cpp:
  • kjs/lookup.h:
  • kjs/math_object.cpp:
  • kjs/math_object.h:
  • kjs/nodes.cpp:
  • kjs/nodes.h:
  • kjs/nodes2string.cpp:
  • kjs/number_object.cpp:
  • kjs/number_object.h:
  • kjs/object.cpp:
  • kjs/object.h:
  • kjs/object_object.cpp:
  • kjs/object_object.h:
  • kjs/operations.cpp:
  • kjs/operations.h:
  • kjs/property_map.cpp:
  • kjs/property_map.h:
  • kjs/reference.cpp:
  • kjs/reference.h:
  • kjs/reference_list.cpp:
  • kjs/reference_list.h:
  • kjs/regexp.cpp:
  • kjs/regexp.h:
  • kjs/regexp_object.cpp:
  • kjs/regexp_object.h:
  • kjs/scope_chain.cpp:
  • kjs/scope_chain.h:
  • kjs/simple_number.h:
  • kjs/string_object.cpp:
  • kjs/string_object.h:
  • kjs/testkjs.cpp:
  • kjs/types.h:
  • kjs/ustring.cpp:
  • kjs/ustring.h:
  • kjs/value.cpp:
  • kjs/value.h:
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
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 * Copyright (C) 2004 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "value.h"
29#include "object.h"
30#include "types.h"
31#include "interpreter.h"
32
33using namespace KJS;
34
35class TestFunctionImp : public ObjectImp {
36public:
37 TestFunctionImp(int i, int length);
38 virtual bool implementsCall() const { return true; }
39 virtual Value call(ExecState *exec, Object &thisObj, const List &args);
40
41 enum { Print, Debug, Quit };
42
43private:
44 int id;
45};
46
47TestFunctionImp::TestFunctionImp(int i, int length) : ObjectImp(), id(i)
48{
49 putDirect(lengthPropertyName,length,DontDelete|ReadOnly|DontEnum);
50}
51
52Value TestFunctionImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
53{
54 switch (id) {
55 case Print:
56 case Debug:
57 fprintf(stderr,"--> %s\n",args[0].toString(exec).ascii());
58 return Undefined();
59 case Quit:
60 exit(0);
61 return Undefined();
62 default:
63 break;
64 }
65
66 return Undefined();
67}
68
69class VersionFunctionImp : public ObjectImp {
70public:
71 VersionFunctionImp() : ObjectImp() {}
72 virtual bool implementsCall() const { return true; }
73 virtual Value call(ExecState *exec, Object &thisObj, const List &args);
74};
75
76Value VersionFunctionImp::call(ExecState */*exec*/, Object &/*thisObj*/, const List &/*args*/)
77{
78 // We need this function for compatibility with the Mozilla JS tests but for now
79 // we don't actually do any version-specific handling
80 return Undefined();
81}
82
83class GlobalImp : public ObjectImp {
84public:
85 virtual UString className() const { return "global"; }
86};
87
88int main(int argc, char **argv)
89{
90 // expecting a filename
91 if (argc < 2) {
92 fprintf(stderr, "You have to specify at least one filename\n");
93 return -1;
94 }
95
96 bool ret = true;
97 {
98 Interpreter::lock();
99
100 Object global(new GlobalImp());
101
102 // create interpreter
103 Interpreter interp(global);
104 // add debug() function
105 global.put(interp.globalExec(), "debug", Object(new TestFunctionImp(TestFunctionImp::Debug,1)));
106 // add "print" for compatibility with the mozilla js shell
107 global.put(interp.globalExec(), "print", Object(new TestFunctionImp(TestFunctionImp::Print,1)));
108 // add "quit" for compatibility with the mozilla js shell
109 global.put(interp.globalExec(), "quit", Object(new TestFunctionImp(TestFunctionImp::Quit,0)));
110 // add "version" for compatibility with the mozilla js shell
111 global.put(interp.globalExec(), "version", Object(new VersionFunctionImp()));
112
113 for (int i = 1; i < argc; i++) {
114 int code_len = 0;
115 int code_alloc = 1024;
116 char *code = (char*)malloc(code_alloc);
117
118 const char *file = argv[i];
119 if (strcmp(file, "-f") == 0)
120 continue;
121 FILE *f = fopen(file, "r");
122 if (!f) {
123 fprintf(stderr, "Error opening %s.\n", file);
124 return 2;
125 }
126
127 while (!feof(f) && !ferror(f)) {
128 size_t len = fread(code+code_len,1,code_alloc-code_len,f);
129 code_len += len;
130 if (code_len >= code_alloc) {
131 code_alloc *= 2;
132 code = (char*)realloc(code,code_alloc);
133 }
134 }
135 code = (char*)realloc(code,code_len+1);
136 code[code_len] = '\0';
137
138 // run
139 Completion comp(interp.evaluate(file, 1, code));
140
141 fclose(f);
142
143 if (comp.complType() == Throw) {
144 ExecState *exec = interp.globalExec();
145 Value exVal = comp.value();
146 char *msg = exVal.toString(exec).ascii();
147 int lineno = -1;
148 if (exVal.type() == ObjectType) {
149 Value lineVal = Object::dynamicCast(exVal).get(exec,"line");
150 if (lineVal.type() == NumberType)
151 lineno = int(lineVal.toNumber(exec));
152 }
153 if (lineno != -1)
154 fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
155 else
156 fprintf(stderr,"Exception: %s\n",msg);
157 ret = false;
158 }
159 else if (comp.complType() == ReturnValue) {
160 char *msg = comp.value().toString(interp.globalExec()).ascii();
161 fprintf(stderr,"Return value: %s\n",msg);
162 }
163
164 free(code);
165 }
166
167 Interpreter::unlock();
168 } // end block, so that Interpreter and global get deleted
169
170 if (ret)
171 fprintf(stderr, "OK.\n");
172
173#ifdef KJS_DEBUG_MEM
174 Interpreter::finalCheck();
175#endif
176 return ret ? 0 : 3;
177}
Note: See TracBrowser for help on using the repository browser.