#ifndef _RUNTIME_ROOT_H_
#define _RUNTIME_ROOT_H_
#include <JavaScriptCore/interpreter.h>
#include <JavaScriptCore/object.h>
#include <JavaScriptCore/jni_jsobject.h>
namespace KJS {
namespace Bindings {
class RootObject;
typedef RootObject *(*FindRootObjectForNativeHandleFunctionPtr)(void *);
extern CFMutableDictionaryRef findReferenceDictionary(ObjectImp *imp);
extern const Bindings::RootObject *rootForImp (ObjectImp *imp);
extern const Bindings::RootObject *KJS::Bindings::rootForInterpreter (KJS::Interpreter *interpreter);
extern void addNativeReference (const Bindings::RootObject *root, ObjectImp *imp);
extern void removeNativeReference (ObjectImp *imp);
class RootObject
{
friend class JSObject;
public:
RootObject (const void *nativeHandle) : _nativeHandle(nativeHandle), _imp(0), _interpreter(0) {}
~RootObject (){
#if !USE_CONSERVATIVE_GC
_imp->deref();
#endif
#if USE_CONSERVATIVE_GC | TEST_CONSERVATIVE_GC
gcUnprotect(_imp);
#endif
}
void setRootObjectImp (KJS::ObjectImp *i) {
_imp = i;
#if !USE_CONSERVATIVE_GC
_imp->ref();
#endif
#if USE_CONSERVATIVE_GC | TEST_CONSERVATIVE_GC
gcProtect(_imp);
#endif
}
KJS::ObjectImp *rootObjectImp() const { return _imp; }
void setInterpreter (KJS::Interpreter *i);
KJS::Interpreter *interpreter() const { return _interpreter; }
void removeAllNativeReferences ();
static void setFindRootObjectForNativeHandleFunction(FindRootObjectForNativeHandleFunctionPtr aFunc);
static FindRootObjectForNativeHandleFunctionPtr findRootObjectForNativeHandleFunction() {
return _findRootObjectForNativeHandleFunctionPtr;
}
static CFRunLoopRef runLoop() { return _runLoop; }
static CFRunLoopSourceRef performJavaScriptSource() { return _performJavaScriptSource; }
static void dispatchToJavaScriptThread(JSObjectCallContext *context);
const void *nativeHandle() const { return _nativeHandle; }
private:
const void *_nativeHandle;
KJS::ObjectImp *_imp;
KJS::Interpreter *_interpreter;
static FindRootObjectForNativeHandleFunctionPtr _findRootObjectForNativeHandleFunctionPtr;
static CFRunLoopRef _runLoop;
static CFRunLoopSourceRef _performJavaScriptSource;
};
}
}
#endif