1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
3 | * Copyright (C) 2003, 2008, 2016 Apple Inc. All rights reserved. |
4 | * |
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. |
9 | * |
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. |
14 | * |
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 |
18 | * |
19 | */ |
20 | |
21 | #include "config.h" |
22 | #include "ErrorPrototype.h" |
23 | |
24 | #include "Error.h" |
25 | #include "JSFunction.h" |
26 | #include "JSStringInlines.h" |
27 | #include "ObjectPrototype.h" |
28 | #include "JSCInlines.h" |
29 | #include "StringRecursionChecker.h" |
30 | |
31 | namespace JSC { |
32 | |
33 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ErrorPrototype); |
34 | |
35 | static EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState*); |
36 | |
37 | } |
38 | |
39 | #include "ErrorPrototype.lut.h" |
40 | |
41 | namespace JSC { |
42 | |
43 | const ClassInfo ErrorPrototype::s_info = { "Object" , &Base::s_info, &errorPrototypeTable, nullptr, CREATE_METHOD_TABLE(ErrorPrototype) }; |
44 | |
45 | /* Source for ErrorPrototype.lut.h |
46 | @begin errorPrototypeTable |
47 | toString errorProtoFuncToString DontEnum|Function 0 |
48 | @end |
49 | */ |
50 | |
51 | ErrorPrototype::ErrorPrototype(VM& vm, Structure* structure) |
52 | : JSNonFinalObject(vm, structure) |
53 | { |
54 | } |
55 | |
56 | ErrorPrototype* ErrorPrototype::create(VM& vm, JSGlobalObject*, Structure* structure) |
57 | { |
58 | ErrorPrototype* prototype = new (NotNull, allocateCell<ErrorPrototype>(vm.heap)) ErrorPrototype(vm, structure); |
59 | prototype->finishCreation(vm, "Error"_s ); |
60 | return prototype; |
61 | } |
62 | |
63 | void ErrorPrototype::finishCreation(VM& vm, const String& name) |
64 | { |
65 | Base::finishCreation(vm); |
66 | ASSERT(inherits(vm, info())); |
67 | putDirectWithoutTransition(vm, vm.propertyNames->name, jsString(&vm, name), static_cast<unsigned>(PropertyAttribute::DontEnum)); |
68 | putDirectWithoutTransition(vm, vm.propertyNames->message, jsEmptyString(&vm), static_cast<unsigned>(PropertyAttribute::DontEnum)); |
69 | } |
70 | |
71 | // ------------------------------ Functions --------------------------- |
72 | |
73 | // ECMA-262 5.1, 15.11.4.4 |
74 | EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState* exec) |
75 | { |
76 | VM& vm = exec->vm(); |
77 | auto scope = DECLARE_THROW_SCOPE(vm); |
78 | |
79 | // 1. Let O be the this value. |
80 | JSValue thisValue = exec->thisValue(); |
81 | |
82 | // 2. If Type(O) is not Object, throw a TypeError exception. |
83 | if (!thisValue.isObject()) |
84 | return throwVMTypeError(exec, scope); |
85 | JSObject* thisObj = asObject(thisValue); |
86 | |
87 | // Guard against recursion! |
88 | StringRecursionChecker checker(exec, thisObj); |
89 | EXCEPTION_ASSERT(!scope.exception() || checker.earlyReturnValue()); |
90 | if (JSValue earlyReturnValue = checker.earlyReturnValue()) |
91 | return JSValue::encode(earlyReturnValue); |
92 | |
93 | // 3. Let name be the result of calling the [[Get]] internal method of O with argument "name". |
94 | JSValue name = thisObj->get(exec, vm.propertyNames->name); |
95 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
96 | |
97 | // 4. If name is undefined, then let name be "Error"; else let name be ToString(name). |
98 | String nameString; |
99 | if (name.isUndefined()) |
100 | nameString = "Error"_s ; |
101 | else { |
102 | nameString = name.toWTFString(exec); |
103 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
104 | } |
105 | |
106 | // 5. Let msg be the result of calling the [[Get]] internal method of O with argument "message". |
107 | JSValue message = thisObj->get(exec, vm.propertyNames->message); |
108 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
109 | |
110 | // (sic) |
111 | // 6. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg). |
112 | // 7. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg). |
113 | String messageString; |
114 | if (message.isUndefined()) |
115 | messageString = String(); |
116 | else { |
117 | messageString = message.toWTFString(exec); |
118 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
119 | } |
120 | |
121 | // 8. If name is the empty String, return msg. |
122 | if (!nameString.length()) |
123 | return JSValue::encode(message.isString() ? message : jsString(exec, messageString)); |
124 | |
125 | // 9. If msg is the empty String, return name. |
126 | if (!messageString.length()) |
127 | return JSValue::encode(name.isString() ? name : jsString(exec, nameString)); |
128 | |
129 | // 10. Return the result of concatenating name, ":", a single space character, and msg. |
130 | RELEASE_AND_RETURN(scope, JSValue::encode(jsMakeNontrivialString(exec, nameString, ": " , messageString))); |
131 | } |
132 | |
133 | } // namespace JSC |
134 | |