• 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/builtins/builtins.h"
6 #include "src/builtins/builtins-utils.h"
7 
8 #include "src/counters.h"
9 #include "src/messages.h"
10 #include "src/objects-inl.h"
11 #include "src/property-descriptor.h"
12 #include "src/string-builder.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   if (!object_obj->IsJSObject()) {
44     THROW_NEW_ERROR_RETURN_FAILURE(
45         isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj));
46   }
47   Handle<JSObject> object = Handle<JSObject>::cast(object_obj);
48   Handle<Object> caller = args.atOrUndefined(isolate, 2);
49   FrameSkipMode mode = caller->IsJSFunction() ? SKIP_UNTIL_SEEN : SKIP_FIRST;
50 
51   // Collect the stack trace.
52 
53   RETURN_FAILURE_ON_EXCEPTION(isolate,
54                               isolate->CaptureAndSetDetailedStackTrace(object));
55 
56   // Eagerly format the stack trace and set the stack property.
57 
58   Handle<Object> stack_trace =
59       isolate->CaptureSimpleStackTrace(object, mode, caller);
60   if (!stack_trace->IsJSArray()) return isolate->heap()->undefined_value();
61 
62   Handle<Object> formatted_stack_trace;
63   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
64       isolate, formatted_stack_trace,
65       ErrorUtils::FormatStackTrace(isolate, object, stack_trace));
66 
67   PropertyDescriptor desc;
68   desc.set_configurable(true);
69   desc.set_writable(true);
70   desc.set_value(formatted_stack_trace);
71   Maybe<bool> status = JSReceiver::DefineOwnProperty(
72       isolate, object, isolate->factory()->stack_string(), &desc,
73       Object::THROW_ON_ERROR);
74   if (!status.IsJust()) return isolate->heap()->exception();
75   CHECK(status.FromJust());
76   return isolate->heap()->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::cast(*template_index)->value(),
100                                             arg0, 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