#ifndef KJS_BINDING_H_
#define KJS_BINDING_H_
#include <kjs/lookup.h>
#if __APPLE__
#include <JavaScriptCore/runtime.h>
#endif
namespace WebCore {
class Document;
class Event;
class Frame;
class Node;
class String;
typedef int ExceptionCode;
}
namespace KJS {
class DOMObject : public JSObject {
protected:
DOMObject() : JSObject(false) {}
public:
virtual UString toString(ExecState *exec) const;
};
class DOMNode;
class ScriptInterpreter : public Interpreter
{
public:
ScriptInterpreter(JSObject *global, WebCore::Frame *frame);
static DOMObject* getDOMObject(void* objectHandle);
static void putDOMObject(void* objectHandle, DOMObject* obj);
static void forgetDOMObject(void* objectHandle);
static DOMNode *getDOMNodeForDocument(WebCore::Document *document, WebCore::Node *node);
static void putDOMNodeForDocument(WebCore::Document *document, WebCore::Node *nodeHandle, DOMNode *nodeWrapper);
static void forgetDOMNodeForDocument(WebCore::Document *document, WebCore::Node *node);
static void forgetAllDOMNodesForDocument(WebCore::Document *document);
static void updateDOMNodeDocument(WebCore::Node *nodeHandle, WebCore::Document *oldDoc, WebCore::Document *newDoc);
static void markDOMNodesForDocument(WebCore::Document*);
WebCore::Frame* frame() const { return m_frame; }
virtual int rtti() { return 1; }
void setCurrentEvent( WebCore::Event *evt ) { m_evt = evt; }
void setInlineCode( bool inlineCode ) { m_inlineCode = inlineCode; }
void setProcessingTimerCallback( bool timerCallback ) { m_timerCallback = timerCallback; }
bool wasRunByUserGesture() const;
virtual void mark(bool currentThreadIsMainThread);
virtual ExecState *globalExec();
WebCore::Event *getCurrentEvent() const { return m_evt; }
virtual bool isGlobalObject(JSValue *v);
virtual Interpreter *interpreterForGlobalObject (const JSValue *imp);
virtual bool isSafeScript (const Interpreter *target);
virtual void *createLanguageInstanceForValue (ExecState *exec, int language, JSObject *value, const Bindings::RootObject *origin, const Bindings::RootObject *current);
void *createObjcInstanceForValue (ExecState *exec, JSObject *value, const Bindings::RootObject *origin, const Bindings::RootObject *current);
bool shouldStopExecution();
virtual bool shouldInterruptScript() const;
protected:
virtual ~ScriptInterpreter() { }
private:
WebCore::Frame* m_frame;
WebCore::Event *m_evt;
bool m_inlineCode;
bool m_timerCallback;
};
template<class DOMObj, class KJSDOMObj>
inline JSValue *cacheDOMObject(ExecState *exec, DOMObj *domObj)
{
if (!domObj)
return jsNull();
ScriptInterpreter *interp = static_cast<ScriptInterpreter *>(exec->dynamicInterpreter());
if (DOMObject *ret = interp->getDOMObject(domObj))
return ret;
DOMObject *ret = new KJSDOMObj(exec, domObj);
interp->putDOMObject(domObj, ret);
return ret;
}
void setDOMException(ExecState*, WebCore::ExceptionCode);
class DOMExceptionTranslator {
public:
explicit DOMExceptionTranslator(ExecState *exec) : m_exec(exec), m_code(0) { }
~DOMExceptionTranslator() { setDOMException(m_exec, m_code); }
operator WebCore::ExceptionCode&() { return m_code; }
private:
ExecState *m_exec;
WebCore::ExceptionCode m_code;
};
JSValue *jsStringOrNull(const WebCore::String&);
JSValue *jsStringOrUndefined(const WebCore::String&);
JSValue *jsStringOrFalse(const WebCore::String&);
WebCore::String valueToStringWithNullCheck(ExecState* exec, JSValue* val);
bool valueToBooleanTreatUndefinedAsTrue(ExecState* exec, JSValue* val);
template <typename T> inline JSValue* toJS(ExecState* exec, PassRefPtr<T> ptr) { return toJS(exec, ptr.get()); }
}
#endif