• 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/accessors.h"
6 #include "src/builtins/builtins-utils-inl.h"
7 #include "src/builtins/builtins.h"
8 #include "src/execution/isolate-inl.h"
9 #include "src/execution/messages.h"
10 #include "src/logging/counters.h"
11 #include "src/objects/api-callbacks.h"
12 #include "src/objects/objects-inl.h"
13 #include "src/objects/property-descriptor.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 // ES6 section 19.5.1.1 Error ( message )
BUILTIN(ErrorConstructor)19 BUILTIN(ErrorConstructor) {
20   HandleScope scope(isolate);
21   Handle<Object> options = FLAG_harmony_error_cause
22                                ? args.atOrUndefined(isolate, 2)
23                                : isolate->factory()->undefined_value();
24   RETURN_RESULT_OR_FAILURE(
25       isolate, ErrorUtils::Construct(isolate, args.target(), args.new_target(),
26                                      args.atOrUndefined(isolate, 1), options));
27 }
28 
29 // static
BUILTIN(ErrorCaptureStackTrace)30 BUILTIN(ErrorCaptureStackTrace) {
31   HandleScope scope(isolate);
32   Handle<Object> object_obj = args.atOrUndefined(isolate, 1);
33 
34   isolate->CountUsage(v8::Isolate::kErrorCaptureStackTrace);
35 
36   if (!object_obj->IsJSObject()) {
37     THROW_NEW_ERROR_RETURN_FAILURE(
38         isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj));
39   }
40 
41   Handle<JSObject> object = Handle<JSObject>::cast(object_obj);
42   Handle<Object> caller = args.atOrUndefined(isolate, 2);
43   FrameSkipMode mode = caller->IsJSFunction() ? SKIP_UNTIL_SEEN : SKIP_FIRST;
44 
45   // Collect the stack trace.
46 
47   RETURN_FAILURE_ON_EXCEPTION(
48       isolate, isolate->CaptureAndSetErrorStack(object, mode, caller));
49 
50   // Add the stack accessors.
51 
52   Handle<AccessorInfo> error_stack = isolate->factory()->error_stack_accessor();
53   Handle<Name> name(Name::cast(error_stack->name()), isolate);
54 
55   // Explicitly check for frozen objects. Other access checks are performed by
56   // the LookupIterator in SetAccessor below.
57   if (!JSObject::IsExtensible(object)) {
58     return isolate->Throw(*isolate->factory()->NewTypeError(
59         MessageTemplate::kDefineDisallowed, name));
60   }
61 
62   RETURN_FAILURE_ON_EXCEPTION(
63       isolate, JSObject::SetAccessor(object, name, error_stack, DONT_ENUM));
64   return ReadOnlyRoots(isolate).undefined_value();
65 }
66 
67 // ES6 section 19.5.3.4 Error.prototype.toString ( )
BUILTIN(ErrorPrototypeToString)68 BUILTIN(ErrorPrototypeToString) {
69   HandleScope scope(isolate);
70   RETURN_RESULT_OR_FAILURE(isolate,
71                            ErrorUtils::ToString(isolate, args.receiver()));
72 }
73 
74 }  // namespace internal
75 }  // namespace v8
76