summaryrefslogtreecommitdiffstats
path: root/kjs/interpreter.h
blob: e8f4f84f34d274ef13a9ed3ba9eaffaa54b7fb87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// -*- c-basic-offset: 2 -*-
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
 *  Copyright (C) 2003 Apple Computer, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#ifndef _KJS_INTERPRETER_H_
#define _KJS_INTERPRETER_H_

#include "value.h"
#include "object.h"
#include "types.h"

namespace KJS {

  class ContextImp;
  class InterpreterImp;

  /**
   * The three different types of code that can be executed in a Context.
   * These are:
   * <ul>
   *   <li>GlobalCode - code executed as a result of a call to
   *   Interpreter::evaluate().</li>
   *   <li>EvalCode - executed by a call to the builtin eval() function</li>
   *   <li>FunctionCode - inside a function call (ECMAScript functions only;
   *   does not include builtin native functions or funcitons supplied by the
   *   host environment</li>
   * </ul>
   */
  enum CodeType {
    GlobalCode   = 0,
    EvalCode     = 1,
    FunctionCode = 2
  };

  /**
   * Represents an execution context, as specified by section 10 of the ECMA
   * spec.
   *
   * An execution context contains information about the current state of the
   * script - the scope for variable lookup, the value of "this", etc. A new
   * execution context is entered whenever global code is executed (e.g. with
   * Interpreter::evaluate()), a function is called (see
   * Object::call()), or the builtin "eval" function is executed.
   *
   * Most inheritable functions in the KJS api take a ExecState pointer as
   * their first parameter. This can be used to obtain a handle to the current
   * execution context.
   *
   * Note: Context objects are wrapper classes/smart pointers for the internal
   * KJS ContextImp type. When one context variable is assigned to another, it
   * is still referencing the same internal object.
   */
  class KJS_EXPORT Context {
  public:
    Context(ContextImp *i) : rep(i) { }

    ContextImp *imp() const { return rep; }

    /**
     * Returns the scope chain for this execution context. This is used for
     * variable lookup, with the list being searched from start to end until a
     * variable is found.
     *
     * @return The execution context's scope chain
     */
    const ScopeChain &scopeChain() const;

    /**
     * Returns the variable object for the execution context. This contains a
     * property for each variable declared in the execution context.
     *
     * @return The execution context's variable object
     */
    Object variableObject() const;

    /**
     * Returns the "this" value for the execution context. This is the value
     * returned when a script references the special variable "this". It should
     * always be an Object, unless application-specific code has passed in a
     * different type.
     *
     * The object that is used as the "this" value depends on the type of
     * execution context - for global contexts, the global object is used. For
     * function objewcts, the value is given by the caller (e.g. in the case of
     * obj.func(), obj would be the "this" value). For code executed by the
     * built-in "eval" function, the this value is the same as the calling
     * context.
     *
     * @return The execution context's "this" value
     */
    Object thisValue() const;

    /**
     * Returns the context from which the current context was invoked. For
     * global code this will be a null context (i.e. one for which
     * isNull() returns true). You should check isNull() on the returned
     * value before calling any of it's methods.
     *
     * @return The calling execution context
     */
    const Context callingContext() const;

    /**
     * The type of code being executed in this context. One of GlobalCode,
     * EvalCode or FunctionCode
     */
    CodeType codeType() const;

    /**
     * The identifier of the source code fragment containing the code being
     * executed
     */
    int sourceId() const;

    /**
     * The line number on which the current statement begins
     */
    int curStmtFirstLine() const;

    /**
     * The line number on which the current statement ends
     */
    int curStmtLastLine() const;

    /**
     * In the case of FunctionCode, the function objects being called
     */
    Object function() const;

    /**
     * In the case of FunctionCode, the name of the function being called
     */
    Identifier functionName() const;

    /**
     * In the case of FunctionCode, the arguments passed to the function
     */
    List args() const;

  private:
    ContextImp *rep;
  };

  bool operator==(const Context &c1, const Context &c2);
  bool operator!=(const Context &c1, const Context &c2);

