FunctionConstructor.cpp 5.35 KB
Newer Older
weinig@apple.com's avatar
weinig@apple.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"
#include "FunctionConstructor.h"

24
#include "Debugger.h"
25
#include "ExceptionHelpers.h"
weinig@apple.com's avatar
weinig@apple.com committed
26 27 28 29
#include "FunctionPrototype.h"
#include "JSFunction.h"
#include "JSGlobalObject.h"
#include "JSString.h"
30 31
#include "Lexer.h"
#include "Nodes.h"
32
#include "Parser.h"
33
#include "UStringBuilder.h"
34
#include "UStringConcatenate.h"
weinig@apple.com's avatar
weinig@apple.com committed
35

36
namespace JSC {
weinig@apple.com's avatar
weinig@apple.com committed
37

ggaren@apple.com's avatar
ggaren@apple.com committed
38 39
ASSERT_CLASS_FITS_IN_CELL(FunctionConstructor);

40 41
const ClassInfo FunctionConstructor::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionConstructor) };

42
FunctionConstructor::FunctionConstructor(JSGlobalObject* globalObject, Structure* structure)
43
    : InternalFunction(globalObject, structure)
weinig@apple.com's avatar
weinig@apple.com committed
44
{
45 46 47 48 49
}

void FunctionConstructor::finishCreation(ExecState* exec, FunctionPrototype* functionPrototype)
{
    Base::finishCreation(exec->globalData(), Identifier(exec, functionPrototype->classInfo()->className));
50
    putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, functionPrototype, DontEnum | DontDelete | ReadOnly);
weinig@apple.com's avatar
weinig@apple.com committed
51 52

    // Number of arguments for constructor
53
    putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontDelete | DontEnum);
weinig@apple.com's avatar
weinig@apple.com committed
54 55
}

56
static EncodedJSValue JSC_HOST_CALL constructWithFunctionConstructor(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
57
{
58
    ArgList args(exec);
59
    return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
weinig@apple.com's avatar
weinig@apple.com committed
60 61 62 63 64
}

ConstructType FunctionConstructor::getConstructData(ConstructData& constructData)
{
    constructData.native.function = constructWithFunctionConstructor;
ggaren@apple.com's avatar
ggaren@apple.com committed
65
    return ConstructTypeHost;
weinig@apple.com's avatar
weinig@apple.com committed
66 67
}

68
static EncodedJSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
69
{
70
    ArgList args(exec);
71
    return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
weinig@apple.com's avatar
weinig@apple.com committed
72 73
}

74 75 76 77 78
CallType FunctionConstructor::getCallDataVirtual(CallData& callData)
{
    return getCallData(this, callData);
}

weinig@apple.com's avatar
weinig@apple.com committed
79
// ECMA 15.3.1 The Function Constructor Called as a Function
80
CallType FunctionConstructor::getCallData(JSCell*, CallData& callData)
weinig@apple.com's avatar
weinig@apple.com committed
81 82
{
    callData.native.function = callFunctionConstructor;
ggaren@apple.com's avatar
ggaren@apple.com committed
83
    return CallTypeHost;
weinig@apple.com's avatar
weinig@apple.com committed
84 85 86
}

// ECMA 15.3.2 The Function Constructor
87
JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
88
{
89
    if (!globalObject->evalEnabled())
90 91 92 93 94
        return throwError(exec, createEvalError(exec, "Function constructor is disabled"));
    return constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceURL, lineNumber);
}

JSObject* constructFunctionSkippingEvalEnabledCheck(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
weinig@apple.com's avatar
weinig@apple.com committed
95
{
96 97 98
    // Functions need to have a space following the opening { due to for web compatibility
    // see https://bugs.webkit.org/show_bug.cgi?id=24350
    // We also need \n before the closing } to handle // comments at the end of the last line
99 100
    UString program;
    if (args.isEmpty())
101
        program = "(function() { \n})";
102
    else if (args.size() == 1)
103
        program = makeUString("(function() { ", args.at(0).toString(exec), "\n})");
weinig@apple.com's avatar
weinig@apple.com committed
104
    else {
105
        UStringBuilder builder;
106 107 108 109 110 111 112 113 114
        builder.append("(function(");
        builder.append(args.at(0).toString(exec));
        for (size_t i = 1; i < args.size() - 1; i++) {
            builder.append(",");
            builder.append(args.at(i).toString(exec));
        }
        builder.append(") { ");
        builder.append(args.at(args.size() - 1).toString(exec));
        builder.append("\n})");
115
        program = builder.toUString();
weinig@apple.com's avatar
weinig@apple.com committed
116 117
    }

118
    JSGlobalData& globalData = globalObject->globalData();
119
    SourceCode source = makeSource(program, sourceURL, lineNumber);
120
    JSObject* exception = 0;
121
    FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, exec, exec->dynamicGlobalObject()->debugger(), source, &exception);
122 123 124 125
    if (!function) {
        ASSERT(exception);
        return throwError(exec, exception);
    }
weinig@apple.com's avatar
weinig@apple.com committed
126

127 128
    ScopeChainNode* scopeChain = ScopeChainNode::create(exec, 0, globalObject, &globalData, globalObject, exec->globalThisValue());
    return JSFunction::create(exec, function, scopeChain);
weinig@apple.com's avatar
weinig@apple.com committed
129 130 131
}

// ECMA 15.3.2 The Function Constructor
132
JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
weinig@apple.com's avatar
weinig@apple.com committed
133
{
134
    return constructFunction(exec, globalObject, args, Identifier(exec, "anonymous"), UString(), 1);
weinig@apple.com's avatar
weinig@apple.com committed
135 136
}

137
} // namespace JSC