kjs Library API Documentation

internal.h

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
00005  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Lesser General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Lesser General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Lesser General Public License
00018  *  along with this library; see the file COPYING.LIB.  If not, write to
00019  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  *  Boston, MA 02111-1307, USA.
00021  *
00022  */
00023 
00024 #ifndef _INTERNAL_H_
00025 #define _INTERNAL_H_
00026 
00027 #include "ustring.h"
00028 #include "value.h"
00029 #include "object.h"
00030 #include "types.h"
00031 #include "interpreter.h"
00032 
00033 #define I18N_NOOP(s) s
00034 
00035 namespace KJS {
00036 
00037   static const double D16 = 65536.0;
00038   static const double D32 = 4294967296.0;
00039 
00040   class ProgramNode;
00041   class FunctionBodyNode;
00042   class FunctionPrototypeImp;
00043   class FunctionImp;
00044   class Debugger;
00045 
00046   // ---------------------------------------------------------------------------
00047   //                            Primitive impls
00048   // ---------------------------------------------------------------------------
00049 
00050   class UndefinedImp : public ValueImp {
00051   public:
00052     UndefinedImp() {}
00053     virtual ~UndefinedImp() { }
00054 
00055     Type type() const { return UndefinedType; }
00056 
00057     Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
00058     bool toBoolean(ExecState *exec) const;
00059     double toNumber(ExecState *exec) const;
00060     UString toString(ExecState *exec) const;
00061     Object toObject(ExecState *exec) const;
00062 
00063     static UndefinedImp *staticUndefined;
00064   };
00065 
00066   class NullImp : public ValueImp {
00067   public:
00068     NullImp() {}
00069     virtual ~NullImp() { }
00070 
00071     Type type() const { return NullType; }
00072 
00073     Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
00074     bool toBoolean(ExecState *exec) const;
00075     double toNumber(ExecState *exec) const;
00076     UString toString(ExecState *exec) const;
00077     Object toObject(ExecState *exec) const;
00078 
00079     static NullImp *staticNull;
00080   };
00081 
00082   class BooleanImp : public ValueImp {
00083   public:
00084     virtual ~BooleanImp() { }
00085     BooleanImp(bool v = false) : val(v) { }
00086     bool value() const { return val; }
00087 
00088     Type type() const { return BooleanType; }
00089 
00090     Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
00091     bool toBoolean(ExecState *exec) const;
00092     double toNumber(ExecState *exec) const;
00093     UString toString(ExecState *exec) const;
00094     Object toObject(ExecState *exec) const;
00095 
00096     static BooleanImp *staticTrue;
00097     static BooleanImp *staticFalse;
00098   private:
00099     bool val;
00100   };
00101 
00102   class StringImp : public ValueImp {
00103   public:
00104     StringImp(const UString& v);
00105     virtual ~StringImp() { }
00106     UString value() const { return val; }
00107 
00108     Type type() const { return StringType; }
00109 
00110     Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
00111     bool toBoolean(ExecState *exec) const;
00112     double toNumber(ExecState *exec) const;
00113     UString toString(ExecState *exec) const;
00114     Object toObject(ExecState *exec) const;
00115 
00116   private:
00117     UString val;
00118   };
00119 
00120   class NumberImp : public ValueImp {
00121   public:
00122     NumberImp(double v);
00123     virtual ~NumberImp() { }
00124     double value() const { return val; }
00125 
00126     Type type() const { return NumberType; }
00127 
00128     Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
00129     bool toBoolean(ExecState *exec) const;
00130     double toNumber(ExecState *exec) const;
00131     UString toString(ExecState *exec) const;
00132     Object toObject(ExecState *exec) const;
00133 
00134   private:
00135     double val;
00136   };
00137 
00138   // ---------------------------------------------------------------------------
00139   //                            Internal type impls
00140   // ---------------------------------------------------------------------------
00141 
00142   // TODO: remove. replaced by light-weight new Reference2 class
00143   class ReferenceImp : public ValueImp {
00144   public:
00145 
00146     ReferenceImp(const Value& v, const UString& p);
00147     virtual ~ReferenceImp() { }
00148     virtual void mark();
00149 
00150     Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
00151     bool toBoolean(ExecState *exec) const;
00152     double toNumber(ExecState *exec) const;
00153     UString toString(ExecState *exec) const;
00154     Object toObject(ExecState *exec) const;
00155 
00156     Value getBase() const { return Value(base); }
00157     UString getPropertyName() const { return prop; }
00158 
00159     Type type() const { return ReferenceType; }
00160 
00161   private:
00162     ValueImp *base;
00163     UString prop;
00164   };
00165 
00166   class CompletionImp : public ValueImp {
00167   public:
00168     Type type() const { return CompletionType; }
00169 
00170     CompletionImp(ComplType c, const Value& v, const UString& t);
00171     virtual ~CompletionImp();
00172     virtual void mark();
00173 
00174     Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
00175     bool toBoolean(ExecState *exec) const;
00176     double toNumber(ExecState *exec) const;
00177     UString toString(ExecState *exec) const;
00178     Object toObject(ExecState *exec) const;
00179 
00180     ComplType complType() const { return comp; }
00181     Value value() const { return Value(val); }
00182     UString target() const { return tar; }
00183 
00184   private:
00185     ComplType comp;
00186     ValueImp * val;
00187     UString tar;
00188   };
00189 
00193   /* TODO: KDE 4.0: delete Reference in types.h and rename this one. */
00194   class Reference2 {
00195   public:
00199     Reference2() { }
00203     Reference2(const Value& v) : bs(v) { }
00207     Reference2(const Value& v, const UString& p) : bs(v), prop(p) { }
00208 
00209     bool isValid() const { return !bs.isNull() && !prop.isNull(); }
00210 
00211 
00212     // ECMA 8.7.1
00213     Value base() const { return Value(bs); }
00214     // ECMA 8.7.2
00215     UString propertyName() const { return prop; }
00216 
00217     // ECMA 8.7.1
00218     Value getValue(ExecState *exec) const;
00219     void putValue(ExecState *exec, const Value& w);
00220 
00221   private:
00222     Value bs;
00223     UString prop;
00224   };
00225 
00229   class ListNode {
00230     friend class List;
00231     friend class ListImp;
00232     friend class ListIterator;
00233     ListNode(Value val, ListNode *p, ListNode *n)
00234       : member(val.imp()), prev(p), next(n) {};
00235     ValueImp *member;
00236     ListNode *prev, *next;
00237   };
00238 
00239   class ListImp : public ValueImp {
00240     friend class ListIterator;
00241     friend class List;
00242     friend class InterpreterImp;
00243   public:
00244     ListImp();
00245     ~ListImp();
00246 
00247     Type type() const { return ListType; }
00248 
00249     virtual void mark();
00250 
00251     Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
00252     bool toBoolean(ExecState *exec) const;
00253     double toNumber(ExecState *exec) const;
00254     UString toString(ExecState *exec) const;
00255     Object toObject(ExecState *exec) const;
00256 
00257     void append(const Value& val);
00258     void prepend(const Value& val);
00259     void appendList(const List& lst);
00260     void prependList(const List& lst);
00261     void removeFirst();
00262     void removeLast();
00263     void remove(const Value &val);
00264     void clear();
00265     ListImp *copy() const;
00266     ListIterator begin() const { return ListIterator(hook->next); }
00267     ListIterator end() const { return ListIterator(hook); }
00268     //    bool isEmpty() const { return (hook->prev == hook); }
00269     bool isEmpty() const;
00270     int size() const;
00271     Value at(int i) const;
00272     Value operator[](int i) const { return at(i); }
00273     static ListImp* empty();
00274 
00275 #ifdef KJS_DEBUG_MEM
00276     static int count;
00277 #endif
00278   private:
00279     void erase(ListNode *n);
00280     ListNode *hook;
00281     static ListImp *emptyList;
00282   };
00283 
00287   class LabelStack {
00288   public:
00289     LabelStack(): tos(0L) {}
00290     ~LabelStack();
00291 
00292     LabelStack(const LabelStack &other);
00293     LabelStack &operator=(const LabelStack &other);
00294 
00299     bool push(const UString &id);
00303     bool contains(const UString &id) const;
00307     void pop();
00308   private:
00309     struct StackElem {
00310       UString id;
00311       StackElem *prev;
00312     };
00313 
00314     StackElem *tos;
00315     void clear();
00316   };
00317 
00318 
00319   // ---------------------------------------------------------------------------
00320   //                            Parsing & evaluateion
00321   // ---------------------------------------------------------------------------
00322 
00323   enum CodeType { GlobalCode,
00324                   EvalCode,
00325                   FunctionCode,
00326                   AnonymousCode };
00327 
00331   class ContextImp {
00332   public:
00333     // TODO: remove glob parameter. deducable from exec.
00334     ContextImp(Object &glob, ExecState *exec, Object &thisV, CodeType type = GlobalCode,
00335                ContextImp *_callingContext = 0L, FunctionImp *func = 0L, const List &args = List());
00336     virtual ~ContextImp();
00337 
00338     const List scopeChain() const { return scope; }
00339     Object variableObject() const { return variable; }
00340     void setVariableObject(const Object &v) { variable = v; }
00341     Object thisValue() const { return thisVal; }
00342     ContextImp *callingContext() { return callingCon; }
00343     Object activationObject() { return activation; }
00344 
00345     void pushScope(const Object &s);
00346     void popScope();
00347     LabelStack *seenLabels() { return &ls; }
00348 
00349   private:
00350 
00351     List scope;
00352     Object activation;
00353     Object variable;
00354     Object thisVal;
00355 
00356     ContextImp *callingCon;
00357 
00358     LabelStack ls;
00359     CodeType codeType;
00360   };
00361 
00369   class Parser {
00370   public:
00371     static ProgramNode *parse(const UChar *code, unsigned int length, int *sourceId = 0,
00372                               int *errLine = 0, UString *errMsg = 0);
00373 
00374     static ProgramNode *progNode;
00375     static int sid;
00376   };
00377 
00378   class InterpreterImp {
00379     friend class Collector;
00380   public:
00381     static void globalInit();
00382     static void globalClear();
00383 
00384     InterpreterImp(Interpreter *interp, const Object &glob);
00385     ~InterpreterImp();
00386 
00387     Object globalObject() const { return global; }
00388     Interpreter* interpreter() const { return m_interpreter; }
00389 
00390     void initGlobalObject();
00391 
00392     void mark();
00393 
00394     ExecState *globalExec() { return globExec; }
00395     bool checkSyntax(const UString &code);
00396     Completion evaluate(const UString &code, const Value &thisV);
00397     Debugger *debugger() const { return dbg; }
00398     void setDebugger(Debugger *d);
00399 
00400     Object builtinObject() const { return b_Object; }
00401     Object builtinFunction() const { return b_Function; }
00402     Object builtinArray() const { return b_Array; }
00403     Object builtinBoolean() const { return b_Boolean; }
00404     Object builtinString() const { return b_String; }
00405     Object builtinNumber() const { return b_Number; }
00406     Object builtinDate() const { return b_Date; }
00407     Object builtinRegExp() const { return b_RegExp; }
00408     Object builtinError() const { return b_Error; }
00409 
00410     Object builtinObjectPrototype() const { return b_ObjectPrototype; }
00411     Object builtinFunctionPrototype() const { return b_FunctionPrototype; }
00412     Object builtinArrayPrototype() const { return b_ArrayPrototype; }
00413     Object builtinBooleanPrototype() const { return b_BooleanPrototype; }
00414     Object builtinStringPrototype() const { return b_StringPrototype; }
00415     Object builtinNumberPrototype() const { return b_NumberPrototype; }
00416     Object builtinDatePrototype() const { return b_DatePrototype; }
00417     Object builtinRegExpPrototype() const { return b_RegExpPrototype; }
00418     Object builtinErrorPrototype() const { return b_ErrorPrototype; }
00419 
00420     Object builtinEvalError() const { return b_evalError; }
00421     Object builtinRangeError() const { return b_rangeError; }
00422     Object builtinReferenceError() const { return b_referenceError; }
00423     Object builtinSyntaxError() const { return b_syntaxError; }
00424     Object builtinTypeError() const { return b_typeError; }
00425     Object builtinURIError() const { return b_uriError; }
00426 
00427     Object builtinEvalErrorPrototype() const { return b_evalErrorPrototype; }
00428     Object builtinRangeErrorPrototype() const { return b_rangeErrorPrototype; }
00429     Object builtinReferenceErrorPrototype() const { return b_referenceErrorPrototype; }
00430     Object builtinSyntaxErrorPrototype() const { return b_syntaxErrorPrototype; }
00431     Object builtinTypeErrorPrototype() const { return b_typeErrorPrototype; }
00432     Object builtinURIErrorPrototype() const { return b_uriErrorPrototype; }
00433 
00434     void setCompatMode(Interpreter::CompatMode mode) { m_compatMode = mode; }
00435     Interpreter::CompatMode compatMode() const { return m_compatMode; }
00436 
00437     // Chained list of interpreters (ring)
00438     static InterpreterImp* firstInterpreter() { return s_hook; }
00439     InterpreterImp *nextInterpreter() const { return next; }
00440     InterpreterImp *prevInterpreter() const { return prev; }
00441 
00442   private:
00443     void clear();
00444     Interpreter *m_interpreter;
00445     Object global;
00446     Debugger *dbg;
00447 
00448     // Built-in properties of the object prototype. These are accessible
00449     // from here even if they are replaced by js code (e.g. assigning to
00450     // Array.prototype)
00451 
00452     Object b_Object;
00453     Object b_Function;
00454     Object b_Array;
00455     Object b_Boolean;
00456     Object b_String;
00457     Object b_Number;
00458     Object b_Date;
00459     Object b_RegExp;
00460     Object b_Error;
00461 
00462     Object b_ObjectPrototype;
00463     Object b_FunctionPrototype;
00464     Object b_ArrayPrototype;
00465     Object b_BooleanPrototype;
00466     Object b_StringPrototype;
00467     Object b_NumberPrototype;
00468     Object b_DatePrototype;
00469     Object b_RegExpPrototype;
00470     Object b_ErrorPrototype;
00471 
00472     Object b_evalError;
00473     Object b_rangeError;
00474     Object b_referenceError;
00475     Object b_syntaxError;
00476     Object b_typeError;
00477     Object b_uriError;
00478 
00479     Object b_evalErrorPrototype;
00480     Object b_rangeErrorPrototype;
00481     Object b_referenceErrorPrototype;
00482     Object b_syntaxErrorPrototype;
00483     Object b_typeErrorPrototype;
00484     Object b_uriErrorPrototype;
00485 
00486     ExecState *globExec;
00487     Interpreter::CompatMode m_compatMode;
00488 
00489     // Chained list of interpreters (ring) - for collector
00490     static InterpreterImp* s_hook;
00491     InterpreterImp *next, *prev;
00492 
00493     int recursion;
00494   };
00495 
00496   class AttachedInterpreter;
00497   class DebuggerImp {
00498   public:
00499 
00500     DebuggerImp() {
00501       interps = 0;
00502       isAborted = false;
00503     }
00504 
00505     void abort() { isAborted = true; }
00506     bool aborted() const { return isAborted; }
00507 
00508     AttachedInterpreter *interps;
00509     bool isAborted;
00510   };
00511 
00512 
00513 
00514   class InternalFunctionImp : public ObjectImp {
00515   public:
00516     InternalFunctionImp(FunctionPrototypeImp *funcProto);
00517     bool implementsHasInstance() const;
00518     Boolean hasInstance(ExecState *exec, const Value &value);
00519 
00520     virtual const ClassInfo *classInfo() const { return &info; }
00521     static const ClassInfo info;
00522   };
00523 
00524   // helper function for toInteger, toInt32, toUInt32 and toUInt16
00525   double roundValue(ExecState *exec, const Value &v);
00526 
00527 #ifndef NDEBUG
00528   void printInfo(ExecState *exec, const char *s, const Value &o, int lineno = -1);
00529 #endif
00530 
00531 } // namespace
00532 
00533 
00534 #endif //  _INTERNAL_H_
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:27 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001