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/accessors.h"
6 #include "src/builtins/builtins-utils-inl.h"
7 #include "src/builtins/builtins.h"
8 #include "src/execution/isolate-inl.h"
9 #include "src/execution/messages.h"
10 #include "src/logging/counters.h"
11 #include "src/objects/api-callbacks.h"
12 #include "src/objects/objects-inl.h"
13 #include "src/objects/property-descriptor.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 // ES6 section 19.5.1.1 Error ( message )
BUILTIN(ErrorConstructor)19 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   if (args.new_target()->IsJSFunction()) {
29     mode = SKIP_UNTIL_SEEN;
30     caller = args.new_target();
31   }
32 
33   RETURN_RESULT_OR_FAILURE(
34       isolate,
35       ErrorUtils::Construct(isolate, args.target(),
36                             Handle<Object>::cast(args.new_target()),
37                             args.atOrUndefined(isolate, 1), mode, caller,
38                             ErrorUtils::StackTraceCollection::kDetailed));
39 }
40 
41 // static
BUILTIN(ErrorCaptureStackTrace)42 BUILTIN(ErrorCaptureStackTrace) {
43   HandleScope scope(isolate);
44   Handle<Object> object_obj = args.atOrUndefined(isolate, 1);
45 
46   isolate->CountUsage(v8::Isolate::kErrorCaptureStackTrace);
47 
48   if (!object_obj->IsJSObject()) {
49     THROW_NEW_ERROR_RETURN_FAILURE(
50         isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj));
51   }
52 
53   Handle<JSObject> object = Handle<JSObject>::cast(object_obj);
54   Handle<Object> caller = args.atOrUndefined(isolate, 2);
55   FrameSkipMode mode = caller->IsJSFunction() ? SKIP_UNTIL_SEEN : SKIP_FIRST;
56 
57   // Collect the stack trace.
58 
59   RETURN_FAILURE_ON_EXCEPTION(isolate,
60                               isolate->CaptureAndSetDetailedStackTrace(object));
61   RETURN_FAILURE_ON_EXCEPTION(
62       isolate, isolate->CaptureAndSetSimpleStackTrace(object, mode, caller));
63 
64   // Add the stack accessors.
65 
66   Handle<AccessorInfo> error_stack = isolate->factory()->error_stack_accessor();
67   Handle<Name> name(Name::cast(error_stack->name()), isolate);
68 
69   // Explicitly check for frozen objects. Other access checks are performed by
70   // the LookupIterator in SetAccessor below.
71   if (!JSObject::IsExtensible(object)) {
72     return isolate->Throw(*isolate->factory()->NewTypeError(
73         MessageTemplate::kDefineDisallowed, name));
74   }
75 
76   RETURN_FAILURE_ON_EXCEPTION(
77       isolate, JSObject::SetAccessor(object, name, error_stack, DONT_ENUM));
78   return ReadOnlyRoots(isolate).undefined_value();
79 }
80 
81 // ES6 section 19.5.3.4 Error.prototype.toString ( )
BUILTIN(ErrorPrototypeToString)82 BUILTIN(ErrorPrototypeToString) {
83   HandleScope scope(isolate);
84   RETURN_RESULT_OR_FAILURE(isolate,
85                            ErrorUtils::ToString(isolate, args.receiver()));
86 }
87 
88 namespace {
89 
MakeGenericError(Isolate * isolate,BuiltinArguments args,Handle<JSFunction> constructor)90 Object MakeGenericError(Isolate* isolate, BuiltinArguments args,
91                         Handle<JSFunction> constructor) {
92   Handle<Object> template_index = args.atOrUndefined(isolate, 1);
93   Handle<Object> arg0 = args.atOrUndefined(isolate, 2);
94   Handle<Object> arg1 = args.atOrUndefined(isolate, 3);
95   Handle<Object> arg2 = args.atOrUndefined(isolate, 4);
96 
97   DCHECK(template_index->IsSmi());
98 
99   return *ErrorUtils::MakeGenericError(
100       isolate, constructor, MessageTemplateFromInt(Smi::ToInt(*template_index)),
101       arg0, arg1, arg2, SKIP_NONE);
102 }
103 
104 }  // namespace
105 
BUILTIN(MakeError)106 BUILTIN(MakeError) {
107   HandleScope scope(isolate);
108   return MakeGenericError(isolate, args, isolate->error_function());
109 }
110 
BUILTIN(MakeRangeError)111 BUILTIN(MakeRangeError) {
112   HandleScope scope(isolate);
113   return MakeGenericError(isolate, args, isolate->range_error_function());
114 }
115 
BUILTIN(MakeSyntaxError)116 BUILTIN(MakeSyntaxError) {
117   HandleScope scope(isolate);
118   return MakeGenericError(isolate, args, isolate->syntax_error_function());
119 }
120 
BUILTIN(MakeTypeError)121 BUILTIN(MakeTypeError) {
122   HandleScope scope(isolate);
123   return MakeGenericError(isolate, args, isolate->type_error_function());
124 }
125 
BUILTIN(MakeURIError)126 BUILTIN(MakeURIError) {
127   HandleScope scope(isolate);
128   Handle<JSFunction> constructor = isolate->uri_error_function();
129   Handle<Object> undefined = isolate->factory()->undefined_value();
130   MessageTemplate template_index = MessageTemplate::kURIMalformed;
131   return *ErrorUtils::MakeGenericError(isolate, constructor, template_index,
132                                        undefined, undefined, undefined,
133                                        SKIP_NONE);
134 }
135 
136 }  // namespace internal
137 }  // namespace v8
138