/ runtime / ErrorConstructor.cpp
ErrorConstructor.cpp
  1  /*
  2   *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3   *  Copyright (C) 2003-2017 Apple Inc. All rights reserved.
  4   *
  5   *  This library is free software; you can redistribute it and/or
  6   *  modify it under the terms of the GNU Lesser General Public
  7   *  License as published by the Free Software Foundation; either
  8   *  version 2 of the License, or (at your option) any later version.
  9   *
 10   *  This library is distributed in the hope that it will be useful,
 11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13   *  Lesser General Public License for more details.
 14   *
 15   *  You should have received a copy of the GNU Lesser General Public
 16   *  License along with this library; if not, write to the Free Software
 17   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 18   *
 19   */
 20  
 21  #include "config.h"
 22  #include "ErrorConstructor.h"
 23  
 24  #include "ErrorPrototype.h"
 25  #include "JSCInlines.h"
 26  
 27  namespace JSC {
 28  
 29  STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ErrorConstructor);
 30  
 31  const ClassInfo ErrorConstructor::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(ErrorConstructor) };
 32  
 33  static JSC_DECLARE_HOST_FUNCTION(callErrorConstructor);
 34  static JSC_DECLARE_HOST_FUNCTION(constructErrorConstructor);
 35  
 36  ErrorConstructor::ErrorConstructor(VM& vm, Structure* structure)
 37      : InternalFunction(vm, structure, callErrorConstructor, constructErrorConstructor)
 38  {
 39  }
 40  
 41  void ErrorConstructor::finishCreation(VM& vm, ErrorPrototype* errorPrototype)
 42  {
 43      Base::finishCreation(vm, 1, vm.propertyNames->Error.string(), PropertyAdditionMode::WithoutStructureTransition);
 44      // ECMA 15.11.3.1 Error.prototype
 45      putDirectWithoutTransition(vm, vm.propertyNames->prototype, errorPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
 46      putDirectWithoutTransition(vm, vm.propertyNames->stackTraceLimit, jsNumber(globalObject()->stackTraceLimit().valueOr(Options::defaultErrorStackTraceLimit())), static_cast<unsigned>(PropertyAttribute::None));
 47  }
 48  
 49  // ECMA 15.9.3
 50  
 51  JSC_DEFINE_HOST_FUNCTION(constructErrorConstructor, (JSGlobalObject* globalObject, CallFrame* callFrame))
 52  {
 53      VM& vm = globalObject->vm();
 54      auto scope = DECLARE_THROW_SCOPE(vm);
 55      JSValue message = callFrame->argument(0);
 56  
 57      JSObject* newTarget = asObject(callFrame->newTarget());
 58      Structure* errorStructure = newTarget == callFrame->jsCallee()
 59          ? globalObject->errorStructure()
 60          : InternalFunction::createSubclassStructure(globalObject, newTarget, getFunctionRealm(vm, newTarget)->errorStructure());
 61      RETURN_IF_EXCEPTION(scope, { });
 62  
 63      RELEASE_AND_RETURN(scope, JSValue::encode(ErrorInstance::create(globalObject, errorStructure, message, nullptr, TypeNothing, false)));
 64  }
 65  
 66  JSC_DEFINE_HOST_FUNCTION(callErrorConstructor, (JSGlobalObject* globalObject, CallFrame* callFrame))
 67  {
 68      JSValue message = callFrame->argument(0);
 69      Structure* errorStructure = globalObject->errorStructure();
 70      return JSValue::encode(ErrorInstance::create(globalObject, errorStructure, message, nullptr, TypeNothing, false));
 71  }
 72  
 73  bool ErrorConstructor::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
 74  {
 75      VM& vm = globalObject->vm();
 76      ErrorConstructor* thisObject = jsCast<ErrorConstructor*>(cell);
 77  
 78      if (propertyName == vm.propertyNames->stackTraceLimit) {
 79          if (value.isNumber()) {
 80              double effectiveLimit = value.asNumber();
 81              effectiveLimit = std::max(0., effectiveLimit);
 82              effectiveLimit = std::min(effectiveLimit, static_cast<double>(std::numeric_limits<unsigned>::max()));
 83              thisObject->globalObject()->setStackTraceLimit(static_cast<unsigned>(effectiveLimit));
 84          } else
 85              thisObject->globalObject()->setStackTraceLimit(WTF::nullopt);
 86      }
 87  
 88      return Base::put(thisObject, globalObject, propertyName, value, slot);
 89  }
 90  
 91  bool ErrorConstructor::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, DeletePropertySlot& slot)
 92  {
 93      VM& vm = globalObject->vm();
 94      ErrorConstructor* thisObject = jsCast<ErrorConstructor*>(cell);
 95  
 96      if (propertyName == vm.propertyNames->stackTraceLimit)
 97          thisObject->globalObject()->setStackTraceLimit(WTF::nullopt);
 98  
 99      return Base::deleteProperty(thisObject, globalObject, propertyName, slot);
100  }
101  
102  } // namespace JSC