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

kjs

  • kjs
internal.h
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 #ifndef _INTERNAL_H_
25 #define _INTERNAL_H_
26 
27 #include "ustring.h"
28 #include "value.h"
29 #include "object.h"
30 #include "function.h"
31 #include "types.h"
32 #include "interpreter.h"
33 #include "scope_chain.h"
34 #include "array_instance.h"
35 
36 #ifndef I18N_NOOP
37 #define I18N_NOOP(s) s
38 #endif
39 
40 namespace KJS {
41 
42  static const double D16 = 65536.0;
43  static const double D32 = 4294967296.0;
44 
45  class FunctionBodyNode;
46  class FunctionBodyNode;
47  class FunctionPrototypeImp;
48  class FunctionImp;
49  class Parameter;
50  class Debugger;
51 
52  // ---------------------------------------------------------------------------
53  // Primitive impls
54  // ---------------------------------------------------------------------------
55 
56  class UndefinedImp : public ValueImp {
57  public:
58  Type type() const { return UndefinedType; }
59 
60  Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
61  bool toBoolean(ExecState *exec) const;
62  double toNumber(ExecState *exec) const;
63  UString toString(ExecState *exec) const;
64  Object toObject(ExecState *exec) const;
65 
66  static UndefinedImp *staticUndefined;
67  };
68 
69  inline Undefined::Undefined(UndefinedImp *imp) : Value(imp) { }
70 
71  class NullImp : public ValueImp {
72  public:
73  Type type() const { return NullType; }
74 
75  Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
76  bool toBoolean(ExecState *exec) const;
77  double toNumber(ExecState *exec) const;
78  UString toString(ExecState *exec) const;
79  Object toObject(ExecState *exec) const;
80 
81  static NullImp *staticNull;
82  };
83 
84  inline Null::Null(NullImp *imp) : Value(imp) { }
85 
86  class BooleanImp : public ValueImp {
87  public:
88  BooleanImp(bool v = false) : val(v) { }
89  bool value() const { return val; }
90 
91  Type type() const { return BooleanType; }
92 
93  Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
94  bool toBoolean(ExecState *exec) const;
95  double toNumber(ExecState *exec) const;
96  UString toString(ExecState *exec) const;
97  Object toObject(ExecState *exec) const;
98 
99  static BooleanImp *staticTrue;
100  static BooleanImp *staticFalse;
101  private:
102  bool val;
103  };
104 
105  inline Boolean::Boolean(BooleanImp *imp) : Value(imp) { }
106 
107  class StringImp : public ValueImp {
108  public:
109  StringImp(const UString& v) : val(v) { }
110  UString value() const { return val; }
111 
112  Type type() const { return StringType; }
113 
114  Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
115  bool toBoolean(ExecState *exec) const;
116  double toNumber(ExecState *exec) const;
117  UString toString(ExecState *exec) const;
118  Object toObject(ExecState *exec) const;
119 
120  private:
121  UString val;
122  };
123 
124  inline String::String(StringImp *imp) : Value(imp) { }
125 
126  class NumberImp : public ValueImp {
127  friend class Number;
128  friend class InterpreterImp;
129  public:
130  static ValueImp *create(int);
131  static ValueImp *create(double);
132  static ValueImp *zero() { return SimpleNumber::make(0); }
133  static ValueImp *one() { return SimpleNumber::make(1); }
134  static ValueImp *two() { return SimpleNumber::make(2); }
135 
136  double value() const { return val; }
137 
138  Type type() const { return NumberType; }
139 
140  Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
141  bool toBoolean(ExecState *exec) const;
142  double toNumber(ExecState *exec) const;
143  UString toString(ExecState *exec) const;
144  Object toObject(ExecState *exec) const;
145 
146  static NumberImp *staticNaN;
147 
148  private:
149  NumberImp(double v) : val(v) { }
150 
151  virtual bool toUInt32(unsigned&) const;
152 
153  double val;
154  };
155 
156  inline Number::Number(NumberImp *imp) : Value(imp) { }
157 
161  class LabelStack {
162  public:
163  LabelStack(): tos(0L), iterationDepth(0), switchDepth(0) {}
164  ~LabelStack();
165 
166  LabelStack(const LabelStack &other);
167  LabelStack &operator=(const LabelStack &other);
168 
173  bool push(const Identifier &id);
177  bool contains(const Identifier &id) const;
181  void pop();
182 
183  void pushIteration() { iterationDepth++; }
184  void popIteration() { iterationDepth--; }
185  bool inIteration() const { return (iterationDepth > 0); }
186 
187  void pushSwitch() { switchDepth++; }
188  void popSwitch() { switchDepth--; }
189  bool inSwitch() const { return (switchDepth > 0); }
190 
191  private:
192  struct StackElem {
193  Identifier id;
194  StackElem *prev;
195  };
196 
197  StackElem *tos;
198  void clear();
199  int iterationDepth;
200  int switchDepth;
201  };
202 
203 
204  // ---------------------------------------------------------------------------
205  // Parsing & evaluateion
206  // ---------------------------------------------------------------------------
207 
208  class SourceCode {
209  public:
210  SourceCode(int _sid)
211  : sid(_sid), interpreter(0), refcount(0), next(0) {}
212 
213  void ref() { refcount++; }
214  void deref() { if (!--refcount) cleanup(); }
215  void cleanup();
216 
217  int sid;
218  InterpreterImp *interpreter;
219  int refcount;
220  SourceCode *next;
221  };
222 
230  class Parser {
231  public:
232  static FunctionBodyNode *parse(const UChar *code, unsigned int length, SourceCode **src,
233  int *errLine = 0, UString *errMsg = 0);
234 
235  static FunctionBodyNode *progNode;
236  static SourceCode *source;
237  static int sid;
238  private:
239  };
240 
241  class InterpreterImp {
242  friend class Collector;
243  public:
244  static void globalInit();
245  static void globalClear();
246 
247  InterpreterImp(Interpreter *interp, const Object &glob);
248  ~InterpreterImp();
249 
250  Object &globalObject() const { return const_cast<Object &>(global); }
251  Interpreter* interpreter() const { return m_interpreter; }
252 
253  void initGlobalObject();
254  static void lock();
255  static void unlock();
256 
257  void mark();
258 
259  ExecState *globalExec() { return globExec; }
260  bool checkSyntax(const UString &code,int *errLine, UString *errMsg);
261  bool checkSyntax(const UString &code);
262  Completion evaluate(const UString &code, const Value &thisV);
263  Debugger *debugger() const { return dbg; }
264  void setDebugger(Debugger *d);
265 
266  Object builtinObject() const { return b_Object; }
267  Object builtinFunction() const { return b_Function; }
268  Object builtinArray() const { return b_Array; }
269  Object builtinBoolean() const { return b_Boolean; }
270  Object builtinString() const { return b_String; }
271  Object builtinNumber() const { return b_Number; }
272  Object builtinDate() const { return b_Date; }
273  Object builtinRegExp() const { return b_RegExp; }
274  Object builtinError() const { return b_Error; }
275 
276  Object builtinObjectPrototype() const { return b_ObjectPrototype; }
277  Object builtinFunctionPrototype() const { return b_FunctionPrototype; }
278  Object builtinArrayPrototype() const { return b_ArrayPrototype; }
279  Object builtinBooleanPrototype() const { return b_BooleanPrototype; }
280  Object builtinStringPrototype() const { return b_StringPrototype; }
281  Object builtinNumberPrototype() const { return b_NumberPrototype; }
282  Object builtinDatePrototype() const { return b_DatePrototype; }
283  Object builtinRegExpPrototype() const { return b_RegExpPrototype; }
284  Object builtinErrorPrototype() const { return b_ErrorPrototype; }
285 
286  Object builtinEvalError() const { return b_evalError; }
287  Object builtinRangeError() const { return b_rangeError; }
288  Object builtinReferenceError() const { return b_referenceError; }
289  Object builtinSyntaxError() const { return b_syntaxError; }
290  Object builtinTypeError() const { return b_typeError; }
291  Object builtinURIError() const { return b_uriError; }
292 
293  Object builtinEvalErrorPrototype() const { return b_evalErrorPrototype; }
294  Object builtinRangeErrorPrototype() const { return b_rangeErrorPrototype; }
295  Object builtinReferenceErrorPrototype() const { return b_referenceErrorPrototype; }
296  Object builtinSyntaxErrorPrototype() const { return b_syntaxErrorPrototype; }
297  Object builtinTypeErrorPrototype() const { return b_typeErrorPrototype; }
298  Object builtinURIErrorPrototype() const { return b_uriErrorPrototype; }
299 
300  void setCompatMode(Interpreter::CompatMode mode) { m_compatMode = mode; }
301  Interpreter::CompatMode compatMode() const { return m_compatMode; }
302 
303  // Chained list of interpreters (ring)
304  static InterpreterImp* firstInterpreter() { return s_hook; }
305  InterpreterImp *nextInterpreter() const { return next; }
306  InterpreterImp *prevInterpreter() const { return prev; }
307 
308  void addSourceCode(SourceCode *code);
309  void removeSourceCode(SourceCode *code);
310 
311  void setContext(ContextImp *c) { _context = c; }
312 
313  private:
314  void clear();
315  Interpreter *m_interpreter;
316  Object global;
317  Debugger *dbg;
318 
319  // Built-in properties of the object prototype. These are accessible
320  // from here even if they are replaced by js code (e.g. assigning to
321  // Array.prototype)
322 
323  Object b_Object;
324  Object b_Function;
325  Object b_Array;
326  Object b_Boolean;
327  Object b_String;
328  Object b_Number;
329  Object b_Date;
330  Object b_RegExp;
331  Object b_Error;
332 
333  Object b_ObjectPrototype;
334  Object b_FunctionPrototype;
335  Object b_ArrayPrototype;
336  Object b_BooleanPrototype;
337  Object b_StringPrototype;
338  Object b_NumberPrototype;
339  Object b_DatePrototype;
340  Object b_RegExpPrototype;
341  Object b_ErrorPrototype;
342 
343  Object b_evalError;
344  Object b_rangeError;
345  Object b_referenceError;
346  Object b_syntaxError;
347  Object b_typeError;
348  Object b_uriError;
349 
350  Object b_evalErrorPrototype;
351  Object b_rangeErrorPrototype;
352  Object b_referenceErrorPrototype;
353  Object b_syntaxErrorPrototype;
354  Object b_typeErrorPrototype;
355  Object b_uriErrorPrototype;
356 
357  ExecState *globExec;
358  Interpreter::CompatMode m_compatMode;
359 
360  // Chained list of interpreters (ring) - for collector
361  static InterpreterImp* s_hook;
362  InterpreterImp *next, *prev;
363 
364  ContextImp *_context;
365 
366  int recursion;
367  SourceCode *sources;
368  };
369 
370  class AttachedInterpreter;
371  class DebuggerImp {
372  public:
373 
374  DebuggerImp() {
375  interps = 0;
376  isAborted = false;
377  }
378 
379  void abort() { isAborted = true; }
380  bool aborted() const { return isAborted; }
381 
382  AttachedInterpreter *interps;
383  bool isAborted;
384  };
385 
389  class FunctionImp : public InternalFunctionImp {
390  friend class ActivationImp;
391  public:
392  FunctionImp(ExecState *exec, const Identifier &n = Identifier::null());
393  virtual ~FunctionImp();
394 
395  virtual Value get(ExecState *exec, const Identifier &propertyName) const;
396  virtual void put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr = None);
397  virtual bool hasProperty(ExecState *exec, const Identifier &propertyName) const;
398  virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
399 
400  virtual bool implementsCall() const;
401  virtual Value call(ExecState *exec, Object &thisObj, const List &args);
402 
403  void addParameter(const Identifier &n);
404  Identifier parameterProperty(int index) const;
405  // parameters in string representation, e.g. (a, b, c)
406  UString parameterString() const;
407  virtual CodeType codeType() const = 0;
408 
409  virtual Completion execute(ExecState *exec) = 0;
410  int firstLine() const { return line0; }
411  int lastLine() const { return line1; }
412  int sourceId() const { return sid; }
413 
414  virtual const ClassInfo *classInfo() const { return &info; }
415  static const ClassInfo info;
416  protected:
417  Parameter *param;
418  int line0;
419  int line1;
420  int sid;
421 
422  private:
423  void processParameters(ExecState *exec, const List &);
424  virtual void processVarDecls(ExecState *exec);
425  };
426 
427  class DeclaredFunctionImp : public FunctionImp {
428  public:
429  DeclaredFunctionImp(ExecState *exec, const Identifier &n,
430  FunctionBodyNode *b, const ScopeChain &sc);
431  ~DeclaredFunctionImp();
432 
433  bool implementsConstruct() const;
434  Object construct(ExecState *exec, const List &args);
435 
436  virtual Completion execute(ExecState *exec);
437  CodeType codeType() const { return FunctionCode; }
438  FunctionBodyNode *body;
439 
440  virtual const ClassInfo *classInfo() const { return &info; }
441  KJS_EXPORT static const ClassInfo info;
442  private:
443  virtual void processVarDecls(ExecState *exec);
444  };
445 
446  class ActivationImp;
447 
448  class ArgumentsImp : public ObjectImp {
449  public:
450  ArgumentsImp(ExecState *exec, FunctionImp *func, const List &args, ActivationImp *act);
451 
452  virtual void mark();
453 
454  virtual Value get(ExecState *exec, const Identifier &propertyName) const;
455  virtual void put(ExecState *exec, const Identifier &propertyName,
456  const Value &value, int attr = None);
457 
458  virtual const ClassInfo *classInfo() const { return &info; }
459  static const ClassInfo info;
460 
461  private:
462  ActivationImp *activation;
463  };
464 
465  class ActivationImp : public ObjectImp {
466  public:
467  ActivationImp(FunctionImp *function, const List &arguments);
468 
469  virtual Value get(ExecState *exec, const Identifier &propertyName) const;
470  virtual bool hasProperty(ExecState *exec, const Identifier &propertyName) const;
471  virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
472 
473  virtual const ClassInfo *classInfo() const { return &info; }
474  static const ClassInfo info;
475 
476  virtual void mark();
477 
478  private:
479  FunctionImp *_function;
480  List _arguments;
481  mutable ArgumentsImp *_argumentsObject;
482  };
483 
484  class GlobalFuncImp : public InternalFunctionImp {
485  public:
486  GlobalFuncImp(ExecState *exec, FunctionPrototypeImp *funcProto,
487  int i, int len, const Identifier &_ident);
488  virtual bool implementsCall() const;
489  virtual Value call(ExecState *exec, Object &thisObj, const List &args);
490  virtual CodeType codeType() const;
491  enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, DecodeURI, DecodeURIComponent,
492  EncodeURI, EncodeURIComponent, Escape, UnEscape, KJSPrint };
493  private:
494  int id;
495  };
496 
497  // helper function for toInteger, toInt32, toUInt32 and toUInt16
498  double roundValue(ExecState *exec, const Value &v);
499 
500 #ifndef NDEBUG
501  void printInfo(ExecState *exec, const char *s, const Value &o, int lineno = -1);
502 #endif
503 
504 } // namespace
505 
506 
507 #endif // _INTERNAL_H_
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:167
TDEStdAccel::next
const TDEShortcut & next()
KJS::InternalFunctionImp
Base class for all function objects.
Definition: function.h:40
KJS::ScopeChain
A scope chain object.
Definition: scope_chain.h:47
KJS::LabelStack::push
bool push(const Identifier &id)
If id is not empty and is not in the stack already, puts it on top of the stack and returns true...
Definition: internal.cpp:320
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::FunctionImp
Implementation class for functions implemented in JS.
Definition: internal.h:389
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::LabelStack::contains
bool contains(const Identifier &id) const
Is the id in the stack?
Definition: internal.cpp:332
KJS
Definition: array_instance.h:27
KJS::LabelStack::pop
void pop()
Removes from the stack the last pushed id (what else?)
Definition: internal.cpp:344
KJS::Completion
Completion objects are used to convey the return status and value from functions. ...
Definition: completion.h:48
KJS::List
Native list type.
Definition: list.h:48
KJS::ClassInfo
Class Information.
Definition: object.h:58
KJS::LabelStack
The "label set" in Ecma-262 spec.
Definition: internal.h:161
KJS::Identifier::null
static const Identifier & null()
Creates an empty Identifier.
Definition: identifier.cpp:302
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32

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.