JSDocumentCustom.cpp [plain text]
#include "config.h"
#include "JSDocument.h"
#include "ExceptionCode.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "HTMLDocument.h"
#include "JSCanvasRenderingContext2D.h"
#include "JSDOMConvert.h"
#include "JSDOMWindowCustom.h"
#include "JSHTMLDocument.h"
#include "JSLocation.h"
#include "JSXMLDocument.h"
#include "Location.h"
#include "NodeTraversal.h"
#include "SVGDocument.h"
#include "ScriptController.h"
#include "TouchList.h"
#include "XMLDocument.h"
#include <wtf/GetPtr.h>
#if ENABLE(WEBGL)
#include "JSWebGLRenderingContextBase.h"
#endif
#if ENABLE(TOUCH_EVENTS)
#include "JSTouch.h"
#include "JSTouchList.h"
#endif
using namespace JSC;
namespace WebCore {
static inline JSValue createNewDocumentWrapper(ExecState& state, JSDOMGlobalObject& globalObject, Ref<Document>&& passedDocument)
{
auto& document = passedDocument.get();
JSObject* wrapper;
if (document.isHTMLDocument())
wrapper = createWrapper<HTMLDocument>(&globalObject, WTFMove(passedDocument));
else if (document.isXMLDocument())
wrapper = createWrapper<XMLDocument>(&globalObject, WTFMove(passedDocument));
else
wrapper = createWrapper<Document>(&globalObject, WTFMove(passedDocument));
reportMemoryForDocumentIfFrameless(state, document);
return wrapper;
}
JSObject* cachedDocumentWrapper(ExecState& state, JSDOMGlobalObject& globalObject, Document& document)
{
if (auto* wrapper = getCachedWrapper(globalObject.world(), document))
return wrapper;
auto* window = document.domWindow();
if (!window)
return nullptr;
return getCachedWrapper(toJSDOMWindow(toJS(&state, *window))->world(), document);
}
void reportMemoryForDocumentIfFrameless(ExecState& state, Document& document)
{
if (document.frame())
return;
size_t memoryCost = 0;
for (Node* node = &document; node; node = NodeTraversal::next(*node))
memoryCost += node->approximateMemoryCost();
state.heap()->deprecatedReportExtraMemory(memoryCost);
}
JSValue toJSNewlyCreated(ExecState* state, JSDOMGlobalObject* globalObject, Ref<Document>&& document)
{
return createNewDocumentWrapper(*state, *globalObject, WTFMove(document));
}
JSValue toJS(ExecState* state, JSDOMGlobalObject* globalObject, Document& document)
{
if (auto* wrapper = cachedDocumentWrapper(*state, *globalObject, document))
return wrapper;
return toJSNewlyCreated(state, globalObject, Ref<Document>(document));
}
#if ENABLE(TOUCH_EVENTS)
JSValue JSDocument::createTouchList(ExecState& state)
{
VM& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto touchList = TouchList::create();
for (size_t i = 0; i < state.argumentCount(); ++i) {
auto* item = JSTouch::toWrapped(state.uncheckedArgument(i));
if (!item)
return JSValue::decode(throwArgumentTypeError(state, scope, i, "touches", "Document", "createTouchList", "Touch"));
touchList->append(*item);
}
return toJSNewlyCreated(&state, globalObject(), WTFMove(touchList));
}
#endif
JSValue JSDocument::getCSSCanvasContext(JSC::ExecState& state)
{
VM& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (UNLIKELY(state.argumentCount() < 4))
return throwException(&state, scope, createNotEnoughArgumentsError(&state));
auto contextId = state.uncheckedArgument(0).toWTFString(&state);
RETURN_IF_EXCEPTION(scope, JSValue());
auto name = state.uncheckedArgument(1).toWTFString(&state);
RETURN_IF_EXCEPTION(scope, JSValue());
auto width = convert<IDLLong>(state, state.uncheckedArgument(2), IntegerConversionConfiguration::Normal);
RETURN_IF_EXCEPTION(scope, JSValue());
auto height = convert<IDLLong>(state, state.uncheckedArgument(3), IntegerConversionConfiguration::Normal);
RETURN_IF_EXCEPTION(scope, JSValue());
auto* context = wrapped().getCSSCanvasContext(WTFMove(contextId), WTFMove(name), WTFMove(width), WTFMove(height));
if (!context)
return jsNull();
#if ENABLE(WEBGL)
if (is<WebGLRenderingContextBase>(*context))
return toJS(&state, globalObject(), downcast<WebGLRenderingContextBase>(*context));
#endif
return toJS(&state, globalObject(), downcast<CanvasRenderingContext2D>(*context));
}
void JSDocument::visitAdditionalChildren(SlotVisitor& visitor)
{
visitor.addOpaqueRoot(static_cast<ScriptExecutionContext*>(&wrapped()));
}
}