Skip to content

Commit

Permalink
move more of evaluateJavaScript() into evaluateJavaScriptString()
Browse files Browse the repository at this point in the history
  • Loading branch information
mnutt committed Jul 6, 2023
1 parent 6489a48 commit 9223914
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
35 changes: 1 addition & 34 deletions Source/WebKitLegacy/qt/Api/qwebelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <WebCore/NamedNodeMap.h>
#include <WebCore/QStyleHelpers.h>
#include <WebCore/RenderElement.h>
#include <WebCore/ScriptController.h>
#include <WebCore/StaticNodeList.h>
#include <WebCore/markup.h>
#include <WebCore/qt_runtime.h>
Expand Down Expand Up @@ -673,22 +672,6 @@ QWebFrame *QWebElement::webFrame() const
return frameAdapter->apiHandle();
}

static bool setupScriptContext(WebCore::Element* element, JSC::JSGlobalObject*& lexicalGlobalObject)
{
if (!element)
return false;

LocalFrame* frame = element->document().frame();
if (!frame)
return false;

lexicalGlobalObject = frame->script().globalObject(mainThreadNormalWorld())->globalObject();
if (!lexicalGlobalObject)
return false;

return true;
}

/*!
Executes \a scriptSource with this element as \c this object
and returns the result of the last executed statement.
Expand All @@ -704,23 +687,7 @@ QVariant QWebElement::evaluateJavaScript(const QString& scriptSource)
if (scriptSource.isEmpty())
return QVariant();

JSC::JSGlobalObject* lexicalGlobalObject = nullptr;

if (!setupScriptContext(m_element, lexicalGlobalObject))
return QVariant();

JSC::JSLockHolder lock(lexicalGlobalObject);
RefPtr<Element> protect = m_element;

JSC::JSValue thisValue = toJS(lexicalGlobalObject, toJSLocalDOMWindow(m_element->document().frame(), currentWorld(*lexicalGlobalObject)), m_element);
if (!thisValue)
return QVariant();

JSValueRef evaluationResultRef = evaluateJavaScriptString(lexicalGlobalObject, scriptSource, thisValue);

int distance = 0;
JSValueRef* ignoredException = 0;
return JSC::Bindings::convertValueToQVariant(toRef(lexicalGlobalObject), evaluationResultRef, QMetaType::Void, &distance, ignoredException);
return evaluateJavaScriptString(scriptSource, m_element);
}

/*!
Expand Down
46 changes: 42 additions & 4 deletions Source/WebKitLegacy/qt/WebCoreSupport/EvaluateJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,55 @@
#include <JavaScriptCore/JSValueRef.h>
#include <JavaScriptCore/Completion.h>
#include <wtf/NakedPtr.h>
#include <WebCore/ElementInlines.h>
#include <WebCore/ScriptSourceCode.h>
#include <WebCore/ScriptController.h>
#include <WebCore/JSElement.h>
#include <WebCore/qt_runtime.h>
#include <QVariant>

using namespace WebCore;

static bool setupScriptContext(Element* element, JSC::JSGlobalObject*& lexicalGlobalObject)
{
if (!element)
return false;

LocalFrame* frame = element->document().frame();
if (!frame)
return false;

lexicalGlobalObject = frame->script().globalObject(mainThreadNormalWorld())->globalObject();
if (!lexicalGlobalObject)
return false;

return true;
}

// Extracted from qwebelement.cpp because that file needs rtti and WebCore::ScriptSourceCode has rtti disabled
JSValueRef evaluateJavaScriptString(JSC::JSGlobalObject* lexicalGlobalObject, const String& scriptSource, const JSC::JSValue& thisValue)
QVariant evaluateJavaScriptString(const String& scriptSource, Element* element)
{
WebCore::ScriptSourceCode sourceCode(scriptSource);
JSC::JSGlobalObject* lexicalGlobalObject = nullptr;

if (!setupScriptContext(element, lexicalGlobalObject))
return QVariant();

JSC::JSLockHolder lock(lexicalGlobalObject);

JSC::JSValue thisValue = toJS(lexicalGlobalObject, toJSLocalDOMWindow(element->document().frame(), currentWorld(*lexicalGlobalObject)), element);
if (!thisValue)
return QVariant();

ScriptSourceCode sourceCode(scriptSource);

NakedPtr<JSC::Exception> evaluationException;
JSC::JSValue evaluationResult = JSC::evaluate(lexicalGlobalObject, sourceCode.jsSourceCode(), thisValue, evaluationException);
if (evaluationException)
return JSValueMakeUndefined(toRef(lexicalGlobalObject));
return QVariant();

JSValueRef evaluationResultRef = toRef(lexicalGlobalObject, evaluationResult);

return toRef(lexicalGlobalObject, evaluationResult);
int distance = 0;
JSValueRef* ignoredException = 0;
return JSC::Bindings::convertValueToQVariant(toRef(lexicalGlobalObject), evaluationResultRef, QMetaType::Void, &distance, ignoredException);
}
6 changes: 3 additions & 3 deletions Source/WebKitLegacy/qt/WebCoreSupport/EvaluateJS.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#pragma once

#include <JavaScriptCore/JSGlobalObject.h>
#include <JavaScriptCore/JSValueRef.h>
#include <WebCore/Element.h>
#include <QVariant>

JSValueRef evaluateJavaScriptString(JSC::JSGlobalObject* lexicalGlobalObject, const String& scriptSource, const JSC::JSValue& thisValue);
QVariant evaluateJavaScriptString(const String& scriptSource, WebCore::Element* element);

0 comments on commit 9223914

Please sign in to comment.