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

kjs

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

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.8.13
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |