V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
builtins-error.cc
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 )
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
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 ( )
80 BUILTIN(ErrorPrototypeToString) {
81  HandleScope scope(isolate);
82  RETURN_RESULT_OR_FAILURE(isolate,
83  ErrorUtils::ToString(isolate, args.receiver()));
84 }
85 
86 namespace {
87 
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(
99  isolate, constructor,
100  MessageTemplateFromInt(Smi::ToInt(*template_index)), arg0,
101  arg1, arg2, SKIP_NONE));
102 }
103 
104 } // namespace
105 
106 BUILTIN(MakeError) {
107  HandleScope scope(isolate);
108  return MakeGenericError(isolate, args, isolate->error_function());
109 }
110 
111 BUILTIN(MakeRangeError) {
112  HandleScope scope(isolate);
113  return MakeGenericError(isolate, args, isolate->range_error_function());
114 }
115 
116 BUILTIN(MakeSyntaxError) {
117  HandleScope scope(isolate);
118  return MakeGenericError(isolate, args, isolate->syntax_error_function());
119 }
120 
121 BUILTIN(MakeTypeError) {
122  HandleScope scope(isolate);
123  return MakeGenericError(isolate, args, isolate->type_error_function());
124 }
125 
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_RESULT_OR_FAILURE(
132  isolate,
133  ErrorUtils::MakeGenericError(isolate, constructor, template_index,
134  undefined, undefined, undefined, SKIP_NONE));
135 }
136 
137 } // namespace internal
138 } // namespace v8
Definition: libplatform.h:13