khtml Library API Documentation

kjs_binding.h

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
00005  *  Copyright (C) 2003 Apple Computer, Inc.
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Library General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Library General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public
00018  *  License along with this library; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 
00022 #ifndef _KJS_BINDING_H_
00023 #define _KJS_BINDING_H_
00024 
00025 #include <kjs/interpreter.h>
00026 #include <kjs/function_object.h> 
00027 
00028 #include <dom/dom_node.h>
00029 #include <qvariant.h>
00030 #include <qptrdict.h>
00031 #include <kurl.h>
00032 #include <kjs/lookup.h>
00033 
00034 namespace KParts {
00035   class ReadOnlyPart;
00036   class LiveConnectExtension;
00037 }
00038 
00039 namespace khtml {
00040   class ChildFrame;
00041 }
00042 
00043 namespace KJS {
00044 
00050   class DOMObject : public ObjectImp {
00051   public:
00052     DOMObject(const Object &proto) : ObjectImp(proto) {}
00053     virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00054     virtual Value tryGet(ExecState *exec, const Identifier &propertyName) const
00055       { return ObjectImp::get(exec, propertyName); }
00056 
00057     virtual void put(ExecState *exec, const Identifier &propertyName,
00058                      const Value &value, int attr = None);
00059     virtual void tryPut(ExecState *exec, const Identifier &propertyName,
00060                         const Value& value, int attr = None)
00061       { ObjectImp::put(exec,propertyName,value,attr); }
00062 
00063     virtual UString toString(ExecState *exec) const;
00064   };
00065 
00071   class DOMFunction : public InternalFunctionImp {
00072   public:
00073     DOMFunction(ExecState* exec) : InternalFunctionImp(
00074       static_cast<FunctionPrototypeImp*>(exec->interpreter()->builtinFunctionPrototype().imp())
00075       ) {}
00076     virtual Value get(ExecState *exec, const Identifier &propertyName) const;
00077     virtual Value tryGet(ExecState *exec, const Identifier &propertyName) const
00078       { return ObjectImp::get(exec, propertyName); }
00079 
00080     virtual bool implementsCall() const { return true; }
00081     virtual Value call(ExecState *exec, Object &thisObj, const List &args);
00082 
00083     virtual Value tryCall(ExecState *exec, Object &thisObj, const List&args)
00084       { return ObjectImp::call(exec, thisObj, args); }
00085     virtual bool toBoolean(ExecState *) const { return true; }
00086   };
00087 
00093   class ScriptInterpreter : public Interpreter
00094   {
00095   public:
00096     ScriptInterpreter( const Object &global, khtml::ChildFrame* frame );
00097     virtual ~ScriptInterpreter();
00098 
00099     DOMObject* getDOMObject( void* objectHandle ) const {
00100       return m_domObjects[objectHandle];
00101     }
00102     void putDOMObject( void* objectHandle, DOMObject* obj ) {
00103       m_domObjects.insert( objectHandle, obj );
00104     }
00105     bool deleteDOMObject( void* objectHandle ) {
00106       return m_domObjects.remove( objectHandle );
00107     }
00108     void clear() {
00109       m_domObjects.clear();
00110     }
00114     static void forgetDOMObject( void* objectHandle );
00115 
00119     virtual void mark();
00120     KParts::ReadOnlyPart* part() const;
00121 
00122     virtual int rtti() { return 1; }
00123 
00127     void setCurrentEvent( DOM::Event *evt ) { m_evt = evt; }
00128     void setInlineCode( bool inlineCode ) { m_inlineCode = inlineCode; }
00129     void setProcessingTimerCallback( bool timerCallback ) { m_timerCallback = timerCallback; }
00133     bool isWindowOpenAllowed() const;
00134 
00135   private:
00136     khtml::ChildFrame* m_frame;
00137     QPtrDict<DOMObject> m_domObjects;
00138     DOM::Event *m_evt;
00139     bool m_inlineCode;
00140     bool m_timerCallback;
00141   };
00145   template<class DOMObj, class KJSDOMObj>
00146   inline Value cacheDOMObject(ExecState *exec, DOMObj domObj)
00147   {
00148     DOMObject *ret;
00149     if (domObj.isNull())
00150       return Null();
00151     ScriptInterpreter* interp = static_cast<ScriptInterpreter *>(exec->interpreter());
00152     if ((ret = interp->getDOMObject(domObj.handle())))
00153       return Value(ret);
00154     else {
00155       ret = new KJSDOMObj(exec, domObj);
00156       interp->putDOMObject(domObj.handle(),ret);
00157       return Value(ret);
00158     }
00159   }
00160 
00164   DOM::Node toNode(const Value&);
00168   Value getString(DOM::DOMString s);
00169 
00173   QVariant ValueToVariant(ExecState* exec, const Value& val);
00174 
00179   template <class FuncImp, class ThisImp, class ParentImp>
00180   inline Value DOMObjectLookupGet(ExecState *exec, const Identifier &propertyName,
00181                                   const HashTable* table, const ThisImp* thisObj)
00182   {
00183     const HashEntry* entry = Lookup::findEntry(table, propertyName);
00184 
00185     if (!entry) // not found, forward to parent
00186       return thisObj->ParentImp::tryGet(exec, propertyName);
00187 
00188     if (entry->attr & Function) {
00189       return lookupOrCreateFunction<FuncImp>(exec, propertyName, thisObj, entry->value, entry->params, entry->attr);
00190     }
00191     return thisObj->getValueProperty(exec, entry->value);
00192   }
00193 
00198   template <class ThisImp, class ParentImp>
00199   inline Value DOMObjectLookupGetValue(ExecState *exec, const Identifier &propertyName,
00200                                        const HashTable* table, const ThisImp* thisObj)
00201   {
00202     const HashEntry* entry = Lookup::findEntry(table, propertyName);
00203 
00204     if (!entry) // not found, forward to parent
00205       return thisObj->ParentImp::tryGet(exec, propertyName);
00206 
00207     if (entry->attr & Function)
00208       fprintf(stderr, "Function bit set! Shouldn't happen in lookupValue!\n" );
00209     return thisObj->getValueProperty(exec, entry->value);
00210   }
00211 
00216   template <class ThisImp, class ParentImp>
00217   inline void DOMObjectLookupPut(ExecState *exec, const Identifier &propertyName,
00218                                  const Value& value, int attr,
00219                                  const HashTable* table, ThisImp* thisObj)
00220   {
00221     const HashEntry* entry = Lookup::findEntry(table, propertyName);
00222 
00223     if (!entry) // not found: forward to parent
00224       thisObj->ParentImp::tryPut(exec, propertyName, value, attr);
00225     else if (entry->attr & Function) // function: put as override property
00226       thisObj->ObjectImp::put(exec, propertyName, value, attr);
00227     else if (entry->attr & ReadOnly) // readonly! Can't put!
00228 #ifdef KJS_VERBOSE
00229       fprintf(stderr,"WARNING: Attempt to change value of readonly property '%s'\n",propertyName.ascii());
00230 #else
00231     ; // do nothing
00232 #endif
00233     else
00234       thisObj->putValueProperty(exec, entry->value, value, attr);
00235   }
00236 
00237   // Modified version of IMPLEMENT_PROTOFUNC, to use DOMFunction and tryCall
00238 #define IMPLEMENT_PROTOFUNC_DOM(ClassFunc) \
00239   namespace KJS { \
00240   class ClassFunc : public DOMFunction { \
00241   public: \
00242     ClassFunc(ExecState *exec, int i, int len) \
00243        : DOMFunction( exec ), id(i) { \
00244        Value protect(this); \
00245        put(exec,lengthPropertyName,Number(len),DontDelete|ReadOnly|DontEnum); \
00246     } \
00247  \
00248     virtual Value tryCall(ExecState *exec, Object &thisObj, const List &args); \
00249   private: \
00250     int id; \
00251   }; \
00252   }
00253 
00254   Value getLiveConnectValue(KParts::LiveConnectExtension *lc, const QString & name, const int type, const QString & value, int id);
00255 
00256 } // namespace
00257 
00258 #endif
KDE Logo
This file is part of the documentation for khtml Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 7 22:11:49 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003