1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
5 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
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 Steet, 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 "protect.h"
|
---|
32 | #include "types.h"
|
---|
33 | #include "interpreter.h"
|
---|
34 | #include "scope_chain.h"
|
---|
35 | #include <kxmlcore/SharedPtr.h>
|
---|
36 |
|
---|
37 | #if !WIN32
|
---|
38 | #define KJS_MULTIPLE_THREADS 1
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #define I18N_NOOP(s) s
|
---|
42 |
|
---|
43 | namespace KJS {
|
---|
44 |
|
---|
45 | class Node;
|
---|
46 | class ProgramNode;
|
---|
47 | class FunctionBodyNode;
|
---|
48 | class FunctionPrototypeImp;
|
---|
49 | class FunctionImp;
|
---|
50 | class Debugger;
|
---|
51 |
|
---|
52 | // ---------------------------------------------------------------------------
|
---|
53 | // Primitive impls
|
---|
54 | // ---------------------------------------------------------------------------
|
---|
55 |
|
---|
56 | class UndefinedImp : public AllocatedValueImp {
|
---|
57 | public:
|
---|
58 | Type type() const { return UndefinedType; }
|
---|
59 |
|
---|
60 | ValueImp *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 | ObjectImp *toObject(ExecState *exec) const;
|
---|
65 | };
|
---|
66 |
|
---|
67 | class NullImp : public AllocatedValueImp {
|
---|
68 | public:
|
---|
69 | Type type() const { return NullType; }
|
---|
70 |
|
---|
71 | ValueImp *toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
|
---|
72 | bool toBoolean(ExecState *exec) const;
|
---|
73 | double toNumber(ExecState *exec) const;
|
---|
74 | UString toString(ExecState *exec) const;
|
---|
75 | ObjectImp *toObject(ExecState *exec) const;
|
---|
76 | };
|
---|
77 |
|
---|
78 | class BooleanImp : public AllocatedValueImp {
|
---|
79 | public:
|
---|
80 | BooleanImp(bool v = false) : val(v) { }
|
---|
81 | bool value() const { return val; }
|
---|
82 |
|
---|
83 | Type type() const { return BooleanType; }
|
---|
84 |
|
---|
85 | ValueImp *toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
|
---|
86 | bool toBoolean(ExecState *exec) const;
|
---|
87 | double toNumber(ExecState *exec) const;
|
---|
88 | UString toString(ExecState *exec) const;
|
---|
89 | ObjectImp *toObject(ExecState *exec) const;
|
---|
90 |
|
---|
91 | private:
|
---|
92 | bool val;
|
---|
93 | };
|
---|
94 |
|
---|
95 | class StringImp : public AllocatedValueImp {
|
---|
96 | public:
|
---|
97 | StringImp(const UString& v) : val(v) { }
|
---|
98 | UString value() const { return val; }
|
---|
99 |
|
---|
100 | Type type() const { return StringType; }
|
---|
101 |
|
---|
102 | ValueImp *toPrimitive(ExecState *exec, Type preferred = UnspecifiedType) const;
|
---|
103 | bool toBoolean(ExecState *exec) const;
|
---|
104 | double toNumber(ExecState *exec) const;
|
---|
105 | UString toString(ExecState *exec) const;
|
---|
106 | ObjectImp *toObject(ExecState *exec) const;
|
---|
107 |
|
---|
108 | private:
|
---|
109 | UString val;
|
---|
110 | };
|
---|
111 |
|
---|
112 | class NumberImp : public AllocatedValueImp {
|
---|
113 | friend class ConstantValues;
|
---|
114 | friend class InterpreterImp;
|
---|
115 | friend ValueImp *jsNumber(int);
|
---|
116 | friend ValueImp *jsNumber(unsigned);
|
---|
117 | friend ValueImp *jsNumber(long);
|
---|
118 | friend ValueImp *jsNumber(unsigned long);
|
---|
119 | friend ValueImp *jsNumber(long long);
|
---|
120 | friend ValueImp *jsNumber(unsigned long long);
|
---|
121 | friend ValueImp *jsNumber(double);
|
---|
122 | friend ValueImp *jsNumber(double, bool);
|
---|
123 | public:
|
---|
124 | double value() const { return val; }
|
---|
125 |
|
---|
126 | Type type() const { return NumberType; }
|
---|
127 |
|
---|
128 | ValueImp *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 | ObjectImp *toObject(ExecState *exec) const;
|
---|
133 |
|
---|
134 | private:
|
---|
135 | NumberImp(double v) : val(v) { }
|
---|
136 |
|
---|
137 | virtual bool getUInt32(uint32_t&) const;
|
---|
138 |
|
---|
139 | double val;
|
---|
140 | };
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * @short The "label set" in Ecma-262 spec
|
---|
144 | */
|
---|
145 | class LabelStack {
|
---|
146 | public:
|
---|
147 | LabelStack(): tos(0L), iterationDepth(0), switchDepth(0) {}
|
---|
148 | ~LabelStack();
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * If id is not empty and is not in the stack already, puts it on top of
|
---|
152 | * the stack and returns true, otherwise returns false
|
---|
153 | */
|
---|
154 | bool push(const Identifier &id);
|
---|
155 | /**
|
---|
156 | * Is the id in the stack?
|
---|
157 | */
|
---|
158 | bool contains(const Identifier &id) const;
|
---|
159 | /**
|
---|
160 | * Removes from the stack the last pushed id (what else?)
|
---|
161 | */
|
---|
162 | void pop();
|
---|
163 |
|
---|
164 | void pushIteration() { iterationDepth++; }
|
---|
165 | void popIteration() { iterationDepth--; }
|
---|
166 | bool inIteration() const { return (iterationDepth > 0); }
|
---|
167 |
|
---|
168 | void pushSwitch() { switchDepth++; }
|
---|
169 | void popSwitch() { switchDepth--; }
|
---|
170 | bool inSwitch() const { return (switchDepth > 0); }
|
---|
171 |
|
---|
172 | private:
|
---|
173 | LabelStack(const LabelStack &other);
|
---|
174 | LabelStack &operator=(const LabelStack &other);
|
---|
175 |
|
---|
176 | struct StackElem {
|
---|
177 | Identifier id;
|
---|
178 | StackElem *prev;
|
---|
179 | };
|
---|
180 |
|
---|
181 | StackElem *tos;
|
---|
182 | int iterationDepth;
|
---|
183 | int switchDepth;
|
---|
184 | };
|
---|
185 |
|
---|
186 |
|
---|
187 | // ---------------------------------------------------------------------------
|
---|
188 | // Parsing & evaluation
|
---|
189 | // ---------------------------------------------------------------------------
|
---|
190 |
|
---|
191 | enum CodeType { GlobalCode,
|
---|
192 | EvalCode,
|
---|
193 | FunctionCode,
|
---|
194 | AnonymousCode };
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * @internal
|
---|
198 | *
|
---|
199 | * Parses ECMAScript source code and converts into ProgramNode objects, which
|
---|
200 | * represent the root of a parse tree. This class provides a conveniant workaround
|
---|
201 | * for the problem of the bison parser working in a static context.
|
---|
202 | */
|
---|
203 | class Parser {
|
---|
204 | public:
|
---|
205 | static SharedPtr<ProgramNode> parse(const UString &sourceURL, int startingLineNumber,
|
---|
206 | const UChar *code, unsigned int length, int *sourceId = 0,
|
---|
207 | int *errLine = 0, UString *errMsg = 0);
|
---|
208 | static void accept(ProgramNode *prog);
|
---|
209 |
|
---|
210 | static void saveNewNode(Node *node);
|
---|
211 |
|
---|
212 | static int sid;
|
---|
213 | };
|
---|
214 |
|
---|
215 | class SavedBuiltinsInternal {
|
---|
216 | friend class InterpreterImp;
|
---|
217 | private:
|
---|
218 | ProtectedPtr<ObjectImp> b_Object;
|
---|
219 | ProtectedPtr<ObjectImp> b_Function;
|
---|
220 | ProtectedPtr<ObjectImp> b_Array;
|
---|
221 | ProtectedPtr<ObjectImp> b_Boolean;
|
---|
222 | ProtectedPtr<ObjectImp> b_String;
|
---|
223 | ProtectedPtr<ObjectImp> b_Number;
|
---|
224 | ProtectedPtr<ObjectImp> b_Date;
|
---|
225 | ProtectedPtr<ObjectImp> b_RegExp;
|
---|
226 | ProtectedPtr<ObjectImp> b_Error;
|
---|
227 |
|
---|
228 | ProtectedPtr<ObjectImp> b_ObjectPrototype;
|
---|
229 | ProtectedPtr<ObjectImp> b_FunctionPrototype;
|
---|
230 | ProtectedPtr<ObjectImp> b_ArrayPrototype;
|
---|
231 | ProtectedPtr<ObjectImp> b_BooleanPrototype;
|
---|
232 | ProtectedPtr<ObjectImp> b_StringPrototype;
|
---|
233 | ProtectedPtr<ObjectImp> b_NumberPrototype;
|
---|
234 | ProtectedPtr<ObjectImp> b_DatePrototype;
|
---|
235 | ProtectedPtr<ObjectImp> b_RegExpPrototype;
|
---|
236 | ProtectedPtr<ObjectImp> b_ErrorPrototype;
|
---|
237 |
|
---|
238 | ProtectedPtr<ObjectImp> b_evalError;
|
---|
239 | ProtectedPtr<ObjectImp> b_rangeError;
|
---|
240 | ProtectedPtr<ObjectImp> b_referenceError;
|
---|
241 | ProtectedPtr<ObjectImp> b_syntaxError;
|
---|
242 | ProtectedPtr<ObjectImp> b_typeError;
|
---|
243 | ProtectedPtr<ObjectImp> b_uriError;
|
---|
244 |
|
---|
245 | ProtectedPtr<ObjectImp> b_evalErrorPrototype;
|
---|
246 | ProtectedPtr<ObjectImp> b_rangeErrorPrototype;
|
---|
247 | ProtectedPtr<ObjectImp> b_referenceErrorPrototype;
|
---|
248 | ProtectedPtr<ObjectImp> b_syntaxErrorPrototype;
|
---|
249 | ProtectedPtr<ObjectImp> b_typeErrorPrototype;
|
---|
250 | ProtectedPtr<ObjectImp> b_uriErrorPrototype;
|
---|
251 | };
|
---|
252 |
|
---|
253 | class InterpreterImp {
|
---|
254 | friend class Collector;
|
---|
255 | public:
|
---|
256 | static void globalInit();
|
---|
257 | static void globalClear();
|
---|
258 |
|
---|
259 | InterpreterImp(Interpreter *interp, ObjectImp *glob);
|
---|
260 | ~InterpreterImp();
|
---|
261 |
|
---|
262 | ObjectImp *globalObject() { return global; }
|
---|
263 | Interpreter *interpreter() const { return m_interpreter; }
|
---|
264 |
|
---|
265 | void initGlobalObject();
|
---|
266 | static void lock();
|
---|
267 | static void unlock();
|
---|
268 | static int lockCount();
|
---|
269 |
|
---|
270 | void mark();
|
---|
271 |
|
---|
272 | ExecState *globalExec() { return &globExec; }
|
---|
273 | bool checkSyntax(const UString &code);
|
---|
274 | Completion evaluate(const UString &code, ValueImp *thisV, const UString &sourceURL, int startingLineNumber);
|
---|
275 | Debugger *debugger() const { return dbg; }
|
---|
276 | void setDebugger(Debugger *d) { dbg = d; }
|
---|
277 |
|
---|
278 | ObjectImp *builtinObject() const { return b_Object; }
|
---|
279 | ObjectImp *builtinFunction() const { return b_Function; }
|
---|
280 | ObjectImp *builtinArray() const { return b_Array; }
|
---|
281 | ObjectImp *builtinBoolean() const { return b_Boolean; }
|
---|
282 | ObjectImp *builtinString() const { return b_String; }
|
---|
283 | ObjectImp *builtinNumber() const { return b_Number; }
|
---|
284 | ObjectImp *builtinDate() const { return b_Date; }
|
---|
285 | ObjectImp *builtinRegExp() const { return b_RegExp; }
|
---|
286 | ObjectImp *builtinError() const { return b_Error; }
|
---|
287 |
|
---|
288 | ObjectImp *builtinObjectPrototype() const { return b_ObjectPrototype; }
|
---|
289 | ObjectImp *builtinFunctionPrototype() const { return b_FunctionPrototype; }
|
---|
290 | ObjectImp *builtinArrayPrototype() const { return b_ArrayPrototype; }
|
---|
291 | ObjectImp *builtinBooleanPrototype() const { return b_BooleanPrototype; }
|
---|
292 | ObjectImp *builtinStringPrototype() const { return b_StringPrototype; }
|
---|
293 | ObjectImp *builtinNumberPrototype() const { return b_NumberPrototype; }
|
---|
294 | ObjectImp *builtinDatePrototype() const { return b_DatePrototype; }
|
---|
295 | ObjectImp *builtinRegExpPrototype() const { return b_RegExpPrototype; }
|
---|
296 | ObjectImp *builtinErrorPrototype() const { return b_ErrorPrototype; }
|
---|
297 |
|
---|
298 | ObjectImp *builtinEvalError() const { return b_evalError; }
|
---|
299 | ObjectImp *builtinRangeError() const { return b_rangeError; }
|
---|
300 | ObjectImp *builtinReferenceError() const { return b_referenceError; }
|
---|
301 | ObjectImp *builtinSyntaxError() const { return b_syntaxError; }
|
---|
302 | ObjectImp *builtinTypeError() const { return b_typeError; }
|
---|
303 | ObjectImp *builtinURIError() const { return b_uriError; }
|
---|
304 |
|
---|
305 | ObjectImp *builtinEvalErrorPrototype() const { return b_evalErrorPrototype; }
|
---|
306 | ObjectImp *builtinRangeErrorPrototype() const { return b_rangeErrorPrototype; }
|
---|
307 | ObjectImp *builtinReferenceErrorPrototype() const { return b_referenceErrorPrototype; }
|
---|
308 | ObjectImp *builtinSyntaxErrorPrototype() const { return b_syntaxErrorPrototype; }
|
---|
309 | ObjectImp *builtinTypeErrorPrototype() const { return b_typeErrorPrototype; }
|
---|
310 | ObjectImp *builtinURIErrorPrototype() const { return b_uriErrorPrototype; }
|
---|
311 |
|
---|
312 | void setCompatMode(Interpreter::CompatMode mode) { m_compatMode = mode; }
|
---|
313 | Interpreter::CompatMode compatMode() const { return m_compatMode; }
|
---|
314 |
|
---|
315 | // Chained list of interpreters (ring)
|
---|
316 | static InterpreterImp* firstInterpreter() { return s_hook; }
|
---|
317 | InterpreterImp *nextInterpreter() const { return next; }
|
---|
318 | InterpreterImp *prevInterpreter() const { return prev; }
|
---|
319 |
|
---|
320 | static InterpreterImp *interpreterWithGlobalObject(ObjectImp *);
|
---|
321 |
|
---|
322 | void setContext(ContextImp *c) { _context = c; }
|
---|
323 | ContextImp *context() const { return _context; }
|
---|
324 |
|
---|
325 | void saveBuiltins (SavedBuiltins &builtins) const;
|
---|
326 | void restoreBuiltins (const SavedBuiltins &builtins);
|
---|
327 |
|
---|
328 | private:
|
---|
329 | void clear();
|
---|
330 | Interpreter *m_interpreter;
|
---|
331 | ObjectImp *global;
|
---|
332 | Debugger *dbg;
|
---|
333 |
|
---|
334 | // Built-in properties of the object prototype. These are accessible
|
---|
335 | // from here even if they are replaced by js code (e.g. assigning to
|
---|
336 | // Array.prototype)
|
---|
337 |
|
---|
338 | ProtectedPtr<ObjectImp> b_Object;
|
---|
339 | ProtectedPtr<ObjectImp> b_Function;
|
---|
340 | ProtectedPtr<ObjectImp> b_Array;
|
---|
341 | ProtectedPtr<ObjectImp> b_Boolean;
|
---|
342 | ProtectedPtr<ObjectImp> b_String;
|
---|
343 | ProtectedPtr<ObjectImp> b_Number;
|
---|
344 | ProtectedPtr<ObjectImp> b_Date;
|
---|
345 | ProtectedPtr<ObjectImp> b_RegExp;
|
---|
346 | ProtectedPtr<ObjectImp> b_Error;
|
---|
347 |
|
---|
348 | ProtectedPtr<ObjectImp> b_ObjectPrototype;
|
---|
349 | ProtectedPtr<ObjectImp> b_FunctionPrototype;
|
---|
350 | ProtectedPtr<ObjectImp> b_ArrayPrototype;
|
---|
351 | ProtectedPtr<ObjectImp> b_BooleanPrototype;
|
---|
352 | ProtectedPtr<ObjectImp> b_StringPrototype;
|
---|
353 | ProtectedPtr<ObjectImp> b_NumberPrototype;
|
---|
354 | ProtectedPtr<ObjectImp> b_DatePrototype;
|
---|
355 | ProtectedPtr<ObjectImp> b_RegExpPrototype;
|
---|
356 | ProtectedPtr<ObjectImp> b_ErrorPrototype;
|
---|
357 |
|
---|
358 | ProtectedPtr<ObjectImp> b_evalError;
|
---|
359 | ProtectedPtr<ObjectImp> b_rangeError;
|
---|
360 | ProtectedPtr<ObjectImp> b_referenceError;
|
---|
361 | ProtectedPtr<ObjectImp> b_syntaxError;
|
---|
362 | ProtectedPtr<ObjectImp> b_typeError;
|
---|
363 | ProtectedPtr<ObjectImp> b_uriError;
|
---|
364 |
|
---|
365 | ProtectedPtr<ObjectImp> b_evalErrorPrototype;
|
---|
366 | ProtectedPtr<ObjectImp> b_rangeErrorPrototype;
|
---|
367 | ProtectedPtr<ObjectImp> b_referenceErrorPrototype;
|
---|
368 | ProtectedPtr<ObjectImp> b_syntaxErrorPrototype;
|
---|
369 | ProtectedPtr<ObjectImp> b_typeErrorPrototype;
|
---|
370 | ProtectedPtr<ObjectImp> b_uriErrorPrototype;
|
---|
371 |
|
---|
372 | ExecState globExec;
|
---|
373 | Interpreter::CompatMode m_compatMode;
|
---|
374 |
|
---|
375 | // Chained list of interpreters (ring) - for collector
|
---|
376 | static InterpreterImp* s_hook;
|
---|
377 | InterpreterImp *next, *prev;
|
---|
378 |
|
---|
379 | ContextImp *_context;
|
---|
380 |
|
---|
381 | int recursion;
|
---|
382 | };
|
---|
383 |
|
---|
384 | class AttachedInterpreter;
|
---|
385 | class DebuggerImp {
|
---|
386 | public:
|
---|
387 |
|
---|
388 | DebuggerImp() {
|
---|
389 | interps = 0;
|
---|
390 | isAborted = false;
|
---|
391 | }
|
---|
392 |
|
---|
393 | void abort() { isAborted = true; }
|
---|
394 | bool aborted() const { return isAborted; }
|
---|
395 |
|
---|
396 | AttachedInterpreter *interps;
|
---|
397 | bool isAborted;
|
---|
398 | };
|
---|
399 |
|
---|
400 |
|
---|
401 |
|
---|
402 | class InternalFunctionImp : public ObjectImp {
|
---|
403 | public:
|
---|
404 | InternalFunctionImp();
|
---|
405 | InternalFunctionImp(FunctionPrototypeImp *funcProto);
|
---|
406 | bool implementsHasInstance() const;
|
---|
407 | bool hasInstance(ExecState *exec, ValueImp *value);
|
---|
408 |
|
---|
409 | virtual const ClassInfo *classInfo() const { return &info; }
|
---|
410 | static const ClassInfo info;
|
---|
411 | };
|
---|
412 |
|
---|
413 | // helper function for toInteger, toInt32, toUInt32 and toUInt16
|
---|
414 | double roundValue(ExecState *, ValueImp *);
|
---|
415 |
|
---|
416 | #ifndef NDEBUG
|
---|
417 | void printInfo(ExecState *exec, const char *s, ValueImp *, int lineno = -1);
|
---|
418 | #endif
|
---|
419 |
|
---|
420 | inline LabelStack::~LabelStack()
|
---|
421 | {
|
---|
422 | StackElem *prev;
|
---|
423 | for (StackElem *e = tos; e; e = prev) {
|
---|
424 | prev = e->prev;
|
---|
425 | delete e;
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|
429 | inline void LabelStack::pop()
|
---|
430 | {
|
---|
431 | if (StackElem *e = tos) {
|
---|
432 | tos = e->prev;
|
---|
433 | delete e;
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | } // namespace
|
---|
438 |
|
---|
439 | #endif // INTERNAL_H
|
---|