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/objects/api-callbacks.h"
13 #include "src/property-descriptor.h"
14 #include "src/string-builder.h"
15
16 namespace v8 {
17 namespace internal {
18
19 // ES6 section 19.5.1.1 Error ( message )
BUILTIN(ErrorConstructor)20 BUILTIN(ErrorConstructor) {
21 HandleScope scope(isolate);
22
23 FrameSkipMode mode = SKIP_FIRST;
24 Handle<Object> caller;
25
26 // When we're passed a JSFunction as new target, we can skip frames until that
27 // specific function is seen instead of unconditionally skipping the first
28 // frame.
29 if (args.new_target()->IsJSFunction()) {
30 mode = SKIP_UNTIL_SEEN;
31 caller = args.new_target();
32 }
33
34 RETURN_RESULT_OR_FAILURE(
35 isolate, ErrorUtils::Construct(isolate, args.target(),
36 Handle<Object>::cast(args.new_target()),
37 args.atOrUndefined(isolate, 1), mode,
38 caller, false));
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 isolate->heap()->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_RESULT_OR_FAILURE(
100 isolate, ErrorUtils::MakeGenericError(isolate, constructor,
101 Smi::ToInt(*template_index), arg0,
102 arg1, arg2, SKIP_NONE));
103 }
104
105 } // namespace
106
BUILTIN(MakeError)107 BUILTIN(MakeError) {
108 HandleScope scope(isolate);
109 return MakeGenericError(isolate, args, isolate->error_function());
110 }
111
BUILTIN(MakeRangeError)112 BUILTIN(MakeRangeError) {
113 HandleScope scope(isolate);
114 return MakeGenericError(isolate, args, isolate->range_error_function());
115 }
116
BUILTIN(MakeSyntaxError)117 BUILTIN(MakeSyntaxError) {
118 HandleScope scope(isolate);
119 return MakeGenericError(isolate, args, isolate->syntax_error_function());
120 }
121
BUILTIN(MakeTypeError)122 BUILTIN(MakeTypeError) {
123 HandleScope scope(isolate);
124 return MakeGenericError(isolate, args, isolate->type_error_function());
125 }
126
BUILTIN(MakeURIError)127 BUILTIN(MakeURIError) {
128 HandleScope scope(isolate);
129 Handle<JSFunction> constructor = isolate->uri_error_function();
130 Handle<Object> undefined = isolate->factory()->undefined_value();
131 const int template_index = MessageTemplate::kURIMalformed;
132 RETURN_RESULT_OR_FAILURE(
133 isolate,
134 ErrorUtils::MakeGenericError(isolate, constructor, template_index,
135 undefined, undefined, undefined, SKIP_NONE));
136 }
137
138 } // namespace internal
139 } // namespace v8
140