  /**
   * Interpreter objects can be used to evaluate ECMAScript code. Each
   * interpreter has a global object which is used for the purposes of code
   * evaluation, and also provides access to built-in properties such as
   * " Object" and "Number".
   */
  class KJS_EXPORT Interpreter {
  public:
    /**
     * Creates a new interpreter. The supplied object will be used as the global
     * object for all scripts executed with this interpreter. During
     * constuction, all the standard properties such as "Object" and "Number"
     * will be added to the global object.
     *
     * Note: You should not use the same global object for multiple
     * interpreters.
     *
     * This is due do the fact that the built-in properties are set in the
     * constructor, and if these objects have been modified from another
     * interpreter (e.g. a script modifying String.prototype), the changes will
     * be overridden.
     *
     * @param global The object to use as the global object for this interpreter
     */
    Interpreter(const Object &global);
    /**
     * Creates a new interpreter. A global object will be created and
     * initialized with the standard global properties.
     */
    Interpreter();
    virtual ~Interpreter();

    /**
     * Returns the object that is used as the global object during all script
     * execution performed by this interpreter
     */
    Object &globalObject() const;

    void initGlobalObject();

    static void lock();
    static void unlock();

    /**
     * Returns the execution state object which can be used to execute
     * scripts using this interpreter at a the "global" level, i.e. one
     * with a execution context that has the global object as the "this"
     * value, and who's scope chain contains only the global object.
     *
     * Note: this pointer remains constant for the life of the interpreter
     * and should not be manually deleted.
     *
     * @return The interpreter global execution state object
     */
    ExecState *globalExec();

    /**
     * Parses the supplied ECMAScript code and checks for syntax errors.
     *
     * @param code The code to check
     * @param errLine Returns the line the error was on (if there was one).
     * @param errMsg Returns the error message (if there was one).
     * @return true if there were no syntax errors in the code, otherwise false
     */
    bool checkSyntax(const UString &code, int *errLine, UString *errMsg);

    /**
     * Parses the supplied ECMAScript code and checks for syntax errors.
     *
     * @param code The code to check
     * @return true if there were no syntax errors in the code, otherwise false
     */
    bool checkSyntax(const UString &code);

    /**
     * Evaluates the supplied ECMAScript code.
     *
     * Since this method returns a Completion, you should check the type of
     * completion to detect an error or before attempting to access the returned
     * value. For example, if an error occurs during script execution and is not
     * caught by the script, the completion type will be Throw.
     *
     * If the supplied code is invalid, a SyntaxError will be thrown.
     *
     * @param code The code to evaluate
     * @param thisV The value to pass in as the "this" value for the script
     * execution. This should either be Null() or an Object.
     * @return A completion object representing the result of the execution.
     */
    Completion evaluate(const UString &code, const Value &thisV = Value());

    /**
     * @internal
     *
     * Returns the implementation object associated with this interpreter.
     * Only useful for internal KJS operations.
     */
    InterpreterImp *imp();

    /**
     * Returns the builtin "Object" object. This is the object that was set
     * as a property of the global object during construction; if the property
     * is replaced by script code, this method will still return the original
     * object.
     *
     * @return The builtin "Object" object
     */
    Object builtinObject() const;

    /**
     * Returns the builtin "Function" object.
     */
    Object builtinFunction() const;

    /**
     * Returns the builtin "Array" object.
     */
    Object builtinArray() const;

    /**
     * Returns the builtin "Boolean" object.
     */
    Object builtinBoolean() const;

    /**
     * Returns the builtin "String" object.
     */
    Object builtinString() const;

    /**
     * Returns the builtin "Number" object.
     */
    Object builtinNumber() const;

    /**
     * Returns the builtin "Date" object.
     */
    Object builtinDate() const;

    /**
     * Returns the builtin "RegExp" object.
     */
    Object builtinRegExp() const;

    /**
     * Returns the builtin "Error" object.
     */
    Object builtinError() const;

    /**
     * Returns the builtin "Object.prototype" object.
     */
    Object builtinObjectPrototype() const;

    /**
     * Returns the builtin "Function.prototype" object.
     */
    Object builtinFunctionPrototype() const;

    /**
     * Returns the builtin "Array.prototype" object.
     */
    Object builtinArrayPrototype() const;

    /**
     * Returns the builtin "Boolean.prototype" object.
     */
    Object builtinBooleanPrototype() const;

    /**
     * Returns the builtin "String.prototype" object.
     */
    Object builtinStringPrototype() const;

