Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjipeng committed Nov 5, 2024
1 parent eb1e35b commit 602591e
Show file tree
Hide file tree
Showing 11 changed files with 894 additions and 32 deletions.
4 changes: 4 additions & 0 deletions src/core/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ bool _global_initialize(void)
{
//FIXME: add setup parameters
_globalData.runtime = JS_NewRuntime();
_globalData.domObjects = new DOMObjectMap;
_globalData.domNodesPerDoc = new NodePerDocMap;
return true;
}

void _global_shutdown(void)
{
JS_FreeRuntime(_globalData.runtime);
delete _globalData.domNodesPerDoc;
delete _globalData.domObjects;
}

}
5 changes: 4 additions & 1 deletion src/core/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@

#include <stdint.h>
#include <stddef.h>

#include <quickjs.h>

#include "qjs_dom_map.h"

namespace mescal {

#define GLOBAL() mescal::_global()
Expand All @@ -40,6 +41,8 @@ namespace mescal {
typedef struct {
// quick javascript runtime
JSRuntime* runtime;
DOMObjectMap* domObjects;
NodePerDocMap* domNodesPerDoc;
} GlobalData;

const GlobalData* const _global(void);
Expand Down
125 changes: 125 additions & 0 deletions src/webcore/bindings/qjs/QJSElementCustom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2024, Zhang Ji Peng <onecoolx@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"

#if ENABLE(QJS)
#include "QJSElement.h"

#include "Attr.h"
#include "Document.h"
#include "Element.h"
#include "ExceptionCode.h"
#include "HTMLFrameElementBase.h"
#include "HTMLNames.h"
#include <text/String.h>
#include "qjs_binding.h"
#include "qjs_dom.h"

using namespace QJS;

namespace WebCore {

using namespace HTMLNames;

static inline bool allowSettingSrcToJavascriptURL(JSContext* ctx, Element* element, String name, String value)
{
if ((element->hasTagName(iframeTag) || element->hasTagName(frameTag)) && equalIgnoringCase(name, "src")
&& value.startsWith("javascript:", false)) {
HTMLFrameElementBase* frame = static_cast<HTMLFrameElementBase*>(element);
if (!checkNodeSecurity(ctx, frame->contentDocument()))
return false;
}
return true;
}

JSValue JSElement::setAttribute(JSContext *ctx, int argc, JSValueConst *argv, Element *imp)
{
ExceptionCode ec = 0;
String name = valueToString(ctx, argv[0]);
String value = valueToString(ctx, argv[1]);

if (!allowSettingSrcToJavascriptURL(ctx, imp, name, value))
return JS_UNDEFINED;

imp->setAttribute(name, value, ec);
setDOMException(ctx, ec);
return JS_UNDEFINED;
}

JSValue JSElement::setAttributeNode(JSContext *ctx, int argc, JSValueConst *argv, Element *imp)
{
ExceptionCode ec = 0;
bool newAttrOk;
Attr* newAttr = toAttr(argv[0], newAttrOk);
if (!newAttrOk) {
setDOMException(ctx, TYPE_MISMATCH_ERR);
return JS_UNDEFINED;
}

if (!allowSettingSrcToJavascriptURL(ctx, imp, newAttr->name(), newAttr->value()))
return JS_UNDEFINED;

JSValue result = toJS(ctx, QJS::getPtr(imp->setAttributeNode(newAttr, ec)));
setDOMException(ctx, ec);
return result;
}

JSValue JSElement::setAttributeNS(JSContext *ctx, int argc, JSValueConst *argv, Element *imp)
{
ExceptionCode ec = 0;
String namespaceURI = valueToStringWithNullCheck(ctx, argv[0]);
String qualifiedName = valueToString(ctx, argv[1]);
String value = valueToString(ctx, argv[2]);

if (!allowSettingSrcToJavascriptURL(ctx, imp, qualifiedName, value))
return JS_UNDEFINED;

imp->setAttributeNS(namespaceURI, qualifiedName, value, ec);
setDOMException(ctx, ec);
return JS_UNDEFINED;
}

JSValue JSElement::setAttributeNodeNS(JSContext *ctx, int argc, JSValueConst *argv, Element *imp)
{
ExceptionCode ec = 0;
bool newAttrOk;
Attr* newAttr = toAttr(argv[0], newAttrOk);
if (!newAttrOk) {
setDOMException(ctx, TYPE_MISMATCH_ERR);
return JS_UNDEFINED;
}

if (!allowSettingSrcToJavascriptURL(ctx, imp, newAttr->name(), newAttr->value()))
return JS_UNDEFINED;

JSValue result = toJS(ctx, QJS::getPtr(imp->setAttributeNodeNS(newAttr, ec)));
setDOMException(ctx, ec);
return result;
}

} // namespace WebCore
#endif
5 changes: 2 additions & 3 deletions src/webcore/bindings/qjs/QJSHTMLCollectionCustom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ JSValue toJS(JSContext* ctx, HTMLCollection* collection)
if (!collection)
return JS_NULL;

QJS::ScriptInterpreter* interp = static_cast<QJS::ScriptInterpreter*>(JS_GetContextOpaque(ctx));
JSValue ret = QJS::ScriptInterpreter::getDOMObject(interp, collection);
JSValue ret = QJS::ScriptInterpreter::getDOMObject(collection);

if (!JS_IsNull(ret)) {
return ret;
Expand All @@ -146,7 +145,7 @@ JSValue toJS(JSContext* ctx, HTMLCollection* collection)
break;
}

QJS::ScriptInterpreter::putDOMObject(interp, collection, ret);
QJS::ScriptInterpreter::putDOMObject(collection, ret);
return ret;
}

Expand Down
41 changes: 41 additions & 0 deletions src/webcore/bindings/qjs/QJSHTMLElementWrapperFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2024, Zhang Ji Peng <onecoolx@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _JSHTMLElementWrapperFactory_H_
#define _JSHTMLElementWrapperFactory_H_

#if ENABLE(QJS)
#include <wtf/Forward.h>
#include <quickjs.h>

namespace WebCore {
class HTMLElement;

JSValue createJSHTMLWrapper(JSContext*, PassRefPtr<HTMLElement>);
}

#endif
#endif
Loading

0 comments on commit 602591e

Please sign in to comment.