forked from phoboslab/JavaScriptCore-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionPrototype.cpp
More file actions
219 lines (182 loc) · 9 KB
/
FunctionPrototype.cpp
File metadata and controls
219 lines (182 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (C) 1999-2001 Harri Porten ([email protected])
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 "FunctionPrototype.h"
#include "Arguments.h"
#include "JSArray.h"
#include "JSBoundFunction.h"
#include "JSFunction.h"
#include "JSString.h"
#include "JSStringBuilder.h"
#include "Interpreter.h"
#include "Lexer.h"
#include "Operations.h"
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(FunctionPrototype);
const ClassInfo FunctionPrototype::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionPrototype) };
static EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState*);
static EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState*);
static EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState*);
static EncodedJSValue JSC_HOST_CALL functionProtoFuncBind(ExecState*);
FunctionPrototype::FunctionPrototype(VM& vm, Structure* structure)
: InternalFunction(vm, structure)
{
}
void FunctionPrototype::finishCreation(VM& vm, const String& name)
{
Base::finishCreation(vm, name);
putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
}
void FunctionPrototype::addFunctionProperties(ExecState* exec, JSGlobalObject* globalObject, JSFunction** callFunction, JSFunction** applyFunction)
{
VM& vm = exec->vm();
JSFunction* toStringFunction = JSFunction::create(vm, globalObject, 0, vm.propertyNames->toString.string(), functionProtoFuncToString);
putDirectWithoutTransition(vm, vm.propertyNames->toString, toStringFunction, DontEnum);
*applyFunction = JSFunction::create(vm, globalObject, 2, vm.propertyNames->apply.string(), functionProtoFuncApply);
putDirectWithoutTransition(vm, vm.propertyNames->apply, *applyFunction, DontEnum);
*callFunction = JSFunction::create(vm, globalObject, 1, vm.propertyNames->call.string(), functionProtoFuncCall);
putDirectWithoutTransition(vm, vm.propertyNames->call, *callFunction, DontEnum);
JSFunction* bindFunction = JSFunction::create(vm, globalObject, 1, vm.propertyNames->bind.string(), functionProtoFuncBind);
putDirectWithoutTransition(vm, vm.propertyNames->bind, bindFunction, DontEnum);
}
static EncodedJSValue JSC_HOST_CALL callFunctionPrototype(ExecState*)
{
return JSValue::encode(jsUndefined());
}
// ECMA 15.3.4
CallType FunctionPrototype::getCallData(JSCell*, CallData& callData)
{
callData.native.function = callFunctionPrototype;
return CallTypeHost;
}
// Functions
// Compatibility hack for the Optimost JavaScript library. (See <rdar://problem/6595040>.)
static inline void insertSemicolonIfNeeded(String& functionBody, bool bodyIncludesBraces)
{
if (!bodyIncludesBraces)
functionBody = makeString("{ ", functionBody, "}");
ASSERT(functionBody[0] == '{');
ASSERT(functionBody[functionBody.length() - 1] == '}');
for (size_t i = functionBody.length() - 2; i > 0; --i) {
UChar ch = functionBody[i];
if (!Lexer<UChar>::isWhiteSpace(ch) && !Lexer<UChar>::isLineTerminator(ch)) {
if (ch != ';' && ch != '}')
functionBody = makeString(functionBody.substringSharingImpl(0, i + 1), ";", functionBody.substringSharingImpl(i + 1, functionBody.length() - (i + 1)));
return;
}
}
}
EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
if (thisValue.inherits(JSFunction::info())) {
JSFunction* function = jsCast<JSFunction*>(thisValue);
if (function->isHostFunction())
return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "() {\n [native code]\n}"));
FunctionExecutable* executable = function->jsExecutable();
String sourceString = executable->source().toString();
insertSemicolonIfNeeded(sourceString, executable->bodyIncludesBraces());
return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "(", executable->paramString(), ") ", sourceString));
}
if (thisValue.inherits(InternalFunction::info())) {
InternalFunction* function = asInternalFunction(thisValue);
return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "() {\n [native code]\n}"));
}
return throwVMTypeError(exec);
}
EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
CallData callData;
CallType callType = getCallData(thisValue, callData);
if (callType == CallTypeNone)
return throwVMTypeError(exec);
JSValue array = exec->argument(1);
MarkedArgumentBuffer applyArgs;
if (!array.isUndefinedOrNull()) {
if (!array.isObject())
return throwVMTypeError(exec);
if (asObject(array)->classInfo() == Arguments::info()) {
if (asArguments(array)->length(exec) > Arguments::MaxArguments)
return JSValue::encode(throwStackOverflowError(exec));
asArguments(array)->fillArgList(exec, applyArgs);
} else if (isJSArray(array)) {
if (asArray(array)->length() > Arguments::MaxArguments)
return JSValue::encode(throwStackOverflowError(exec));
asArray(array)->fillArgList(exec, applyArgs);
} else {
unsigned length = asObject(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
if (length > Arguments::MaxArguments)
return JSValue::encode(throwStackOverflowError(exec));
for (unsigned i = 0; i < length; ++i)
applyArgs.append(asObject(array)->get(exec, i));
}
}
return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), applyArgs));
}
EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
CallData callData;
CallType callType = getCallData(thisValue, callData);
if (callType == CallTypeNone)
return throwVMTypeError(exec);
ArgList args(exec);
ArgList callArgs;
args.getSlice(1, callArgs);
return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), callArgs));
}
// 15.3.4.5 Function.prototype.bind (thisArg [, arg1 [, arg2, ...]])
EncodedJSValue JSC_HOST_CALL functionProtoFuncBind(ExecState* exec)
{
JSGlobalObject* globalObject = exec->callee()->globalObject();
// Let Target be the this value.
JSValue target = exec->hostThisValue();
// If IsCallable(Target) is false, throw a TypeError exception.
CallData callData;
CallType callType = getCallData(target, callData);
if (callType == CallTypeNone)
return throwVMTypeError(exec);
// Primitive values are not callable.
ASSERT(target.isObject());
JSObject* targetObject = asObject(target);
VM& vm = exec->vm();
// Let A be a new (possibly empty) internal list of all of the argument values provided after thisArg (arg1, arg2 etc), in order.
size_t numBoundArgs = exec->argumentCount() > 1 ? exec->argumentCount() - 1 : 0;
JSArray* boundArgs = JSArray::tryCreateUninitialized(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithUndecided), numBoundArgs);
if (!boundArgs)
return JSValue::encode(throwOutOfMemoryError(exec));
for (size_t i = 0; i < numBoundArgs; ++i)
boundArgs->initializeIndex(vm, i, exec->argument(i + 1));
// If the [[Class]] internal property of Target is "Function", then ...
// Else set the length own property of F to 0.
unsigned length = 0;
if (targetObject->inherits(JSFunction::info())) {
ASSERT(target.get(exec, exec->propertyNames().length).isNumber());
// a. Let L be the length property of Target minus the length of A.
// b. Set the length own property of F to either 0 or L, whichever is larger.
unsigned targetLength = (unsigned)target.get(exec, exec->propertyNames().length).asNumber();
if (targetLength > numBoundArgs)
length = targetLength - numBoundArgs;
}
JSString* name = target.get(exec, exec->propertyNames().name).toString(exec);
return JSValue::encode(JSBoundFunction::create(vm, globalObject, targetObject, exec->argument(0), boundArgs, length, name->value(exec)));
}
} // namespace JSC