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/accessors.h" | |
9 #include "src/bootstrapper.h" | 8 #include "src/bootstrapper.h" |
10 #include "src/messages.h" | 9 #include "src/messages.h" |
11 #include "src/property-descriptor.h" | 10 #include "src/property-descriptor.h" |
12 #include "src/string-builder.h" | 11 #include "src/string-builder.h" |
13 | 12 |
14 namespace v8 { | 13 namespace v8 { |
15 namespace internal { | 14 namespace internal { |
16 | 15 |
17 // ES6 section 19.5.1.1 Error ( message ) | 16 // ES6 section 19.5.1.1 Error ( message ) |
18 BUILTIN(ErrorConstructor) { | 17 BUILTIN(ErrorConstructor) { |
(...skipping 10 matching lines...) Expand all Loading... |
29 HandleScope scope(isolate); | 28 HandleScope scope(isolate); |
30 Handle<Object> object_obj = args.atOrUndefined(isolate, 1); | 29 Handle<Object> object_obj = args.atOrUndefined(isolate, 1); |
31 if (!object_obj->IsJSObject()) { | 30 if (!object_obj->IsJSObject()) { |
32 THROW_NEW_ERROR_RETURN_FAILURE( | 31 THROW_NEW_ERROR_RETURN_FAILURE( |
33 isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj)); | 32 isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj)); |
34 } | 33 } |
35 Handle<JSObject> object = Handle<JSObject>::cast(object_obj); | 34 Handle<JSObject> object = Handle<JSObject>::cast(object_obj); |
36 Handle<Object> caller = args.atOrUndefined(isolate, 2); | 35 Handle<Object> caller = args.atOrUndefined(isolate, 2); |
37 FrameSkipMode mode = caller->IsJSFunction() ? SKIP_UNTIL_SEEN : SKIP_NONE; | 36 FrameSkipMode mode = caller->IsJSFunction() ? SKIP_UNTIL_SEEN : SKIP_NONE; |
38 | 37 |
39 // TODO(jgruber): Eagerly format the stack trace and remove accessors.h | |
40 // include. | |
41 | |
42 // Handle writes to the global object. | |
43 | |
44 if (object->IsJSGlobalProxy()) { | |
45 Map* map = object->map(); | |
46 if (map->has_hidden_prototype()) { | |
47 object = handle(JSGlobalObject::cast(map->prototype()), isolate); | |
48 } | |
49 } | |
50 | |
51 // Check if the stack property is read-only. | |
52 | |
53 bool is_extensible = true; | |
54 if (!JSObject::IsExtensible(object)) { | |
55 is_extensible = false; | |
56 } | |
57 | |
58 PropertyDescriptor desc; | |
59 Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor( | |
60 isolate, object, isolate->factory()->stack_string(), &desc); | |
61 if (owned.FromMaybe(false)) { | |
62 if (!desc.configurable() || !desc.writable()) { | |
63 is_extensible = false; | |
64 } | |
65 } | |
66 | |
67 if (!is_extensible) { | |
68 THROW_NEW_ERROR_RETURN_FAILURE( | |
69 isolate, NewTypeError(MessageTemplate::kDefineDisallowed, | |
70 isolate->factory()->stack_string(), object)); | |
71 } | |
72 | |
73 // Add stack accessors to the given object | |
74 | |
75 Handle<Map> map(object->map()); | |
76 PropertyAttributes attribs = DONT_ENUM; | |
77 Handle<AccessorInfo> error_stack = | |
78 Accessors::ErrorStackInfo(isolate, attribs); | |
79 { | |
80 AccessorConstantDescriptor d(Handle<Name>(Name::cast(error_stack->name())), | |
81 error_stack, attribs); | |
82 Handle<DescriptorArray> old_descriptors(map->instance_descriptors()); | |
83 int index = old_descriptors->SearchWithCache(isolate, *d.GetKey(), *map); | |
84 if (index == DescriptorArray::kNotFound) { | |
85 // TODO(jgruber): This ensures we do not crash when CaptureStackTrace is | |
86 // called on an object with an existing "stack" property. This will be | |
87 // removed as soon as we move to eager trace formatting. | |
88 Handle<Map> new_map = | |
89 Map::CopyInsertDescriptor(map, &d, INSERT_TRANSITION); | |
90 JSObject::MigrateToMap(object, new_map, 1); | |
91 } | |
92 } | |
93 | |
94 // Collect the stack trace. | 38 // Collect the stack trace. |
95 | 39 |
96 RETURN_FAILURE_ON_EXCEPTION(isolate, | 40 RETURN_FAILURE_ON_EXCEPTION(isolate, |
97 isolate->CaptureAndSetDetailedStackTrace(object)); | 41 isolate->CaptureAndSetDetailedStackTrace(object)); |
| 42 |
| 43 // Eagerly format the stack trace and set the stack property. |
| 44 |
| 45 Handle<Object> stack_trace = |
| 46 isolate->CaptureSimpleStackTrace(object, mode, caller); |
| 47 |
| 48 Handle<Object> formatted_stack_trace; |
| 49 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 50 isolate, formatted_stack_trace, |
| 51 FormatStackTrace(isolate, object, stack_trace)); |
| 52 |
98 RETURN_FAILURE_ON_EXCEPTION( | 53 RETURN_FAILURE_ON_EXCEPTION( |
99 isolate, isolate->CaptureAndSetSimpleStackTrace(object, mode, caller)); | 54 isolate, JSObject::SetProperty(object, isolate->factory()->stack_string(), |
| 55 formatted_stack_trace, STRICT)); |
100 | 56 |
101 return *isolate->factory()->undefined_value(); | 57 return *isolate->factory()->undefined_value(); |
102 } | 58 } |
103 | 59 |
104 namespace { | 60 namespace { |
105 | 61 |
106 MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate, | 62 MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate, |
107 Handle<JSReceiver> recv, | 63 Handle<JSReceiver> recv, |
108 Handle<String> key, | 64 Handle<String> key, |
109 Handle<String> default_str) { | 65 Handle<String> default_str) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 // the code unit 0x0020 (SPACE), and msg. | 117 // the code unit 0x0020 (SPACE), and msg. |
162 IncrementalStringBuilder builder(isolate); | 118 IncrementalStringBuilder builder(isolate); |
163 builder.AppendString(name); | 119 builder.AppendString(name); |
164 builder.AppendCString(": "); | 120 builder.AppendCString(": "); |
165 builder.AppendString(msg); | 121 builder.AppendString(msg); |
166 RETURN_RESULT_OR_FAILURE(isolate, builder.Finish()); | 122 RETURN_RESULT_OR_FAILURE(isolate, builder.Finish()); |
167 } | 123 } |
168 | 124 |
169 } // namespace internal | 125 } // namespace internal |
170 } // namespace v8 | 126 } // namespace v8 |
OLD | NEW |