JSCallbackConstructor.cpp 3.81 KB
Newer Older
ggaren's avatar
ggaren committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
ggaren's avatar
ggaren committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

darin@apple.com's avatar
darin@apple.com committed
26
#include "config.h"
ggaren's avatar
ggaren committed
27
#include "JSCallbackConstructor.h"
weinig's avatar
weinig committed
28

29
#include "APIShims.h"
ggaren's avatar
ggaren committed
30
#include "APICast.h"
31
#include <runtime/Error.h>
32
#include <runtime/JSGlobalObject.h>
33
#include <runtime/JSLock.h>
34
#include <runtime/ObjectPrototype.h>
weinig's avatar
weinig committed
35
#include <wtf/Vector.h>
ggaren's avatar
ggaren committed
36

37
namespace JSC {
ggaren's avatar
ggaren committed
38

39
const ClassInfo JSCallbackConstructor::s_info = { "CallbackConstructor", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSCallbackConstructor) };
ggaren's avatar
ggaren committed
40

41
JSCallbackConstructor::JSCallbackConstructor(JSGlobalObject* globalObject, Structure* structure, JSClassRef jsClass, JSObjectCallAsConstructorCallback callback)
42
    : JSDestructibleObject(globalObject->globalData(), structure)
ggaren's avatar
ggaren committed
43
    , m_class(jsClass)
ggaren's avatar
ggaren committed
44 45
    , m_callback(callback)
{
46 47 48 49
}

void JSCallbackConstructor::finishCreation(JSGlobalObject* globalObject, JSClassRef jsClass)
{
50
    Base::finishCreation(globalObject->globalData());
51
    ASSERT(inherits(&s_info));
ggaren's avatar
ggaren committed
52 53 54 55 56 57 58 59
    if (m_class)
        JSClassRetain(jsClass);
}

JSCallbackConstructor::~JSCallbackConstructor()
{
    if (m_class)
        JSClassRelease(m_class);
ggaren's avatar
ggaren committed
60 61
}

62 63
void JSCallbackConstructor::destroy(JSCell* cell)
{
64
    static_cast<JSCallbackConstructor*>(cell)->JSCallbackConstructor::~JSCallbackConstructor();
65 66
}

67
static EncodedJSValue JSC_HOST_CALL constructJSCallback(ExecState* exec)
ggaren's avatar
ggaren committed
68
{
69
    JSObject* constructor = exec->callee();
ggaren's avatar
ggaren committed
70
    JSContextRef ctx = toRef(exec);
darin@apple.com's avatar
darin@apple.com committed
71
    JSObjectRef constructorRef = toRef(constructor);
ggaren's avatar
ggaren committed
72

73
    JSObjectCallAsConstructorCallback callback = jsCast<JSCallbackConstructor*>(constructor)->callback();
darin@apple.com's avatar
darin@apple.com committed
74
    if (callback) {
75
        int argumentCount = static_cast<int>(exec->argumentCount());
weinig's avatar
weinig committed
76
        Vector<JSValueRef, 16> arguments(argumentCount);
darin's avatar
darin committed
77
        for (int i = 0; i < argumentCount; i++)
78
            arguments[i] = toRef(exec, exec->argument(i));
weinig@apple.com's avatar
weinig@apple.com committed
79 80 81 82

        JSValueRef exception = 0;
        JSObjectRef result;
        {
83
            APICallbackShim callbackShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
84 85 86
            result = callback(ctx, constructorRef, argumentCount, arguments.data(), &exception);
        }
        if (exception)
87
            throwError(exec, toJS(exec, exception));
88 89 90
        // result must be a valid JSValue.
        if (!result)
            return throwVMTypeError(exec);
91
        return JSValue::encode(toJS(result));
ggaren's avatar
ggaren committed
92 93
    }
    
94
    return JSValue::encode(toJS(JSObjectMake(ctx, jsCast<JSCallbackConstructor*>(constructor)->classRef(), 0)));
darin@apple.com's avatar
darin@apple.com committed
95 96
}

97
ConstructType JSCallbackConstructor::getConstructData(JSCell*, ConstructData& constructData)
darin@apple.com's avatar
darin@apple.com committed
98 99
{
    constructData.native.function = constructJSCallback;
ggaren@apple.com's avatar
ggaren@apple.com committed
100
    return ConstructTypeHost;
ggaren's avatar
ggaren committed
101 102
}

103
} // namespace JSC