Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: Add Rust native support #554

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if(MSVC)
endif()

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
# set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
endif()

if(WIN32)
Expand Down Expand Up @@ -138,6 +138,7 @@ list(APPEND BRIDGE_INCLUDE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/include
${CMAKE_CURRENT_LIST_DIR}/polyfill/dist
${CMAKE_CURRENT_LIST_DIR}/core_rs/include
${CMAKE_CURRENT_LIST_DIR}/third_party/dart
${ADDITIONAL_INCLUDE_DIRS}
)
Expand Down Expand Up @@ -271,6 +272,16 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs")
core/script_state.cc
core/page.cc
core/dart_methods.cc
core/rust_api/exception_state.cc
core/rust_api/event_target.cc
core/rust_api/node.cc
core/rust_api/executing_context.cc
core/rust_api/container_node.cc
core/rust_api/document.cc
core/rust_api/element.cc
core/rust_api/window.cc
core/rust_api/text.cc
core/rust_api/character_data.cc
core/dart_isolate_context.cc
core/dart_context_data.cc
core/executing_context_data.cc
Expand Down Expand Up @@ -567,6 +578,12 @@ list(APPEND PUBLIC_HEADER
add_library(webf SHARED ${BRIDGE_SOURCE})
add_library(webf_static STATIC ${BRIDGE_SOURCE})

if (${CMAKE_BUILD_TYPE} STREQUAL "Release" OR ${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
target_link_libraries(webf PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/core_rs/target/release/libwebf_core_rs.a)
else ()
target_link_libraries(webf PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/core_rs/target/debug/libwebf_core_rs.a)
endif ()

if(MSVC)
target_compile_options(webf PRIVATE /JMC)
endif()
Expand Down
6 changes: 6 additions & 0 deletions bridge/bindings/qjs/exception_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#include "exception_state.h"
#include "core/rust_api/exception_state.h"

namespace webf {

ExceptionStateRustMethods* ExceptionState::rustMethodPointer() {
static auto* rust_methods = new ExceptionStateRustMethods();
return rust_methods;
}

void ExceptionState::ThrowException(JSContext* ctx, ErrorType type, const std::string& message) {
switch (type) {
case ErrorType::TypeError:
Expand Down
4 changes: 4 additions & 0 deletions bridge/bindings/qjs/exception_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace webf {

class ExceptionStateRustMethods;

enum ErrorType { TypeError, InternalError, RangeError, ReferenceError, SyntaxError };

// ExceptionState is a scope-like class and provides a way to store an exception.
Expand All @@ -21,6 +23,8 @@ class ExceptionState {
WEBF_DISALLOW_NEW();

public:
static ExceptionStateRustMethods* rustMethodPointer();

void ThrowException(JSContext* ctx, ErrorType type, const std::string& message);
void ThrowException(JSContext* ctx, JSValue exception);
bool HasException();
Expand Down
21 changes: 10 additions & 11 deletions bridge/bindings/qjs/script_wrappable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,19 @@ void ScriptWrappable::InitializeQuickJSObject() {
}

void ScriptWrappable::KeepAlive() {
if (is_alive)
return;

context_->RegisterActiveScriptWrappers(this);
JS_DupValue(ctx_, jsObject_);
is_alive = true;
if (alive_count == 0) {
context_->RegisterActiveScriptWrappers(this);
JS_DupValue(ctx_, jsObject_);
}
alive_count++;
}

void ScriptWrappable::ReleaseAlive() {
if (!is_alive)
return;
context_->InActiveScriptWrappers(this);
JS_FreeValue(ctx_, jsObject_);
is_alive = false;
alive_count--;
if (alive_count == 0) {
context_->InActiveScriptWrappers(this);
JS_FreeValue(ctx_, jsObject_);
}
}

} // namespace webf
2 changes: 1 addition & 1 deletion bridge/bindings/qjs/script_wrappable.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ScriptWrappable : public GarbageCollected<ScriptWrappable> {
void ReleaseAlive();

private:
bool is_alive = false;
uint32_t alive_count = 0;
JSValue jsObject_{JS_NULL};
JSContext* ctx_{nullptr};
ExecutingContext* context_{nullptr};
Expand Down
6 changes: 6 additions & 0 deletions bridge/core/dom/character_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ CharacterData::CharacterData(TreeScope& tree_scope, const AtomicString& text, No
assert(type == kCreateOther || type == kCreateText);
}

RustMethods* CharacterData::rustMethodPointer() {
auto* super_rust_method = Node::rustMethodPointer();
static auto* rust_methods = new CharacterDataRustMethods(static_cast<NodeRustMethods*>(super_rust_method));
return rust_methods;
}

} // namespace webf
5 changes: 5 additions & 0 deletions bridge/core/dom/character_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BRIDGE_CHARACTER_DATA_H
#define BRIDGE_CHARACTER_DATA_H

#include "core/rust_api/character_data.h"
#include "node.h"

namespace webf {
Expand All @@ -16,6 +17,8 @@ class CharacterData : public Node {
DEFINE_WRAPPERTYPEINFO();

public:
// static CharacterDataRustMethods* rustMethodPointer();

const AtomicString& data() const { return data_; }
int64_t length() const { return data_.length(); };
void setData(const AtomicString& data, ExceptionState& exception_state);
Expand All @@ -26,6 +29,8 @@ class CharacterData : public Node {
bool IsCharacterDataNode() const override;
void setNodeValue(const AtomicString&, ExceptionState&) override;

RustMethods* rustMethodPointer() override;

protected:
CharacterData(TreeScope& tree_scope, const AtomicString& text, ConstructionType type);

Expand Down
6 changes: 6 additions & 0 deletions bridge/core/dom/container_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,10 @@ void ContainerNode::Trace(GCVisitor* visitor) const {
Node::Trace(visitor);
}

RustMethods* ContainerNode::rustMethodPointer() {
auto* super_rust_method = Node::rustMethodPointer();
static auto* rust_method = new ContainerNodeRustMethods(static_cast<NodeRustMethods*>(super_rust_method));
return rust_method;
}

} // namespace webf
2 changes: 2 additions & 0 deletions bridge/core/dom/container_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "bindings/qjs/cppgc/gc_visitor.h"
#include "bindings/qjs/heap_vector.h"
#include "core/html/collection_type.h"
#include "core/rust_api/container_node.h"
#include "node.h"

namespace webf {
Expand Down Expand Up @@ -151,6 +152,7 @@ class ContainerNode : public Node {
Collection* EnsureCachedCollection(CollectionType);

void Trace(GCVisitor* visitor) const override;
RustMethods* rustMethodPointer() override;

protected:
ContainerNode(TreeScope* tree_scope, ConstructionType = kCreateContainer);
Expand Down
6 changes: 6 additions & 0 deletions bridge/core/dom/document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,10 @@ void Document::Trace(GCVisitor* visitor) const {
ContainerNode::Trace(visitor);
}

RustMethods* Document::rustMethodPointer() {
auto* super_rust_method = ContainerNode::rustMethodPointer();
static auto* rust_method = new DocumentRustMethods(static_cast<ContainerNodeRustMethods*>(super_rust_method));
return rust_method;
}

} // namespace webf
2 changes: 2 additions & 0 deletions bridge/core/dom/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "bindings/qjs/cppgc/local_handle.h"
#include "container_node.h"
#include "core/rust_api/document.h"
#include "event_type_names.h"
#include "scripted_animation_controller.h"
#include "tree_scope.h"
Expand Down Expand Up @@ -126,6 +127,7 @@ class Document : public ContainerNode, public TreeScope {
std::shared_ptr<EventListener> GetWindowAttributeEventListener(const AtomicString& event_type);

void Trace(GCVisitor* visitor) const override;
RustMethods* rustMethodPointer() override;

private:
int node_count_{0};
Expand Down
6 changes: 6 additions & 0 deletions bridge/core/dom/element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ void Element::Trace(GCVisitor* visitor) const {
ContainerNode::Trace(visitor);
}

RustMethods* Element::rustMethodPointer() {
auto* super_rust_methods = ContainerNode::rustMethodPointer();
static auto* rust_methods = new ElementRustMethods(static_cast<ContainerNodeRustMethods*>(super_rust_methods));
return rust_methods;
}

// https://dom.spec.whatwg.org/#concept-element-qualified-name
const AtomicString Element::getUppercasedQualifiedName() const {
auto name = getQualifiedName();
Expand Down
2 changes: 2 additions & 0 deletions bridge/core/dom/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "bindings/qjs/script_promise.h"
#include "container_node.h"
#include "core/css/inline_css_style_declaration.h"
#include "core/rust_api/element.h"
#include "element_data.h"
#include "legacy/bounding_client_rect.h"
#include "legacy/element_attributes.h"
Expand Down Expand Up @@ -143,6 +144,7 @@ class Element : public ContainerNode {
virtual bool IsWidgetElement() const;

void Trace(GCVisitor* visitor) const override;
RustMethods* rustMethodPointer() override;

protected:
void SetAttributeInternal(const AtomicString&,
Expand Down
2 changes: 1 addition & 1 deletion bridge/core/dom/events/event_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class EventListener {

virtual void Trace(GCVisitor* visitor) const = 0;

private:
protected:
EventListener() = default;

friend JSBasedEventListener;
Expand Down
12 changes: 12 additions & 0 deletions bridge/core/dom/events/event_target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ bool EventTarget::addEventListener(const AtomicString& event_type,
return AddEventListenerInternal(event_type, event_listener, options);
}

bool EventTarget::addEventListener(const webf::AtomicString& event_type,
const std::shared_ptr<EventListener>& event_listener,
const std::shared_ptr<AddEventListenerOptions>& options,
ExceptionState& exception_state) {
return AddEventListenerInternal(event_type, event_listener, options);
}

bool EventTarget::removeEventListener(const AtomicString& event_type,
const std::shared_ptr<EventListener>& event_listener,
ExceptionState& exception_state) {
Expand Down Expand Up @@ -246,6 +253,11 @@ bool EventTarget::IsEventTarget() const {
return true;
}

RustMethods* EventTarget::rustMethodPointer() {
static auto* rust_methods = new EventTargetRustMethods();
return rust_methods;
}

void EventTarget::Trace(GCVisitor* visitor) const {
ScriptWrappable::Trace(visitor);
BindingObject::Trace(visitor);
Expand Down
7 changes: 7 additions & 0 deletions bridge/core/dom/events/event_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "bindings/qjs/qjs_function.h"
#include "bindings/qjs/script_wrappable.h"
#include "core/binding_object.h"
#include "core/rust_api/event_target.h"
#include "event_listener_map.h"
#include "foundation/logging.h"
#include "foundation/native_string.h"
Expand Down Expand Up @@ -105,6 +106,10 @@ class EventTarget : public BindingObject {
bool addEventListener(const AtomicString& event_type,
const std::shared_ptr<EventListener>& event_listener,
ExceptionState& exception_state);
bool addEventListener(const AtomicString& event_type,
const std::shared_ptr<EventListener>& event_listener,
const std::shared_ptr<AddEventListenerOptions>& options,
ExceptionState& exception_state);
bool removeEventListener(const AtomicString& event_type,
const std::shared_ptr<EventListener>& event_listener,
ExceptionState& exception_state);
Expand Down Expand Up @@ -135,6 +140,8 @@ class EventTarget : public BindingObject {
virtual bool IsNode() const { return false; }
bool IsEventTarget() const override;

virtual RustMethods* rustMethodPointer();

NativeValue HandleCallFromDartSide(const AtomicString& method,
int32_t argc,
const NativeValue* argv,
Expand Down
6 changes: 6 additions & 0 deletions bridge/core/dom/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,10 @@ void Node::Trace(GCVisitor* visitor) const {
EventTarget::Trace(visitor);
}

RustMethods* Node::rustMethodPointer() {
auto* super_rust_methods = EventTarget::rustMethodPointer();
static auto* rust_methods = new NodeRustMethods(static_cast<EventTargetRustMethods*>(super_rust_methods));
return rust_methods;
}

} // namespace webf
2 changes: 2 additions & 0 deletions bridge/core/dom/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <set>
#include <utility>

#include "core/rust_api/node.h"
#include "events/event_target.h"
#include "foundation/macros.h"
#include "mutation_observer.h"
Expand Down Expand Up @@ -259,6 +260,7 @@ class Node : public EventTarget {
const MutationObserverRegistrationSet* TransientMutationObserverRegistry();

void Trace(GCVisitor*) const override;
RustMethods* rustMethodPointer() override;

private:
enum NodeFlags : uint32_t {
Expand Down
6 changes: 6 additions & 0 deletions bridge/core/dom/text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Node::NodeType Text::nodeType() const {
return Node::kTextNode;
}

RustMethods* Text::rustMethodPointer() {
auto* super_rust_method = CharacterData::rustMethodPointer();
static auto* rust_method = new TextNodeRustMethods(static_cast<CharacterDataRustMethods*>(super_rust_method));
return rust_method;
}

std::string Text::nodeName() const {
return "#text";
}
Expand Down
2 changes: 2 additions & 0 deletions bridge/core/dom/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BRIDGE_CORE_DOM_TEXT_H_

#include "character_data.h"
#include "core/rust_api/text.h"

namespace webf {

Expand All @@ -26,6 +27,7 @@ class Text : public CharacterData {
}

NodeType nodeType() const override;
RustMethods* rustMethodPointer() override;

private:
std::string nodeName() const override;
Expand Down