• 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
41using namespace KJS;
42
43// ------------------------------ Context --------------------------------------
44
45const ScopeChain &Context::scopeChain() const
46{
47 return rep->scopeChain();
48}
49
50Object Context::variableObject() const
51{
52 return rep->variableObject();
53}
54
55Object Context::thisValue() const
56{
57 return rep->thisValue();
58}
59
60const Context Context::callingContext() const
61{
62 return rep->callingContext();
63}
64
65CodeType Context::codeType() const
66{
67 return rep->codeType();
68}
69
70int Context::sourceId() const
71{
72 return rep->sourceId;
73}
74
75int Context::curStmtFirstLine() const
76{
77 return rep->line0;
78}
79
80int Context::curStmtLastLine() const
81{
82 return rep->line1;
83}
84
85Object Context::function() const
86{
87 return Object(rep->function());
88}
89
90Identifier Context::functionName() const
91{
92 return rep->functionName;
93}
94
95List Context::args() const
96{
97 return rep->args;
98}
99
100bool KJS::operator==(const Context &c1, const Context &c2)
101{
102 return (c1.imp() == c2.imp());
103}
104
105bool KJS::operator!=(const Context &c1, const Context &c2)
106{
107 return (c1.imp() != c2.imp());
108}
109
110// ------------------------------ Interpreter ---------------------------------
111
112Interpreter::Interpreter(const Object &global)
113{
114 rep = new InterpreterImp(this,global);
115}
116
117Interpreter::Interpreter()
118{
119 Object global(new ObjectImp());
120 rep = new InterpreterImp(this,global);
121}
122
123Interpreter::~Interpreter()
124{
125 delete rep;
126}
127
128Object &Interpreter::globalObject() const
129{
130 return rep->globalObject();
131}
132
133void Interpreter::initGlobalObject()
134{
135 rep->initGlobalObject();
136}
137
138void Interpreter::lock()
139{
140 InterpreterImp::lock();
141}
142
143void Interpreter::unlock()
144{
145 InterpreterImp::unlock();
146}
147
148ExecState *Interpreter::globalExec()
149{
150 return rep->globalExec();
151}
152
153bool Interpreter::checkSyntax(const UString &code, int *errLine, UString *errMsg)
154{
155 return rep->checkSyntax(code,errLine,errMsg);
156}
157
158bool Interpreter::checkSyntax(const UString &code)
159{
160 return rep->checkSyntax(code);
161}
162
163Completion Interpreter::evaluate(const UString &code, const Value &thisV)
164{
165 return rep->evaluate(code,thisV);
166}
167
168InterpreterImp *Interpreter::imp()
169{
170 return rep;
171}
172
173Object Interpreter::builtinObject() const
174{
175 return rep->builtinObject();
176}
177
178Object Interpreter::builtinFunction() const
179{
180 return rep->builtinFunction();
181}
182
183Object Interpreter::builtinArray() const
184{
185 return rep->builtinArray();
186}
187
188Object Interpreter::builtinBoolean() const
189{
190 return rep->builtinBoolean();
191}
192
193Object Interpreter::builtinString() const
194{
195 return rep->builtinString();
196}
197
198Object Interpreter::builtinNumber() const
199{
200 return rep->builtinNumber();
201}
202
203Object Interpreter::builtinDate() const
204{
205 return rep->builtinDate();
206}
207
208Object Interpreter::builtinRegExp() const
209{
210 return rep->builtinRegExp();
211}
212
213Object Interpreter::builtinError() const
214{
215 return rep->builtinError();
216}
217
218Object Interpreter::builtinObjectPrototype() const
219{
220 return rep->builtinObjectPrototype();
221}
222
223Object Interpreter::builtinFunctionPrototype() const
224{
225 return rep->builtinFunctionPrototype();
226}
227
228Object Interpreter::builtinArrayPrototype() const
229{
230 return rep->builtinArrayPrototype();
231}
232
233Object Interpreter::builtinBooleanPrototype() const
234{
235 return rep->builtinBooleanPrototype();
236}
237
238Object Interpreter::builtinStringPrototype() const
239{
240 return rep->builtinStringPrototype();
241}
242
243Object Interpreter::builtinNumberPrototype() const
244{
245 return rep->builtinNumberPrototype();
246}
247
248Object Interpreter::builtinDatePrototype() const
249{
250 return rep->builtinDatePrototype();
251}
252
253Object Interpreter::builtinRegExpPrototype() const
254{
255 return rep->builtinRegExpPrototype();
256}
257
258Object Interpreter::builtinErrorPrototype() const
259{
260 return rep->builtinErrorPrototype();
261}
262
263Object Interpreter::builtinEvalError() const
264{
265 return rep->builtinEvalError();
266}
267
268Object Interpreter::builtinRangeError() const
269{
270 return rep->builtinRangeError();
271}
272
273Object Interpreter::builtinReferenceError() const
274{
275 return rep->builtinReferenceError();
276}
277
278Object Interpreter::builtinSyntaxError() const
279{
280 return rep->builtinSyntaxError();
281}
282
283Object Interpreter::builtinTypeError() const
284{
285 return rep->builtinTypeError();
286}
287
288Object Interpreter::builtinURIError() const
289{
290 return rep->builtinURIError();
291}
292
293Object Interpreter::builtinEvalErrorPrototype() const
294{
295 return rep->builtinEvalErrorPrototype();
296}
297
298Object Interpreter::builtinRangeErrorPrototype() const
299{
300 return rep->builtinRangeErrorPrototype();
301}
302
303Object Interpreter::builtinReferenceErrorPrototype() const
304{
305 return rep->builtinReferenceErrorPrototype();
306}
307
308Object Interpreter::builtinSyntaxErrorPrototype() const
309{
310 return rep->builtinSyntaxErrorPrototype();
311}
312
313Object Interpreter::builtinTypeErrorPrototype() const
314{
315 return rep->builtinTypeErrorPrototype();
316}
317
318Object Interpreter::builtinURIErrorPrototype() const
319{
320 return rep->builtinURIErrorPrototype();
321}
322
323void Interpreter::setCompatMode(CompatMode mode)
324{
325 rep->setCompatMode(mode);
326}
327
328Interpreter::CompatMode Interpreter::compatMode() const
329{
330 return rep->compatMode();
331}
332
333bool Interpreter::collect()
334{
335 return Collector::collect();
336}
337
338#ifdef KJS_DEBUG_MEM
339#include "lexer.h"
340void 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
358void 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
368void ExecState::clearException()
369{
370 terminate_request = false;
371 _exception = Value();
372}
373
374bool ExecState::terminate_request = false;
375
376static bool defaultConfirm() { return true; }
377
378bool (*ExecState::confirmTerminate)() = defaultConfirm;
379
380bool 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
390void Interpreter::virtual_hook( int, void* )
391{ /*BASE::virtual_hook( id, data );*/ }
392
393
394Interpreter *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::Collector::collect
static bool collect()
Run the garbage collection.
Definition: collector.cpp:157
KJS::Completion
Completion objects are used to convey the return status and value from functions.
Definition: completion.h:48
KJS::Context
Represents an execution context, as specified by section 10 of the ECMA spec.
Definition: interpreter.h:72
KJS::Context::functionName
Identifier functionName() const
In the case of FunctionCode, the name of the function being called.
Definition: interpreter.cpp:90
KJS::Context::thisValue
Object thisValue() const
Returns the "this" value for the execution context.
Definition: interpreter.cpp:55
KJS::Context::args
List args() const
In the case of FunctionCode, the arguments passed to the function.
Definition: interpreter.cpp:95
KJS::Context::callingContext
const Context callingContext() const
Returns the context from which the current context was invoked.
Definition: interpreter.cpp:60
KJS::Context::sourceId
int sourceId() const
The identifier of the source code fragment containing the code being executed.
Definition: interpreter.cpp:70
KJS::Context::codeType
CodeType codeType() const
The type of code being executed in this context.
Definition: interpreter.cpp:65
KJS::Context::function
Object function() const
In the case of FunctionCode, the function objects being called.
Definition: interpreter.cpp:85
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::Context::variableObject
Object variableObject() const
Returns the variable object for the execution context.
Definition: interpreter.cpp:50
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::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::ExecState::dynamicInterpreter
Interpreter * dynamicInterpreter() const
Returns the interpreter associated with this execution state.
Definition: interpreter.h:452
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope's global object.
Definition: interpreter.cpp:394
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::Interpreter
Interpreter objects can be used to evaluate ECMAScript code.
Definition: interpreter.h:172
KJS::Interpreter::builtinFunctionPrototype
Object builtinFunctionPrototype() const
Returns the builtin "Function.prototype" object.
Definition: interpreter.cpp:223
KJS::Interpreter::collect
static bool collect()
Run the garbage collection.
Definition: interpreter.cpp:333
KJS::Interpreter::builtinFunction
Object builtinFunction() const
Returns the builtin "Function" object.
Definition: interpreter.cpp:178
KJS::Interpreter::builtinArrayPrototype
Object builtinArrayPrototype() const
Returns the builtin "Array.prototype" object.
Definition: interpreter.cpp:228
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::Interpreter::builtinError
Object builtinError() const
Returns the builtin "Error" object.
Definition: interpreter.cpp:213
KJS::Interpreter::builtinDate
Object builtinDate() const
Returns the builtin "Date" object.
Definition: interpreter.cpp:203
KJS::Interpreter::builtinRegExp
Object builtinRegExp() const
Returns the builtin "RegExp" object.
Definition: interpreter.cpp:208
KJS::Interpreter::builtinObjectPrototype
Object builtinObjectPrototype() const
Returns the builtin "Object.prototype" object.
Definition: interpreter.cpp:218
KJS::Interpreter::evaluate
Completion evaluate(const UString &code, const Value &thisV=Value())
Evaluates the supplied ECMAScript code.
Definition: interpreter.cpp:163
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::Interpreter::builtinDatePrototype
Object builtinDatePrototype() const
Returns the builtin "Date.prototype" object.
Definition: interpreter.cpp:248
KJS::Interpreter::builtinNumberPrototype
Object builtinNumberPrototype() const
Returns the builtin "Number.prototype" object.
Definition: interpreter.cpp:243
KJS::Interpreter::builtinRegExpPrototype
Object builtinRegExpPrototype() const
Returns the builtin "RegExp.prototype" object.
Definition: interpreter.cpp:253
KJS::Interpreter::builtinErrorPrototype
Object builtinErrorPrototype() const
Returns the builtin "Error.prototype" object.
Definition: interpreter.cpp:258
KJS::Interpreter::builtinNumber
Object builtinNumber() const
Returns the builtin "Number" object.
Definition: interpreter.cpp:198
KJS::Interpreter::builtinArray
Object builtinArray() const
Returns the builtin "Array" object.
Definition: interpreter.cpp:183
KJS::Interpreter::Interpreter
Interpreter()
Creates a new interpreter.
Definition: interpreter.cpp:117
KJS::Interpreter::builtinEvalError
Object builtinEvalError() const
The initial value of "Error" global property.
Definition: interpreter.cpp:263
KJS::Interpreter::setCompatMode
void setCompatMode(CompatMode mode)
Call this to enable a compatibility mode with another browser.
Definition: interpreter.cpp:323
KJS::Interpreter::builtinStringPrototype
Object builtinStringPrototype() const
Returns the builtin "String.prototype" object.
Definition: interpreter.cpp:238
KJS::Interpreter::builtinString
Object builtinString() const
Returns the builtin "String" object.
Definition: interpreter.cpp:193
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::builtinBoolean
Object builtinBoolean() const
Returns the builtin "Boolean" object.
Definition: interpreter.cpp:188
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::List
Native list type.
Definition: list.h:48
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::ScopeChain
A scope chain object.
Definition: scope_chain.h:47
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:181

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