#ifndef ScriptState_h
#define ScriptState_h
#include "bindings/v8/ScopedPersistent.h"
#include <v8.h>
#include "wtf/Noncopyable.h"
namespace WebCore {
class DOMWindow;
class DOMWrapperWorld;
class LocalFrame;
class ExecutionContext;
class WorkerGlobalScope;
class ScriptState {
WTF_MAKE_NONCOPYABLE(ScriptState);
public:
bool hadException() { return !m_exception.isEmpty(); }
void setException(v8::Local<v8::Value> exception)
{
m_exception.set(m_isolate, exception);
}
v8::Local<v8::Value> exception() { return m_exception.newLocal(m_isolate); }
void clearException() { m_exception.clear(); }
v8::Local<v8::Context> context() const
{
return m_context.newLocal(m_isolate);
}
v8::Isolate* isolate()
{
return m_isolate;
}
DOMWindow* domWindow() const;
ExecutionContext* executionContext() const;
bool evalEnabled() const;
void setEvalEnabled(bool);
static ScriptState* forContext(v8::Handle<v8::Context>);
static ScriptState* current();
protected:
ScriptState()
: m_isolate(v8::Isolate::GetCurrent())
{
}
~ScriptState();
private:
friend ScriptState* mainWorldScriptState(LocalFrame*);
explicit ScriptState(v8::Handle<v8::Context>);
static void setWeakCallback(const v8::WeakCallbackData<v8::Context, ScriptState>&);
ScopedPersistent<v8::Value> m_exception;
ScopedPersistent<v8::Context> m_context;
v8::Isolate* m_isolate;
};
class ScriptStateProtectedPtr {
WTF_MAKE_NONCOPYABLE(ScriptStateProtectedPtr);
public:
ScriptStateProtectedPtr()
: m_scriptState(0)
{
}
ScriptStateProtectedPtr(ScriptState* scriptState)
: m_scriptState(scriptState)
{
v8::HandleScope handleScope(scriptState->isolate());
m_context.set(scriptState->isolate(), scriptState->context());
}
ScriptState* get() const { return m_scriptState; }
private:
ScriptState* m_scriptState;
ScopedPersistent<v8::Context> m_context;
};
ScriptState* mainWorldScriptState(LocalFrame*);
ScriptState* scriptStateFromWorkerGlobalScope(WorkerGlobalScope*);
}
#endif