function_object.cpp [plain text]
#include "function_object.h"
#include "internal.h"
#include "function.h"
#include "array_object.h"
#include "nodes.h"
#include "lexer.h"
#include "debugger.h"
#include "object.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
using namespace KJS;
FunctionPrototypeImp::FunctionPrototypeImp(ExecState *exec)
: InternalFunctionImp(0)
{
Value protect(this);
putDirect(toStringPropertyName, new FunctionProtoFuncImp(exec, this, FunctionProtoFuncImp::ToString, 0), DontEnum);
static const Identifier applyPropertyName("apply");
putDirect(applyPropertyName, new FunctionProtoFuncImp(exec, this, FunctionProtoFuncImp::Apply, 2), DontEnum);
static const Identifier callPropertyName("call");
putDirect(callPropertyName, new FunctionProtoFuncImp(exec, this, FunctionProtoFuncImp::Call, 1), DontEnum);
}
FunctionPrototypeImp::~FunctionPrototypeImp()
{
}
bool FunctionPrototypeImp::implementsCall() const
{
return true;
}
Value FunctionPrototypeImp::call(ExecState *, Object &, const List &)
{
return Undefined();
}
FunctionProtoFuncImp::FunctionProtoFuncImp(ExecState *exec,
FunctionPrototypeImp *funcProto, int i, int len)
: InternalFunctionImp(funcProto), id(i)
{
Value protect(this);
putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
}
bool FunctionProtoFuncImp::implementsCall() const
{
return true;
}
Value FunctionProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &args)
{
Value result;
switch (id) {
case ToString: {
if (thisObj.isNull() || !thisObj.inherits(&InternalFunctionImp::info)) {
#ifndef NDEBUG
fprintf(stderr,"attempted toString() call on null or non-function object\n");
#endif
Object err = Error::create(exec,TypeError);
exec->setException(err);
return err;
}
if (thisObj.inherits(&DeclaredFunctionImp::info)) {
DeclaredFunctionImp *fi = static_cast<DeclaredFunctionImp*>
(thisObj.imp());
return String("function " + fi->name().ustring() + "(" +
fi->parameterString() + ") " + fi->body->toString());
} else if (thisObj.inherits(&FunctionImp::info) &&
!static_cast<FunctionImp*>(thisObj.imp())->name().isNull()) {
result = String("function " + static_cast<FunctionImp*>(thisObj.imp())->name().ustring() + "()");
}
else {
result = String("(Internal function)");
}
}
break;
case Apply: {
Value thisArg = args[0];
Value argArray = args[1];
Object func = thisObj;
if (!func.implementsCall()) {
Object err = Error::create(exec,TypeError);
exec->setException(err);
return err;
}
Object applyThis;
if (thisArg.isA(NullType) || thisArg.isA(UndefinedType))
applyThis = exec->interpreter()->globalObject();
else
applyThis = thisArg.toObject(exec);
List applyArgs;
if (!argArray.isA(NullType) && !argArray.isA(UndefinedType)) {
if (argArray.isA(ObjectType) &&
(Object::dynamicCast(argArray).inherits(&ArrayInstanceImp::info) ||
Object::dynamicCast(argArray).inherits(&ArgumentsImp::info))) {
Object argArrayObj = Object::dynamicCast(argArray);
unsigned int length = argArrayObj.get(exec,lengthPropertyName).toUInt32(exec);
for (unsigned int i = 0; i < length; i++)
applyArgs.append(argArrayObj.get(exec,i));
}
else {
Object err = Error::create(exec,TypeError);
exec->setException(err);
return err;
}
}
result = func.call(exec,applyThis,applyArgs);
}
break;
case Call: {
Value thisArg = args[0];
Object func = thisObj;
if (!func.implementsCall()) {
Object err = Error::create(exec,TypeError);
exec->setException(err);
return err;
}
Object callThis;
if (thisArg.isA(NullType) || thisArg.isA(UndefinedType))
callThis = exec->interpreter()->globalObject();
else
callThis = thisArg.toObject(exec);
result = func.call(exec,callThis,args.copyTail());
}
break;
}
return result;
}
FunctionObjectImp::FunctionObjectImp(ExecState *exec, FunctionPrototypeImp *funcProto)
: InternalFunctionImp(funcProto)
{
Value protect(this);
putDirect(prototypePropertyName, funcProto, DontEnum|DontDelete|ReadOnly);
putDirect(lengthPropertyName, NumberImp::one(), ReadOnly|DontDelete|DontEnum);
}
FunctionObjectImp::~FunctionObjectImp()
{
}
bool FunctionObjectImp::implementsConstruct() const
{
return true;
}
Object FunctionObjectImp::construct(ExecState *exec, const List &args)
{
UString p("");
UString body;
int argsSize = args.size();
if (argsSize == 0) {
body = "";
} else if (argsSize == 1) {
body = args[0].toString(exec);
} else {
p = args[0].toString(exec);
for (int k = 1; k < argsSize - 1; k++)
p += "," + args[k].toString(exec);
body = args[argsSize-1].toString(exec);
}
int sid;
int errLine;
UString errMsg;
ProgramNode *progNode = Parser::parse(body.data(),body.size(),&sid,&errLine,&errMsg);
Debugger *dbg = exec->interpreter()->imp()->debugger();
if (dbg) {
bool cont = dbg->sourceParsed(exec,sid,body,errLine);
if (!cont) {
dbg->imp()->abort();
return Object(new ObjectImp());
}
}
if (!progNode) {
Object err = Error::create(exec,SyntaxError,errMsg.ascii(),errLine);
exec->setException(err);
return err;
}
ScopeChain scopeChain;
scopeChain.push(exec->interpreter()->globalObject().imp());
FunctionBodyNode *bodyNode = progNode;
FunctionImp *fimp = new DeclaredFunctionImp(exec, Identifier::null(), bodyNode,
scopeChain);
Object ret(fimp);
int len = p.size();
const UChar *c = p.data();
int i = 0, params = 0;
UString param;
while (i < len) {
while (*c == ' ' && i < len)
c++, i++;
if (Lexer::isIdentLetter(c->uc)) { param = UString(c, 1);
c++, i++;
while (i < len && (Lexer::isIdentLetter(c->uc) ||
Lexer::isDecimalDigit(c->uc))) {
param += UString(c, 1);
c++, i++;
}
while (i < len && *c == ' ')
c++, i++;
if (i == len) {
fimp->addParameter(Identifier(param));
params++;
break;
} else if (*c == ',') {
fimp->addParameter(Identifier(param));
params++;
c++, i++;
continue;
} }
Object err = Error::create(exec,SyntaxError,
I18N_NOOP("Syntax error in parameter list"),
-1);
exec->setException(err);
return err;
}
List consArgs;
Object objCons = exec->interpreter()->builtinObject();
Object prototype = objCons.construct(exec,List::empty());
prototype.put(exec, constructorPropertyName, Value(fimp), DontEnum|DontDelete|ReadOnly);
fimp->put(exec, prototypePropertyName, prototype, DontEnum|DontDelete|ReadOnly);
return ret;
}
bool FunctionObjectImp::implementsCall() const
{
return true;
}
Value FunctionObjectImp::call(ExecState *exec, Object &, const List &args)
{
return construct(exec,args);
}