forked from phoboslab/JavaScriptCore-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorPrototype.cpp
More file actions
131 lines (106 loc) · 4.61 KB
/
ErrorPrototype.cpp
File metadata and controls
131 lines (106 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (C) 1999-2000 Harri Porten ([email protected])
* Copyright (C) 2003, 2008 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "ErrorPrototype.h"
#include "Error.h"
#include "JSFunction.h"
#include "JSString.h"
#include "JSStringBuilder.h"
#include "ObjectPrototype.h"
#include "Operations.h"
#include "StringRecursionChecker.h"
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ErrorPrototype);
static EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState*);
}
#include "ErrorPrototype.lut.h"
namespace JSC {
const ClassInfo ErrorPrototype::s_info = { "Error", &ErrorInstance::s_info, 0, ExecState::errorPrototypeTable, CREATE_METHOD_TABLE(ErrorPrototype) };
/* Source for ErrorPrototype.lut.h
@begin errorPrototypeTable
toString errorProtoFuncToString DontEnum|Function 0
@end
*/
ErrorPrototype::ErrorPrototype(VM& vm, Structure* structure)
: ErrorInstance(vm, structure)
{
}
void ErrorPrototype::finishCreation(VM& vm, JSGlobalObject*)
{
Base::finishCreation(vm, "");
ASSERT(inherits(info()));
putDirect(vm, vm.propertyNames->name, jsNontrivialString(&vm, String(ASCIILiteral("Error"))), DontEnum);
}
bool ErrorPrototype::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
{
return getStaticFunctionSlot<ErrorInstance>(exec, ExecState::errorPrototypeTable(exec), jsCast<ErrorPrototype*>(object), propertyName, slot);
}
// ------------------------------ Functions ---------------------------
// ECMA-262 5.1, 15.11.4.4
EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState* exec)
{
// 1. Let O be the this value.
JSValue thisValue = exec->hostThisValue();
// 2. If Type(O) is not Object, throw a TypeError exception.
if (!thisValue.isObject())
return throwVMTypeError(exec);
JSObject* thisObj = asObject(thisValue);
// Guard against recursion!
StringRecursionChecker checker(exec, thisObj);
if (JSValue earlyReturnValue = checker.earlyReturnValue())
return JSValue::encode(earlyReturnValue);
// 3. Let name be the result of calling the [[Get]] internal method of O with argument "name".
JSValue name = thisObj->get(exec, exec->propertyNames().name);
if (exec->hadException())
return JSValue::encode(jsUndefined());
// 4. If name is undefined, then let name be "Error"; else let name be ToString(name).
String nameString;
if (name.isUndefined())
nameString = ASCIILiteral("Error");
else {
nameString = name.toString(exec)->value(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
}
// 5. Let msg be the result of calling the [[Get]] internal method of O with argument "message".
JSValue message = thisObj->get(exec, exec->propertyNames().message);
if (exec->hadException())
return JSValue::encode(jsUndefined());
// (sic)
// 6. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg).
// 7. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg).
String messageString;
if (message.isUndefined())
messageString = String();
else {
messageString = message.toString(exec)->value(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
}
// 8. If name is the empty String, return msg.
if (!nameString.length())
return JSValue::encode(message.isString() ? message : jsString(exec, messageString));
// 9. If msg is the empty String, return name.
if (!messageString.length())
return JSValue::encode(name.isString() ? name : jsNontrivialString(exec, nameString));
// 10. Return the result of concatenating name, ":", a single space character, and msg.
return JSValue::encode(jsMakeNontrivialString(exec, nameString, ": ", messageString));
}
} // namespace JSC