1 | /*
|
---|
2 | * Copyright (C) 1999-2002 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
4 | * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
|
---|
5 | * Copyright (C) 2007 Cameron Zwarich ([email protected])
|
---|
6 | * Copyright (C) 2007 Maks Orlovich
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Library General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Library General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Library General Public License
|
---|
19 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
21 | * Boston, MA 02110-1301, USA.
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "config.h"
|
---|
26 | #include "JSFunction.h"
|
---|
27 |
|
---|
28 | #include "CommonIdentifiers.h"
|
---|
29 | #include "ExecState.h"
|
---|
30 | #include "FunctionPrototype.h"
|
---|
31 | #include "JSGlobalObject.h"
|
---|
32 | #include "Machine.h"
|
---|
33 | #include "ObjectPrototype.h"
|
---|
34 | #include "Parser.h"
|
---|
35 | #include "PropertyNameArray.h"
|
---|
36 | #include "ScopeChainMark.h"
|
---|
37 |
|
---|
38 | using namespace WTF;
|
---|
39 | using namespace Unicode;
|
---|
40 |
|
---|
41 | namespace KJS {
|
---|
42 |
|
---|
43 | ASSERT_CLASS_FITS_IN_CELL(JSFunction);
|
---|
44 |
|
---|
45 | const ClassInfo JSFunction::info = { "Function", 0, 0, 0 };
|
---|
46 |
|
---|
47 | JSFunction::JSFunction(ExecState* exec, const Identifier& name, FunctionBodyNode* body, ScopeChainNode* scopeChainNode)
|
---|
48 | : Base(exec, exec->lexicalGlobalObject()->functionPrototype(), name)
|
---|
49 | , m_body(body)
|
---|
50 | , m_scopeChain(scopeChainNode)
|
---|
51 | {
|
---|
52 | }
|
---|
53 |
|
---|
54 | void JSFunction::mark()
|
---|
55 | {
|
---|
56 | Base::mark();
|
---|
57 | m_body->mark();
|
---|
58 | m_scopeChain.mark();
|
---|
59 | }
|
---|
60 |
|
---|
61 | CallType JSFunction::getCallData(CallData& callData)
|
---|
62 | {
|
---|
63 | callData.js.functionBody = m_body.get();
|
---|
64 | callData.js.scopeChain = m_scopeChain.node();
|
---|
65 | return CallTypeJS;
|
---|
66 | }
|
---|
67 |
|
---|
68 | JSValue* JSFunction::call(ExecState* exec, JSValue* thisValue, const ArgList& args)
|
---|
69 | {
|
---|
70 | return exec->machine()->execute(m_body.get(), exec, this, thisValue->toThisObject(exec), args, m_scopeChain.node(), exec->exceptionSlot());
|
---|
71 | }
|
---|
72 |
|
---|
73 | JSValue* JSFunction::argumentsGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
|
---|
74 | {
|
---|
75 | JSFunction* thisObj = static_cast<JSFunction*>(slot.slotBase());
|
---|
76 | return exec->machine()->retrieveArguments(exec, thisObj);
|
---|
77 | }
|
---|
78 |
|
---|
79 | JSValue* JSFunction::callerGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
|
---|
80 | {
|
---|
81 | JSFunction* thisObj = static_cast<JSFunction*>(slot.slotBase());
|
---|
82 | return exec->machine()->retrieveCaller(exec, thisObj);
|
---|
83 | }
|
---|
84 |
|
---|
85 | JSValue* JSFunction::lengthGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
|
---|
86 | {
|
---|
87 | JSFunction* thisObj = static_cast<JSFunction*>(slot.slotBase());
|
---|
88 | return jsNumber(exec, thisObj->m_body->parameters().size());
|
---|
89 | }
|
---|
90 |
|
---|
91 | bool JSFunction::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
92 | {
|
---|
93 | if (propertyName == exec->propertyNames().arguments) {
|
---|
94 | slot.setCustom(this, argumentsGetter);
|
---|
95 | return true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (propertyName == exec->propertyNames().length) {
|
---|
99 | slot.setCustom(this, lengthGetter);
|
---|
100 | return true;
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (propertyName == exec->propertyNames().caller) {
|
---|
104 | slot.setCustom(this, callerGetter);
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | return Base::getOwnPropertySlot(exec, propertyName, slot);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void JSFunction::put(ExecState* exec, const Identifier& propertyName, JSValue* value, PutPropertySlot& slot)
|
---|
112 | {
|
---|
113 | if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
|
---|
114 | return;
|
---|
115 | Base::put(exec, propertyName, value, slot);
|
---|
116 | }
|
---|
117 |
|
---|
118 | bool JSFunction::deleteProperty(ExecState* exec, const Identifier& propertyName)
|
---|
119 | {
|
---|
120 | if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
|
---|
121 | return false;
|
---|
122 | return Base::deleteProperty(exec, propertyName);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* Returns the parameter name corresponding to the given index. eg:
|
---|
126 | * function f1(x, y, z): getParameterName(0) --> x
|
---|
127 | *
|
---|
128 | * If a name appears more than once, only the last index at which
|
---|
129 | * it appears associates with it. eg:
|
---|
130 | * function f2(x, x): getParameterName(0) --> null
|
---|
131 | */
|
---|
132 | const Identifier& JSFunction::getParameterName(int index)
|
---|
133 | {
|
---|
134 | Vector<Identifier>& parameters = m_body->parameters();
|
---|
135 |
|
---|
136 | if (static_cast<size_t>(index) >= m_body->parameters().size())
|
---|
137 | return m_scopeChain.globalObject()->globalData()->propertyNames->nullIdentifier;
|
---|
138 |
|
---|
139 | const Identifier& name = parameters[index];
|
---|
140 |
|
---|
141 | // Are there any subsequent parameters with the same name?
|
---|
142 | size_t size = parameters.size();
|
---|
143 | for (size_t i = index + 1; i < size; ++i) {
|
---|
144 | if (parameters[i] == name)
|
---|
145 | return m_scopeChain.globalObject()->globalData()->propertyNames->nullIdentifier;
|
---|
146 | }
|
---|
147 |
|
---|
148 | return name;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // ECMA 13.2.2 [[Construct]]
|
---|
152 | ConstructType JSFunction::getConstructData(ConstructData& constructData)
|
---|
153 | {
|
---|
154 | constructData.js.functionBody = m_body.get();
|
---|
155 | constructData.js.scopeChain = m_scopeChain.node();
|
---|
156 | return ConstructTypeJS;
|
---|
157 | }
|
---|
158 |
|
---|
159 | JSObject* JSFunction::construct(ExecState* exec, const ArgList& args)
|
---|
160 | {
|
---|
161 | JSObject* proto;
|
---|
162 | JSValue* p = get(exec, exec->propertyNames().prototype);
|
---|
163 | if (p->isObject())
|
---|
164 | proto = static_cast<JSObject*>(p);
|
---|
165 | else
|
---|
166 | proto = exec->lexicalGlobalObject()->objectPrototype();
|
---|
167 |
|
---|
168 | JSObject* thisObj = new (exec) JSObject(proto);
|
---|
169 |
|
---|
170 | JSValue* result = exec->machine()->execute(m_body.get(), exec, this, thisObj, args, m_scopeChain.node(), exec->exceptionSlot());
|
---|
171 | if (exec->hadException() || !result->isObject())
|
---|
172 | return thisObj;
|
---|
173 | return static_cast<JSObject*>(result);
|
---|
174 | }
|
---|
175 |
|
---|
176 | } // namespace KJS
|
---|