initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / FunctionConstructor.cpp
1 /*
2  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 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 "FunctionConstructor.h"
23
24 #include "Debugger.h"
25 #include "ExceptionHelpers.h"
26 #include "FunctionPrototype.h"
27 #include "JSFunction.h"
28 #include "JSGlobalObject.h"
29 #include "JSString.h"
30 #include "Lexer.h"
31 #include "Nodes.h"
32 #include "Parser.h"
33 #include "UStringBuilder.h"
34 #include "UStringConcatenate.h"
35
36 namespace JSC {
37
38 ASSERT_CLASS_FITS_IN_CELL(FunctionConstructor);
39
40 FunctionConstructor::FunctionConstructor(JSGlobalObject* globalObject, Structure* structure)
41     : InternalFunction(globalObject, structure)
42 {
43 }
44
45 void FunctionConstructor::finishCreation(ExecState* exec, FunctionPrototype* functionPrototype)
46 {
47     Base::finishCreation(exec->globalData(), Identifier(exec, functionPrototype->classInfo()->className));
48     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, functionPrototype, DontEnum | DontDelete | ReadOnly);
49
50     // Number of arguments for constructor
51     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontDelete | DontEnum);
52 }
53
54 static EncodedJSValue JSC_HOST_CALL constructWithFunctionConstructor(ExecState* exec)
55 {
56     ArgList args(exec);
57     return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
58 }
59
60 ConstructType FunctionConstructor::getConstructData(ConstructData& constructData)
61 {
62     constructData.native.function = constructWithFunctionConstructor;
63     return ConstructTypeHost;
64 }
65
66 static EncodedJSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec)
67 {
68     ArgList args(exec);
69     return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
70 }
71
72 // ECMA 15.3.1 The Function Constructor Called as a Function
73 CallType FunctionConstructor::getCallData(CallData& callData)
74 {
75     callData.native.function = callFunctionConstructor;
76     return CallTypeHost;
77 }
78
79 // ECMA 15.3.2 The Function Constructor
80 JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
81 {
82     if (!globalObject->evalEnabled())
83         return throwError(exec, createEvalError(exec, "Function constructor is disabled"));
84     return constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceURL, lineNumber);
85 }
86
87 JSObject* constructFunctionSkippingEvalEnabledCheck(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const UString& sourceURL, int lineNumber)
88 {
89     // Functions need to have a space following the opening { due to for web compatibility
90     // see https://bugs.webkit.org/show_bug.cgi?id=24350
91     // We also need \n before the closing } to handle // comments at the end of the last line
92     UString program;
93     if (args.isEmpty())
94         program = "(function() { \n})";
95     else if (args.size() == 1)
96         program = makeUString("(function() { ", args.at(0).toString(exec), "\n})");
97     else {
98         UStringBuilder builder;
99         builder.append("(function(");
100         builder.append(args.at(0).toString(exec));
101         for (size_t i = 1; i < args.size() - 1; i++) {
102             builder.append(",");
103             builder.append(args.at(i).toString(exec));
104         }
105         builder.append(") { ");
106         builder.append(args.at(args.size() - 1).toString(exec));
107         builder.append("\n})");
108         program = builder.toUString();
109     }
110
111     JSGlobalData& globalData = globalObject->globalData();
112     SourceCode source = makeSource(program, sourceURL, lineNumber);
113     JSObject* exception = 0;
114     FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, exec, exec->dynamicGlobalObject()->debugger(), source, &exception);
115     if (!function) {
116         ASSERT(exception);
117         return throwError(exec, exception);
118     }
119
120     ScopeChainNode* scopeChain = ScopeChainNode::create(exec, 0, globalObject, &globalData, globalObject, exec->globalThisValue());
121     return JSFunction::create(exec, function, scopeChain);
122 }
123
124 // ECMA 15.3.2 The Function Constructor
125 JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
126 {
127     return constructFunction(exec, globalObject, args, Identifier(exec, "anonymous"), UString(), 1);
128 }
129
130 } // namespace JSC