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