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" | 8 #include "src/accessors.h" |
9 #include "src/bootstrapper.h" | 9 #include "src/bootstrapper.h" |
| 10 #include "src/messages.h" |
10 #include "src/property-descriptor.h" | 11 #include "src/property-descriptor.h" |
11 #include "src/string-builder.h" | 12 #include "src/string-builder.h" |
12 | 13 |
13 namespace v8 { | 14 namespace v8 { |
14 namespace internal { | 15 namespace internal { |
15 | 16 |
16 // ES6 section 19.5.1.1 Error ( message ) | 17 // ES6 section 19.5.1.1 Error ( message ) |
17 BUILTIN(ErrorConstructor) { | 18 BUILTIN(ErrorConstructor) { |
18 HandleScope scope(isolate); | 19 HandleScope scope(isolate); |
19 | 20 RETURN_RESULT_OR_FAILURE( |
20 // 1. If NewTarget is undefined, let newTarget be the active function object, | 21 isolate, ConstructError(isolate, args.target<JSFunction>(), |
21 // else let newTarget be NewTarget. | 22 Handle<Object>::cast(args.new_target()), |
22 | 23 args.atOrUndefined(isolate, 1), SKIP_FIRST)); |
23 Handle<JSFunction> target = args.target<JSFunction>(); | |
24 Handle<JSReceiver> new_target; | |
25 if (args.new_target()->IsJSReceiver()) { | |
26 new_target = Handle<JSReceiver>::cast(args.new_target()); | |
27 } else { | |
28 new_target = target; | |
29 } | |
30 | |
31 // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%ErrorPrototype%", | |
32 // « [[ErrorData]] »). | |
33 Handle<JSObject> err; | |
34 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, err, | |
35 JSObject::New(target, new_target)); | |
36 | |
37 // 3. If message is not undefined, then | |
38 // a. Let msg be ? ToString(message). | |
39 // b. Let msgDesc be the PropertyDescriptor{[[Value]]: msg, [[Writable]]: | |
40 // true, [[Enumerable]]: false, [[Configurable]]: true}. | |
41 // c. Perform ! DefinePropertyOrThrow(O, "message", msgDesc). | |
42 // 4. Return O. | |
43 | |
44 Handle<Object> msg = args.atOrUndefined(isolate, 1); | |
45 if (!msg->IsUndefined(isolate)) { | |
46 Handle<String> msg_string; | |
47 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, msg_string, | |
48 Object::ToString(isolate, msg)); | |
49 RETURN_FAILURE_ON_EXCEPTION( | |
50 isolate, | |
51 JSObject::SetOwnPropertyIgnoreAttributes( | |
52 err, isolate->factory()->message_string(), msg_string, DONT_ENUM)); | |
53 } | |
54 | |
55 // Capture the stack trace unless we're setting up. | |
56 if (!isolate->bootstrapper()->IsActive()) { | |
57 // Optionally capture a more detailed stack trace for the message. | |
58 RETURN_FAILURE_ON_EXCEPTION(isolate, | |
59 isolate->CaptureAndSetDetailedStackTrace(err)); | |
60 // Capture a simple stack trace for the stack property. | |
61 RETURN_FAILURE_ON_EXCEPTION(isolate, | |
62 isolate->CaptureAndSetSimpleStackTrace(err)); | |
63 } | |
64 | |
65 return *err; | |
66 } | 24 } |
67 | 25 |
68 // static | 26 // static |
69 BUILTIN(ErrorCaptureStackTrace) { | 27 BUILTIN(ErrorCaptureStackTrace) { |
70 HandleScope scope(isolate); | 28 HandleScope scope(isolate); |
71 Handle<Object> object_obj = args.atOrUndefined(isolate, 1); | 29 Handle<Object> object_obj = args.atOrUndefined(isolate, 1); |
72 if (!object_obj->IsJSObject()) { | 30 if (!object_obj->IsJSObject()) { |
73 THROW_NEW_ERROR_RETURN_FAILURE( | 31 THROW_NEW_ERROR_RETURN_FAILURE( |
74 isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj)); | 32 isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj)); |
75 } | 33 } |
76 Handle<JSObject> object = Handle<JSObject>::cast(object_obj); | 34 Handle<JSObject> object = Handle<JSObject>::cast(object_obj); |
77 Handle<Object> caller = args.atOrUndefined(isolate, 2); | 35 Handle<Object> caller = args.atOrUndefined(isolate, 2); |
| 36 FrameSkipMode mode = caller->IsJSFunction() ? SKIP_UNTIL_SEEN : SKIP_NONE; |
78 | 37 |
79 // TODO(jgruber): Eagerly format the stack trace and remove accessors.h | 38 // TODO(jgruber): Eagerly format the stack trace and remove accessors.h |
80 // include. | 39 // include. |
81 | 40 |
82 // Handle writes to the global object. | 41 // Handle writes to the global object. |
83 | 42 |
84 if (object->IsJSGlobalProxy()) { | 43 if (object->IsJSGlobalProxy()) { |
85 Map* map = object->map(); | 44 Map* map = object->map(); |
86 if (map->has_hidden_prototype()) { | 45 if (map->has_hidden_prototype()) { |
87 object = handle(JSGlobalObject::cast(map->prototype()), isolate); | 46 object = handle(JSGlobalObject::cast(map->prototype()), isolate); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 Map::CopyInsertDescriptor(map, &d, INSERT_TRANSITION); | 88 Map::CopyInsertDescriptor(map, &d, INSERT_TRANSITION); |
130 JSObject::MigrateToMap(object, new_map, 1); | 89 JSObject::MigrateToMap(object, new_map, 1); |
131 } | 90 } |
132 } | 91 } |
133 | 92 |
134 // Collect the stack trace. | 93 // Collect the stack trace. |
135 | 94 |
136 RETURN_FAILURE_ON_EXCEPTION(isolate, | 95 RETURN_FAILURE_ON_EXCEPTION(isolate, |
137 isolate->CaptureAndSetDetailedStackTrace(object)); | 96 isolate->CaptureAndSetDetailedStackTrace(object)); |
138 RETURN_FAILURE_ON_EXCEPTION( | 97 RETURN_FAILURE_ON_EXCEPTION( |
139 isolate, isolate->CaptureAndSetSimpleStackTrace(object, caller)); | 98 isolate, isolate->CaptureAndSetSimpleStackTrace(object, mode, caller)); |
140 | 99 |
141 return *isolate->factory()->undefined_value(); | 100 return *isolate->factory()->undefined_value(); |
142 } | 101 } |
143 | 102 |
144 namespace { | 103 namespace { |
145 | 104 |
146 MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate, | 105 MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate, |
147 Handle<JSReceiver> recv, | 106 Handle<JSReceiver> recv, |
148 Handle<String> key, | 107 Handle<String> key, |
149 Handle<String> default_str) { | 108 Handle<String> default_str) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 // the code unit 0x0020 (SPACE), and msg. | 160 // the code unit 0x0020 (SPACE), and msg. |
202 IncrementalStringBuilder builder(isolate); | 161 IncrementalStringBuilder builder(isolate); |
203 builder.AppendString(name); | 162 builder.AppendString(name); |
204 builder.AppendCString(": "); | 163 builder.AppendCString(": "); |
205 builder.AppendString(msg); | 164 builder.AppendString(msg); |
206 RETURN_RESULT_OR_FAILURE(isolate, builder.Finish()); | 165 RETURN_RESULT_OR_FAILURE(isolate, builder.Finish()); |
207 } | 166 } |
208 | 167 |
209 } // namespace internal | 168 } // namespace internal |
210 } // namespace v8 | 169 } // namespace v8 |
OLD | NEW |