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