FunctionPrototype.cpp [plain text]
#include "config.h"
#include "FunctionPrototype.h"
#include "Arguments.h"
#include "BuiltinExecutables.h"
#include "BuiltinNames.h"
#include "JSArray.h"
#include "JSBoundFunction.h"
#include "JSFunction.h"
#include "JSString.h"
#include "JSStringBuilder.h"
#include "Interpreter.h"
#include "Lexer.h"
#include "JSCInlines.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 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 = putDirectBuiltinFunctionWithoutTransition(vm, globalObject, vm.propertyNames->builtinNames().applyPublicName(), functionPrototypeApplyCodeGenerator(vm), DontEnum);
*callFunction = putDirectBuiltinFunctionWithoutTransition(vm, globalObject, vm.propertyNames->builtinNames().callPublicName(), functionPrototypeCallCodeGenerator(vm), 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());
}
CallType FunctionPrototype::getCallData(JSCell*, CallData& callData)
{
callData.native.function = callFunctionPrototype;
return CallTypeHost;
}
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->thisValue();
if (thisValue.inherits(JSFunction::info())) {
JSFunction* function = jsCast<JSFunction*>(thisValue);
if (function->isHostOrBuiltinFunction())
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 functionProtoFuncBind(ExecState* exec)
{
JSGlobalObject* globalObject = exec->callee()->globalObject();
JSValue target = exec->thisValue();
CallData callData;
CallType callType = getCallData(target, callData);
if (callType == CallTypeNone)
return throwVMTypeError(exec);
ASSERT(target.isObject());
JSObject* targetObject = asObject(target);
VM& vm = exec->vm();
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));
unsigned length = 0;
if (targetObject->inherits(JSFunction::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(vm, globalObject, targetObject, exec->argument(0), boundArgs, length, name->value(exec)));
}
}