Changeset 798 in webkit for trunk/JavaScriptCore/kjs/internal.h
- Timestamp:
- Mar 21, 2002, 4:31:57 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/internal.h
r6 r798 1 // -*- c-basic-offset: 2 -*- 1 2 /* 2 3 * This file is part of the KDE libraries 3 * Copyright (C) 1999-2000 Harri Porten ([email protected]) 4 * Copyright (C) 1999-2001 Harri Porten ([email protected]) 5 * Copyright (C) 2001 Peter Kelly ([email protected]) 4 6 * 5 7 * This library is free software; you can redistribute it and/or … … 17 19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 20 * Boston, MA 02111-1307, USA. 21 * 19 22 */ 20 23 … … 22 25 #define _INTERNAL_H_ 23 26 24 #ifdef HAVE_CONFIG_H 25 #include <config.h> 26 #endif 27 28 #include "kjs.h" 27 #include "ustring.h" 28 #include "value.h" 29 29 #include "object.h" 30 #include "function.h" 30 #include "types.h" 31 #include "interpreter.h" 31 32 32 33 #define I18N_NOOP(s) s … … 34 35 namespace KJS { 35 36 36 class Boolean; 37 class Number; 38 class String; 39 class Object; 40 class RegExp; 41 class Node; 37 static const double D16 = 65536.0; 38 static const double D32 = 4294967296.0; 39 40 class ProgramNode; 42 41 class FunctionBodyNode; 43 class ProgramNode;44 #ifdef KJS_DEBUGGER 42 class FunctionPrototypeImp; 43 class FunctionImp; 45 44 class Debugger; 46 #endif 47 48 class UndefinedImp : public Imp { 49 public: 50 UndefinedImp(); 45 46 // --------------------------------------------------------------------------- 47 // Primitive impls 48 // --------------------------------------------------------------------------- 49 50 class UndefinedImp : public ValueImp { 51 public: 52 UndefinedImp() {} 51 53 virtual ~UndefinedImp() { } 52 virtual KJSO toPrimitive(Type preferred = UndefinedType) const; 53 virtual Boolean toBoolean() const;54 virtual Number toNumber() const; 55 virtual String toString() const;56 virtual Object toObject() const;57 58 virtual const TypeInfo* typeInfo() const { return &info; }59 static const TypeInfo info;60 54 55 Type type() const { return UndefinedType; } 56 57 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const; 58 bool toBoolean(ExecState *exec) const; 59 double toNumber(ExecState *exec) const; 60 UString toString(ExecState *exec) const; 61 Object toObject(ExecState *exec) const; 62 61 63 static UndefinedImp *staticUndefined; 62 64 }; 63 65 64 class NullImp : public Imp {65 public: 66 NullImp() ;66 class NullImp : public ValueImp { 67 public: 68 NullImp() {} 67 69 virtual ~NullImp() { } 68 virtual KJSO toPrimitive(Type preferred = UndefinedType) const; 69 virtual Boolean toBoolean() const;70 virtual Number toNumber() const; 71 virtual String toString() const;72 virtual Object toObject() const;73 74 virtual const TypeInfo* typeInfo() const { return &info; }75 static const TypeInfo info;70 71 Type type() const { return NullType; } 72 73 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const; 74 bool toBoolean(ExecState *exec) const; 75 double toNumber(ExecState *exec) const; 76 UString toString(ExecState *exec) const; 77 Object toObject(ExecState *exec) const; 76 78 77 79 static NullImp *staticNull; 78 80 }; 79 81 80 class BooleanImp : public Imp {82 class BooleanImp : public ValueImp { 81 83 public: 82 84 virtual ~BooleanImp() { } 83 85 BooleanImp(bool v = false) : val(v) { } 84 86 bool value() const { return val; } 85 virtual KJSO toPrimitive(Type preferred = UndefinedType) const; 86 virtual Boolean toBoolean() const; 87 virtual Number toNumber() const; 88 virtual String toString() const; 89 virtual Object toObject() const; 90 91 virtual const TypeInfo* typeInfo() const { return &info; } 92 static const TypeInfo info; 93 94 static BooleanImp *staticTrue, *staticFalse; 87 88 Type type() const { return BooleanType; } 89 90 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const; 91 bool toBoolean(ExecState *exec) const; 92 double toNumber(ExecState *exec) const; 93 UString toString(ExecState *exec) const; 94 Object toObject(ExecState *exec) const; 95 96 static BooleanImp *staticTrue; 97 static BooleanImp *staticFalse; 95 98 private: 96 99 bool val; 97 100 }; 98 101 99 class NumberImp : public Imp { 102 class StringImp : public ValueImp { 103 public: 104 StringImp(const UString& v); 105 virtual ~StringImp() { } 106 UString value() const { return val; } 107 108 Type type() const { return StringType; } 109 110 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const; 111 bool toBoolean(ExecState *exec) const; 112 double toNumber(ExecState *exec) const; 113 UString toString(ExecState *exec) const; 114 Object toObject(ExecState *exec) const; 115 116 private: 117 UString val; 118 }; 119 120 class NumberImp : public ValueImp { 100 121 public: 101 122 NumberImp(double v); 102 123 virtual ~NumberImp() { } 103 124 double value() const { return val; } 104 virtual KJSO toPrimitive(Type preferred = UndefinedType) const; 105 virtual Boolean toBoolean() const; 106 virtual Number toNumber() const; 107 virtual String toString() const; 108 virtual Object toObject() const; 109 110 virtual const TypeInfo* typeInfo() const { return &info; } 111 static const TypeInfo info; 125 126 Type type() const { return NumberType; } 127 128 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const; 129 bool toBoolean(ExecState *exec) const; 130 double toNumber(ExecState *exec) const; 131 UString toString(ExecState *exec) const; 132 Object toObject(ExecState *exec) const; 133 112 134 private: 113 135 double val; 114 136 }; 115 137 116 class StringImp : public Imp { 117 public: 118 StringImp(const UString& v); 119 virtual ~StringImp() { } 120 UString value() const { return val; } 121 virtual KJSO toPrimitive(Type preferred = UndefinedType) const; 122 virtual Boolean toBoolean() const; 123 virtual Number toNumber() const; 124 virtual String toString() const; 125 virtual Object toObject() const; 126 127 virtual const TypeInfo* typeInfo() const { return &info; } 128 static const TypeInfo info; 129 private: 130 UString val; 131 }; 132 133 class ReferenceImp : public Imp { 134 public: 135 ReferenceImp(const KJSO& b, const UString& p); 138 // --------------------------------------------------------------------------- 139 // Internal type impls 140 // --------------------------------------------------------------------------- 141 142 class ReferenceImp : public ValueImp { 143 public: 144 145 ReferenceImp(const Value& v, const UString& p); 136 146 virtual ~ReferenceImp() { } 137 virtual void mark(Imp*); 138 KJSO getBase() const { return base; } 147 virtual void mark(); 148 149 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const; 150 bool toBoolean(ExecState *exec) const; 151 double toNumber(ExecState *exec) const; 152 UString toString(ExecState *exec) const; 153 Object toObject(ExecState *exec) const; 154 155 Value getBase() const { return Value(base); } 139 156 UString getPropertyName() const { return prop; } 140 157 141 virtual const TypeInfo* typeInfo() const { return &info; }142 static const TypeInfo info; 143 private: 144 KJSObase;158 Type type() const { return ReferenceType; } 159 160 private: 161 ValueImp *base; 145 162 UString prop; 146 163 }; 147 164 148 class CompletionImp : public Imp { 149 public: 150 CompletionImp(Compl c, const KJSO& v, const UString& t); 151 virtual ~CompletionImp() { } 152 virtual void mark(Imp*); 153 Compl completion() const { return comp; } 154 KJSO value() const { return val; } 165 class CompletionImp : public ValueImp { 166 public: 167 Type type() const { return CompletionType; } 168 169 CompletionImp(ComplType c, const Value& v, const UString& t); 170 virtual ~CompletionImp(); 171 virtual void mark(); 172 173 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const; 174 bool toBoolean(ExecState *exec) const; 175 double toNumber(ExecState *exec) const; 176 UString toString(ExecState *exec) const; 177 Object toObject(ExecState *exec) const; 178 179 ComplType complType() const { return comp; } 180 Value value() const { return Value(val); } 155 181 UString target() const { return tar; } 156 182 157 virtual const TypeInfo* typeInfo() const { return &info; } 158 static const TypeInfo info; 159 private: 160 Compl comp; 161 KJSO val; 183 private: 184 ComplType comp; 185 ValueImp * val; 162 186 UString tar; 163 187 }; 164 188 165 class RegExpImp : public ObjectImp { 166 public: 167 RegExpImp(); 168 ~RegExpImp(); 169 void setRegExp(RegExp *r) { reg = r; } 170 RegExp* regExp() const { return reg; } 171 private: 172 RegExp *reg; 173 }; 174 175 class StatementNode; 176 class UString; 177 178 class Reference : public KJSO { 179 public: 180 Reference(const KJSO& b, const UString &p); 181 virtual ~Reference(); 189 /** 190 * @internal 191 */ 192 class ListNode { 193 friend class List; 194 friend class ListImp; 195 friend class ListIterator; 196 ListNode(Value val, ListNode *p, ListNode *n) 197 : member(val.imp()), prev(p), next(n) {}; 198 ValueImp *member; 199 ListNode *prev, *next; 200 }; 201 202 class ListImp : public ValueImp { 203 friend class ListIterator; 204 friend class List; 205 friend class InterpreterImp; 206 public: 207 ListImp(); 208 ~ListImp(); 209 210 Type type() const { return ListType; } 211 212 virtual void mark(); 213 214 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const; 215 bool toBoolean(ExecState *exec) const; 216 double toNumber(ExecState *exec) const; 217 UString toString(ExecState *exec) const; 218 Object toObject(ExecState *exec) const; 219 220 void append(const Value& val); 221 void prepend(const Value& val); 222 void appendList(const List& lst); 223 void prependList(const List& lst); 224 void removeFirst(); 225 void removeLast(); 226 void remove(const Value &val); 227 void clear(); 228 ListImp *copy() const; 229 ListIterator begin() const { return ListIterator(hook->next); } 230 ListIterator end() const { return ListIterator(hook); } 231 // bool isEmpty() const { return (hook->prev == hook); } 232 bool isEmpty() const; 233 int size() const; 234 Value at(int i) const; 235 Value operator[](int i) const { return at(i); } 236 static ListImp* empty(); 237 238 #ifdef KJS_DEBUG_MEM 239 static int count; 240 #endif 241 private: 242 void erase(ListNode *n); 243 ListNode *hook; 244 static ListImp *emptyList; 182 245 }; 183 246 … … 189 252 LabelStack(): tos(0L) {} 190 253 ~LabelStack(); 254 255 LabelStack(const LabelStack &other); 256 LabelStack &operator=(const LabelStack &other); 191 257 192 258 /** … … 204 270 void pop(); 205 271 private: 206 struct StackEl m {272 struct StackElem { 207 273 UString id; 208 StackEl m *prev;274 StackElem *prev; 209 275 }; 210 276 211 StackElm *tos; 212 }; 277 StackElem *tos; 278 void clear(); 279 }; 280 281 282 // --------------------------------------------------------------------------- 283 // Parsing & evaluateion 284 // --------------------------------------------------------------------------- 285 286 enum CodeType { GlobalCode, 287 EvalCode, 288 FunctionCode, 289 AnonymousCode }; 213 290 214 291 /** 215 292 * @short Execution context. 216 293 */ 217 class Context { 218 public: 219 Context(CodeType type = GlobalCode, Context *callingContext = 0L, 220 FunctionImp *func = 0L, const List *args = 0L, Imp *thisV = 0L); 221 virtual ~Context(); 222 static Context *current(); 223 static void setCurrent(Context *c); 224 const List *pScopeChain() const { return scopeChain; } 225 void pushScope(const KJSO &s); 294 class ContextImp { 295 public: 296 ContextImp(Object &glob, ExecState *exec, Object &thisV, CodeType type = GlobalCode, 297 ContextImp *_callingContext = 0L, FunctionImp *func = 0L, const List &args = List()); 298 virtual ~ContextImp(); 299 300 const List scopeChain() const { return scope; } 301 Object variableObject() const { return variable; } 302 void setVariableObject(const Object &v) { variable = v; } 303 Object thisValue() const { return thisVal; } 304 ContextImp *callingContext() { return callingCon; } 305 Object activationObject() { return activation; } 306 307 void pushScope(const Object &s); 226 308 void popScope(); 227 List *copyOfChain();228 KJSO variableObject() const { return variable; }229 void setVariableObject( const KJSO &obj ) { variable = obj; }230 KJSO thisValue() const { return thisVal; }231 void setThisValue(const KJSO &t) { thisVal = t; }232 309 LabelStack *seenLabels() { return &ls; } 233 private: 310 311 private: 312 313 List scope; 314 Object variable; 315 Object thisVal; 316 ContextImp *callingCon; 317 Object activation; 318 319 234 320 LabelStack ls; 235 KJSO thisVal; 236 KJSO activation; 237 KJSO variable; 238 List *scopeChain; 239 }; 240 241 class DeclaredFunctionImp : public ConstructorImp { 242 public: 243 DeclaredFunctionImp(const UString &n, FunctionBodyNode *b, 244 const List *sc); 245 ~DeclaredFunctionImp(); 246 Completion execute(const List &); 247 Object construct(const List &); 248 CodeType codeType() const { return FunctionCode; } 249 List *scopeChain() const { return scopes; } 250 private: 251 FunctionBodyNode *body; 252 List *scopes; 253 }; 254 255 class AnonymousFunction : public Function { 256 public: 257 AnonymousFunction(); 258 Completion execute(const List &); 259 CodeType codeType() const { return AnonymousCode; } 260 }; 261 262 class ActivationImp : public Imp { 263 public: 264 ActivationImp(FunctionImp *f, const List *args); 265 virtual const TypeInfo* typeInfo() const { return &info; } 266 static const TypeInfo info; 267 }; 268 269 class ExecutionStack { 270 public: 271 ExecutionStack(); 272 ExecutionStack *push(); 273 ExecutionStack *pop(); 274 275 ProgramNode *progNode; 276 Node *firstNode; 277 private: 278 ExecutionStack *prev; 279 }; 280 281 class KJScriptImp { 282 friend class ::KJScript; 283 friend class Lexer; 284 friend class Context; 285 friend class Global; 321 CodeType codeType; 322 }; 323 324 /** 325 * @internal 326 * 327 * Parses ECMAScript source code and converts into ProgramNode objects, which 328 * represent the root of a parse tree. This class provides a conveniant workaround 329 * for the problem of the bison parser working in a static context. 330 */ 331 class Parser { 332 public: 333 static ProgramNode *parse(const UChar *code, unsigned int length, int *sourceId = 0, 334 int *errLine = 0, UString *errMsg = 0); 335 336 static ProgramNode *progNode; 337 static int sid; 338 }; 339 340 class InterpreterImp { 286 341 friend class Collector; 287 342 public: 288 KJScriptImp(KJScript *s); 289 ~KJScriptImp(); 343 static void globalInit(); 344 static void globalClear(); 345 346 InterpreterImp(Interpreter *interp, const Object &glob); 347 ~InterpreterImp(); 348 349 Object globalObject() const { return global; } 350 Interpreter* interpreter() const { return m_interpreter; } 351 290 352 void mark(); 291 static KJScriptImp *current() { return curr; } 292 static void setException(Imp *e); 293 static void setException(const char *msg); 294 static bool hadException(); 295 static KJSO exception(); 296 static void clearException(); 297 298 Context *context() const { return con; } 299 void setContext(Context *c) { con = c; } 300 301 #ifdef KJS_DEBUGGER 302 /** 303 * Attach debugger d to this engine. If there already was another instance 304 * attached it will be detached. 305 */ 306 void attachDebugger(Debugger *d); 353 354 ExecState *globalExec() { return globExec; } 355 bool checkSyntax(const UString &code); 356 Completion evaluate(const UString &code, const Value &thisV); 307 357 Debugger *debugger() const { return dbg; } 308 int sourceId() const { return sid; } 309 bool setBreakpoint(int id, int line, bool set); 358 void setDebugger(Debugger *d); 359 360 Object builtinObject() const { return b_Object; } 361 Object builtinFunction() const { return b_Function; } 362 Object builtinArray() const { return b_Array; } 363 Object builtinBoolean() const { return b_Boolean; } 364 Object builtinString() const { return b_String; } 365 Object builtinNumber() const { return b_Number; } 366 Object builtinDate() const { return b_Date; } 367 Object builtinRegExp() const { return b_RegExp; } 368 Object builtinError() const { return b_Error; } 369 370 Object builtinObjectPrototype() const { return b_ObjectPrototype; } 371 Object builtinFunctionPrototype() const { return b_FunctionPrototype; } 372 Object builtinArrayPrototype() const { return b_ArrayPrototype; } 373 Object builtinBooleanPrototype() const { return b_BooleanPrototype; } 374 Object builtinStringPrototype() const { return b_StringPrototype; } 375 Object builtinNumberPrototype() const { return b_NumberPrototype; } 376 Object builtinDatePrototype() const { return b_DatePrototype; } 377 Object builtinRegExpPrototype() const { return b_RegExpPrototype; } 378 Object builtinErrorPrototype() const { return b_ErrorPrototype; } 379 380 Object builtinEvalError() const { return b_evalError; } 381 Object builtinRangeError() const { return b_rangeError; } 382 Object builtinReferenceError() const { return b_referenceError; } 383 Object builtinSyntaxError() const { return b_syntaxError; } 384 Object builtinTypeError() const { return b_typeError; } 385 Object builtinURIError() const { return b_uriError; } 386 387 Object builtinEvalErrorPrototype() const { return b_evalErrorPrototype; } 388 Object builtinRangeErrorPrototype() const { return b_rangeErrorPrototype; } 389 Object builtinReferenceErrorPrototype() const { return b_referenceErrorPrototype; } 390 Object builtinSyntaxErrorPrototype() const { return b_syntaxErrorPrototype; } 391 Object builtinTypeErrorPrototype() const { return b_typeErrorPrototype; } 392 Object builtinURIErrorPrototype() const { return b_uriErrorPrototype; } 393 394 void setCompatMode(Interpreter::CompatMode mode) { m_compatMode = mode; } 395 Interpreter::CompatMode compatMode() const { return m_compatMode; } 396 397 // Chained list of interpreters (ring) 398 static InterpreterImp* firstInterpreter() { return s_hook; } 399 InterpreterImp *nextInterpreter() const { return next; } 400 InterpreterImp *prevInterpreter() const { return prev; } 401 402 private: 403 void clear(); 404 Interpreter *m_interpreter; 405 Object global; 406 Debugger *dbg; 407 408 // Built-in properties of the object prototype. These are accessible 409 // from here even if they are replaced by js code (e.g. assigning to 410 // Array.prototype) 411 412 Object b_Object; 413 Object b_Function; 414 Object b_Array; 415 Object b_Boolean; 416 Object b_String; 417 Object b_Number; 418 Object b_Date; 419 Object b_RegExp; 420 Object b_Error; 421 422 Object b_ObjectPrototype; 423 Object b_FunctionPrototype; 424 Object b_ArrayPrototype; 425 Object b_BooleanPrototype; 426 Object b_StringPrototype; 427 Object b_NumberPrototype; 428 Object b_DatePrototype; 429 Object b_RegExpPrototype; 430 Object b_ErrorPrototype; 431 432 Object b_evalError; 433 Object b_rangeError; 434 Object b_referenceError; 435 Object b_syntaxError; 436 Object b_typeError; 437 Object b_uriError; 438 439 Object b_evalErrorPrototype; 440 Object b_rangeErrorPrototype; 441 Object b_referenceErrorPrototype; 442 Object b_syntaxErrorPrototype; 443 Object b_typeErrorPrototype; 444 Object b_uriErrorPrototype; 445 446 ExecState *globExec; 447 Interpreter::CompatMode m_compatMode; 448 449 // Chained list of interpreters (ring) - for collector 450 static InterpreterImp* s_hook; 451 InterpreterImp *next, *prev; 452 453 int recursion; 454 }; 455 456 class AttachedInterpreter; 457 class DebuggerImp { 458 public: 459 460 DebuggerImp() { 461 interps = 0; 462 isAborted = false; 463 } 464 465 void abort() { isAborted = true; } 466 bool aborted() const { return isAborted; } 467 468 AttachedInterpreter *interps; 469 bool isAborted; 470 }; 471 472 473 474 class InternalFunctionImp : public ObjectImp { 475 public: 476 InternalFunctionImp(FunctionPrototypeImp *funcProto); 477 bool implementsHasInstance() const; 478 Boolean hasInstance(ExecState *exec, const Value &value); 479 480 virtual const ClassInfo *classInfo() const { return &info; } 481 static const ClassInfo info; 482 }; 483 484 // helper function for toInteger, toInt32, toUInt32 and toUInt16 485 double roundValue(ExecState *exec, const Value &v); 486 487 #ifndef NDEBUG 488 void printInfo(ExecState *exec, const char *s, const Value &o, int lineno = -1); 310 489 #endif 311 private:312 /**313 * Initialize global object and context. For internal use only.314 */315 void init();316 void clear();317 /**318 * Called when the first interpreter is instanciated. Initializes319 * global pointers.320 */321 void globalInit();322 /**323 * Called when the last interpreter instance is destroyed. Frees324 * globally allocated memory.325 */326 void globalClear();327 bool evaluate(const UChar *code, unsigned int length, const KJSO &thisV = KJSO(),328 bool onlyCheckSyntax = false);329 bool call(const KJSO &scope, const UString &func, const List &args);330 bool call(const KJSO &func, const KJSO &thisV,331 const List &args, const List &extraScope);332 333 public:334 ProgramNode *progNode() const { return stack->progNode; }335 void setProgNode(ProgramNode *p) { stack->progNode = p; }336 Node *firstNode() const { return stack->firstNode; }337 void setFirstNode(Node *n) { stack->firstNode = n; }338 void pushStack();339 void popStack();340 KJScriptImp *next, *prev;341 KJScript *scr;342 ExecutionStack *stack;343 344 private:345 ProgramNode *progN;346 Node *firstN;347 348 static KJScriptImp *curr, *hook;349 static int instances; // total number of instances350 static int running; // total number running351 bool initialized;352 Lexer *lex;353 Context *con;354 Global glob;355 int errType, errLine;356 UString errMsg;357 #ifdef KJS_DEBUGGER358 Debugger *dbg;359 int sid;360 #endif361 const char *exMsg;362 Imp *exVal;363 Imp *retVal;364 int recursion;365 };366 367 inline bool KJScriptImp::hadException()368 {369 assert(curr);370 return curr->exMsg;371 }372 373 /**374 * @short Struct used to return the property names of an object375 */376 class PropList {377 public:378 PropList(UString nm = UString::null, PropList *nx = 0) :379 name(nm), next(nx) {};380 ~PropList() {381 if(next) delete next;382 }383 /**384 * The property name385 */386 UString name;387 /**388 * The next property389 */390 PropList *next;391 bool contains(const UString &name);392 };393 394 /* TODO just temporary until functions are objects and this becomes395 a member function. Called by RelationNode for 'instanceof' operator. */396 KJSO hasInstance(const KJSO &F, const KJSO &V);397 398 // #define KJS_VERBOSE399 #ifndef NDEBUG400 void printInfo( const char *s, const KJSO &o );401 #endif402 490 403 491 }; // namespace 404 492 405 493 406 #endif 494 #endif // _INTERNAL_H_
Note:
See TracChangeset
for help on using the changeset viewer.