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, 2004, 2005, 2006 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 "JSType.h"
|
---|
29 | #include "interpreter.h"
|
---|
30 | #include "object.h"
|
---|
31 | #include "protect.h"
|
---|
32 | #include "scope_chain.h"
|
---|
33 | #include "types.h"
|
---|
34 | #include "ustring.h"
|
---|
35 |
|
---|
36 | #include <wtf/Noncopyable.h>
|
---|
37 |
|
---|
38 | #define I18N_NOOP(s) s
|
---|
39 |
|
---|
40 | namespace KJS {
|
---|
41 |
|
---|
42 | class Debugger;
|
---|
43 | class FunctionPrototype;
|
---|
44 |
|
---|
45 | // ---------------------------------------------------------------------------
|
---|
46 | // Primitive impls
|
---|
47 | // ---------------------------------------------------------------------------
|
---|
48 |
|
---|
49 | class StringImp : public JSCell {
|
---|
50 | public:
|
---|
51 | StringImp(const UString& v) : val(v) { }
|
---|
52 | UString value() const { return val; }
|
---|
53 |
|
---|
54 | JSType type() const { return StringType; }
|
---|
55 |
|
---|
56 | JSValue *toPrimitive(ExecState *exec, JSType preferred = UnspecifiedType) const;
|
---|
57 | bool toBoolean(ExecState *exec) const;
|
---|
58 | double toNumber(ExecState *exec) const;
|
---|
59 | UString toString(ExecState *exec) const;
|
---|
60 | JSObject *toObject(ExecState *exec) const;
|
---|
61 |
|
---|
62 | private:
|
---|
63 | UString val;
|
---|
64 | };
|
---|
65 |
|
---|
66 | class NumberImp : public JSCell {
|
---|
67 | friend class ConstantValues;
|
---|
68 | friend class InterpreterImp;
|
---|
69 | friend JSValue *jsNumberCell(double);
|
---|
70 | public:
|
---|
71 | double value() const { return val; }
|
---|
72 |
|
---|
73 | JSType type() const { return NumberType; }
|
---|
74 |
|
---|
75 | JSValue *toPrimitive(ExecState *exec, JSType preferred = UnspecifiedType) const;
|
---|
76 | bool toBoolean(ExecState *exec) const;
|
---|
77 | double toNumber(ExecState *exec) const;
|
---|
78 | UString toString(ExecState *exec) const;
|
---|
79 | JSObject *toObject(ExecState *exec) const;
|
---|
80 |
|
---|
81 | private:
|
---|
82 | NumberImp(double v) : val(v) { }
|
---|
83 |
|
---|
84 | virtual bool getUInt32(uint32_t&) const;
|
---|
85 |
|
---|
86 | double val;
|
---|
87 | };
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * @short The "label set" in Ecma-262 spec
|
---|
92 | */
|
---|
93 | class LabelStack : Noncopyable {
|
---|
94 | public:
|
---|
95 | LabelStack()
|
---|
96 | : tos(0)
|
---|
97 | {
|
---|
98 | }
|
---|
99 | ~LabelStack();
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * If id is not empty and is not in the stack already, puts it on top of
|
---|
103 | * the stack and returns true, otherwise returns false
|
---|
104 | */
|
---|
105 | bool push(const Identifier &id);
|
---|
106 | /**
|
---|
107 | * Is the id in the stack?
|
---|
108 | */
|
---|
109 | bool contains(const Identifier &id) const;
|
---|
110 | /**
|
---|
111 | * Removes from the stack the last pushed id (what else?)
|
---|
112 | */
|
---|
113 | void pop();
|
---|
114 |
|
---|
115 | private:
|
---|
116 | struct StackElem {
|
---|
117 | Identifier id;
|
---|
118 | StackElem *prev;
|
---|
119 | };
|
---|
120 |
|
---|
121 | StackElem *tos;
|
---|
122 | };
|
---|
123 |
|
---|
124 |
|
---|
125 | // ---------------------------------------------------------------------------
|
---|
126 | // Evaluation
|
---|
127 | // ---------------------------------------------------------------------------
|
---|
128 |
|
---|
129 | enum CodeType { GlobalCode,
|
---|
130 | EvalCode,
|
---|
131 | FunctionCode,
|
---|
132 | AnonymousCode };
|
---|
133 |
|
---|
134 | class SavedBuiltinsInternal {
|
---|
135 | friend class InterpreterImp;
|
---|
136 | private:
|
---|
137 | ProtectedPtr<JSObject> b_Object;
|
---|
138 | ProtectedPtr<JSObject> b_Function;
|
---|
139 | ProtectedPtr<JSObject> b_Array;
|
---|
140 | ProtectedPtr<JSObject> b_Boolean;
|
---|
141 | ProtectedPtr<JSObject> b_String;
|
---|
142 | ProtectedPtr<JSObject> b_Number;
|
---|
143 | ProtectedPtr<JSObject> b_Date;
|
---|
144 | ProtectedPtr<JSObject> b_RegExp;
|
---|
145 | ProtectedPtr<JSObject> b_Error;
|
---|
146 |
|
---|
147 | ProtectedPtr<JSObject> b_ObjectPrototype;
|
---|
148 | ProtectedPtr<JSObject> b_FunctionPrototype;
|
---|
149 | ProtectedPtr<JSObject> b_ArrayPrototype;
|
---|
150 | ProtectedPtr<JSObject> b_BooleanPrototype;
|
---|
151 | ProtectedPtr<JSObject> b_StringPrototype;
|
---|
152 | ProtectedPtr<JSObject> b_NumberPrototype;
|
---|
153 | ProtectedPtr<JSObject> b_DatePrototype;
|
---|
154 | ProtectedPtr<JSObject> b_RegExpPrototype;
|
---|
155 | ProtectedPtr<JSObject> b_ErrorPrototype;
|
---|
156 |
|
---|
157 | ProtectedPtr<JSObject> b_evalError;
|
---|
158 | ProtectedPtr<JSObject> b_rangeError;
|
---|
159 | ProtectedPtr<JSObject> b_referenceError;
|
---|
160 | ProtectedPtr<JSObject> b_syntaxError;
|
---|
161 | ProtectedPtr<JSObject> b_typeError;
|
---|
162 | ProtectedPtr<JSObject> b_uriError;
|
---|
163 |
|
---|
164 | ProtectedPtr<JSObject> b_evalErrorPrototype;
|
---|
165 | ProtectedPtr<JSObject> b_rangeErrorPrototype;
|
---|
166 | ProtectedPtr<JSObject> b_referenceErrorPrototype;
|
---|
167 | ProtectedPtr<JSObject> b_syntaxErrorPrototype;
|
---|
168 | ProtectedPtr<JSObject> b_typeErrorPrototype;
|
---|
169 | ProtectedPtr<JSObject> b_uriErrorPrototype;
|
---|
170 | };
|
---|
171 |
|
---|
172 | class InterpreterImp {
|
---|
173 | friend class Collector;
|
---|
174 | public:
|
---|
175 | InterpreterImp(Interpreter *interp, JSObject *glob);
|
---|
176 | ~InterpreterImp();
|
---|
177 |
|
---|
178 | JSObject *globalObject() { return global; }
|
---|
179 | Interpreter *interpreter() const { return m_interpreter; }
|
---|
180 |
|
---|
181 | void initGlobalObject();
|
---|
182 |
|
---|
183 | void mark();
|
---|
184 |
|
---|
185 | ExecState *globalExec() { return &globExec; }
|
---|
186 | bool checkSyntax(const UString &code);
|
---|
187 | Completion evaluate(const UChar* code, int codeLength, JSValue* thisV, const UString& sourceURL, int startingLineNumber);
|
---|
188 | Debugger *debugger() const { return dbg; }
|
---|
189 | void setDebugger(Debugger *d) { dbg = d; }
|
---|
190 |
|
---|
191 | JSObject *builtinObject() const { return b_Object; }
|
---|
192 | JSObject *builtinFunction() const { return b_Function; }
|
---|
193 | JSObject *builtinArray() const { return b_Array; }
|
---|
194 | JSObject *builtinBoolean() const { return b_Boolean; }
|
---|
195 | JSObject *builtinString() const { return b_String; }
|
---|
196 | JSObject *builtinNumber() const { return b_Number; }
|
---|
197 | JSObject *builtinDate() const { return b_Date; }
|
---|
198 | JSObject *builtinRegExp() const { return b_RegExp; }
|
---|
199 | JSObject *builtinError() const { return b_Error; }
|
---|
200 |
|
---|
201 | JSObject *builtinObjectPrototype() const { return b_ObjectPrototype; }
|
---|
202 | JSObject *builtinFunctionPrototype() const { return b_FunctionPrototype; }
|
---|
203 | JSObject *builtinArrayPrototype() const { return b_ArrayPrototype; }
|
---|
204 | JSObject *builtinBooleanPrototype() const { return b_BooleanPrototype; }
|
---|
205 | JSObject *builtinStringPrototype() const { return b_StringPrototype; }
|
---|
206 | JSObject *builtinNumberPrototype() const { return b_NumberPrototype; }
|
---|
207 | JSObject *builtinDatePrototype() const { return b_DatePrototype; }
|
---|
208 | JSObject *builtinRegExpPrototype() const { return b_RegExpPrototype; }
|
---|
209 | JSObject *builtinErrorPrototype() const { return b_ErrorPrototype; }
|
---|
210 |
|
---|
211 | JSObject *builtinEvalError() const { return b_evalError; }
|
---|
212 | JSObject *builtinRangeError() const { return b_rangeError; }
|
---|
213 | JSObject *builtinReferenceError() const { return b_referenceError; }
|
---|
214 | JSObject *builtinSyntaxError() const { return b_syntaxError; }
|
---|
215 | JSObject *builtinTypeError() const { return b_typeError; }
|
---|
216 | JSObject *builtinURIError() const { return b_uriError; }
|
---|
217 |
|
---|
218 | JSObject *builtinEvalErrorPrototype() const { return b_evalErrorPrototype; }
|
---|
219 | JSObject *builtinRangeErrorPrototype() const { return b_rangeErrorPrototype; }
|
---|
220 | JSObject *builtinReferenceErrorPrototype() const { return b_referenceErrorPrototype; }
|
---|
221 | JSObject *builtinSyntaxErrorPrototype() const { return b_syntaxErrorPrototype; }
|
---|
222 | JSObject *builtinTypeErrorPrototype() const { return b_typeErrorPrototype; }
|
---|
223 | JSObject *builtinURIErrorPrototype() const { return b_uriErrorPrototype; }
|
---|
224 |
|
---|
225 | void setCompatMode(Interpreter::CompatMode mode) { m_compatMode = mode; }
|
---|
226 | Interpreter::CompatMode compatMode() const { return m_compatMode; }
|
---|
227 |
|
---|
228 | // Chained list of interpreters (ring)
|
---|
229 | static InterpreterImp* firstInterpreter() { return s_hook; }
|
---|
230 | InterpreterImp *nextInterpreter() const { return next; }
|
---|
231 | InterpreterImp *prevInterpreter() const { return prev; }
|
---|
232 |
|
---|
233 | static InterpreterImp *interpreterWithGlobalObject(JSObject *);
|
---|
234 |
|
---|
235 | void setContext(ContextImp *c) { _context = c; }
|
---|
236 | ContextImp *context() const { return _context; }
|
---|
237 |
|
---|
238 | void saveBuiltins (SavedBuiltins &builtins) const;
|
---|
239 | void restoreBuiltins (const SavedBuiltins &builtins);
|
---|
240 |
|
---|
241 | private:
|
---|
242 | void clear();
|
---|
243 | Interpreter *m_interpreter;
|
---|
244 | JSObject *global;
|
---|
245 | Debugger *dbg;
|
---|
246 |
|
---|
247 | // Built-in properties of the object prototype. These are accessible
|
---|
248 | // from here even if they are replaced by js code (e.g. assigning to
|
---|
249 | // Array.prototype)
|
---|
250 |
|
---|
251 | ProtectedPtr<JSObject> b_Object;
|
---|
252 | ProtectedPtr<JSObject> b_Function;
|
---|
253 | ProtectedPtr<JSObject> b_Array;
|
---|
254 | ProtectedPtr<JSObject> b_Boolean;
|
---|
255 | ProtectedPtr<JSObject> b_String;
|
---|
256 | ProtectedPtr<JSObject> b_Number;
|
---|
257 | ProtectedPtr<JSObject> b_Date;
|
---|
258 | ProtectedPtr<JSObject> b_RegExp;
|
---|
259 | ProtectedPtr<JSObject> b_Error;
|
---|
260 |
|
---|
261 | ProtectedPtr<JSObject> b_ObjectPrototype;
|
---|
262 | ProtectedPtr<JSObject> b_FunctionPrototype;
|
---|
263 | ProtectedPtr<JSObject> b_ArrayPrototype;
|
---|
264 | ProtectedPtr<JSObject> b_BooleanPrototype;
|
---|
265 | ProtectedPtr<JSObject> b_StringPrototype;
|
---|
266 | ProtectedPtr<JSObject> b_NumberPrototype;
|
---|
267 | ProtectedPtr<JSObject> b_DatePrototype;
|
---|
268 | ProtectedPtr<JSObject> b_RegExpPrototype;
|
---|
269 | ProtectedPtr<JSObject> b_ErrorPrototype;
|
---|
270 |
|
---|
271 | ProtectedPtr<JSObject> b_evalError;
|
---|
272 | ProtectedPtr<JSObject> b_rangeError;
|
---|
273 | ProtectedPtr<JSObject> b_referenceError;
|
---|
274 | ProtectedPtr<JSObject> b_syntaxError;
|
---|
275 | ProtectedPtr<JSObject> b_typeError;
|
---|
276 | ProtectedPtr<JSObject> b_uriError;
|
---|
277 |
|
---|
278 | ProtectedPtr<JSObject> b_evalErrorPrototype;
|
---|
279 | ProtectedPtr<JSObject> b_rangeErrorPrototype;
|
---|
280 | ProtectedPtr<JSObject> b_referenceErrorPrototype;
|
---|
281 | ProtectedPtr<JSObject> b_syntaxErrorPrototype;
|
---|
282 | ProtectedPtr<JSObject> b_typeErrorPrototype;
|
---|
283 | ProtectedPtr<JSObject> b_uriErrorPrototype;
|
---|
284 |
|
---|
285 | ExecState globExec;
|
---|
286 | Interpreter::CompatMode m_compatMode;
|
---|
287 |
|
---|
288 | // Chained list of interpreters (ring) - for collector
|
---|
289 | static InterpreterImp* s_hook;
|
---|
290 | InterpreterImp *next, *prev;
|
---|
291 |
|
---|
292 | ContextImp *_context;
|
---|
293 |
|
---|
294 | int recursion;
|
---|
295 | };
|
---|
296 |
|
---|
297 | class AttachedInterpreter;
|
---|
298 | class DebuggerImp {
|
---|
299 | public:
|
---|
300 |
|
---|
301 | DebuggerImp() {
|
---|
302 | interps = 0;
|
---|
303 | isAborted = false;
|
---|
304 | }
|
---|
305 |
|
---|
306 | void abort() { isAborted = true; }
|
---|
307 | bool aborted() const { return isAborted; }
|
---|
308 |
|
---|
309 | AttachedInterpreter *interps;
|
---|
310 | bool isAborted;
|
---|
311 | };
|
---|
312 |
|
---|
313 | class InternalFunctionImp : public JSObject {
|
---|
314 | public:
|
---|
315 | InternalFunctionImp();
|
---|
316 | InternalFunctionImp(FunctionPrototype*);
|
---|
317 | InternalFunctionImp(FunctionPrototype*, const Identifier&);
|
---|
318 |
|
---|
319 | virtual bool implementsCall() const;
|
---|
320 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObjec, const List& args) = 0;
|
---|
321 | virtual bool implementsHasInstance() const;
|
---|
322 | virtual bool hasInstance(ExecState*, JSValue*);
|
---|
323 |
|
---|
324 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
325 | static const ClassInfo info;
|
---|
326 | const Identifier& functionName() const { return m_name; }
|
---|
327 |
|
---|
328 | private:
|
---|
329 | Identifier m_name;
|
---|
330 | };
|
---|
331 |
|
---|
332 | // helper function for toInteger, toInt32, toUInt32 and toUInt16
|
---|
333 | double roundValue(ExecState *, JSValue *);
|
---|
334 |
|
---|
335 | #ifndef NDEBUG
|
---|
336 | void printInfo(ExecState *exec, const char *s, JSValue *, int lineno = -1);
|
---|
337 | #endif
|
---|
338 |
|
---|
339 | inline LabelStack::~LabelStack()
|
---|
340 | {
|
---|
341 | StackElem *prev;
|
---|
342 | for (StackElem *e = tos; e; e = prev) {
|
---|
343 | prev = e->prev;
|
---|
344 | delete e;
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | inline void LabelStack::pop()
|
---|
349 | {
|
---|
350 | if (StackElem *e = tos) {
|
---|
351 | tos = e->prev;
|
---|
352 | delete e;
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | } // namespace
|
---|
357 |
|
---|
358 | #endif // INTERNAL_H
|
---|