diff --git a/src/kjs/api/kjsobject.h b/src/kjs/api/kjsobject.h --- a/src/kjs/api/kjsobject.h +++ b/src/kjs/api/kjsobject.h @@ -135,6 +135,18 @@ */ void setProperty(KJSContext *ctx, const QString &name, const KJSObject &value); + /** + * Calls this KJObject as a function with no arguments + * using this of the Context (ctx) as the this object. This operation + * might throw an exception. + */ + KJSObject call(KJSContext *ctx); + /** + * Calls this KJObject as a function using this of the Context (ctx) as the this object + * and passing the arguments as parameters to the function. This operation + * might throw an exception. + */ + KJSObject call(KJSContext *ctx, const QList &arguments); /** * @overload */ diff --git a/src/kjs/api/kjsobject.cpp b/src/kjs/api/kjsobject.cpp --- a/src/kjs/api/kjsobject.cpp +++ b/src/kjs/api/kjsobject.cpp @@ -203,6 +203,35 @@ return KJSObject(JSVALUE_HANDLE(res)); } +KJSObject KJSObject::call(KJSContext *ctx) +{ + QList emptyArgs; + return call(ctx, emptyArgs); +} + +KJSObject KJSObject::call(KJSContext *ctx, const QList &arguments) +{ + JSValue *v = JSVALUE(this); + assert(v); + + ExecState *exec = EXECSTATE(ctx); + JSObject *o = v->toObject(exec); + + if( !o->isFunctionType() ) + return KJSUndefined(); + + JSObject *thisObject = exec->thisValue(); + + KJS::List args = List(); + foreach(KJSObject arg, arguments) + { + args.append(JSVALUE(&arg)); + } + JSValue *res = o->call(exec, thisObject, args); + + return KJSObject(JSVALUE_HANDLE(res)); +} + void KJSObject::setProperty(KJSContext *ctx, const QString &name, const KJSObject &value) {