| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/builtins/builtins.h" | 5 #include "src/builtins/builtins.h" |
| 6 #include "src/builtins/builtins-utils.h" | 6 #include "src/builtins/builtins-utils.h" |
| 7 | 7 |
| 8 #include "src/bootstrapper.h" | |
| 9 #include "src/messages.h" | 8 #include "src/messages.h" |
| 10 #include "src/property-descriptor.h" | 9 #include "src/property-descriptor.h" |
| 11 #include "src/string-builder.h" | 10 #include "src/string-builder.h" |
| 12 | 11 |
| 13 namespace v8 { | 12 namespace v8 { |
| 14 namespace internal { | 13 namespace internal { |
| 15 | 14 |
| 16 // ES6 section 19.5.1.1 Error ( message ) | 15 // ES6 section 19.5.1.1 Error ( message ) |
| 17 BUILTIN(ErrorConstructor) { | 16 BUILTIN(ErrorConstructor) { |
| 18 HandleScope scope(isolate); | 17 HandleScope scope(isolate); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 desc.set_writable(true); | 55 desc.set_writable(true); |
| 57 desc.set_value(formatted_stack_trace); | 56 desc.set_value(formatted_stack_trace); |
| 58 Maybe<bool> status = JSReceiver::DefineOwnProperty( | 57 Maybe<bool> status = JSReceiver::DefineOwnProperty( |
| 59 isolate, object, isolate->factory()->stack_string(), &desc, | 58 isolate, object, isolate->factory()->stack_string(), &desc, |
| 60 Object::THROW_ON_ERROR); | 59 Object::THROW_ON_ERROR); |
| 61 if (!status.IsJust()) return isolate->heap()->exception(); | 60 if (!status.IsJust()) return isolate->heap()->exception(); |
| 62 CHECK(status.FromJust()); | 61 CHECK(status.FromJust()); |
| 63 return isolate->heap()->undefined_value(); | 62 return isolate->heap()->undefined_value(); |
| 64 } | 63 } |
| 65 | 64 |
| 66 namespace { | |
| 67 | |
| 68 MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate, | |
| 69 Handle<JSReceiver> recv, | |
| 70 Handle<String> key, | |
| 71 Handle<String> default_str) { | |
| 72 Handle<Object> obj; | |
| 73 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, JSObject::GetProperty(recv, key), | |
| 74 String); | |
| 75 | |
| 76 Handle<String> str; | |
| 77 if (obj->IsUndefined(isolate)) { | |
| 78 str = default_str; | |
| 79 } else { | |
| 80 ASSIGN_RETURN_ON_EXCEPTION(isolate, str, Object::ToString(isolate, obj), | |
| 81 String); | |
| 82 } | |
| 83 | |
| 84 return str; | |
| 85 } | |
| 86 | |
| 87 } // namespace | |
| 88 | |
| 89 // ES6 section 19.5.3.4 Error.prototype.toString ( ) | 65 // ES6 section 19.5.3.4 Error.prototype.toString ( ) |
| 90 BUILTIN(ErrorPrototypeToString) { | 66 BUILTIN(ErrorPrototypeToString) { |
| 91 HandleScope scope(isolate); | 67 HandleScope scope(isolate); |
| 92 | 68 RETURN_RESULT_OR_FAILURE(isolate, ErrorToString(isolate, args.receiver())); |
| 93 // 1. Let O be the this value. | |
| 94 // 2. If Type(O) is not Object, throw a TypeError exception. | |
| 95 CHECK_RECEIVER(JSReceiver, receiver, "Error.prototype.toString"); | |
| 96 | |
| 97 // 3. Let name be ? Get(O, "name"). | |
| 98 // 4. If name is undefined, let name be "Error"; otherwise let name be | |
| 99 // ? ToString(name). | |
| 100 Handle<String> name_key = isolate->factory()->name_string(); | |
| 101 Handle<String> name_default = isolate->factory()->Error_string(); | |
| 102 Handle<String> name; | |
| 103 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 104 isolate, name, | |
| 105 GetStringPropertyOrDefault(isolate, receiver, name_key, name_default)); | |
| 106 | |
| 107 // 5. Let msg be ? Get(O, "message"). | |
| 108 // 6. If msg is undefined, let msg be the empty String; otherwise let msg be | |
| 109 // ? ToString(msg). | |
| 110 Handle<String> msg_key = isolate->factory()->message_string(); | |
| 111 Handle<String> msg_default = isolate->factory()->empty_string(); | |
| 112 Handle<String> msg; | |
| 113 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 114 isolate, msg, | |
| 115 GetStringPropertyOrDefault(isolate, receiver, msg_key, msg_default)); | |
| 116 | |
| 117 // 7. If name is the empty String, return msg. | |
| 118 // 8. If msg is the empty String, return name. | |
| 119 if (name->length() == 0) return *msg; | |
| 120 if (msg->length() == 0) return *name; | |
| 121 | |
| 122 // 9. Return the result of concatenating name, the code unit 0x003A (COLON), | |
| 123 // the code unit 0x0020 (SPACE), and msg. | |
| 124 IncrementalStringBuilder builder(isolate); | |
| 125 builder.AppendString(name); | |
| 126 builder.AppendCString(": "); | |
| 127 builder.AppendString(msg); | |
| 128 RETURN_RESULT_OR_FAILURE(isolate, builder.Finish()); | |
| 129 } | 69 } |
| 130 | 70 |
| 131 } // namespace internal | 71 } // namespace internal |
| 132 } // namespace v8 | 72 } // namespace v8 |
| OLD | NEW |