Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include "src/builtins/builtins.h"
6 : #include "src/builtins/builtins-utils.h"
7 :
8 : #include "src/accessors.h"
9 : #include "src/counters.h"
10 : #include "src/messages.h"
11 : #include "src/objects-inl.h"
12 : #include "src/property-descriptor.h"
13 : #include "src/string-builder.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 : // ES6 section 19.5.1.1 Error ( message )
19 43050 : BUILTIN(ErrorConstructor) {
20 : HandleScope scope(isolate);
21 :
22 : FrameSkipMode mode = SKIP_FIRST;
23 : Handle<Object> caller;
24 :
25 : // When we're passed a JSFunction as new target, we can skip frames until that
26 : // specific function is seen instead of unconditionally skipping the first
27 : // frame.
28 14350 : if (args.new_target()->IsJSFunction()) {
29 : mode = SKIP_UNTIL_SEEN;
30 13852 : caller = args.new_target();
31 : }
32 :
33 43040 : RETURN_RESULT_OR_FAILURE(
34 : isolate, ErrorUtils::Construct(isolate, args.target(),
35 : Handle<Object>::cast(args.new_target()),
36 : args.atOrUndefined(isolate, 1), mode,
37 : caller, false));
38 : }
39 :
40 : // static
41 570 : BUILTIN(ErrorCaptureStackTrace) {
42 : HandleScope scope(isolate);
43 : Handle<Object> object_obj = args.atOrUndefined(isolate, 1);
44 :
45 190 : if (!object_obj->IsJSObject()) {
46 20 : THROW_NEW_ERROR_RETURN_FAILURE(
47 : isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj));
48 : }
49 :
50 : Handle<JSObject> object = Handle<JSObject>::cast(object_obj);
51 : Handle<Object> caller = args.atOrUndefined(isolate, 2);
52 180 : FrameSkipMode mode = caller->IsJSFunction() ? SKIP_UNTIL_SEEN : SKIP_FIRST;
53 :
54 : // Collect the stack trace.
55 :
56 360 : RETURN_FAILURE_ON_EXCEPTION(isolate,
57 : isolate->CaptureAndSetDetailedStackTrace(object));
58 360 : RETURN_FAILURE_ON_EXCEPTION(
59 : isolate, isolate->CaptureAndSetSimpleStackTrace(object, mode, caller));
60 :
61 : // Add the stack accessors.
62 :
63 : Handle<AccessorInfo> error_stack =
64 180 : Accessors::ErrorStackInfo(isolate, DONT_ENUM);
65 :
66 : // Explicitly check for frozen objects. Other access checks are performed by
67 : // the LookupIterator in SetAccessor below.
68 180 : if (!JSObject::IsExtensible(object)) {
69 : return isolate->Throw(*isolate->factory()->NewTypeError(
70 : MessageTemplate::kDefineDisallowed,
71 60 : handle(error_stack->name(), isolate)));
72 : }
73 :
74 300 : RETURN_FAILURE_ON_EXCEPTION(isolate,
75 : JSObject::SetAccessor(object, error_stack));
76 150 : return isolate->heap()->undefined_value();
77 : }
78 :
79 : // ES6 section 19.5.3.4 Error.prototype.toString ( )
80 978927 : BUILTIN(ErrorPrototypeToString) {
81 : HandleScope scope(isolate);
82 957772 : RETURN_RESULT_OR_FAILURE(isolate,
83 : ErrorUtils::ToString(isolate, args.receiver()));
84 : }
85 :
86 : namespace {
87 :
88 9061 : Object* MakeGenericError(Isolate* isolate, BuiltinArguments args,
89 : Handle<JSFunction> constructor) {
90 : Handle<Object> template_index = args.atOrUndefined(isolate, 1);
91 9061 : Handle<Object> arg0 = args.atOrUndefined(isolate, 2);
92 9061 : Handle<Object> arg1 = args.atOrUndefined(isolate, 3);
93 9061 : Handle<Object> arg2 = args.atOrUndefined(isolate, 4);
94 :
95 : DCHECK(template_index->IsSmi());
96 :
97 27183 : RETURN_RESULT_OR_FAILURE(
98 : isolate, ErrorUtils::MakeGenericError(isolate, constructor,
99 : Smi::ToInt(*template_index), arg0,
100 : arg1, arg2, SKIP_NONE));
101 : }
102 :
103 : } // namespace
104 :
105 0 : BUILTIN(MakeError) {
106 : HandleScope scope(isolate);
107 0 : return MakeGenericError(isolate, args, isolate->error_function());
108 : }
109 :
110 1161 : BUILTIN(MakeRangeError) {
111 : HandleScope scope(isolate);
112 774 : return MakeGenericError(isolate, args, isolate->range_error_function());
113 : }
114 :
115 0 : BUILTIN(MakeSyntaxError) {
116 : HandleScope scope(isolate);
117 0 : return MakeGenericError(isolate, args, isolate->syntax_error_function());
118 : }
119 :
120 26022 : BUILTIN(MakeTypeError) {
121 : HandleScope scope(isolate);
122 17348 : return MakeGenericError(isolate, args, isolate->type_error_function());
123 : }
124 :
125 0 : BUILTIN(MakeURIError) {
126 : HandleScope scope(isolate);
127 0 : Handle<JSFunction> constructor = isolate->uri_error_function();
128 : Handle<Object> undefined = isolate->factory()->undefined_value();
129 : const int template_index = MessageTemplate::kURIMalformed;
130 0 : RETURN_RESULT_OR_FAILURE(
131 : isolate,
132 : ErrorUtils::MakeGenericError(isolate, constructor, template_index,
133 : undefined, undefined, undefined, SKIP_NONE));
134 : }
135 :
136 : } // namespace internal
137 : } // namespace v8
|