#ifndef _RUNTIME_ROOT_H_
#define _RUNTIME_ROOT_H_
#include <JavaScriptCore/interpreter.h>
#include <JavaScriptCore/object.h>
#include <JavaScriptCore/jni_jsobject.h>
#include <JavaScriptCore/protect.h>
namespace KJS {
namespace Bindings {
class RootObject;
typedef RootObject *(*FindRootObjectForNativeHandleFunctionPtr)(void *);
extern CFMutableDictionaryRef findReferenceDictionary(ObjectImp *imp);
extern const RootObject *rootForImp (ObjectImp *imp);
extern const RootObject *rootForInterpreter (Interpreter *interpreter);
extern void addNativeReference (const 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
InterpreterLock lock;
gcUnprotect(_imp);
#endif
}
void setRootObjectImp (ObjectImp *i) {
#if USE_CONSERVATIVE_GC | TEST_CONSERVATIVE_GC
InterpreterLock lock;
#endif
_imp = i;
#if !USE_CONSERVATIVE_GC
_imp->ref();
#endif
#if USE_CONSERVATIVE_GC | TEST_CONSERVATIVE_GC
gcProtect(_imp);
#endif
}
ObjectImp *rootObjectImp() const { return _imp; }
void setInterpreter (Interpreter *i);
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;
ObjectImp *_imp;
Interpreter *_interpreter;
static FindRootObjectForNativeHandleFunctionPtr _findRootObjectForNativeHandleFunctionPtr;
static CFRunLoopRef _runLoop;
static CFRunLoopSourceRef _performJavaScriptSource;
};
}
}
#endif