1/*
2 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 *  Copyright (C) 2003, 2008 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 "JSFunction.h"
26#include "JSString.h"
27#include "NativeErrorPrototype.h"
28
29namespace JSC {
30
31ASSERT_CLASS_FITS_IN_CELL(NativeErrorConstructor);
32
33const ClassInfo NativeErrorConstructor::s_info = { "Function", &InternalFunction::s_info, 0, 0 };
34
35NativeErrorConstructor::NativeErrorConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, Structure* prototypeStructure, const UString& nameAndMessage)
36    : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, nameAndMessage))
37{
38    ASSERT(inherits(&s_info));
39
40    NativeErrorPrototype* prototype = new (exec) NativeErrorPrototype(exec, globalObject, prototypeStructure, nameAndMessage, this);
41
42    putDirect(exec->globalData(), exec->propertyNames().length, jsNumber(1), DontDelete | ReadOnly | DontEnum); // ECMA 15.11.7.5
43    putDirect(exec->globalData(), exec->propertyNames().prototype, prototype, DontDelete | ReadOnly | DontEnum);
44    m_errorStructure.set(exec->globalData(), this, ErrorInstance::createStructure(exec->globalData(), prototype));
45    ASSERT(m_errorStructure);
46    ASSERT(m_errorStructure->typeInfo().type() == ObjectType);
47}
48
49void NativeErrorConstructor::markChildren(MarkStack& markStack)
50{
51    InternalFunction::markChildren(markStack);
52    if (m_errorStructure)
53        markStack.append(&m_errorStructure);
54}
55
56static EncodedJSValue JSC_HOST_CALL constructWithNativeErrorConstructor(ExecState* exec)
57{
58    JSValue message = exec->argumentCount() ? exec->argument(0) : jsUndefined();
59    Structure* errorStructure = static_cast<NativeErrorConstructor*>(exec->callee())->errorStructure();
60    ASSERT(errorStructure);
61    return JSValue::encode(ErrorInstance::create(exec, errorStructure, message));
62}
63
64ConstructType NativeErrorConstructor::getConstructData(ConstructData& constructData)
65{
66    constructData.native.function = constructWithNativeErrorConstructor;
67    return ConstructTypeHost;
68}
69
70static EncodedJSValue JSC_HOST_CALL callNativeErrorConstructor(ExecState* exec)
71{
72    JSValue message = exec->argumentCount() ? exec->argument(0) : jsUndefined();
73    Structure* errorStructure = static_cast<NativeErrorConstructor*>(exec->callee())->errorStructure();
74    return JSValue::encode(ErrorInstance::create(exec, errorStructure, message));
75}
76
77CallType NativeErrorConstructor::getCallData(CallData& callData)
78{
79    callData.native.function = callNativeErrorConstructor;
80    return CallTypeHost;
81}
82
83} // namespace JSC
84