JSLazyEventListener.cpp [plain text]
#include "config.h"
#include "JSLazyEventListener.h"
#include "Frame.h"
#include "JSNode.h"
#include <runtime/FunctionConstructor.h>
#include <runtime/JSFunction.h>
#include <runtime/JSLock.h>
#include <wtf/RefCountedLeakCounter.h>
using namespace JSC;
namespace WebCore {
#ifndef NDEBUG
static WTF::RefCountedLeakCounter eventListenerCounter("JSLazyEventListener");
#endif
JSLazyEventListener::JSLazyEventListener(const String& functionName, const String& eventParameterName, const String& code, Node* node, const String& sourceURL, int lineNumber, JSObject* wrapper, DOMWrapperWorld* isolatedWorld)
: JSEventListener(0, wrapper, true, isolatedWorld)
, m_functionName(functionName)
, m_eventParameterName(eventParameterName)
, m_code(code)
, m_sourceURL(sourceURL)
, m_lineNumber(lineNumber)
, m_originalNode(node)
{
if (m_lineNumber == 0)
m_lineNumber = 1;
#ifndef NDEBUG
eventListenerCounter.increment();
#endif
}
JSLazyEventListener::~JSLazyEventListener()
{
#ifndef NDEBUG
eventListenerCounter.decrement();
#endif
}
JSObject* JSLazyEventListener::initializeJSFunction(ScriptExecutionContext* executionContext) const
{
ASSERT(executionContext);
ASSERT(executionContext->isDocument());
if (!executionContext)
return 0;
Frame* frame = static_cast<Document*>(executionContext)->frame();
if (!frame)
return 0;
ScriptController* scriptController = frame->script();
if (!scriptController->canExecuteScripts(AboutToExecuteScript))
return 0;
JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(executionContext, isolatedWorld());
if (!globalObject)
return 0;
if (executionContext->isDocument()) {
JSDOMWindow* window = static_cast<JSDOMWindow*>(globalObject);
Frame* frame = window->impl()->frame();
if (!frame)
return 0;
ScriptController* script = frame->script();
if (!script->canExecuteScripts(AboutToExecuteScript) || script->isPaused())
return 0;
}
ExecState* exec = globalObject->globalExec();
MarkedArgumentBuffer args;
args.append(jsNontrivialString(exec, stringToUString(m_eventParameterName)));
args.append(jsString(exec, m_code));
JSObject* jsFunction = constructFunction(exec, args, Identifier(exec, stringToUString(m_functionName)), stringToUString(m_sourceURL), m_lineNumber); if (exec->hadException()) {
exec->clearException();
return 0;
}
JSFunction* listenerAsFunction = static_cast<JSFunction*>(jsFunction);
if (m_originalNode) {
if (!wrapper()) {
JSLock lock(SilenceAssertionsOnly);
setWrapper(asObject(toJS(globalObject->globalExec(), globalObject, m_originalNode)));
}
ScopeChain scope = listenerAsFunction->scope();
static_cast<JSNode*>(wrapper())->pushEventHandlerScope(exec, scope);
listenerAsFunction->setScope(scope);
}
m_functionName = String();
m_code = String();
m_eventParameterName = String();
m_sourceURL = String();
return jsFunction;
}
}