    /**
     * Returns the builtin "Number.prototype" object.
     */
    Object builtinNumberPrototype() const;

    /**
     * Returns the builtin "Date.prototype" object.
     */
    Object builtinDatePrototype() const;

    /**
     * Returns the builtin "RegExp.prototype" object.
     */
    Object builtinRegExpPrototype() const;

    /**
     * Returns the builtin "Error.prototype" object.
     */
    Object builtinErrorPrototype() const;

    /**
     * The initial value of "Error" global property
     */
    Object builtinEvalError() const;
    Object builtinRangeError() const;
    Object builtinReferenceError() const;
    Object builtinSyntaxError() const;
    Object builtinTypeError() const;
    Object builtinURIError() const;

    Object builtinEvalErrorPrototype() const;
    Object builtinRangeErrorPrototype() const;
    Object builtinReferenceErrorPrototype() const;
    Object builtinSyntaxErrorPrototype() const;
    Object builtinTypeErrorPrototype() const;
    Object builtinURIErrorPrototype() const;

    enum CompatMode { NativeMode, IECompat, NetscapeCompat };
    /**
     * Call this to enable a compatibility mode with another browser.
     * (by default konqueror is in "native mode").
     * Currently, in KJS, this only changes the behavior of Date::getYear()
     * which returns the full year under IE.
     */
    void setCompatMode(CompatMode mode);
    CompatMode compatMode() const;

    /**
     * Run the garbage collection. Returns true when at least one object
     * was collected; false otherwise.
     */
    static bool collect();

    /**
     * Called by InterpreterImp during the mark phase of the garbage collector
     * Default implementation does nothing, this exist for classes that reimplement Interpreter.
     */
    virtual void mark() {}

    /**
     * Provides a way to distinguish derived classes.
     * Only useful if you reimplement Interpreter and if different kind of
     * interpreters are created in the same process.
     * The base class returns 0, the ECMA-bindings interpreter returns 1.
     */
    virtual int rtti() { return 0; }

#ifdef KJS_DEBUG_MEM
    /**
     * @internal
     */
    static void finalCheck();
#endif
  private:
    InterpreterImp *rep;

    /**
     * This constructor is not implemented, in order to prevent
     * copy-construction of Interpreter objects. You should always pass around
     * pointers to an interpreter instance instead.
     */
    Interpreter(const Interpreter&);

    /**
     * This constructor is not implemented, in order to prevent assignment of
     * Interpreter objects. You should always pass around pointers to an
     * interpreter instance instead.
     */
    Interpreter operator=(const Interpreter&);
  protected:
    virtual void virtual_hook( int id, void* data );
  };

  /**
   * Represents the current state of script execution. This object allows you
   * obtain a handle the interpreter that is currently executing the script,
   * and also the current execution state context.
   */
  class KJS_EXPORT ExecState {
    friend class InterpreterImp;
    friend class FunctionImp;
    friend class GlobalFuncImp;
    friend class TryNode;
    friend class VarDeclNode;
    friend class FuncDeclNode;
  public:
    /**
     * Returns the interpreter associated with this execution state
     *
     * @return The interpreter executing the script
     */
    // ### make non-const or provide an overload pair
    Interpreter *dynamicInterpreter() const { return _interpreter; }

    // for compatibility
    Interpreter *interpreter() const { return dynamicInterpreter(); }

    /**
     * Returns the interpreter associated with the current scope's
     * global object
     *
     * @return The interpreter currently in scope
     */
    Interpreter *lexicalInterpreter() const;

    /**
     * Returns the execution context associated with this execution state
     *
     * @return The current execution state context
     */
    Context context() const { return _context; }

    void setException(const Value &e);
    void clearException();
    Value exception() const { return _exception; }
    // ### make const
    bool hadException();

    /*
     * request for ending execution with an exception
     */
    static void requestTerminate() { terminate_request = true; }
    /*
     * optional confirmation for ending execution after requestTerminate()
     */
    static bool (*confirmTerminate)();
  private:
    ExecState(Interpreter *interp, ContextImp *con)
        : _interpreter(interp), _context(con) { }
    Interpreter *_interpreter;
    ContextImp *_context;
    Value _exception;
    static bool terminate_request;
  };

} // namespace

#endif // _KJS_INTERPRETER_H_