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

kjs

  • kjs
interpreter.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4  * Copyright (C) 2001 Peter Kelly (pmk@post.com)
5  * Copyright (C) 2003 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 Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #include "value.h"
25 #include "object.h"
26 #include "types.h"
27 #include "interpreter.h"
28 
29 #include <assert.h>
30 #include <math.h>
31 #include <stdio.h>
32 
33 #include "internal.h"
34 #include "collector.h"
35 #include "operations.h"
36 #include "error_object.h"
37 #include "debugger.h"
38 #include "nodes.h"
39 #include "context.h"
40 
41 using namespace KJS;
42 
43 // ------------------------------ Context --------------------------------------
44 
45 const ScopeChain &Context::scopeChain() const
46 {
47  return rep->scopeChain();
48 }
49 
50 Object Context::variableObject() const
51 {
52  return rep->variableObject();
53 }
54 
55 Object Context::thisValue() const
56 {
57  return rep->thisValue();
58 }
59 
60 const Context Context::callingContext() const
61 {
62  return rep->callingContext();
63 }
64 
65 CodeType Context::codeType() const
66 {
67  return rep->codeType();
68 }
69 
70 int Context::sourceId() const
71 {
72  return rep->sourceId;
73 }
74 
75 int Context::curStmtFirstLine() const
76 {
77  return rep->line0;
78 }
79 
80 int Context::curStmtLastLine() const
81 {
82  return rep->line1;
83 }
84 
85 Object Context::function() const
86 {
87  return Object(rep->function());
88 }
89 
90 Identifier Context::functionName() const
91 {
92  return rep->functionName;
93 }
94 
95 List Context::args() const
96 {
97  return rep->args;
98 }
99 
100 bool KJS::operator==(const Context &c1, const Context &c2)
101 {
102  return (c1.imp() == c2.imp());
103 }
104 
105 bool KJS::operator!=(const Context &c1, const Context &c2)
106 {
107  return (c1.imp() != c2.imp());
108 }
109 
110 // ------------------------------ Interpreter ---------------------------------
111 
112 Interpreter::Interpreter(const Object &global)
113 {
114  rep = new InterpreterImp(this,global);
115 }
116 
117 Interpreter::Interpreter()
118 {
119  Object global(new ObjectImp());
120  rep = new InterpreterImp(this,global);
121 }
122 
123 Interpreter::~Interpreter()
124 {
125  delete rep;
126 }
127 
128 Object &Interpreter::globalObject() const
129 {
130  return rep->globalObject();
131 }
132 
133 void Interpreter::initGlobalObject()
134 {
135  rep->initGlobalObject();
136 }
137 
138 void Interpreter::lock()
139 {
140  InterpreterImp::lock();
141 }
142 
143 void Interpreter::unlock()
144 {
145  InterpreterImp::unlock();
146 }
147 
148 ExecState *Interpreter::globalExec()
149 {
150  return rep->globalExec();
151 }
152 
153 bool Interpreter::checkSyntax(const UString &code, int *errLine, UString *errMsg)
154 {
155  return rep->checkSyntax(code,errLine,errMsg);
156 }
157 
158 bool Interpreter::checkSyntax(const UString &code)
159 {
160  return rep->checkSyntax(code);
161 }
162 
163 Completion Interpreter::evaluate(const UString &code, const Value &thisV)
164 {
165  return rep->evaluate(code,thisV);
166 }
167 
168 InterpreterImp *Interpreter::imp()
169 {
170  return rep;
171 }
172 
173 Object Interpreter::builtinObject() const
174 {
175  return rep->builtinObject();
176 }
177 
178 Object Interpreter::builtinFunction() const
179 {
180  return rep->builtinFunction();
181 }
182 
183 Object Interpreter::builtinArray() const
184 {
185  return rep->builtinArray();
186 }
187 
188 Object Interpreter::builtinBoolean() const
189 {
190  return rep->builtinBoolean();
191 }
192 
193 Object Interpreter::builtinString() const
194 {
195  return rep->builtinString();
196 }
197 
198 Object Interpreter::builtinNumber() const
199 {
200  return rep->builtinNumber();
201 }
202 
203 Object Interpreter::builtinDate() const
204 {
205  return rep->builtinDate();
206 }
207 
208 Object Interpreter::builtinRegExp() const
209 {
210  return rep->builtinRegExp();
211 }
212 
213 Object Interpreter::builtinError() const
214 {
215  return rep->builtinError();
216 }
217 
218 Object Interpreter::builtinObjectPrototype() const
219 {
220  return rep->builtinObjectPrototype();
221 }
222 
223 Object Interpreter::builtinFunctionPrototype() const
224 {
225  return rep->builtinFunctionPrototype();
226 }
227 
228 Object Interpreter::builtinArrayPrototype() const
229 {
230  return rep->builtinArrayPrototype();
231 }
232 
233 Object Interpreter::builtinBooleanPrototype() const
234 {
235  return rep->builtinBooleanPrototype();
236 }
237 
238 Object Interpreter::builtinStringPrototype() const
239 {
240  return rep->builtinStringPrototype();
241 }
242 
243 Object Interpreter::builtinNumberPrototype() const
244 {
245  return rep->builtinNumberPrototype();
246 }
247 
248 Object Interpreter::builtinDatePrototype() const
249 {
250  return rep->builtinDatePrototype();
251 }
252 
253 Object Interpreter::builtinRegExpPrototype() const
254 {
255  return rep->builtinRegExpPrototype();
256 }
257 
258 Object Interpreter::builtinErrorPrototype() const
259 {
260  return rep->builtinErrorPrototype();
261 }
262 
263 Object Interpreter::builtinEvalError() const
264 {
265  return rep->builtinEvalError();
266 }
267 
268 Object Interpreter::builtinRangeError() const
269 {
270  return rep->builtinRangeError();
271 }
272 
273 Object Interpreter::builtinReferenceError() const
274 {
275  return rep->builtinReferenceError();
276 }
277 
278 Object Interpreter::builtinSyntaxError() const
279 {
280  return rep->builtinSyntaxError();
281 }
282 
283 Object Interpreter::builtinTypeError() const
284 {
285  return rep->builtinTypeError();
286 }
287 
288 Object Interpreter::builtinURIError() const
289 {
290  return rep->builtinURIError();
291 }
292 
293 Object Interpreter::builtinEvalErrorPrototype() const
294 {
295  return rep->builtinEvalErrorPrototype();
296 }
297 
298 Object Interpreter::builtinRangeErrorPrototype() const
299 {
300  return rep->builtinRangeErrorPrototype();
301 }
302 
303 Object Interpreter::builtinReferenceErrorPrototype() const
304 {
305  return rep->builtinReferenceErrorPrototype();
306 }
307 
308 Object Interpreter::builtinSyntaxErrorPrototype() const
309 {
310  return rep->builtinSyntaxErrorPrototype();
311 }
312 
313 Object Interpreter::builtinTypeErrorPrototype() const
314 {
315  return rep->builtinTypeErrorPrototype();
316 }
317 
318 Object Interpreter::builtinURIErrorPrototype() const
319 {
320  return rep->builtinURIErrorPrototype();
321 }
322 
323 void Interpreter::setCompatMode(CompatMode mode)
324 {
325  rep->setCompatMode(mode);
326 }
327 
328 Interpreter::CompatMode Interpreter::compatMode() const
329 {
330  return rep->compatMode();
331 }
332 
333 bool Interpreter::collect()
334 {
335  return Collector::collect();
336 }
337 
338 #ifdef KJS_DEBUG_MEM
339 #include "lexer.h"
340 void Interpreter::finalCheck()
341 {
342  fprintf(stderr,"Interpreter::finalCheck()\n");
343  // Garbage collect - as many times as necessary
344  // (we could delete an object which was holding another object, so
345  // the deref() will happen too late for deleting the impl of the 2nd object).
346  while( Collector::collect() )
347  ;
348 
349  Node::finalCheck();
350  Collector::finalCheck();
351  Lexer::globalClear();
352  UString::globalClear();
353 }
354 #endif
355 
356 // ------------------------------ ExecState --------------------------------------
357 
358 void ExecState::setException(const Value &e)
359 {
360  if (e.isValid()) {
361  Debugger *dbg = _interpreter->imp()->debugger();
362  if (dbg)
363  dbg->exception(this,e,_context->inTryCatch());
364  }
365  _exception = e;
366 }
367 
368 void ExecState::clearException()
369 {
370  terminate_request = false;
371  _exception = Value();
372 }
373 
374 bool ExecState::terminate_request = false;
375 
376 static bool defaultConfirm() { return true; }
377 
378 bool (*ExecState::confirmTerminate)() = defaultConfirm;
379 
380 bool ExecState::hadException()
381 {
382  if (terminate_request) {
383  terminate_request = false;
384  if (confirmTerminate())
385  _exception = Error::create((ExecState*)this);
386  }
387  return _exception.isValid();
388 }
389 
390 void Interpreter::virtual_hook( int, void* )
391 { /*BASE::virtual_hook( id, data );*/ }
392 
393 
394 Interpreter *ExecState::lexicalInterpreter() const
395 {
396  // TODO: use proper implementation
397 #if 1
398  return dynamicInterpreter();
399 #else
400  if (!_context) {
401  return dynamicInterpreter();
402  }
403 
404  InterpreterImp *result = InterpreterImp::interpreterWithGlobalObject(_context->scopeChain().bottom());
405 
406  if (!result) {
407  return dynamicInterpreter();
408  }
409 
410  return result->interpreter();
411 #endif
412 }
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:167
KJS::Interpreter::builtinObject
Object builtinObject() const
Returns the builtin "Object" object.
Definition: interpreter.cpp:173
KJS::Interpreter::builtinBooleanPrototype
Object builtinBooleanPrototype() const
Returns the builtin "Boolean.prototype" object.
Definition: interpreter.cpp:233
KJS::Context::curStmtFirstLine
int curStmtFirstLine() const
The line number on which the current statement begins.
Definition: interpreter.cpp:75
KJS::Context::curStmtLastLine
int curStmtLastLine() const
The line number on which the current statement ends.
Definition: interpreter.cpp:80
KJS::ExecState::dynamicInterpreter
Interpreter * dynamicInterpreter() const
Returns the interpreter associated with this execution state.
Definition: interpreter.h:452
KJS::Context::functionName
Identifier functionName() const
In the case of FunctionCode, the name of the function being called.
Definition: interpreter.cpp:90
KJS::ScopeChain
A scope chain object.
Definition: scope_chain.h:47
KJS::Collector::collect
static bool collect()
Run the garbage collection.
Definition: collector.cpp:157
KJS::Context::args
List args() const
In the case of FunctionCode, the arguments passed to the function.
Definition: interpreter.cpp:95
KJS::Interpreter::globalObject
Object & globalObject() const
Returns the object that is used as the global object during all script execution performed by this in...
Definition: interpreter.cpp:128
KJS::Interpreter::builtinArrayPrototype
Object builtinArrayPrototype() const
Returns the builtin "Array.prototype" object.
Definition: interpreter.cpp:228
KJS::Interpreter::builtinFunction
Object builtinFunction() const
Returns the builtin "Function" object.
Definition: interpreter.cpp:178
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope's global object.
Definition: interpreter.cpp:394
KJS::Context::callingContext
const Context callingContext() const
Returns the context from which the current context was invoked.
Definition: interpreter.cpp:60
KJS::Interpreter::builtinDatePrototype
Object builtinDatePrototype() const
Returns the builtin "Date.prototype" object.
Definition: interpreter.cpp:248
KJS::Interpreter::builtinRegExpPrototype
Object builtinRegExpPrototype() const
Returns the builtin "RegExp.prototype" object.
Definition: interpreter.cpp:253
KJS::Error::create
static Object create(ExecState *exec, ErrorType errtype=GeneralError, const char *message=0, int lineno=-1, int sourceId=-1)
Factory method for error objects.
Definition: object.cpp:503
KJS::Interpreter::builtinDate
Object builtinDate() const
Returns the builtin "Date" object.
Definition: interpreter.cpp:203
KJS::Interpreter::builtinBoolean
Object builtinBoolean() const
Returns the builtin "Boolean" object.
Definition: interpreter.cpp:188
KJS::Interpreter::globalExec
ExecState * globalExec()
Returns the execution state object which can be used to execute scripts using this interpreter at a t...
Definition: interpreter.cpp:148
KJS::Interpreter
Interpreter objects can be used to evaluate ECMAScript code.
Definition: interpreter.h:172
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::Context
Represents an execution context, as specified by section 10 of the ECMA spec.
Definition: interpreter.h:72
KJS::Context::function
Object function() const
In the case of FunctionCode, the function objects being called.
Definition: interpreter.cpp:85
KJS::Interpreter::builtinError
Object builtinError() const
Returns the builtin "Error" object.
Definition: interpreter.cpp:213
KJS::Interpreter::builtinNumber
Object builtinNumber() const
Returns the builtin "Number" object.
Definition: interpreter.cpp:198
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::Interpreter::builtinErrorPrototype
Object builtinErrorPrototype() const
Returns the builtin "Error.prototype" object.
Definition: interpreter.cpp:258
KJS::Context::thisValue
Object thisValue() const
Returns the "this" value for the execution context.
Definition: interpreter.cpp:55
KJS::Interpreter::evaluate
Completion evaluate(const UString &code, const Value &thisV=Value())
Evaluates the supplied ECMAScript code.
Definition: interpreter.cpp:163
KJS
Definition: array_instance.h:27
KJS::Interpreter::builtinEvalError
Object builtinEvalError() const
The initial value of "Error" global property.
Definition: interpreter.cpp:263
KJS::Completion
Completion objects are used to convey the return status and value from functions. ...
Definition: completion.h:48
KJS::Interpreter::builtinObjectPrototype
Object builtinObjectPrototype() const
Returns the builtin "Object.prototype" object.
Definition: interpreter.cpp:218
KJS::Context::codeType
CodeType codeType() const
The type of code being executed in this context.
Definition: interpreter.cpp:65
KJS::Interpreter::checkSyntax
bool checkSyntax(const UString &code, int *errLine, UString *errMsg)
Parses the supplied ECMAScript code and checks for syntax errors.
Definition: interpreter.cpp:153
KJS::List
Native list type.
Definition: list.h:48
KJS::Interpreter::builtinString
Object builtinString() const
Returns the builtin "String" object.
Definition: interpreter.cpp:193
KJS::Context::scopeChain
const ScopeChain & scopeChain() const
Returns the scope chain for this execution context.
Definition: interpreter.cpp:45
KJS::Interpreter::builtinArray
Object builtinArray() const
Returns the builtin "Array" object.
Definition: interpreter.cpp:183
KJS::Interpreter::builtinFunctionPrototype
Object builtinFunctionPrototype() const
Returns the builtin "Function.prototype" object.
Definition: interpreter.cpp:223
KJS::Interpreter::builtinNumberPrototype
Object builtinNumberPrototype() const
Returns the builtin "Number.prototype" object.
Definition: interpreter.cpp:243
KJS::Interpreter::builtinStringPrototype
Object builtinStringPrototype() const
Returns the builtin "String.prototype" object.
Definition: interpreter.cpp:238
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:181
KJS::Interpreter::builtinRegExp
Object builtinRegExp() const
Returns the builtin "RegExp" object.
Definition: interpreter.cpp:208
KJS::Context::sourceId
int sourceId() const
The identifier of the source code fragment containing the code being executed.
Definition: interpreter.cpp:70
KJS::Context::variableObject
Object variableObject() const
Returns the variable object for the execution context.
Definition: interpreter.cpp:50
KJS::Interpreter::setCompatMode
void setCompatMode(CompatMode mode)
Call this to enable a compatibility mode with another browser.
Definition: interpreter.cpp:323
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::Interpreter::collect
static bool collect()
Run the garbage collection.
Definition: interpreter.cpp:333
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::Interpreter::Interpreter
Interpreter()
Creates a new interpreter.
Definition: interpreter.cpp:117

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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.8.8
This website is maintained by Timothy Pearson.