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