]>
git.saurik.com Git - apple/javascriptcore.git/blob - kjs/error_object.cpp
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2008 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "error_object.h"
24 #include "JSGlobalObject.h"
26 #include "operations.h"
32 // ------------------------------ ErrorInstance ----------------------------
34 const ClassInfo
ErrorInstance::info
= { "Error", 0, 0 };
36 ErrorInstance::ErrorInstance(JSObject
* prototype
)
41 // ------------------------------ ErrorPrototype ----------------------------
44 ErrorPrototype::ErrorPrototype(ExecState
* exec
, ObjectPrototype
* objectPrototype
, FunctionPrototype
* functionPrototype
)
45 : ErrorInstance(objectPrototype
)
47 // The constructor will be added later in ErrorObjectImp's constructor
49 putDirect(exec
->propertyNames().name
, jsString("Error"), DontEnum
);
50 putDirect(exec
->propertyNames().message
, jsString("Unknown error"), DontEnum
);
52 putDirectFunction(new PrototypeFunction(exec
, functionPrototype
, 0, exec
->propertyNames().toString
, errorProtoFuncToString
), DontEnum
);
55 JSValue
* errorProtoFuncToString(ExecState
* exec
, JSObject
* thisObj
, const List
&)
59 JSValue
* v
= thisObj
->get(exec
, exec
->propertyNames().name
);
60 if (!v
->isUndefined())
61 s
= v
->toString(exec
);
63 v
= thisObj
->get(exec
, exec
->propertyNames().message
);
64 if (!v
->isUndefined())
65 // Mozilla compatible format
66 s
+= ": " + v
->toString(exec
);
71 // ------------------------------ ErrorObjectImp -------------------------------
73 ErrorObjectImp::ErrorObjectImp(ExecState
* exec
, FunctionPrototype
* funcProto
, ErrorPrototype
* errorProto
)
74 : InternalFunctionImp(funcProto
, errorProto
->classInfo()->className
)
76 // ECMA 15.11.3.1 Error.prototype
77 putDirect(exec
->propertyNames().prototype
, errorProto
, DontEnum
|DontDelete
|ReadOnly
);
78 putDirect(exec
->propertyNames().length
, jsNumber(1), DontDelete
|ReadOnly
|DontEnum
);
81 bool ErrorObjectImp::implementsConstruct() const
87 JSObject
* ErrorObjectImp::construct(ExecState
* exec
, const List
& args
)
89 JSObject
* proto
= static_cast<JSObject
*>(exec
->lexicalGlobalObject()->errorPrototype());
90 JSObject
* imp
= new ErrorInstance(proto
);
93 if (!args
[0]->isUndefined())
94 imp
->putDirect(exec
->propertyNames().message
, jsString(args
[0]->toString(exec
)));
100 JSValue
* ErrorObjectImp::callAsFunction(ExecState
* exec
, JSObject
* /*thisObj*/, const List
& args
)
102 // "Error()" gives the sames result as "new Error()"
103 return construct(exec
, args
);
106 // ------------------------------ NativeErrorPrototype ----------------------
108 NativeErrorPrototype::NativeErrorPrototype(ExecState
* exec
, ErrorPrototype
* errorProto
, const UString
& name
, const UString
& message
)
109 : JSObject(errorProto
)
111 putDirect(exec
->propertyNames().name
, jsString(name
), 0);
112 putDirect(exec
->propertyNames().message
, jsString(message
), 0);
115 // ------------------------------ NativeErrorImp -------------------------------
117 const ClassInfo
NativeErrorImp::info
= { "Function", &InternalFunctionImp::info
, 0 };
119 NativeErrorImp::NativeErrorImp(ExecState
* exec
, FunctionPrototype
* funcProto
, NativeErrorPrototype
* prot
)
120 : InternalFunctionImp(funcProto
, Identifier(prot
->getDirect(exec
->propertyNames().name
)->getString()))
123 putDirect(exec
->propertyNames().length
, jsNumber(1), DontDelete
|ReadOnly
|DontEnum
); // ECMA 15.11.7.5
124 putDirect(exec
->propertyNames().prototype
, proto
, DontDelete
|ReadOnly
|DontEnum
);
127 bool NativeErrorImp::implementsConstruct() const
132 JSObject
* NativeErrorImp::construct(ExecState
* exec
, const List
& args
)
134 JSObject
* imp
= new ErrorInstance(proto
);
136 if (!args
[0]->isUndefined())
137 imp
->putDirect(exec
->propertyNames().message
, jsString(args
[0]->toString(exec
)));
141 JSValue
* NativeErrorImp::callAsFunction(ExecState
* exec
, JSObject
*, const List
& args
)
143 return construct(exec
, args
);
146 void NativeErrorImp::mark()
149 if (proto
&& !proto
->marked())