FunctionPrototype.cpp [plain text]
#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 {
ASSERT_HAS_TRIVIAL_DESTRUCTOR(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(JSGlobalObject* globalObject, Structure* structure)
: InternalFunction(globalObject, structure)
{
}
void FunctionPrototype::finishCreation(ExecState* exec, const String& name)
{
Base::finishCreation(exec->vm(), name);
putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
}
void FunctionPrototype::addFunctionProperties(ExecState* exec, JSGlobalObject* globalObject, JSFunction** callFunction, JSFunction** applyFunction)
{
JSFunction* toStringFunction = JSFunction::create(exec, globalObject, 0, exec->propertyNames().toString.string(), functionProtoFuncToString);
putDirectWithoutTransition(exec->vm(), exec->propertyNames().toString, toStringFunction, DontEnum);
*applyFunction = JSFunction::create(exec, globalObject, 2, exec->propertyNames().apply.string(), functionProtoFuncApply);
putDirectWithoutTransition(exec->vm(), exec->propertyNames().apply, *applyFunction, DontEnum);
*callFunction = JSFunction::create(exec, globalObject, 1, exec->propertyNames().call.string(), functionProtoFuncCall);
putDirectWithoutTransition(exec->vm(), exec->propertyNames().call, *callFunction, DontEnum);
JSFunction* bindFunction = JSFunction::create(exec, globalObject, 1, exec->propertyNames().bind.string(), functionProtoFuncBind);
putDirectWithoutTransition(exec->vm(), exec->propertyNames().bind, bindFunction, DontEnum);
}
static EncodedJSValue JSC_HOST_CALL callFunctionPrototype(ExecState*)
{
return JSValue::encode(jsUndefined());
}
CallType FunctionPrototype::getCallData(JSCell*, CallData& callData)
{
callData.native.function = callFunctionPrototype;
return CallTypeHost;
}
static inline void insertSemicolonIfNeeded(String& 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::s_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);
return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "(", executable->paramString(), ") ", sourceString));
}
if (thisValue.inherits(&InternalFunction::s_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::s_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));
}
EncodedJSValue JSC_HOST_CALL functionProtoFuncBind(ExecState* exec)
{
JSGlobalObject* globalObject = exec->callee()->globalObject();
JSValue target = exec->hostThisValue();
CallData callData;
CallType callType = getCallData(target, callData);
if (callType == CallTypeNone)
return throwVMTypeError(exec);
ASSERT(target.isObject());
JSObject* targetObject = asObject(target);
size_t numBoundArgs = exec->argumentCount() > 1 ? exec->argumentCount() - 1 : 0;
JSArray* boundArgs = JSArray::tryCreateUninitialized(exec->vm(), globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithUndecided), numBoundArgs);
if (!boundArgs)
return JSValue::encode(throwOutOfMemoryError(exec));
for (size_t i = 0; i < numBoundArgs; ++i)
boundArgs->initializeIndex(exec->vm(), i, exec->argument(i + 1));
unsigned length = 0;
if (targetObject->inherits(&JSFunction::s_info)) {
ASSERT(target.get(exec, exec->propertyNames().length).isNumber());
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(exec, globalObject, targetObject, exec->argument(0), boundArgs, length, name->value(exec)));
}
}