32 #include "interpreter.h"
33 #include "scope_chain.h"
34 #include "array_instance.h"
37 #define I18N_NOOP(s) s
42 static const double D16 = 65536.0;
43 static const double D32 = 4294967296.0;
45 class FunctionBodyNode;
46 class FunctionBodyNode;
47 class FunctionPrototypeImp;
56 class UndefinedImp :
public ValueImp {
58 Type type()
const {
return UndefinedType; }
60 Value 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 Object toObject(ExecState *exec)
const;
66 static UndefinedImp *staticUndefined;
69 inline Undefined::Undefined(UndefinedImp *imp) : Value(imp) { }
71 class NullImp :
public ValueImp {
73 Type type()
const {
return NullType; }
75 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType)
const;
76 bool toBoolean(ExecState *exec)
const;
77 double toNumber(ExecState *exec)
const;
78 UString toString(ExecState *exec)
const;
79 Object toObject(ExecState *exec)
const;
81 static NullImp *staticNull;
84 inline Null::Null(NullImp *imp) : Value(imp) { }
86 class BooleanImp :
public ValueImp {
88 BooleanImp(
bool v =
false) : val(v) { }
89 bool value()
const {
return val; }
91 Type type()
const {
return BooleanType; }
93 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType)
const;
94 bool toBoolean(ExecState *exec)
const;
95 double toNumber(ExecState *exec)
const;
96 UString toString(ExecState *exec)
const;
97 Object toObject(ExecState *exec)
const;
99 static BooleanImp *staticTrue;
100 static BooleanImp *staticFalse;
105 inline Boolean::Boolean(BooleanImp *imp) : Value(imp) { }
107 class StringImp :
public ValueImp {
109 StringImp(
const UString& v) : val(v) { }
110 UString value()
const {
return val; }
112 Type type()
const {
return StringType; }
114 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType)
const;
115 bool toBoolean(ExecState *exec)
const;
116 double toNumber(ExecState *exec)
const;
117 UString toString(ExecState *exec)
const;
118 Object toObject(ExecState *exec)
const;
124 inline String::String(StringImp *imp) : Value(imp) { }
126 class NumberImp :
public ValueImp {
128 friend class InterpreterImp;
130 static ValueImp *create(
int);
131 static ValueImp *create(
double);
132 static ValueImp *zero() {
return SimpleNumber::make(0); }
133 static ValueImp *one() {
return SimpleNumber::make(1); }
134 static ValueImp *two() {
return SimpleNumber::make(2); }
136 double value()
const {
return val; }
138 Type type()
const {
return NumberType; }
140 Value toPrimitive(ExecState *exec, Type preferred = UnspecifiedType)
const;
141 bool toBoolean(ExecState *exec)
const;
142 double toNumber(ExecState *exec)
const;
143 UString toString(ExecState *exec)
const;
144 Object toObject(ExecState *exec)
const;
146 static NumberImp *staticNaN;
149 NumberImp(
double v) : val(v) { }
151 virtual bool toUInt32(
unsigned&)
const;
156 inline Number::Number(NumberImp *imp) : Value(imp) { }
163 LabelStack(): tos(0L), iterationDepth(0), switchDepth(0) {}
183 void pushIteration() { iterationDepth++; }
184 void popIteration() { iterationDepth--; }
185 bool inIteration()
const {
return (iterationDepth > 0); }
187 void pushSwitch() { switchDepth++; }
188 void popSwitch() { switchDepth--; }
189 bool inSwitch()
const {
return (switchDepth > 0); }
211 : sid(_sid), interpreter(0), refcount(0), next(0) {}
213 void ref() { refcount++; }
214 void deref() {
if (!--refcount) cleanup(); }
218 InterpreterImp *interpreter;
232 static FunctionBodyNode *parse(
const UChar *code,
unsigned int length, SourceCode **src,
233 int *errLine = 0, UString *errMsg = 0);
235 static FunctionBodyNode *progNode;
236 static SourceCode *source;
241 class InterpreterImp {
242 friend class Collector;
244 static void globalInit();
245 static void globalClear();
247 InterpreterImp(Interpreter *interp,
const Object &glob);
250 Object &globalObject()
const {
return const_cast<Object &
>(global); }
251 Interpreter* interpreter()
const {
return m_interpreter; }
253 void initGlobalObject();
255 static void unlock();
259 ExecState *globalExec() {
return globExec; }
260 bool checkSyntax(
const UString &code,
int *errLine, UString *errMsg);
261 bool checkSyntax(
const UString &code);
262 Completion evaluate(
const UString &code,
const Value &thisV);
263 Debugger *debugger()
const {
return dbg; }
264 void setDebugger(Debugger *d);
266 Object builtinObject()
const {
return b_Object; }
267 Object builtinFunction()
const {
return b_Function; }
268 Object builtinArray()
const {
return b_Array; }
269 Object builtinBoolean()
const {
return b_Boolean; }
270 Object builtinString()
const {
return b_String; }
271 Object builtinNumber()
const {
return b_Number; }
272 Object builtinDate()
const {
return b_Date; }
273 Object builtinRegExp()
const {
return b_RegExp; }
274 Object builtinError()
const {
return b_Error; }
276 Object builtinObjectPrototype()
const {
return b_ObjectPrototype; }
277 Object builtinFunctionPrototype()
const {
return b_FunctionPrototype; }
278 Object builtinArrayPrototype()
const {
return b_ArrayPrototype; }
279 Object builtinBooleanPrototype()
const {
return b_BooleanPrototype; }
280 Object builtinStringPrototype()
const {
return b_StringPrototype; }
281 Object builtinNumberPrototype()
const {
return b_NumberPrototype; }
282 Object builtinDatePrototype()
const {
return b_DatePrototype; }
283 Object builtinRegExpPrototype()
const {
return b_RegExpPrototype; }
284 Object builtinErrorPrototype()
const {
return b_ErrorPrototype; }
286 Object builtinEvalError()
const {
return b_evalError; }
287 Object builtinRangeError()
const {
return b_rangeError; }
288 Object builtinReferenceError()
const {
return b_referenceError; }
289 Object builtinSyntaxError()
const {
return b_syntaxError; }
290 Object builtinTypeError()
const {
return b_typeError; }
291 Object builtinURIError()
const {
return b_uriError; }
293 Object builtinEvalErrorPrototype()
const {
return b_evalErrorPrototype; }
294 Object builtinRangeErrorPrototype()
const {
return b_rangeErrorPrototype; }
295 Object builtinReferenceErrorPrototype()
const {
return b_referenceErrorPrototype; }
296 Object builtinSyntaxErrorPrototype()
const {
return b_syntaxErrorPrototype; }
297 Object builtinTypeErrorPrototype()
const {
return b_typeErrorPrototype; }
298 Object builtinURIErrorPrototype()
const {
return b_uriErrorPrototype; }
300 void setCompatMode(Interpreter::CompatMode mode) { m_compatMode = mode; }
301 Interpreter::CompatMode compatMode()
const {
return m_compatMode; }
304 static InterpreterImp* firstInterpreter() {
return s_hook; }
305 InterpreterImp *nextInterpreter()
const {
return next; }
306 InterpreterImp *prevInterpreter()
const {
return prev; }
308 void addSourceCode(SourceCode *code);
309 void removeSourceCode(SourceCode *code);
311 void setContext(ContextImp *c) { _context = c; }
315 Interpreter *m_interpreter;
333 Object b_ObjectPrototype;
334 Object b_FunctionPrototype;
335 Object b_ArrayPrototype;
336 Object b_BooleanPrototype;
337 Object b_StringPrototype;
338 Object b_NumberPrototype;
339 Object b_DatePrototype;
340 Object b_RegExpPrototype;
341 Object b_ErrorPrototype;
345 Object b_referenceError;
346 Object b_syntaxError;
350 Object b_evalErrorPrototype;
351 Object b_rangeErrorPrototype;
352 Object b_referenceErrorPrototype;
353 Object b_syntaxErrorPrototype;
354 Object b_typeErrorPrototype;
355 Object b_uriErrorPrototype;
358 Interpreter::CompatMode m_compatMode;
361 static InterpreterImp* s_hook;
362 InterpreterImp *
next, *prev;
364 ContextImp *_context;
370 class AttachedInterpreter;
379 void abort() { isAborted =
true; }
380 bool aborted()
const {
return isAborted; }
382 AttachedInterpreter *interps;
390 friend class ActivationImp;
400 virtual bool implementsCall()
const;
404 Identifier parameterProperty(
int index)
const;
406 UString parameterString()
const;
407 virtual CodeType codeType()
const = 0;
410 int firstLine()
const {
return line0; }
411 int lastLine()
const {
return line1; }
412 int sourceId()
const {
return sid; }
414 virtual const ClassInfo *classInfo()
const {
return &info; }
424 virtual void processVarDecls(
ExecState *exec);
431 ~DeclaredFunctionImp();
433 bool implementsConstruct()
const;
437 CodeType codeType()
const {
return FunctionCode; }
438 FunctionBodyNode *body;
440 virtual const ClassInfo *classInfo()
const {
return &info; }
441 KJS_EXPORT
static const ClassInfo info;
443 virtual void processVarDecls(ExecState *exec);
448 class ArgumentsImp :
public ObjectImp {
450 ArgumentsImp(ExecState *exec, FunctionImp *func,
const List &args, ActivationImp *act);
454 virtual Value get(ExecState *exec,
const Identifier &propertyName)
const;
455 virtual void put(ExecState *exec,
const Identifier &propertyName,
456 const Value &value,
int attr = None);
458 virtual const ClassInfo *classInfo()
const {
return &info; }
459 static const ClassInfo info;
462 ActivationImp *activation;
465 class ActivationImp :
public ObjectImp {
467 ActivationImp(FunctionImp *
function,
const List &arguments);
469 virtual Value get(ExecState *exec,
const Identifier &propertyName)
const;
470 virtual bool hasProperty(ExecState *exec,
const Identifier &propertyName)
const;
471 virtual bool deleteProperty(ExecState *exec,
const Identifier &propertyName);
473 virtual const ClassInfo *classInfo()
const {
return &info; }
474 static const ClassInfo info;
479 FunctionImp *_function;
481 mutable ArgumentsImp *_argumentsObject;
484 class GlobalFuncImp :
public InternalFunctionImp {
486 GlobalFuncImp(ExecState *exec, FunctionPrototypeImp *funcProto,
487 int i,
int len,
const Identifier &_ident);
488 virtual bool implementsCall()
const;
489 virtual Value call(ExecState *exec, Object &thisObj,
const List &args);
490 virtual CodeType codeType()
const;
491 enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, DecodeURI, DecodeURIComponent,
492 EncodeURI, EncodeURIComponent, Escape, UnEscape, KJSPrint };
498 double roundValue(ExecState *exec,
const Value &v);
501 void printInfo(ExecState *exec,
const char *s,
const Value &o,
int lineno = -1);
507 #endif // _INTERNAL_H_