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

kjs

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