/ runtime / AggregateError.cpp
AggregateError.cpp
 1  /*
 2   * Copyright (C) 2020 Apple Inc. All rights reserved.
 3   *
 4   * Redistribution and use in source and binary forms, with or without
 5   * modification, are permitted provided that the following conditions
 6   * are met:
 7   * 1. Redistributions of source code must retain the above copyright
 8   *    notice, this list of conditions and the following disclaimer.
 9   * 2. Redistributions in binary form must reproduce the above copyright
10   *    notice, this list of conditions and the following disclaimer in the
11   *    documentation and/or other materials provided with the distribution.
12   *
13   * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23   * THE POSSIBILITY OF SUCH DAMAGE.
24   */
25  
26  #include "config.h"
27  #include "AggregateError.h"
28  
29  #include "ExceptionScope.h"
30  #include "IteratorOperations.h"
31  #include "JSCJSValueInlines.h"
32  #include "JSGlobalObjectInlines.h"
33  
34  namespace JSC {
35  
36  const ClassInfo AggregateError::s_info = { "AggregateError", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(AggregateError) };
37  
38  AggregateError::AggregateError(VM& vm, Structure* structure)
39      : Base(vm, structure)
40  {
41  }
42  
43  void AggregateError::finishCreation(VM& vm, JSGlobalObject* globalObject, const MarkedArgumentBuffer& errors, const String& message, SourceAppender appender, RuntimeType type, bool useCurrentFrame)
44  {
45      Base::finishCreation(vm, globalObject, message, appender, type, useCurrentFrame);
46      ASSERT(inherits(vm, info()));
47  
48      auto scope = DECLARE_THROW_SCOPE(vm);
49      putDirect(vm, vm.propertyNames->errors, constructArray(globalObject, static_cast<ArrayAllocationProfile*>(nullptr), errors), static_cast<unsigned>(PropertyAttribute::DontEnum));
50      RETURN_IF_EXCEPTION(scope, void());
51  }
52  
53  AggregateError* AggregateError::create(JSGlobalObject* globalObject, VM& vm, Structure* structure, JSValue errors, JSValue message, SourceAppender appender, RuntimeType type, bool useCurrentFrame)
54  {
55      auto scope = DECLARE_THROW_SCOPE(vm);
56  
57      String messageString;
58      if (!message.isUndefined()) {
59          messageString = message.toWTFString(globalObject);
60          RETURN_IF_EXCEPTION(scope, nullptr);
61      }
62  
63      MarkedArgumentBuffer errorsList;
64      forEachInIterable(globalObject, errors, [&] (VM&, JSGlobalObject*, JSValue nextValue) {
65          errorsList.append(nextValue);
66          if (UNLIKELY(errorsList.hasOverflowed()))
67              throwOutOfMemoryError(globalObject, scope);
68      });
69      RETURN_IF_EXCEPTION(scope, nullptr);
70  
71      RELEASE_AND_RETURN(scope, create(globalObject, vm, structure, errorsList, messageString, appender, type, useCurrentFrame));
72  }
73  
74  } // namespace JSC