| 1 | /* |
| 2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
| 3 | * Copyright (C) 2003, 2008, 2016 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 "NativeErrorConstructor.h" |
| 23 | |
| 24 | #include "ErrorInstance.h" |
| 25 | #include "Interpreter.h" |
| 26 | #include "JSFunction.h" |
| 27 | #include "JSString.h" |
| 28 | #include "NativeErrorPrototype.h" |
| 29 | #include "JSCInlines.h" |
| 30 | |
| 31 | namespace JSC { |
| 32 | |
| 33 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(NativeErrorConstructorBase); |
| 34 | |
| 35 | const ClassInfo NativeErrorConstructorBase::s_info = { "Function" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(NativeErrorConstructorBase) }; |
| 36 | |
| 37 | template<ErrorType errorType> |
| 38 | NativeErrorConstructor<errorType>::NativeErrorConstructor(VM& vm, Structure* structure) |
| 39 | : NativeErrorConstructorBase(vm, structure, NativeErrorConstructor<errorType>::callNativeErrorConstructor, NativeErrorConstructor<errorType>::constructNativeErrorConstructor) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | void NativeErrorConstructorBase::finishCreation(VM& vm, NativeErrorPrototype* prototype, ErrorType errorType) |
| 44 | { |
| 45 | Base::finishCreation(vm, errorTypeName(errorType), NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition); |
| 46 | ASSERT(inherits(vm, info())); |
| 47 | |
| 48 | putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly); |
| 49 | putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum); |
| 50 | } |
| 51 | |
| 52 | template<ErrorType errorType> |
| 53 | EncodedJSValue JSC_HOST_CALL NativeErrorConstructor<errorType>::constructNativeErrorConstructor(ExecState* exec) |
| 54 | { |
| 55 | VM& vm = exec->vm(); |
| 56 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 57 | JSValue message = exec->argument(0); |
| 58 | Structure* errorStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), jsCast<NativeErrorConstructor*>(exec->jsCallee())->errorStructure(vm)); |
| 59 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 60 | ASSERT(errorStructure); |
| 61 | RELEASE_AND_RETURN(scope, JSValue::encode(ErrorInstance::create(exec, errorStructure, message, nullptr, TypeNothing, false))); |
| 62 | } |
| 63 | |
| 64 | template<ErrorType errorType> |
| 65 | EncodedJSValue JSC_HOST_CALL NativeErrorConstructor<errorType>::callNativeErrorConstructor(ExecState* exec) |
| 66 | { |
| 67 | VM& vm = exec->vm(); |
| 68 | JSValue message = exec->argument(0); |
| 69 | Structure* errorStructure = jsCast<NativeErrorConstructor*>(exec->jsCallee())->errorStructure(vm); |
| 70 | return JSValue::encode(ErrorInstance::create(exec, errorStructure, message, nullptr, TypeNothing, false)); |
| 71 | } |
| 72 | |
| 73 | template class NativeErrorConstructor<ErrorType::EvalError>; |
| 74 | template class NativeErrorConstructor<ErrorType::RangeError>; |
| 75 | template class NativeErrorConstructor<ErrorType::ReferenceError>; |
| 76 | template class NativeErrorConstructor<ErrorType::SyntaxError>; |
| 77 | template class NativeErrorConstructor<ErrorType::TypeError>; |
| 78 | template class NativeErrorConstructor<ErrorType::URIError>; |
| 79 | |
| 80 | } // namespace JSC |
| 81 | |