7#include <QtCore/qnumeric.h>
8#include <QtCore/qmath.h>
9#include <QtCore/QDateTime>
10#include <QtCore/QStringList>
11#include <QtCore/QDebug>
13#include <private/qv4mm_p.h>
14#include <private/qv4codegen_p.h>
21# include "qplatformdefs.h"
24# include <qt_windows.h>
29void Heap::ErrorObject::init()
34 Scope scope(internalClass->engine);
35 Scoped<QV4::ErrorObject> e(scope,
this);
37 if (internalClass == scope.engine->internalClasses(EngineBase::Class_ErrorProto))
40 setProperty(scope.engine, QV4::ErrorObject::Index_Stack, scope.engine->getStackFunction()->d());
41 setProperty(scope.engine, QV4::ErrorObject::Index_StackSetter, Value::undefinedValue());
42 setProperty(scope.engine, QV4::ErrorObject::Index_FileName, Value::undefinedValue());
43 setProperty(scope.engine, QV4::ErrorObject::Index_LineNumber, Value::undefinedValue());
46void Heap::ErrorObject::init(
const Value &message, ErrorType t)
51 Scope scope(internalClass->engine);
52 Scoped<QV4::ErrorObject> e(scope,
this);
54 setProperty(scope.engine, QV4::ErrorObject::Index_Stack, scope.engine->getStackFunction()->d());
55 setProperty(scope.engine, QV4::ErrorObject::Index_StackSetter, Value::undefinedValue());
57 e->d()->stackTrace =
new StackTrace(scope.engine->stackTrace());
58 if (!e->d()->stackTrace->isEmpty()) {
59 setProperty(scope.engine, QV4::ErrorObject::Index_FileName, scope.engine->newString(e->d()->stackTrace->at(0).source));
60 setProperty(scope.engine, QV4::ErrorObject::Index_LineNumber, Value::fromInt32(qAbs(e->d()->stackTrace->at(0).line)));
63 if (!message.isUndefined())
64 setProperty(scope.engine, QV4::ErrorObject::Index_Message, message);
67void Heap::ErrorObject::init(
const Value &message,
const QString &fileName,
int line,
int column, ErrorObject::ErrorType t)
73 Scope scope(internalClass->engine);
74 Scoped<QV4::ErrorObject> e(scope,
this);
76 setProperty(scope.engine, QV4::ErrorObject::Index_Stack, scope.engine->getStackFunction()->d());
77 setProperty(scope.engine, QV4::ErrorObject::Index_StackSetter, Value::undefinedValue());
79 e->d()->stackTrace =
new StackTrace(scope.engine->stackTrace());
81 frame.source = fileName;
83 frame.column = column;
84 e->d()->stackTrace->prepend(frame);
86 Q_ASSERT(!e->d()->stackTrace->isEmpty());
87 setProperty(scope.engine, QV4::ErrorObject::Index_FileName, scope.engine->newString(e->d()->stackTrace->at(0).source));
88 setProperty(scope.engine, QV4::ErrorObject::Index_LineNumber, Value::fromInt32(qAbs(e->d()->stackTrace->at(0).line)));
90 if (!message.isUndefined())
91 setProperty(scope.engine, QV4::ErrorObject::Index_Message, message);
94const char *
ErrorObject::className(Heap::ErrorObject::ErrorType t)
97 case Heap::ErrorObject::Error:
99 case Heap::ErrorObject::EvalError:
101 case Heap::ErrorObject::RangeError:
103 case Heap::ErrorObject::ReferenceError:
104 return "ReferenceError";
105 case Heap::ErrorObject::SyntaxError:
106 return "SyntaxError";
107 case Heap::ErrorObject::TypeError:
109 case Heap::ErrorObject::URIError:
115ReturnedValue
ErrorObject::method_get_stack(
const FunctionObject *b,
const Value *thisObject,
const Value *,
int)
117 ExecutionEngine *v4 = b->engine();
120 return v4->throwTypeError();
121 if (!This->d()->stack) {
123 for (
int i = 0; i < This->d()->stackTrace->size(); ++i) {
125 trace += QLatin1Char(
'\n');
126 const StackFrame &frame = This->d()->stackTrace->at(i);
127 trace += frame.function + QLatin1Char(
'@') + frame.source;
129 trace += QLatin1Char(
':') + QString::number(frame.line);
131 This->d()->stack.set(v4, v4->newString(trace));
133 return This->d()->stack->asReturnedValue();
138void Heap::SyntaxErrorObject::init(
const Value &msg)
140 Heap::ErrorObject::init(msg, SyntaxError);
143void Heap::SyntaxErrorObject::init(
const Value &msg,
const QString &fileName,
int lineNumber,
int columnNumber)
145 Heap::ErrorObject::init(msg, fileName, lineNumber, columnNumber, SyntaxError);
148void Heap::EvalErrorObject::init(
const Value &message)
150 Heap::ErrorObject::init(message, EvalError);
153void Heap::RangeErrorObject::init(
const Value &message)
155 Heap::ErrorObject::init(message, RangeError);
158void Heap::ReferenceErrorObject::init(
const Value &message)
160 Heap::ErrorObject::init(message, ReferenceError);
163void Heap::ReferenceErrorObject::init(
const Value &msg,
const QString &fileName,
int lineNumber,
int columnNumber)
165 Heap::ErrorObject::init(msg, fileName, lineNumber, columnNumber, ReferenceError);
168void Heap::TypeErrorObject::init(
const Value &message)
170 Heap::ErrorObject::init(message, TypeError);
173void Heap::URIErrorObject::init(
const Value &message)
175 Heap::ErrorObject::init(message, URIError);
186void Heap::ErrorCtor::init(QV4::ExecutionEngine *engine)
188 Heap::FunctionObject::init(engine, QStringLiteral(
"Error"));
191void Heap::ErrorCtor::init(QV4::ExecutionEngine *engine,
const QString &name)
193 Heap::FunctionObject::init(engine, name);
196ReturnedValue
ErrorCtor::virtualCallAsConstructor(
const FunctionObject *f,
const Value *argv,
int argc,
const Value *newTarget)
198 Value v = argc ? *argv : Value::undefinedValue();
202ReturnedValue
ErrorCtor::virtualCall(
const FunctionObject *f,
const Value *,
const Value *argv,
int argc)
204 return f->callAsConstructor(argv, argc);
207void Heap::EvalErrorCtor::init(QV4::ExecutionEngine *engine)
209 Heap::FunctionObject::init(engine, QStringLiteral(
"EvalError"));
212ReturnedValue
EvalErrorCtor::virtualCallAsConstructor(
const FunctionObject *f,
const Value *argv,
int argc,
const Value *newTarget)
214 Value v = argc ? *argv : Value::undefinedValue();
218void Heap::RangeErrorCtor::init(QV4::ExecutionEngine *engine)
220 Heap::FunctionObject::init(engine, QStringLiteral(
"RangeError"));
223ReturnedValue
RangeErrorCtor::virtualCallAsConstructor(
const FunctionObject *f,
const Value *argv,
int argc,
const Value *newTarget)
225 Value v = argc ? *argv : Value::undefinedValue();
229void Heap::ReferenceErrorCtor::init(QV4::ExecutionEngine *engine)
231 Heap::FunctionObject::init(engine, QStringLiteral(
"ReferenceError"));
234ReturnedValue
ReferenceErrorCtor::virtualCallAsConstructor(
const FunctionObject *f,
const Value *argv,
int argc,
const Value *newTarget)
236 Value v = argc ? *argv : Value::undefinedValue();
240void Heap::SyntaxErrorCtor::init(QV4::ExecutionEngine *engine)
242 Heap::FunctionObject::init(engine, QStringLiteral(
"SyntaxError"));
245ReturnedValue
SyntaxErrorCtor::virtualCallAsConstructor(
const FunctionObject *f,
const Value *argv,
int argc,
const Value *newTarget)
247 Value v = argc ? *argv : Value::undefinedValue();
251void Heap::TypeErrorCtor::init(QV4::ExecutionEngine *engine)
253 Heap::FunctionObject::init(engine, QStringLiteral(
"TypeError"));
256ReturnedValue
TypeErrorCtor::virtualCallAsConstructor(
const FunctionObject *f,
const Value *argv,
int argc,
const Value *newTarget)
258 Value v = argc ? *argv : Value::undefinedValue();
262void Heap::URIErrorCtor::init(QV4::ExecutionEngine *engine)
264 Heap::FunctionObject::init(engine, QStringLiteral(
"URIError"));
267ReturnedValue
URIErrorCtor::virtualCallAsConstructor(
const FunctionObject *f,
const Value *argv,
int argc,
const Value *newTarget)
269 Value v = argc ? *argv : Value::undefinedValue();
273void ErrorPrototype::init(ExecutionEngine *engine, Object *ctor, Object *obj, Heap::ErrorObject::ErrorType t)
278 ctor->defineReadonlyProperty(engine->id_prototype(), (o = obj));
279 ctor->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(1));
282 obj->setProperty(Index_Name, engine->newString(QString::fromLatin1(ErrorObject::className(t))));
283 obj->defineDefaultProperty(engine->id_toString(), method_toString, 0);
286ReturnedValue
ErrorPrototype::method_toString(
const FunctionObject *b,
const Value *thisObject,
const Value *,
int)
288 ExecutionEngine *v4 = b->engine();
289 const Object *o = thisObject->as<Object>();
291 return v4->throwTypeError();
294 ScopedValue name(scope, o->get(scope.engine->id_name()));
296 if (name
->isUndefined())
297 qname = QStringLiteral(
"Error");
299 qname = name
->toQString();
301 ScopedString s(scope, scope.engine->newString(QStringLiteral(
"message")));
304 if (!message
->isUndefined())
305 qmessage = message
->toQString();
308 if (qname.isEmpty()) {
310 }
else if (qmessage.isEmpty()) {
313 str = qname + QLatin1String(
": ") + qmessage;
316 return scope.engine->newString(str)->asReturnedValue();
Scoped< Object > ScopedObject
Scoped< String > ScopedString
DEFINE_OBJECT_VTABLE(EvalErrorCtor)
DEFINE_OBJECT_VTABLE(URIErrorCtor)
DEFINE_OBJECT_VTABLE(ErrorCtor)
DEFINE_OBJECT_VTABLE(SyntaxErrorCtor)
DEFINE_OBJECT_VTABLE(RangeErrorCtor)
DEFINE_OBJECT_VTABLE(TypeErrorCtor)
DEFINE_OBJECT_VTABLE(ErrorObject)
DEFINE_OBJECT_VTABLE(ReferenceErrorCtor)
Scope(ExecutionEngine *e)